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