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