/src/libdeflate/lib/zlib_decompress.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * zlib_decompress.c - decompress 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 "lib_common.h" |
29 | | #include "zlib_constants.h" |
30 | | |
31 | | LIBDEFLATEAPI enum libdeflate_result |
32 | | libdeflate_zlib_decompress_ex(struct libdeflate_decompressor *d, |
33 | | const void *in, size_t in_nbytes, |
34 | | void *out, size_t out_nbytes_avail, |
35 | | size_t *actual_in_nbytes_ret, |
36 | | size_t *actual_out_nbytes_ret) |
37 | 6.73k | { |
38 | 6.73k | const u8 *in_next = in; |
39 | 6.73k | const u8 * const in_end = in_next + in_nbytes; |
40 | 6.73k | u16 hdr; |
41 | 6.73k | size_t actual_in_nbytes; |
42 | 6.73k | size_t actual_out_nbytes; |
43 | 6.73k | enum libdeflate_result result; |
44 | | |
45 | 6.73k | if (in_nbytes < ZLIB_MIN_OVERHEAD) |
46 | 71 | return LIBDEFLATE_BAD_DATA; |
47 | | |
48 | | /* 2 byte header: CMF and FLG */ |
49 | 6.65k | hdr = get_unaligned_be16(in_next); |
50 | 6.65k | in_next += 2; |
51 | | |
52 | | /* FCHECK */ |
53 | 6.65k | if ((hdr % 31) != 0) |
54 | 248 | return LIBDEFLATE_BAD_DATA; |
55 | | |
56 | | /* CM */ |
57 | 6.41k | if (((hdr >> 8) & 0xF) != ZLIB_CM_DEFLATE) |
58 | 71 | return LIBDEFLATE_BAD_DATA; |
59 | | |
60 | | /* CINFO */ |
61 | 6.34k | if ((hdr >> 12) > ZLIB_CINFO_32K_WINDOW) |
62 | 22 | return LIBDEFLATE_BAD_DATA; |
63 | | |
64 | | /* FDICT */ |
65 | 6.31k | if ((hdr >> 5) & 1) |
66 | 10 | return LIBDEFLATE_BAD_DATA; |
67 | | |
68 | | /* Compressed data */ |
69 | 6.30k | result = libdeflate_deflate_decompress_ex(d, in_next, |
70 | 6.30k | in_end - ZLIB_FOOTER_SIZE - in_next, |
71 | 6.30k | out, out_nbytes_avail, |
72 | 6.30k | &actual_in_nbytes, actual_out_nbytes_ret); |
73 | 6.30k | if (result != LIBDEFLATE_SUCCESS) |
74 | 5.26k | return result; |
75 | | |
76 | 1.04k | if (actual_out_nbytes_ret) |
77 | 0 | actual_out_nbytes = *actual_out_nbytes_ret; |
78 | 1.04k | else |
79 | 1.04k | actual_out_nbytes = out_nbytes_avail; |
80 | | |
81 | 1.04k | in_next += actual_in_nbytes; |
82 | | |
83 | | /* ADLER32 */ |
84 | 1.04k | if (libdeflate_adler32(1, out, actual_out_nbytes) != |
85 | 1.04k | get_unaligned_be32(in_next)) |
86 | 76 | return LIBDEFLATE_BAD_DATA; |
87 | 965 | in_next += 4; |
88 | | |
89 | 965 | if (actual_in_nbytes_ret) |
90 | 0 | *actual_in_nbytes_ret = in_next - (u8 *)in; |
91 | | |
92 | 965 | return LIBDEFLATE_SUCCESS; |
93 | 1.04k | } |
94 | | |
95 | | LIBDEFLATEAPI enum libdeflate_result |
96 | | libdeflate_zlib_decompress(struct libdeflate_decompressor *d, |
97 | | const void *in, size_t in_nbytes, |
98 | | void *out, size_t out_nbytes_avail, |
99 | | size_t *actual_out_nbytes_ret) |
100 | 6.73k | { |
101 | 6.73k | return libdeflate_zlib_decompress_ex(d, in, in_nbytes, |
102 | 6.73k | out, out_nbytes_avail, |
103 | 6.73k | NULL, actual_out_nbytes_ret); |
104 | 6.73k | } |