2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
23 #include "php_http_encoding_api.h"
25 #include "php_http_api.h"
27 ZEND_EXTERN_MODULE_GLOBALS(http
);
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
)
37 *decoded
= ecalloc(1, encoded_len
);
41 while ((rest
= encoded
+ encoded_len
- e_ptr
) > 0) {
43 int EOL_len
= 0, eol_mismatch
= 0;
46 chunk_len
= strtol(e_ptr
, &n_ptr
, 16);
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
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
;
65 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Invalid character (expected 0x0D 0x0A; got: 0x%X 0x%X)", *n_ptr
, *(n_ptr
+ 1));
67 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Invalid character (expected 0x0A; got: 0x%X)", *n_ptr
);
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
);
85 memcpy(d_ptr
, e_ptr
+= EOL_len
, chunk_len
);
87 e_ptr
+= chunk_len
+ EOL_len
;
88 *decoded_len
+= chunk_len
;
98 /* max count of uncompress trials, alloc_size <<= 2 for each try */
99 #define HTTP_GZMAXTRY 10
101 #define HTTP_GZSAFPAD 10
102 /* add 1% extra space in case we need to encode widely differing (binary) data */
103 #define HTTP_GZBUFLEN(l) (l + (l / 100) + HTTP_GZSAFPAD)
105 static const char http_gzencode_header
[] = {
108 (const char) Z_DEFLATED
,
113 inline void http_init_gzencode_buffer(z_stream
*Z
, const char *data
, size_t data_len
, char **buf_ptr
)
119 Z
->next_in
= (Bytef
*) data
;
120 Z
->avail_in
= data_len
;
121 Z
->avail_out
= HTTP_GZBUFLEN(data_len
) + HTTP_GZSAFPAD
- 1;
123 *buf_ptr
= emalloc(HTTP_GZBUFLEN(data_len
) + sizeof(http_gzencode_header
) + HTTP_GZSAFPAD
);
124 memcpy(*buf_ptr
, http_gzencode_header
, sizeof(http_gzencode_header
));
126 Z
->next_out
= *buf_ptr
+ sizeof(http_gzencode_header
);
129 inline void http_init_deflate_buffer(z_stream
*Z
, const char *data
, size_t data_len
, char **buf_ptr
)
135 Z
->data_type
= Z_UNKNOWN
;
136 Z
->next_in
= (Bytef
*) data
;
137 Z
->avail_in
= data_len
;
138 Z
->avail_out
= HTTP_GZBUFLEN(data_len
) - 1;
139 Z
->next_out
= emalloc(HTTP_GZBUFLEN(data_len
));
141 *buf_ptr
= Z
->next_out
;
144 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
)
150 *buf_len
= data_len
* 2;
151 *buf_ptr
= emalloc(*buf_len
+ 1);
154 *buf_ptr
= erealloc(*buf_ptr
, *buf_len
+ 1);
157 Z
->next_in
= (Bytef
*) data
;
158 Z
->avail_in
= data_len
;
159 Z
->avail_out
= *buf_len
;
160 Z
->next_out
= *buf_ptr
;
163 inline size_t http_finish_buffer(size_t buf_len
, char **buf_ptr
)
165 (*buf_ptr
)[buf_len
] = '\0';
169 inline size_t http_finish_gzencode_buffer(z_stream
*Z
, const char *data
, size_t data_len
, char **buf_ptr
)
174 crc
= crc32(0L, Z_NULL
, 0);
175 crc
= crc32(crc
, (const Bytef
*) data
, data_len
);
177 trailer
= *buf_ptr
+ sizeof(http_gzencode_header
) + Z
->total_out
;
180 trailer
[0] = (char) (crc
& 0xFF);
181 trailer
[1] = (char) ((crc
>> 8) & 0xFF);
182 trailer
[2] = (char) ((crc
>> 16) & 0xFF);
183 trailer
[3] = (char) ((crc
>> 24) & 0xFF);
184 trailer
[4] = (char) ((Z
->total_in
) & 0xFF);
185 trailer
[5] = (char) ((Z
->total_in
>> 8) & 0xFF);
186 trailer
[6] = (char) ((Z
->total_in
>> 16) & 0xFF);
187 trailer
[7] = (char) ((Z
->total_in
>> 24) & 0xFF);
189 return http_finish_buffer(Z
->total_out
+ sizeof(http_gzencode_header
) + 8, buf_ptr
);
193 PHP_HTTP_API STATUS
_http_encoding_gzencode(int level
, const char *data
, size_t data_len
, char **encoded
, size_t *encoded_len TSRMLS_DC
)
196 STATUS status
= Z_OK
;
198 http_init_gzencode_buffer(&Z
, data
, data_len
, encoded
);
200 if ( (Z_OK
== (status
= deflateInit2(&Z
, level
, Z_DEFLATED
, -MAX_WBITS
, MAX_MEM_LEVEL
, Z_DEFAULT_STRATEGY
))) &&
201 (Z_STREAM_END
== (status
= deflate(&Z
, Z_FINISH
))) &&
202 (Z_OK
== (status
= deflateEnd(&Z
)))) {
203 *encoded_len
= http_finish_gzencode_buffer(&Z
, data
, data_len
, encoded
);
208 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not gzencode data: %s", zError(status
));
212 PHP_HTTP_API STATUS
_http_encoding_deflate(int level
, const char *data
, size_t data_len
, char **encoded
, size_t *encoded_len TSRMLS_DC
)
215 STATUS status
= Z_OK
;
217 http_init_deflate_buffer(&Z
, data
, data_len
, encoded
);
219 if ( (Z_OK
== (status
= deflateInit2(&Z
, level
, Z_DEFLATED
, -MAX_WBITS
, MAX_MEM_LEVEL
, Z_DEFAULT_STRATEGY
))) &&
220 (Z_STREAM_END
== (status
= deflate(&Z
, Z_FINISH
))) &&
221 (Z_OK
== (status
= deflateEnd(&Z
)))) {
222 *encoded_len
= http_finish_buffer(Z
.total_out
, encoded
);
227 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not deflate data: %s", zError(status
));
231 PHP_HTTP_API STATUS
_http_encoding_compress(int level
, const char *data
, size_t data_len
, char **encoded
, size_t *encoded_len TSRMLS_DC
)
235 *encoded
= emalloc(*encoded_len
= HTTP_GZBUFLEN(data_len
));
237 if (Z_OK
== (status
= compress2(*encoded
, encoded_len
, data
, data_len
, level
))) {
238 http_finish_buffer(*encoded_len
, encoded
);
243 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not compress data: %s", zError(status
));
247 PHP_HTTP_API STATUS
_http_encoding_gzdecode(const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
249 const char *encoded
= data
+ sizeof(http_gzencode_header
);
252 if (data_len
<= sizeof(http_gzencode_header
) + 8) {
253 http_error(HE_WARNING
, HTTP_E_ENCODING
, "Could not gzdecode data: too short data length");
255 encoded_len
= data_len
- sizeof(http_gzencode_header
) - 8;
257 if (SUCCESS
== http_encoding_inflate(encoded
, encoded_len
, decoded
, decoded_len
)) {
258 unsigned long len
= 0, cmp
= 0, crc
= crc32(0L, Z_NULL
, 0);
260 crc
= crc32(crc
, (const Bytef
*) *decoded
, *decoded_len
);
262 cmp
= (unsigned) ((data
[data_len
-8] & 0xFF));
263 cmp
+= (unsigned) ((data
[data_len
-7] & 0xFF) << 8);
264 cmp
+= (unsigned) ((data
[data_len
-6] & 0xFF) << 16);
265 cmp
+= (unsigned) ((data
[data_len
-5] & 0xFF) << 24);
266 len
= (unsigned) ((data
[data_len
-4] & 0xFF));
267 len
+= (unsigned) ((data
[data_len
-3] & 0xFF) << 8);
268 len
+= (unsigned) ((data
[data_len
-2] & 0xFF) << 16);
269 len
+= (unsigned) ((data
[data_len
-1] & 0xFF) << 24);
272 http_error_ex(HE_NOTICE
, HTTP_E_ENCODING
, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp
, crc
);
274 if (len
!= *decoded_len
) {
275 http_error_ex(HE_NOTICE
, HTTP_E_ENCODING
, "Could not verify data integrity: data sizes do not match (%lu, %lu)", len
, *decoded_len
);
284 PHP_HTTP_API STATUS
_http_encoding_inflate(const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
291 http_init_inflate_buffer(&Z
, data
, data_len
, decoded
, decoded_len
, max
++);
292 if (Z_OK
== (status
= inflateInit2(&Z
, -MAX_WBITS
))) {
293 if (Z_STREAM_END
== (status
= inflate(&Z
, Z_FINISH
))) {
294 if (Z_OK
== (status
= inflateEnd(&Z
))) {
295 *decoded_len
= http_finish_buffer(Z
.total_out
, decoded
);
300 } while (max
< HTTP_GZMAXTRY
);
302 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not inflate data: %s", zError(status
));
306 PHP_HTTP_API STATUS
_http_encoding_uncompress(const char *data
, size_t data_len
, char **decoded
, size_t *decoded_len TSRMLS_DC
)
310 size_t want
= data_len
* 2;
312 *decoded
= emalloc(want
+ 1);
313 if (Z_BUF_ERROR
== (status
= uncompress(*decoded
, &want
, data
, data_len
))) do {
314 /* this is a lot faster with large data than gzuncompress(),
315 but could be a problem with a low memory limit */
317 *decoded
= erealloc(*decoded
, want
+ 1);
318 status
= uncompress(*decoded
, &want
, data
, data_len
);
319 } while (++max
< HTTP_GZMAXTRY
&& status
== Z_BUF_ERROR
);
321 if (Z_OK
== status
) {
322 *decoded_len
= http_finish_buffer(want
, decoded
);
327 http_error_ex(HE_WARNING
, HTTP_E_ENCODING
, "Could not uncompress data: %s", zError(status
));
331 #endif /* HTTP_HAVE_ZLIB */
338 * vim600: noet sw=4 ts=4 fdm=marker
339 * vim<600: noet sw=4 ts=4