- no fatal ereallocs please
[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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15
16 #ifdef HAVE_CONFIG_H
17 # include "config.h"
18 #endif
19
20 #define HTTP_WANT_ZLIB
21 #include "php_http.h"
22
23 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_ZLIB)
24
25 #include "php_http_api.h"
26 #include "php_http_encoding_api.h"
27 #include "php_http_exception_object.h"
28 #include "php_http_inflatestream_object.h"
29
30 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpInflateStream, method, ret_ref, req_args)
31 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpInflateStream, method, ret_ref)
32 #define HTTP_INFLATE_ME(method, visibility) PHP_ME(HttpInflateStream, method, HTTP_ARGS(HttpInflateStream, method), visibility)
33
34 HTTP_BEGIN_ARGS(update, 0, 1)
35 HTTP_ARG_VAL(data, 0)
36 HTTP_END_ARGS;
37
38 HTTP_BEGIN_ARGS(flush, 0, 0)
39 HTTP_ARG_VAL(data, 0)
40 HTTP_END_ARGS;
41
42 HTTP_BEGIN_ARGS(finish, 0, 0)
43 HTTP_ARG_VAL(data, 0)
44 HTTP_END_ARGS;
45
46 zend_class_entry *http_inflatestream_object_ce;
47 zend_function_entry http_inflatestream_object_fe[] = {
48 HTTP_INFLATE_ME(update, ZEND_ACC_PUBLIC)
49 HTTP_INFLATE_ME(flush, ZEND_ACC_PUBLIC)
50 HTTP_INFLATE_ME(finish, ZEND_ACC_PUBLIC)
51
52 EMPTY_FUNCTION_ENTRY
53 };
54 static zend_object_handlers http_inflatestream_object_handlers;
55
56 static inline void http_inflatestream_object_declare_default_properties() { return; }
57
58 PHP_MINIT_FUNCTION(http_inflatestream_object)
59 {
60 HTTP_REGISTER_CLASS_EX(HttpInflateStream, http_inflatestream_object, NULL, 0);
61 http_inflatestream_object_handlers.clone_obj = _http_inflatestream_object_clone_obj;
62 return SUCCESS;
63 }
64
65 zend_object_value _http_inflatestream_object_new(zend_class_entry *ce TSRMLS_DC)
66 {
67 return http_inflatestream_object_new_ex(ce, NULL, NULL);
68 }
69
70 zend_object_value _http_inflatestream_object_new_ex(zend_class_entry *ce, http_encoding_stream *s, http_inflatestream_object **ptr TSRMLS_DC)
71 {
72 zend_object_value ov;
73 http_inflatestream_object *o;
74
75 o = ecalloc(1, sizeof(http_inflatestream_object));
76 o->zo.ce = ce;
77
78 if (ptr) {
79 *ptr = o;
80 }
81
82 if (s) {
83 o->stream = s;
84 }
85
86 ALLOC_HASHTABLE(OBJ_PROP(o));
87 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
88
89 ov.handle = putObject(http_inflatestream_object, o);
90 ov.handlers = &http_inflatestream_object_handlers;
91
92 return ov;
93 }
94
95 zend_object_value _http_inflatestream_object_clone_obj(zval *this_ptr TSRMLS_DC)
96 {
97 http_encoding_stream *s;
98 getObject(http_inflatestream_object, obj);
99
100 s = ecalloc(1, sizeof(http_encoding_stream));
101 s->flags = obj->stream->flags;
102 inflateCopy(&s->stream, &obj->stream->stream);
103 s->stream.opaque = phpstr_dup(s->stream.opaque);
104
105 return http_inflatestream_object_new_ex(Z_OBJCE_P(this_ptr), s, NULL);
106 }
107
108 void _http_inflatestream_object_free(zend_object *object TSRMLS_DC)
109 {
110 http_inflatestream_object *o = (http_inflatestream_object *) object;
111
112 if (OBJ_PROP(o)) {
113 zend_hash_destroy(OBJ_PROP(o));
114 FREE_HASHTABLE(OBJ_PROP(o));
115 }
116 if (o->stream) {
117 http_encoding_inflate_stream_free(&o->stream);
118 }
119 efree(o);
120 }
121
122 /* {{{ proto string HttpInflateStream::update(string data)
123 *
124 * Passes more data through the inflate stream.
125 *
126 * Expects a string parameter containing (a part of) the data to inflate.
127 *
128 * Returns inflated data on success or FALSE on failure.
129 */
130 PHP_METHOD(HttpInflateStream, update)
131 {
132 int data_len;
133 size_t decoded_len = 0;
134 char *data, *decoded = NULL;
135 getObject(http_inflatestream_object, obj);
136
137 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len)) {
138 RETURN_FALSE;
139 }
140
141 if (!data_len) {
142 RETURN_STRING("", 1);
143 }
144
145 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
146 RETURN_FALSE;
147 }
148
149 if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
150 RETURN_STRINGL(decoded, decoded_len, 0);
151 } else {
152 RETURN_FALSE;
153 }
154 }
155 /* }}} */
156
157 /* {{{ proto string HttpInflateStream::flush([string data])
158 *
159 * Flush the inflate stream.
160 *
161 * Returns some inflated data as string on success or FALSE on failure.
162 */
163 PHP_METHOD(HttpInflateStream, flush)
164 {
165 int data_len = 0;
166 size_t decoded_len = 0;
167 char *decoded = NULL, *data = NULL;
168 getObject(http_inflatestream_object, obj);
169
170 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
171 RETURN_FALSE;
172 }
173
174 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
175 RETURN_FALSE;
176 }
177
178 /* flushing the inflate stream is a no-op */
179 if (!data_len) {
180 RETURN_STRINGL("", 0, 1);
181 } else if (SUCCESS == http_encoding_inflate_stream_update(obj->stream, data, data_len, &decoded, &decoded_len)) {
182 RETURN_STRINGL(decoded, decoded_len, 0);
183 } else {
184 RETURN_FALSE;
185 }
186 }
187 /* }}} */
188
189 /* {{{ proto string HttpInflateStream::finish([string data])
190 *
191 * Finalizes the inflate stream. The inflate stream can be reused after finalizing.
192 *
193 * Returns the final part of inflated data.
194 */
195 PHP_METHOD(HttpInflateStream, finish)
196 {
197 int data_len = 0;
198 size_t updated_len = 0, decoded_len = 0;
199 char *updated = NULL, *decoded = NULL, *data = NULL;
200 getObject(http_inflatestream_object, obj);
201
202 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &data, &data_len)) {
203 RETURN_FALSE;
204 }
205
206 if (!obj->stream && !(obj->stream = http_encoding_inflate_stream_init(NULL, 0))) {
207 RETURN_FALSE;
208 }
209
210 if (data_len) {
211 if (SUCCESS != http_encoding_inflate_stream_update(obj->stream, data, data_len, &updated, &updated_len)) {
212 RETURN_FALSE;
213 }
214 }
215
216 if (SUCCESS == http_encoding_inflate_stream_finish(obj->stream, &decoded, &decoded_len)) {
217 if (updated_len) {
218 updated = erealloc(updated, updated_len + decoded_len + 1);
219 updated[updated_len + decoded_len] = '\0';
220 memcpy(updated + updated_len, decoded, decoded_len);
221 updated_len += decoded_len;
222 RETVAL_STRINGL(updated, updated_len, 0);
223 STR_FREE(decoded);
224 } else {
225 RETVAL_STRINGL(decoded, decoded_len, 0);
226 STR_FREE(updated);
227 }
228 } else {
229 RETVAL_FALSE;
230 }
231
232 http_encoding_inflate_stream_dtor(obj->stream);
233 http_encoding_inflate_stream_init(obj->stream, obj->stream->flags);
234 }
235 /* }}} */
236
237
238 #endif /* ZEND_ENGINE_2 && HTTP_HAVE_ZLIB*/
239
240 /*
241 * Local variables:
242 * tab-width: 4
243 * c-basic-offset: 4
244 * End:
245 * vim600: noet sw=4 ts=4 fdm=marker
246 * vim<600: noet sw=4 ts=4
247 */
248