fix php 5.4 build
[m6w6/ext-http] / http_inflatestream_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-2010, 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_inflatestream_object.h"
24
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)
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_inflatestream_object_ce
51 zend_class_entry *http_inflatestream_object_ce;
52 zend_function_entry http_inflatestream_object_fe[] = {
53 HTTP_INFLATE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
54 HTTP_INFLATE_ME(update, ZEND_ACC_PUBLIC)
55 HTTP_INFLATE_ME(flush, ZEND_ACC_PUBLIC)
56 HTTP_INFLATE_ME(finish, ZEND_ACC_PUBLIC)
57
58 HTTP_INFLATE_ME(factory, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
59
60 EMPTY_FUNCTION_ENTRY
61 };
62 static zend_object_handlers http_inflatestream_object_handlers;
63
64 PHP_MINIT_FUNCTION(http_inflatestream_object)
65 {
66 HTTP_REGISTER_CLASS_EX(HttpInflateStream, http_inflatestream_object, NULL, 0);
67 http_inflatestream_object_handlers.clone_obj = _http_inflatestream_object_clone_obj;
68
69 #ifndef WONKY
70 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("FLUSH_NONE")-1, HTTP_ENCODING_STREAM_FLUSH_NONE TSRMLS_CC);
71 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("FLUSH_SYNC")-1, HTTP_ENCODING_STREAM_FLUSH_SYNC TSRMLS_CC);
72 zend_declare_class_constant_long(THIS_CE, ZEND_STRS("FLUSH_FULL")-1, HTTP_ENCODING_STREAM_FLUSH_FULL TSRMLS_CC);
73 #endif
74
75 return SUCCESS;
76 }
77
78 zend_object_value _http_inflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
79 {
80 return http_inflatestream_object_new_ex(ce, NULL, NULL);
81 }
82
83 zend_object_value _http_inflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_inflatestream_object **ptr TSRMLS_DC)
84 {
85 zend_object_value ov;
86 http_inflatestream_object *o;
87
88 o = ecalloc(1, sizeof(http_inflatestream_object));
89 o->zo.ce = ce;
90
91 if (ptr) {
92 *ptr = o;
93 }
94
95 if (s) {
96 o->stream = s;
97 }
98
99 #ifdef ZEND_ENGINE_2_4
100 zend_object_std_init(o, ce TSRMLS_CC);
101 object_properties_init(o, ce);
102 #else
103 ALLOC_HASHTABLE(OBJ_PROP(o));
104 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
105 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
106 #endif
107
108 ov.handle = putObject(http_inflatestream_object, o);
109 ov.handlers = &http_inflatestream_object_handlers;
110
111 return ov;
112 }
113
114 zend_object_value _http_inflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
115 {
116 http_encoding_stream *s;
117 zend_object_value new_ov;
118 http_inflatestream_object *new_obj = NULL;
119 getObject(http_inflatestream_object, old_obj);
120
121 s = ecalloc(1, sizeof(http_encoding_stream));
122 s->flags = old_obj->stream->flags;
123 inflateCopy(&s->stream, &old_obj->stream->stream);
124 s->stream.opaque = phpstr_dup(s->stream.opaque);
125
126 new_ov = http_inflatestream_object_new_ex(old_obj->zo.ce, s, &new_obj);
127 zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
128
129 return new_ov;
130 }
131
132 void _http_inflatestream_object_free(zend_object *object TSRMLS_DC)
133 {
134 http_inflatestream_object *o = (http_inflatestream_object *) object;
135
136 if (o->stream) {
137 http_encoding_inflate_stream_free(&o->stream);
138 }
139 freeObject(o);
140 }
141
142 /* {{{ proto void HttpInflateStream::__construct([int flags = 0])
143 Creates a new HttpInflateStream object instance. */
144 PHP_METHOD(HttpInflateStream, __construct)
145 {
146 long flags = 0;
147
148 SET_EH_THROW_HTTP();
149 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &flags)) {
150 getObject(http_inflatestream_object, obj);
151
152 if (!obj->stream) {
153 obj->stream = http_encoding_inflate_stream_init(NULL, flags & 0x0fffffff);
154 } else {
155 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "HttpInflateStream cannot be initialized twice");
156 }
157 }
158 SET_EH_NORMAL();
159 }
160 /* }}} */
161
162 /* {{{ proto HttpInflateStream HttpInflateStream::factory([int flags[, string class = "HttpInflateStream"]])
163 Creates a new HttpInflateStream object instance. */
164 PHP_METHOD(HttpInflateStream, factory)
165 {
166 long flags = 0;
167 char *cn = NULL;
168 int cl = 0;
169
170 SET_EH_THROW_HTTP();
171 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &flags, &cn, &cl)) {
172 zend_object_value ov;
173 http_encoding_stream *s = http_encoding_inflate_stream_init(NULL, flags & 0x0fffffff);
174
175 if (SUCCESS == http_object_new(&ov, cn, cl, _http_inflatestream_object_new_ex, http_inflatestream_object_ce, s, NULL)) {
176 RETVAL_OBJVAL(ov, 0);
177 }
178 }
179 SET_EH_NORMAL();
180 }
181 /* }}} */
182
183 /* {{{ proto string HttpInflateStream::update(string data)
184 Passes more data through the inflate stream. */
185 PHP_METHOD(HttpInflateStream, update)
186 {
187 int data_len;
188 size_t decoded_len = 0;
189 char *data, *decoded = NULL;
190 getObject(http_inflatestream_object, obj);
191
192 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
193 RETURN_FALSE;
194 }
195
196 if (!data_len) {
197 RETURN_STRING("", 1);
198 }
199
200 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
201 RETURN_FALSE;
202 }
203
204 if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
205 RETURN_STRINGL(decoded, decoded_len, 0);
206 } else {
207 RETURN_FALSE;
208 }
209 }
210 /* }}} */
211
212 /* {{{ proto string HttpInflateStream::flush([string data])
213 Flush the inflate stream. */
214 PHP_METHOD(HttpInflateStream, flush)
215 {
216 int data_len = 0;
217 size_t decoded_len = 0;
218 char *decoded = NULL, *data = NULL;
219 getObject(http_inflatestream_object, obj);
220
221 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
222 RETURN_FALSE;
223 }
224
225 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
226 RETURN_FALSE;
227 }
228
229 /* flushing the inflate stream is a no-op */
230 if (!data_len) {
231 RETURN_STRINGL("", 0, 1);
232 } else if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
233 RETURN_STRINGL(decoded, decoded_len, 0);
234 } else {
235 RETURN_FALSE;
236 }
237 }
238 /* }}} */
239
240 /* {{{ proto string HttpInflateStream::finish([string data])
241 Finalizes the inflate stream. The inflate stream can be reused after finalizing. */
242 PHP_METHOD(HttpInflateStream, finish)
243 {
244 int data_len = 0;
245 size_t updated_len = 0, decoded_len = 0;
246 char *updated = NULL, *decoded = NULL, *data = NULL;
247 getObject(http_inflatestream_object, obj);
248
249 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
250 RETURN_FALSE;
251 }
252
253 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
254 RETURN_FALSE;
255 }
256
257 if (data_len) {
258 if (SUCCESS != http_encoding_inflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
259 RETURN_FALSE;
260 }
261 }
262
263 if (SUCCESS == http_encoding_inflate_stream_finish(obj->stream, &decoded, &decoded_len)) {
264 if (updated_len) {
265 updated = erealloc(updated, updated_len + decoded_len + 1);
266 updated[updated_len + decoded_len] = '\0';
267 memcpy(updated + updated_len, decoded, decoded_len);
268 STR_FREE(decoded);
269 updated_len += decoded_len;
270 RETVAL_STRINGL(updated, updated_len, 0);
271 } else if (decoded) {
272 STR_FREE(updated);
273 RETVAL_STRINGL(decoded, decoded_len, 0);
274 } else {
275 RETVAL_NULL();
276 }
277 } else {
278 STR_FREE(updated);
279 RETVAL_FALSE;
280 }
281
282 http_encoding_inflate_stream_dtor(obj->stream);
283 http_encoding_inflate_stream_init(obj->stream, obj->stream->flags);
284 }
285 /* }}} */
286
287 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
288
289 /*
290 * Local variables:
291 * tab-width: 4
292 * c-basic-offset: 4
293 * End:
294 * vim600: noet sw=4 ts=4 fdm=marker
295 * vim<600: noet sw=4 ts=4
296 */
297