/work/libdeflate-src/lib/gzip_decompress.c
Line | Count | Source |
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 | 1.82k | { |
38 | 1.82k | const u8 *in_next = in; |
39 | 1.82k | const u8 * const in_end = in_next + in_nbytes; |
40 | 1.82k | u8 flg; |
41 | 1.82k | size_t actual_in_nbytes; |
42 | 1.82k | size_t actual_out_nbytes; |
43 | 1.82k | enum libdeflate_result result; |
44 | | |
45 | 1.82k | if (in_nbytes < GZIP_MIN_OVERHEAD) |
46 | 0 | return LIBDEFLATE_BAD_DATA; |
47 | | |
48 | | /* ID1 */ |
49 | 1.82k | if (*in_next++ != GZIP_ID1) |
50 | 0 | return LIBDEFLATE_BAD_DATA; |
51 | | /* ID2 */ |
52 | 1.82k | if (*in_next++ != GZIP_ID2) |
53 | 0 | return LIBDEFLATE_BAD_DATA; |
54 | | /* CM */ |
55 | 1.82k | if (*in_next++ != GZIP_CM_DEFLATE) |
56 | 30 | return LIBDEFLATE_BAD_DATA; |
57 | 1.79k | flg = *in_next++; |
58 | | /* MTIME */ |
59 | 1.79k | in_next += 4; |
60 | | /* XFL */ |
61 | 1.79k | in_next += 1; |
62 | | /* OS */ |
63 | 1.79k | in_next += 1; |
64 | | |
65 | 1.79k | if (flg & GZIP_FRESERVED) |
66 | 6 | return LIBDEFLATE_BAD_DATA; |
67 | | |
68 | | /* Extra field */ |
69 | 1.78k | if (flg & GZIP_FEXTRA) { |
70 | 92 | u16 xlen = get_unaligned_le16(in_next); |
71 | 92 | in_next += 2; |
72 | | |
73 | 92 | if (in_end - in_next < (u32)xlen + GZIP_FOOTER_SIZE) |
74 | 16 | return LIBDEFLATE_BAD_DATA; |
75 | | |
76 | 76 | in_next += xlen; |
77 | 76 | } |
78 | | |
79 | | /* Original file name (zero terminated) */ |
80 | 1.76k | if (flg & GZIP_FNAME) { |
81 | 2.42k | while (*in_next++ != 0 && in_next != in_end) |
82 | 2.15k | ; |
83 | 270 | if (in_end - in_next < GZIP_FOOTER_SIZE) |
84 | 11 | return LIBDEFLATE_BAD_DATA; |
85 | 270 | } |
86 | | |
87 | | /* File comment (zero terminated) */ |
88 | 1.75k | if (flg & GZIP_FCOMMENT) { |
89 | 4.93k | while (*in_next++ != 0 && in_next != in_end) |
90 | 4.54k | ; |
91 | 386 | if (in_end - in_next < GZIP_FOOTER_SIZE) |
92 | 9 | return LIBDEFLATE_BAD_DATA; |
93 | 386 | } |
94 | | |
95 | | /* CRC16 for gzip header */ |
96 | 1.74k | if (flg & GZIP_FHCRC) { |
97 | 511 | in_next += 2; |
98 | 511 | if (in_end - in_next < GZIP_FOOTER_SIZE) |
99 | 4 | return LIBDEFLATE_BAD_DATA; |
100 | 511 | } |
101 | | |
102 | | /* Compressed data */ |
103 | 1.74k | result = libdeflate_deflate_decompress_ex(d, in_next, |
104 | 1.74k | in_end - GZIP_FOOTER_SIZE - in_next, |
105 | 1.74k | out, out_nbytes_avail, |
106 | 1.74k | &actual_in_nbytes, |
107 | 1.74k | actual_out_nbytes_ret); |
108 | 1.74k | if (result != LIBDEFLATE_SUCCESS) |
109 | 1.52k | return result; |
110 | | |
111 | 216 | if (actual_out_nbytes_ret) |
112 | 216 | actual_out_nbytes = *actual_out_nbytes_ret; |
113 | 0 | else |
114 | 0 | actual_out_nbytes = out_nbytes_avail; |
115 | | |
116 | 216 | in_next += actual_in_nbytes; |
117 | | |
118 | | /* CRC32 */ |
119 | 216 | if (libdeflate_crc32(0, out, actual_out_nbytes) != |
120 | 216 | get_unaligned_le32(in_next)) |
121 | 167 | return LIBDEFLATE_BAD_DATA; |
122 | 49 | in_next += 4; |
123 | | |
124 | | /* ISIZE */ |
125 | 49 | if ((u32)actual_out_nbytes != get_unaligned_le32(in_next)) |
126 | 42 | return LIBDEFLATE_BAD_DATA; |
127 | 7 | in_next += 4; |
128 | | |
129 | 7 | if (actual_in_nbytes_ret) |
130 | 0 | *actual_in_nbytes_ret = in_next - (u8 *)in; |
131 | | |
132 | 7 | return LIBDEFLATE_SUCCESS; |
133 | 49 | } |
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 | 1.82k | { |
141 | 1.82k | return libdeflate_gzip_decompress_ex(d, in, in_nbytes, |
142 | 1.82k | out, out_nbytes_avail, |
143 | | NULL, actual_out_nbytes_ret); |
144 | 1.82k | } |