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
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
94 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
96 ov
.handle
= putObject(http_inflatestream_object
, o
);
97 ov
.handlers
= &http_inflatestream_object_handlers
;
102 zend_object_value
_http_inflatestream_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
104 http_encoding_stream
*s
;
105 zend_object_value new_ov
;
106 http_inflatestream_object
*new_obj
= NULL
;
107 getObject(http_inflatestream_object
, old_obj
);
109 s
= ecalloc(1, sizeof(http_encoding_stream
));
110 s
->flags
= old_obj
->stream
->flags
;
111 inflateCopy(&s
->stream
, &old_obj
->stream
->stream
);
112 s
->stream
.opaque
= phpstr_dup(s
->stream
.opaque
);
114 new_ov
= http_inflatestream_object_new_ex(old_obj
->zo
.ce
, s
, &new_obj
);
115 zend_objects_clone_members(&new_obj
->zo
, new_ov
, &old_obj
->zo
, Z_OBJ_HANDLE_P(this_ptr
) TSRMLS_CC
);
120 void _http_inflatestream_object_free(zend_object
*object TSRMLS_DC
)
122 http_inflatestream_object
*o
= (http_inflatestream_object
*) object
;
125 http_encoding_inflate_stream_free(&o
->stream
);
130 /* {{{ proto void HttpInflateStream::__construct([int flags = 0])
132 * Creates a new HttpInflateStream object instance.
134 * Accepts an optional int parameter specifying how to initialize the inflate stream.
136 PHP_METHOD(HttpInflateStream
, __construct
)
141 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &flags
)) {
142 getObject(http_inflatestream_object
, obj
);
145 obj
->stream
= http_encoding_inflate_stream_init(NULL
, flags
& 0x0fffffff);
147 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "HttpInflateStream cannot be initialized twice");
154 /* {{{ proto string HttpInflateStream::update(string data)
156 * Passes more data through the inflate stream.
158 * Expects a string parameter containing (a part of) the data to inflate.
160 * Returns inflated data on success or FALSE on failure.
162 PHP_METHOD(HttpInflateStream
, update
)
165 size_t decoded_len
= 0;
166 char *data
, *decoded
= NULL
;
167 getObject(http_inflatestream_object
, obj
);
169 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
174 RETURN_STRING("", 1);
177 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
181 if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
182 RETURN_STRINGL(decoded
, decoded_len
, 0);
189 /* {{{ proto string HttpInflateStream::flush([string data])
191 * Flush the inflate stream.
193 * Returns some inflated data as string on success or FALSE on failure.
195 PHP_METHOD(HttpInflateStream
, flush
)
198 size_t decoded_len
= 0;
199 char *decoded
= NULL
, *data
= NULL
;
200 getObject(http_inflatestream_object
, obj
);
202 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
206 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
210 /* flushing the inflate stream is a no-op */
212 RETURN_STRINGL("", 0, 1);
213 } else if (SUCCESS
== http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &decoded
, &decoded_len
)) {
214 RETURN_STRINGL(decoded
, decoded_len
, 0);
221 /* {{{ proto string HttpInflateStream::finish([string data])
223 * Finalizes the inflate stream. The inflate stream can be reused after finalizing.
225 * Returns the final part of inflated data.
227 PHP_METHOD(HttpInflateStream
, finish
)
230 size_t updated_len
= 0, decoded_len
= 0;
231 char *updated
= NULL
, *decoded
= NULL
, *data
= NULL
;
232 getObject(http_inflatestream_object
, obj
);
234 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
238 if (!obj
->stream
&& !(obj
->stream
= http_encoding_inflate_stream_init(NULL
, 0))) {
243 if (SUCCESS
!= http_encoding_inflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
248 if (SUCCESS
== http_encoding_inflate_stream_finish(obj
->stream
, &decoded
, &decoded_len
)) {
250 updated
= erealloc(updated
, updated_len
+ decoded_len
+ 1);
251 updated
[updated_len
+ decoded_len
] = '\0';
252 memcpy(updated
+ updated_len
, decoded
, decoded_len
);
254 updated_len
+= decoded_len
;
255 RETVAL_STRINGL(updated
, updated_len
, 0);
258 RETVAL_STRINGL(decoded
, decoded_len
, 0);
265 http_encoding_inflate_stream_dtor(obj
->stream
);
266 http_encoding_inflate_stream_init(obj
->stream
, obj
->stream
->flags
);
271 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
278 * vim600: noet sw=4 ts=4 fdm=marker
279 * vim<600: noet sw=4 ts=4