+inline STATUS http_verify_gzdecode_buffer(const char *data, size_t data_len, const char *decoded, size_t decoded_len, int error_level TSRMLS_DC)
+{
+ STATUS status = SUCCESS;
+ ulong len, cmp, crc;
+
+ crc = crc32(0L, Z_NULL, 0);
+ crc = crc32(crc, (const Bytef *) decoded, decoded_len);
+
+ cmp = (unsigned) ((data[data_len-8] & 0xFF));
+ cmp += (unsigned) ((data[data_len-7] & 0xFF) << 8);
+ cmp += (unsigned) ((data[data_len-6] & 0xFF) << 16);
+ cmp += (unsigned) ((data[data_len-5] & 0xFF) << 24);
+ len = (unsigned) ((data[data_len-4] & 0xFF));
+ len += (unsigned) ((data[data_len-3] & 0xFF) << 8);
+ len += (unsigned) ((data[data_len-2] & 0xFF) << 16);
+ len += (unsigned) ((data[data_len-1] & 0xFF) << 24);
+
+ if (cmp != crc) {
+ http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: CRC checksums do not match (%lu, %lu)", cmp, crc);
+ status = FAILURE;
+ }
+ if (len != decoded_len) {
+ http_error_ex(error_level TSRMLS_CC, HTTP_E_ENCODING, "Could not verify data integrity: data sizes do not match (%lu, %lu)", len, decoded_len);
+ status = FAILURE;
+ }
+ return status;
+}
+
+PHP_HTTP_API STATUS _http_encode(http_encoding_type type, int level, const char *data, size_t data_len, char **encoded, size_t *encoded_len TSRMLS_DC)
+{
+ STATUS status = SUCCESS;
+
+ switch (type)
+ {
+ case HTTP_ENCODING_ANY:
+ case HTTP_ENCODING_GZIP:
+ status = http_encoding_gzencode(level, data, data_len, encoded, encoded_len);
+ break;
+
+ case HTTP_ENCODING_DEFLATE:
+ status = http_encoding_deflate(level, data, data_len, encoded, encoded_len);
+ break;
+
+ case HTTP_ENCODING_COMPRESS:
+ status = http_encoding_compress(level, data, data_len, encoded, encoded_len);
+ break;
+
+ case HTTP_ENCODING_NONE:
+ default:
+ *encoded = estrndup(data, data_len);
+ *encoded_len = data_len;
+ break;
+ }
+
+ return status;
+}
+
+PHP_HTTP_API STATUS _http_decode(http_encoding_type type, const char *data, size_t data_len, char **decoded, size_t *decoded_len TSRMLS_DC)
+{
+ STATUS status = SUCCESS;
+
+ switch (type)
+ {
+ case HTTP_ENCODING_ANY:
+ if ( SUCCESS != http_encoding_gzdecode(data, data_len, decoded, decoded_len) &&
+ SUCCESS != http_encoding_inflate(data, data_len, decoded, decoded_len) &&
+ SUCCESS != http_encoding_uncompress(data, data_len, decoded, decoded_len)) {
+ status = FAILURE;
+ }
+ break;
+
+ case HTTP_ENCODING_GZIP:
+ status = http_encoding_gzdecode(data, data_len, decoded, decoded_len);
+ break;
+
+ case HTTP_ENCODING_DEFLATE:
+ status = http_encoding_inflate(data, data_len, decoded, decoded_len);
+ break;
+
+ case HTTP_ENCODING_COMPRESS:
+ status = http_encoding_uncompress(data, data_len, decoded, decoded_len);
+ break;
+
+ case HTTP_ENCODING_NONE:
+ default:
+ *decoded = estrndup(data, data_len);
+ *decoded_len = data_len;
+ break;
+ }
+
+ return status;
+}