Debian Codegraph @

lib/gzip_decompress.c

raw
1 /*
2 * gzip_decompress.c - decompress with a gzip 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 "gzip_constants.h"
30
31 LIBDEFLATEAPI enum libdeflate_result
32 libdeflate_gzip_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 u8 flg;
41 size_t actual_in_nbytes;
42 size_t actual_out_nbytes;
43 enum libdeflate_result result;
44
45 if (in_nbytes < GZIP_MIN_OVERHEAD)
46 return LIBDEFLATE_BAD_DATA;
47
48 /* ID1 */
49 if (*in_next++ != GZIP_ID1)
50 return LIBDEFLATE_BAD_DATA;
51 /* ID2 */
52 if (*in_next++ != GZIP_ID2)
53 return LIBDEFLATE_BAD_DATA;
54 /* CM */
55 if (*in_next++ != GZIP_CM_DEFLATE)
56 return LIBDEFLATE_BAD_DATA;
57 flg = *in_next++;
58 /* MTIME */
59 in_next += 4;
60 /* XFL */
61 in_next += 1;
62 /* OS */
63 in_next += 1;
64
65 if (flg & GZIP_FRESERVED)
66 return LIBDEFLATE_BAD_DATA;
67
68 /* Extra field */
69 if (flg & GZIP_FEXTRA) {
70 u16 xlen = get_unaligned_le16(in_next);
71 in_next += 2;
72
73 if (in_end - in_next < (u32)xlen + GZIP_FOOTER_SIZE)
74 return LIBDEFLATE_BAD_DATA;
75
76 in_next += xlen;
77 }
78
79 /* Original file name (zero terminated) */
80 if (flg & GZIP_FNAME) {
81 while (*in_next++ != 0 && in_next != in_end)
82 ;
83 if (in_end - in_next < GZIP_FOOTER_SIZE)
84 return LIBDEFLATE_BAD_DATA;
85 }
86
87 /* File comment (zero terminated) */
88 if (flg & GZIP_FCOMMENT) {
89 while (*in_next++ != 0 && in_next != in_end)
90 ;
91 if (in_end - in_next < GZIP_FOOTER_SIZE)
92 return LIBDEFLATE_BAD_DATA;
93 }
94
95 /* CRC16 for gzip header */
96 if (flg & GZIP_FHCRC) {
97 in_next += 2;
98 if (in_end - in_next < GZIP_FOOTER_SIZE)
99 return LIBDEFLATE_BAD_DATA;
100 }
101
102 /* Compressed data */
103 result = libdeflate_deflate_decompress_ex(d, in_next,
104 in_end - GZIP_FOOTER_SIZE - in_next,
105 out, out_nbytes_avail,
106 &actual_in_nbytes,
107 actual_out_nbytes_ret);
108 if (result != LIBDEFLATE_SUCCESS)
109 return result;
110
111 if (actual_out_nbytes_ret)
112 actual_out_nbytes = *actual_out_nbytes_ret;
113 else
114 actual_out_nbytes = out_nbytes_avail;
115
116 in_next += actual_in_nbytes;
117
118 /* CRC32 */
119 if (libdeflate_crc32(0, out, actual_out_nbytes) !=
120 get_unaligned_le32(in_next))
121 return LIBDEFLATE_BAD_DATA;
122 in_next += 4;
123
124 /* ISIZE */
125 if ((u32)actual_out_nbytes != get_unaligned_le32(in_next))
126 return LIBDEFLATE_BAD_DATA;
127 in_next += 4;
128
129 if (actual_in_nbytes_ret)
130 *actual_in_nbytes_ret = in_next - (u8 *)in;
131
132 return LIBDEFLATE_SUCCESS;
133 }
134
135 LIBDEFLATEAPI enum libdeflate_result
136 libdeflate_gzip_decompress(struct libdeflate_decompressor *d,
137 const void *in, size_t in_nbytes,
138 void *out, size_t out_nbytes_avail,
139 size_t *actual_out_nbytes_ret)
140 {
141 return libdeflate_gzip_decompress_ex(d, in, in_nbytes,
142 out, out_nbytes_avail,
143 NULL, actual_out_nbytes_ret);
144 }