fix php4 build
[m6w6/ext-http] / http_deflatestream_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_ZLIB
16 #include "php_http.h"
17
18 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_ZLIB)
19
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"
24
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)
28
29 HTTP_BEGIN_ARGS(__construct, 0)
30 HTTP_ARG_VAL(flags, 0)
31 HTTP_END_ARGS;
32
33 HTTP_BEGIN_ARGS(factory, 0)
34 HTTP_ARG_VAL(flags, 0)
35 HTTP_ARG_VAL(class_name, 0)
36 HTTP_END_ARGS;
37
38 HTTP_BEGIN_ARGS(update, 1)
39 HTTP_ARG_VAL(data, 0)
40 HTTP_END_ARGS;
41
42 HTTP_BEGIN_ARGS(flush, 0)
43 HTTP_ARG_VAL(data, 0)
44 HTTP_END_ARGS;
45
46 HTTP_BEGIN_ARGS(finish, 0)
47 HTTP_ARG_VAL(data, 0)
48 HTTP_END_ARGS;
49
50 #define THIS_CE http_deflatestream_object_ce
51 zend_class_entry *http_deflatestream_object_ce;
52 zend_function_entry http_deflatestream_object_fe[] = {
53 HTTP_DEFLATE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
54 HTTP_DEFLATE_ME(update, ZEND_ACC_PUBLIC)
55 HTTP_DEFLATE_ME(flush, ZEND_ACC_PUBLIC)
56 HTTP_DEFLATE_ME(finish, ZEND_ACC_PUBLIC)
57
58 HTTP_DEFLATE_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
59
60 EMPTY_FUNCTION_ENTRY
61 };
62 static zend_object_handlers http_deflatestream_object_handlers;
63
64 PHP_MINIT_FUNCTION(http_deflatestream_object)
65 {
66 HTTP_REGISTER_CLASS_EX(HttpDeflateStream, http_deflatestream_object, NULL, 0);
67 http_deflatestream_object_handlers.clone_obj = _http_deflatestream_object_clone_obj;
68
69 #ifndef WONKY
70 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_GZIP")-1, HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC);
71 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_ZLIB")-1, HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC);
72 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_RAW")-1, HTTP_DEFLATE_TYPE_RAW TSRMLS_CC);
73 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("LEVEL_DEF")-1, HTTP_DEFLATE_LEVEL_DEF TSRMLS_CC);
74 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("LEVEL_MIN")-1, HTTP_DEFLATE_LEVEL_MIN TSRMLS_CC);
75 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("LEVEL_MAX")-1, HTTP_DEFLATE_LEVEL_MAX TSRMLS_CC);
76 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("STRATEGY_DEF")-1, HTTP_DEFLATE_STRATEGY_DEF TSRMLS_CC);
77 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("STRATEGY_FILT")-1, HTTP_DEFLATE_STRATEGY_FILT TSRMLS_CC);
78 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("STRATEGY_HUFF")-1, HTTP_DEFLATE_STRATEGY_HUFF TSRMLS_CC);
79 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("STRATEGY_RLE")-1, HTTP_DEFLATE_STRATEGY_RLE TSRMLS_CC);
80 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("STRATEGY_FIXED")-1, HTTP_DEFLATE_STRATEGY_FIXED TSRMLS_CC);
81 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("FLUSH_NONE")-1, HTTP_ENCODING_STREAM_FLUSH_NONE TSRMLS_CC);
82 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("FLUSH_SYNC")-1, HTTP_ENCODING_STREAM_FLUSH_SYNC TSRMLS_CC);
83 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("FLUSH_FULL")-1, HTTP_ENCODING_STREAM_FLUSH_FULL TSRMLS_CC);
84 #endif
85
86 return SUCCESS;
87 }
88
89 zend_object_value _http_deflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
90 {
91 return http_deflatestream_object_new_ex(ce, NULL, NULL);
92 }
93
94 zend_object_value _http_deflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_deflatestream_object **ptr TSRMLS_DC)
95 {
96 zend_object_value ov;
97 http_deflatestream_object *o;
98
99 o = ecalloc(1, sizeof(http_deflatestream_object));
100 o->zo.ce = ce;
101
102 if (ptr) {
103 *ptr = o;
104 }
105
106 if (s) {
107 o->stream = s;
108 }
109
110 ALLOC_HASHTABLE(OBJ_PROP(o));
111 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
112 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
113
114 ov.handle = putObject(http_deflatestream_object, o);
115 ov.handlers = &http_deflatestream_object_handlers;
116
117 return ov;
118 }
119
120 zend_object_value _http_deflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
121 {
122 http_encoding_stream *s;
123 zend_object_value new_ov;
124 http_deflatestream_object *new_obj = NULL;
125 getObject(http_deflatestream_object, old_obj);
126
127 s = ecalloc(1, sizeof(http_encoding_stream));
128 s->flags = old_obj->stream->flags;
129 deflateCopy(&s->stream, &old_obj->stream->stream);
130 s->stream.opaque = phpstr_dup(s->stream.opaque);
131
132 new_ov = http_deflatestream_object_new_ex(old_obj->zo.ce, s, &new_obj);
133 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
134
135 return new_ov;
136 }
137
138 void _http_deflatestream_object_free(zend_object *object TSRMLS_DC)
139 {
140 http_deflatestream_object *o = (http_deflatestream_object *) object;
141
142 if (o->stream) {
143 http_encoding_deflate_stream_free(&o->stream);
144 }
145 freeObject(o);
146 }
147
148 /* {{{ proto void HttpDeflateStream::__construct([int flags = 0])
149 Creates a new HttpDeflateStream object instance. */
150 PHP_METHOD(HttpDeflateStream, __construct)
151 {
152 long flags = 0;
153
154 SET_EH_THROW_HTTP();
155 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
156 getObject(http_deflatestream_object, obj);
157
158 if (!obj->stream) {
159 obj->stream = http_encoding_deflate_stream_init(NULL, flags & 0x0fffffff);
160 } else {
161 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpDeflateStream cannot be initialized twice");
162 }
163 }
164 SET_EH_NORMAL();
165 }
166 /* }}} */
167
168 /* {{{ proto HttpDeflateStream HttpDeflateStream::factory([int flags[, string class = "HttpDeflateStream"]])
169 Creates a new HttpDeflateStream object instance. */
170 PHP_METHOD(HttpDeflateStream, factory)
171 {
172 long flags = 0;
173 char *cn = NULL;
174 int cl = 0;
175
176 SET_EH_THROW_HTTP();
177 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &flags, &cn, &cl)) {
178 zend_object_value ov;
179 http_encoding_stream *s = http_encoding_deflate_stream_init(NULL, flags & 0x0fffffff);
180
181 if (SUCCESS == http_object_new(&ov, cn, cl, _http_deflatestream_object_new_ex, http_deflatestream_object_ce, s, NULL)) {
182 RETVAL_OBJVAL(ov, 0);
183 }
184 }
185 SET_EH_NORMAL();
186 }
187 /* }}} */
188
189 /* {{{ proto string HttpDeflateStream::update(string data)
190 Passes more data through the deflate stream. */
191 PHP_METHOD(HttpDeflateStream, update)
192 {
193 int data_len;
194 size_t encoded_len = 0;
195 char *data, *encoded = NULL;
196 getObject(http_deflatestream_object, obj);
197
198 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
199 RETURN_FALSE;
200 }
201
202 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
203 RETURN_FALSE;
204 }
205
206 if (SUCCESS == http_encoding_deflate_stream_update(obj->stream, data, data_len, &encoded, &encoded_len)) {
207 RETURN_STRINGL(encoded, encoded_len, 0);
208 } else {
209 RETURN_FALSE;
210 }
211 }
212 /* }}} */
213
214 /* {{{ proto string HttpDeflateStream::flush([string data])
215 Flushes the deflate stream. */
216 PHP_METHOD(HttpDeflateStream, flush)
217 {
218 int data_len = 0;
219 size_t updated_len = 0, encoded_len = 0;
220 char *updated = NULL, *encoded = NULL, *data = NULL;
221 getObject(http_deflatestream_object, obj);
222
223 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
224 RETURN_FALSE;
225 }
226
227 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
228 RETURN_FALSE;
229 }
230
231 if (data_len) {
232 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
233 RETURN_FALSE;
234 }
235 }
236
237 if (SUCCESS == http_encoding_deflate_stream_flush(obj->stream, &encoded, &encoded_len)) {
238 if (updated_len) {
239 updated = erealloc(updated, updated_len + encoded_len + 1);
240 updated[updated_len + encoded_len] = '\0';
241 memcpy(updated + updated_len, encoded, encoded_len);
242 STR_FREE(encoded);
243 updated_len += encoded_len;
244 RETURN_STRINGL(updated, updated_len, 0);
245 } else if (encoded) {
246 RETVAL_STRINGL(encoded, encoded_len, 0);
247 } else {
248 RETVAL_NULL();
249 }
250 } else {
251 RETVAL_FALSE;
252 }
253 STR_FREE(updated);
254 }
255 /* }}} */
256
257 /* {{{ proto string HttpDeflateStream::finish([string data])
258 Finalizes the deflate stream. The deflate stream can be reused after finalizing. */
259 PHP_METHOD(HttpDeflateStream, finish)
260 {
261 int data_len = 0;
262 size_t updated_len = 0, encoded_len = 0;
263 char *updated = NULL, *encoded = NULL, *data = NULL;
264 getObject(http_deflatestream_object, obj);
265
266 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
267 RETURN_FALSE;
268 }
269
270 if (!obj->stream && !(obj->stream = http_encoding_deflate_stream_init(NULL, 0))) {
271 RETURN_FALSE;
272 }
273
274 if (data_len) {
275 if (SUCCESS != http_encoding_deflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
276 RETURN_FALSE;
277 }
278 }
279
280 if (SUCCESS == http_encoding_deflate_stream_finish(obj->stream, &encoded, &encoded_len)) {
281 if (updated_len) {
282 updated = erealloc(updated, updated_len + encoded_len + 1);
283 updated[updated_len + encoded_len] = '\0';
284 memcpy(updated + updated_len, encoded, encoded_len);
285 STR_FREE(encoded);
286 updated_len += encoded_len;
287 RETVAL_STRINGL(updated, updated_len, 0);
288 } else {
289 STR_FREE(updated);
290 RETVAL_STRINGL(encoded, encoded_len, 0);
291 }
292 } else {
293 STR_FREE(updated);
294 RETVAL_FALSE;
295 }
296
297 http_encoding_deflate_stream_dtor(obj->stream);
298 http_encoding_deflate_stream_init(obj->stream, obj->stream->flags);
299 }
300 /* }}} */
301
302 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
303
304 /*
305 * Local variables:
306 * tab-width: 4
307 * c-basic-offset: 4
308 * End:
309 * vim600: noet sw=4 ts=4 fdm=marker
310 * vim<600: noet sw=4 ts=4
311 */
312