annotate lib/zlib/compress.c @ 754:5ba796adbf5e

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