- add compression functions to userspace
[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 ZEND_EXTERN_MODULE_GLOBALS(http);
28
29 /* {{{ char *http_encoding_dechunk(char *, size_t, char **, size_t *) */
30 PHP_HTTP_API const char *_http_encoding_dechunk(const char *encoded, size_t encoded_len, char **decoded, size_t *decoded_len TSRMLS_DC)
31 {
32 const char *e_ptr;
33 char *d_ptr;
34 long rest;
35
36 *decoded_len = 0;
37 *decoded = ecalloc(1, encoded_len);
38 d_ptr = *decoded;
39 e_ptr = encoded;
40
41 while ((rest = encoded + encoded_len - e_ptr) > 0) {
42 long chunk_len = 0;
43 int EOL_len = 0, eol_mismatch = 0;
44 char *n_ptr;
45
46 chunk_len = strtol(e_ptr, &n_ptr, 16);
47
48 /* check if:
49 * - we could not read in chunk size
50 * - we got a negative chunk size
51 * - chunk size is greater then remaining size
52 * - chunk size is not followed by (CR)LF|NUL
53 */
54 if ( (n_ptr == e_ptr) || (chunk_len < 0) || (chunk_len > rest) ||
55 (*n_ptr && (eol_mismatch = (n_ptr != http_locate_eol(e_ptr, &EOL_len))))) {
56 /* don't fail on apperently not encoded data */
57 if (e_ptr == encoded) {
58 memcpy(*decoded, encoded, encoded_len);
59 *decoded_len = encoded_len;
60 return encoded + encoded_len;
61 } else {
62 efree(*decoded);
63 if (eol_mismatch) {
64 if (EOL_len == 2) {
65 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0D 0x0A; got: 0x%X 0x%X)", *n_ptr, *(n_ptr + 1));
66 } else {
67 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid character (expected 0x0A; got: 0x%X)", *n_ptr);
68 }
69 } else {
70 char *error = estrndup(n_ptr, strcspn(n_ptr, "\r\n "));
71 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Invalid chunk size: '%s' at pos %d", error, n_ptr - encoded);
72 efree(error);
73 }
74 return NULL;
75 }
76 } else {
77 e_ptr = n_ptr;
78 }
79
80 /* reached the end */
81 if (!chunk_len) {
82 break;
83 }
84
85 memcpy(d_ptr, e_ptr += EOL_len, chunk_len);
86 d_ptr += chunk_len;
87 e_ptr += chunk_len + EOL_len;
88 *decoded_len += chunk_len;
89 }
90
91 return e_ptr;
92 }
93 /* }}} */
94
95 #ifdef HTTP_HAVE_ZLIB
96 #include <zlib.h>
97
98 #define HTTP_GZMAXTRY 10
99 #define HTTP_GZBUFLEN(l) (l + (l / 1000) + 16 + 1)
100
101 static const char http_gzencode_header[] = {
102 (const char) 0x1f,
103 (const char) 0x8b,
104 (const char) Z_DEFLATED,
105 0, 0, 0, 0, 0, 0,
106 (const char) 0x03
107 };
108
109 inline void http_init_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
110 {
111 Z->zalloc = Z_NULL;
112 Z->zfree = Z_NULL;
113 Z->opaque = Z_NULL;
114
115 Z->next_in = (Bytef *) data;
116 Z->avail_in = data_len;
117 Z->avail_out = HTTP_GZBUFLEN(data_len) - 1;
118
119 *buf_ptr = emalloc(Z->avail_out + sizeof(http_gzencode_header));
120 memcpy(*buf_ptr, http_gzencode_header, sizeof(http_gzencode_header));
121
122 Z->next_out = *buf_ptr + sizeof(http_gzencode_header);
123 }
124
125 inline void http_init_deflate_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
126 {
127 Z->zalloc = Z_NULL;
128 Z->zfree = Z_NULL;
129 Z->opaque = Z_NULL;
130
131 Z->data_type = Z_ASCII;
132 Z->next_in = (Bytef *) data;
133 Z->avail_in = data_len;
134 Z->avail_out = HTTP_GZBUFLEN(data_len) - 1;
135 Z->next_out = emalloc(Z->avail_out);
136
137 *buf_ptr = Z->next_out;
138 }
139
140 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)
141 {
142 Z->zalloc = Z_NULL;
143 Z->zfree = Z_NULL;
144
145 if (!iteration) {
146 *buf_len = data_len * 2;
147 *buf_ptr = emalloc(*buf_len + 1);
148 } else {
149 *buf_len <<= 2;
150 *buf_ptr = erealloc(*buf_ptr, *buf_len + 1);
151 }
152
153 Z->next_in = (Bytef *) data;
154 Z->avail_in = data_len;
155 Z->avail_out = *buf_len;
156 Z->next_out = *buf_ptr;
157 }
158
159 inline size_t http_finish_buffer(size_t buf_len, char **buf_ptr)
160 {
161 (*buf_ptr)[buf_len] = '\0';
162 return buf_len;
163 }
164
165 inline size_t http_finish_gzencode_buffer(z_stream *Z, const char *data, size_t data_len, char **buf_ptr)
166 {
167 unsigned long crc;
168 char *trailer;
169
170 crc = crc32(0L, Z_NULL, 0);
171 crc = crc32(crc, (const Bytef *) data, data_len);
172
173 trailer = *buf_ptr + sizeof(http_gzencode_header) + Z->total_out;
174
175 /* LSB */
176 trailer[0] = (char) (crc & 0xFF);
177 trailer[1] = (char) ((crc >> 8) & 0xFF);
178 trailer[2] = (char) ((crc >> 16) & 0xFF);
179 trailer[3] = (char) ((crc >> 24) & 0xFF);
180 trailer[4] = (char) ((Z->total_in) & 0xFF);
181 trailer[5] = (char) ((Z->total_in >> 8) & 0xFF);
182 trailer[6] = (char) ((Z->total_in >> 16) & 0xFF);
183 trailer[7] = (char) ((Z->total_in >> 24) & 0xFF);
184
185 return http_finish_buffer(Z->total_out + sizeof(http_gzencode_header) + 8, buf_ptr);
186 }
187
188
189 PHP_HTTP_API STATUS _http_encoding_gzencode(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
190 {
191 z_stream Z;
192 STATUS status = Z_OK;
193
194 http_init_gzencode_buffer(&Z, data, data_len, encoded);
195
196 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
197 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
198 (Z_OK == (status = deflateEnd(&Z)))) {
199 *encoded_len = http_finish_gzencode_buffer(&Z, data, data_len, encoded);
200 return SUCCESS;
201 }
202
203 efree(*encoded);
204 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not gzencode data: %s", zError(status));
205 return FAILURE;
206 }
207
208 PHP_HTTP_API STATUS _http_encoding_deflate(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
209 {
210 z_stream Z;
211 STATUS status = Z_OK;
212
213 http_init_deflate_buffer(&Z, data, data_len, encoded);
214
215 if ( (Z_OK == (status = deflateInit2(&Z, level, Z_DEFLATED, -MAX_WBITS, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY))) &&
216 (Z_STREAM_END == (status = deflate(&Z, Z_FINISH))) &&
217 (Z_OK == (status = deflateEnd(&Z)))) {
218 *encoded_len = http_finish_buffer(Z.total_out, encoded);
219 return SUCCESS;
220 }
221
222 efree(encoded);
223 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not deflate data: %s", zError(status));
224 return FAILURE;
225 }
226
227 PHP_HTTP_API STATUS _http_encoding_compress(int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
228 {
229 STATUS status;
230
231 *encoded = emalloc(*encoded_len = HTTP_GZBUFLEN(data_len));
232
233 if (Z_OK == (status = compress2(*encoded, encoded_len, data, data_len, level))) {
234 http_finish_buffer(*encoded_len, encoded);
235 return SUCCESS;
236 }
237
238 efree(encoded);
239 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not compress data: %s", zError(status));
240 return FAILURE;
241 }
242
243 PHP_HTTP_API STATUS _http_encoding_gzdecode(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
244 {
245 const char *encoded = data + sizeof(http_gzencode_header);
246 size_t encoded_len;
247
248 if (data_len <= sizeof(http_gzencode_header) + 8) {
249 http_error(HE_WARNING, HTTP_E_ENCODING, "Could not gzdecode data: too short data length");
250 } else {
251 encoded_len = data_len - sizeof(http_gzencode_header) - 8;
252
253 if (SUCCESS == http_encoding_inflate(encoded, encoded_len, decoded, decoded_len)) {
254 unsigned long len = 0, cmp = 0, crc = crc32(0L, Z_NULL, 0);
255
256 crc = crc32(crc, (const Bytef *) *decoded, *decoded_len);
257
258 cmp = (unsigned) ((data[data_len-8] & 0xFF));
259 cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
260 cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
261 cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
262 len = (unsigned) ((data[data_len-4] & 0xFF));
263 len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
264 len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
265 len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
266
267 if (cmp != crc) {
268 http_error_ex(HE_NOTICE, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
269 }
270 if (len != *decoded_len) {
271 http_error_ex(HE_NOTICE, HTTP_E_ENCODING, "Could not verify data integrity: data sizes do not match (%lu, %lu)", len, *decoded_len);
272 }
273
274 return SUCCESS;
275 }
276 }
277 return FAILURE;
278 }
279
280 PHP_HTTP_API STATUS _http_encoding_inflate(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
281 {
282 int max = 0;
283 STATUS status;
284 z_stream Z;
285
286 do {
287 http_init_inflate_buffer(&Z, data, data_len, decoded, decoded_len, max++);
288 if (Z_OK == (status = inflateInit2(&Z, -MAX_WBITS))) {
289 if (Z_STREAM_END == (status = inflate(&Z, Z_FINISH))) {
290 if (Z_OK == (status = inflateEnd(&Z))) {
291 *decoded_len = http_finish_buffer(Z.total_out, decoded);
292 return SUCCESS;
293 }
294 }
295 }
296 } while (max < HTTP_GZMAXTRY);
297
298 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not inflate data: %s", zError(status));
299 return FAILURE;
300 }
301
302 PHP_HTTP_API STATUS _http_encoding_uncompress(const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
303 {
304 int max = 0;
305 STATUS status;
306 size_t want = data_len * 2;
307
308 *decoded = emalloc(want + 1);
309 if (Z_BUF_ERROR == (status = uncompress(*decoded, &want, data, data_len))) do {
310 /* this is a lot faster with large data than gzuncompress(),
311 but could be a problem with a low memory limit */
312 want <<= 2;
313 *decoded = erealloc(*decoded, want + 1);
314 status = uncompress(*decoded, &want, data, data_len);
315 } while (++max < HTTP_GZMAXTRY && status == Z_BUF_ERROR);
316
317 if (Z_OK == status) {
318 *decoded_len = http_finish_buffer(want, decoded);
319 return SUCCESS;
320 }
321
322 efree(*decoded);
323 http_error_ex(HE_WARNING, HTTP_E_ENCODING, "Could not uncompress data: %s", zError(status));
324 return FAILURE;
325 }
326
327 #endif /* HTTP_HAVE_ZLIB */
328
329 /*
330 * Local variables:
331 * tab-width: 4
332 * c-basic-offset: 4
333 * End:
334 * vim600: noet sw=4 ts=4 fdm=marker
335 * vim<600: noet sw=4 ts=4
336 */
337