xref: /PHP-5.6/ext/standard/base64.c (revision 40bd921c)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 5                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2016 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Jim Winstead <jimw@php.net>                                  |
16    +----------------------------------------------------------------------+
17  */
18 /* $Id$ */
19 
20 #include <string.h>
21 
22 #include "php.h"
23 #include "base64.h"
24 
25 /* {{{ base64 tables */
26 static const char base64_table[] = {
27 	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
28 	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
29 	'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
30 	'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
31 	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
32 };
33 
34 static const char base64_pad = '=';
35 
36 static const short base64_reverse_table[256] = {
37 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -2, -1, -2, -2,
38 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
39 	-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
40 	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
41 	-2,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
42 	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
43 	-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
44 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
45 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
46 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
47 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
48 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
49 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
50 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
51 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
52 	-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
53 };
54 /* }}} */
55 
php_base64_encode(const unsigned char * str,int length,int * ret_length)56 PHPAPI unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length) /* {{{ */
57 {
58 	const unsigned char *current = str;
59 	unsigned char *p;
60 	unsigned char *result;
61 
62 	if (length < 0) {
63 		if (ret_length != NULL) {
64 			*ret_length = 0;
65 		}
66 		return NULL;
67 	}
68 
69 	if (((size_t)length + 2) / 3 > INT_MAX/4 ) {
70 		TSRMLS_FETCH();
71 		php_error_docref(NULL TSRMLS_CC, E_WARNING, "String too long, maximum is %d", INT_MAX/4);
72 		return NULL;
73 	}
74 
75 	result = (unsigned char *) safe_emalloc((length + 2) / 3, 4 * sizeof(char), 1);
76 	p = result;
77 
78 	while (length > 2) { /* keep going until we have less than 24 bits */
79 		*p++ = base64_table[current[0] >> 2];
80 		*p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
81 		*p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
82 		*p++ = base64_table[current[2] & 0x3f];
83 
84 		current += 3;
85 		length -= 3; /* we just handle 3 octets of data */
86 	}
87 
88 	/* now deal with the tail end of things */
89 	if (length != 0) {
90 		*p++ = base64_table[current[0] >> 2];
91 		if (length > 1) {
92 			*p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
93 			*p++ = base64_table[(current[1] & 0x0f) << 2];
94 			*p++ = base64_pad;
95 		} else {
96 			*p++ = base64_table[(current[0] & 0x03) << 4];
97 			*p++ = base64_pad;
98 			*p++ = base64_pad;
99 		}
100 	}
101 	if (ret_length != NULL) {
102 		*ret_length = (int)(p - result);
103 	}
104 	*p = '\0';
105 	return result;
106 }
107 /* }}} */
108 
109 /* {{{ */
110 /* generate reverse table (do not set index 0 to 64)
111 static unsigned short base64_reverse_table[256];
112 #define rt base64_reverse_table
113 void php_base64_init(void)
114 {
115 	char *s = emalloc(10240), *sp;
116 	char *chp;
117 	short idx;
118 
119 	for(ch = 0; ch < 256; ch++) {
120 		chp = strchr(base64_table, ch);
121 		if(ch && chp) {
122 			idx = chp - base64_table;
123 			if (idx >= 64) idx = -1;
124 			rt[ch] = idx;
125 		} else {
126 			rt[ch] = -1;
127 		}
128 	}
129 	sp = s;
130 	sprintf(sp, "static const short base64_reverse_table[256] = {\n");
131 	for(ch =0; ch < 256;) {
132 		sp = s+strlen(s);
133 		sprintf(sp, "\t% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,% 3d,\n", rt[ch+0], rt[ch+1], rt[ch+2], rt[ch+3], rt[ch+4], rt[ch+5], rt[ch+6], rt[ch+7], rt[ch+8], rt[ch+9], rt[ch+10], rt[ch+11], rt[ch+12], rt[ch+13], rt[ch+14], rt[ch+15]);
134 		ch += 16;
135 	}
136 	sprintf(sp, "};");
137 	php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Reverse_table:\n%s", s);
138 	efree(s);
139 }
140 */
141 /* }}} */
142 
php_base64_decode(const unsigned char * str,int length,int * ret_length)143 PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length) /* {{{ */
144 {
145 	return php_base64_decode_ex(str, length, ret_length, 0);
146 }
147 /* }}} */
148 
php_base64_decode_ex(const unsigned char * str,int length,int * ret_length,zend_bool strict)149 PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict) /* {{{ */
150 {
151 	const unsigned char *current = str;
152 	int ch, i = 0, j = 0, k;
153 	/* this sucks for threaded environments */
154 	unsigned char *result;
155 
156 	result = (unsigned char *)safe_emalloc(length, 1, 1);
157 
158 	/* run through the whole string, converting as we go */
159 	while ((ch = *current++) != '\0' && length-- > 0) {
160 		if (ch == base64_pad) {
161 			if (*current != '=' && ((i % 4) == 1 || (strict && length > 0))) {
162 				if ((i % 4) != 1) {
163 					while (isspace(*(++current))) {
164 						continue;
165 					}
166 					if (*current == '\0') {
167 						continue;
168 					}
169 				}
170 				efree(result);
171 				return NULL;
172 			}
173 			continue;
174 		}
175 
176 		ch = base64_reverse_table[ch];
177 		if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
178 			continue;
179 		} else if (ch == -2) {
180 			efree(result);
181 			return NULL;
182 		}
183 
184 		switch(i % 4) {
185 		case 0:
186 			result[j] = ch << 2;
187 			break;
188 		case 1:
189 			result[j++] |= ch >> 4;
190 			result[j] = (ch & 0x0f) << 4;
191 			break;
192 		case 2:
193 			result[j++] |= ch >>2;
194 			result[j] = (ch & 0x03) << 6;
195 			break;
196 		case 3:
197 			result[j++] |= ch;
198 			break;
199 		}
200 		i++;
201 	}
202 
203 	k = j;
204 	/* mop things up if we ended on a boundary */
205 	if (ch == base64_pad) {
206 		switch(i % 4) {
207 		case 1:
208 			efree(result);
209 			return NULL;
210 		case 2:
211 			k++;
212 		case 3:
213 			result[k] = 0;
214 		}
215 	}
216 	if(ret_length) {
217 		*ret_length = j;
218 	}
219 	result[j] = '\0';
220 	return result;
221 }
222 /* }}} */
223 
224 /* {{{ proto string base64_encode(string str)
225    Encodes string using MIME base64 algorithm */
PHP_FUNCTION(base64_encode)226 PHP_FUNCTION(base64_encode)
227 {
228 	char *str;
229 	unsigned char *result;
230 	int str_len, ret_length;
231 
232 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
233 		return;
234 	}
235 	result = php_base64_encode((unsigned char*)str, str_len, &ret_length);
236 	if (result != NULL) {
237 		RETVAL_STRINGL((char*)result, ret_length, 0);
238 	} else {
239 		RETURN_FALSE;
240 	}
241 }
242 /* }}} */
243 
244 /* {{{ proto string base64_decode(string str[, bool strict])
245    Decodes string using MIME base64 algorithm */
PHP_FUNCTION(base64_decode)246 PHP_FUNCTION(base64_decode)
247 {
248 	char *str;
249 	unsigned char *result;
250 	zend_bool strict = 0;
251 	int str_len, ret_length;
252 
253 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &strict) == FAILURE) {
254 		return;
255 	}
256 	result = php_base64_decode_ex((unsigned char*)str, str_len, &ret_length, strict);
257 	if (result != NULL) {
258 		RETVAL_STRINGL((char*)result, ret_length, 0);
259 	} else {
260 		RETURN_FALSE;
261 	}
262 }
263 /* }}} */
264 
265 /*
266  * Local variables:
267  * tab-width: 4
268  * c-basic-offset: 4
269  * End:
270  * vim600: sw=4 ts=4 fdm=marker
271  * vim<600: sw=4 ts=4
272  */
273