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