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