1 /*
2  * gzip_decompress.c - decompress with a gzip wrapper
3  *
4  * Originally public domain; changes after 2016-09-07 are copyrighted.
5  *
6  * Copyright 2016 Eric Biggers
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following
15  * conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include "gzip_constants.h"
31 #include "unaligned.h"
32 
33 #include "libdeflate.h"
34 
35 LIBDEFLATEAPI enum libdeflate_result
libdeflate_gzip_decompress(struct libdeflate_decompressor * d,const void * in,size_t in_nbytes,void * out,size_t out_nbytes_avail,size_t * actual_out_nbytes_ret)36 libdeflate_gzip_decompress(struct libdeflate_decompressor *d,
37 			   const void *in, size_t in_nbytes,
38 			   void *out, size_t out_nbytes_avail,
39 			   size_t *actual_out_nbytes_ret)
40 {
41 	const u8 *in_next = in;
42 	const u8 * const in_end = in_next + in_nbytes;
43 	u8 flg;
44 	size_t actual_out_nbytes;
45 	enum libdeflate_result result;
46 
47 	if (in_nbytes < GZIP_MIN_OVERHEAD)
48 		return LIBDEFLATE_BAD_DATA;
49 
50 	/* ID1 */
51 	if (*in_next++ != GZIP_ID1)
52 		return LIBDEFLATE_BAD_DATA;
53 	/* ID2 */
54 	if (*in_next++ != GZIP_ID2)
55 		return LIBDEFLATE_BAD_DATA;
56 	/* CM */
57 	if (*in_next++ != GZIP_CM_DEFLATE)
58 		return LIBDEFLATE_BAD_DATA;
59 	flg = *in_next++;
60 	/* MTIME */
61 	in_next += 4;
62 	/* XFL */
63 	in_next += 1;
64 	/* OS */
65 	in_next += 1;
66 
67 	if (flg & GZIP_FRESERVED)
68 		return LIBDEFLATE_BAD_DATA;
69 
70 	/* Extra field */
71 	if (flg & GZIP_FEXTRA) {
72 		u16 xlen = get_unaligned_le16(in_next);
73 		in_next += 2;
74 
75 		if (in_end - in_next < (u32)xlen + GZIP_FOOTER_SIZE)
76 			return LIBDEFLATE_BAD_DATA;
77 
78 		in_next += xlen;
79 	}
80 
81 	/* Original file name (zero terminated) */
82 	if (flg & GZIP_FNAME) {
83 		while (*in_next++ != 0 && in_next != in_end)
84 			;
85 		if (in_end - in_next < GZIP_FOOTER_SIZE)
86 			return LIBDEFLATE_BAD_DATA;
87 	}
88 
89 	/* File comment (zero terminated) */
90 	if (flg & GZIP_FCOMMENT) {
91 		while (*in_next++ != 0 && in_next != in_end)
92 			;
93 		if (in_end - in_next < GZIP_FOOTER_SIZE)
94 			return LIBDEFLATE_BAD_DATA;
95 	}
96 
97 	/* CRC16 for gzip header */
98 	if (flg & GZIP_FHCRC) {
99 		in_next += 2;
100 		if (in_end - in_next < GZIP_FOOTER_SIZE)
101 			return LIBDEFLATE_BAD_DATA;
102 	}
103 
104 	/* Compressed data  */
105 	result = libdeflate_deflate_decompress(d, in_next,
106 					in_end - GZIP_FOOTER_SIZE - in_next,
107 					out, out_nbytes_avail,
108 					actual_out_nbytes_ret);
109 	if (result != LIBDEFLATE_SUCCESS)
110 		return result;
111 
112 	if (actual_out_nbytes_ret)
113 		actual_out_nbytes = *actual_out_nbytes_ret;
114 	else
115 		actual_out_nbytes = out_nbytes_avail;
116 
117 	in_next = in_end - GZIP_FOOTER_SIZE;
118 
119 	/* CRC32 */
120 	if (libdeflate_crc32(0, out, actual_out_nbytes) !=
121 	    get_unaligned_le32(in_next))
122 		return LIBDEFLATE_BAD_DATA;
123 	in_next += 4;
124 
125 	/* ISIZE */
126 	if ((u32)actual_out_nbytes != get_unaligned_le32(in_next))
127 		return LIBDEFLATE_BAD_DATA;
128 
129 	return LIBDEFLATE_SUCCESS;
130 }
131