annotate lib/zlib/compress.c @ 193:269ca53dae41

24.12.12
author Ritor1
date Mon, 24 Dec 2012 10:50:04 +0600
parents 8b8875f5b359
children
rev   line source
0
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
1 /* compress.c -- compress a memory buffer
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
2 * Copyright (C) 1995-1998 Jean-loup Gailly.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
4 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
5
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
6 /* @(#) $Id$ */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
7
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
8 #include "zlib.h"
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
9
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
10 /* ===========================================================================
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
11 Compresses the source buffer into the destination buffer. The level
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
12 parameter has the same meaning as in deflateInit. sourceLen is the byte
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
13 length of the source buffer. Upon entry, destLen is the total size of the
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
14 destination buffer, which must be at least 0.1% larger than sourceLen plus
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
15 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
16
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
17 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
18 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
19 Z_STREAM_ERROR if the level parameter is invalid.
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
20 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
21 int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
22 Bytef *dest;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
23 uLongf *destLen;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
24 const Bytef *source;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
25 uLong sourceLen;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
26 int level;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
27 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
28 z_stream stream;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
29 int err;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
30
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
31 stream.next_in = (Bytef*)source;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
32 stream.avail_in = (uInt)sourceLen;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
33 #ifdef MAXSEG_64K
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
34 /* Check for source > 64K on 16-bit machine: */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
35 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
36 #endif
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
37 stream.next_out = dest;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
38 stream.avail_out = (uInt)*destLen;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
39 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
40
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
41 stream.zalloc = (alloc_func)0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
42 stream.zfree = (free_func)0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
43 stream.opaque = (voidpf)0;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
44
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
45 err = deflateInit(&stream, level);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
46 if (err != Z_OK) return err;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
47
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
48 err = deflate(&stream, Z_FINISH);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
49 if (err != Z_STREAM_END) {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
50 deflateEnd(&stream);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
51 return err == Z_OK ? Z_BUF_ERROR : err;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
52 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
53 *destLen = stream.total_out;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
54
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
55 err = deflateEnd(&stream);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
56 return err;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
57 }
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
58
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
59 /* ===========================================================================
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
60 */
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
61 int ZEXPORT compress (dest, destLen, source, sourceLen)
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
62 Bytef *dest;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
63 uLongf *destLen;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
64 const Bytef *source;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
65 uLong sourceLen;
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
66 {
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
67 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
8b8875f5b359 Initial commit
Nomad
parents:
diff changeset
68 }