/src/libdeflate/lib/zlib_compress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * zlib_compress.c - compress with a zlib wrapper |
3 | | * |
4 | | * Copyright 2016 Eric Biggers |
5 | | * |
6 | | * Permission is hereby granted, free of charge, to any person |
7 | | * obtaining a copy of this software and associated documentation |
8 | | * files (the "Software"), to deal in the Software without |
9 | | * restriction, including without limitation the rights to use, |
10 | | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
11 | | * copies of the Software, and to permit persons to whom the |
12 | | * Software is furnished to do so, subject to the following |
13 | | * conditions: |
14 | | * |
15 | | * The above copyright notice and this permission notice shall be |
16 | | * included in all copies or substantial portions of the Software. |
17 | | * |
18 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
19 | | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
20 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
21 | | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
22 | | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
23 | | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
25 | | * OTHER DEALINGS IN THE SOFTWARE. |
26 | | */ |
27 | | |
28 | | #include "deflate_compress.h" |
29 | | #include "zlib_constants.h" |
30 | | |
31 | | LIBDEFLATEAPI size_t |
32 | | libdeflate_zlib_compress(struct libdeflate_compressor *c, |
33 | | const void *in, size_t in_nbytes, |
34 | | void *out, size_t out_nbytes_avail) |
35 | 0 | { |
36 | 0 | u8 *out_next = out; |
37 | 0 | u16 hdr; |
38 | 0 | unsigned compression_level; |
39 | 0 | unsigned level_hint; |
40 | 0 | size_t deflate_size; |
41 | |
|
42 | 0 | if (out_nbytes_avail <= ZLIB_MIN_OVERHEAD) |
43 | 0 | return 0; |
44 | | |
45 | | /* 2 byte header: CMF and FLG */ |
46 | 0 | hdr = (ZLIB_CM_DEFLATE << 8) | (ZLIB_CINFO_32K_WINDOW << 12); |
47 | 0 | compression_level = libdeflate_get_compression_level(c); |
48 | 0 | if (compression_level < 2) |
49 | 0 | level_hint = ZLIB_FASTEST_COMPRESSION; |
50 | 0 | else if (compression_level < 6) |
51 | 0 | level_hint = ZLIB_FAST_COMPRESSION; |
52 | 0 | else if (compression_level < 8) |
53 | 0 | level_hint = ZLIB_DEFAULT_COMPRESSION; |
54 | 0 | else |
55 | 0 | level_hint = ZLIB_SLOWEST_COMPRESSION; |
56 | 0 | hdr |= level_hint << 6; |
57 | 0 | hdr |= 31 - (hdr % 31); |
58 | |
|
59 | 0 | put_unaligned_be16(hdr, out_next); |
60 | 0 | out_next += 2; |
61 | | |
62 | | /* Compressed data */ |
63 | 0 | deflate_size = libdeflate_deflate_compress(c, in, in_nbytes, out_next, |
64 | 0 | out_nbytes_avail - ZLIB_MIN_OVERHEAD); |
65 | 0 | if (deflate_size == 0) |
66 | 0 | return 0; |
67 | 0 | out_next += deflate_size; |
68 | | |
69 | | /* ADLER32 */ |
70 | 0 | put_unaligned_be32(libdeflate_adler32(1, in, in_nbytes), out_next); |
71 | 0 | out_next += 4; |
72 | |
|
73 | 0 | return out_next - (u8 *)out; |
74 | 0 | } |
75 | | |
76 | | LIBDEFLATEAPI size_t |
77 | | libdeflate_zlib_compress_bound(struct libdeflate_compressor *c, |
78 | | size_t in_nbytes) |
79 | 0 | { |
80 | 0 | return ZLIB_MIN_OVERHEAD + |
81 | 0 | libdeflate_deflate_compress_bound(c, in_nbytes); |
82 | 0 | } |