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