/src/c-blosc2/internal-complibs/zlib-ng-2.0.7/compress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* compress.c -- compress a memory buffer |
2 | | * Copyright (C) 1995-2005, 2014, 2016 Jean-loup Gailly, Mark Adler |
3 | | * For conditions of distribution and use, see copyright notice in zlib.h |
4 | | */ |
5 | | |
6 | | #include "zbuild.h" |
7 | | #include "zutil.h" |
8 | | #if defined(ZLIB_COMPAT) |
9 | | # include "zlib.h" |
10 | | #else |
11 | | # include "zlib-ng.h" |
12 | | #endif |
13 | | |
14 | | /* =========================================================================== |
15 | | * Architecture-specific hooks. |
16 | | */ |
17 | | #ifdef S390_DFLTCC_DEFLATE |
18 | | # include "arch/s390/dfltcc_common.h" |
19 | | #else |
20 | | /* Returns the upper bound on compressed data length based on uncompressed data length, assuming default settings. |
21 | | * Zero means that arch-specific deflation code behaves identically to the regular zlib-ng algorithms. */ |
22 | 0 | # define DEFLATE_BOUND_COMPLEN(source_len) 0 |
23 | | #endif |
24 | | |
25 | | /* =========================================================================== |
26 | | Compresses the source buffer into the destination buffer. The level |
27 | | parameter has the same meaning as in deflateInit. sourceLen is the byte |
28 | | length of the source buffer. Upon entry, destLen is the total size of the |
29 | | destination buffer, which must be at least 0.1% larger than sourceLen plus |
30 | | 12 bytes. Upon exit, destLen is the actual size of the compressed buffer. |
31 | | |
32 | | compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough |
33 | | memory, Z_BUF_ERROR if there was not enough room in the output buffer, |
34 | | Z_STREAM_ERROR if the level parameter is invalid. |
35 | | */ |
36 | | int Z_EXPORT PREFIX(compress2)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, |
37 | 34.2k | z_size_t sourceLen, int level) { |
38 | 34.2k | PREFIX3(stream) stream; |
39 | 34.2k | int err; |
40 | 34.2k | const unsigned int max = (unsigned int)-1; |
41 | 34.2k | z_size_t left; |
42 | | |
43 | 34.2k | left = *destLen; |
44 | 34.2k | *destLen = 0; |
45 | | |
46 | 34.2k | stream.zalloc = NULL; |
47 | 34.2k | stream.zfree = NULL; |
48 | 34.2k | stream.opaque = NULL; |
49 | | |
50 | 34.2k | err = PREFIX(deflateInit)(&stream, level); |
51 | 34.2k | if (err != Z_OK) |
52 | 0 | return err; |
53 | | |
54 | 34.2k | stream.next_out = dest; |
55 | 34.2k | stream.avail_out = 0; |
56 | 34.2k | stream.next_in = (z_const unsigned char *)source; |
57 | 34.2k | stream.avail_in = 0; |
58 | | |
59 | 43.3k | do { |
60 | 43.3k | if (stream.avail_out == 0) { |
61 | 43.3k | stream.avail_out = left > (unsigned long)max ? max : (unsigned int)left; |
62 | 43.3k | left -= stream.avail_out; |
63 | 43.3k | } |
64 | 43.3k | if (stream.avail_in == 0) { |
65 | 43.3k | stream.avail_in = sourceLen > (unsigned long)max ? max : (unsigned int)sourceLen; |
66 | 43.3k | sourceLen -= stream.avail_in; |
67 | 43.3k | } |
68 | 43.3k | err = PREFIX(deflate)(&stream, sourceLen ? Z_NO_FLUSH : Z_FINISH); |
69 | 43.3k | } while (err == Z_OK); |
70 | | |
71 | 34.2k | *destLen = (z_size_t)stream.total_out; |
72 | 34.2k | PREFIX(deflateEnd)(&stream); |
73 | 34.2k | return err == Z_STREAM_END ? Z_OK : err; |
74 | 34.2k | } |
75 | | |
76 | | /* =========================================================================== |
77 | | */ |
78 | 0 | int Z_EXPORT PREFIX(compress)(unsigned char *dest, z_size_t *destLen, const unsigned char *source, z_size_t sourceLen) { |
79 | 0 | return PREFIX(compress2)(dest, destLen, source, sourceLen, Z_DEFAULT_COMPRESSION); |
80 | 0 | } |
81 | | |
82 | | /* =========================================================================== |
83 | | If the default memLevel or windowBits for deflateInit() is changed, then |
84 | | this function needs to be updated. |
85 | | */ |
86 | 0 | z_size_t Z_EXPORT PREFIX(compressBound)(z_size_t sourceLen) { |
87 | 0 | z_size_t complen = DEFLATE_BOUND_COMPLEN(sourceLen); |
88 | |
|
89 | 0 | if (complen > 0) |
90 | | /* Architecture-specific code provided an upper bound. */ |
91 | 0 | return complen + ZLIB_WRAPLEN; |
92 | | |
93 | 0 | #ifndef NO_QUICK_STRATEGY |
94 | 0 | return sourceLen /* The source size itself */ |
95 | 0 | + (sourceLen == 0 ? 1 : 0) /* Always at least one byte for any input */ |
96 | 0 | + (sourceLen < 9 ? 1 : 0) /* One extra byte for lengths less than 9 */ |
97 | | + DEFLATE_QUICK_OVERHEAD(sourceLen) /* Source encoding overhead, padded to next full byte */ |
98 | | + DEFLATE_BLOCK_OVERHEAD /* Deflate block overhead bytes */ |
99 | 0 | + ZLIB_WRAPLEN; /* zlib wrapper */ |
100 | | #else |
101 | | return sourceLen + (sourceLen >> 4) + 7 + ZLIB_WRAPLEN; |
102 | | #endif |
103 | 0 | } |