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(__construct
, 0)
30 HTTP_ARG_VAL(flags
, 0)
33 HTTP_BEGIN_ARGS(update
, 1)
37 HTTP_BEGIN_ARGS(flush
, 0)
41 HTTP_BEGIN_ARGS(finish
, 0)
45 #define OBJ_PROP_CE http_inflatestream_object_ce
46 zend_class_entry
*http_inflatestream_object_ce
;
47 zend_function_entry http_inflatestream_object_fe
[] = {
48 HTTP_INFLATE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
49 HTTP_INFLATE_ME(update
, ZEND_ACC_PUBLIC
)
50 HTTP_INFLATE_ME(flush
, ZEND_ACC_PUBLIC
)
51 HTTP_INFLATE_ME(finish
, ZEND_ACC_PUBLIC
)
55 static zend_object_handlers http_inflatestream_object_handlers
;
57 PHP_MINIT_FUNCTION(http_inflatestream_object
)
59 HTTP_REGISTER_CLASS_EX(HttpInflateStream
, http_inflatestream_object
, NULL
, 0);
60 http_inflatestream_object_handlers
.clone_obj
= _http_inflatestream_object_clone_obj
;
63 DCL_CONST(long, "FLUSH_NONE", HTTP_ENCODING_STREAM_FLUSH_NONE
);
64 DCL_CONST(long, "FLUSH_SYNC", HTTP_ENCODING_STREAM_FLUSH_SYNC
);
65 DCL_CONST(long, "FLUSH_FULL", HTTP_ENCODING_STREAM_FLUSH_FULL
);
71 zend_object_value
_http_inflatestream_object_new(zend_class_entry
*ce TSRMLS_DC
)
73 return http_inflatestream_object_new_ex(ce
, NULL
, NULL
);
76 zend_object_value
_http_inflatestream_object_new_ex(zend_class_entry
*ce
, http_encoding_stream
*s
, http_inflatestream_object
**ptr TSRMLS_DC
)
79 http_inflatestream_object
*o
;
81 o
= ecalloc(1, sizeof(http_inflatestream_object
));
92 ALLOC_HASHTABLE(OBJ_PROP(o
));
93 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
95 ov
.handle
= putObject(http_inflatestream_object
, o
);
96 ov
.handlers
= &http_inflatestream_object_handlers
;
101 zend_object_value
_http_inflatestream_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
103 http_encoding_stream
*s
;
104 getObject(http_inflatestream_object
, obj
);
106 s
= ecalloc(1, sizeof(http_encoding_stream
));
107 s
->flags
= obj
->stream
->flags
;
108 inflateCopy(&s
->stream
, &obj
->stream
->stream
);
109 s
->stream
.opaque
= phpstr_dup(s
->stream
.opaque
);
111 return http_inflatestream_object_new_ex(Z_OBJCE_P(this_ptr
), s
, NULL
);
114 void _http_inflatestream_object_free(zend_object
*object TSRMLS_DC
)
116 http_inflatestream_object
*o
= (http_inflatestream_object
*) object
;
119 zend_hash_destroy(OBJ_PROP(o
));
120 FREE_HASHTABLE(OBJ_PROP(o
));
123 http_encoding_inflate_stream_free(&o
->stream
);
128 /* {{{ proto void HttpInflateStream::__construct([int flags = 0])
130 * Creates a new HttpInflateStream object instance.
132 * Accepts an optional int parameter specifying how to initialize the inflate stream.
134 PHP_METHOD(HttpInflateStream
, __construct
)
139 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &flags
)) {
140 getObject(http_inflatestream_object
, obj
);
143 obj
->stream
= http_encoding_inflate_stream_init(NULL
, flags
& 0x0fffffff);
145 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "HttpInflateStream cannot be initialized twice");
152 /* {{{ proto string HttpInflateStream::update(string data)
154 * Passes more data through the inflate stream.
156 * Expects a string parameter containing (a part of) the data to inflate.
158 * Returns inflated data on success or FALSE on failure.
160 PHP_METHOD(HttpInflateStream
, update
)
163 size_t decoded_len
= 0;
164 char *data
, *decoded
= NULL
;
165 getObject(http_inflatestream_object
, obj
);
167 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
172 RETURN_STRING("", 1);
175 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
179 if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
180 RETURN_STRINGL(decoded
, decoded_len
, 0);
187 /* {{{ proto string HttpInflateStream::flush([string data])
189 * Flush the inflate stream.
191 * Returns some inflated data as string on success or FALSE on failure.
193 PHP_METHOD(HttpInflateStream
, flush
)
196 size_t decoded_len
= 0;
197 char *decoded
= NULL
, *data
= NULL
;
198 getObject(http_inflatestream_object
, obj
);
200 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
204 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
208 /* flushing the inflate stream is a no-op */
210 RETURN_STRINGL("", 0, 1);
211 } else if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
212 RETURN_STRINGL(decoded
, decoded_len
, 0);
219 /* {{{ proto string HttpInflateStream::finish([string data])
221 * Finalizes the inflate stream. The inflate stream can be reused after finalizing.
223 * Returns the final part of inflated data.
225 PHP_METHOD(HttpInflateStream
, finish
)
228 size_t updated_len
= 0, decoded_len
= 0;
229 char *updated
= NULL
, *decoded
= NULL
, *data
= NULL
;
230 getObject(http_inflatestream_object
, obj
);
232 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
236 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
241 if (SUCCESS
!= http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
246 if (SUCCESS
== http_encoding_inflate_stream_finish(obj
->stream
, &decoded
, &decoded_len
)) {
248 updated
= erealloc(updated
, updated_len
+ decoded_len
+ 1);
249 updated
[updated_len
+ decoded_len
] = '\0';
250 memcpy(updated
+ updated_len
, decoded
, decoded_len
);
252 updated_len
+= decoded_len
;
253 RETVAL_STRINGL(updated
, updated_len
, 0);
256 RETVAL_STRINGL(decoded
, decoded_len
, 0);
263 http_encoding_inflate_stream_dtor(obj
->stream
);
264 http_encoding_inflate_stream_init(obj
->stream
, obj
->stream
->flags
);
269 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
276 * vim600: noet sw=4 ts=4 fdm=marker
277 * vim<600: noet sw=4 ts=4