annotate zlib/compress.c @ 3:5a977ccbc7a9 default tip

Empty changelog
author darius
date Sat, 06 Dec 1997 05:41:29 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
1 /* compress.c -- compress a memory buffer
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
2 * Copyright (C) 1995 Jean-loup Gailly.
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
4 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
5
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
6 /* $Id: compress.c,v 1.1.1.1 1997/12/06 05:41:35 darius Exp $ */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
7
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
8 #include "zlib.h"
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
9
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
10 /* ===========================================================================
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
11 Compresses the source buffer into the destination buffer. sourceLen is
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
12 the byte length of the source buffer. Upon entry, destLen is the total
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
13 size of the destination buffer, which must be at least 0.1% larger than
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
14 sourceLen plus 8 bytes. Upon exit, destLen is the actual size of the
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
15 compressed buffer.
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
16 This function can be used to compress a whole file at once if the
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
17 input file is mmap'ed.
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
18 compress returns Z_OK if success, Z_MEM_ERROR if there was not
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
19 enough memory, Z_BUF_ERROR if there was not enough room in the output
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
20 buffer.
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
21 */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
22 int compress (dest, destLen, source, sourceLen)
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
23 Byte *dest;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
24 uLong *destLen;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
25 Byte *source;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
26 uLong sourceLen;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
27 {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
28 z_stream stream;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
29 int err;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
30
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
31 stream.next_in = source;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
32 stream.avail_in = (uInt)sourceLen;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
33 /* Check for source > 64K on 16-bit machine: */
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
34 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
35
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
36 stream.next_out = dest;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
37 stream.avail_out = (uInt)*destLen;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
38 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
39
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
40 stream.zalloc = (alloc_func)0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
41 stream.zfree = (free_func)0;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
42
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
43 err = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
44 if (err != Z_OK) return err;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
45
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
46 err = deflate(&stream, Z_FINISH);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
47 if (err != Z_STREAM_END) {
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
48 deflateEnd(&stream);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
49 return err == Z_OK ? Z_BUF_ERROR : err;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
50 }
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
51 *destLen = stream.total_out;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
52
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
53 err = deflateEnd(&stream);
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
54 return err;
5a977ccbc7a9 Empty changelog
darius
parents:
diff changeset
55 }