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 http_encoding_inflate_stream_free(&o
->stream
);
124 /* {{{ proto void HttpInflateStream::__construct([int flags = 0])
126 * Creates a new HttpInflateStream object instance.
128 * Accepts an optional int parameter specifying how to initialize the inflate stream.
130 PHP_METHOD(HttpInflateStream
, __construct
)
135 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &flags
)) {
136 getObject(http_inflatestream_object
, obj
);
139 obj
->stream
= http_encoding_inflate_stream_init(NULL
, flags
& 0x0fffffff);
141 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "HttpInflateStream cannot be initialized twice");
148 /* {{{ proto string HttpInflateStream::update(string data)
150 * Passes more data through the inflate stream.
152 * Expects a string parameter containing (a part of) the data to inflate.
154 * Returns inflated data on success or FALSE on failure.
156 PHP_METHOD(HttpInflateStream
, update
)
159 size_t decoded_len
= 0;
160 char *data
, *decoded
= NULL
;
161 getObject(http_inflatestream_object
, obj
);
163 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
168 RETURN_STRING("", 1);
171 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
175 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::flush([string data])
185 * Flush the inflate stream.
187 * Returns some inflated data as string on success or FALSE on failure.
189 PHP_METHOD(HttpInflateStream
, flush
)
192 size_t decoded_len
= 0;
193 char *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))) {
204 /* flushing the inflate stream is a no-op */
206 RETURN_STRINGL("", 0, 1);
207 } else if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
208 RETURN_STRINGL(decoded
, decoded_len
, 0);
215 /* {{{ proto string HttpInflateStream::finish([string data])
217 * Finalizes the inflate stream. The inflate stream can be reused after finalizing.
219 * Returns the final part of inflated data.
221 PHP_METHOD(HttpInflateStream
, finish
)
224 size_t updated_len
= 0, decoded_len
= 0;
225 char *updated
= NULL
, *decoded
= NULL
, *data
= NULL
;
226 getObject(http_inflatestream_object
, obj
);
228 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
232 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
237 if (SUCCESS
!= http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
242 if (SUCCESS
== http_encoding_inflate_stream_finish(obj
->stream
, &decoded
, &decoded_len
)) {
244 updated
= erealloc(updated
, updated_len
+ decoded_len
+ 1);
245 updated
[updated_len
+ decoded_len
] = '\0';
246 memcpy(updated
+ updated_len
, decoded
, decoded_len
);
248 updated_len
+= decoded_len
;
249 RETVAL_STRINGL(updated
, updated_len
, 0);
252 RETVAL_STRINGL(decoded
, decoded_len
, 0);
259 http_encoding_inflate_stream_dtor(obj
->stream
);
260 http_encoding_inflate_stream_init(obj
->stream
, obj
->stream
->flags
);
265 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
272 * vim600: noet sw=4 ts=4 fdm=marker
273 * vim<600: noet sw=4 ts=4