compress.c revision 205194
192986Sobrien/* compress.c -- compress a memory buffer 292986Sobrien * Copyright (C) 1995-2005 Jean-loup Gailly. 392986Sobrien * For conditions of distribution and use, see copyright notice in zlib.h 462856Sdcs */ 562856Sdcs 662856Sdcs/* @(#) $Id$ */ 762856Sdcs 862856Sdcs#define ZLIB_INTERNAL 962856Sdcs#include "zlib.h" 1062856Sdcs 1162856Sdcs/* =========================================================================== 1262856Sdcs Compresses the source buffer into the destination buffer. The level 1362856Sdcs parameter has the same meaning as in deflateInit. sourceLen is the byte 1462856Sdcs length of the source buffer. Upon entry, destLen is the total size of the 1562856Sdcs destination buffer, which must be at least 0.1% larger than sourceLen plus 1662856Sdcs 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. 1762856Sdcs 1862856Sdcs compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough 1962856Sdcs memory, Z_BUF_ERROR if there was not enough room in the output buffer, 2062856Sdcs Z_STREAM_ERROR if the level parameter is invalid. 2162856Sdcs*/ 2262856Sdcsint ZEXPORT compress2 (dest, destLen, source, sourceLen, level) 2362856Sdcs Bytef *dest; 2462856Sdcs uLongf *destLen; 2562856Sdcs const Bytef *source; 2662856Sdcs uLong sourceLen; 2762856Sdcs int level; 2862856Sdcs{ 2962856Sdcs z_stream stream; 3062856Sdcs int err; 3162856Sdcs 3262856Sdcs stream.next_in = (Bytef*)source; 3362856Sdcs stream.avail_in = (uInt)sourceLen; 3462856Sdcs#ifdef MAXSEG_64K 3562856Sdcs /* Check for source > 64K on 16-bit machine: */ 3662856Sdcs if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 3762856Sdcs#endif 3862856Sdcs stream.next_out = dest; 3962856Sdcs stream.avail_out = (uInt)*destLen; 4062856Sdcs if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 4192889Sobrien 4262856Sdcs stream.zalloc = (alloc_func)0; 4362856Sdcs stream.zfree = (free_func)0; 4462856Sdcs stream.opaque = (voidpf)0; 4562856Sdcs 4662856Sdcs err = deflateInit(&stream, level); 47176380Skevlo if (err != Z_OK) return err; 4862856Sdcs 4962856Sdcs err = deflate(&stream, Z_FINISH); 5062856Sdcs if (err != Z_STREAM_END) { 5162856Sdcs deflateEnd(&stream); 5262856Sdcs return err == Z_OK ? Z_BUF_ERROR : err; 5362856Sdcs } 5462856Sdcs *destLen = stream.total_out; 5562856Sdcs 5662856Sdcs err = deflateEnd(&stream); 5762856Sdcs return err; 5862856Sdcs} 5962856Sdcs 6062856Sdcs/* =========================================================================== 6162856Sdcs */ 6262856Sdcsint ZEXPORT compress (dest, destLen, source, sourceLen) 6362856Sdcs Bytef *dest; 6462856Sdcs uLongf *destLen; 6562856Sdcs const Bytef *source; 6662856Sdcs uLong sourceLen; 6762856Sdcs{ 6862856Sdcs return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); 6962856Sdcs} 7062856Sdcs 7162856Sdcs/* =========================================================================== 7262856Sdcs If the default memLevel or windowBits for deflateInit() is changed, then 7362856Sdcs this function needs to be updated. 7462856Sdcs */ 7562856SdcsuLong ZEXPORT compressBound (sourceLen) 7662856Sdcs uLong sourceLen; 7762856Sdcs{ 7862856Sdcs return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 7962856Sdcs (sourceLen >> 25) + 13; 8062856Sdcs} 8162856Sdcs