3
|
1 /* compress.c -- compress a memory buffer
|
|
2 * Copyright (C) 1995 Jean-loup Gailly.
|
|
3 * For conditions of distribution and use, see copyright notice in zlib.h
|
|
4 */
|
|
5
|
|
6 /* $Id: compress.c,v 1.1.1.1 1997/12/06 05:41:35 darius Exp $ */
|
|
7
|
|
8 #include "zlib.h"
|
|
9
|
|
10 /* ===========================================================================
|
|
11 Compresses the source buffer into the destination buffer. sourceLen is
|
|
12 the byte length of the source buffer. Upon entry, destLen is the total
|
|
13 size of the destination buffer, which must be at least 0.1% larger than
|
|
14 sourceLen plus 8 bytes. Upon exit, destLen is the actual size of the
|
|
15 compressed buffer.
|
|
16 This function can be used to compress a whole file at once if the
|
|
17 input file is mmap'ed.
|
|
18 compress returns Z_OK if success, Z_MEM_ERROR if there was not
|
|
19 enough memory, Z_BUF_ERROR if there was not enough room in the output
|
|
20 buffer.
|
|
21 */
|
|
22 int compress (dest, destLen, source, sourceLen)
|
|
23 Byte *dest;
|
|
24 uLong *destLen;
|
|
25 Byte *source;
|
|
26 uLong sourceLen;
|
|
27 {
|
|
28 z_stream stream;
|
|
29 int err;
|
|
30
|
|
31 stream.next_in = source;
|
|
32 stream.avail_in = (uInt)sourceLen;
|
|
33 /* Check for source > 64K on 16-bit machine: */
|
|
34 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
|
|
35
|
|
36 stream.next_out = dest;
|
|
37 stream.avail_out = (uInt)*destLen;
|
|
38 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
|
|
39
|
|
40 stream.zalloc = (alloc_func)0;
|
|
41 stream.zfree = (free_func)0;
|
|
42
|
|
43 err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
|
|
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 }
|