- don't call into ext/zlib any longer
[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 ZEND_EXTERN_MODULE_GLOBALS(http);
28
29 #ifdef HTTP_HAVE_ZLIB
30 PHP_MINIT_FUNCTION(http_encoding)
31 {
32 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF);
33 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN);
34 HTTP_LONG_CONSTANT("HTTP_DEFLATE_LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX);
35 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB);
36 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP);
37 HTTP_LONG_CONSTANT("HTTP_DEFLATE_TYPE_RAW", HTTP_DEFLATE_TYPE_RAW);
38 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF);
39 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT);
40 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF);
41 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE);
42 HTTP_LONG_CONSTANT("HTTP_DEFLATE_STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED);
43 return SUCCESS;
44 }
45 #endif
46
47 static inline int eol_match(char **line, int *eol_len)
48 {
49 char *ptr = *line;
50
51 while (0x20 == *ptr) ++ptr;
52
53 if (ptr == http_locate_eol(*line, eol_len)) {
54 *line = ptr;
55 return 1;
56 } else {
57 return 0;
58 }
59 }
60
61 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
62 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
63 {
64 int eol_len = 0;
65 char *n_ptr = NULL;
66 const char *e_ptr = encoded;
67
68 *decoded_len = 0;
69 *decoded = ecalloc(1, encoded_len);
70
71 while ((encoded + encoded_len - e_ptr) > 0) {
72 ulong chunk_len = 0, rest;
73
74 chunk_len = strtoul(e_ptr, &n_ptr, 16);
75
76 /* we could not read in chunk size */
77 if (n_ptr == e_ptr) {
78 /*
79 * if this is the first turn and there doesn't seem to be a chunk
80 * size at the begining of the body, do not fail on apparently
81 * not encoded data and return a copy
82 */
83 if (e_ptr == encoded) {
84 http_error(HE_NOTICE, HTTP_E_ENCODING, "Data does not seem to be chunked encoded");
85 memcpy(*decoded, encoded, encoded_len);
86 *decoded_len = encoded_len;
87 return encoded + encoded_len;
88 } else {
89 efree(*decoded);
90 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Expected chunk size at pos %tu of %zu but got trash", n_ptr - encoded, encoded_len);
91 return NULL;
92 }
93 }
94
95 /* reached the end */
96 if (!chunk_len) {
97 /* move over '0' chunked encoding terminator */
98 while (*e_ptr == '0') ++e_ptr;
99 break;
100 }
101
102 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
103 if (*n_ptr && !eol_match(&n_ptr, &eol_len)) {
104 if (eol_len == 2) {
105 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));
106 } else {
107 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);
108 }
109 }
110 n_ptr += eol_len;
111
112 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
113 if (chunk_len > (rest = encoded + encoded_len - n_ptr)) {
114 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);
115 chunk_len = rest;
116 }
117
118 /* copy the chunk */
119 memcpy(*decoded + *decoded_len, n_ptr, chunk_len);
120 *decoded_len += chunk_len;
121
122 if (chunk_len == rest) {
123 e_ptr = n_ptr + chunk_len;
124 break;
125 } else {
126 /* advance to next chunk */
127 e_ptr = n_ptr + chunk_len + eol_len;
128 }
129 }
130
131 return e_ptr;
132 }
133 /* }}} */
134
135 #ifdef HTTP_HAVE_ZLIB
136
137 #define HTTP_DEFLATE_LEVEL_SET(flags, level) \
138 switch (flags & 0xf) \
139 { \
140 default: \
141 if ((flags & 0xf) < 10) { \
142 level = flags & 0xf; \
143 break; \
144 } \
145 case HTTP_DEFLATE_LEVEL_DEF: \
146 level = Z_DEFAULT_COMPRESSION; \
147 break; \
148 }
149
150 #define HTTP_DEFLATE_WBITS_SET(flags, wbits) \
151 switch (flags & 0xf0) \
152 { \
153 case HTTP_DEFLATE_TYPE_GZIP: \
154 wbits = HTTP_WINDOW_BITS_GZIP; \
155 break; \
156 case HTTP_DEFLATE_TYPE_RAW: \
157 wbits = HTTP_WINDOW_BITS_RAW; \
158 break; \
159 default: \
160 wbits = HTTP_WINDOW_BITS_ZLIB; \
161 break; \
162 }
163
164 #define HTTP_INFLATE_WBITS_SET(flags, wbits) \
165 if (flags & HTTP_INFLATE_TYPE_RAW) { \
166 wbits = HTTP_WINDOW_BITS_RAW; \
167 } else { \
168 wbits = HTTP_WINDOW_BITS_ANY; \
169 }
170
171 #define HTTP_DEFLATE_STRATEGY_SET(flags, strategy) \
172 switch (flags & 0xf00) \
173 { \
174 case HTTP_DEFLATE_STRATEGY_FILT: \
175 strategy = Z_FILTERED; \
176 break; \
177 case HTTP_DEFLATE_STRATEGY_HUFF: \
178 strategy = Z_HUFFMAN_ONLY; \
179 break; \
180 case HTTP_DEFLATE_STRATEGY_RLE: \
181 strategy = Z_RLE; \
182 break; \
183 case HTTP_DEFLATE_STRATEGY_FIXED: \
184 strategy = Z_FIXED; \
185 break; \
186 default: \
187 strategy = Z_DEFAULT_STRATEGY; \
188 break; \
189 }
190
191 #define HTTP_WINDOW_BITS_ZLIB 0x0000000f
192 #define HTTP_WINDOW_BITS_GZIP 0x0000001f
193 #define HTTP_WINDOW_BITS_ANY 0x0000002f
194 #define HTTP_WINDOW_BITS_RAW -0x000000f
195
196 STATUS _http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
197 {
198 int status, level, wbits, strategy;
199 z_stream Z;
200
201 HTTP_DEFLATE_LEVEL_SET(flags, level);
202 HTTP_DEFLATE_WBITS_SET(flags, wbits);
203 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
204
205 memset(&Z, 0, sizeof(z_stream));
206 *encoded = NULL;
207 *encoded_len = 0;
208
209 status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
210 if (Z_OK == status) {
211 *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
212 *encoded = emalloc(*encoded_len);
213
214 Z.next_in = (Bytef *) data;
215 Z.next_out = (Bytef *) *encoded;
216 Z.avail_in = data_len;
217 Z.avail_out = *encoded_len;
218
219 status = deflate(&Z, Z_FINISH);
220 if (Z_STREAM_END == status) {
221 /* size buffer down to actual length */
222 *encoded = erealloc(*encoded, Z.total_out + 1);
223 (*encoded)[*encoded_len = Z.total_out] = '\0';
224 deflateEnd(&Z);
225 return SUCCESS;
226 } else {
227 STR_SET(*encoded, NULL);
228 *encoded_len = 0;
229 }
230 }
231
232 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s (%s)", zError(status), Z.msg);
233 deflateEnd(&Z);
234 return FAILURE;
235 }
236
237 STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
238 {
239 int status, max = 0, wbits = HTTP_WINDOW_BITS_ANY;
240 z_stream Z;
241 phpstr buffer;
242
243 memset(&Z, 0, sizeof(z_stream));
244 *decoded = NULL;
245 *decoded_len = 0;
246
247 phpstr_init_ex(&buffer, data_len << 2, PHPSTR_INIT_PREALLOC);
248 buffer.size = data_len;
249
250 retry_inflate:
251 status = inflateInit2(&Z, wbits);
252 if (Z_OK == status) {
253 Z.next_in = (Bytef *) data;
254 Z.avail_in = data_len;
255
256 do {
257 phpstr_resize(&buffer, data_len);
258
259 do {
260 Z.next_out = buffer.data + (buffer.used += Z.total_out);
261 Z.avail_out = (buffer.free -= Z.total_out);
262 status = inflate(&Z, Z_NO_FLUSH);
263 } while (Z_OK == status);
264
265 } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
266
267 if (Z_DATA_ERROR == status && HTTP_WINDOW_BITS_ANY == wbits) {
268 /* raw deflated data? */
269 inflateEnd(&Z);
270 wbits = HTTP_WINDOW_BITS_RAW;
271 goto retry_inflate;
272 }
273
274 if (Z_STREAM_END == status) {
275 *decoded = erealloc(buffer.data, (buffer.used += Z.total_out) + 1);
276 (*decoded)[*decoded_len = buffer.used] = '\0';
277 inflateEnd(&Z);
278 return SUCCESS;
279 } else {
280 phpstr_dtor(&buffer);
281 }
282 }
283
284 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s (%s)", zError(status), Z.msg);
285 inflateEnd(&Z);
286 return FAILURE;
287 }
288
289
290 http_encoding_stream *_http_encoding_deflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
291 {
292 int status, level, wbits, strategy, free_stream;
293
294 if ((free_stream = !s)) {
295 s = pemalloc(sizeof(http_encoding_stream), (flags & HTTP_ENCODING_STREAM_PERSISTENT));
296 }
297 memset(s, 0, sizeof(http_encoding_stream));
298 s->flags = flags;
299
300 HTTP_DEFLATE_LEVEL_SET(flags, level);
301 HTTP_DEFLATE_WBITS_SET(flags, wbits);
302 HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
303
304 if (Z_OK == (status = deflateInit2(&s->stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
305 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
306
307 if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
308 return s;
309 }
310 deflateEnd(&s->stream);
311 status = Z_MEM_ERROR;
312 }
313
314 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize deflate encoding stream: %s", zError(status));
315 if (free_stream) {
316 efree(s);
317 }
318 return NULL;
319 }
320
321 http_encoding_stream *_http_encoding_inflate_stream_init(http_encoding_stream *s, int flags TSRMLS_DC)
322 {
323 int status, wbits, free_stream;
324
325 if ((free_stream = !s)) {
326 s = emalloc(sizeof(http_encoding_stream));
327 }
328 memset(s, 0, sizeof(http_encoding_stream));
329 s->flags = flags;
330
331 HTTP_INFLATE_WBITS_SET(flags, wbits);
332
333 if (Z_OK == (status = inflateInit2(&s->stream, wbits))) {
334 int p = (flags & HTTP_ENCODING_STREAM_PERSISTENT) ? PHPSTR_INIT_PERSISTENT:0;
335
336 if ((s->stream.opaque = phpstr_init_ex(NULL, 0x8000, p))) {
337 return s;
338 }
339 inflateEnd(&s->stream);
340 status = Z_MEM_ERROR;
341 }
342
343 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to initialize inflate stream: %s", zError(status));
344 if (free_stream) {
345 efree(s);
346 }
347 return NULL;
348 }
349
350 STATUS _http_encoding_deflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
351 {
352 int status;
353
354 /* append input to our buffer */
355 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
356
357 s->stream.next_in = PHPSTR_VAL(s->stream.opaque);
358 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
359
360 /* deflate */
361 s->stream.avail_out = *encoded_len = HTTP_ENCODING_BUFLEN(data_len);
362 s->stream.next_out = *encoded = emalloc(*encoded_len);
363
364 switch (status = deflate(&s->stream, Z_NO_FLUSH))
365 {
366 case Z_OK:
367 case Z_STREAM_END:
368 /* cut processed chunk off the buffer */
369 phpstr_cut(PHPSTR(s->stream.opaque), 0, data_len - s->stream.avail_in);
370
371 /* size buffer down to actual size */
372 *encoded_len -= s->stream.avail_out;
373 *encoded = erealloc(*encoded, *encoded_len + 1);
374 (*encoded)[*encoded_len] = '\0';
375 return SUCCESS;
376 break;
377 }
378
379 STR_SET(*encoded, NULL);
380 *encoded_len = 0;
381 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
382 return FAILURE;
383 }
384
385 STATUS _http_encoding_inflate_stream_update(http_encoding_stream *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
386 {
387 int status, max = 0;
388
389 /* append input to buffer */
390 phpstr_append(PHPSTR(s->stream.opaque), data, data_len);
391
392 /* for realloc() */
393 *decoded = NULL;
394 *decoded_len = data_len << 1;
395
396 /* inflate */
397 do {
398 *decoded_len <<= 1;
399 *decoded = erealloc(*decoded, *decoded_len);
400
401 retry_raw_inflate:
402 s->stream.next_in = PHPSTR_VAL(s->stream.opaque);
403 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
404
405 s->stream.next_out = *decoded;
406 s->stream.avail_out = *decoded_len;
407
408 switch (status = inflate(&s->stream, Z_NO_FLUSH))
409 {
410 case Z_OK:
411 case Z_STREAM_END:
412 /* cut off */
413 phpstr_cut(PHPSTR(s->stream.opaque), 0, data_len - s->stream.avail_in);
414
415 /* size down */
416 *decoded_len -= s->stream.avail_out;
417 *decoded = erealloc(*decoded, *decoded_len + 1);
418 (*decoded)[*decoded_len] = '\0';
419 return SUCCESS;
420 break;
421
422 case Z_DATA_ERROR:
423 /* raw deflated data ? */
424 if (!(s->flags & HTTP_INFLATE_TYPE_RAW) && !s->stream.total_out) {
425 inflateEnd(&s->stream);
426 s->flags |= HTTP_INFLATE_TYPE_RAW;
427 inflateInit2(&s->stream, HTTP_WINDOW_BITS_RAW);
428 goto retry_raw_inflate;
429 }
430 break;
431 }
432 if (Z_BUF_ERROR == status) DebugBreak();
433 } while (Z_BUF_ERROR == status && ++max < HTTP_ENCODING_MAXTRY);
434
435 STR_SET(*decoded, NULL);
436 *decoded_len = 0;
437 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not update inflate stream: %s", zError(status));
438 return FAILURE;
439 }
440
441 STATUS _http_encoding_deflate_stream_finish(http_encoding_stream *s, char **encoded, size_t *encoded_len TSRMLS_DC)
442 {
443 int status;
444
445 /* deflate remaining input */
446 s->stream.next_in = PHPSTR_VAL(s->stream.opaque);
447 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
448
449 s->stream.avail_out = *encoded_len = 0x800;
450 s->stream.next_out = *encoded = emalloc(*encoded_len);
451
452 do {
453 status = deflate(&s->stream, Z_FINISH);
454 } while (Z_OK == status);
455
456 if (Z_STREAM_END == status) {
457 /* cut processed intp off */
458 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
459
460 /* size down */
461 *encoded_len -= s->stream.avail_out;
462 *encoded = erealloc(*encoded, *encoded_len + 1);
463 (*encoded)[*encoded_len] = '\0';
464 return SUCCESS;
465 }
466
467 STR_SET(*encoded, NULL);
468 *encoded_len = 0;
469 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
470 return FAILURE;
471 }
472
473 STATUS _http_encoding_inflate_stream_finish(http_encoding_stream *s, char **decoded, size_t *decoded_len TSRMLS_DC)
474 {
475 int status;
476
477 /* inflate remaining input */
478 s->stream.next_in = PHPSTR_VAL(s->stream.opaque);
479 s->stream.avail_in = PHPSTR_LEN(s->stream.opaque);
480
481 s->stream.avail_out = *decoded_len = s->stream.avail_in << 2;
482 s->stream.next_out = *decoded = emalloc(*decoded_len);
483
484 if (Z_STREAM_END == (status = inflate(&s->stream, Z_FINISH))) {
485 /* cut processed input off */
486 phpstr_cut(PHPSTR(s->stream.opaque), 0, PHPSTR_LEN(s->stream.opaque) - s->stream.avail_in);
487
488 /* size down */
489 *decoded_len -= s->stream.avail_out;
490 *decoded = erealloc(*decoded, *decoded_len + 1);
491 (*decoded)[*decoded_len] = '\0';
492 return SUCCESS;
493 }
494
495 STR_SET(*decoded, NULL);
496 *decoded_len = 0;
497 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
498 return FAILURE;
499 }
500
501 void _http_encoding_deflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
502 {
503 if (s) {
504 if (s->stream.opaque) {
505 phpstr_free((phpstr **) &s->stream.opaque);
506 }
507 deflateEnd(&s->stream);
508 }
509 }
510
511 void _http_encoding_inflate_stream_dtor(http_encoding_stream *s TSRMLS_DC)
512 {
513 if (s) {
514 if (s->stream.opaque) {
515 phpstr_free((phpstr **) &s->stream.opaque);
516 }
517 inflateEnd(&s->stream);
518 }
519 }
520
521 void _http_encoding_deflate_stream_free(http_encoding_stream **s TSRMLS_DC)
522 {
523 if (s) {
524 http_encoding_deflate_stream_dtor(*s);
525 if (*s) {
526 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
527 }
528 *s = NULL;
529 }
530 }
531
532 void _http_encoding_inflate_stream_free(http_encoding_stream **s TSRMLS_DC)
533 {
534 if (s) {
535 http_encoding_inflate_stream_dtor(*s);
536 if (*s) {
537 pefree(*s, (*s)->flags & HTTP_ENCODING_STREAM_PERSISTENT);
538 }
539 *s = NULL;
540 }
541 }
542
543 static const char http_encoding_gzip_header[] = {
544 (const char) 0x1f, // fixed value
545 (const char) 0x8b, // fixed value
546 (const char) Z_DEFLATED, // compression algorithm
547 (const char) 0, // none of the possible flags defined by the GZIP "RFC"
548 (const char) 0, // MTIME
549 (const char) 0, // =*=
550 (const char) 0, // =*=
551 (const char) 0, // =*=
552 (const char) 0, // two possible flag values for 9 compression levels? o_O
553 #ifdef PHP_WIN32
554 (const char) 0x0b // OS_CODE
555 #else
556 (const char) 0x03 // OS_CODE
557 #endif
558 };
559
560 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)
561 {
562 size_t offset = sizeof(http_encoding_gzip_header);
563
564 if (data_len < offset) {
565 goto really_bad_gzip_header;
566 }
567
568 if (data[0] != (const char) 0x1F || data[1] != (const char) 0x8B) {
569 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));
570 return FAILURE;
571 }
572
573 if (data[2] != (const char) Z_DEFLATED) {
574 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Unrecognized compression format (%d)", (int) (data[2] & 0xFF));
575 /* still try to decode */
576 }
577 if ((data[3] & 0x4) == 0x4) {
578 if (data_len < offset + 2) {
579 goto really_bad_gzip_header;
580 }
581 /* there are extra fields, the length follows the common header as 2 bytes LSB */
582 offset += (unsigned) ((data[offset] & 0xFF));
583 offset += 1;
584 offset += (unsigned) ((data[offset] & 0xFF) << 8);
585 offset += 1;
586 }
587 if ((data[3] & 0x8) == 0x8) {
588 if (data_len <= offset) {
589 goto really_bad_gzip_header;
590 }
591 /* there's a file name */
592 offset += strlen(&data[offset]) + 1 /*NUL*/;
593 }
594 if ((data[3] & 0x10) == 0x10) {
595 if (data_len <= offset) {
596 goto really_bad_gzip_header;
597 }
598 /* there's a comment */
599 offset += strlen(&data[offset]) + 1 /* NUL */;
600 }
601 if ((data[3] & 0x2) == 0x2) {
602 /* there's a CRC16 of the header */
603 offset += 2;
604 if (data_len <= offset) {
605 goto really_bad_gzip_header;
606 } else {
607 ulong crc, cmp;
608
609 cmp = (unsigned) ((data[offset-2] & 0xFF));
610 cmp += (unsigned) ((data[offset-1] & 0xFF) << 8);
611
612 crc = crc32(0L, Z_NULL, 0);
613 crc = crc32(crc, (const Bytef *) data, sizeof(http_encoding_gzip_header));
614
615 if (cmp != (crc & 0xFFFF)) {
616 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "GZIP headers CRC checksums so not match (%lu, %lu)", cmp, crc & 0xFFFF);
617 return FAILURE;
618 }
619 }
620 }
621
622 if (data_len < offset + 8) {
623 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP footer");
624 return FAILURE;
625 }
626
627 if (encoded) {
628 *encoded = data + offset;
629 }
630 if (encoded_len) {
631 *encoded_len = data_len - offset - 8 /* size of the assumed GZIP footer */;
632 }
633
634 return SUCCESS;
635
636 really_bad_gzip_header:
637 http_error(error_level TSRMLS_CC, HTTP_E_ENCODING, "Missing or truncated GZIP header");
638 return FAILURE;
639 }
640
641 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)
642 {
643 STATUS status = SUCCESS;
644 ulong len, cmp, crc;
645
646 crc = crc32(0L, Z_NULL, 0);
647 crc = crc32(crc, (const Bytef *) decoded, decoded_len);
648
649 cmp = (unsigned) ((data[data_len-8] & 0xFF));
650 cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
651 cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
652 cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
653 len = (unsigned) ((data[data_len-4] & 0xFF));
654 len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
655 len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
656 len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
657
658 if (cmp != crc) {
659 http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
660 status = FAILURE;
661 }
662 if (len != decoded_len) {
663 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);
664 status = FAILURE;
665 }
666 return status;
667 }
668
669 #endif /* HTTP_HAVE_ZLIB */
670
671 PHP_HTTP_API zend_bool _http_encoding_response_start(size_t content_length TSRMLS_DC)
672 {
673 if ( php_ob_handler_used("ob_gzhandler" TSRMLS_CC) ||
674 php_ob_handler_used("zlib output compression" TSRMLS_CC)) {
675 HTTP_G(send).gzip_encoding = 0;
676 } else {
677 if (!HTTP_G(send).gzip_encoding) {
678 /* emit a content-length header */
679 if (content_length) {
680 char cl_header_str[128];
681 size_t cl_header_len;
682 cl_header_len = snprintf(cl_header_str, lenof(cl_header_str), "Content-Length: %zu", content_length);
683 http_send_header_string_ex(cl_header_str, cl_header_len, 1);
684 }
685 } else {
686 #ifndef HTTP_HAVE_ZLIB
687 HTTP_G(send).gzip_encoding = 0;
688 php_start_ob_buffer_named("ob_gzhandler", 0, 0 TSRMLS_CC);
689 #else
690 HashTable *selected;
691 zval zsupported;
692
693 INIT_PZVAL(&zsupported);
694 array_init(&zsupported);
695 add_next_index_stringl(&zsupported, "gzip", lenof("gzip"), 1);
696 add_next_index_stringl(&zsupported, "x-gzip", lenof("x-gzip"), 1);
697 add_next_index_stringl(&zsupported, "deflate", lenof("deflate"), 1);
698
699 HTTP_G(send).gzip_encoding = 0;
700
701 if ((selected = http_negotiate_encoding(&zsupported))) {
702 STATUS hs = FAILURE;
703 char *encoding = NULL;
704 ulong idx;
705
706 if (HASH_KEY_IS_STRING == zend_hash_get_current_key(selected, &encoding, &idx, 0) && encoding) {
707 if (!strcmp(encoding, "gzip") || !strcmp(encoding, "x-gzip")) {
708 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: gzip"))) {
709 HTTP_G(send).gzip_encoding = HTTP_ENCODING_GZIP;
710 }
711 } else if (!strcmp(encoding, "deflate")) {
712 if (SUCCESS == (hs = http_send_header_string("Content-Encoding: deflate"))) {
713 HTTP_G(send).gzip_encoding = HTTP_ENCODING_DEFLATE;
714 }
715 }
716 if (SUCCESS == hs) {
717 http_send_header_string("Vary: Accept-Encoding");
718 }
719 }
720
721 zend_hash_destroy(selected);
722 FREE_HASHTABLE(selected);
723 }
724
725 zval_dtor(&zsupported);
726 return HTTP_G(send).gzip_encoding;
727 #endif
728 }
729 }
730 return 0;
731 }
732
733 /*
734 * Local variables:
735 * tab-width: 4
736 * c-basic-offset: 4
737 * End:
738 * vim600: noet sw=4 ts=4 fdm=marker
739 * vim<600: noet sw=4 ts=4
740 */
741