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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
20 #define HTTP_WANT_ZLIB
23 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_ZLIB)
25 #include "php_http_api.h"
26 #include "php_http_encoding_api.h"
27 #include "php_http_exception_object.h"
28 #include "php_http_deflatestream_object.h"
30 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpDeflateStream, method, ret_ref, req_args)
31 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpDeflateStream, method, ret_ref)
32 #define HTTP_DEFLATE_ME(method, visibility) PHP_ME(HttpDeflateStream, method, HTTP_ARGS(HttpDeflateStream, method), visibility)
34 HTTP_BEGIN_ARGS(__construct
, 0, 0)
35 HTTP_ARG_VAL(flags
, 0)
38 HTTP_BEGIN_ARGS(update
, 0, 1)
42 HTTP_BEGIN_ARGS(flush
, 0, 0)
46 HTTP_BEGIN_ARGS(finish
, 0, 0)
50 #define http_deflatestream_object_declare_default_properties() _http_deflatestream_object_declare_default_properties(TSRMLS_C)
51 static inline void _http_deflatestream_object_declare_default_properties(TSRMLS_D
);
53 zend_class_entry
*http_deflatestream_object_ce
;
54 zend_function_entry http_deflatestream_object_fe
[] = {
55 HTTP_DEFLATE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
56 HTTP_DEFLATE_ME(update
, ZEND_ACC_PUBLIC
)
57 HTTP_DEFLATE_ME(flush
, ZEND_ACC_PUBLIC
)
58 HTTP_DEFLATE_ME(finish
, ZEND_ACC_PUBLIC
)
62 static zend_object_handlers http_deflatestream_object_handlers
;
64 PHP_MINIT_FUNCTION(http_deflatestream_object
)
66 HTTP_REGISTER_CLASS_EX(HttpDeflateStream
, http_deflatestream_object
, NULL
, 0);
67 http_deflatestream_object_handlers
.clone_obj
= _http_deflatestream_object_clone_obj
;
71 zend_object_value
_http_deflatestream_object_new(zend_class_entry
*ce TSRMLS_DC
)
73 return http_deflatestream_object_new_ex(ce
, NULL
, NULL
);
76 zend_object_value
_http_deflatestream_object_new_ex(zend_class_entry
*ce
, http_encoding_stream
*s
, http_deflatestream_object
**ptr TSRMLS_DC
)
79 http_deflatestream_object
*o
;
81 o
= ecalloc(1, sizeof(http_deflatestream_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_deflatestream_object
, o
);
96 ov
.handlers
= &http_deflatestream_object_handlers
;
101 zend_object_value
_http_deflatestream_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
103 http_encoding_stream
*s
;
104 getObject(http_deflatestream_object
, obj
);
106 s
= ecalloc(1, sizeof(http_encoding_stream
));
107 s
->flags
= obj
->stream
->flags
;
108 deflateCopy(&s
->stream
, &obj
->stream
->stream
);
109 s
->stream
.opaque
= phpstr_dup(s
->stream
.opaque
);
111 return http_deflatestream_object_new_ex(Z_OBJCE_P(this_ptr
), s
, NULL
);
114 static inline void _http_deflatestream_object_declare_default_properties(TSRMLS_D
)
116 zend_class_entry
*ce
= http_deflatestream_object_ce
;
119 DCL_CONST(long, "TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP
);
120 DCL_CONST(long, "TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB
);
121 DCL_CONST(long, "TYPE_RAW", HTTP_DEFLATE_TYPE_RAW
);
122 DCL_CONST(long, "LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF
);
123 DCL_CONST(long, "LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN
);
124 DCL_CONST(long, "LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX
);
125 DCL_CONST(long, "STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF
);
126 DCL_CONST(long, "STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT
);
127 DCL_CONST(long, "STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF
);
128 DCL_CONST(long, "STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE
);
129 DCL_CONST(long, "STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED
);
133 void _http_deflatestream_object_free(zend_object
*object TSRMLS_DC
)
135 http_deflatestream_object
*o
= (http_deflatestream_object
*) object
;
138 zend_hash_destroy(OBJ_PROP(o
));
139 FREE_HASHTABLE(OBJ_PROP(o
));
142 http_encoding_deflate_stream_free(&o
->stream
);
147 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
149 * Creates a new HttpDeflateStream object instance.
151 * Accepts an optional int parameter specifying how to initialize the deflate stream.
153 PHP_METHOD(HttpDeflateStream
, __construct
)
158 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &flags
)) {
159 getObject(http_deflatestream_object
, obj
);
162 obj
->stream
= http_encoding_deflate_stream_init(NULL
, flags
);
164 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "HttpDeflateStream cannot be initialized twice");
171 /* {{{ proto string HttpDeflateStream::update(string data)
173 * Passes more data through the deflate stream.
175 * Expects a string parameter containing (a part of) the data to deflate.
177 * Returns deflated data on success or FALSE on failure.
179 PHP_METHOD(HttpDeflateStream
, update
)
182 size_t encoded_len
= 0;
183 char *data
, *encoded
= NULL
;
184 getObject(http_deflatestream_object
, obj
);
186 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
190 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
194 if (SUCCESS
== http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &encoded
, &encoded_len
)) {
195 RETURN_STRINGL(encoded
, encoded_len
, 0);
202 /* {{{ proto string HttpDeflateStream::flush([string data])
204 * Flushes the deflate stream.
206 * Returns some deflated data as string on success or FALSE on failure.
208 PHP_METHOD(HttpDeflateStream
, flush
)
211 size_t updated_len
= 0, encoded_len
= 0;
212 char *updated
= NULL
, *encoded
= NULL
, *data
= NULL
;
213 getObject(http_deflatestream_object
, obj
);
215 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
219 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
224 if (SUCCESS
!= http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
229 if (SUCCESS
== http_encoding_deflate_stream_flush(obj
->stream
, &encoded
, &encoded_len
)) {
231 updated
= erealloc(updated
, updated_len
+ encoded_len
+ 1);
232 updated
[updated_len
+ encoded_len
] = '\0';
233 memcpy(updated
+ updated_len
, encoded
, encoded_len
);
235 updated_len
+= encoded_len
;
236 RETURN_STRINGL(updated
, updated_len
, 0);
238 RETVAL_STRINGL(encoded
, encoded_len
, 0);
247 /* {{{ proto string HttpDeflateStream::finish([string data])
249 * Finalizes the deflate stream. The deflate stream can be reused after finalizing.
251 * Returns the final part of deflated data.
253 PHP_METHOD(HttpDeflateStream
, finish
)
256 size_t updated_len
= 0, encoded_len
= 0;
257 char *updated
= NULL
, *encoded
= NULL
, *data
= NULL
;
258 getObject(http_deflatestream_object
, obj
);
260 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
264 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
269 if (SUCCESS
!= http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
274 if (SUCCESS
== http_encoding_deflate_stream_finish(obj
->stream
, &encoded
, &encoded_len
)) {
276 updated
= erealloc(updated
, updated_len
+ encoded_len
+ 1);
277 updated
[updated_len
+ encoded_len
] = '\0';
278 memcpy(updated
+ updated_len
, encoded
, encoded_len
);
280 updated_len
+= encoded_len
;
281 RETVAL_STRINGL(updated
, updated_len
, 0);
284 RETVAL_STRINGL(encoded
, encoded_len
, 0);
291 http_encoding_deflate_stream_dtor(obj
->stream
);
292 http_encoding_deflate_stream_init(obj
->stream
, obj
->stream
->flags
);
297 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
304 * vim600: noet sw=4 ts=4 fdm=marker
305 * vim<600: noet sw=4 ts=4