2 +--------------------------------------------------------------------+
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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
17 static inline int eol_match(char **line
, int *eol_len
)
21 while (' ' == *ptr
) ++ptr
;
23 if (ptr
== php_http_locate_eol(*line
, eol_len
)) {
31 const char *php_http_encoding_dechunk(const char *encoded
, size_t encoded_len
, char **decoded
, size_t *decoded_len
)
35 const char *e_ptr
= encoded
;
38 *decoded
= ecalloc(1, encoded_len
+ 1);
40 while ((encoded
+ encoded_len
- e_ptr
) > 0) {
41 ulong chunk_len
= 0, rest
;
43 chunk_len
= strtoul(e_ptr
, &n_ptr
, 16);
45 /* we could not read in chunk size */
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
52 if (e_ptr
== encoded
) {
53 php_error_docref(NULL
, E_NOTICE
, "Data does not seem to be chunked encoded");
54 memcpy(*decoded
, encoded
, encoded_len
);
55 *decoded_len
= encoded_len
;
56 return encoded
+ encoded_len
;
59 php_error_docref(NULL
, E_WARNING
, "Expected chunk size at pos %tu of %zu but got trash", n_ptr
- encoded
, encoded_len
);
66 /* move over '0' chunked encoding terminator and any new lines */
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
)) {
83 php_error_docref(NULL
, E_WARNING
, "Expected CRLF at pos %tu of %zu but got 0x%02X 0x%02X", n_ptr
- encoded
, encoded_len
, *n_ptr
, *(n_ptr
+ 1));
85 php_error_docref(NULL
, E_WARNING
, "Expected LF at pos %tu of %zu but got 0x%02X", n_ptr
- encoded
, encoded_len
, *n_ptr
);
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_error_docref(NULL
, E_WARNING
, "Truncated message: chunk size %lu exceeds remaining data size %lu at pos %tu of %zu", chunk_len
, rest
, n_ptr
- encoded
, encoded_len
);
97 memcpy(*decoded
+ *decoded_len
, n_ptr
, chunk_len
);
98 *decoded_len
+= chunk_len
;
100 if (chunk_len
== rest
) {
101 e_ptr
= n_ptr
+ chunk_len
;
104 /* advance to next chunk */
105 e_ptr
= n_ptr
+ chunk_len
+ eol_len
;
112 static inline int php_http_inflate_rounds(z_stream
*Z
, int flush
, char **buf
, size_t *len
)
114 int status
= 0, round
= 0;
115 php_http_buffer_t buffer
;
120 php_http_buffer_init_ex(&buffer
, Z
->avail_in
, PHP_HTTP_BUFFER_INIT_PREALLOC
);
123 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize_ex(&buffer
, buffer
.size
, 0, 1)) {
124 status
= Z_MEM_ERROR
;
126 Z
->avail_out
= buffer
.free
;
127 Z
->next_out
= (Bytef
*) buffer
.data
+ buffer
.used
;
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
);
131 status
= inflate(Z
, flush
);
132 php_http_buffer_account(&buffer
, buffer
.free
- Z
->avail_out
);
134 fprintf(stderr
, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round
, status
, buffer
.size
, buffer
.free
, buffer
.used
, Z
->avail_in
, Z
->avail_out
);
136 PHP_HTTP_INFLATE_BUFFER_SIZE_ALIGN(buffer
.size
);
138 } while ((Z_BUF_ERROR
== status
|| (Z_OK
== status
&& Z
->avail_in
)) && ++round
< PHP_HTTP_INFLATE_ROUNDS
);
140 if (status
== Z_OK
|| status
== Z_STREAM_END
) {
141 php_http_buffer_shrink(&buffer
);
142 php_http_buffer_fix(&buffer
);
146 php_http_buffer_dtor(&buffer
);
152 ZEND_RESULT_CODE
php_http_encoding_deflate(int flags
, const char *data
, size_t data_len
, char **encoded
, size_t *encoded_len
)
154 int status
, level
, wbits
, strategy
;
157 PHP_HTTP_DEFLATE_LEVEL_SET(flags
, level
);
158 PHP_HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
159 PHP_HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
161 memset(&Z
, 0, sizeof(z_stream
));
165 status
= deflateInit2(&Z
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
);
166 if (Z_OK
== status
) {
167 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
168 *encoded
= emalloc(*encoded_len
);
170 Z
.next_in
= (Bytef
*) data
;
171 Z
.next_out
= (Bytef
*) *encoded
;
172 Z
.avail_in
= data_len
;
173 Z
.avail_out
= *encoded_len
;
175 status
= deflate(&Z
, Z_FINISH
);
178 if (Z_STREAM_END
== status
) {
179 /* size buffer down to actual length */
180 *encoded
= erealloc(*encoded
, Z
.total_out
+ 1);
181 (*encoded
)[*encoded_len
= Z
.total_out
] = '\0';
184 PTR_SET(*encoded
, NULL
);
189 php_error_docref(NULL
, E_WARNING
, "Could not deflate data: %s", zError(status
));
193 ZEND_RESULT_CODE
php_http_encoding_inflate(const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len
)
196 int status
, wbits
= PHP_HTTP_WINDOW_BITS_ANY
;
198 memset(&Z
, 0, sizeof(z_stream
));
201 status
= inflateInit2(&Z
, wbits
);
202 if (Z_OK
== status
) {
203 Z
.next_in
= (Bytef
*) data
;
204 Z
.avail_in
= data_len
+ 1; /* include the terminating NULL, see #61287 */
206 switch (status
= php_http_inflate_rounds(&Z
, Z_NO_FLUSH
, decoded
, decoded_len
)) {
212 status
= Z_DATA_ERROR
;
216 /* raw deflated data? */
217 if (PHP_HTTP_WINDOW_BITS_ANY
== wbits
) {
219 wbits
= PHP_HTTP_WINDOW_BITS_RAW
;
220 goto retry_raw_inflate
;
226 if (*decoded_len
&& *decoded
) {
231 php_error_docref(NULL
, E_WARNING
, "Could not inflate data: %s", zError(status
));
235 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
)
240 s
= pemalloc(sizeof(*s
), (flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
242 memset(s
, 0, sizeof(*s
));
246 if ((s
->ops
= ops
)) {
247 php_http_encoding_stream_t
*ss
= s
->ops
->init(s
);
257 pefree(s
, (flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
262 php_http_encoding_stream_t
*php_http_encoding_stream_copy(php_http_encoding_stream_t
*from
, php_http_encoding_stream_t
*to
)
264 if (from
->ops
->copy
) {
265 php_http_encoding_stream_t
*ns
;
268 to
= pemalloc(sizeof(*to
), (from
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
270 memset(to
, 0, sizeof(*to
));
272 to
->flags
= from
->flags
;
275 if ((ns
= to
->ops
->copy(from
, to
))) {
285 ZEND_RESULT_CODE
php_http_encoding_stream_reset(php_http_encoding_stream_t
**s
)
287 php_http_encoding_stream_t
*ss
;
289 if ((*s
)->ops
->dtor
) {
292 if ((ss
= (*s
)->ops
->init(*s
))) {
299 ZEND_RESULT_CODE
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
)
301 if (!s
->ops
->update
) {
304 return s
->ops
->update(s
, in_str
, in_len
, out_str
, out_len
);
307 ZEND_RESULT_CODE
php_http_encoding_stream_flush(php_http_encoding_stream_t
*s
, char **out_str
, size_t *out_len
)
309 if (!s
->ops
->flush
) {
314 return s
->ops
->flush(s
, out_str
, out_len
);
317 zend_bool
php_http_encoding_stream_done(php_http_encoding_stream_t
*s
)
322 return s
->ops
->done(s
);
325 ZEND_RESULT_CODE
php_http_encoding_stream_finish(php_http_encoding_stream_t
*s
, char **out_str
, size_t *out_len
)
327 if (!s
->ops
->finish
) {
332 return s
->ops
->finish(s
, out_str
, out_len
);
335 void php_http_encoding_stream_dtor(php_http_encoding_stream_t
*s
)
342 void php_http_encoding_stream_free(php_http_encoding_stream_t
**s
)
345 if ((*s
)->ops
->dtor
) {
348 pefree(*s
, ((*s
)->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
354 php_http_buffer_t buffer
;
359 static php_http_encoding_stream_t
*deflate_init(php_http_encoding_stream_t
*s
)
361 int status
, level
, wbits
, strategy
, p
= (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
);
362 z_streamp ctx
= pecalloc(1, sizeof(z_stream
), p
);
364 PHP_HTTP_DEFLATE_LEVEL_SET(s
->flags
, level
);
365 PHP_HTTP_DEFLATE_WBITS_SET(s
->flags
, wbits
);
366 PHP_HTTP_DEFLATE_STRATEGY_SET(s
->flags
, strategy
);
368 if (Z_OK
== (status
= deflateInit2(ctx
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
))) {
369 if ((ctx
->opaque
= php_http_buffer_init_ex(NULL
, PHP_HTTP_DEFLATE_BUFFER_SIZE
, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0))) {
374 status
= Z_MEM_ERROR
;
377 php_error_docref(NULL
, E_WARNING
, "Failed to initialize deflate encoding stream: %s", zError(status
));
381 static php_http_encoding_stream_t
*inflate_init(php_http_encoding_stream_t
*s
)
383 int status
, wbits
, p
= (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
);
384 z_streamp ctx
= pecalloc(1, sizeof(z_stream
), p
);
386 PHP_HTTP_INFLATE_WBITS_SET(s
->flags
, wbits
);
388 if (Z_OK
== (status
= inflateInit2(ctx
, wbits
))) {
389 if ((ctx
->opaque
= php_http_buffer_init_ex(NULL
, PHP_HTTP_DEFLATE_BUFFER_SIZE
, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0))) {
394 status
= Z_MEM_ERROR
;
397 php_error_docref(NULL
, E_WARNING
, "Failed to initialize inflate stream: %s", zError(status
));
401 static php_http_encoding_stream_t
*dechunk_init(php_http_encoding_stream_t
*s
)
403 struct dechunk_ctx
*ctx
= pecalloc(1, sizeof(*ctx
), (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
405 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)) {
416 static php_http_encoding_stream_t
*deflate_copy(php_http_encoding_stream_t
*from
, php_http_encoding_stream_t
*to
)
418 int status
, p
= to
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
;
419 z_streamp from_ctx
= from
->ctx
, to_ctx
= pecalloc(1, sizeof(*to_ctx
), p
);
421 if (Z_OK
== (status
= deflateCopy(to_ctx
, from_ctx
))) {
422 if ((to_ctx
->opaque
= php_http_buffer_init_ex(NULL
, PHP_HTTP_DEFLATE_BUFFER_SIZE
, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0))) {
423 php_http_buffer_append(to_ctx
->opaque
, PHP_HTTP_BUFFER(from_ctx
->opaque
)->data
, PHP_HTTP_BUFFER(from_ctx
->opaque
)->used
);
428 status
= Z_MEM_ERROR
;
430 php_error_docref(NULL
, E_WARNING
, "Failed to copy deflate encoding stream: %s", zError(status
));
434 static php_http_encoding_stream_t
*inflate_copy(php_http_encoding_stream_t
*from
, php_http_encoding_stream_t
*to
)
436 int status
, p
= from
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
;
437 z_streamp from_ctx
= from
->ctx
, to_ctx
= pecalloc(1, sizeof(*to_ctx
), p
);
439 if (Z_OK
== (status
= inflateCopy(to_ctx
, from_ctx
))) {
440 if ((to_ctx
->opaque
= php_http_buffer_init_ex(NULL
, PHP_HTTP_DEFLATE_BUFFER_SIZE
, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0))) {
441 php_http_buffer_append(to_ctx
->opaque
, PHP_HTTP_BUFFER(from_ctx
->opaque
)->data
, PHP_HTTP_BUFFER(from_ctx
->opaque
)->used
);
446 status
= Z_MEM_ERROR
;
448 php_error_docref(NULL
, E_WARNING
, "Failed to copy inflate encoding stream: %s", zError(status
));
452 static php_http_encoding_stream_t
*dechunk_copy(php_http_encoding_stream_t
*from
, php_http_encoding_stream_t
*to
)
454 int p
= from
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
;
455 struct dechunk_ctx
*from_ctx
= from
->ctx
, *to_ctx
= pemalloc(sizeof(*to_ctx
), p
);
457 if (php_http_buffer_init_ex(&to_ctx
->buffer
, PHP_HTTP_BUFFER_DEFAULT_SIZE
, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0)) {
458 to_ctx
->hexlen
= from_ctx
->hexlen
;
459 to_ctx
->zeroed
= from_ctx
->zeroed
;
460 php_http_buffer_append(&to_ctx
->buffer
, from_ctx
->buffer
.data
, from_ctx
->buffer
.used
);
465 php_error_docref(NULL
, E_WARNING
, "Failed to copy inflate encoding stream: out of memory");
469 static ZEND_RESULT_CODE
deflate_update(php_http_encoding_stream_t
*s
, const char *data
, size_t data_len
, char **encoded
, size_t *encoded_len
)
472 z_streamp ctx
= s
->ctx
;
474 /* append input to our buffer */
475 php_http_buffer_append(PHP_HTTP_BUFFER(ctx
->opaque
), data
, data_len
);
477 ctx
->next_in
= (Bytef
*) PHP_HTTP_BUFFER(ctx
->opaque
)->data
;
478 ctx
->avail_in
= PHP_HTTP_BUFFER(ctx
->opaque
)->used
;
481 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
482 *encoded
= emalloc(*encoded_len
);
483 ctx
->avail_out
= *encoded_len
;
484 ctx
->next_out
= (Bytef
*) *encoded
;
486 switch (status
= deflate(ctx
, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s
->flags
))) {
489 /* cut processed chunk off the buffer */
491 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx
->opaque
), 0, PHP_HTTP_BUFFER(ctx
->opaque
)->used
- ctx
->avail_in
);
493 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx
->opaque
));
496 /* size buffer down to actual size */
497 *encoded_len
-= ctx
->avail_out
;
498 *encoded
= erealloc(*encoded
, *encoded_len
+ 1);
499 (*encoded
)[*encoded_len
] = '\0';
503 PTR_SET(*encoded
, NULL
);
505 php_error_docref(NULL
, E_WARNING
, "Failed to update deflate stream: %s", zError(status
));
509 static ZEND_RESULT_CODE
inflate_update(php_http_encoding_stream_t
*s
, const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len
)
512 z_streamp ctx
= s
->ctx
;
514 /* append input to buffer */
515 php_http_buffer_append(PHP_HTTP_BUFFER(ctx
->opaque
), data
, data_len
);
518 ctx
->next_in
= (Bytef
*) PHP_HTTP_BUFFER(ctx
->opaque
)->data
;
519 ctx
->avail_in
= PHP_HTTP_BUFFER(ctx
->opaque
)->used
;
521 switch (status
= php_http_inflate_rounds(ctx
, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s
->flags
), decoded
, decoded_len
)) {
526 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx
->opaque
), 0, PHP_HTTP_BUFFER(ctx
->opaque
)->used
- ctx
->avail_in
);
528 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx
->opaque
));
533 /* raw deflated data ? */
534 if (!(s
->flags
& PHP_HTTP_INFLATE_TYPE_RAW
) && !ctx
->total_out
) {
536 s
->flags
|= PHP_HTTP_INFLATE_TYPE_RAW
;
537 inflateInit2(ctx
, PHP_HTTP_WINDOW_BITS_RAW
);
538 goto retry_raw_inflate
;
543 php_error_docref(NULL
, E_WARNING
, "Failed to update inflate stream: %s", zError(status
));
547 static ZEND_RESULT_CODE
dechunk_update(php_http_encoding_stream_t
*s
, const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len
)
549 php_http_buffer_t tmp
;
550 struct dechunk_ctx
*ctx
= s
->ctx
;
553 php_error_docref(NULL
, E_WARNING
, "Dechunk encoding stream has already reached the end of chunked input");
556 if ((PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(&ctx
->buffer
, data
, data_len
)) || !php_http_buffer_fix(&ctx
->buffer
)) {
564 php_http_buffer_init(&tmp
);
566 /* we have data in our buffer */
567 while (ctx
->buffer
.used
) {
569 /* we already know the size of the chunk and are waiting for data */
572 /* not enough data buffered */
573 if (ctx
->buffer
.used
< ctx
->hexlen
) {
576 if (s
->flags
& PHP_HTTP_ENCODING_STREAM_FLUSH_FULL
) {
577 /* flush all data (should only be chunk data) */
578 php_http_buffer_append(&tmp
, ctx
->buffer
.data
, ctx
->buffer
.used
);
579 /* waiting for less data now */
580 ctx
->hexlen
-= ctx
->buffer
.used
;
581 /* no more buffered data */
582 php_http_buffer_reset(&ctx
->buffer
);
586 /* we have too less data and don't need to flush */
592 /* we seem to have all data of the chunk */
594 php_http_buffer_append(&tmp
, ctx
->buffer
.data
, ctx
->hexlen
);
595 /* remove outgoing data from the buffer */
596 php_http_buffer_cut(&ctx
->buffer
, 0, ctx
->hexlen
);
603 /* we don't know the length of the chunk yet */
607 /* ignore preceeding CRLFs (too loose?) */
608 while (off
< ctx
->buffer
.used
&& (
609 ctx
->buffer
.data
[off
] == '\n' ||
610 ctx
->buffer
.data
[off
] == '\r')) {
614 php_http_buffer_cut(&ctx
->buffer
, 0, off
);
617 /* still data there? */
618 if (ctx
->buffer
.used
) {
622 /* we need eol, so we can be sure we have all hex digits */
623 php_http_buffer_fix(&ctx
->buffer
);
624 if ((eolstr
= php_http_locate_bin_eol(ctx
->buffer
.data
, ctx
->buffer
.used
, &eollen
))) {
627 /* read in chunk size */
628 ctx
->hexlen
= strtoul(ctx
->buffer
.data
, &stop
, 16);
630 /* if strtoul() stops at the beginning of the buffered data
631 there's something oddly wrong, i.e. bad input */
632 if (stop
== ctx
->buffer
.data
) {
633 php_error_docref(NULL
, E_WARNING
, "Failed to parse chunk len from '%.*s'", (int) MIN(16, ctx
->buffer
.used
), ctx
->buffer
.data
);
634 php_http_buffer_dtor(&tmp
);
638 /* cut out <chunk size hex><chunk extension><eol> */
639 php_http_buffer_cut(&ctx
->buffer
, 0, eolstr
+ eollen
- ctx
->buffer
.data
);
640 /* buffer->hexlen is 0 now or contains the size of the next chunk */
644 /* ignore following CRLFs (too loose?) */
645 while (off
< ctx
->buffer
.used
&& (
646 ctx
->buffer
.data
[off
] == '\n' ||
647 ctx
->buffer
.data
[off
] == '\r')) {
651 php_http_buffer_cut(&ctx
->buffer
, 0, off
);
659 /* we have not enough data buffered to read in chunk size */
667 php_http_buffer_fix(&tmp
);
669 *decoded_len
= tmp
.used
;
674 static ZEND_RESULT_CODE
deflate_flush(php_http_encoding_stream_t
*s
, char **encoded
, size_t *encoded_len
)
677 z_streamp ctx
= s
->ctx
;
679 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE
;
680 *encoded
= emalloc(*encoded_len
);
684 ctx
->avail_out
= *encoded_len
;
685 ctx
->next_out
= (Bytef
*) *encoded
;
687 switch (status
= deflate(ctx
, Z_FULL_FLUSH
)) {
690 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE
- ctx
->avail_out
;
691 *encoded
= erealloc(*encoded
, *encoded_len
+ 1);
692 (*encoded
)[*encoded_len
] = '\0';
696 PTR_SET(*encoded
, NULL
);
698 php_error_docref(NULL
, E_WARNING
, "Failed to flush deflate stream: %s", zError(status
));
702 static ZEND_RESULT_CODE
dechunk_flush(php_http_encoding_stream_t
*s
, char **decoded
, size_t *decoded_len
)
704 struct dechunk_ctx
*ctx
= s
->ctx
;
707 /* flush all data (should only be chunk data) */
708 php_http_buffer_fix(&ctx
->buffer
);
709 php_http_buffer_data(&ctx
->buffer
, decoded
, decoded_len
);
710 /* waiting for less data now */
711 ctx
->hexlen
-= ctx
->buffer
.used
;
712 /* no more buffered data */
713 php_http_buffer_reset(&ctx
->buffer
);
722 static ZEND_RESULT_CODE
deflate_finish(php_http_encoding_stream_t
*s
, char **encoded
, size_t *encoded_len
)
725 z_streamp ctx
= s
->ctx
;
727 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE
;
728 *encoded
= emalloc(*encoded_len
);
730 /* deflate remaining input */
731 ctx
->next_in
= (Bytef
*) PHP_HTTP_BUFFER(ctx
->opaque
)->data
;
732 ctx
->avail_in
= PHP_HTTP_BUFFER(ctx
->opaque
)->used
;
734 ctx
->avail_out
= *encoded_len
;
735 ctx
->next_out
= (Bytef
*) *encoded
;
738 status
= deflate(ctx
, Z_FINISH
);
739 } while (Z_OK
== status
);
741 if (Z_STREAM_END
== status
) {
742 /* cut processed input off */
743 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx
->opaque
), 0, PHP_HTTP_BUFFER(ctx
->opaque
)->used
- ctx
->avail_in
);
746 *encoded_len
-= ctx
->avail_out
;
747 *encoded
= erealloc(*encoded
, *encoded_len
+ 1);
748 (*encoded
)[*encoded_len
] = '\0';
752 PTR_SET(*encoded
, NULL
);
754 php_error_docref(NULL
, E_WARNING
, "Failed to finish deflate stream: %s", zError(status
));
758 static ZEND_RESULT_CODE
inflate_finish(php_http_encoding_stream_t
*s
, char **decoded
, size_t *decoded_len
)
761 z_streamp ctx
= s
->ctx
;
763 if (!PHP_HTTP_BUFFER(ctx
->opaque
)->used
) {
769 *decoded_len
= (PHP_HTTP_BUFFER(ctx
->opaque
)->used
+ 1) * PHP_HTTP_INFLATE_ROUNDS
;
770 *decoded
= emalloc(*decoded_len
);
772 /* inflate remaining input */
773 ctx
->next_in
= (Bytef
*) PHP_HTTP_BUFFER(ctx
->opaque
)->data
;
774 ctx
->avail_in
= PHP_HTTP_BUFFER(ctx
->opaque
)->used
;
776 ctx
->avail_out
= *decoded_len
;
777 ctx
->next_out
= (Bytef
*) *decoded
;
779 if (Z_STREAM_END
== (status
= inflate(ctx
, Z_FINISH
))) {
780 /* cut processed input off */
781 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx
->opaque
), 0, PHP_HTTP_BUFFER(ctx
->opaque
)->used
- ctx
->avail_in
);
784 *decoded_len
-= ctx
->avail_out
;
785 *decoded
= erealloc(*decoded
, *decoded_len
+ 1);
786 (*decoded
)[*decoded_len
] = '\0';
790 PTR_SET(*decoded
, NULL
);
792 php_error_docref(NULL
, E_WARNING
, "Failed to finish inflate stream: %s", zError(status
));
796 static zend_bool
deflate_done(php_http_encoding_stream_t
*s
)
798 z_streamp ctx
= s
->ctx
;
799 return !ctx
->avail_in
&& !PHP_HTTP_BUFFER(ctx
->opaque
)->used
;
802 static zend_bool
inflate_done(php_http_encoding_stream_t
*s
)
804 z_streamp ctx
= s
->ctx
;
805 return !ctx
->avail_in
&& !PHP_HTTP_BUFFER(ctx
->opaque
)->used
;
808 static zend_bool
dechunk_done(php_http_encoding_stream_t
*s
)
810 return ((struct dechunk_ctx
*) s
->ctx
)->zeroed
;
813 static void deflate_dtor(php_http_encoding_stream_t
*s
)
816 z_streamp ctx
= s
->ctx
;
819 php_http_buffer_free((php_http_buffer_t
**) &ctx
->opaque
);
822 pefree(ctx
, (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
827 static void inflate_dtor(php_http_encoding_stream_t
*s
)
830 z_streamp ctx
= s
->ctx
;
833 php_http_buffer_free((php_http_buffer_t
**) &ctx
->opaque
);
836 pefree(ctx
, (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
841 static void dechunk_dtor(php_http_encoding_stream_t
*s
)
844 struct dechunk_ctx
*ctx
= s
->ctx
;
846 php_http_buffer_dtor(&ctx
->buffer
);
847 pefree(ctx
, (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
852 static php_http_encoding_stream_ops_t php_http_encoding_deflate_ops
= {
862 php_http_encoding_stream_ops_t
*php_http_encoding_stream_get_deflate_ops(void)
864 return &php_http_encoding_deflate_ops
;
867 static php_http_encoding_stream_ops_t php_http_encoding_inflate_ops
= {
877 php_http_encoding_stream_ops_t
*php_http_encoding_stream_get_inflate_ops(void)
879 return &php_http_encoding_inflate_ops
;
882 static php_http_encoding_stream_ops_t php_http_encoding_dechunk_ops
= {
892 php_http_encoding_stream_ops_t
*php_http_encoding_stream_get_dechunk_ops(void)
894 return &php_http_encoding_dechunk_ops
;
897 static zend_object_handlers php_http_encoding_stream_object_handlers
;
899 zend_object
*php_http_encoding_stream_object_new(zend_class_entry
*ce
)
901 return &php_http_encoding_stream_object_new_ex(ce
, NULL
)->zo
;
904 php_http_encoding_stream_object_t
*php_http_encoding_stream_object_new_ex(zend_class_entry
*ce
, php_http_encoding_stream_t
*s
)
906 php_http_encoding_stream_object_t
*o
;
908 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
909 zend_object_std_init(&o
->zo
, ce
);
910 object_properties_init(&o
->zo
, ce
);
916 o
->zo
.handlers
= &php_http_encoding_stream_object_handlers
;
921 zend_object
*php_http_encoding_stream_object_clone(zval
*object
)
923 php_http_encoding_stream_object_t
*new_obj
= NULL
, *old_obj
= PHP_HTTP_OBJ(NULL
, object
);
924 php_http_encoding_stream_t
*cpy
= php_http_encoding_stream_copy(old_obj
->stream
, NULL
);
926 new_obj
= php_http_encoding_stream_object_new_ex(old_obj
->zo
.ce
, cpy
);
927 zend_objects_clone_members(&new_obj
->zo
, &old_obj
->zo
);
932 void php_http_encoding_stream_object_free(zend_object
*object
)
934 php_http_encoding_stream_object_t
*o
= PHP_HTTP_OBJ(object
, NULL
);
937 php_http_encoding_stream_free(&o
->stream
);
939 zend_object_std_dtor(object
);
942 static zend_class_entry
*php_http_encoding_stream_class_entry
;
943 zend_class_entry
*php_http_get_encoding_stream_class_entry(void)
945 return php_http_encoding_stream_class_entry
;
947 static zend_class_entry
*php_http_deflate_stream_class_entry
;
948 zend_class_entry
*php_http_get_deflate_stream_class_entry(void)
950 return php_http_deflate_stream_class_entry
;
952 static zend_class_entry
*php_http_inflate_stream_class_entry
;
953 zend_class_entry
*php_http_get_inflate_stream_class_entry(void)
955 return php_http_inflate_stream_class_entry
;
957 static zend_class_entry
*php_http_dechunk_stream_class_entry
;
958 zend_class_entry
*php_http_get_dechunk_stream_class_entry(void)
960 return php_http_dechunk_stream_class_entry
;
963 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEncodingStream___construct
, 0, 0, 0)
964 ZEND_ARG_INFO(0, flags
)
966 static PHP_METHOD(HttpEncodingStream
, __construct
)
969 php_http_encoding_stream_object_t
*obj
;
970 php_http_encoding_stream_ops_t
*ops
;
972 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &flags
), invalid_arg
, return);
974 obj
= PHP_HTTP_OBJ(NULL
, getThis());
977 php_http_throw(bad_method_call
, "http\\Encoding\\Stream cannot be initialized twice", NULL
);
981 if (instanceof_function(obj
->zo
.ce
, php_http_deflate_stream_class_entry
)) {
982 ops
= &php_http_encoding_deflate_ops
;
983 } else if (instanceof_function(obj
->zo
.ce
, php_http_inflate_stream_class_entry
)) {
984 ops
= &php_http_encoding_inflate_ops
;
985 } else if (instanceof_function(obj
->zo
.ce
, php_http_dechunk_stream_class_entry
)) {
986 ops
= &php_http_encoding_dechunk_ops
;
988 php_http_throw(runtime
, "Unknown http\\Encoding\\Stream class '%s'", obj
->zo
.ce
->name
->val
);
992 php_http_expect(obj
->stream
= php_http_encoding_stream_init(obj
->stream
, ops
, flags
), runtime
, return);
995 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEncodingStream_update
, 0, 0, 1)
996 ZEND_ARG_INFO(0, data
)
998 static PHP_METHOD(HttpEncodingStream
, update
)
1003 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &data_str
, &data_len
)) {
1004 php_http_encoding_stream_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1007 char *encoded_str
= NULL
;
1010 if (SUCCESS
== php_http_encoding_stream_update(obj
->stream
, data_str
, data_len
, &encoded_str
, &encoded_len
)) {
1012 RETURN_STR(php_http_cs2zs(encoded_str
, encoded_len
));
1014 RETURN_EMPTY_STRING();
1021 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEncodingStream_flush
, 0, 0, 0)
1022 ZEND_END_ARG_INFO();
1023 static PHP_METHOD(HttpEncodingStream
, flush
)
1025 if (SUCCESS
== zend_parse_parameters_none()) {
1026 php_http_encoding_stream_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1029 char *encoded_str
= NULL
;
1032 if (SUCCESS
== php_http_encoding_stream_flush(obj
->stream
, &encoded_str
, &encoded_len
)) {
1034 RETURN_STR(php_http_cs2zs(encoded_str
, encoded_len
));
1036 RETURN_EMPTY_STRING();
1043 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEncodingStream_done
, 0, 0, 0)
1044 ZEND_END_ARG_INFO();
1045 static PHP_METHOD(HttpEncodingStream
, done
)
1047 if (SUCCESS
== zend_parse_parameters_none()) {
1048 php_http_encoding_stream_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1051 RETURN_BOOL(php_http_encoding_stream_done(obj
->stream
));
1056 ZEND_BEGIN_ARG_INFO_EX(ai_HttpEncodingStream_finish
, 0, 0, 0)
1057 ZEND_END_ARG_INFO();
1058 static PHP_METHOD(HttpEncodingStream
, finish
)
1060 if (SUCCESS
== zend_parse_parameters_none()) {
1061 php_http_encoding_stream_object_t
*obj
= PHP_HTTP_OBJ(NULL
, getThis());
1064 char *encoded_str
= NULL
;
1067 if (SUCCESS
== php_http_encoding_stream_finish(obj
->stream
, &encoded_str
, &encoded_len
)) {
1068 if (SUCCESS
== php_http_encoding_stream_reset(&obj
->stream
)) {
1070 RETURN_STR(php_http_cs2zs(encoded_str
, encoded_len
));
1072 RETURN_EMPTY_STRING();
1075 PTR_FREE(encoded_str
);
1082 static zend_function_entry php_http_encoding_stream_methods
[] = {
1083 PHP_ME(HttpEncodingStream
, __construct
, ai_HttpEncodingStream___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
1084 PHP_ME(HttpEncodingStream
, update
, ai_HttpEncodingStream_update
, ZEND_ACC_PUBLIC
)
1085 PHP_ME(HttpEncodingStream
, flush
, ai_HttpEncodingStream_flush
, ZEND_ACC_PUBLIC
)
1086 PHP_ME(HttpEncodingStream
, done
, ai_HttpEncodingStream_done
, ZEND_ACC_PUBLIC
)
1087 PHP_ME(HttpEncodingStream
, finish
, ai_HttpEncodingStream_finish
, ZEND_ACC_PUBLIC
)
1088 EMPTY_FUNCTION_ENTRY
1091 ZEND_BEGIN_ARG_INFO_EX(ai_HttpDeflateStream_encode
, 0, 0, 1)
1092 ZEND_ARG_INFO(0, data
)
1093 ZEND_ARG_INFO(0, flags
)
1094 ZEND_END_ARG_INFO();
1095 static PHP_METHOD(HttpDeflateStream
, encode
)
1099 zend_long flags
= 0;
1101 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &str
, &len
, &flags
)) {
1102 char *enc_str
= NULL
;
1105 if (SUCCESS
== php_http_encoding_deflate(flags
, str
, len
, &enc_str
, &enc_len
)) {
1107 RETURN_STR(php_http_cs2zs(enc_str
, enc_len
));
1109 RETURN_EMPTY_STRING();
1116 static zend_function_entry php_http_deflate_stream_methods
[] = {
1117 PHP_ME(HttpDeflateStream
, encode
, ai_HttpDeflateStream_encode
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
1118 EMPTY_FUNCTION_ENTRY
1121 ZEND_BEGIN_ARG_INFO_EX(ai_HttpInflateStream_decode
, 0, 0, 1)
1122 ZEND_ARG_INFO(0, data
)
1123 ZEND_END_ARG_INFO();
1124 static PHP_METHOD(HttpInflateStream
, decode
)
1129 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str
, &len
)) {
1130 char *enc_str
= NULL
;
1133 if (SUCCESS
== php_http_encoding_inflate(str
, len
, &enc_str
, &enc_len
)) {
1135 RETURN_STR(php_http_cs2zs(enc_str
, enc_len
));
1137 RETURN_EMPTY_STRING();
1144 static zend_function_entry php_http_inflate_stream_methods
[] = {
1145 PHP_ME(HttpInflateStream
, decode
, ai_HttpInflateStream_decode
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
1146 EMPTY_FUNCTION_ENTRY
1149 ZEND_BEGIN_ARG_INFO_EX(ai_HttpDechunkStream_decode
, 0, 0, 1)
1150 ZEND_ARG_INFO(0, data
)
1151 ZEND_ARG_INFO(1, decoded_len
)
1152 ZEND_END_ARG_INFO();
1153 static PHP_METHOD(HttpDechunkStream
, decode
)
1159 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!", &str
, &len
, &zlen
)) {
1160 const char *end_ptr
;
1161 char *enc_str
= NULL
;
1164 if ((end_ptr
= php_http_encoding_dechunk(str
, len
, &enc_str
, &enc_len
))) {
1168 ZVAL_LONG(zlen
, str
+ len
- end_ptr
);
1171 RETURN_STR(php_http_cs2zs(enc_str
, enc_len
));
1173 RETURN_EMPTY_STRING();
1180 static zend_function_entry php_http_dechunk_stream_methods
[] = {
1181 PHP_ME(HttpDechunkStream
, decode
, ai_HttpDechunkStream_decode
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
1182 EMPTY_FUNCTION_ENTRY
1185 PHP_MINIT_FUNCTION(http_encoding
)
1187 zend_class_entry ce
= {0};
1189 INIT_NS_CLASS_ENTRY(ce
, "http\\Encoding", "Stream", php_http_encoding_stream_methods
);
1190 php_http_encoding_stream_class_entry
= zend_register_internal_class(&ce
);
1191 php_http_encoding_stream_class_entry
->ce_flags
|= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS
;
1192 php_http_encoding_stream_class_entry
->create_object
= php_http_encoding_stream_object_new
;
1193 memcpy(&php_http_encoding_stream_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
1194 php_http_encoding_stream_object_handlers
.offset
= XtOffsetOf(php_http_encoding_stream_object_t
, zo
);
1195 php_http_encoding_stream_object_handlers
.clone_obj
= php_http_encoding_stream_object_clone
;
1196 php_http_encoding_stream_object_handlers
.free_obj
= php_http_encoding_stream_object_free
;
1198 zend_declare_class_constant_long(php_http_encoding_stream_class_entry
, ZEND_STRL("FLUSH_NONE"), PHP_HTTP_ENCODING_STREAM_FLUSH_NONE
);
1199 zend_declare_class_constant_long(php_http_encoding_stream_class_entry
, ZEND_STRL("FLUSH_SYNC"), PHP_HTTP_ENCODING_STREAM_FLUSH_SYNC
);
1200 zend_declare_class_constant_long(php_http_encoding_stream_class_entry
, ZEND_STRL("FLUSH_FULL"), PHP_HTTP_ENCODING_STREAM_FLUSH_FULL
);
1202 memset(&ce
, 0, sizeof(ce
));
1203 INIT_NS_CLASS_ENTRY(ce
, "http\\Encoding\\Stream", "Deflate", php_http_deflate_stream_methods
);
1204 php_http_deflate_stream_class_entry
= zend_register_internal_class_ex(&ce
, php_http_encoding_stream_class_entry
);
1205 php_http_deflate_stream_class_entry
->create_object
= php_http_encoding_stream_object_new
;
1207 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("TYPE_GZIP"), PHP_HTTP_DEFLATE_TYPE_GZIP
);
1208 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("TYPE_ZLIB"), PHP_HTTP_DEFLATE_TYPE_ZLIB
);
1209 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("TYPE_RAW"), PHP_HTTP_DEFLATE_TYPE_RAW
);
1210 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("LEVEL_DEF"), PHP_HTTP_DEFLATE_LEVEL_DEF
);
1211 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("LEVEL_MIN"), PHP_HTTP_DEFLATE_LEVEL_MIN
);
1212 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("LEVEL_MAX"), PHP_HTTP_DEFLATE_LEVEL_MAX
);
1213 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("STRATEGY_DEF"), PHP_HTTP_DEFLATE_STRATEGY_DEF
);
1214 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("STRATEGY_FILT"), PHP_HTTP_DEFLATE_STRATEGY_FILT
);
1215 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("STRATEGY_HUFF"), PHP_HTTP_DEFLATE_STRATEGY_HUFF
);
1216 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("STRATEGY_RLE"), PHP_HTTP_DEFLATE_STRATEGY_RLE
);
1217 zend_declare_class_constant_long(php_http_deflate_stream_class_entry
, ZEND_STRL("STRATEGY_FIXED"), PHP_HTTP_DEFLATE_STRATEGY_FIXED
);
1219 memset(&ce
, 0, sizeof(ce
));
1220 INIT_NS_CLASS_ENTRY(ce
, "http\\Encoding\\Stream", "Inflate", php_http_inflate_stream_methods
);
1221 php_http_inflate_stream_class_entry
= zend_register_internal_class_ex(&ce
, php_http_encoding_stream_class_entry
);
1222 php_http_inflate_stream_class_entry
->create_object
= php_http_encoding_stream_object_new
;
1224 memset(&ce
, 0, sizeof(ce
));
1225 INIT_NS_CLASS_ENTRY(ce
, "http\\Encoding\\Stream", "Dechunk", php_http_dechunk_stream_methods
);
1226 php_http_dechunk_stream_class_entry
= zend_register_internal_class_ex(&ce
, php_http_encoding_stream_class_entry
);
1227 php_http_dechunk_stream_class_entry
->create_object
= php_http_encoding_stream_object_new
;
1238 * vim600: noet sw=4 ts=4 fdm=marker
1239 * vim<600: noet sw=4 ts=4