1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: base64.c 284206 2009-07-16 22:19:09Z andrei $ */
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 :
56 : PHPAPI unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length) /* {{{ */
57 434 : {
58 434 : const unsigned char *current = str;
59 : unsigned char *p;
60 : unsigned char *result;
61 :
62 434 : if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) {
63 0 : if (ret_length != NULL) {
64 0 : *ret_length = 0;
65 : }
66 0 : return NULL;
67 : }
68 :
69 434 : result = (unsigned char *)safe_emalloc(((length + 2) / 3) * 4, sizeof(char), 1);
70 434 : p = result;
71 :
72 4904 : while (length > 2) { /* keep going until we have less than 24 bits */
73 4036 : *p++ = base64_table[current[0] >> 2];
74 4036 : *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
75 4036 : *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
76 4036 : *p++ = base64_table[current[2] & 0x3f];
77 :
78 4036 : current += 3;
79 4036 : length -= 3; /* we just handle 3 octets of data */
80 : }
81 :
82 : /* now deal with the tail end of things */
83 434 : if (length != 0) {
84 371 : *p++ = base64_table[current[0] >> 2];
85 371 : if (length > 1) {
86 47 : *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
87 47 : *p++ = base64_table[(current[1] & 0x0f) << 2];
88 47 : *p++ = base64_pad;
89 : } else {
90 324 : *p++ = base64_table[(current[0] & 0x03) << 4];
91 324 : *p++ = base64_pad;
92 324 : *p++ = base64_pad;
93 : }
94 : }
95 434 : if (ret_length != NULL) {
96 434 : *ret_length = (int)(p - result);
97 : }
98 434 : *p = '\0';
99 434 : return result;
100 : }
101 : /* }}} */
102 :
103 : /* {{{ */
104 : /* generate reverse table (do not set index 0 to 64)
105 : static unsigned short base64_reverse_table[256];
106 : #define rt base64_reverse_table
107 : void php_base64_init(void)
108 : {
109 : char *s = emalloc(10240), *sp;
110 : char *chp;
111 : short idx;
112 :
113 : for(ch = 0; ch < 256; ch++) {
114 : chp = strchr(base64_table, ch);
115 : if(ch && chp) {
116 : idx = chp - base64_table;
117 : if (idx >= 64) idx = -1;
118 : rt[ch] = idx;
119 : } else {
120 : rt[ch] = -1;
121 : }
122 : }
123 : sp = s;
124 : sprintf(sp, "static const short base64_reverse_table[256] = {\n");
125 : for(ch =0; ch < 256;) {
126 : sp = s+strlen(s);
127 : 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]);
128 : ch += 16;
129 : }
130 : sprintf(sp, "};");
131 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Reverse_table:\n%s", s);
132 : efree(s);
133 : }
134 : */
135 : /* }}} */
136 :
137 : PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length) /* {{{ */
138 119 : {
139 119 : return php_base64_decode_ex(str, length, ret_length, 0);
140 : }
141 : /* }}} */
142 :
143 : PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict) /* {{{ */
144 386 : {
145 386 : const unsigned char *current = str;
146 386 : int ch, i = 0, j = 0, k;
147 : /* this sucks for threaded environments */
148 : unsigned char *result;
149 :
150 386 : result = (unsigned char *)safe_emalloc(length, 1, 1);
151 :
152 : /* run through the whole string, converting as we go */
153 13909 : while ((ch = *current++) != '\0' && length-- > 0) {
154 13154 : if (ch == base64_pad) {
155 443 : if (*current != '=' && (i % 4) == 1) {
156 1 : efree(result);
157 1 : return NULL;
158 : }
159 442 : continue;
160 : }
161 :
162 12711 : ch = base64_reverse_table[ch];
163 12711 : if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
164 : continue;
165 12429 : } else if (ch == -2) {
166 16 : efree(result);
167 16 : return NULL;
168 : }
169 :
170 12413 : switch(i % 4) {
171 : case 0:
172 3219 : result[j] = ch << 2;
173 3219 : break;
174 : case 1:
175 3211 : result[j++] |= ch >> 4;
176 3211 : result[j] = (ch & 0x0f) << 4;
177 3211 : break;
178 : case 2:
179 3039 : result[j++] |= ch >>2;
180 3039 : result[j] = (ch & 0x03) << 6;
181 3039 : break;
182 : case 3:
183 2944 : result[j++] |= ch;
184 : break;
185 : }
186 12413 : i++;
187 : }
188 :
189 369 : k = j;
190 : /* mop things up if we ended on a boundary */
191 369 : if (ch == base64_pad) {
192 0 : switch(i % 4) {
193 : case 1:
194 0 : efree(result);
195 0 : return NULL;
196 : case 2:
197 0 : k++;
198 : case 3:
199 0 : result[k] = 0;
200 : }
201 : }
202 369 : if(ret_length) {
203 369 : *ret_length = j;
204 : }
205 369 : result[j] = '\0';
206 369 : return result;
207 : }
208 : /* }}} */
209 :
210 : /* {{{ proto binary base64_encode(binary str) U
211 : Encodes string using MIME base64 algorithm */
212 : PHP_FUNCTION(base64_encode)
213 432 : {
214 : zstr str;
215 : unsigned char *result;
216 : int str_len, ret_length;
217 : zend_uchar str_type;
218 :
219 432 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &str, &str_len, &str_type) == FAILURE) {
220 8 : return;
221 : }
222 :
223 424 : if (str_type == IS_UNICODE) {
224 21 : char *utf8_str = NULL;
225 : int utf8_str_len;
226 21 : UErrorCode status = U_ZERO_ERROR;
227 :
228 21 : zend_unicode_to_string_ex(UG(utf8_conv), &utf8_str, &utf8_str_len, str.u, str_len, &status);
229 21 : if (U_FAILURE(status)) {
230 0 : if (utf8_str) {
231 0 : efree(utf8_str);
232 : }
233 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "could not convert argument to UTF-8");
234 0 : RETURN_FALSE;
235 : }
236 :
237 21 : result = php_base64_encode((unsigned char*)utf8_str, utf8_str_len, &ret_length);
238 21 : efree(utf8_str);
239 21 : php_error_docref(NULL TSRMLS_CC, E_STRICT, "expecting binary parameter, received Unicode parameter was converted to UTF-8");
240 : } else {
241 403 : result = php_base64_encode((unsigned char*)str.s, str_len, &ret_length);
242 : }
243 :
244 424 : if (result != NULL) {
245 424 : RETVAL_STRINGL((char*)result, ret_length, 0);
246 : } else {
247 0 : RETURN_FALSE;
248 : }
249 : }
250 : /* }}} */
251 :
252 : /* {{{ proto binary base64_decode(binary str[, bool strict]) U
253 : Decodes string using MIME base64 algorithm */
254 : PHP_FUNCTION(base64_decode)
255 283 : {
256 : char *str;
257 : unsigned char *result;
258 283 : zend_bool strict = 0;
259 : int str_len, ret_length;
260 :
261 283 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &strict) == FAILURE) {
262 16 : return;
263 : }
264 267 : result = php_base64_decode_ex((unsigned char*)str, str_len, &ret_length, strict);
265 267 : if (result != NULL) {
266 251 : RETVAL_STRINGL((char*)result, ret_length, 0);
267 : } else {
268 16 : RETURN_FALSE;
269 : }
270 : }
271 : /* }}} */
272 :
273 : /*
274 : * Local variables:
275 : * tab-width: 4
276 : * c-basic-offset: 4
277 : * End:
278 : * vim600: sw=4 ts=4 fdm=marker
279 : * vim<600: sw=4 ts=4
280 : */
|