nuclear@0: /* compress.c -- compress a memory buffer nuclear@0: * Copyright (C) 1995-2003 Jean-loup Gailly. nuclear@0: * For conditions of distribution and use, see copyright notice in zlib.h nuclear@0: */ nuclear@0: nuclear@0: /* @(#) $Id$ */ nuclear@0: nuclear@0: #define ZLIB_INTERNAL nuclear@0: #include "zlib.h" nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: Compresses the source buffer into the destination buffer. The level nuclear@0: parameter has the same meaning as in deflateInit. sourceLen is the byte nuclear@0: length of the source buffer. Upon entry, destLen is the total size of the nuclear@0: destination buffer, which must be at least 0.1% larger than sourceLen plus nuclear@0: 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. nuclear@0: nuclear@0: compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough nuclear@0: memory, Z_BUF_ERROR if there was not enough room in the output buffer, nuclear@0: Z_STREAM_ERROR if the level parameter is invalid. nuclear@0: */ nuclear@0: int ZEXPORT compress2 (dest, destLen, source, sourceLen, level) nuclear@0: Bytef *dest; nuclear@0: uLongf *destLen; nuclear@0: const Bytef *source; nuclear@0: uLong sourceLen; nuclear@0: int level; nuclear@0: { nuclear@0: z_stream stream; nuclear@0: int err; nuclear@0: nuclear@0: stream.next_in = (Bytef*)source; nuclear@0: stream.avail_in = (uInt)sourceLen; nuclear@0: #ifdef MAXSEG_64K nuclear@0: /* Check for source > 64K on 16-bit machine: */ nuclear@0: if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; nuclear@0: #endif nuclear@0: stream.next_out = dest; nuclear@0: stream.avail_out = (uInt)*destLen; nuclear@0: if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; nuclear@0: nuclear@0: stream.zalloc = (alloc_func)0; nuclear@0: stream.zfree = (free_func)0; nuclear@0: stream.opaque = (voidpf)0; nuclear@0: nuclear@0: err = deflateInit(&stream, level); nuclear@0: if (err != Z_OK) return err; nuclear@0: nuclear@0: err = deflate(&stream, Z_FINISH); nuclear@0: if (err != Z_STREAM_END) { nuclear@0: deflateEnd(&stream); nuclear@0: return err == Z_OK ? Z_BUF_ERROR : err; nuclear@0: } nuclear@0: *destLen = stream.total_out; nuclear@0: nuclear@0: err = deflateEnd(&stream); nuclear@0: return err; nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: */ nuclear@0: int ZEXPORT compress (dest, destLen, source, sourceLen) nuclear@0: Bytef *dest; nuclear@0: uLongf *destLen; nuclear@0: const Bytef *source; nuclear@0: uLong sourceLen; nuclear@0: { nuclear@0: return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); nuclear@0: } nuclear@0: nuclear@0: /* =========================================================================== nuclear@0: If the default memLevel or windowBits for deflateInit() is changed, then nuclear@0: this function needs to be updated. nuclear@0: */ nuclear@0: uLong ZEXPORT compressBound (sourceLen) nuclear@0: uLong sourceLen; nuclear@0: { nuclear@0: return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11; nuclear@0: }