1617148749e1a69c4695d0dddebcc3aa42f78667
[m6w6/ext-http] / http_encoding_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "php_http_encoding_api.h"
24 #include "php_http.h"
25 #include "php_http_api.h"
26
27 ZEND_EXTERN_MODULE_GLOBALS(http);
28
29 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
30 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
31 {
32 const char *e_ptr;
33 char *d_ptr;
34 long rest;
35
36 *decoded_len = 0;
37 *decoded = ecalloc(1, encoded_len);
38 d_ptr = *decoded;
39 e_ptr = encoded;
40
41 while ((rest = encoded + encoded_len - e_ptr) > 0) {
42 long chunk_len = 0;
43 int EOL_len = 0, eol_mismatch = 0;
44 char *n_ptr;
45
46 chunk_len = strtol(e_ptr, &n_ptr, 16);
47
48 /* check if:
49 * - we could not read in chunk size
50 * - we got a negative chunk size
51 * - chunk size is greater then remaining size
52 * - chunk size is not followed by (CR)LF|NUL
53 */
54 if ( (n_ptr == e_ptr) || (chunk_len < 0) || (chunk_len > rest) ||
55 (*n_ptr && (eol_mismatch = (n_ptr != http_locate_eol(e_ptr, &EOL_len))))) {
56 /* don't fail on apperently not encoded data */
57 if (e_ptr == encoded) {
58 memcpy(*decoded, encoded, encoded_len);
59 *decoded_len = encoded_len;
60 return encoded + encoded_len;
61 } else {
62 efree(*decoded);
63 if (eol_mismatch) {
64 if (EOL_len == 2) {
65 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0D 0x0A; got: 0x%X 0x%X)", *n_ptr, *(n_ptr + 1));
66 } else {
67 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0A; got: 0x%X)", *n_ptr);
68 }
69 } else {
70 char *error = estrndup(n_ptr, strcspn(n_ptr, "\r\n "));
71 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid chunk size: '%s' at pos %d", error, n_ptr - encoded);
72 efree(error);
73 }
74 return NULL;
75 }
76 } else {
77 e_ptr = n_ptr;
78 }
79
80 /* reached the end */
81 if (!chunk_len) {
82 break;
83 }
84
85 memcpy(d_ptr, e_ptr += EOL_len, chunk_len);
86 d_ptr += chunk_len;
87 e_ptr += chunk_len + EOL_len;
88 *decoded_len += chunk_len;
89 }
90
91 return e_ptr;
92 }
93 /* }}} */
94
95 #ifdef HTTP_HAVE_ZLIB
96 #include <zlib.h>
97
98 /* max count of uncompress trials, alloc_size <<= 2 for each try */
99 #define HTTP_GZMAXTRY 10
100 /* safe padding */
101 #define HTTP_GZSAFPAD 10
102 /* add 1% extra space in case we need to encode widely differing (binary) data */
103 #define HTTP_GZBUFLEN(l) (l + (l / 100) + HTTP_GZSAFPAD)
104
105 static const char http_gzencode_header[] = {
106 (const char) 0x1f,
107 (const char) 0x8b,
108 (const char) Z_DEFLATED,
109 0, 0, 0, 0, 0, 0,
110 (const char) 0x03
111 };
112
113 inline void http_init_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
114 {
115 Z->zalloc = Z_NULL;
116 Z->zfree = Z_NULL;
117 Z->opaque = Z_NULL;
118
119 Z->next_in = (Bytef *) data;
120 Z->avail_in = data_len;
121 Z->avail_out = HTTP_GZBUFLEN(data_len) + HTTP_GZSAFPAD - 1;
122
123 *buf_ptr = emalloc(HTTP_GZBUFLEN(data_len) + sizeof(http_gzencode_header) + HTTP_GZSAFPAD);
124 memcpy(*buf_ptr, http_gzencode_header, sizeof(http_gzencode_header));
125
126 Z->next_out = *buf_ptr + sizeof(http_gzencode_header);
127 }
128
129 inline void http_init_deflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
130 {
131 Z->zalloc = Z_NULL;
132 Z->zfree = Z_NULL;
133 Z->opaque = Z_NULL;
134
135 Z->data_type = Z_UNKNOWN;
136 Z->next_in = (Bytef *) data;
137 Z->avail_in = data_len;
138 Z->avail_out = HTTP_GZBUFLEN(data_len) - 1;
139 Z->next_out = emalloc(HTTP_GZBUFLEN(data_len));
140
141 *buf_ptr = Z->next_out;
142 }
143
144 inline void http_init_uncompress_buffer(size_t data_len, char **buf_ptr, size_t *buf_len, int iteration)
145 {
146 if (!iteration) {
147 *buf_len = data_len * 2;
148 *buf_ptr = emalloc(*buf_len + 1);
149 } else {
150 *buf_len <<= 2;
151 *buf_ptr = erealloc(*buf_ptr, *buf_len + 1);
152 }
153 }
154
155 inline void http_init_inflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr, size_t *buf_len, int iteration)
156 {
157 Z->zalloc = Z_NULL;
158 Z->zfree = Z_NULL;
159
160 http_init_uncompress_buffer(data_len, buf_ptr, buf_len, iteration);
161
162 Z->next_in = (Bytef *) data;
163 Z->avail_in = data_len;
164 Z->avail_out = *buf_len;
165 Z->next_out = *buf_ptr;
166 }
167
168 inline size_t http_finish_buffer(size_t buf_len, char **buf_ptr)
169 {
170 (*buf_ptr)[buf_len] = '\0';
171 return buf_len;
172 }
173
174 inline size_t http_finish_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
175 {
176 unsigned long crc;
177 char *trailer;
178
179 crc = crc32(0L, Z_NULL, 0);
180 crc = crc32(crc, (const Bytef *) data, data_len);
181
182 trailer = *buf_ptr + sizeof(http_gzencode_header) + Z->total_out;
183
184 /* LSB */
185 trailer[0] = (char) (crc & 0xFF);
186 trailer[1] = (char) ((crc >> 8) & 0xFF);
187 trailer[2] = (char) ((crc >> 16) & 0xFF);
188 trailer[3] = (char) ((crc >> 24) & 0xFF);
189 trailer[4] = (char) ((Z->total_in) & 0xFF);
190 trailer[5] = (char) ((Z->total_in >> 8) & 0xFF);
191 trailer[6] = (char) ((Z->total_in >> 16) & 0xFF);
192 trailer[7] = (char) ((Z->total_in >> 24) & 0xFF);
193
194 return http_finish_buffer(Z->total_out + sizeof(http_gzencode_header) + 8, buf_ptr);
195 }
196
197 inline STATUS http_verify_gzdecode_buffer(const char *data, size_t data_len, const char *decoded, size_t decoded_len, int error_level TSRMLS_DC)
198 {
199 STATUS status = SUCCESS;
200 unsigned long len, cmp, crc;
201
202 crc = crc32(0L, Z_NULL, 0);
203 crc = crc32(crc, (const Bytef *) decoded, decoded_len);
204
205 cmp = (unsigned) ((data[data_len-8] & 0xFF));
206 cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
207 cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
208 cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
209 len = (unsigned) ((data[data_len-4] & 0xFF));
210 len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
211 len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
212 len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
213
214 if (cmp != crc) {
215 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
216 status = FAILURE;
217 }
218 if (len != decoded_len) {
219 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: data sizes do not match (%lu, %lu)", len, decoded_len);
220 status = FAILURE;
221 }
222 return status;
223 }
224
225 PHP_HTTP_API STATUS _http_encoding_gzencode(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
226 {
227 z_stream Z;
228 STATUS status = Z_OK;
229
230 http_init_gzencode_buffer(&Z, data, data_len, encoded);
231
232 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
233 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
234 (Z_OK == (status = deflateEnd(&Z)))) {
235 *encoded_len = http_finish_gzencode_buffer(&Z, data, data_len, encoded);
236 return SUCCESS;
237 }
238
239 efree(*encoded);
240 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not gzencode data: %s", zError(status));
241 return FAILURE;
242 }
243
244 PHP_HTTP_API STATUS _http_encoding_deflate(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
245 {
246 z_stream Z;
247 STATUS status = Z_OK;
248
249 http_init_deflate_buffer(&Z, data, data_len, encoded);
250
251 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
252 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
253 (Z_OK == (status = deflateEnd(&Z)))) {
254 *encoded_len = http_finish_buffer(Z.total_out, encoded);
255 return SUCCESS;
256 }
257
258 efree(encoded);
259 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
260 return FAILURE;
261 }
262
263 PHP_HTTP_API STATUS _http_encoding_compress(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
264 {
265 STATUS status;
266
267 *encoded = emalloc(*encoded_len = HTTP_GZBUFLEN(data_len));
268
269 if (Z_OK == (status = compress2(*encoded, encoded_len, data, data_len, level))) {
270 http_finish_buffer(*encoded_len, encoded);
271 return SUCCESS;
272 }
273
274 efree(encoded);
275 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not compress data: %s", zError(status));
276 return FAILURE;
277 }
278
279 PHP_HTTP_API STATUS _http_encoding_gzdecode(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
280 {
281 const char *encoded = data + sizeof(http_gzencode_header);
282 size_t encoded_len;
283
284 if (data_len <= sizeof(http_gzencode_header) + 8) {
285 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not gzdecode data: too short data length");
286 } else {
287 encoded_len = data_len - sizeof(http_gzencode_header) - 8;
288
289 if (SUCCESS == http_encoding_inflate(encoded, encoded_len, decoded, decoded_len)) {
290 http_verify_gzdecode_buffer(data, data_len, *decoded, *decoded_len, HE_NOTICE);
291 return SUCCESS;
292 }
293 }
294 return FAILURE;
295 }
296
297 PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
298 {
299 int max = 0;
300 STATUS status;
301 z_stream Z;
302
303 do {
304 http_init_inflate_buffer(&Z, data, data_len, decoded, decoded_len, max++);
305 if (Z_OK == (status = inflateInit2(&Z, -MAX_WBITS))) {
306 if (Z_STREAM_END == (status = inflate(&Z, Z_FINISH))) {
307 if (Z_OK == (status = inflateEnd(&Z))) {
308 *decoded_len = http_finish_buffer(Z.total_out, decoded);
309 return SUCCESS;
310 }
311 }
312 }
313 } while (max < HTTP_GZMAXTRY && status == Z_BUF_ERROR);
314
315 efree(*decoded);
316 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
317 return FAILURE;
318 }
319
320 PHP_HTTP_API STATUS _http_encoding_uncompress(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
321 {
322 int max = 0;
323 STATUS status;
324
325 do {
326 http_init_uncompress_buffer(data_len, decoded, decoded_len, max++);
327 if (Z_OK == (status = uncompress(*decoded, decoded_len, data, data_len))) {
328 http_finish_buffer(*decoded_len, decoded);
329 return SUCCESS;
330 }
331 } while (max < HTTP_GZMAXTRY && status == Z_BUF_ERROR);
332
333 efree(*decoded);
334 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not uncompress data: %s", zError(status));
335 return FAILURE;
336 }
337
338 #endif /* HTTP_HAVE_ZLIB */
339
340 /*
341 * Local variables:
342 * tab-width: 4
343 * c-basic-offset: 4
344 * End:
345 * vim600: noet sw=4 ts=4 fdm=marker
346 * vim<600: noet sw=4 ts=4
347 */
348