annotate Modules/zlib/compress.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 81fa47d2d477
children 467019588fcc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36186
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
1 /* compress.c -- compress a memory buffer
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
2 * Copyright (C) 1995-2003 Jean-loup Gailly.
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
3 * For conditions of distribution and use, see copyright notice in zlib.h
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
4 */
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
5
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
6 /* @(#) $Id$ */
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
7
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
8 #define ZLIB_INTERNAL
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
9 #include "zlib.h"
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
10
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
11 /* ===========================================================================
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
12 Compresses the source buffer into the destination buffer. The level
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
13 parameter has the same meaning as in deflateInit. sourceLen is the byte
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
14 length of the source buffer. Upon entry, destLen is the total size of the
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
15 destination buffer, which must be at least 0.1% larger than sourceLen plus
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
16 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
17
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
18 compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
19 memory, Z_BUF_ERROR if there was not enough room in the output buffer,
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
20 Z_STREAM_ERROR if the level parameter is invalid.
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
21 */
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
22 int ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
23 Bytef *dest;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
24 uLongf *destLen;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
25 const Bytef *source;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
26 uLong sourceLen;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
27 int level;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
28 {
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
29 z_stream stream;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
30 int err;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
31
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
32 stream.next_in = (Bytef*)source;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
33 stream.avail_in = (uInt)sourceLen;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
34 #ifdef MAXSEG_64K
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
35 /* Check for source > 64K on 16-bit machine: */
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
36 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
37 #endif
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
38 stream.next_out = dest;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
39 stream.avail_out = (uInt)*destLen;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
40 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
41
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
42 stream.zalloc = (alloc_func)0;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
43 stream.zfree = (free_func)0;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
44 stream.opaque = (voidpf)0;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
45
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
46 err = deflateInit(&stream, level);
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
47 if (err != Z_OK) return err;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
48
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
49 err = deflate(&stream, Z_FINISH);
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
50 if (err != Z_STREAM_END) {
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
51 deflateEnd(&stream);
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
52 return err == Z_OK ? Z_BUF_ERROR : err;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
53 }
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
54 *destLen = stream.total_out;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
55
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
56 err = deflateEnd(&stream);
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
57 return err;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
58 }
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
59
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
60 /* ===========================================================================
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
61 */
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
62 int ZEXPORT compress (dest, destLen, source, sourceLen)
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
63 Bytef *dest;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
64 uLongf *destLen;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
65 const Bytef *source;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
66 uLong sourceLen;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
67 {
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
68 return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
69 }
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
70
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
71 /* ===========================================================================
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
72 If the default memLevel or windowBits for deflateInit() is changed, then
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
73 this function needs to be updated.
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
74 */
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
75 uLong ZEXPORT compressBound (sourceLen)
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
76 uLong sourceLen;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
77 {
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
78 return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + 11;
81fa47d2d477 Import zlib into Python tree.
Martin v. Löwis <martin@v.loewis.de>
parents:
diff changeset
79 }