ยปCore Development>Code coverage>Modules/zlib/compress.c

Python code coverage for Modules/zlib/compress.c

#countcontent
1n/a/* compress.c -- compress a memory buffer
2n/a * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler
3n/a * For conditions of distribution and use, see copyright notice in zlib.h
4n/a */
5n/a
6n/a/* @(#) $Id$ */
7n/a
8n/a#define ZLIB_INTERNAL
9n/a#include "zlib.h"
10n/a
11n/a/* ===========================================================================
12n/a Compresses the source buffer into the destination buffer. The level
13n/a parameter has the same meaning as in deflateInit. sourceLen is the byte
14n/a length of the source buffer. Upon entry, destLen is the total size of the
15n/a destination buffer, which must be at least 0.1% larger than sourceLen plus
16n/a 12 bytes. Upon exit, destLen is the actual size of the compressed buffer.
17n/a
18n/a compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
19n/a memory, Z_BUF_ERROR if there was not enough room in the output buffer,
20n/a Z_STREAM_ERROR if the level parameter is invalid.
21n/a*/
22n/aint ZEXPORT compress2 (dest, destLen, source, sourceLen, level)
23n/a Bytef *dest;
24n/a uLongf *destLen;
25n/a const Bytef *source;
26n/a uLong sourceLen;
27n/a int level;
28n/a{
29n/a z_stream stream;
30n/a int err;
31n/a const uInt max = (uInt)-1;
32n/a uLong left;
33n/a
34n/a left = *destLen;
35n/a *destLen = 0;
36n/a
37n/a stream.zalloc = (alloc_func)0;
38n/a stream.zfree = (free_func)0;
39n/a stream.opaque = (voidpf)0;
40n/a
41n/a err = deflateInit(&stream, level);
42n/a if (err != Z_OK) return err;
43n/a
44n/a stream.next_out = dest;
45n/a stream.avail_out = 0;
46n/a stream.next_in = (z_const Bytef *)source;
47n/a stream.avail_in = 0;
48n/a
49n/a do {
50n/a if (stream.avail_out == 0) {
51n/a stream.avail_out = left > (uLong)max ? max : (uInt)left;
52n/a left -= stream.avail_out;
53n/a }
54n/a if (stream.avail_in == 0) {
55n/a stream.avail_in = sourceLen > (uLong)max ? max : (uInt)sourceLen;
56n/a sourceLen -= stream.avail_in;
57n/a }
58n/a err = deflate(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH);
59n/a } while (err == Z_OK);
60n/a
61n/a *destLen = stream.total_out;
62n/a deflateEnd(&stream);
63n/a return err == Z_STREAM_END ? Z_OK : err;
64n/a}
65n/a
66n/a/* ===========================================================================
67n/a */
68n/aint ZEXPORT compress (dest, destLen, source, sourceLen)
69n/a Bytef *dest;
70n/a uLongf *destLen;
71n/a const Bytef *source;
72n/a uLong sourceLen;
73n/a{
74n/a return compress2(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION);
75n/a}
76n/a
77n/a/* ===========================================================================
78n/a If the default memLevel or windowBits for deflateInit() is changed, then
79n/a this function needs to be updated.
80n/a */
81n/auLong ZEXPORT compressBound (sourceLen)
82n/a uLong sourceLen;
83n/a{
84n/a return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) +
85n/a (sourceLen >> 25) + 13;
86n/a}