abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
compress_.c
Go to the documentation of this file.
1 /* compress.c -- compress a memory buffer
2  * Copyright (C) 1995-2005 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 "zlib.h"
10 
12 
13 /* ===========================================================================
14  Compresses the source buffer into the destination buffer. The level
15  parameter has the same meaning as in deflateInit. sourceLen is the byte
16  length of the source buffer. Upon entry, destLen is the total size of the
17  destination buffer, which must be at least 0.1% larger than sourceLen plus
18  12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
19 
20  compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
21  memory, Z_BUF_ERROR if there was not enough room in the output buffer,
22  Z_STREAM_ERROR if the level parameter is invalid.
23 */
24 int ZEXPORT compress2 (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
25 {
26  z_stream stream;
27  int err;
28 
29  stream.next_in = (Bytef*)source;
30  stream.avail_in = (uInt)sourceLen;
31 #ifdef MAXSEG_64K
32  /* Check for source > 64K on 16-bit machine: */
33  if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
34 #endif
35  stream.next_out = dest;
36  stream.avail_out = (uInt)*destLen;
37  if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
38 
39  stream.zalloc = (alloc_func)0;
40  stream.zfree = (free_func)0;
41  stream.opaque = (voidpf)0;
42 
43  err = deflateInit(&stream, level);
44  if (err != Z_OK) return err;
45 
46  err = deflate(&stream, Z_FINISH);
47  if (err != Z_STREAM_END) {
48  deflateEnd(&stream);
49  return err == Z_OK ? Z_BUF_ERROR : err;
50  }
51  *destLen = stream.total_out;
52 
53  err = deflateEnd(&stream);
54  return err;
55 }
56 
57 /* ===========================================================================
58  */
59 int ZEXPORT compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
60 {
61  return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
62 }
63 
64 /* ===========================================================================
65  If the default memLevel or windowBits for deflateInit() is changed, then
66  this function needs to be updated.
67  */
69 {
70  return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
71  (sourceLen >> 25) + 13;
72 }
73 
74 
76 
Byte FAR * voidpf
Definition: zconf.h:355
Bytef * next_in
Definition: zlib.h:94
uInt avail_in
Definition: zlib.h:95
unsigned long uLong
Definition: zconf.h:336
int ZEXPORT deflateEnd(z_streamp strm)
Definition: deflate.c:866
Byte FAR Bytef
Definition: zconf.h:342
voidpf opaque
Definition: zlib.h:107
free_func zfree
Definition: zlib.h:106
#define Z_FINISH
Definition: zlib.h:176
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
#define Z_STREAM_END
Definition: zlib.h:182
alloc_func zalloc
Definition: zlib.h:105
uLong ZEXPORT compressBound(uLong sourceLen)
Definition: compress_.c:68
Bytef * next_out
Definition: zlib.h:98
int ZEXPORT compress(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
Definition: compress_.c:59
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:555
ABC_NAMESPACE_IMPL_START int ZEXPORT compress2(Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
Definition: compress_.c:24
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
uLong total_out
Definition: zlib.h:100
uLong FAR uLongf
Definition: zconf.h:347
#define Z_BUF_ERROR
Definition: zlib.h:188
#define deflateInit(strm, level)
Definition: zlib.h:1554
uInt avail_out
Definition: zlib.h:99
#define Z_OK
Definition: zlib.h:181
#define ZEXPORT
Definition: zconf.h:322
#define Z_DEFAULT_COMPRESSION
Definition: zlib.h:197
unsigned int uInt
Definition: zconf.h:335