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