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