1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
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 274571 2009-01-25 18:27:39Z iliaa $ */
19 :
20 : #include <string.h>
21 :
22 : #include "php.h"
23 : #include "base64.h"
24 :
25 : /* {{{ */
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 : /* {{{ php_base64_encode */
57 : PHPAPI unsigned char *php_base64_encode(const unsigned char *str, int length, int *ret_length)
58 653 : {
59 653 : const unsigned char *current = str;
60 : unsigned char *p;
61 : unsigned char *result;
62 :
63 653 : if ((length + 2) < 0 || ((length + 2) / 3) >= (1 << (sizeof(int) * 8 - 2))) {
64 0 : if (ret_length != NULL) {
65 0 : *ret_length = 0;
66 : }
67 0 : return NULL;
68 : }
69 :
70 653 : result = (unsigned char *)safe_emalloc(((length + 2) / 3) * 4, sizeof(char), 1);
71 653 : p = result;
72 :
73 7360 : while (length > 2) { /* keep going until we have less than 24 bits */
74 6054 : *p++ = base64_table[current[0] >> 2];
75 6054 : *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
76 6054 : *p++ = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
77 6054 : *p++ = base64_table[current[2] & 0x3f];
78 :
79 6054 : current += 3;
80 6054 : length -= 3; /* we just handle 3 octets of data */
81 : }
82 :
83 : /* now deal with the tail end of things */
84 653 : if (length != 0) {
85 571 : *p++ = base64_table[current[0] >> 2];
86 571 : if (length > 1) {
87 136 : *p++ = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
88 136 : *p++ = base64_table[(current[1] & 0x0f) << 2];
89 136 : *p++ = base64_pad;
90 : } else {
91 435 : *p++ = base64_table[(current[0] & 0x03) << 4];
92 435 : *p++ = base64_pad;
93 435 : *p++ = base64_pad;
94 : }
95 : }
96 653 : if (ret_length != NULL) {
97 653 : *ret_length = (int)(p - result);
98 : }
99 653 : *p = '\0';
100 653 : return result;
101 : }
102 : /* }}} */
103 :
104 : /* {{{ */
105 : /* generate reverse table (do not set index 0 to 64)
106 : static unsigned short base64_reverse_table[256];
107 : #define rt base64_reverse_table
108 : void php_base64_init(void)
109 : {
110 : char *s = emalloc(10240), *sp;
111 : char *chp;
112 : short idx;
113 :
114 : for(ch = 0; ch < 256; ch++) {
115 : chp = strchr(base64_table, ch);
116 : if(ch && chp) {
117 : idx = chp - base64_table;
118 : if (idx >= 64) idx = -1;
119 : rt[ch] = idx;
120 : } else {
121 : rt[ch] = -1;
122 : }
123 : }
124 : sp = s;
125 : sprintf(sp, "static const short base64_reverse_table[256] = {\n");
126 : for(ch =0; ch < 256;) {
127 : sp = s+strlen(s);
128 : 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]);
129 : ch += 16;
130 : }
131 : sprintf(sp, "};");
132 : php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Reverse_table:\n%s", s);
133 : efree(s);
134 : }
135 : */
136 : /* }}} */
137 :
138 : PHPAPI unsigned char *php_base64_decode(const unsigned char *str, int length, int *ret_length)
139 118 : {
140 118 : return php_base64_decode_ex(str, length, ret_length, 0);
141 : }
142 :
143 : /* {{{ php_base64_decode */
144 : /* as above, but backwards. :) */
145 : PHPAPI unsigned char *php_base64_decode_ex(const unsigned char *str, int length, int *ret_length, zend_bool strict)
146 417 : {
147 417 : const unsigned char *current = str;
148 417 : int ch, i = 0, j = 0, k;
149 : /* this sucks for threaded environments */
150 : unsigned char *result;
151 :
152 417 : result = (unsigned char *)emalloc(length + 1);
153 :
154 : /* run through the whole string, converting as we go */
155 15328 : while ((ch = *current++) != '\0' && length-- > 0) {
156 14512 : if (ch == base64_pad) {
157 467 : if (*current != '=' && (i % 4) == 1) {
158 1 : efree(result);
159 1 : return NULL;
160 : }
161 466 : continue;
162 : }
163 :
164 14045 : ch = base64_reverse_table[ch];
165 14045 : if ((!strict && ch < 0) || ch == -1) { /* a space or some other separator character, we simply skip over */
166 : continue;
167 13763 : } else if (ch == -2) {
168 17 : efree(result);
169 17 : return NULL;
170 : }
171 :
172 13746 : switch(i % 4) {
173 : case 0:
174 3559 : result[j] = ch << 2;
175 3559 : break;
176 : case 1:
177 3551 : result[j++] |= ch >> 4;
178 3551 : result[j] = (ch & 0x0f) << 4;
179 3551 : break;
180 : case 2:
181 3369 : result[j++] |= ch >>2;
182 3369 : result[j] = (ch & 0x03) << 6;
183 3369 : break;
184 : case 3:
185 3267 : result[j++] |= ch;
186 : break;
187 : }
188 13746 : i++;
189 : }
190 :
191 399 : k = j;
192 : /* mop things up if we ended on a boundary */
193 399 : if (ch == base64_pad) {
194 0 : switch(i % 4) {
195 : case 1:
196 0 : efree(result);
197 0 : return NULL;
198 : case 2:
199 0 : k++;
200 : case 3:
201 0 : result[k++] = 0;
202 : }
203 : }
204 399 : if(ret_length) {
205 399 : *ret_length = j;
206 : }
207 399 : result[j] = '\0';
208 399 : return result;
209 : }
210 : /* }}} */
211 :
212 : /* {{{ proto string base64_encode(string str)
213 : Encodes string using MIME base64 algorithm */
214 : PHP_FUNCTION(base64_encode)
215 491 : {
216 : char *str;
217 : unsigned char *result;
218 : int str_len, ret_length;
219 :
220 491 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
221 8 : return;
222 : }
223 483 : result = php_base64_encode(str, str_len, &ret_length);
224 483 : if (result != NULL) {
225 483 : RETVAL_STRINGL(result, ret_length, 0);
226 : } else {
227 0 : RETURN_FALSE;
228 : }
229 : }
230 : /* }}} */
231 :
232 :
233 : /* {{{ proto string base64_decode(string str[, bool strict])
234 : Decodes string using MIME base64 algorithm */
235 : PHP_FUNCTION(base64_decode)
236 315 : {
237 : char *str;
238 : unsigned char *result;
239 315 : zend_bool strict = 0;
240 : int str_len, ret_length;
241 :
242 315 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &str, &str_len, &strict) == FAILURE) {
243 16 : return;
244 : }
245 299 : result = php_base64_decode_ex(str, str_len, &ret_length, strict);
246 299 : if (result != NULL) {
247 282 : RETVAL_STRINGL(result, ret_length, 0);
248 : } else {
249 17 : RETURN_FALSE;
250 : }
251 : }
252 : /* }}} */
253 :
254 :
255 : /*
256 : * Local variables:
257 : * tab-width: 4
258 : * c-basic-offset: 4
259 : * End:
260 : * vim600: sw=4 ts=4 fdm=marker
261 : * vim<600: sw=4 ts=4
262 : */
|