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