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