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