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_deflatestream_object.h"
25 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpDeflateStream, method, 0, req_args)
26 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpDeflateStream, method, 0)
27 #define HTTP_DEFLATE_ME(method, visibility) PHP_ME(HttpDeflateStream, method, HTTP_ARGS(HttpDeflateStream, 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_deflatestream_object_ce
46 zend_class_entry
*http_deflatestream_object_ce
;
47 zend_function_entry http_deflatestream_object_fe
[] = {
48 HTTP_DEFLATE_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
)
49 HTTP_DEFLATE_ME(update
, ZEND_ACC_PUBLIC
)
50 HTTP_DEFLATE_ME(flush
, ZEND_ACC_PUBLIC
)
51 HTTP_DEFLATE_ME(finish
, ZEND_ACC_PUBLIC
)
55 static zend_object_handlers http_deflatestream_object_handlers
;
57 PHP_MINIT_FUNCTION(http_deflatestream_object
)
59 HTTP_REGISTER_CLASS_EX(HttpDeflateStream
, http_deflatestream_object
, NULL
, 0);
60 http_deflatestream_object_handlers
.clone_obj
= _http_deflatestream_object_clone_obj
;
63 DCL_CONST(long, "TYPE_GZIP", HTTP_DEFLATE_TYPE_GZIP
);
64 DCL_CONST(long, "TYPE_ZLIB", HTTP_DEFLATE_TYPE_ZLIB
);
65 DCL_CONST(long, "TYPE_RAW", HTTP_DEFLATE_TYPE_RAW
);
66 DCL_CONST(long, "LEVEL_DEF", HTTP_DEFLATE_LEVEL_DEF
);
67 DCL_CONST(long, "LEVEL_MIN", HTTP_DEFLATE_LEVEL_MIN
);
68 DCL_CONST(long, "LEVEL_MAX", HTTP_DEFLATE_LEVEL_MAX
);
69 DCL_CONST(long, "STRATEGY_DEF", HTTP_DEFLATE_STRATEGY_DEF
);
70 DCL_CONST(long, "STRATEGY_FILT", HTTP_DEFLATE_STRATEGY_FILT
);
71 DCL_CONST(long, "STRATEGY_HUFF", HTTP_DEFLATE_STRATEGY_HUFF
);
72 DCL_CONST(long, "STRATEGY_RLE", HTTP_DEFLATE_STRATEGY_RLE
);
73 DCL_CONST(long, "STRATEGY_FIXED", HTTP_DEFLATE_STRATEGY_FIXED
);
74 DCL_CONST(long, "FLUSH_NONE", HTTP_ENCODING_STREAM_FLUSH_NONE
);
75 DCL_CONST(long, "FLUSH_SYNC", HTTP_ENCODING_STREAM_FLUSH_SYNC
);
76 DCL_CONST(long, "FLUSH_FULL", HTTP_ENCODING_STREAM_FLUSH_FULL
);
82 zend_object_value
_http_deflatestream_object_new(zend_class_entry
*ce TSRMLS_DC
)
84 return http_deflatestream_object_new_ex(ce
, NULL
, NULL
);
87 zend_object_value
_http_deflatestream_object_new_ex(zend_class_entry
*ce
, http_encoding_stream
*s
, http_deflatestream_object
**ptr TSRMLS_DC
)
90 http_deflatestream_object
*o
;
92 o
= ecalloc(1, sizeof(http_deflatestream_object
));
103 ALLOC_HASHTABLE(OBJ_PROP(o
));
104 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
106 ov
.handle
= putObject(http_deflatestream_object
, o
);
107 ov
.handlers
= &http_deflatestream_object_handlers
;
112 zend_object_value
_http_deflatestream_object_clone_obj(zval
*this_ptr TSRMLS_DC
)
114 http_encoding_stream
*s
;
115 getObject(http_deflatestream_object
, obj
);
117 s
= ecalloc(1, sizeof(http_encoding_stream
));
118 s
->flags
= obj
->stream
->flags
;
119 deflateCopy(&s
->stream
, &obj
->stream
->stream
);
120 s
->stream
.opaque
= phpstr_dup(s
->stream
.opaque
);
122 return http_deflatestream_object_new_ex(Z_OBJCE_P(this_ptr
), s
, NULL
);
125 void _http_deflatestream_object_free(zend_object
*object TSRMLS_DC
)
127 http_deflatestream_object
*o
= (http_deflatestream_object
*) object
;
130 zend_hash_destroy(OBJ_PROP(o
));
131 FREE_HASHTABLE(OBJ_PROP(o
));
134 http_encoding_deflate_stream_free(&o
->stream
);
139 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
141 * Creates a new HttpDeflateStream object instance.
143 * Accepts an optional int parameter specifying how to initialize the deflate stream.
145 PHP_METHOD(HttpDeflateStream
, __construct
)
150 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|l", &flags
)) {
151 getObject(http_deflatestream_object
, obj
);
154 obj
->stream
= http_encoding_deflate_stream_init(NULL
, flags
& 0x0fffffff);
156 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "HttpDeflateStream cannot be initialized twice");
163 /* {{{ proto string HttpDeflateStream::update(string data)
165 * Passes more data through the deflate stream.
167 * Expects a string parameter containing (a part of) the data to deflate.
169 * Returns deflated data on success or FALSE on failure.
171 PHP_METHOD(HttpDeflateStream
, update
)
174 size_t encoded_len
= 0;
175 char *data
, *encoded
= NULL
;
176 getObject(http_deflatestream_object
, obj
);
178 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &data
, &data_len
)) {
182 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
186 if (SUCCESS
== http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &encoded
, &encoded_len
)) {
187 RETURN_STRINGL(encoded
, encoded_len
, 0);
194 /* {{{ proto string HttpDeflateStream::flush([string data])
196 * Flushes the deflate stream.
198 * Returns some deflated data as string on success or FALSE on failure.
200 PHP_METHOD(HttpDeflateStream
, flush
)
203 size_t updated_len
= 0, encoded_len
= 0;
204 char *updated
= NULL
, *encoded
= NULL
, *data
= NULL
;
205 getObject(http_deflatestream_object
, obj
);
207 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
211 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
216 if (SUCCESS
!= http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
221 if (SUCCESS
== http_encoding_deflate_stream_flush(obj
->stream
, &encoded
, &encoded_len
)) {
223 updated
= erealloc(updated
, updated_len
+ encoded_len
+ 1);
224 updated
[updated_len
+ encoded_len
] = '\0';
225 memcpy(updated
+ updated_len
, encoded
, encoded_len
);
227 updated_len
+= encoded_len
;
228 RETURN_STRINGL(updated
, updated_len
, 0);
230 RETVAL_STRINGL(encoded
, encoded_len
, 0);
239 /* {{{ proto string HttpDeflateStream::finish([string data])
241 * Finalizes the deflate stream. The deflate stream can be reused after finalizing.
243 * Returns the final part of deflated data.
245 PHP_METHOD(HttpDeflateStream
, finish
)
248 size_t updated_len
= 0, encoded_len
= 0;
249 char *updated
= NULL
, *encoded
= NULL
, *data
= NULL
;
250 getObject(http_deflatestream_object
, obj
);
252 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|s", &data
, &data_len
)) {
256 if (!obj
->stream
&& !(obj
->stream
= http_encoding_deflate_stream_init(NULL
, 0))) {
261 if (SUCCESS
!= http_encoding_deflate_stream_update(obj
->stream
, data
, data_len
, &updated
, &updated_len
)) {
266 if (SUCCESS
== http_encoding_deflate_stream_finish(obj
->stream
, &encoded
, &encoded_len
)) {
268 updated
= erealloc(updated
, updated_len
+ encoded_len
+ 1);
269 updated
[updated_len
+ encoded_len
] = '\0';
270 memcpy(updated
+ updated_len
, encoded
, encoded_len
);
272 updated_len
+= encoded_len
;
273 RETVAL_STRINGL(updated
, updated_len
, 0);
276 RETVAL_STRINGL(encoded
, encoded_len
, 0);
283 http_encoding_deflate_stream_dtor(obj
->stream
);
284 http_encoding_deflate_stream_init(obj
->stream
, obj
->stream
->flags
);
289 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
296 * vim600: noet sw=4 ts=4 fdm=marker
297 * vim<600: noet sw=4 ts=4