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