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