- ws
[m6w6/ext-http] / http_encoding_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_ZLIB
20 #include "php_http.h"
21
22 #include "php_http_api.h"
23 #include "php_http_encoding_api.h"
24 #include "php_http_send_api.h"
25 #include "php_http_headers_api.h"
26
27 #ifdef HTTP_HAVE_ZLIB
28 PHP_MINIT_FUNCTION(http_encoding)
29 {
30 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF);
31 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN);
32 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX);
33 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB);
34 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP);
35 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_RAW", HTTP_DEFLATE_TYPE_RAW);
36 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF);
37 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT);
38 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF);
39 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE);
40 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED);
41 return SUCCESS;
42 }
43
44 PHP_RINIT_FUNCTION(http_encoding)
45 {
46 getGlobals(G);
47
48 if (G->send.inflate.start_auto) {
49 php_ob_set_internal_handler(_http_ob_inflatehandler, 0x1000, "http inflate", 0 TSRMLS_CC);
50 }
51 if (G->send.deflate.start_auto) {
52 php_ob_set_internal_handler(_http_ob_deflatehandler, 0x8000, "http deflate", 0 TSRMLS_CC);
53 }
54 return SUCCESS;
55 }
56
57 PHP_RSHUTDOWN_FUNCTION(http_encoding)
58 {
59 getGlobals(G);
60
61 if (G->send.deflate.stream) {
62 http_encoding_deflate_stream_free((http_encoding_stream **) &G->send.deflate.stream);
63 }
64 if (G->send.inflate.stream) {
65 http_encoding_inflate_stream_free((http_encoding_stream **) &G->send.inflate.stream);
66 }
67 return SUCCESS;
68 }
69 #endif
70
71 static inline int eol_match(char **line, int *eol_len)
72 {
73 char *ptr = *line;
74
75 while (0x20 == *ptr) ++ptr;
76
77 if (ptr == http_locate_eol(*line, eol_len)) {
78 *line = ptr;
79 return 1;
80 } else {
81 return 0;
82 }
83 }
84
85 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
86 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
87 {
88 int eol_len = 0;
89 char *n_ptr = NULL;
90 const char *e_ptr = encoded;
91
92 *decoded_len = 0;
93 *decoded = ecalloc(1, encoded_len);
94
95 while ((encoded + encoded_len - e_ptr) > 0) {
96 ulong chunk_len = 0, rest;
97
98 chunk_len = strtoul(e_ptr, &n_ptr, 16);
99
100 /* we could not read in chunk size */
101 if (n_ptr == e_ptr) {
102 /*
103 * if this is the first turn and there doesn't seem to be a chunk
104 * size at the begining of the body, do not fail on apparently
105 * not encoded data and return a copy
106 */
107 if (e_ptr == encoded) {
108 http_error(HE_NOTICE, HTTP_E_ENCODING, "Data does not seem to be chunked encoded");
109 memcpy(*decoded, encoded, encoded_len);
110 *decoded_len = encoded_len;
111 return encoded + encoded_len;
112 } else {
113 efree(*decoded);
114 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Expected chunk size at pos %tu of %zu but got trash", n_ptr - encoded, encoded_len);
115 return NULL;
116 }
117 }
118
119 /* reached the end */
120 if (!chunk_len) {
121 /* move over '0' chunked encoding terminator */
122 while (*e_ptr == '0') ++e_ptr;
123 break;
124 }
125
126 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
127 if (*n_ptr && !eol_match(&n_ptr, &eol_len)) {
128 if (eol_len == 2) {
129 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Expected CRLF at pos %tu of %zu but got 0x%02X 0x%02X", n_ptr - encoded, encoded_len, *n_ptr, *(n_ptr + 1));
130 } else {
131 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Expected LF at pos %tu of %zu but got 0x%02X", n_ptr - encoded, encoded_len, *n_ptr);
132 }
133 }
134 n_ptr += eol_len;
135
136 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
137 if (chunk_len > (rest = encoded + encoded_len - n_ptr)) {
138 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Truncated message: chunk size %lu exceeds remaining data size %lu at pos %tu of %zu", chunk_len, rest, n_ptr - encoded, encoded_len);
139 chunk_len = rest;
140 }
141
142 /* copy the chunk */
143 memcpy(*decoded + *decoded_len, n_ptr, chunk_len);
144 *decoded_len += chunk_len;
145
146 if (chunk_len == rest) {
147 e_ptr = n_ptr + chunk_len;
148 break;
149 } else {
150 /* advance to next chunk */
151 e_ptr = n_ptr + chunk_len + eol_len;
152 }
153 }
154
155 return e_ptr;
156 }
157 /* }}} */
158
159 #ifdef HTTP_HAVE_ZLIB
160
161 #define HTTP_DEFLATE_LEVEL_SET(flags, level) \
162 switch (flags & 0xf) \
163 { \
164 default: \
165 if ((flags & 0xf) < 10) { \
166 level = flags & 0xf; \
167 break; \
168 } \
169 case HTTP_DEFLATE_LEVEL_DEF: \
170 level = Z_DEFAULT_COMPRESSION; \
171 break; \
172 }
173
174 #define HTTP_DEFLATE_WBITS_SET(flags, wbits) \
175 switch (flags & 0xf0) \
176 { \
177 case HTTP_DEFLATE_TYPE_GZIP: \
178 wbits = HTTP_WINDOW_BITS_GZIP; \
179 break; \
180 case HTTP_DEFLATE_TYPE_RAW: \
181 wbits = HTTP_WINDOW_BITS_RAW; \
182 break; \
183 default: \
184 wbits = HTTP_WINDOW_BITS_ZLIB; \
185 break; \
186 }
187
188 #define HTTP_INFLATE_WBITS_SET(flags, wbits) \
189 if (flags & HTTP_INFLATE_TYPE_RAW) { \
190 wbits = HTTP_WINDOW_BITS_RAW; \
191 } else { \
192 wbits = HTTP_WINDOW_BITS_ANY; \
193 }
194
195 #define HTTP_DEFLATE_STRATEGY_SET(flags, strategy) \
196 switch (flags & 0xf00) \
197 { \
198 case HTTP_DEFLATE_STRATEGY_FILT: \
199 strategy = Z_FILTERED; \
200 break; \
201 case HTTP_DEFLATE_STRATEGY_HUFF: \
202 strategy = Z_HUFFMAN_ONLY; \
203 break; \
204 case HTTP_DEFLATE_STRATEGY_RLE: \
205 strategy = Z_RLE; \
206 break; \
207 case HTTP_DEFLATE_STRATEGY_FIXED: \
208 strategy = Z_FIXED; \
209 break; \
210 default: \
211 strategy = Z_DEFAULT_STRATEGY; \
212 break; \
213 }
214
215 #define HTTP_WINDOW_BITS_ZLIB 0x0000000f
216 #define HTTP_WINDOW_BITS_GZIP 0x0000001f
217 #define HTTP_WINDOW_BITS_ANY 0x0000002f
218 #define HTTP_WINDOW_BITS_RAW -0x000000f
219
220 PHP_HTTP_API STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
221 {
222 int status, level, wbits, strategy;
223 z_stream Z;
224
225 HTTP_DEFLATE_LEVEL_SET(flags, level);
226 HTTP_DEFLATE_WBITS_SET(flags, wbits);
227 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
228
229 memset(&Z, 0, sizeof(z_stream));
230 *encoded = NULL;
231 *encoded_len = 0;
232
233 status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
234 if (Z_OK == status) {
235 *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
236 *encoded = emalloc_rel(*encoded_len);
237
238 Z.next_in = (Bytef *) data;
239 Z.next_out = (Bytef *) *encoded;
240 Z.avail_in = data_len;
241 Z.avail_out = *encoded_len;
242
243 status = deflate(&Z, Z_FINISH);
244 deflateEnd(&Z);
245
246 if (Z_STREAM_END == status) {
247 /* size buffer down to actual length */
248 *encoded = erealloc_rel(*encoded, Z.total_out + 1);
249 (*encoded)[*encoded_len = Z.total_out] = '\0';
250 return SUCCESS;
251 } else {
252 STR_SET(*encoded, NULL);
253 *encoded_len = 0;
254 }
255 }
256
257 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s (%s)", zError(status));
258 return FAILURE;
259 }
260
261 PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
262 {
263 int status, max = 0, wbits = HTTP_WINDOW_BITS_ANY;
264 z_stream Z;
265 phpstr buffer;
266
267 memset(&Z, 0, sizeof(z_stream));
268 *decoded = NULL;
269 *decoded_len = 0;
270
271 phpstr_init_ex(&buffer, data_len << 2, PHPSTR_INIT_PREALLOC);
272 buffer.size = data_len;
273
274 retry_inflate:
275 status = inflateInit2(&Z, wbits);
276 if (Z_OK == status) {
277 Z.next_in = (Bytef *) data;
278 Z.avail_in = data_len;
279
280 do {
281 phpstr_resize(&buffer, data_len << 2);
282
283 do {
284 Z.avail_out = (buffer.free -= Z.total_out - buffer.used);
285 Z.next_out = (Bytef *) buffer.data + (buffer.used = Z.total_out);
286 status = inflate(&Z, Z_NO_FLUSH);
287 } while (Z_OK == status);
288 } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
289
290 if (Z_DATA_ERROR == status && HTTP_WINDOW_BITS_ANY == wbits) {
291 /* raw deflated data? */
292 inflateEnd(&Z);
293 wbits = HTTP_WINDOW_BITS_RAW;
294 goto retry_inflate;
295 }
296
297 inflateEnd(&Z);
298
299 if (Z_STREAM_END == status) {
300 *decoded_len = Z.total_out;
301 *decoded = erealloc_rel(buffer.data, *decoded_len + 1);
302 (*decoded)[*decoded_len] = '\0';
303 return SUCCESS;
304 } else {
305 phpstr_dtor(&buffer);
306 }
307 }
308
309 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
310 return FAILURE;
311 }
312
313
314 PHP_HTTP_API http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
315 {
316 int status, level, wbits, strategy, free_stream;
317
318 if ((free_stream = !s)) {
319 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
320 }
321 memset(s, 0, sizeof(http_encoding_stream));
322 s->flags = flags;
323
324 HTTP_DEFLATE_LEVEL_SET(flags, level);
325 HTTP_DEFLATE_WBITS_SET(flags, wbits);
326 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
327
328 if (Z_OK == (status = deflateInit2(&s->stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
329 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
330
331 if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
332 return s;
333 }
334 deflateEnd(&s->stream);
335 status = Z_MEM_ERROR;
336 }
337
338 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize deflate encoding stream: %s", zError(status));
339 if (free_stream) {
340 efree(s);
341 }
342 return NULL;
343 }
344
345 PHP_HTTP_API http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
346 {
347 int status, wbits, free_stream;
348
349 if ((free_stream = !s)) {
350 s = pemalloc_rel(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
351 }
352 memset(s, 0, sizeof(http_encoding_stream));
353 s->flags = flags;
354
355 HTTP_INFLATE_WBITS_SET(flags, wbits);
356
357 if (Z_OK == (status = inflateInit2(&s->stream, wbits))) {
358 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
359
360 if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
361 return s;
362 }
363 inflateEnd(&s->stream);
364 status = Z_MEM_ERROR;
365 }
366
367 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize inflate stream: %s", zError(status));
368 if (free_stream) {
369 efree(s);
370 }
371 return NULL;
372 }
373
374 PHP_HTTP_API STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
375 {
376 int status;
377
378 /* append input to our buffer */
379 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
380
381 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
382 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
383
384 /* deflate */
385 *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
386 *encoded = emalloc_rel(*encoded_len);
387 s->stream.avail_out = *encoded_len;
388 s->stream.next_out = (Bytef *) *encoded;
389
390 switch (status = deflate(&s->stream, Z_NO_FLUSH))
391 {
392 case Z_OK:
393 case Z_STREAM_END:
394 /* cut processed chunk off the buffer */
395 phpstr_cut(PHPSTR(s->stream.opaque), 0, data_len - s->stream.avail_in);
396
397 /* size buffer down to actual size */
398 *encoded_len -= s->stream.avail_out;
399 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
400 (*encoded)[*encoded_len] = '\0';
401 return SUCCESS;
402 break;
403 }
404
405 STR_SET(*encoded, NULL);
406 *encoded_len = 0;
407 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
408 return FAILURE;
409 }
410
411 PHP_HTTP_API STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
412 {
413 int status, max = 0;
414
415 /* append input to buffer */
416 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
417
418 /* for realloc() */
419 *decoded = NULL;
420 *decoded_len = data_len << 1;
421
422 /* inflate */
423 do {
424 *decoded_len <<= 1;
425 *decoded = erealloc_rel(*decoded, *decoded_len);
426
427 retry_raw_inflate:
428 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
429 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
430
431 s->stream.next_out = (Bytef *) *decoded;
432 s->stream.avail_out = *decoded_len;
433
434 switch (status = inflate(&s->stream, Z_NO_FLUSH))
435 {
436 case Z_OK:
437 case Z_STREAM_END:
438 /* cut off */
439 phpstr_cut(PHPSTR(s->stream.opaque), 0, data_len - s->stream.avail_in);
440
441 /* size down */
442 *decoded_len -= s->stream.avail_out;
443 *decoded = erealloc_rel(*decoded, *decoded_len + 1);
444 (*decoded)[*decoded_len] = '\0';
445 return SUCCESS;
446 break;
447
448 case Z_DATA_ERROR:
449 /* raw deflated data ? */
450 if (!(s->flags & HTTP_INFLATE_TYPE_RAW) && !s->stream.total_out) {
451 inflateEnd(&s->stream);
452 s->flags |= HTTP_INFLATE_TYPE_RAW;
453 inflateInit2(&s->stream, HTTP_WINDOW_BITS_RAW);
454 goto retry_raw_inflate;
455 }
456 break;
457 }
458 } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
459
460 STR_SET(*decoded, NULL);
461 *decoded_len = 0;
462 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
463 return FAILURE;
464 }
465
466 PHP_HTTP_API STATUS _http_encoding_deflate_stream_flush(http_encoding_stream *s, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
467 {
468 int status;
469
470 *encoded_len = 0x800;
471 *encoded = emalloc_rel(*encoded_len);
472
473 s->stream.avail_in = 0;
474 s->stream.next_in = NULL;
475 s->stream.avail_out = *encoded_len;
476 s->stream.next_out = (Bytef *) *encoded;
477
478 switch (status = deflate(&s->stream, Z_SYNC_FLUSH))
479 {
480 case Z_OK:
481 case Z_STREAM_END:
482 *encoded_len = 0x800 - s->stream.avail_out;
483 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
484 (*encoded)[*encoded_len] = '\0';
485 return SUCCESS;
486 break;
487 }
488
489 STR_SET(*encoded, NULL);
490 *encoded_len = 0;
491 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
492 return FAILURE;
493 }
494
495 PHP_HTTP_API STATUS _http_encoding_inflate_stream_flush(http_encoding_stream *s, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
496 {
497 int status;
498
499 *decoded_len = 0x800;
500 *decoded = emalloc_rel(*decoded_len);
501
502 s->stream.avail_in = 0;
503 s->stream.next_in = NULL;
504 s->stream.avail_out = *decoded_len;
505 s->stream.next_out = (Bytef *) *decoded;
506
507 switch (status = inflate(&s->stream, Z_SYNC_FLUSH))
508 {
509 case Z_OK:
510 case Z_STREAM_END:
511 *decoded_len = 0x800 - s->stream.avail_out;
512 *decoded = erealloc_rel(*decoded, *decoded_len + 1);
513 (*decoded)[*decoded_len] = '\0';
514 return SUCCESS;
515 break;
516 }
517
518 STR_SET(*decoded, NULL);
519 *decoded_len = 0;
520 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to flush inflate stream: %s", zError(status));
521 return FAILURE;
522 }
523
524 PHP_HTTP_API STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
525 {
526 int status;
527
528 *encoded_len = 0x800;
529 *encoded = emalloc_rel(*encoded_len);
530
531 /* deflate remaining input */
532 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
533 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
534
535 s->stream.avail_out = *encoded_len;
536 s->stream.next_out = (Bytef *) *encoded;
537
538 do {
539 status = deflate(&s->stream, Z_FINISH);
540 } while (Z_OK == status);
541
542 if (Z_STREAM_END == status) {
543 /* cut processed intp off */
544 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
545
546 /* size down */
547 *encoded_len -= s->stream.avail_out;
548 *encoded = erealloc_rel(*encoded, *encoded_len + 1);
549 (*encoded)[*encoded_len] = '\0';
550 return SUCCESS;
551 }
552
553 STR_SET(*encoded, NULL);
554 *encoded_len = 0;
555 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
556 return FAILURE;
557 }
558
559 PHP_HTTP_API STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC TSRMLS_DC)
560 {
561 int status;
562
563 *decoded_len = s->stream.avail_in << 2;
564 *decoded = emalloc_rel(*decoded_len);
565
566 /* inflate remaining input */
567 s->stream.next_in = (Bytef *) PHPSTR_VAL(s->stream.opaque);
568 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
569
570 s->stream.avail_out = *decoded_len;
571 s->stream.next_out = (Bytef *) *decoded;
572
573 if (Z_STREAM_END == (status = inflate(&s->stream, Z_FINISH))) {
574 /* cut processed input off */
575 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
576
577 /* size down */
578 *decoded_len -= s->stream.avail_out;
579 *decoded = erealloc_rel(*decoded, *decoded_len + 1);
580 (*decoded)[*decoded_len] = '\0';
581 return SUCCESS;
582 }
583
584 STR_SET(*decoded, NULL);
585 *decoded_len = 0;
586 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
587 return FAILURE;
588 }
589
590 PHP_HTTP_API void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
591 {
592 if (s) {
593 if (s->stream.opaque) {
594 phpstr_free((phpstr **) &s->stream.opaque);
595 }
596 deflateEnd(&s->stream);
597 }
598 }
599
600 PHP_HTTP_API void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
601 {
602 if (s) {
603 if (s->stream.opaque) {
604 phpstr_free((phpstr **) &s->stream.opaque);
605 }
606 inflateEnd(&s->stream);
607 }
608 }
609
610 PHP_HTTP_API void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
611 {
612 if (s) {
613 http_encoding_deflate_stream_dtor(*s);
614 if (*s) {
615 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
616 }
617 *s = NULL;
618 }
619 }
620
621 PHP_HTTP_API void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
622 {
623 if (s) {
624 http_encoding_inflate_stream_dtor(*s);
625 if (*s) {
626 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
627 }
628 *s = NULL;
629 }
630 }
631
632 void _http_ob_deflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
633 {
634 getGlobals(G);
635
636 *handled_output = NULL;
637 *handled_output_len = 0;
638
639 if (mode & PHP_OUTPUT_HANDLER_START) {
640 int flags;
641
642 if (G->send.deflate.stream) {
643 zend_error(E_ERROR, "ob_deflatehandler() can only be used once");
644 return;
645 }
646
647 G->send.deflate.encoding = !0;
648
649 switch (http_encoding_response_start(0))
650 {
651 case HTTP_ENCODING_GZIP:
652 flags = HTTP_DEFLATE_TYPE_GZIP;
653 break;
654
655 case HTTP_ENCODING_DEFLATE:
656 flags = HTTP_DEFLATE_TYPE_ZLIB;
657 break;
658
659 default:
660 goto deflate_passthru_plain;
661 break;
662 }
663
664 flags |= (G->send.deflate.start_flags &~ 0xf0);
665 G->send.deflate.stream = http_encoding_deflate_stream_init(NULL, flags);
666 }
667
668 if (G->send.deflate.stream) {
669 http_encoding_deflate_stream_update((http_encoding_stream *) G->send.deflate.stream, output, output_len, handled_output, handled_output_len);
670
671 if (mode & PHP_OUTPUT_HANDLER_END) {
672 char *remaining = NULL;
673 size_t remaining_len = 0;
674
675 http_encoding_deflate_stream_finish((http_encoding_stream *) G->send.deflate.stream, &remaining, &remaining_len);
676 http_encoding_deflate_stream_free((http_encoding_stream **) &G->send.deflate.stream);
677 if (remaining) {
678 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
679 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
680 (*handled_output)[*handled_output_len += remaining_len] = '\0';
681 efree(remaining);
682 }
683 }
684 } else {
685 deflate_passthru_plain:
686 *handled_output = estrndup(output, *handled_output_len = output_len);
687 }
688 }
689
690 void _http_ob_inflatehandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
691 {
692 getGlobals(G);
693
694 *handled_output = NULL;
695 *handled_output_len = 0;
696
697 if (mode & PHP_OUTPUT_HANDLER_START) {
698 if (G->send.inflate.stream) {
699 zend_error(E_ERROR, "ob_inflatehandler() can only be used once");
700 return;
701 }
702 G->send.inflate.stream = http_encoding_inflate_stream_init(NULL, (HTTP_G(send).inflate.start_flags &~ 0xf0));
703 }
704
705 if (G->send.inflate.stream) {
706 http_encoding_inflate_stream_update((http_encoding_stream *) G->send.inflate.stream, output, output_len, handled_output, handled_output_len);
707
708 if (mode & PHP_OUTPUT_HANDLER_END) {
709 char *remaining = NULL;
710 size_t remaining_len = 0;
711
712 http_encoding_inflate_stream_finish((http_encoding_stream *) G->send.inflate.stream, &remaining, &remaining_len);
713 http_encoding_inflate_stream_free((http_encoding_stream **) &G->send.inflate.stream);
714 if (remaining) {
715 *handled_output = erealloc(*handled_output, *handled_output_len + remaining_len + 1);
716 memcpy(*handled_output + *handled_output_len, remaining, remaining_len);
717 (*handled_output)[*handled_output_len += remaining_len] = '\0';
718 efree(remaining);
719 }
720 }
721 } else {
722 *handled_output = estrndup(output, *handled_output_len = output_len);
723 }
724 }
725
726 static const char http_encoding_gzip_header[] = {
727 (const char) 0x1f, // fixed value
728 (const char) 0x8b, // fixed value
729 (const char) Z_DEFLATED, // compression algorithm
730 (const char) 0, // none of the possible flags defined by the GZIP "RFC"
731 (const char) 0, // MTIME
732 (const char) 0, // =*=
733 (const char) 0, // =*=
734 (const char) 0, // =*=
735 (const char) 0, // two possible flag values for 9 compression levels? o_O
736 #ifdef PHP_WIN32
737 (const char) 0x0b // OS_CODE
738 #else
739 (const char) 0x03 // OS_CODE
740 #endif
741 };
742
743 PHP_HTTP_API STATUS _http_encoding_gzencode_verify(const char *data, size_t data_len, const char **encoded, size_t *encoded_len, int error_level TSRMLS_DC)
744 {
745 size_t offset = sizeof(http_encoding_gzip_header);
746
747 if (data_len < offset) {
748 goto really_bad_gzip_header;
749 }
750
751 if (data[0] != (const char) 0x1F || data[1] != (const char) 0x8B) {
752 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized GZIP header start: 0x%02X 0x%02X", (int) data[0], (int) (data[1] & 0xFF));
753 return FAILURE;
754 }
755
756 if (data[2] != (const char) Z_DEFLATED) {
757 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized compression format (%d)", (int) (data[2] & 0xFF));
758 /* still try to decode */
759 }
760 if ((data[3] & 0x4) == 0x4) {
761 if (data_len < offset + 2) {
762 goto really_bad_gzip_header;
763 }
764 /* there are extra fields, the length follows the common header as 2 bytes LSB */
765 offset += (unsigned) ((data[offset] & 0xFF));
766 offset += 1;
767 offset += (unsigned) ((data[offset] & 0xFF) << 8);
768 offset += 1;
769 }
770 if ((data[3] & 0x8) == 0x8) {
771 if (data_len <= offset) {
772 goto really_bad_gzip_header;
773 }
774 /* there's a file name */
775 offset += strlen(&data[offset]) + 1 /*NUL*/;
776 }
777 if ((data[3] & 0x10) == 0x10) {
778 if (data_len <= offset) {
779 goto really_bad_gzip_header;
780 }
781 /* there's a comment */
782 offset += strlen(&data[offset]) + 1 /* NUL */;
783 }
784 if ((data[3] & 0x2) == 0x2) {
785 /* there's a CRC16 of the header */
786 offset += 2;
787 if (data_len <= offset) {
788 goto really_bad_gzip_header;
789 } else {
790 ulong crc, cmp;
791
792 cmp = (unsigned) ((data[offset-2] & 0xFF));
793 cmp += (unsigned) ((data[offset-1] & 0xFF) << 8);
794
795 crc = crc32(0L, Z_NULL, 0);
796 crc = crc32(crc, (const Bytef *) data, sizeof(http_encoding_gzip_header));
797
798 if (cmp != (crc & 0xFFFF)) {
799 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "GZIP headers CRC checksums so not match (%lu, %lu)", cmp, crc & 0xFFFF);
800 return FAILURE;
801 }
802 }
803 }
804
805 if (data_len < offset + 8) {
806 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP footer");
807 return FAILURE;
808 }
809
810 if (encoded) {
811 *encoded = data + offset;
812 }
813 if (encoded_len) {
814 *encoded_len = data_len - offset - 8 /* size of the assumed GZIP footer */;
815 }
816
817 return SUCCESS;
818
819 really_bad_gzip_header:
820 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP header");
821 return FAILURE;
822 }
823
824 PHP_HTTP_API STATUS _http_encoding_gzdecode_verify(const char *data, size_t data_len, const char *decoded, size_t decoded_len, int error_level TSRMLS_DC)
825 {
826 STATUS status = SUCCESS;
827 ulong len, cmp, crc;
828
829 crc = crc32(0L, Z_NULL, 0);
830 crc = crc32(crc, (const Bytef *) decoded, decoded_len);
831
832 cmp = (unsigned) ((data[data_len-8] & 0xFF));
833 cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
834 cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
835 cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
836 len = (unsigned) ((data[data_len-4] & 0xFF));
837 len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
838 len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
839 len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
840
841 if (cmp != crc) {
842 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
843 status = FAILURE;
844 }
845 if (len != decoded_len) {
846 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);
847 status = FAILURE;
848 }
849 return status;
850 }
851
852 #endif /* HTTP_HAVE_ZLIB */
853
854 PHP_HTTP_API int _http_encoding_response_start(size_t content_length TSRMLS_DC)
855 {
856 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
857 php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
858 HTTP_G(send).deflate.encoding = 0;
859 } else {
860 if (!HTTP_G(send).deflate.encoding) {
861 /* emit a content-length header */
862 if (content_length) {
863 char cl_header_str[128];
864 size_t cl_header_len;
865 cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
866 http_send_header_string_ex(cl_header_str, cl_header_len, 1);
867 }
868 } else {
869 #ifndef HTTP_HAVE_ZLIB
870 HTTP_G(send).deflate.encoding = 0;
871 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
872 #else
873 HashTable *selected;
874 zval zsupported;
875
876 INIT_PZVAL(&zsupported);
877 array_init(&zsupported);
878 add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
879 add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
880 add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
881
882 HTTP_G(send).deflate.encoding = 0;
883
884 if ((selected = http_negotiate_encoding(&zsupported))) {
885 STATUS hs = FAILURE;
886 char *encoding = NULL;
887 ulong idx;
888
889 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
890 if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
891 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
892 HTTP_G(send).deflate.encoding = HTTP_ENCODING_GZIP;
893 }
894 } else if (!strcmp(encoding, "deflate")) {
895 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
896 HTTP_G(send).deflate.encoding = HTTP_ENCODING_DEFLATE;
897 }
898 }
899 if (SUCCESS == hs) {
900 http_send_header_string("Vary: Accept-Encoding");
901 }
902 }
903
904 zend_hash_destroy(selected);
905 FREE_HASHTABLE(selected);
906 }
907
908 zval_dtor(&zsupported);
909 return HTTP_G(send).deflate.encoding;
910 #endif
911 }
912 }
913 return 0;
914 }
915
916 /*
917 * Local variables:
918 * tab-width: 4
919 * c-basic-offset: 4
920 * End:
921 * vim600: noet sw=4 ts=4 fdm=marker
922 * vim<600: noet sw=4 ts=4
923 */
924