1*44bedb31SLionel Sambuc /* $NetBSD: compress.c,v 1.2 2006/01/27 00:45:27 christos Exp $ */
2*44bedb31SLionel Sambuc
3*44bedb31SLionel Sambuc /* compress.c -- compress a memory buffer
4*44bedb31SLionel Sambuc * Copyright (C) 1995-2003 Jean-loup Gailly.
5*44bedb31SLionel Sambuc * For conditions of distribution and use, see copyright notice in zlib.h
6*44bedb31SLionel Sambuc */
7*44bedb31SLionel Sambuc
8*44bedb31SLionel Sambuc /* @(#) Id */
9*44bedb31SLionel Sambuc
10*44bedb31SLionel Sambuc #define ZLIB_INTERNAL
11*44bedb31SLionel Sambuc #include "zlib.h"
12*44bedb31SLionel Sambuc
13*44bedb31SLionel Sambuc /* ===========================================================================
14*44bedb31SLionel Sambuc Compresses the source buffer into the destination buffer. The level
15*44bedb31SLionel Sambuc parameter has the same meaning as in deflateInit. sourceLen is the byte
16*44bedb31SLionel Sambuc length of the source buffer. Upon entry, destLen is the total size of the
17*44bedb31SLionel Sambuc destination buffer, which must be at least 0.1% larger than sourceLen plus
18*44bedb31SLionel Sambuc 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
19*44bedb31SLionel Sambuc
20*44bedb31SLionel Sambuc compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
21*44bedb31SLionel Sambuc memory, Z_BUF_ERROR if there was not enough room in the output buffer,
22*44bedb31SLionel Sambuc Z_STREAM_ERROR if the level parameter is invalid.
23*44bedb31SLionel Sambuc */
compress2(dest,destLen,source,sourceLen,level)24*44bedb31SLionel Sambuc int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
25*44bedb31SLionel Sambuc Bytef *dest;
26*44bedb31SLionel Sambuc uLongf *destLen;
27*44bedb31SLionel Sambuc const Bytef *source;
28*44bedb31SLionel Sambuc uLong sourceLen;
29*44bedb31SLionel Sambuc int level;
30*44bedb31SLionel Sambuc {
31*44bedb31SLionel Sambuc z_stream stream;
32*44bedb31SLionel Sambuc int err;
33*44bedb31SLionel Sambuc
34*44bedb31SLionel Sambuc stream.next_in = __UNCONST(source);
35*44bedb31SLionel Sambuc stream.avail_in = (uInt)sourceLen;
36*44bedb31SLionel Sambuc #ifdef MAXSEG_64K
37*44bedb31SLionel Sambuc /* Check for source > 64K on 16-bit machine: */
38*44bedb31SLionel Sambuc if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
39*44bedb31SLionel Sambuc #endif
40*44bedb31SLionel Sambuc stream.next_out = dest;
41*44bedb31SLionel Sambuc stream.avail_out = (uInt)*destLen;
42*44bedb31SLionel Sambuc if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
43*44bedb31SLionel Sambuc
44*44bedb31SLionel Sambuc stream.zalloc = (alloc_func)0;
45*44bedb31SLionel Sambuc stream.zfree = (free_func)0;
46*44bedb31SLionel Sambuc stream.opaque = (voidpf)0;
47*44bedb31SLionel Sambuc
48*44bedb31SLionel Sambuc err = deflateInit(&stream, level);
49*44bedb31SLionel Sambuc if (err != Z_OK) return err;
50*44bedb31SLionel Sambuc
51*44bedb31SLionel Sambuc err = deflate(&stream, Z_FINISH);
52*44bedb31SLionel Sambuc if (err != Z_STREAM_END) {
53*44bedb31SLionel Sambuc deflateEnd(&stream);
54*44bedb31SLionel Sambuc return err == Z_OK ? Z_BUF_ERROR : err;
55*44bedb31SLionel Sambuc }
56*44bedb31SLionel Sambuc *destLen = stream.total_out;
57*44bedb31SLionel Sambuc
58*44bedb31SLionel Sambuc err = deflateEnd(&stream);
59*44bedb31SLionel Sambuc return err;
60*44bedb31SLionel Sambuc }
61*44bedb31SLionel Sambuc
62*44bedb31SLionel Sambuc /* ===========================================================================
63*44bedb31SLionel Sambuc */
compress(dest,destLen,source,sourceLen)64*44bedb31SLionel Sambuc int ZEXPORT compress (dest, destLen, source, sourceLen)
65*44bedb31SLionel Sambuc Bytef *dest;
66*44bedb31SLionel Sambuc uLongf *destLen;
67*44bedb31SLionel Sambuc const Bytef *source;
68*44bedb31SLionel Sambuc uLong sourceLen;
69*44bedb31SLionel Sambuc {
70*44bedb31SLionel Sambuc return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
71*44bedb31SLionel Sambuc }
72*44bedb31SLionel Sambuc
73*44bedb31SLionel Sambuc /* ===========================================================================
74*44bedb31SLionel Sambuc If the default memLevel or windowBits for deflateInit() is changed, then
75*44bedb31SLionel Sambuc this function needs to be updated.
76*44bedb31SLionel Sambuc */
compressBound(sourceLen)77*44bedb31SLionel Sambuc uLong ZEXPORT compressBound (sourceLen)
78*44bedb31SLionel Sambuc uLong sourceLen;
79*44bedb31SLionel Sambuc {
80*44bedb31SLionel Sambuc return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
81*44bedb31SLionel Sambuc }
82