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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
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 PHP_HTTP_API
const char *php_http_encoding_dechunk(const char *encoded
, size_t encoded_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
35 const char *e_ptr
= encoded
;
38 *decoded
= ecalloc(1, encoded_len
);
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_http_error(HE_NOTICE
, PHP_HTTP_E_ENCODING
, "Data does not seem to be chunked encoded");
54 memcpy(*decoded
, encoded
, encoded_len
);
55 *decoded_len
= encoded_len
;
56 return encoded
+ encoded_len
;
59 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Expected chunk size at pos %tu of %zu but got trash", n_ptr
- encoded
, encoded_len
);
66 /* move over '0' chunked encoding terminator and any new lines */
79 /* there should be CRLF after the chunk size, but we'll ignore SP+ too */
80 if (*n_ptr
&& !eol_match(&n_ptr
, &eol_len
)) {
82 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Expected CRLF at pos %tu of %zu but got 0x%02X 0x%02X", n_ptr
- encoded
, encoded_len
, *n_ptr
, *(n_ptr
+ 1));
84 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Expected LF at pos %tu of %zu but got 0x%02X", n_ptr
- encoded
, encoded_len
, *n_ptr
);
89 /* chunk size pretends more data than we actually got, so it's probably a truncated message */
90 if (chunk_len
> (rest
= encoded
+ encoded_len
- n_ptr
)) {
91 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Truncated message: chunk size %lu exceeds remaining data size %lu at pos %tu of %zu", chunk_len
, rest
, n_ptr
- encoded
, encoded_len
);
96 memcpy(*decoded
+ *decoded_len
, n_ptr
, chunk_len
);
97 *decoded_len
+= chunk_len
;
99 if (chunk_len
== rest
) {
100 e_ptr
= n_ptr
+ chunk_len
;
103 /* advance to next chunk */
104 e_ptr
= n_ptr
+ chunk_len
+ eol_len
;
111 static inline int php_http_inflate_rounds(z_stream
*Z
, int flush
, char **buf
, size_t *len
)
113 int status
= 0, round
= 0;
114 php_http_buffer_t buffer
;
119 php_http_buffer_init_ex(&buffer
, Z
->avail_in
, PHP_HTTP_BUFFER_INIT_PREALLOC
);
122 if (PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_resize_ex(&buffer
, buffer
.size
, 0, 1)) {
123 status
= Z_MEM_ERROR
;
125 Z
->avail_out
= buffer
.free
;
126 Z
->next_out
= (Bytef
*) buffer
.data
+ buffer
.used
;
128 fprintf(stderr
, "\n%3d: %3d PRIOR: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round
, status
, buffer
.size
, buffer
.free
, buffer
.used
, Z
->avail_in
, Z
->avail_out
);
130 status
= inflate(Z
, flush
);
132 buffer
.used
+= buffer
.free
- Z
->avail_out
;
133 buffer
.free
= Z
->avail_out
;
135 fprintf(stderr
, "%3d: %3d AFTER: size=%7lu,\tfree=%7lu,\tused=%7lu,\tavail_in=%7lu,\tavail_out=%7lu\n", round
, status
, buffer
.size
, buffer
.free
, buffer
.used
, Z
->avail_in
, Z
->avail_out
);
137 PHP_HTTP_INFLATE_BUFFER_SIZE_ALIGN(buffer
.size
);
139 } while ((Z_BUF_ERROR
== status
|| (Z_OK
== status
&& Z
->avail_in
)) && ++round
< PHP_HTTP_INFLATE_ROUNDS
);
141 if (status
== Z_OK
|| status
== Z_STREAM_END
) {
142 php_http_buffer_shrink(&buffer
);
143 php_http_buffer_fix(&buffer
);
147 php_http_buffer_dtor(&buffer
);
153 PHP_HTTP_API STATUS
php_http_encoding_deflate(int flags
, const char *data
, size_t data_len
, char **encoded
, size_t *encoded_len TSRMLS_DC
)
155 int status
, level
, wbits
, strategy
;
158 PHP_HTTP_DEFLATE_LEVEL_SET(flags
, level
);
159 PHP_HTTP_DEFLATE_WBITS_SET(flags
, wbits
);
160 PHP_HTTP_DEFLATE_STRATEGY_SET(flags
, strategy
);
162 memset(&Z
, 0, sizeof(z_stream
));
166 status
= deflateInit2(&Z
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
);
167 if (Z_OK
== status
) {
168 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
169 *encoded
= emalloc(*encoded_len
);
171 Z
.next_in
= (Bytef
*) data
;
172 Z
.next_out
= (Bytef
*) *encoded
;
173 Z
.avail_in
= data_len
;
174 Z
.avail_out
= *encoded_len
;
176 status
= deflate(&Z
, Z_FINISH
);
179 if (Z_STREAM_END
== status
) {
180 /* size buffer down to actual length */
181 *encoded
= erealloc(*encoded
, Z
.total_out
+ 1);
182 (*encoded
)[*encoded_len
= Z
.total_out
] = '\0';
185 STR_SET(*encoded
, NULL
);
190 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Could not deflate data: %s", zError(status
));
194 PHP_HTTP_API STATUS
php_http_encoding_inflate(const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
197 int status
, wbits
= PHP_HTTP_WINDOW_BITS_ANY
;
199 memset(&Z
, 0, sizeof(z_stream
));
202 status
= inflateInit2(&Z
, wbits
);
203 if (Z_OK
== status
) {
204 Z
.next_in
= (Bytef
*) data
;
205 Z
.avail_in
= data_len
;
207 switch (status
= php_http_inflate_rounds(&Z
, Z_NO_FLUSH
, decoded
, decoded_len
)) {
213 status
= Z_DATA_ERROR
;
217 /* raw deflated data? */
218 if (PHP_HTTP_WINDOW_BITS_ANY
== wbits
) {
220 wbits
= PHP_HTTP_WINDOW_BITS_RAW
;
221 goto retry_raw_inflate
;
227 if (decoded_len
&& *decoded
) {
232 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Could not inflate data: %s", zError(status
));
236 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
)
241 s
= pemalloc(sizeof(*s
), (flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
243 memset(s
, 0, sizeof(*s
));
246 TSRMLS_SET_CTX(s
->ts
);
248 if ((s
->ops
= ops
)) {
249 php_http_encoding_stream_t
*ss
= s
->ops
->init(s
);
259 pefree(s
, (flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
264 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 TSRMLS_FETCH_FROM_CTX(from
->ts
);
268 if (!from
->ops
->copy
) {
272 return from
->ops
->copy(from
, php_http_encoding_stream_init(to
, from
->ops
, from
->flags TSRMLS_CC
));
275 PHP_HTTP_API STATUS
php_http_encoding_stream_reset(php_http_encoding_stream_t
**s
)
277 php_http_encoding_stream_t
*ss
;
278 if ((*s
)->ops
->dtor
) {
281 if ((ss
= (*s
)->ops
->init(*s
))) {
288 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 if (!s
->ops
->update
) {
293 return s
->ops
->update(s
, in_str
, in_len
, out_str
, out_len
);
296 PHP_HTTP_API STATUS
php_http_encoding_stream_flush(php_http_encoding_stream_t
*s
, char **out_str
, size_t *out_len
)
298 if (!s
->ops
->flush
) {
303 return s
->ops
->flush(s
, out_str
, out_len
);
306 PHP_HTTP_API zend_bool
php_http_encoding_stream_done(php_http_encoding_stream_t
*s
)
311 return s
->ops
->done(s
);
314 PHP_HTTP_API STATUS
php_http_encoding_stream_finish(php_http_encoding_stream_t
*s
, char **out_str
, size_t *out_len
)
316 if (!s
->ops
->finish
) {
321 return s
->ops
->finish(s
, out_str
, out_len
);
324 PHP_HTTP_API
void php_http_encoding_stream_dtor(php_http_encoding_stream_t
*s
)
331 PHP_HTTP_API
void php_http_encoding_stream_free(php_http_encoding_stream_t
**s
)
334 if ((*s
)->ops
->dtor
) {
337 pefree(*s
, ((*s
)->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
343 php_http_buffer_t buffer
;
348 static php_http_encoding_stream_t
*deflate_init(php_http_encoding_stream_t
*s
)
350 int status
, level
, wbits
, strategy
, p
= (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
);
351 z_streamp ctx
= pecalloc(1, sizeof(z_stream
), p
);
352 TSRMLS_FETCH_FROM_CTX(s
->ts
);
354 PHP_HTTP_DEFLATE_LEVEL_SET(s
->flags
, level
);
355 PHP_HTTP_DEFLATE_WBITS_SET(s
->flags
, wbits
);
356 PHP_HTTP_DEFLATE_STRATEGY_SET(s
->flags
, strategy
);
358 if (Z_OK
== (status
= deflateInit2(ctx
, level
, Z_DEFLATED
, wbits
, MAX_MEM_LEVEL
, strategy
))) {
359 if ((ctx
->opaque
= php_http_buffer_init_ex(NULL
, PHP_HTTP_DEFLATE_BUFFER_SIZE
, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0))) {
364 status
= Z_MEM_ERROR
;
367 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Failed to initialize deflate encoding stream: %s", zError(status
));
371 static php_http_encoding_stream_t
*inflate_init(php_http_encoding_stream_t
*s
)
373 int status
, wbits
, p
= (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
);
374 z_streamp ctx
= pecalloc(1, sizeof(z_stream
), p
);
375 TSRMLS_FETCH_FROM_CTX(s
->ts
);
377 PHP_HTTP_INFLATE_WBITS_SET(s
->flags
, wbits
);
379 if (Z_OK
== (status
= inflateInit2(ctx
, wbits
))) {
380 if ((ctx
->opaque
= php_http_buffer_init_ex(NULL
, PHP_HTTP_DEFLATE_BUFFER_SIZE
, p
? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0))) {
385 status
= Z_MEM_ERROR
;
388 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Failed to initialize inflate stream: %s", zError(status
));
392 static php_http_encoding_stream_t
*dechunk_init(php_http_encoding_stream_t
*s
)
394 struct dechunk_ctx
*ctx
= pecalloc(1, sizeof(*ctx
), (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
396 if (!php_http_buffer_init_ex(&ctx
->buffer
, PHP_HTTP_BUFFER_DEFAULT_SIZE
, (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
) ? PHP_HTTP_BUFFER_INIT_PERSISTENT
: 0)) {
407 static php_http_encoding_stream_t
*deflate_copy(php_http_encoding_stream_t
*from
, php_http_encoding_stream_t
*to
)
409 z_streamp from_ctx
= from
->ctx
, to_ctx
= to
->ctx
;
411 deflateCopy(to_ctx
, from_ctx
);
412 php_http_buffer_append(to_ctx
->opaque
, PHP_HTTP_BUFFER_VAL(from_ctx
->opaque
), PHP_HTTP_BUFFER_LEN(from_ctx
->opaque
));
417 static php_http_encoding_stream_t
*inflate_copy(php_http_encoding_stream_t
*from
, php_http_encoding_stream_t
*to
)
419 z_streamp from_ctx
= from
->ctx
, to_ctx
= to
->ctx
;
421 inflateCopy(to_ctx
, from_ctx
);
422 php_http_buffer_append(to_ctx
->opaque
, PHP_HTTP_BUFFER_VAL(from_ctx
->opaque
), PHP_HTTP_BUFFER_LEN(from_ctx
->opaque
));
427 static php_http_encoding_stream_t
*dechunk_copy(php_http_encoding_stream_t
*from
, php_http_encoding_stream_t
*to
)
429 struct dechunk_ctx
*from_ctx
= from
->ctx
, *to_ctx
= to
->ctx
;
431 to_ctx
->hexlen
= from_ctx
->hexlen
;
432 to_ctx
->zeroed
= from_ctx
->zeroed
;
433 php_http_buffer_append(&to_ctx
->buffer
, PHP_HTTP_BUFFER_VAL(&from_ctx
->buffer
), PHP_HTTP_BUFFER_LEN(&from_ctx
->buffer
));
438 static STATUS
deflate_update(php_http_encoding_stream_t
*s
, const char *data
, size_t data_len
, char **encoded
, size_t *encoded_len
)
441 z_streamp ctx
= s
->ctx
;
442 TSRMLS_FETCH_FROM_CTX(s
->ts
);
444 /* append input to our buffer */
445 php_http_buffer_append(PHP_HTTP_BUFFER(ctx
->opaque
), data
, data_len
);
447 ctx
->next_in
= (Bytef
*) PHP_HTTP_BUFFER_VAL(ctx
->opaque
);
448 ctx
->avail_in
= PHP_HTTP_BUFFER_LEN(ctx
->opaque
);
451 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE_GUESS(data_len
);
452 *encoded
= emalloc(*encoded_len
);
453 ctx
->avail_out
= *encoded_len
;
454 ctx
->next_out
= (Bytef
*) *encoded
;
456 switch (status
= deflate(ctx
, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s
->flags
))) {
459 /* cut processed chunk off the buffer */
461 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx
->opaque
), 0, PHP_HTTP_BUFFER_LEN(ctx
->opaque
) - ctx
->avail_in
);
463 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx
->opaque
));
466 /* size buffer down to actual size */
467 *encoded_len
-= ctx
->avail_out
;
468 *encoded
= erealloc(*encoded
, *encoded_len
+ 1);
469 (*encoded
)[*encoded_len
] = '\0';
473 STR_SET(*encoded
, NULL
);
475 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Failed to update deflate stream: %s", zError(status
));
479 static STATUS
inflate_update(php_http_encoding_stream_t
*s
, const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len
)
482 z_streamp ctx
= s
->ctx
;
483 TSRMLS_FETCH_FROM_CTX(s
->ts
);
485 /* append input to buffer */
486 php_http_buffer_append(PHP_HTTP_BUFFER(ctx
->opaque
), data
, data_len
);
489 ctx
->next_in
= (Bytef
*) PHP_HTTP_BUFFER_VAL(ctx
->opaque
);
490 ctx
->avail_in
= PHP_HTTP_BUFFER_LEN(ctx
->opaque
);
492 switch (status
= php_http_inflate_rounds(ctx
, PHP_HTTP_ENCODING_STREAM_FLUSH_FLAG(s
->flags
), decoded
, decoded_len
)) {
497 php_http_buffer_cut(PHP_HTTP_BUFFER(ctx
->opaque
), 0, PHP_HTTP_BUFFER_LEN(ctx
->opaque
) - ctx
->avail_in
);
499 php_http_buffer_reset(PHP_HTTP_BUFFER(ctx
->opaque
));
504 /* raw deflated data ? */
505 if (!(s
->flags
& PHP_HTTP_INFLATE_TYPE_RAW
) && !ctx
->total_out
) {
507 s
->flags
|= PHP_HTTP_INFLATE_TYPE_RAW
;
508 inflateInit2(ctx
, PHP_HTTP_WINDOW_BITS_RAW
);
509 goto retry_raw_inflate
;
514 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Failed to update inflate stream: %s", zError(status
));
518 static STATUS
dechunk_update(php_http_encoding_stream_t
*s
, const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len
)
520 php_http_buffer_t tmp
;
521 struct dechunk_ctx
*ctx
= s
->ctx
;
522 TSRMLS_FETCH_FROM_CTX(s
->ts
);
525 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Dechunk encoding stream has already reached the end of chunked input");
528 if ((PHP_HTTP_BUFFER_NOMEM
== php_http_buffer_append(&ctx
->buffer
, data
, data_len
)) || !php_http_buffer_fix(&ctx
->buffer
)) {
536 php_http_buffer_init(&tmp
);
538 /* we have data in our buffer */
539 while (PHP_HTTP_BUFFER_LEN(&ctx
->buffer
)) {
541 /* we already know the size of the chunk and are waiting for data */
544 /* not enough data buffered */
545 if (PHP_HTTP_BUFFER_LEN(&ctx
->buffer
) < ctx
->hexlen
) {
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
);
558 /* we have too less data and don't need to flush */
564 /* we seem to have all data of the chunk */
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
);
575 /* we don't know the length of the chunk yet */
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')) {
586 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx
->buffer
), 0, off
);
589 /* still data there? */
590 if (PHP_HTTP_BUFFER_LEN(&ctx
->buffer
)) {
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
))) {
599 /* read in chunk size */
600 ctx
->hexlen
= strtoul(PHP_HTTP_BUFFER_VAL(&ctx
->buffer
), &stop
, 16);
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
);
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 */
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')) {
623 php_http_buffer_cut(PHP_HTTP_BUFFER(&ctx
->buffer
), 0, off
);
631 /* we have not enough data buffered to read in chunk size */
639 php_http_buffer_fix(&tmp
);
640 *decoded
= PHP_HTTP_BUFFER_VAL(&tmp
);
641 *decoded_len
= PHP_HTTP_BUFFER_LEN(&tmp
);
646 static STATUS
deflate_flush(php_http_encoding_stream_t
*s
, char **encoded
, size_t *encoded_len
)
649 z_streamp ctx
= s
->ctx
;
650 TSRMLS_FETCH_FROM_CTX(s
->ts
);
652 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE
;
653 *encoded
= emalloc(*encoded_len
);
657 ctx
->avail_out
= *encoded_len
;
658 ctx
->next_out
= (Bytef
*) *encoded
;
660 switch (status
= deflate(ctx
, Z_FULL_FLUSH
)) {
663 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE
- ctx
->avail_out
;
664 *encoded
= erealloc(*encoded
, *encoded_len
+ 1);
665 (*encoded
)[*encoded_len
] = '\0';
669 STR_SET(*encoded
, NULL
);
671 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Failed to flush deflate stream: %s", zError(status
));
675 static STATUS
dechunk_flush(php_http_encoding_stream_t
*s
, char **decoded
, size_t *decoded_len
)
677 struct dechunk_ctx
*ctx
= s
->ctx
;
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
);
695 static STATUS
deflate_finish(php_http_encoding_stream_t
*s
, char **encoded
, size_t *encoded_len
)
698 z_streamp ctx
= s
->ctx
;
699 TSRMLS_FETCH_FROM_CTX(s
->ts
);
701 *encoded_len
= PHP_HTTP_DEFLATE_BUFFER_SIZE
;
702 *encoded
= emalloc(*encoded_len
);
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
);
708 ctx
->avail_out
= *encoded_len
;
709 ctx
->next_out
= (Bytef
*) *encoded
;
712 status
= deflate(ctx
, Z_FINISH
);
713 } while (Z_OK
== status
);
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
);
720 *encoded_len
-= ctx
->avail_out
;
721 *encoded
= erealloc(*encoded
, *encoded_len
+ 1);
722 (*encoded
)[*encoded_len
] = '\0';
726 STR_SET(*encoded
, NULL
);
728 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Failed to finish deflate stream: %s", zError(status
));
732 static STATUS
inflate_finish(php_http_encoding_stream_t
*s
, char **decoded
, size_t *decoded_len
)
735 z_streamp ctx
= s
->ctx
;
736 TSRMLS_FETCH_FROM_CTX(s
->ts
);
738 if (!PHP_HTTP_BUFFER_LEN(ctx
->opaque
)) {
744 *decoded_len
= (PHP_HTTP_BUFFER_LEN(ctx
->opaque
) + 1) * PHP_HTTP_INFLATE_ROUNDS
;
745 *decoded
= emalloc(*decoded_len
);
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
);
751 ctx
->avail_out
= *decoded_len
;
752 ctx
->next_out
= (Bytef
*) *decoded
;
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
);
759 *decoded_len
-= ctx
->avail_out
;
760 *decoded
= erealloc(*decoded
, *decoded_len
+ 1);
761 (*decoded
)[*decoded_len
] = '\0';
765 STR_SET(*decoded
, NULL
);
767 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Failed to finish inflate stream: %s", zError(status
));
771 static zend_bool
deflate_done(php_http_encoding_stream_t
*s
)
773 z_streamp ctx
= s
->ctx
;
774 return !ctx
->avail_in
&& !PHP_HTTP_BUFFER_LEN(ctx
->opaque
);
777 static zend_bool
inflate_done(php_http_encoding_stream_t
*s
)
779 z_streamp ctx
= s
->ctx
;
780 return !ctx
->avail_in
&& !PHP_HTTP_BUFFER_LEN(ctx
->opaque
);
783 static zend_bool
dechunk_done(php_http_encoding_stream_t
*s
)
785 return ((struct dechunk_ctx
*) s
->ctx
)->zeroed
;
788 static void deflate_dtor(php_http_encoding_stream_t
*s
)
791 z_streamp ctx
= s
->ctx
;
794 php_http_buffer_free((php_http_buffer_t
**) &ctx
->opaque
);
797 pefree(ctx
, (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
802 static void inflate_dtor(php_http_encoding_stream_t
*s
)
805 z_streamp ctx
= s
->ctx
;
808 php_http_buffer_free((php_http_buffer_t
**) &ctx
->opaque
);
811 pefree(ctx
, (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
816 static void dechunk_dtor(php_http_encoding_stream_t
*s
)
819 struct dechunk_ctx
*ctx
= s
->ctx
;
821 php_http_buffer_dtor(&ctx
->buffer
);
822 pefree(ctx
, (s
->flags
& PHP_HTTP_ENCODING_STREAM_PERSISTENT
));
827 static php_http_encoding_stream_ops_t php_http_encoding_deflate_ops
= {
837 PHP_HTTP_API php_http_encoding_stream_ops_t
*php_http_encoding_stream_get_deflate_ops(void)
839 return &php_http_encoding_deflate_ops
;
842 static php_http_encoding_stream_ops_t php_http_encoding_inflate_ops
= {
852 PHP_HTTP_API php_http_encoding_stream_ops_t
*php_http_encoding_stream_get_inflate_ops(void)
854 return &php_http_encoding_inflate_ops
;
857 static php_http_encoding_stream_ops_t php_http_encoding_dechunk_ops
= {
867 PHP_HTTP_API php_http_encoding_stream_ops_t
*php_http_encoding_stream_get_dechunk_ops(void)
869 return &php_http_encoding_dechunk_ops
;
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)
876 PHP_HTTP_BEGIN_ARGS(__construct
, 0)
877 PHP_HTTP_ARG_VAL(flags
, 0)
880 PHP_HTTP_BEGIN_ARGS(update
, 1)
881 PHP_HTTP_ARG_VAL(data
, 0)
884 PHP_HTTP_EMPTY_ARGS(flush
);
885 PHP_HTTP_EMPTY_ARGS(done
);
886 PHP_HTTP_EMPTY_ARGS(finish
);
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
)
898 static zend_object_handlers php_http_encoding_stream_object_handlers
;
900 zend_object_value
php_http_encoding_stream_object_new(zend_class_entry
*ce TSRMLS_DC
)
902 return php_http_encoding_stream_object_new_ex(ce
, NULL
, NULL TSRMLS_CC
);
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
)
907 zend_object_value ov
;
908 php_http_encoding_stream_object_t
*o
;
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
);
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
;
928 zend_object_value
php_http_encoding_stream_object_clone(zval
*this_ptr TSRMLS_DC
)
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
);
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
);
939 void php_http_encoding_stream_object_free(void *object TSRMLS_DC
)
941 php_http_encoding_stream_object_t
*o
= (php_http_encoding_stream_object_t
*) object
;
944 php_http_encoding_stream_free(&o
->stream
);
946 zend_object_std_dtor((zend_object
*) o TSRMLS_CC
);
950 PHP_METHOD(HttpEncodingStream
, __construct
)
952 with_error_handling(EH_THROW
, php_http_exception_class_entry
) {
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
);
960 php_http_encoding_stream_ops_t
*ops
= NULL
;
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
;
971 obj
->stream
= php_http_encoding_stream_init(obj
->stream
, ops
, flags TSRMLS_CC
);
973 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "Unknown HttpEncodingStream class %s", obj
->zo
.ce
->name
);
977 php_http_error(HE_WARNING
, PHP_HTTP_E_ENCODING
, "HttpEncodingStream cannot be initialized twice");
979 } end_error_handling();
981 } end_error_handling();
984 PHP_METHOD(HttpEncodingStream
, update
)
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
);
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);
1004 PHP_METHOD(HttpEncodingStream
, flush
)
1006 if (SUCCESS
== zend_parse_parameters_none()) {
1007 php_http_encoding_stream_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1013 if (SUCCESS
== php_http_encoding_stream_flush(obj
->stream
, &encoded_str
, &encoded_len
)) {
1014 RETURN_STRINGL(encoded_str
, encoded_len
, 0);
1021 PHP_METHOD(HttpEncodingStream
, done
)
1023 if (SUCCESS
== zend_parse_parameters_none()) {
1024 php_http_encoding_stream_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
1027 RETURN_BOOL(php_http_encoding_stream_done(obj
->stream
));
1033 PHP_METHOD(HttpEncodingStream
, finish
)
1035 if (SUCCESS
== zend_parse_parameters_none()) {
1036 php_http_encoding_stream_object_t
*obj
= zend_object_store_get_object(getThis() TSRMLS_CC
);
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);
1046 STR_FREE(encoded_str
);
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)
1060 PHP_HTTP_BEGIN_ARGS(encode
, 1)
1061 PHP_HTTP_ARG_VAL(data
, 0)
1062 PHP_HTTP_ARG_VAL(flags
, 0)
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
)
1069 EMPTY_FUNCTION_ENTRY
1072 PHP_METHOD(HttpDeflateStream
, encode
)
1078 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|l", &str
, &len
, &flags
)) {
1082 if (SUCCESS
== php_http_encoding_deflate(flags
, str
, len
, &enc_str
, &enc_len TSRMLS_CC
)) {
1083 RETURN_STRINGL(enc_str
, enc_len
, 0);
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)
1095 PHP_HTTP_BEGIN_ARGS(decode
, 1)
1096 PHP_HTTP_ARG_VAL(data
, 0)
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
)
1103 EMPTY_FUNCTION_ENTRY
1106 PHP_METHOD(HttpInflateStream
, decode
)
1111 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &str
, &len
)) {
1115 if (SUCCESS
== php_http_encoding_inflate(str
, len
, &enc_str
, &enc_len TSRMLS_CC
)) {
1116 RETURN_STRINGL(enc_str
, enc_len
, 0);
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)
1128 PHP_HTTP_BEGIN_ARGS(decode
, 1)
1129 PHP_HTTP_ARG_VAL(data
, 0)
1130 PHP_HTTP_ARG_VAL(decoded_len
, 1)
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
)
1137 EMPTY_FUNCTION_ENTRY
1140 PHP_METHOD(HttpDechunkStream
, decode
)
1146 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s|z!", &str
, &len
, &zlen
)) {
1147 const char *end_ptr
;
1151 if ((end_ptr
= php_http_encoding_dechunk(str
, len
, &enc_str
, &enc_len TSRMLS_CC
))) {
1154 ZVAL_LONG(zlen
, str
+ len
- end_ptr
);
1156 RETURN_STRINGL(enc_str
, enc_len
, 0);
1162 PHP_MINIT_FUNCTION(http_encoding
)
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
;
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
);
1173 PHP_HTTP_REGISTER_CLASS(http
\\Encoding
\\Stream
, Deflate
, http_deflate_stream
, php_http_encoding_stream_class_entry
, 0);
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
);
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);
1199 * vim600: noet sw=4 ts=4 fdm=marker
1200 * vim<600: noet sw=4 ts=4