avs_stream_abstract_t * _avs_http_create_decompressor(http_compression_format_t format, int window_bits, size_t input_buffer_size, size_t output_buffer_size) { int result; zlib_stream_t *stream = zlib_stream_init(&decompressor_vtable, input_buffer_size, output_buffer_size); if (!stream) { return NULL; } stream->zlib.opaque = &decompressor_flush_holder; result = inflateInit2( &stream->zlib, window_bits + (format == HTTP_COMPRESSION_GZIP ? 16 : 0)); if (result != Z_OK) { LOG(ERROR, "could not initialize zlib (%d): %s", result, get_zlib_msg(stream)); avs_free(stream); return NULL; } reset_fields(stream); return (avs_stream_abstract_t *) stream; }
/** * Creates an incremental zlib deflater for `len' bytes starting at `data', * with specified compression `level'. * * @param data data to compress; if NULL, will be incrementally given * @param len length of data to compress (if data not NULL) or estimation * @param dest where compressed data should go, or NULL if allocated * @param destlen length of supplied output buffer, if dest != NULL * @param level compression level, between 0 and 9. * * @return new deflater, or NULL if error. */ static zlib_deflater_t * zlib_deflater_alloc( const void *data, size_t len, void *dest, size_t destlen, int level) { zlib_deflater_t *zd; z_streamp outz; int ret; g_assert(size_is_non_negative(len)); g_assert(size_is_non_negative(destlen)); g_assert(level == Z_DEFAULT_COMPRESSION || (level >= 0 && level <= 9)); WALLOC(outz); outz->zalloc = zlib_alloc_func; outz->zfree = zlib_free_func; outz->opaque = NULL; ret = deflateInit(outz, level); if (ret != Z_OK) { WFREE(outz); g_carp("%s(): unable to initialize compressor: %s", G_STRFUNC, zlib_strerror(ret)); return NULL; } WALLOC0(zd); zd->zs.magic = ZLIB_DEFLATER_MAGIC; zd->zs.z = outz; zd->zs.closed = FALSE; zlib_stream_init(&zd->zs, data, len, dest, destlen); return zd; }
/** * Reset an incremental zlib stream. * * The compression parameters remain the same as the ones used when the * deflater was initially created. Only the input/output settings change. * * @param zd the zlib deflater to reset * @param data data to compress; if NULL, will be incrementally given * @param len length of data to compress (if data not NULL) or estimation * @param dest where compressed data should go, or NULL if allocated * @param destlen length of supplied output buffer, if dest != NULL */ static void zlib_stream_reset_into(zlib_stream_t *zs, const void *data, size_t len, void *dest, size_t destlen) { g_assert(zs->z != NULL); /* Stream not closed yet */ zs->closed = FALSE; switch (zs->magic) { case ZLIB_INFLATER_MAGIC: inflateReset(zs->z); break; case ZLIB_DEFLATER_MAGIC: deflateReset(zs->z); break; } zlib_stream_init(zs, data, len, dest, destlen); }