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