fix problem with dechunk/inflate stream usage
[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 *out_str = NULL;
299 *out_len = 0;
300 return SUCCESS;
301 }
302 return s->ops->flush(s, out_str, out_len);
303 }
304
305 PHP_HTTP_API zend_bool php_http_encoding_stream_done(php_http_encoding_stream_t *s)
306 {
307 if (!s->ops->done) {
308 return 0;
309 }
310 return s->ops->done(s);
311 }
312
313 PHP_HTTP_API STATUS php_http_encoding_stream_finish(php_http_encoding_stream_t *s, char **out_str, size_t *out_len)
314 {
315 if (!s->ops->finish) {
316 *out_str = NULL;
317 *out_len = 0;
318 return SUCCESS;
319 }
320 return s->ops->finish(s, out_str, out_len);
321 }
322
323 PHP_HTTP_API void php_http_encoding_stream_dtor(php_http_encoding_stream_t *s)
324 {
325 if (s->ops->dtor) {
326 s->ops->dtor(s);
327 }
328 }
329
330 PHP_HTTP_API void php_http_encoding_stream_free(php_http_encoding_stream_t **s)
331 {
332 if (*s) {
333 if ((*s)->ops->dtor) {
334 (*s)->ops->dtor(*s);
335 }
336 pefree(*s, ((*s)->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
337 *s = NULL;
338 }
339 }
340
341 struct dechunk_ctx {
342 php_http_buffer buffer;
343 ulong hexlen;
344 unsigned zeroed:1;
345 };
346
347 static php_http_encoding_stream_t *deflate_init(php_http_encoding_stream_t *s)
348 {
349 int status, level, wbits, strategy, p = (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT);
350 z_streamp ctx = pecalloc(1, sizeof(z_stream), p);
351 TSRMLS_FETCH_FROM_CTX(s->ts);
352
353 PHP_HTTP_DEFLATE_LEVEL_SET(s->flags, level);
354 PHP_HTTP_DEFLATE_WBITS_SET(s->flags, wbits);
355 PHP_HTTP_DEFLATE_STRATEGY_SET(s->flags, strategy);
356
357 if (Z_OK == (status = deflateInit2(ctx, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, strategy))) {
358 if ((ctx->opaque = php_http_buffer_init_ex(NULL, PHP_HTTP_DEFLATE_BUFFER_SIZE, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0))) {
359 s->ctx = ctx;
360 return s;
361 }
362 deflateEnd(ctx);
363 status = Z_MEM_ERROR;
364 }
365 pefree(ctx, p);
366 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to initialize deflate encoding stream: %s", zError(status));
367 return NULL;
368 }
369
370 static php_http_encoding_stream_t *inflate_init(php_http_encoding_stream_t *s)
371 {
372 int status, wbits, p = (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT);
373 z_streamp ctx = pecalloc(1, sizeof(z_stream), p);
374 TSRMLS_FETCH_FROM_CTX(s->ts);
375
376 PHP_HTTP_INFLATE_WBITS_SET(s->flags, wbits);
377
378 if (Z_OK == (status = inflateInit2(ctx, wbits))) {
379 if ((ctx->opaque = php_http_buffer_init_ex(NULL, PHP_HTTP_DEFLATE_BUFFER_SIZE, p ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0))) {
380 s->ctx = ctx;
381 return s;
382 }
383 inflateEnd(ctx);
384 status = Z_MEM_ERROR;
385 }
386 pefree(ctx, p);
387 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to initialize inflate stream: %s", zError(status));
388 return NULL;
389 }
390
391 static php_http_encoding_stream_t *dechunk_init(php_http_encoding_stream_t *s)
392 {
393 struct dechunk_ctx *ctx = pecalloc(1, sizeof(*ctx), (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
394 TSRMLS_FETCH_FROM_CTX(s->ts);
395
396 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)) {
397 return NULL;
398 }
399
400 ctx->hexlen = 0;
401 ctx->zeroed = 0;
402 s->ctx = ctx;
403
404 return s;
405 }
406
407 static php_http_encoding_stream_t *deflate_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
408 {
409 z_streamp from_ctx = from->ctx, to_ctx = to->ctx;
410
411 deflateCopy(to_ctx, from_ctx);
412 php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
413
414 return to;
415 }
416
417 static php_http_encoding_stream_t *inflate_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
418 {
419 z_streamp from_ctx = from->ctx, to_ctx = to->ctx;
420
421 inflateCopy(to_ctx, from_ctx);
422 php_http_buffer_append(to_ctx->opaque, PHP_HTTP_BUFFER_VAL(from_ctx->opaque), PHP_HTTP_BUFFER_LEN(from_ctx->opaque));
423
424 return to;
425 }
426
427 static php_http_encoding_stream_t *dechunk_copy(php_http_encoding_stream_t *from, php_http_encoding_stream_t *to)
428 {
429 struct dechunk_ctx *from_ctx = from->ctx, *to_ctx = to->ctx;
430
431 to_ctx->hexlen = from_ctx->hexlen;
432 to_ctx->zeroed = from_ctx->zeroed;
433 php_http_buffer_append(&to_ctx->buffer, PHP_HTTP_BUFFER_VAL(&from_ctx->buffer), PHP_HTTP_BUFFER_LEN(&from_ctx->buffer));
434
435 return to;
436 }
437
438 static STATUS deflate_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **encoded, size_t *encoded_len)
439 {
440 int status;
441 z_streamp ctx = s->ctx;
442 TSRMLS_FETCH_FROM_CTX(s->ts);
443
444 /* append input to our buffer */
445 php_http_buffer_append(PHP_HTTP_BUFFER(ctx->opaque), data, data_len);
446
447 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
448 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
449
450 /* deflate */
451 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len);
452 *encoded = emalloc(*encoded_len);
453 ctx->avail_out = *encoded_len;
454 ctx->next_out = (Bytef *) *encoded;
455
456 switch (status = deflate(ctx, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags))) {
457 case Z_OK:
458 case Z_STREAM_END:
459 /* cut processed chunk off the buffer */
460 if (ctx->avail_in) {
461 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
462 } else {
463 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx->opaque));
464 }
465
466 /* size buffer down to actual size */
467 *encoded_len -= ctx->avail_out;
468 *encoded = erealloc(*encoded, *encoded_len + 1);
469 (*encoded)[*encoded_len] = '\0';
470 return SUCCESS;
471 }
472
473 STR_SET(*encoded, NULL);
474 *encoded_len = 0;
475 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to update deflate stream: %s", zError(status));
476 return FAILURE;
477 }
478
479 static STATUS inflate_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len)
480 {
481 int status;
482 z_streamp ctx = s->ctx;
483 TSRMLS_FETCH_FROM_CTX(s->ts);
484
485 /* append input to buffer */
486 php_http_buffer_append(PHP_HTTP_BUFFER(ctx->opaque), data, data_len);
487
488 retry_raw_inflate:
489 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
490 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
491
492 switch (status = php_http_inflate_rounds(ctx, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s->flags), decoded, decoded_len)) {
493 case Z_OK:
494 case Z_STREAM_END:
495 /* cut off */
496 if (ctx->avail_in) {
497 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
498 } else {
499 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx->opaque));
500 }
501 return SUCCESS;
502
503 case Z_DATA_ERROR:
504 /* raw deflated data ? */
505 if (!(s->flags & PHP_HTTP_INFLATE_TYPE_RAW) && !ctx->total_out) {
506 inflateEnd(ctx);
507 s->flags |= PHP_HTTP_INFLATE_TYPE_RAW;
508 inflateInit2(ctx, PHP_HTTP_WINDOW_BITS_RAW);
509 goto retry_raw_inflate;
510 }
511 }
512
513 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to update inflate stream: %s", zError(status));
514 return FAILURE;
515 }
516
517 static STATUS dechunk_update(php_http_encoding_stream_t *s, const char *data, size_t data_len, char **decoded, size_t *decoded_len)
518 {
519 php_http_buffer tmp;
520 struct dechunk_ctx *ctx = s->ctx;
521 TSRMLS_FETCH_FROM_CTX(s->ts);
522
523 if (ctx->zeroed) {
524 return FAILURE;
525 }
526 if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(&ctx->buffer, data, data_len)) {
527 return FAILURE;
528 }
529 if (!php_http_buffer_fix(&ctx->buffer)) {
530 return FAILURE;
531 }
532
533 *decoded = NULL;
534 *decoded_len = 0;
535
536 php_http_buffer_init(&tmp);
537
538 /* we have data in our buffer */
539 while (PHP_HTTP_BUFFER_LEN(&ctx->buffer)) {
540
541 /* we already know the size of the chunk and are waiting for data */
542 if (ctx->hexlen) {
543
544 /* not enough data buffered */
545 if (PHP_HTTP_BUFFER_LEN(&ctx->buffer) < ctx->hexlen) {
546
547 /* flush anyway? */
548 if (s->flags & PHP_HTTP_ENCODING_STREAM_FLUSH_FULL) {
549 /* flush all data (should only be chunk data) */
550 php_http_buffer_append(&tmp, PHP_HTTP_BUFFER_VAL(&ctx->buffer), PHP_HTTP_BUFFER_LEN(&ctx->buffer));
551 /* waiting for less data now */
552 ctx->hexlen -= PHP_HTTP_BUFFER_LEN(&ctx->buffer);
553 /* no more buffered data */
554 php_http_buffer_reset(&ctx->buffer);
555 /* break */
556 }
557
558 /* we have too less data and don't need to flush */
559 else {
560 break;
561 }
562 }
563
564 /* we seem to have all data of the chunk */
565 else {
566 php_http_buffer_append(&tmp, PHP_HTTP_BUFFER_VAL(&ctx->buffer), ctx->hexlen);
567 /* remove outgoing data from the buffer */
568 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, ctx->hexlen);
569 /* reset hexlen */
570 ctx->hexlen = 0;
571 /* continue */
572 }
573 }
574
575 /* we don't know the length of the chunk yet */
576 else {
577 size_t off = 0;
578
579 /* ignore preceeding CRLFs (too loose?) */
580 while (off < PHP_HTTP_BUFFER_LEN(&ctx->buffer) && (
581 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\n' ||
582 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\r')) {
583 ++off;
584 }
585 if (off) {
586 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, off);
587 }
588
589 /* still data there? */
590 if (PHP_HTTP_BUFFER_LEN(&ctx->buffer)) {
591 int eollen;
592 const char *eolstr;
593
594 /* we need eol, so we can be sure we have all hex digits */
595 php_http_buffer_fix(&ctx->buffer);
596 if ((eolstr = php_http_locate_bin_eol(PHP_HTTP_BUFFER_VAL(&ctx->buffer), PHP_HTTP_BUFFER_LEN(&ctx->buffer), &eollen))) {
597 char *stop = NULL;
598
599 /* read in chunk size */
600 ctx->hexlen = strtoul(PHP_HTTP_BUFFER_VAL(&ctx->buffer), &stop, 16);
601
602 /* if strtoul() stops at the beginning of the buffered data
603 there's domething oddly wrong, i.e. bad input */
604 if (stop == PHP_HTTP_BUFFER_VAL(&ctx->buffer)) {
605 php_http_buffer_dtor(&tmp);
606 return FAILURE;
607 }
608
609 /* cut out <chunk size hex><chunk extension><eol> */
610 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, eolstr + eollen - PHP_HTTP_BUFFER_VAL(&ctx->buffer));
611 /* buffer->hexlen is 0 now or contains the size of the next chunk */
612 if (!ctx->hexlen) {
613 size_t off = 0;
614
615 /* ignore following CRLFs (too loose?) */
616 while (off < PHP_HTTP_BUFFER_LEN(&ctx->buffer) && (
617 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\n' ||
618 PHP_HTTP_BUFFER_VAL(&ctx->buffer)[off] == '\r')) {
619 ++off;
620 }
621 if (off) {
622 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx->buffer), 0, off);
623 }
624
625 ctx->zeroed = 1;
626 break;
627 }
628 /* continue */
629 } else {
630 /* we have not enough data buffered to read in chunk size */
631 break;
632 }
633 }
634 /* break */
635 }
636 }
637
638 php_http_buffer_fix(&tmp);
639 *decoded = PHP_HTTP_BUFFER_VAL(&tmp);
640 *decoded_len = PHP_HTTP_BUFFER_LEN(&tmp);
641
642 return SUCCESS;
643 }
644
645 static STATUS deflate_flush(php_http_encoding_stream_t *s, char **encoded, size_t *encoded_len)
646 {
647 int status;
648 z_streamp ctx = s->ctx;
649 TSRMLS_FETCH_FROM_CTX(s->ts);
650
651 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE;
652 *encoded = emalloc(*encoded_len);
653
654 ctx->avail_in = 0;
655 ctx->next_in = NULL;
656 ctx->avail_out = *encoded_len;
657 ctx->next_out = (Bytef *) *encoded;
658
659 switch (status = deflate(ctx, Z_FULL_FLUSH)) {
660 case Z_OK:
661 case Z_STREAM_END:
662 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE - ctx->avail_out;
663 *encoded = erealloc(*encoded, *encoded_len + 1);
664 (*encoded)[*encoded_len] = '\0';
665 return SUCCESS;
666 }
667
668 STR_SET(*encoded, NULL);
669 *encoded_len = 0;
670 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to flush deflate stream: %s", zError(status));
671 return FAILURE;
672 }
673
674 static STATUS dechunk_flush(php_http_encoding_stream_t *s, char **decoded, size_t *decoded_len)
675 {
676 struct dechunk_ctx *ctx = s->ctx;
677
678 if (ctx->hexlen) {
679 /* flush all data (should only be chunk data) */
680 php_http_buffer_fix(&ctx->buffer);
681 php_http_buffer_data(&ctx->buffer, decoded, decoded_len);
682 /* waiting for less data now */
683 ctx->hexlen -= PHP_HTTP_BUFFER_LEN(&ctx->buffer);
684 /* no more buffered data */
685 php_http_buffer_reset(&ctx->buffer);
686 } else {
687 *decoded = NULL;
688 *decoded_len = 0;
689 }
690
691 return SUCCESS;
692 }
693
694 static STATUS deflate_finish(php_http_encoding_stream_t *s, char **encoded, size_t *encoded_len)
695 {
696 int status;
697 z_streamp ctx = s->ctx;
698 TSRMLS_FETCH_FROM_CTX(s->ts);
699
700 *encoded_len = PHP_HTTP_DEFLATE_BUFFER_SIZE;
701 *encoded = emalloc(*encoded_len);
702
703 /* deflate remaining input */
704 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
705 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
706
707 ctx->avail_out = *encoded_len;
708 ctx->next_out = (Bytef *) *encoded;
709
710 do {
711 status = deflate(ctx, Z_FINISH);
712 } while (Z_OK == status);
713
714 if (Z_STREAM_END == status) {
715 /* cut processed intp off */
716 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
717
718 /* size down */
719 *encoded_len -= ctx->avail_out;
720 *encoded = erealloc(*encoded, *encoded_len + 1);
721 (*encoded)[*encoded_len] = '\0';
722 return SUCCESS;
723 }
724
725 STR_SET(*encoded, NULL);
726 *encoded_len = 0;
727 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to finish deflate stream: %s", zError(status));
728 return FAILURE;
729 }
730
731 static STATUS inflate_finish(php_http_encoding_stream_t *s, char **decoded, size_t *decoded_len)
732 {
733 int status;
734 z_streamp ctx = s->ctx;
735 TSRMLS_FETCH_FROM_CTX(s->ts);
736
737 if (!PHP_HTTP_BUFFER_LEN(ctx->opaque)) {
738 *decoded = NULL;
739 *decoded_len = 0;
740 return SUCCESS;
741 }
742
743 *decoded_len = (PHP_HTTP_BUFFER_LEN(ctx->opaque) + 1) * PHP_HTTP_INFLATE_ROUNDS;
744 *decoded = emalloc(*decoded_len);
745
746 /* inflate remaining input */
747 ctx->next_in = (Bytef *) PHP_HTTP_BUFFER_VAL(ctx->opaque);
748 ctx->avail_in = PHP_HTTP_BUFFER_LEN(ctx->opaque);
749
750 ctx->avail_out = *decoded_len;
751 ctx->next_out = (Bytef *) *decoded;
752
753 if (Z_STREAM_END == (status = inflate(ctx, Z_FINISH))) {
754 /* cut processed input off */
755 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx->opaque), 0, PHP_HTTP_BUFFER_LEN(ctx->opaque) - ctx->avail_in);
756
757 /* size down */
758 *decoded_len -= ctx->avail_out;
759 *decoded = erealloc(*decoded, *decoded_len + 1);
760 (*decoded)[*decoded_len] = '\0';
761 return SUCCESS;
762 }
763
764 STR_SET(*decoded, NULL);
765 *decoded_len = 0;
766 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Failed to finish inflate stream: %s", zError(status));
767 return FAILURE;
768 }
769
770 static zend_bool deflate_done(php_http_encoding_stream_t *s)
771 {
772 z_streamp ctx = s->ctx;
773 return !ctx->avail_in && !PHP_HTTP_BUFFER_LEN(ctx->opaque);
774 }
775
776 static zend_bool inflate_done(php_http_encoding_stream_t *s)
777 {
778 z_streamp ctx = s->ctx;
779 return !ctx->avail_in && !PHP_HTTP_BUFFER_LEN(ctx->opaque);
780 }
781
782 static zend_bool dechunk_done(php_http_encoding_stream_t *s)
783 {
784 return ((struct dechunk_ctx *) s->ctx)->zeroed;
785 }
786
787 static void deflate_dtor(php_http_encoding_stream_t *s)
788 {
789 if (s->ctx) {
790 z_streamp ctx = s->ctx;
791
792 if (ctx->opaque) {
793 php_http_buffer_free((php_http_buffer **) &ctx->opaque);
794 }
795 deflateEnd(ctx);
796 pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
797 s->ctx = NULL;
798 }
799 }
800
801 static void inflate_dtor(php_http_encoding_stream_t *s)
802 {
803 if (s->ctx) {
804 z_streamp ctx = s->ctx;
805
806 if (ctx->opaque) {
807 php_http_buffer_free((php_http_buffer **) &ctx->opaque);
808 }
809 inflateEnd(ctx);
810 pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
811 s->ctx = NULL;
812 }
813 }
814
815 static void dechunk_dtor(php_http_encoding_stream_t *s)
816 {
817 if (s->ctx) {
818 struct dechunk_ctx *ctx = s->ctx;
819
820 php_http_buffer_dtor(&ctx->buffer);
821 pefree(ctx, (s->flags & PHP_HTTP_ENCODING_STREAM_PERSISTENT));
822 s->ctx = NULL;
823 }
824 }
825
826 static php_http_encoding_stream_ops_t php_http_encoding_deflate_ops = {
827 deflate_init,
828 deflate_copy,
829 deflate_update,
830 deflate_flush,
831 deflate_done,
832 deflate_finish,
833 deflate_dtor
834 };
835
836 PHP_HTTP_API php_http_encoding_stream_ops_t *php_http_encoding_stream_get_deflate_ops(void)
837 {
838 return &php_http_encoding_deflate_ops;
839 }
840
841 static php_http_encoding_stream_ops_t php_http_encoding_inflate_ops = {
842 inflate_init,
843 inflate_copy,
844 inflate_update,
845 NULL,
846 inflate_done,
847 inflate_finish,
848 inflate_dtor
849 };
850
851 PHP_HTTP_API php_http_encoding_stream_ops_t *php_http_encoding_stream_get_inflate_ops(void)
852 {
853 return &php_http_encoding_inflate_ops;
854 }
855
856 static php_http_encoding_stream_ops_t php_http_encoding_dechunk_ops = {
857 dechunk_init,
858 dechunk_copy,
859 dechunk_update,
860 dechunk_flush,
861 dechunk_done,
862 NULL,
863 dechunk_dtor
864 };
865
866 PHP_HTTP_API php_http_encoding_stream_ops_t *php_http_encoding_stream_get_dechunk_ops(void)
867 {
868 return &php_http_encoding_dechunk_ops;
869 }
870
871 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpEncodingStream, method, 0, req_args)
872 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpEncodingStream, method, 0)
873 #define PHP_HTTP_ENCSTREAM_ME(method, visibility) PHP_ME(HttpEncodingStream, method, PHP_HTTP_ARGS(HttpEncodingStream, method), visibility)
874
875 PHP_HTTP_BEGIN_ARGS(__construct, 0)
876 PHP_HTTP_ARG_VAL(flags, 0)
877 PHP_HTTP_END_ARGS;
878
879 PHP_HTTP_BEGIN_ARGS(update, 1)
880 PHP_HTTP_ARG_VAL(data, 0)
881 PHP_HTTP_END_ARGS;
882
883 PHP_HTTP_EMPTY_ARGS(flush);
884 PHP_HTTP_EMPTY_ARGS(done);
885 PHP_HTTP_EMPTY_ARGS(finish);
886
887 zend_class_entry *php_http_encoding_stream_class_entry;
888 zend_function_entry php_http_encoding_stream_method_entry[] = {
889 PHP_HTTP_ENCSTREAM_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
890 PHP_HTTP_ENCSTREAM_ME(update, ZEND_ACC_PUBLIC)
891 PHP_HTTP_ENCSTREAM_ME(flush, ZEND_ACC_PUBLIC)
892 PHP_HTTP_ENCSTREAM_ME(done, ZEND_ACC_PUBLIC)
893 PHP_HTTP_ENCSTREAM_ME(finish, ZEND_ACC_PUBLIC)
894
895 EMPTY_FUNCTION_ENTRY
896 };
897 static zend_object_handlers php_http_encoding_stream_object_handlers;
898
899 zend_object_value php_http_encoding_stream_object_new(zend_class_entry *ce TSRMLS_DC)
900 {
901 return php_http_encoding_stream_object_new_ex(ce, NULL, NULL TSRMLS_CC);
902 }
903
904 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)
905 {
906 zend_object_value ov;
907 php_http_encoding_stream_object_t *o;
908
909 o = ecalloc(1, sizeof(*o));
910 zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
911 object_properties_init((zend_object *) o, ce);
912
913 if (ptr) {
914 *ptr = o;
915 }
916
917 if (s) {
918 o->stream = s;
919 }
920
921 ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_encoding_stream_object_free, NULL TSRMLS_CC);
922 ov.handlers = &php_http_encoding_stream_object_handlers;
923
924 return ov;
925 }
926
927 zend_object_value php_http_encoding_stream_object_clone(zval *this_ptr TSRMLS_DC)
928 {
929 zend_object_value new_ov;
930 php_http_encoding_stream_object_t *new_obj = NULL, *old_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
931
932 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);
933 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
934
935 return new_ov;
936 }
937
938 void php_http_encoding_stream_object_free(void *object TSRMLS_DC)
939 {
940 php_http_encoding_stream_object_t *o = (php_http_encoding_stream_object_t *) object;
941
942 if (o->stream) {
943 php_http_encoding_stream_free(&o->stream TSRMLS_CC);
944 }
945 zend_object_std_dtor((zend_object *) o TSRMLS_CC);
946 efree(o);
947 }
948
949 PHP_METHOD(HttpEncodingStream, __construct)
950 {
951 with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) {
952 long flags = 0;
953
954 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
955 with_error_handling(EH_THROW, PHP_HTTP_EX_CE(encoding)) {
956 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
957
958 if (!obj->stream) {
959 php_http_encoding_stream_ops_t *ops = NULL;
960
961 if (instanceof_function(obj->zo.ce, php_http_deflate_stream_class_entry TSRMLS_CC)) {
962 ops = &php_http_encoding_deflate_ops;
963 } else if (instanceof_function(obj->zo.ce, php_http_inflate_stream_class_entry TSRMLS_CC)) {
964 ops = &php_http_encoding_inflate_ops;
965 } else if (instanceof_function(obj->zo.ce, php_http_dechunk_stream_class_entry TSRMLS_CC)) {
966 ops = &php_http_encoding_dechunk_ops;
967 }
968
969 if (ops) {
970 obj->stream = php_http_encoding_stream_init(obj->stream, ops, flags TSRMLS_CC);
971 } else {
972 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Unknown HttpEncodingStream class %s", obj->zo.ce->name);
973 }
974
975 } else {
976 php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "HttpEncodingStream cannot be initialized twice");
977 }
978 } end_error_handling();
979 }
980 } end_error_handling();
981 }
982
983 PHP_METHOD(HttpEncodingStream, update)
984 {
985 int data_len;
986 char *data_str;
987
988 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data_str, &data_len)) {
989 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
990
991 if (obj->stream) {
992 size_t encoded_len;
993 char *encoded_str;
994
995 if (SUCCESS == php_http_encoding_stream_update(obj->stream, data_str, data_len, &encoded_str, &encoded_len TSRMLS_CC)) {
996 RETURN_STRINGL(encoded_str, encoded_len, 0);
997 }
998 }
999 }
1000 RETURN_FALSE;
1001 }
1002
1003 PHP_METHOD(HttpEncodingStream, flush)
1004 {
1005 if (SUCCESS == zend_parse_parameters_none()) {
1006 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1007
1008 if (obj->stream) {
1009 char *encoded_str;
1010 size_t encoded_len;
1011
1012 if (SUCCESS == php_http_encoding_stream_flush(obj->stream, &encoded_str, &encoded_len TSRMLS_CC)) {
1013 RETURN_STRINGL(encoded_str, encoded_len, 0);
1014 }
1015 }
1016 }
1017 RETURN_FALSE;
1018 }
1019
1020 PHP_METHOD(HttpEncodingStream, done)
1021 {
1022 if (SUCCESS == zend_parse_parameters_none()) {
1023 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1024
1025 if (obj->stream) {
1026 RETURN_BOOL(php_http_encoding_stream_done(obj->stream));
1027 }
1028 }
1029 RETURN_FALSE;
1030 }
1031
1032 PHP_METHOD(HttpEncodingStream, finish)
1033 {
1034 if (SUCCESS == zend_parse_parameters_none()) {
1035 php_http_encoding_stream_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
1036
1037 if (obj->stream) {
1038 char *encoded_str;
1039 size_t encoded_len;
1040
1041 if (SUCCESS == php_http_encoding_stream_finish(obj->stream, &encoded_str, &encoded_len TSRMLS_CC)) {
1042 if (SUCCESS == php_http_encoding_stream_reset(&obj->stream)) {
1043 RETURN_STRINGL(encoded_str, encoded_len, 0);
1044 } else {
1045 STR_FREE(encoded_str);
1046 }
1047 }
1048 }
1049 }
1050 RETURN_FALSE;
1051 }
1052
1053 #undef PHP_HTTP_BEGIN_ARGS
1054 #undef PHP_HTTP_EMPTY_ARGS
1055 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpDeflateStream, method, 0, req_args)
1056 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpDeflateStream, method, 0)
1057 #define PHP_HTTP_DEFLATE_ME(method, visibility) PHP_ME(HttpDeflateStream, method, PHP_HTTP_ARGS(HttpDeflateStream, method), visibility)
1058
1059 PHP_HTTP_BEGIN_ARGS(encode, 1)
1060 PHP_HTTP_ARG_VAL(data, 0)
1061 PHP_HTTP_ARG_VAL(flags, 0)
1062 PHP_HTTP_END_ARGS;
1063
1064 zend_class_entry *php_http_deflate_stream_class_entry;
1065 zend_function_entry php_http_deflate_stream_method_entry[] = {
1066 PHP_HTTP_DEFLATE_ME(encode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1067
1068 EMPTY_FUNCTION_ENTRY
1069 };
1070
1071 PHP_METHOD(HttpDeflateStream, encode)
1072 {
1073 char *str;
1074 int len;
1075 long flags = 0;
1076
1077 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &str, &len, &flags)) {
1078 char *enc_str;
1079 size_t enc_len;
1080
1081 if (SUCCESS == php_http_encoding_deflate(flags, str, len, &enc_str, &enc_len TSRMLS_CC)) {
1082 RETURN_STRINGL(enc_str, enc_len, 0);
1083 }
1084 }
1085 RETURN_FALSE;
1086 }
1087
1088 #undef PHP_HTTP_BEGIN_ARGS
1089 #undef PHP_HTTP_EMPTY_ARGS
1090 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpInflateStream, method, 0, req_args)
1091 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpInflateStream, method, 0)
1092 #define PHP_HTTP_INFLATE_ME(method, visibility) PHP_ME(HttpInflateStream, method, PHP_HTTP_ARGS(HttpInflateStream, method), visibility)
1093
1094 PHP_HTTP_BEGIN_ARGS(decode, 1)
1095 PHP_HTTP_ARG_VAL(data, 0)
1096 PHP_HTTP_END_ARGS;
1097
1098 zend_class_entry *php_http_inflate_stream_class_entry;
1099 zend_function_entry php_http_inflate_stream_method_entry[] = {
1100 PHP_HTTP_INFLATE_ME(decode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1101
1102 EMPTY_FUNCTION_ENTRY
1103 };
1104
1105 PHP_METHOD(HttpInflateStream, decode)
1106 {
1107 char *str;
1108 int len;
1109
1110 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
1111 char *enc_str;
1112 size_t enc_len;
1113
1114 if (SUCCESS == php_http_encoding_inflate(str, len, &enc_str, &enc_len TSRMLS_CC)) {
1115 RETURN_STRINGL(enc_str, enc_len, 0);
1116 }
1117 }
1118 RETURN_FALSE;
1119 }
1120
1121 #undef PHP_HTTP_BEGIN_ARGS
1122 #undef PHP_HTTP_EMPTY_ARGS
1123 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpDechunkStream, method, 0, req_args)
1124 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpDechunkStream, method, 0)
1125 #define PHP_HTTP_DECHUNK_ME(method, visibility) PHP_ME(HttpDechunkStream, method, PHP_HTTP_ARGS(HttpDechunkStream, method), visibility)
1126
1127 PHP_HTTP_BEGIN_ARGS(decode, 1)
1128 PHP_HTTP_ARG_VAL(data, 0)
1129 PHP_HTTP_ARG_VAL(decoded_len, 1)
1130 PHP_HTTP_END_ARGS;
1131
1132 zend_class_entry *php_http_dechunk_stream_class_entry;
1133 zend_function_entry php_http_dechunk_stream_method_entry[] = {
1134 PHP_HTTP_DECHUNK_ME(decode, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
1135
1136 EMPTY_FUNCTION_ENTRY
1137 };
1138
1139 PHP_METHOD(HttpDechunkStream, decode)
1140 {
1141 char *str;
1142 int len;
1143 zval *zlen;
1144
1145 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!", &str, &len, &zlen)) {
1146 const char *end_ptr;
1147 char *enc_str;
1148 size_t enc_len;
1149
1150 if ((end_ptr = php_http_encoding_dechunk(str, len, &enc_str, &enc_len TSRMLS_CC))) {
1151 if (zlen) {
1152 zval_dtor(zlen);
1153 ZVAL_LONG(zlen, str + len - end_ptr);
1154 }
1155 RETURN_STRINGL(enc_str, enc_len, 0);
1156 }
1157 }
1158 RETURN_FALSE;
1159 }
1160
1161 PHP_MINIT_FUNCTION(http_encoding)
1162 {
1163 PHP_HTTP_REGISTER_CLASS(http\\encoding, Stream, http_encoding_stream, php_http_object_class_entry, ZEND_ACC_EXPLICIT_ABSTRACT_CLASS);
1164 php_http_encoding_stream_class_entry->create_object = php_http_encoding_stream_object_new;
1165 memcpy(&php_http_encoding_stream_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
1166 php_http_encoding_stream_object_handlers.clone_obj = php_http_encoding_stream_object_clone;
1167
1168 zend_declare_class_constant_long(php_http_encoding_stream_class_entry, ZEND_STRL("FLUSH_NONE"), PHP_HTTP_ENCODING_STREAM_FLUSH_NONE TSRMLS_CC);
1169 zend_declare_class_constant_long(php_http_encoding_stream_class_entry, ZEND_STRL("FLUSH_SYNC"), PHP_HTTP_ENCODING_STREAM_FLUSH_SYNC TSRMLS_CC);
1170 zend_declare_class_constant_long(php_http_encoding_stream_class_entry, ZEND_STRL("FLUSH_FULL"), PHP_HTTP_ENCODING_STREAM_FLUSH_FULL TSRMLS_CC);
1171
1172 PHP_HTTP_REGISTER_CLASS(http\\encoding\\stream, Deflate, http_deflate_stream, php_http_encoding_stream_class_entry, 0);
1173
1174 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("TYPE_GZIP"), PHP_HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC);
1175 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("TYPE_ZLIB"), PHP_HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC);
1176 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("TYPE_RAW"), PHP_HTTP_DEFLATE_TYPE_RAW TSRMLS_CC);
1177 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("LEVEL_DEF"), PHP_HTTP_DEFLATE_LEVEL_DEF TSRMLS_CC);
1178 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("LEVEL_MIN"), PHP_HTTP_DEFLATE_LEVEL_MIN TSRMLS_CC);
1179 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("LEVEL_MAX"), PHP_HTTP_DEFLATE_LEVEL_MAX TSRMLS_CC);
1180 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_DEF"), PHP_HTTP_DEFLATE_STRATEGY_DEF TSRMLS_CC);
1181 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_FILT"), PHP_HTTP_DEFLATE_STRATEGY_FILT TSRMLS_CC);
1182 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_HUFF"), PHP_HTTP_DEFLATE_STRATEGY_HUFF TSRMLS_CC);
1183 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_RLE"), PHP_HTTP_DEFLATE_STRATEGY_RLE TSRMLS_CC);
1184 zend_declare_class_constant_long(php_http_deflate_stream_class_entry, ZEND_STRL("STRATEGY_FIXED"), PHP_HTTP_DEFLATE_STRATEGY_FIXED TSRMLS_CC);
1185
1186 PHP_HTTP_REGISTER_CLASS(http\\encoding\\stream, Inflate, http_inflate_stream, php_http_encoding_stream_class_entry, 0);
1187 PHP_HTTP_REGISTER_CLASS(http\\encoding\\stream, Dechunk, http_dechunk_stream, php_http_encoding_stream_class_entry, 0);
1188
1189 return SUCCESS;
1190 }
1191
1192
1193 /*
1194 * Local variables:
1195 * tab-width: 4
1196 * c-basic-offset: 4
1197 * End:
1198 * vim600: noet sw=4 ts=4 fdm=marker
1199 * vim<600: noet sw=4 ts=4
1200 */
1201