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