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