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