/src/libdeflate/lib/zlib_decompress.c
Line | Count | Source |
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 | 10.0k | { |
38 | 10.0k | const u8 *in_next = in; |
39 | 10.0k | const u8 *in_end; |
40 | 10.0k | u16 hdr; |
41 | 10.0k | size_t actual_in_nbytes; |
42 | 10.0k | size_t actual_out_nbytes; |
43 | 10.0k | enum libdeflate_result result; |
44 | | |
45 | | /* |
46 | | * Buffers larger than PTRDIFF_MAX cannot be accepted, since pointer |
47 | | * arithmetic would not be fully defined on such buffers. (Just check |
48 | | * 'in_nbytes' here; 'out_nbytes_avail' is checked later.) |
49 | | */ |
50 | 10.0k | if (unlikely(in_nbytes < ZLIB_MIN_OVERHEAD || in_nbytes > PTRDIFF_MAX)) |
51 | 1.25k | return LIBDEFLATE_BAD_DATA; |
52 | 8.80k | in_end = in_next + in_nbytes; |
53 | | |
54 | | /* 2 byte header: CMF and FLG */ |
55 | 8.80k | hdr = get_unaligned_be16(in_next); |
56 | 8.80k | in_next += 2; |
57 | | |
58 | | /* FCHECK */ |
59 | 8.80k | if ((hdr % 31) != 0) |
60 | 3.27k | return LIBDEFLATE_BAD_DATA; |
61 | | |
62 | | /* CM */ |
63 | 5.52k | if (((hdr >> 8) & 0xF) != ZLIB_CM_DEFLATE) |
64 | 380 | return LIBDEFLATE_BAD_DATA; |
65 | | |
66 | | /* CINFO */ |
67 | 5.14k | if ((hdr >> 12) > ZLIB_CINFO_32K_WINDOW) |
68 | 36 | return LIBDEFLATE_BAD_DATA; |
69 | | |
70 | | /* FDICT */ |
71 | 5.10k | if ((hdr >> 5) & 1) |
72 | 15 | return LIBDEFLATE_BAD_DATA; |
73 | | |
74 | | /* Compressed data */ |
75 | 5.09k | result = libdeflate_deflate_decompress_ex(d, in_next, |
76 | 5.09k | in_end - ZLIB_FOOTER_SIZE - in_next, |
77 | 5.09k | out, out_nbytes_avail, |
78 | 5.09k | &actual_in_nbytes, actual_out_nbytes_ret); |
79 | 5.09k | if (result != LIBDEFLATE_SUCCESS) |
80 | 3.31k | return result; |
81 | | |
82 | 1.77k | if (actual_out_nbytes_ret) |
83 | 0 | actual_out_nbytes = *actual_out_nbytes_ret; |
84 | 1.77k | else |
85 | 1.77k | actual_out_nbytes = out_nbytes_avail; |
86 | | |
87 | 1.77k | in_next += actual_in_nbytes; |
88 | | |
89 | | /* ADLER32 */ |
90 | 1.77k | if (libdeflate_adler32(1, out, actual_out_nbytes) != |
91 | 1.77k | get_unaligned_be32(in_next)) |
92 | 58 | return LIBDEFLATE_BAD_DATA; |
93 | 1.72k | in_next += 4; |
94 | | |
95 | 1.72k | if (actual_in_nbytes_ret) |
96 | 0 | *actual_in_nbytes_ret = in_next - (u8 *)in; |
97 | | |
98 | 1.72k | return LIBDEFLATE_SUCCESS; |
99 | 1.77k | } |
100 | | |
101 | | LIBDEFLATEAPI enum libdeflate_result |
102 | | libdeflate_zlib_decompress(struct libdeflate_decompressor *d, |
103 | | const void *in, size_t in_nbytes, |
104 | | void *out, size_t out_nbytes_avail, |
105 | | size_t *actual_out_nbytes_ret) |
106 | 10.0k | { |
107 | 10.0k | return libdeflate_zlib_decompress_ex(d, in, in_nbytes, |
108 | 10.0k | out, out_nbytes_avail, |
109 | | NULL, actual_out_nbytes_ret); |
110 | 10.0k | } |