67c27a109477b7dadb729be4347bbd661191eee7
[m6w6/ext-http] / http_encoding_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 #include "php.h"
22
23 #include "php_http_encoding_api.h"
24 #include "php_http.h"
25 #include "php_http_api.h"
26
27 #ifdef HTTP_HAVE_ZLIB
28 #include <zlib.h>
29
30 #define HTTP_GZMAXTRY 10
31 #define HTTP_GZBUFLEN(l) (l + (l / 1000) + 16 + 1)
32
33 ZEND_EXTERN_MODULE_GLOBALS(http);
34
35 static const char http_gzencode_header[] = {
36 (const char) 0x1f,
37 (const char) 0x8b,
38 (const char) Z_DEFLATED,
39 0, 0, 0, 0, 0, 0,
40 (const char) 0x03
41 };
42
43 inline void http_init_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
44 {
45 Z->zalloc = Z_NULL;
46 Z->zfree = Z_NULL;
47 Z->opaque = Z_NULL;
48
49 Z->next_in = (Bytef *) data;
50 Z->avail_in = data_len;
51 Z->avail_out = HTTP_GZBUFLEN(data_len) - 1;
52
53 *buf_ptr = emalloc(Z->avail_out + sizeof(http_gzencode_header));
54 memcpy(*buf_ptr, http_gzencode_header, sizeof(http_gzencode_header));
55
56 Z->next_out = *buf_ptr + sizeof(http_gzencode_header);
57 }
58
59 inline void http_init_deflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
60 {
61 Z->zalloc = Z_NULL;
62 Z->zfree = Z_NULL;
63 Z->opaque = Z_NULL;
64
65 Z->data_type = Z_ASCII;
66 Z->next_in = (Bytef *) data;
67 Z->avail_in = data_len;
68 Z->avail_out = HTTP_GZBUFLEN(data_len) - 1;
69 Z->next_out = emalloc(Z->avail_out);
70
71 *buf_ptr = Z->next_out;
72 }
73
74 inline void http_init_inflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr, size_t *buf_len, int iteration)
75 {
76 Z->zalloc = Z_NULL;
77 Z->zfree = Z_NULL;
78
79 if (!iteration) {
80 *buf_len = data_len * 2;
81 *buf_ptr = emalloc(*buf_len + 1);
82 } else {
83 *buf_len <<= 2;
84 *buf_ptr = erealloc(*buf_ptr, *buf_len + 1);
85 }
86
87 Z->next_in = (Bytef *) data;
88 Z->avail_in = data_len;
89 Z->avail_out = *buf_len;
90 Z->next_out = *buf_ptr;
91 }
92
93 inline size_t http_finish_buffer(size_t buf_len, char **buf_ptr)
94 {
95 (*buf_ptr)[buf_len] = '\0';
96 return buf_len;
97 }
98
99 inline size_t http_finish_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
100 {
101 unsigned long crc;
102 char *trailer;
103
104 crc = crc32(0L, Z_NULL, 0);
105 crc = crc32(crc, (const Bytef *) data, data_len);
106
107 trailer = *buf_ptr + sizeof(http_gzencode_header) + Z->total_out;
108
109 /* write crc & stream.total_in in LSB order */
110 trailer[0] = (char) crc & 0xFF;
111 trailer[1] = (char) (crc >> 8) & 0xFF;
112 trailer[2] = (char) (crc >> 16) & 0xFF;
113 trailer[3] = (char) (crc >> 24) & 0xFF;
114 trailer[4] = (char) (Z->total_in) & 0xFF;
115 trailer[5] = (char) (Z->total_in >> 8) & 0xFF;
116 trailer[6] = (char) (Z->total_in >> 16) & 0xFF;
117 trailer[7] = (char) (Z->total_in >> 24) & 0xFF;
118
119 return http_finish_buffer(Z->total_out + sizeof(http_gzencode_header) + 8, buf_ptr);
120 }
121
122
123 PHP_HTTP_API STATUS _http_encoding_gzencode(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
124 {
125 z_stream Z;
126 STATUS status = Z_OK;
127
128 http_init_gzencode_buffer(&Z, data, data_len, encoded);
129
130 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
131 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
132 (Z_OK == (status = deflateEnd(&Z)))) {
133 *encoded_len = http_finish_gzencode_buffer(&Z, data, data_len, encoded);
134 return SUCCESS;
135 }
136
137 efree(*encoded);
138 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not gzencode data: %s", zError(status));
139 return FAILURE;
140 }
141
142 PHP_HTTP_API STATUS _http_encoding_deflate(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
143 {
144 z_stream Z;
145 STATUS status = Z_OK;
146
147 http_init_deflate_buffer(&Z, data, data_len, encoded);
148
149 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
150 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
151 (Z_OK == (status = deflateEnd(&Z)))) {
152 *encoded_len = http_finish_buffer(Z.total_out, encoded);
153 return SUCCESS;
154 }
155
156 efree(encoded);
157 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
158 return FAILURE;
159 }
160
161 PHP_HTTP_API STATUS _http_encoding_compress(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
162 {
163 STATUS status;
164
165 *encoded = emalloc(*encoded_len = HTTP_GZBUFLEN(data_len));
166
167 if (Z_OK == (status = compress2(*encoded, encoded_len, data, data_len, level))) {
168 http_finish_buffer(*encoded_len, encoded);
169 return SUCCESS;
170 }
171
172 efree(encoded);
173 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not compress data: %s", zError(status));
174 return FAILURE;
175 }
176
177 PHP_HTTP_API STATUS _http_encoding_gzdecode(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
178 {
179 const char *encoded = data + sizeof(http_gzencode_header);
180 size_t encoded_len;
181
182 if (data_len <= sizeof(http_gzencode_header) + 8) {
183 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not gzdecode data: too short data length");
184 } else {
185 encoded_len = data_len - sizeof(http_gzencode_header) - 8;
186
187 if (SUCCESS == http_encoding_inflate(encoded, encoded_len, decoded, decoded_len)) {
188 unsigned long len = 0, cmp = 0, crc = crc32(0L, Z_NULL, 0);
189
190 crc = crc32(crc, *decoded, *decoded_len);
191
192 cmp = (unsigned) (data[data_len-8]);
193 cmp += (unsigned) (data[data_len-7] << 8);
194 cmp += (unsigned) (data[data_len-6] << 16);
195 cmp += (unsigned) (data[data_len-5] << 24);
196 len = (unsigned) (data[data_len-4]);
197 len += (unsigned) (data[data_len-4] << 8);
198 len += (unsigned) (data[data_len-4] << 16);
199 len += (unsigned) (data[data_len-4] << 24);
200
201 if (cmp != crc) {
202 http_error(HE_NOTICE, HTTP_E_ENCODING, "Could not verify data integrity: CRC check failed");
203 }
204 if (len != *decoded_len) {
205 http_error(HE_NOTICE, HTTP_E_ENCODING, "Could not verify data integrity: data length check failed");
206 }
207
208 return SUCCESS;
209 }
210 }
211 return FAILURE;
212 }
213
214 PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
215 {
216 int max = 0;
217 STATUS status;
218 z_stream Z;
219
220 do {
221 http_init_inflate_buffer(&Z, data, data_len, decoded, decoded_len, max++);
222 if (Z_OK == (status = inflateInit2(&Z, -MAX_WBITS))) {
223 if (Z_STREAM_END == (status = inflate(&Z, Z_FINISH))) {
224 if (Z_OK == (status = inflateEnd(&Z))) {
225 *decoded_len = http_finish_buffer(Z.total_out, decoded);
226 return SUCCESS;
227 }
228 }
229 }
230 } while (max < HTTP_GZMAXTRY);
231
232 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
233 return FAILURE;
234 }
235
236 PHP_HTTP_API STATUS _http_encoding_uncompress(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
237 {
238 int max = 0;
239 STATUS status;
240 size_t want = data_len * 2;
241
242 *decoded = emalloc(want + 1);
243 if (Z_BUF_ERROR == (status = uncompress(*decoded, &want, data, data_len))) do {
244 /* this is a lot faster with large data than gzuncompress(),
245 but could be a problem with a low memory limit */
246 want <<= 2;
247 *decoded = erealloc(*decoded, want + 1);
248 status = uncompress(*decoded, &want, data, data_len);
249 } while (++max < HTTP_GZMAXTRY && status == Z_BUF_ERROR);
250
251 if (Z_OK == status) {
252 *decoded_len = http_finish_buffer(want, decoded);
253 return SUCCESS;
254 }
255
256 efree(*decoded);
257 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not uncompress data: %s", zError(status));
258 return FAILURE;
259 }
260
261 #endif /* HTTP_HAVE_ZLIB */
262
263 /*
264 * Local variables:
265 * tab-width: 4
266 * c-basic-offset: 4
267 * End:
268 * vim600: noet sw=4 ts=4 fdm=marker
269 * vim<600: noet sw=4 ts=4
270 */
271