CCompressionProcessor::EStatus CZipCompressor::Init(void) { if ( IsBusy() ) { // Abnormal previous session termination End(); } // Initialize members Reset(); SetBusy(); m_CRC32 = 0; m_NeedWriteHeader = true; m_Cache.erase(); // Initialize the compressor stream structure memset(STREAM, 0, sizeof(z_stream)); // Create a compressor stream int errcode = deflateInit2_(STREAM, GetLevel(), Z_DEFLATED, F_ISSET(fWriteGZipFormat) ? -m_WindowBits : m_WindowBits, m_MemLevel, m_Strategy, ZLIB_VERSION, (int)sizeof(z_stream)); SetError(errcode, zError(errcode)); if ( errcode == Z_OK ) { return eStatus_Success; } ERR_COMPRESS(60, FormatErrorMessage("CZipCompressor::Init", GetProcessedSize())); return eStatus_Error; }
long CZipCompression::EstimateCompressionBufferSize(size_t src_len) { #if !defined(ZLIB_VERNUM) || ZLIB_VERNUM < 0x1200 return -1; #else size_t header_len = 0; int errcode = Z_OK; if ( F_ISSET(fWriteGZipFormat) ) { // Default empty GZIP header header_len = 10; } STREAM->zalloc = (alloc_func)0; STREAM->zfree = (free_func)0; STREAM->opaque = (voidpf)0; errcode = deflateInit2_(STREAM, GetLevel(), Z_DEFLATED, header_len ? -m_WindowBits : m_WindowBits, m_MemLevel, m_Strategy, ZLIB_VERSION, (int)sizeof(z_stream)); if (errcode != Z_OK) { SetError(errcode, zError(errcode)); return -1; } long n = (long)(deflateBound(STREAM, (unsigned long)src_len) + header_len); deflateEnd(STREAM); return n; #endif }
/* ========================================================================= */ EXPORT_C int ZEXPORT deflateInit_( z_streamp strm, int level, const char *version, int stream_size) { return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, version, stream_size); /* To do: ignore strm->next_in if we use it as window */ }
static TACommandVerdict deflateInit2__cmd(TAThread thread,TAInputStream stream) { z_stream strm; int level, method, windowBits, memLevel, strategy, stream_size, res, is_null; char* version; is_null = readZStream(&stream, &strm); level = readInt(&stream); method = readInt(&stream); windowBits = readInt(&stream); memLevel = readInt(&stream); strategy = readInt(&stream); version = readString(&stream); stream_size = readInt(&stream); START_TARGET_OPERATION(thread); if(!is_null) res = deflateInit2_(&strm, level, method, windowBits, memLevel, strategy, version, stream_size); else res = deflateInit2_(0, level, method, windowBits, memLevel, strategy, version, stream_size); END_TARGET_OPERATION(thread); // Response if(!is_null) writeZStream(thread, &strm); else writeZStream(thread, 0); writeInt(thread, res); sendResponse(thread); return taDefaultVerdict; }
int gmio_zlib_compress_init( struct z_stream_s* z_stream, const struct gmio_zlib_compress_options* z_opts) { const int zlib_compress_level = gmio_to_zlib_compress_level(z_opts->level); const int zlib_compress_memusage = gmio_to_zlib_compress_memusage(z_opts->memory_usage); const int z_init_error = deflateInit2_( z_stream, zlib_compress_level, Z_DEFLATED, /* Method */ z_window_bits_for_no_zlib_wrapper, zlib_compress_memusage, z_opts->strategy, ZLIB_VERSION, sizeof(struct z_stream_s)); return zlib_error_to_gmio_error(z_init_error); }
bool CZipCompression::CompressBuffer( const void* src_buf, size_t src_len, void* dst_buf, size_t dst_size, /* out */ size_t* dst_len) { *dst_len = 0; // Check parameters if ( !src_len ) { if (!F_ISSET(fAllowEmptyData)) { src_buf = NULL; } } if ( !src_buf ) { SetError(Z_STREAM_ERROR, "bad argument"); ERR_COMPRESS(48, FormatErrorMessage("CZipCompression::CompressBuffer")); return false; } if ( !dst_buf || !dst_len ) { SetError(Z_STREAM_ERROR, "bad argument"); ERR_COMPRESS(48, FormatErrorMessage("CZipCompression::CompressBuffer")); return false; } if (src_len > kMax_UInt) { SetError(Z_STREAM_ERROR, "size of the source buffer is too big"); ERR_COMPRESS(49, FormatErrorMessage("CZipCompression::CompressBuffer")); return false; } LIMIT_SIZE_PARAM_U(dst_size); size_t header_len = 0; int errcode = Z_OK; // Write gzip file header if ( F_ISSET(fWriteGZipFormat) ) { header_len = s_WriteGZipHeader(dst_buf, dst_size); if (!header_len) { SetError(Z_STREAM_ERROR, "Cannot write gzip header"); ERR_COMPRESS(50, FormatErrorMessage("CZipCompression::CompressBuffer")); return false; } } STREAM->next_in = (unsigned char*)src_buf; STREAM->avail_in = (unsigned int)src_len; #ifdef MAXSEG_64K // Check for source > 64K on 16-bit machine: if ( STREAM->avail_in != src_len ) { SetError(Z_BUF_ERROR, zError(Z_BUF_ERROR)); ERR_COMPRESS(51, FormatErrorMessage("CZipCompression::CompressBuffer")); return false; } #endif STREAM->next_out = (unsigned char*)dst_buf + header_len; STREAM->avail_out = (unsigned int)(dst_size - header_len); if ( STREAM->avail_out != dst_size - header_len ) { SetError(Z_BUF_ERROR, zError(Z_BUF_ERROR)); ERR_COMPRESS(52, FormatErrorMessage("CZipCompression::CompressBuffer")); return false; } STREAM->zalloc = (alloc_func)0; STREAM->zfree = (free_func)0; STREAM->opaque = (voidpf)0; errcode = deflateInit2_(STREAM, GetLevel(), Z_DEFLATED, header_len ? -m_WindowBits : m_WindowBits, m_MemLevel, m_Strategy, ZLIB_VERSION, (int)sizeof(z_stream)); if (errcode == Z_OK) { errcode = deflate(STREAM, Z_FINISH); *dst_len = STREAM->total_out + header_len; if (errcode == Z_STREAM_END) { errcode = deflateEnd(STREAM); } else { if ( errcode == Z_OK ) { errcode = Z_BUF_ERROR; } deflateEnd(STREAM); } } SetError(errcode, zError(errcode)); if ( errcode != Z_OK ) { ERR_COMPRESS(53, FormatErrorMessage("CZipCompression::CompressBuffer")); return false; } // Write gzip file footer if ( F_ISSET(fWriteGZipFormat) ) { unsigned long crc = crc32(0L, (unsigned char*)src_buf, (unsigned int)src_len); size_t footer_len = s_WriteGZipFooter( (char*)dst_buf + *dst_len, dst_size, (unsigned long)src_len, crc); if ( !footer_len ) { SetError(-1, "Cannot write gzip footer"); ERR_COMPRESS(54, FormatErrorMessage("CZipCompressor::CompressBuffer")); return false; } *dst_len += footer_len; } return true; }
/* * Compress blocks with zlib */ int zzip(void *dst, unsigned long *lenp, unsigned char *src, unsigned long srclen, int stoponerr, int (*func)(unsigned long, unsigned long)) { z_stream s; int r, flush, orig, window; unsigned long comp_len, left_len; if (!srclen) return 0; #ifndef CONFIG_GZIP window = MAX_WBITS; #else window = 2 * MAX_WBITS; #endif orig = *lenp; s.zalloc = zalloc; s.zfree = zfree; s.opaque = Z_NULL; r = deflateInit2_(&s, Z_BEST_SPEED, Z_DEFLATED, window, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, ZLIB_VERSION, sizeof(z_stream)); if (r != Z_OK) { printf ("Error: deflateInit2_() returned %d\n", r); return -1; } while (srclen > 0) { comp_len = (srclen > CONFIG_GZIP_COMPRESS_DEF_SZ) ? CONFIG_GZIP_COMPRESS_DEF_SZ : srclen; s.next_in = src; s.avail_in = comp_len; flush = (srclen > CONFIG_GZIP_COMPRESS_DEF_SZ)? Z_NO_FLUSH : Z_FINISH; do { left_len = (*lenp > CONFIG_GZIP_COMPRESS_DEF_SZ) ? CONFIG_GZIP_COMPRESS_DEF_SZ : *lenp; s.next_out = dst; s.avail_out = left_len; r = deflate(&s, flush); if (r == Z_STREAM_ERROR && stoponerr == 1) { printf("Error: deflate() returned %d\n", r); r = -1; goto bail; } if (!func) { dst += (left_len - s.avail_out); *lenp -= (left_len - s.avail_out); } else if (left_len - s.avail_out > 0) { r = func((unsigned long)dst, left_len - s.avail_out); if (r < 0) goto bail; } } while (s.avail_out == 0 && (*lenp > 0)); if (s.avail_in) { printf("Deflate failed to consume %u bytes", s.avail_in); r = -1; goto bail; } if (*lenp == 0) { printf("Deflate need more space to compress " "left %lu bytes\n", srclen); r = -1; goto bail; } srclen -= comp_len; src += comp_len; } r = 0; bail: deflateEnd(&s); *lenp = orig - *lenp; return r; }
int main(int argc, char *argv[]) { unsigned char *buf = ssh_xmalloc(BUFSIZE); unsigned char *buf2 = ssh_xmalloc(BUFSIZE); unsigned char *buf3 = ssh_xmalloc(BUFSIZE); int buf2len; int i; int status; z_stream s_deflate; z_stream s_inflate; for (i = 0; i < BUFSIZE; i++) #if 0 buf[i] = (unsigned char) ssh_random_get_byte(); #else buf[i] = (unsigned char) i; #endif memset(&s_deflate, 0, sizeof(s_deflate)); status = deflateInit2_(&s_deflate, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -11, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, ZLIB_VERSION, sizeof(z_stream)); SSH_ASSERT(status == Z_OK); memset(&s_inflate, 0, sizeof(s_inflate)); status = inflateInit2_(&s_inflate, -15, ZLIB_VERSION, sizeof(z_stream)); SSH_ASSERT(status == Z_OK); for (i = 0; i < BUFSIZE; i++) { s_deflate.next_in = buf; s_deflate.avail_in = i; s_deflate.next_out = buf2; s_deflate.avail_out = BUFSIZE; status = deflate(&s_deflate, Z_FINISH); if (status != Z_STREAM_END) ssh_fatal("status=%d", status); buf2len = s_deflate.total_out; status = deflateReset(&s_deflate); SSH_ASSERT(status == Z_OK); s_inflate.next_in = buf2; s_inflate.avail_in = buf2len; s_inflate.next_out = buf3; s_inflate.avail_out = BUFSIZE; status = inflate(&s_inflate, Z_FINISH); if (status != Z_STREAM_END) ssh_warning("status=%d", status); if (buf2len < i || 1) printf("inlen=%d, outlen=%d, inlen=%lu, %.2f\n", i, buf2len, (unsigned long) s_inflate.total_out, (double) buf2len / i); #if 0 SSH_ASSERT(i == s_inflate.total_out); #endif if (memcmp(buf, buf3, i) != 0) ssh_warning("output differs"); status = inflateReset(&s_inflate); SSH_ASSERT(status == Z_OK); } return 0; }