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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_ZLIB
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_ZLIB)
20 #include "php_http_api.h"
21 #include "php_http_encoding_api.h"
22 #include "php_http_exception_object.h"
23 #include "php_http_inflatestream_object.h"
25 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpInflateStream, method, 0, req_args)
26 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpInflateStream, method, 0)
27 #define HTTP_INFLATE_ME(method, visibility) PHP_ME(HttpInflateStream, method, HTTP_ARGS(HttpInflateStream, method), visibility)
29 HTTP_BEGIN_ARGS(update
, 1)
33 HTTP_BEGIN_ARGS(flush
, 0)
37 HTTP_BEGIN_ARGS(finish
, 0)
41 #define OBJ_PROP_CE http_inflatestream_object_ce
42 zend_class_entry
*http_inflatestream_object_ce
;
43 zend_function_entry http_inflatestream_object_fe
[] = {
44 HTTP_INFLATE_ME(update
, ZEND_ACC_PUBLIC
)
45 HTTP_INFLATE_ME(flush
, ZEND_ACC_PUBLIC
)
46 HTTP_INFLATE_ME(finish
, ZEND_ACC_PUBLIC
)
50 static zend_object_handlers http_inflatestream_object_handlers
;
52 PHP_MINIT_FUNCTION(http_inflatestream_object
)
54 HTTP_REGISTER_CLASS_EX(HttpInflateStream
, http_inflatestream_object
, NULL
, 0);
55 http_inflatestream_object_handlers
.clone_obj
= _http_inflatestream_object_clone_obj
;
59 zend_object_value
_http_inflatestream_object_new(zend_class_entry
*ce TSRMLS_DC
)
61 return http_inflatestream_object_new_ex(ce
, NULL
, NULL
);
64 zend_object_value
_http_inflatestream_object_new_ex(zend_class_entry
*ce
, http_encoding_stream
*s
, http_inflatestream_object
**ptr TSRMLS_DC
)
67 http_inflatestream_object
*o
;
69 o
= ecalloc(1, sizeof(http_inflatestream_object
));
80 ALLOC_HASHTABLE(OBJ_PROP(o
));
81 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
83 ov
.handle
= putObject(http_inflatestream_object
, o
);
84 ov
.handlers
= &http_inflatestream_object_handlers
;
89 zend_object_value
_http_inflatestream_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
91 http_encoding_stream
*s
;
92 getObject(http_inflatestream_object
, obj
);
94 s
= ecalloc(1, sizeof(http_encoding_stream
));
95 s
->flags
= obj
->stream
->flags
;
96 inflateCopy(&s
->stream
, &obj
->stream
->stream
);
97 s
->stream
.opaque
= phpstr_dup(s
->stream
.opaque
);
99 return http_inflatestream_object_new_ex(Z_OBJCE_P(this_ptr
), s
, NULL
);
102 void _http_inflatestream_object_free(zend_object
*object TSRMLS_DC
)
104 http_inflatestream_object
*o
= (http_inflatestream_object
*) object
;
107 zend_hash_destroy(OBJ_PROP(o
));
108 FREE_HASHTABLE(OBJ_PROP(o
));
111 http_encoding_inflate_stream_free(&o
->stream
);
116 /* {{{ proto string HttpInflateStream::update(string data)
118 * Passes more data through the inflate stream.
120 * Expects a string parameter containing (a part of) the data to inflate.
122 * Returns inflated data on success or FALSE on failure.
124 PHP_METHOD(HttpInflateStream
, update
)
127 size_t decoded_len
= 0;
128 char *data
, *decoded
= NULL
;
129 getObject(http_inflatestream_object
, obj
);
131 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
136 RETURN_STRING("", 1);
139 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
143 if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
144 RETURN_STRINGL(decoded
, decoded_len
, 0);
151 /* {{{ proto string HttpInflateStream::flush([string data])
153 * Flush the inflate stream.
155 * Returns some inflated data as string on success or FALSE on failure.
157 PHP_METHOD(HttpInflateStream
, flush
)
160 size_t decoded_len
= 0;
161 char *decoded
= NULL
, *data
= NULL
;
162 getObject(http_inflatestream_object
, obj
);
164 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
168 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
172 /* flushing the inflate stream is a no-op */
174 RETURN_STRINGL("", 0, 1);
175 } else if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
176 RETURN_STRINGL(decoded
, decoded_len
, 0);
183 /* {{{ proto string HttpInflateStream::finish([string data])
185 * Finalizes the inflate stream. The inflate stream can be reused after finalizing.
187 * Returns the final part of inflated data.
189 PHP_METHOD(HttpInflateStream
, finish
)
192 size_t updated_len
= 0, decoded_len
= 0;
193 char *updated
= NULL
, *decoded
= NULL
, *data
= NULL
;
194 getObject(http_inflatestream_object
, obj
);
196 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
200 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
205 if (SUCCESS
!= http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
210 if (SUCCESS
== http_encoding_inflate_stream_finish(obj
->stream
, &decoded
, &decoded_len
)) {
212 updated
= erealloc(updated
, updated_len
+ decoded_len
+ 1);
213 updated
[updated_len
+ decoded_len
] = '\0';
214 memcpy(updated
+ updated_len
, decoded
, decoded_len
);
216 updated_len
+= decoded_len
;
217 RETVAL_STRINGL(updated
, updated_len
, 0);
220 RETVAL_STRINGL(decoded
, decoded_len
, 0);
227 http_encoding_inflate_stream_dtor(obj
->stream
);
228 http_encoding_inflate_stream_init(obj
->stream
, obj
->stream
->flags
);
233 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
240 * vim600: noet sw=4 ts=4 fdm=marker
241 * vim<600: noet sw=4 ts=4