- add the functions to the function entries ...
- add test
- fix buffer errors when encoding much widely differing (binary) data
PHP_FE(http_build_query, NULL)
#endif
PHP_FE(ob_etaghandler, NULL)
+#ifdef HTTP_HAVE_ZLIB
+ PHP_FE(http_gzencode, NULL)
+ PHP_FE(http_gzdecode, NULL)
+ PHP_FE(http_deflate, NULL)
+ PHP_FE(http_inflate, NULL)
+ PHP_FE(http_compress, NULL)
+ PHP_FE(http_uncompress, NULL)
+#endif
EMPTY_FUNCTION_ENTRY
};
#ifdef HTTP_HAVE_ZLIB
#include <zlib.h>
+/* max count of uncompress trials, alloc_size <<= 2 for each try */
#define HTTP_GZMAXTRY 10
-#define HTTP_GZBUFLEN(l) (l + (l / 1000) + 16 + 1)
+/* safe padding */
+#define HTTP_GZSAFPAD 10
+/* add 1% extra space in case we need to encode widely differing (binary) data */
+#define HTTP_GZBUFLEN(l) (l + (l / 100) + HTTP_GZSAFPAD)
static const char http_gzencode_header[] = {
(const char) 0x1f,
Z->next_in = (Bytef *) data;
Z->avail_in = data_len;
- Z->avail_out = HTTP_GZBUFLEN(data_len) - 1;
+ Z->avail_out = HTTP_GZBUFLEN(data_len) + HTTP_GZSAFPAD - 1;
- *buf_ptr = emalloc(Z->avail_out + sizeof(http_gzencode_header));
+ *buf_ptr = emalloc(HTTP_GZBUFLEN(data_len) + sizeof(http_gzencode_header));
memcpy(*buf_ptr, http_gzencode_header, sizeof(http_gzencode_header));
Z->next_out = *buf_ptr + sizeof(http_gzencode_header);
Z->zfree = Z_NULL;
Z->opaque = Z_NULL;
- Z->data_type = Z_ASCII;
+ Z->data_type = Z_UNKNOWN;
Z->next_in = (Bytef *) data;
Z->avail_in = data_len;
Z->avail_out = HTTP_GZBUFLEN(data_len) - 1;
- Z->next_out = emalloc(Z->avail_out);
+ Z->next_out = emalloc(HTTP_GZBUFLEN(data_len));
*buf_ptr = Z->next_out;
}
{
char *data;
int data_len;
- long level;
+ long level = -1;
RETVAL_NULL();
size_t decoded_len;
if (SUCCESS == http_encoding_uncompress(data, data_len, &decoded, &decoded_len)) {
- RETURN_STRINGL(decoded, decoded_len, 0);
+ RETURN_STRINGL(decoded, (int) decoded_len, 0);
}
}
}
--- /dev/null
+--TEST--
+encodings
+--SKIPIF--
+<?php
+include 'skip.inc';
+skipif(!function_exists('http_gzencode'), 'need zlib');
+?>
+--FILE--
+<?php
+echo "-TEST\n";
+
+error_reporting(E_ALL);
+
+$s = '';
+
+srand(time());
+for ($i = 0; $i < 1000000; $i++) {
+ $s .= chr(rand(0,255));
+}
+
+/* cannot test ext/zlib against this generated data,
+ because it will fail with such widely differing binary data */
+
+var_dump($s == http_gzdecode(http_gzencode($s)));
+var_dump($s == http_inflate(http_deflate($s)));
+var_dump($s == http_uncompress(http_compress($s)));
+
+
+$s = "A simple test string, which won't blow up ext/zlib.\n";
+
+var_dump($s == http_gzdecode(gzencode($s)));
+var_dump($s == http_inflate(gzdeflate($s)));
+var_dump($s == http_uncompress(gzcompress($s)));
+
+/* no gzdecode in ext/zlib
+var_dump($s == gzdecode(http_gzencode($s))); */
+var_dump($s == gzinflate(http_deflate($s)));
+var_dump($s == gzuncompress(http_compress($s)));
+
+if (extension_loaded('zlib')) {
+ (gzencode($s) == http_gzencode($s)) or print "GZIP Failed\n";
+ (gzdeflate($s) == http_deflate($s)) or print "DEFLATE Failed\n";
+ (gzcompress($s) == http_compress($s)) or print "COMPRESS Failed\n";
+}
+
+echo "Done\n";
+?>
+--EXPECTF--
+%sTEST
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+bool(true)
+Done