Debian Codegraph @

lib/zlib_decompress.c

raw
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 {
38 const u8 *in_next = in;
39 const u8 * const in_end = in_next + in_nbytes;
40 u16 hdr;
41 size_t actual_in_nbytes;
42 size_t actual_out_nbytes;
43 enum libdeflate_result result;
44
45 if (in_nbytes < ZLIB_MIN_OVERHEAD)
46 return LIBDEFLATE_BAD_DATA;
47
48 /* 2 byte header: CMF and FLG */
49 hdr = get_unaligned_be16(in_next);
50 in_next += 2;
51
52 /* FCHECK */
53 if ((hdr % 31) != 0)
54 return LIBDEFLATE_BAD_DATA;
55
56 /* CM */
57 if (((hdr >> 8) & 0xF) != ZLIB_CM_DEFLATE)
58 return LIBDEFLATE_BAD_DATA;
59
60 /* CINFO */
61 if ((hdr >> 12) > ZLIB_CINFO_32K_WINDOW)
62 return LIBDEFLATE_BAD_DATA;
63
64 /* FDICT */
65 if ((hdr >> 5) & 1)
66 return LIBDEFLATE_BAD_DATA;
67
68 /* Compressed data */
69 result = libdeflate_deflate_decompress_ex(d, in_next,
70 in_end - ZLIB_FOOTER_SIZE - in_next,
71 out, out_nbytes_avail,
72 &actual_in_nbytes, actual_out_nbytes_ret);
73 if (result != LIBDEFLATE_SUCCESS)
74 return result;
75
76 if (actual_out_nbytes_ret)
77 actual_out_nbytes = *actual_out_nbytes_ret;
78 else
79 actual_out_nbytes = out_nbytes_avail;
80
81 in_next += actual_in_nbytes;
82
83 /* ADLER32 */
84 if (libdeflate_adler32(1, out, actual_out_nbytes) !=
85 get_unaligned_be32(in_next))
86 return LIBDEFLATE_BAD_DATA;
87 in_next += 4;
88
89 if (actual_in_nbytes_ret)
90 *actual_in_nbytes_ret = in_next - (u8 *)in;
91
92 return LIBDEFLATE_SUCCESS;
93 }
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 {
101 return libdeflate_zlib_decompress_ex(d, in, in_nbytes,
102 out, out_nbytes_avail,
103 NULL, actual_out_nbytes_ret);
104 }