import 2.0 devl branch, suitable for PHP-trunk
[m6w6/ext-http] / php_http_encoding.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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id: http_encoding_api.c 298592 2010-04-26 11:47:29Z mike $ */
14
15 #include "php_http.h"
16
17 static inline int eol_match(char **line, int *eol_len)
18 {
19 char *ptr = *line;
20
21 while (' ' == *ptr) ++ptr;
22
23 if (ptr == php_http_locate_eol(*line, eol_len)) {
24 *line = ptr;
25 return 1;
26 } else {
27 return 0;
28 }
29 }
30
31 PHP_HTTP_API const char *php_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
32 {
33 int eol_len = 0;
34 char *n_ptr = NULL;
35 const char *e_ptr = encoded;
36
37 *decoded_len = 0;
38 *decoded = ecalloc(1, encoded_len);
39
40 while ((encoded + encoded_len - e_ptr) > 0) {
41 ulong chunk_len = 0, rest;
42
43 chunk_len = strtoul(e_ptr, &n_ptr, 16);
44
45 /* we could not read in chunk size */
46 if (n_ptr == e_ptr) {
47 /*
48 * if this is the first turn and there doesn't seem to be a chunk
49 * size at the begining of the body, do not fail on apparently
50 * not encoded data and return a copy
51 */
52 if (e_ptr == encoded) {
53 php_http_error(HE_NOTICE, PHP_HTTP_E_ENCODING, "Data does not seem to be chunked encoded");
54 memcpy(*decoded, encoded, encoded_len);
55 *decoded_len = encoded_len;
56 return encoded + encoded_len;
57 } else {
58 efree(*decoded);
59 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Expected chunk size at pos %tu of %zu but got trash", n_ptr - encoded, encoded_len);
60 return NULL;
61 }
62 }
63
64 /* reached the end */
65 if (!chunk_len) {
66 /* move over '0' chunked encoding terminator and any new lines */
67 do {
68 switch (*e_ptr) {
69 case '0':
70 case '\r':
71 case '\n':
72 ++e_ptr;
73 continue;
74 }
75 } while (0);
76 break;
77 }
78
79 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
80 if (*n_ptr && !eol_match(&n_ptr, &eol_len)) {
81 if (eol_len == 2) {
82 php_http_error(HE_WARNING, PHP_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));
83 } else {
84 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Expected LF at pos %tu of %zu but got 0x%02X", n_ptr - encoded, encoded_len, *n_ptr);
85 }
86 }
87 n_ptr += eol_len;
88
89 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
90 if (chunk_len > (rest = encoded + encoded_len - n_ptr)) {
91 php_http_error(HE_WARNING, PHP_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);
92 chunk_len = rest;
93 }
94
95 /* copy the chunk */
96 memcpy(*decoded + *decoded_len, n_ptr, chunk_len);
97 *decoded_len += chunk_len;
98
99 if (chunk_len == rest) {
100 e_ptr = n_ptr + chunk_len;
101 break;
102 } else {
103 /* advance to next chunk */
104 e_ptr = n_ptr + chunk_len + eol_len;
105 }
106 }
107
108 return e_ptr;
109 }
110
111 static inline int php_http_inflate_rounds(z_stream *Z, int flush, char **buf, size_t *len)
112 {
113 int status = 0, round = 0;
114 php_http_buffer buffer;
115
116 *buf = NULL;
117 *len = 0;
118
119 php_http_buffer_init_ex(&buffer, Z->avail_in, PHP_HTTP_BUFFER_INIT_PREALLOC);
120
121 do {
122 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize_ex(&buffer, buffer.size, 0, 1)) {
123 status = Z_MEM_ERROR;
124 } else {
125 Z->avail_out = buffer.free;
126 Z->next_out = (Bytef *) buffer.data + buffer.used;
127 #if 0
128 fprintf(stderr, "\n%3d: %3d PRIOR: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
129 #endif
130 status = inflate(Z, flush);
131
132 buffer.used += buffer.free - Z->avail_out;
133 buffer.free = Z->avail_out;
134 #if 0
135 fprintf(stderr, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round, status, buffer.size, buffer.free, buffer.used, Z->avail_in, Z->avail_out);
136 #endif
137 PHP_HTTP_INFLATE_BUFFER_SIZE_ALIGN(buffer.size);
138 }
139 } while ((Z_BUF_ERROR == status || (Z_OK == status && Z->avail_in)) && ++round < PHP_HTTP_INFLATE_ROUNDS);
140
141 if (status == Z_OK || status == Z_STREAM_END) {
142 php_http_buffer_shrink(&buffer);
143 php_http_buffer_fix(&buffer);
144 *buf = buffer.data;
145 *len = buffer.used;
146 } else {
147 php_http_buffer_dtor(&buffer);
148 }
149
150 return status;
151 }
152
153 PHP_HTTP_API STATUS php_http_encoding_deflate(int flags, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
154 {
155 int status, level, wbits, strategy;
156 z_stream Z;
157
158 PHP_HTTP_DEFLATE_LEVEL_SET(flags, level);
159 PHP_HTTP_DEFLATE_WBITS_SET(flags, wbits);
160 PHP_HTTP_DEFLATE_STRATEGY_SET(flags, strategy);
161
162 memset(&Z, 0, sizeof(z_stream));
163 *encoded = NULL;
164 *encoded_len = 0;
165
166 status = deflateInit2(&Z, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy);
167 if (Z_OK == status) {
168 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
169 *encoded = emalloc(*encoded_len);
170
171 Z.next_in = (Bytef *) data;
172 Z.next_out = (Bytef *) *encoded;
173 Z.avail_in = data_len;
174 Z.avail_out = *encoded_len;
175
176 status = deflate(&Z, Z_FINISH);
177 deflateEnd(&Z);
178
179 if (Z_STREAM_END == status) {
180 /* size buffer down to actual length */
181 *encoded = erealloc(*encoded, Z.total_out + 1);
182 (*encoded)[*encoded_len = Z.total_out] = '\0';
183 return SUCCESS;
184 } else {
185 STR_SET(*encoded, NULL);
186 *encoded_len = 0;
187 }
188 }
189
190 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
191 return FAILURE;
192 }
193
194 PHP_HTTP_API STATUS php_http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
195 {
196 z_stream Z;
197 int status, wbits = PHP_HTTP_WINDOW_BITS_ANY;
198
199 memset(&Z, 0, sizeof(z_stream));
200
201 retry_raw_inflate:
202 status = inflateInit2(&Z, wbits);
203 if (Z_OK == status) {
204 Z.next_in = (Bytef *) data;
205 Z.avail_in = data_len;
206
207 switch (status = php_http_inflate_rounds(&Z, Z_NO_FLUSH, decoded, decoded_len)) {
208 case Z_STREAM_END:
209 inflateEnd(&Z);
210 return SUCCESS;
211
212 case Z_OK:
213 status = Z_DATA_ERROR;
214 break;
215
216 case Z_DATA_ERROR:
217 /* raw deflated data? */
218 if (PHP_HTTP_WINDOW_BITS_ANY == wbits) {
219 inflateEnd(&Z);
220 wbits = PHP_HTTP_WINDOW_BITS_RAW;
221 goto retry_raw_inflate;
222 }
223 }
224 inflateEnd(&Z);
225
226 if (decoded_len && *decoded) {
227 efree(*decoded);
228 }
229 }
230
231 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
232 return FAILURE;
233 }
234
235 PHP_HTTP_API php_http_encoding_stream_t *php_http_encoding_stream_init(php_http_encoding_stream_t *s, php_http_encoding_stream_ops_t *ops, unsigned flags TSRMLS_DC)
236 {
237 int freeme;
238
239 if ((freeme = !s)) {
240 s = pemalloc(sizeof(*s), (flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
241 }
242 memset(s, 0, sizeof(*s));
243
244 s->flags = flags;
245 TSRMLS_SET_CTX(s->ts);
246
247 if ((s->ops = ops)) {
248 php_http_encoding_stream_t *ss = s->ops->init(s);
249
250 if (ss) {
251 return ss;
252 }
253 } else {
254 return s;
255 }
256
257 if (freeme) {
258 pefree(s, (flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
259 }
260 return NULL;
261 }
262
263 PHP_HTTP_API php_http_encoding_stream_t *php_http_encoding_stream_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
264 {
265 TSRMLS_FETCH_FROM_CTX(from->ts);
266
267 if (!from->ops->copy) {
268 return NULL;
269 }
270
271 return from->ops->copy(from, php_http_encoding_stream_init(to, from->ops, from->flags TSRMLS_CC));
272 }
273
274 PHP_HTTP_API STATUS php_http_encoding_stream_reset(php_http_encoding_stream_t **s)
275 {
276 php_http_encoding_stream_t *ss;
277 if ((*s)->ops->dtor) {
278 (*s)->ops->dtor(*s);
279 }
280 if ((ss = (*s)->ops->init(*s))) {
281 *s = ss;
282 return SUCCESS;
283 }
284 return FAILURE;
285 }
286
287 PHP_HTTP_API STATUS php_http_encoding_stream_update(php_http_encoding_stream_t *s, const char *in_str, size_t in_len, char **out_str, size_t *out_len)
288 {
289 if (!s->ops->update) {
290 return FAILURE;
291 }
292 return s->ops->update(s, in_str, in_len, out_str, out_len);
293 }
294
295 PHP_HTTP_API STATUS php_http_encoding_stream_flush(php_http_encoding_stream_t *s, char **out_str, size_t *out_len)
296 {
297 if (!s->ops->flush) {
298 return SUCCESS;
299 }
300 return s->ops->flush(s, out_str, out_len);
301 }
302
303 PHP_HTTP_API zend_bool php_http_encoding_stream_done(php_http_encoding_stream_t *s)
304 {
305 if (!s->ops->done) {
306 return 0;
307 }
308 return s->ops->done(s);
309 }
310
311 PHP_HTTP_API STATUS php_http_encoding_stream_finish(php_http_encoding_stream_t *s, char **out_str, size_t *out_len)
312 {
313 if (!s->ops->finish) {
314 return SUCCESS;
315 }
316 return s->ops->finish(s, out_str, out_len);
317 }
318
319 PHP_HTTP_API void php_http_encoding_stream_dtor(php_http_encoding_stream_t *s)
320 {
321 if (s->ops->dtor) {
322 s->ops->dtor(s);
323 }
324 }
325
326 PHP_HTTP_API void php_http_encoding_stream_free(php_http_encoding_stream_t **s)
327 {
328 if (*s) {
329 if ((*s)->ops->dtor) {
330 (*s)->ops->dtor(*s);
331 }
332 pefree(*s, ((*s)->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
333 *s = NULL;
334 }
335 }
336
337 struct dechunk_ctx {
338 php_http_buffer buffer;
339 ulong hexlen;
340 unsigned zeroed:1;
341 };
342
343 static php_http_encoding_stream_t *deflate_init(php_http_encoding_stream_t *s)
344 {
345 int status, level, wbits, strategy, p = (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT);
346 z_streamp ctx = pecalloc(1, sizeof(z_stream), p);
347 TSRMLS_FETCH_FROM_CTX(s->ts);
348
349 PHP_HTTP_DEFLATE_LEVEL_SET(s->flags, level);
350 PHP_HTTP_DEFLATE_WBITS_SET(s->flags, wbits);
351 PHP_HTTP_DEFLATE_STRATEGY_SET(s->flags, strategy);
352
353 if (Z_OK == (status = deflateInit2(ctx, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
354 if ((ctx->opaque = php_http_buffer_init_ex(NULL, PHP_HTTP_DEFLATE_BUFFER_SIZE, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0))) {
355 s->ctx = ctx;
356 return s;
357 }
358 deflateEnd(ctx);
359 status = Z_MEM_ERROR;
360 }
361 pefree(ctx, p);
362 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to initialize deflate encoding stream: %s", zError(status));
363 return NULL;
364 }
365
366 static php_http_encoding_stream_t *inflate_init(php_http_encoding_stream_t *s)
367 {
368 int status, wbits, p = (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT);
369 z_streamp ctx = pecalloc(1, sizeof(z_stream), p);
370 TSRMLS_FETCH_FROM_CTX(s->ts);
371
372 PHP_HTTP_INFLATE_WBITS_SET(s->flags, wbits);
373
374 if (Z_OK == (status = inflateInit2(ctx, wbits))) {
375 if ((ctx->opaque = php_http_buffer_init_ex(NULL, PHP_HTTP_DEFLATE_BUFFER_SIZE, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0))) {
376 s->ctx = ctx;
377 return s;
378 }
379 inflateEnd(ctx);
380 status = Z_MEM_ERROR;
381 }
382 pefree(ctx, p);
383 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to initialize inflate stream: %s", zError(status));
384 return NULL;
385 }
386
387 static php_http_encoding_stream_t *dechunk_init(php_http_encoding_stream_t *s)
388 {
389 struct dechunk_ctx *ctx = pecalloc(1, sizeof(*ctx), (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
390 TSRMLS_FETCH_FROM_CTX(s->ts);
391
392 if (!php_http_buffer_init_ex(&ctx->buffer, PHP_HTTP_BUFFER_DEFAULT_SIZE, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT) ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0)) {
393 return NULL;
394 }
395
396 ctx->hexlen = 0;
397 ctx->zeroed = 0;
398 s->ctx = ctx;
399
400 return s;
401 }
402
403 static php_http_encoding_stream_t *deflate_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
404 {
405 z_streamp from_ctx = from->ctx, to_ctx = to->ctx;
406
407 deflateCopy(to_ctx, from_ctx);
408 php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
409
410 return to;
411 }
412
413 static php_http_encoding_stream_t *inflate_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
414 {
415 z_streamp from_ctx = from->ctx, to_ctx = to->ctx;
416
417 inflateCopy(to_ctx, from_ctx);
418 php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
419
420 return to;
421 }
422
423 static php_http_encoding_stream_t *dechunk_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
424 {
425 struct dechunk_ctx *from_ctx = from->ctx, *to_ctx = to->ctx;
426
427 to_ctx->hexlen = from_ctx->hexlen;
428 to_ctx->zeroed = from_ctx->zeroed;
429 php_http_buffer_append(&to_ctx->buffer, PHP_HTTP_BUFFER_VAL(&from_ctx->buffer), PHP_HTTP_BUFFER_LEN(&from_ctx->buffer));
430
431 return to;
432 }
433
434 static STATUS deflate_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len)
435 {
436 int status;
437 z_streamp ctx = s->ctx;
438 TSRMLS_FETCH_FROM_CTX(s->ts);
439
440 /* append input to our buffer */
441 php_http_buffer_append(PHP_HTTP_BUFFER(ctx->opaque), data, data_len);
442
443 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
444 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
445
446 /* deflate */
447 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
448 *encoded = emalloc(*encoded_len);
449 ctx->avail_out = *encoded_len;
450 ctx->next_out = (Bytef *) *encoded;
451
452 switch (status = deflate(ctx, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags))) {
453 case Z_OK:
454 case Z_STREAM_END:
455 /* cut processed chunk off the buffer */
456 if (ctx->avail_in) {
457 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
458 } else {
459 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx->opaque));
460 }
461
462 /* size buffer down to actual size */
463 *encoded_len -= ctx->avail_out;
464 *encoded = erealloc(*encoded, *encoded_len + 1);
465 (*encoded)[*encoded_len] = '\0';
466 return SUCCESS;
467 }
468
469 STR_SET(*encoded, NULL);
470 *encoded_len = 0;
471 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
472 return FAILURE;
473 }
474
475 static STATUS inflate_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len)
476 {
477 int status;
478 z_streamp ctx = s->ctx;
479 TSRMLS_FETCH_FROM_CTX(s->ts);
480
481 /* append input to buffer */
482 php_http_buffer_append(PHP_HTTP_BUFFER(ctx->opaque), data, data_len);
483
484 retry_raw_inflate:
485 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
486 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
487
488 switch (status = php_http_inflate_rounds(ctx, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags), decoded, decoded_len)) {
489 case Z_OK:
490 case Z_STREAM_END:
491 /* cut off */
492 if (ctx->avail_in) {
493 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
494 } else {
495 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx->opaque));
496 }
497 return SUCCESS;
498
499 case Z_DATA_ERROR:
500 /* raw deflated data ? */
501 if (!(s->flags & PHP_HTTP_INFLATE_TYPE_RAW) && !ctx->total_out) {
502 inflateEnd(ctx);
503 s->flags |= PHP_HTTP_INFLATE_TYPE_RAW;
504 inflateInit2(ctx, PHP_HTTP_WINDOW_BITS_RAW);
505 goto retry_raw_inflate;
506 }
507 }
508
509 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
510 return FAILURE;
511 }
512
513 static STATUS dechunk_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len)
514 {
515 php_http_buffer tmp;
516 struct dechunk_ctx *ctx = s->ctx;
517 TSRMLS_FETCH_FROM_CTX(s->ts);
518
519 if (ctx->zeroed) {
520 return FAILURE;
521 }
522 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(&ctx->buffer, data, data_len)) {
523 return FAILURE;
524 }
525 if (!php_http_buffer_fix(&ctx->buffer)) {
526 return FAILURE;
527 }
528
529 *decoded = NULL;
530 *decoded_len = 0;
531
532 php_http_buffer_init(&tmp);
533
534 /* we have data in our buffer */
535 while (PHP_HTTP_BUFFER_LEN(&ctx->buffer)) {
536
537 /* we already know the size of the chunk and are waiting for data */
538 if (ctx->hexlen) {
539
540 /* not enough data buffered */
541 if (PHP_HTTP_BUFFER_LEN(&ctx->buffer) < ctx->hexlen) {
542
543 /* flush anyway? */
544 if (s->flags & PHP_HTTP_ENCODING_STREAM_FLUSH_FULL) {
545 /* flush all data (should only be chunk data) */
546 php_http_buffer_append(&tmp, PHP_HTTP_BUFFER_VAL(&ctx->buffer), PHP_HTTP_BUFFER_LEN(&ctx->buffer));
547 /* waiting for less data now */
548 ctx->hexlen -= PHP_HTTP_BUFFER_LEN(&ctx->buffer);
549 /* no more buffered data */
550 php_http_buffer_reset(&ctx->buffer);
551 /* break */
552 }
553
554 /* we have too less data and don't need to flush */
555 else {
556 break;
557 }
558 }
559
560 /* we seem to have all data of the chunk */
561 else {
562 php_http_buffer_append(&tmp, PHP_HTTP_BUFFER_VAL(&ctx->buffer), ctx->hexlen);
563 /* remove outgoing data from the buffer */
564 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, ctx->hexlen);
565 /* reset hexlen */
566 ctx->hexlen = 0;
567 /* continue */
568 }
569 }
570
571 /* we don't know the length of the chunk yet */
572 else {
573 size_t off = 0;
574
575 /* ignore preceeding CRLFs (too loose?) */
576 while (off < PHP_HTTP_BUFFER_LEN(&ctx->buffer) && (
577 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\n' ||
578 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\r')) {
579 ++off;
580 }
581 if (off) {
582 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, off);
583 }
584
585 /* still data there? */
586 if (PHP_HTTP_BUFFER_LEN(&ctx->buffer)) {
587 int eollen;
588 const char *eolstr;
589
590 /* we need eol, so we can be sure we have all hex digits */
591 php_http_buffer_fix(&ctx->buffer);
592 if ((eolstr = php_http_locate_bin_eol(PHP_HTTP_BUFFER_VAL(&ctx->buffer), PHP_HTTP_BUFFER_LEN(&ctx->buffer), &eollen))) {
593 char *stop = NULL;
594
595 /* read in chunk size */
596 ctx->hexlen = strtoul(PHP_HTTP_BUFFER_VAL(&ctx->buffer), &stop, 16);
597
598 /* if strtoul() stops at the beginning of the buffered data
599 there's domething oddly wrong, i.e. bad input */
600 if (stop == PHP_HTTP_BUFFER_VAL(&ctx->buffer)) {
601 php_http_buffer_dtor(&tmp);
602 return FAILURE;
603 }
604
605 /* cut out <chunk size hex><chunk extension><eol> */
606 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, eolstr + eollen - PHP_HTTP_BUFFER_VAL(&ctx->buffer));
607 /* buffer->hexlen is 0 now or contains the size of the next chunk */
608 if (!ctx->hexlen) {
609 size_t off = 0;
610
611 /* ignore following CRLFs (too loose?) */
612 while (off < PHP_HTTP_BUFFER_LEN(&ctx->buffer) && (
613 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\n' ||
614 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\r')) {
615 ++off;
616 }
617 if (off) {
618 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, off);
619 }
620
621 ctx->zeroed = 1;
622 break;
623 }
624 /* continue */
625 } else {
626 /* we have not enough data buffered to read in chunk size */
627 break;
628 }
629 }
630 /* break */
631 }
632 }
633
634 php_http_buffer_fix(&tmp);
635 *decoded = PHP_HTTP_BUFFER_VAL(&tmp);
636 *decoded_len = PHP_HTTP_BUFFER_LEN(&tmp);
637
638 return SUCCESS;
639 }
640
641 static STATUS deflate_flush(php_http_encoding_stream_t *s, char **encoded, size_t *encoded_len)
642 {
643 int status;
644 z_streamp ctx = s->ctx;
645 TSRMLS_FETCH_FROM_CTX(s->ts);
646
647 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE;
648 *encoded = emalloc(*encoded_len);
649
650 ctx->avail_in = 0;
651 ctx->next_in = NULL;
652 ctx->avail_out = *encoded_len;
653 ctx->next_out = (Bytef *) *encoded;
654
655 switch (status = deflate(ctx, Z_FULL_FLUSH)) {
656 case Z_OK:
657 case Z_STREAM_END:
658 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE - ctx->avail_out;
659 *encoded = erealloc(*encoded, *encoded_len + 1);
660 (*encoded)[*encoded_len] = '\0';
661 return SUCCESS;
662 }
663
664 STR_SET(*encoded, NULL);
665 *encoded_len = 0;
666 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
667 return FAILURE;
668 }
669
670 static STATUS dechunk_flush(php_http_encoding_stream_t *s, char **decoded, size_t *decoded_len)
671 {
672 struct dechunk_ctx *ctx = s->ctx;
673
674 if (ctx->hexlen) {
675 /* flush all data (should only be chunk data) */
676 php_http_buffer_fix(&ctx->buffer);
677 php_http_buffer_data(&ctx->buffer, decoded, decoded_len);
678 /* waiting for less data now */
679 ctx->hexlen -= PHP_HTTP_BUFFER_LEN(&ctx->buffer);
680 /* no more buffered data */
681 php_http_buffer_reset(&ctx->buffer);
682 } else {
683 *decoded = NULL;
684 *decoded_len = 0;
685 }
686
687 return SUCCESS;
688 }
689
690 static STATUS deflate_finish(php_http_encoding_stream_t *s, char **encoded, size_t *encoded_len)
691 {
692 int status;
693 z_streamp ctx = s->ctx;
694 TSRMLS_FETCH_FROM_CTX(s->ts);
695
696 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE;
697 *encoded = emalloc(*encoded_len);
698
699 /* deflate remaining input */
700 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
701 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
702
703 ctx->avail_out = *encoded_len;
704 ctx->next_out = (Bytef *) *encoded;
705
706 do {
707 status = deflate(ctx, Z_FINISH);
708 } while (Z_OK == status);
709
710 if (Z_STREAM_END == status) {
711 /* cut processed intp off */
712 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
713
714 /* size down */
715 *encoded_len -= ctx->avail_out;
716 *encoded = erealloc(*encoded, *encoded_len + 1);
717 (*encoded)[*encoded_len] = '\0';
718 return SUCCESS;
719 }
720
721 STR_SET(*encoded, NULL);
722 *encoded_len = 0;
723 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
724 return FAILURE;
725 }
726
727 static STATUS inflate_finish(php_http_encoding_stream_t *s, char **decoded, size_t *decoded_len)
728 {
729 int status;
730 z_streamp ctx = s->ctx;
731 TSRMLS_FETCH_FROM_CTX(s->ts);
732
733 if (!PHP_HTTP_BUFFER_LEN(ctx->opaque)) {
734 *decoded = NULL;
735 *decoded_len = 0;
736 return SUCCESS;
737 }
738
739 *decoded_len = (PHP_HTTP_BUFFER_LEN(ctx->opaque) + 1) * PHP_HTTP_INFLATE_ROUNDS;
740 *decoded = emalloc(*decoded_len);
741
742 /* inflate remaining input */
743 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
744 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
745
746 ctx->avail_out = *decoded_len;
747 ctx->next_out = (Bytef *) *decoded;
748
749 if (Z_STREAM_END == (status = inflate(ctx, Z_FINISH))) {
750 /* cut processed input off */
751 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
752
753 /* size down */
754 *decoded_len -= ctx->avail_out;
755 *decoded = erealloc(*decoded, *decoded_len + 1);
756 (*decoded)[*decoded_len] = '\0';
757 return SUCCESS;
758 }
759
760 STR_SET(*decoded, NULL);
761 *decoded_len = 0;
762 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
763 return FAILURE;
764 }
765
766 static zend_bool deflate_done(php_http_encoding_stream_t *s)
767 {
768 z_streamp ctx = s->ctx;
769 return !ctx->avail_in && !PHP_HTTP_BUFFER_LEN(ctx->opaque);
770 }
771
772 static zend_bool inflate_done(php_http_encoding_stream_t *s)
773 {
774 z_streamp ctx = s->ctx;
775 return !ctx->avail_in && !PHP_HTTP_BUFFER_LEN(ctx->opaque);
776 }
777
778 static zend_bool dechunk_done(php_http_encoding_stream_t *s)
779 {
780 return ((struct dechunk_ctx *) s->ctx)->zeroed;
781 }
782
783 static void deflate_dtor(php_http_encoding_stream_t *s)
784 {
785 if (s->ctx) {
786 z_streamp ctx = s->ctx;
787
788 if (ctx->opaque) {
789 php_http_buffer_free((php_http_buffer **) &ctx->opaque);
790 }
791 deflateEnd(ctx);
792 pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
793 s->ctx = NULL;
794 }
795 }
796
797 static void inflate_dtor(php_http_encoding_stream_t *s)
798 {
799 if (s->ctx) {
800 z_streamp ctx = s->ctx;
801
802 if (ctx->opaque) {
803 php_http_buffer_free((php_http_buffer **) &ctx->opaque);
804 }
805 inflateEnd(ctx);
806 pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
807 s->ctx = NULL;
808 }
809 }
810
811 static void dechunk_dtor(php_http_encoding_stream_t *s)
812 {
813 if (s->ctx) {
814 struct dechunk_ctx *ctx = s->ctx;
815
816 php_http_buffer_dtor(&ctx->buffer);
817 pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
818 s->ctx = NULL;
819 }
820 }
821
822 static php_http_encoding_stream_ops_t php_http_encoding_deflate_ops = {
823 deflate_init,
824 deflate_copy,
825 deflate_update,
826 deflate_flush,
827 deflate_done,
828 deflate_finish,
829 deflate_dtor
830 };
831
832 PHP_HTTP_API php_http_encoding_stream_ops_t *php_http_encoding_stream_get_deflate_ops(void)
833 {
834 return &php_http_encoding_deflate_ops;
835 }
836
837 static php_http_encoding_stream_ops_t php_http_encoding_inflate_ops = {
838 inflate_init,
839 inflate_copy,
840 inflate_update,
841 NULL,
842 inflate_done,
843 inflate_finish,
844 inflate_dtor
845 };
846
847 PHP_HTTP_API php_http_encoding_stream_ops_t *php_http_encoding_stream_get_inflate_ops(void)
848 {
849 return &php_http_encoding_inflate_ops;
850 }
851
852 static php_http_encoding_stream_ops_t php_http_encoding_dechunk_ops = {
853 dechunk_init,
854 dechunk_copy,
855 dechunk_update,
856 dechunk_flush,
857 dechunk_done,
858 NULL,
859 dechunk_dtor
860 };
861
862 PHP_HTTP_API php_http_encoding_stream_ops_t *php_http_encoding_stream_get_dechunk_ops(void)
863 {
864 return &php_http_encoding_dechunk_ops;
865 }
866
867 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEncodingStream, method, 0, req_args)
868 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEncodingStream, method, 0)
869 #define PHP_HTTP_ENCSTREAM_ME(method, visibility) PHP_ME(HttpEncodingStream, method, PHP_HTTP_ARGS(HttpEncodingStream, method), visibility)
870
871 PHP_HTTP_BEGIN_ARGS(__construct, 0)
872 PHP_HTTP_ARG_VAL(flags, 0)
873 PHP_HTTP_END_ARGS;
874
875 PHP_HTTP_BEGIN_ARGS(update, 1)
876 PHP_HTTP_ARG_VAL(data, 0)
877 PHP_HTTP_END_ARGS;
878
879 PHP_HTTP_EMPTY_ARGS(flush);
880 PHP_HTTP_EMPTY_ARGS(done);
881 PHP_HTTP_EMPTY_ARGS(finish);
882
883 zend_class_entry *php_http_encoding_stream_class_entry;
884 zend_function_entry php_http_encoding_stream_method_entry[] = {
885 PHP_HTTP_ENCSTREAM_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
886 PHP_HTTP_ENCSTREAM_ME(update, ZEND_ACC_PUBLIC)
887 PHP_HTTP_ENCSTREAM_ME(flush, ZEND_ACC_PUBLIC)
888 PHP_HTTP_ENCSTREAM_ME(done, ZEND_ACC_PUBLIC)
889 PHP_HTTP_ENCSTREAM_ME(finish, ZEND_ACC_PUBLIC)
890
891 EMPTY_FUNCTION_ENTRY
892 };
893 static zend_object_handlers php_http_encoding_stream_object_handlers;
894
895 zend_object_value php_http_encoding_stream_object_new(zend_class_entry *ce TSRMLS_DC)
896 {
897 return php_http_encoding_stream_object_new_ex(ce, NULL, NULL TSRMLS_CC);
898 }
899
900 zend_object_value php_http_encoding_stream_object_new_ex(zend_class_entry *ce, php_http_encoding_stream_t *s, php_http_encoding_stream_object_t **ptr TSRMLS_DC)
901 {
902 zend_object_value ov;
903 php_http_encoding_stream_object_t *o;
904
905 o = ecalloc(1, sizeof(*o));
906 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
907 object_properties_init((zend_object *) o, ce);
908
909 if (ptr) {
910 *ptr = o;
911 }
912
913 if (s) {
914 o->stream = s;
915 }
916
917 ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_encoding_stream_object_free, NULL TSRMLS_CC);
918 ov.handlers = &php_http_encoding_stream_object_handlers;
919
920 return ov;
921 }
922
923 zend_object_value php_http_encoding_stream_object_clone(zval *this_ptr TSRMLS_DC)
924 {
925 zend_object_value new_ov;
926 php_http_encoding_stream_object_t *new_obj = NULL, *old_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
927
928 new_ov = php_http_encoding_stream_object_new_ex(old_obj->zo.ce, php_http_encoding_stream_copy(old_obj->stream, NULL), &new_obj TSRMLS_CC);
929 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
930
931 return new_ov;
932 }
933
934 void php_http_encoding_stream_object_free(void *object TSRMLS_DC)
935 {
936 php_http_encoding_stream_object_t *o = (php_http_encoding_stream_object_t *) object;
937
938 if (o->stream) {
939 php_http_encoding_stream_free(&o->stream TSRMLS_CC);
940 }
941 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
942 efree(o);
943 }
944
945 PHP_METHOD(HttpEncodingStream, __construct)
946 {
947 with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) {
948 long flags = 0;
949
950 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
951 with_error_handling(EH_THROW, PHP_HTTP_EX_CE(encoding)) {
952 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
953
954 if (!obj->stream) {
955 php_http_encoding_stream_ops_t *ops = NULL;
956
957 if (instanceof_function(obj->zo.ce, php_http_deflate_stream_class_entry TSRMLS_CC)) {
958 ops = &php_http_encoding_deflate_ops;
959 } else if (instanceof_function(obj->zo.ce, php_http_inflate_stream_class_entry TSRMLS_CC)) {
960 ops = &php_http_encoding_inflate_ops;
961 } else if (instanceof_function(obj->zo.ce, php_http_dechunk_stream_class_entry TSRMLS_CC)) {
962 ops = &php_http_encoding_dechunk_ops;
963 }
964
965 if (ops) {
966 obj->stream = php_http_encoding_stream_init(obj->stream, ops, flags TSRMLS_CC);
967 } else {
968 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Unknown HttpEncodingStream class %s", obj->zo.ce->name);
969 }
970
971 } else {
972 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "HttpEncodingStream cannot be initialized twice");
973 }
974 } end_error_handling();
975 }
976 } end_error_handling();
977 }
978
979 PHP_METHOD(HttpEncodingStream, update)
980 {
981 int data_len;
982 char *data_str;
983
984 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data_str, &data_len)) {
985 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
986
987 if (obj->stream) {
988 size_t encoded_len;
989 char *encoded_str;
990
991 if (SUCCESS == php_http_encoding_stream_update(obj->stream, data_str, data_len, &encoded_str, &encoded_len TSRMLS_CC)) {
992 RETURN_STRINGL(encoded_str, encoded_len, 0);
993 }
994 }
995 }
996 RETURN_FALSE;
997 }
998
999 PHP_METHOD(HttpEncodingStream, flush)
1000 {
1001 if (SUCCESS == zend_parse_parameters_none()) {
1002 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1003
1004 if (obj->stream) {
1005 char *encoded_str;
1006 size_t encoded_len;
1007
1008 if (SUCCESS == php_http_encoding_stream_flush(obj->stream, &encoded_str, &encoded_len TSRMLS_CC)) {
1009 RETURN_STRINGL(encoded_str, encoded_len, 0);
1010 }
1011 }
1012 }
1013 RETURN_FALSE;
1014 }
1015
1016 PHP_METHOD(HttpEncodingStream, done)
1017 {
1018 if (SUCCESS == zend_parse_parameters_none()) {
1019 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1020
1021 if (obj->stream) {
1022 RETURN_BOOL(php_http_encoding_stream_done(obj->stream));
1023 }
1024 }
1025 RETURN_FALSE;
1026 }
1027
1028 PHP_METHOD(HttpEncodingStream, finish)
1029 {
1030 if (SUCCESS == zend_parse_parameters_none()) {
1031 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1032
1033 if (obj->stream) {
1034 char *encoded_str;
1035 size_t encoded_len;
1036
1037 if (SUCCESS == php_http_encoding_stream_finish(obj->stream, &encoded_str, &encoded_len TSRMLS_CC)) {
1038 if (SUCCESS == php_http_encoding_stream_reset(&obj->stream)) {
1039 RETURN_STRINGL(encoded_str, encoded_len, 0);
1040 } else {
1041 STR_FREE(encoded_str);
1042 }
1043 }
1044 }
1045 }
1046 RETURN_FALSE;
1047 }
1048
1049 #undef PHP_HTTP_BEGIN_ARGS
1050 #undef PHP_HTTP_EMPTY_ARGS
1051 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpDeflateStream, method, 0, req_args)
1052 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpDeflateStream, method, 0)
1053 #define PHP_HTTP_DEFLATE_ME(method, visibility) PHP_ME(HttpDeflateStream, method, PHP_HTTP_ARGS(HttpDeflateStream, method), visibility)
1054
1055 PHP_HTTP_BEGIN_ARGS(encode, 1)
1056 PHP_HTTP_ARG_VAL(data, 0)
1057 PHP_HTTP_ARG_VAL(flags, 0)
1058 PHP_HTTP_END_ARGS;
1059
1060 zend_class_entry *php_http_deflate_stream_class_entry;
1061 zend_function_entry php_http_deflate_stream_method_entry[] = {
1062 PHP_HTTP_DEFLATE_ME(encode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1063
1064 EMPTY_FUNCTION_ENTRY
1065 };
1066
1067 PHP_METHOD(HttpDeflateStream, encode)
1068 {
1069 char *str;
1070 int len;
1071 long flags = 0;
1072
1073 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &len, &flags)) {
1074 char *enc_str;
1075 size_t enc_len;
1076
1077 if (SUCCESS == php_http_encoding_deflate(flags, str, len, &enc_str, &enc_len TSRMLS_CC)) {
1078 RETURN_STRINGL(enc_str, enc_len, 0);
1079 }
1080 }
1081 RETURN_FALSE;
1082 }
1083
1084 #undef PHP_HTTP_BEGIN_ARGS
1085 #undef PHP_HTTP_EMPTY_ARGS
1086 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpInflateStream, method, 0, req_args)
1087 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpInflateStream, method, 0)
1088 #define PHP_HTTP_INFLATE_ME(method, visibility) PHP_ME(HttpInflateStream, method, PHP_HTTP_ARGS(HttpInflateStream, method), visibility)
1089
1090 PHP_HTTP_BEGIN_ARGS(decode, 1)
1091 PHP_HTTP_ARG_VAL(data, 0)
1092 PHP_HTTP_END_ARGS;
1093
1094 zend_class_entry *php_http_inflate_stream_class_entry;
1095 zend_function_entry php_http_inflate_stream_method_entry[] = {
1096 PHP_HTTP_INFLATE_ME(decode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1097
1098 EMPTY_FUNCTION_ENTRY
1099 };
1100
1101 PHP_METHOD(HttpInflateStream, decode)
1102 {
1103 char *str;
1104 int len;
1105
1106 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1107 char *enc_str;
1108 size_t enc_len;
1109
1110 if (SUCCESS == php_http_encoding_inflate(str, len, &enc_str, &enc_len TSRMLS_CC)) {
1111 RETURN_STRINGL(enc_str, enc_len, 0);
1112 }
1113 }
1114 RETURN_FALSE;
1115 }
1116
1117 #undef PHP_HTTP_BEGIN_ARGS
1118 #undef PHP_HTTP_EMPTY_ARGS
1119 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpDechunkStream, method, 0, req_args)
1120 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpDechunkStream, method, 0)
1121 #define PHP_HTTP_DECHUNK_ME(method, visibility) PHP_ME(HttpDechunkStream, method, PHP_HTTP_ARGS(HttpDechunkStream, method), visibility)
1122
1123 PHP_HTTP_BEGIN_ARGS(decode, 1)
1124 PHP_HTTP_ARG_VAL(data, 0)
1125 PHP_HTTP_ARG_VAL(decoded_len, 1)
1126 PHP_HTTP_END_ARGS;
1127
1128 zend_class_entry *php_http_dechunk_stream_class_entry;
1129 zend_function_entry php_http_dechunk_stream_method_entry[] = {
1130 PHP_HTTP_DECHUNK_ME(decode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1131
1132 EMPTY_FUNCTION_ENTRY
1133 };
1134
1135 PHP_METHOD(HttpDechunkStream, decode)
1136 {
1137 char *str;
1138 int len;
1139 zval *zlen;
1140
1141 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!", &str, &len, &zlen)) {
1142 const char *end_ptr;
1143 char *enc_str;
1144 size_t enc_len;
1145
1146 if ((end_ptr = php_http_encoding_dechunk(str, len, &enc_str, &enc_len TSRMLS_CC))) {
1147 if (zlen) {
1148 zval_dtor(zlen);
1149 ZVAL_LONG(zlen, str + len - end_ptr);
1150 }
1151 RETURN_STRINGL(enc_str, enc_len, 0);
1152 }
1153 }
1154 RETURN_FALSE;
1155 }
1156
1157 PHP_MINIT_FUNCTION(http_encoding)
1158 {
1159 PHP_HTTP_REGISTER_CLASS(http\\encoding, Stream, http_encoding_stream, php_http_object_class_entry, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
1160 php_http_encoding_stream_class_entry->create_object = php_http_encoding_stream_object_new;
1161 memcpy(&php_http_encoding_stream_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1162 php_http_encoding_stream_object_handlers.clone_obj = php_http_encoding_stream_object_clone;
1163
1164 zend_declare_class_constant_long(php_http_encoding_stream_class_entry, ZEND_STRL("FLUSH_NONE"), PHP_HTTP_ENCODING_STREAM_FLUSH_NONE TSRMLS_CC);
1165 zend_declare_class_constant_long(php_http_encoding_stream_class_entry, ZEND_STRL("FLUSH_SYNC"), PHP_HTTP_ENCODING_STREAM_FLUSH_SYNC TSRMLS_CC);
1166 zend_declare_class_constant_long(php_http_encoding_stream_class_entry, ZEND_STRL("FLUSH_FULL"), PHP_HTTP_ENCODING_STREAM_FLUSH_FULL TSRMLS_CC);
1167
1168 PHP_HTTP_REGISTER_CLASS(http\\encoding\\stream, Deflate, http_deflate_stream, php_http_encoding_stream_class_entry, 0);
1169
1170 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("TYPE_GZIP"), PHP_HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC);
1171 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("TYPE_ZLIB"), PHP_HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC);
1172 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("TYPE_RAW"), PHP_HTTP_DEFLATE_TYPE_RAW TSRMLS_CC);
1173 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("LEVEL_DEF"), PHP_HTTP_DEFLATE_LEVEL_DEF TSRMLS_CC);
1174 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("LEVEL_MIN"), PHP_HTTP_DEFLATE_LEVEL_MIN TSRMLS_CC);
1175 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("LEVEL_MAX"), PHP_HTTP_DEFLATE_LEVEL_MAX TSRMLS_CC);
1176 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_DEF"), PHP_HTTP_DEFLATE_STRATEGY_DEF TSRMLS_CC);
1177 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_FILT"), PHP_HTTP_DEFLATE_STRATEGY_FILT TSRMLS_CC);
1178 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_HUFF"), PHP_HTTP_DEFLATE_STRATEGY_HUFF TSRMLS_CC);
1179 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_RLE"), PHP_HTTP_DEFLATE_STRATEGY_RLE TSRMLS_CC);
1180 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_FIXED"), PHP_HTTP_DEFLATE_STRATEGY_FIXED TSRMLS_CC);
1181
1182 PHP_HTTP_REGISTER_CLASS(http\\encoding\\stream, Inflate, http_inflate_stream, php_http_encoding_stream_class_entry, 0);
1183 PHP_HTTP_REGISTER_CLASS(http\\encoding\\stream, Dechunk, http_dechunk_stream, php_http_encoding_stream_class_entry, 0);
1184
1185 return SUCCESS;
1186 }
1187
1188
1189 /*
1190 * Local variables:
1191 * tab-width: 4
1192 * c-basic-offset: 4
1193 * End:
1194 * vim600: noet sw=4 ts=4 fdm=marker
1195 * vim<600: noet sw=4 ts=4
1196 */
1197