1 | /*
|
---|
2 | * Copyright (c) 2014 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | // XXX: This file is a duplicate of the same in uspace/lib/compress
|
---|
30 |
|
---|
31 | #include <stdint.h>
|
---|
32 | #include <stddef.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include <mem.h>
|
---|
35 | #include <byteorder.h>
|
---|
36 | #include <gzip.h>
|
---|
37 | #include <inflate.h>
|
---|
38 |
|
---|
39 | #define GZIP_ID1 UINT8_C(0x1f)
|
---|
40 | #define GZIP_ID2 UINT8_C(0x8b)
|
---|
41 |
|
---|
42 | #define GZIP_METHOD_DEFLATE UINT8_C(0x08)
|
---|
43 |
|
---|
44 | #define GZIP_FLAGS_MASK UINT8_C(0x1f)
|
---|
45 | #define GZIP_FLAG_FHCRC UINT8_C(1 << 1)
|
---|
46 | #define GZIP_FLAG_FEXTRA UINT8_C(1 << 2)
|
---|
47 | #define GZIP_FLAG_FNAME UINT8_C(1 << 3)
|
---|
48 | #define GZIP_FLAG_FCOMMENT UINT8_C(1 << 4)
|
---|
49 |
|
---|
50 | typedef struct {
|
---|
51 | uint8_t id1;
|
---|
52 | uint8_t id2;
|
---|
53 | uint8_t method;
|
---|
54 | uint8_t flags;
|
---|
55 | uint32_t mtime;
|
---|
56 | uint8_t extra_flags;
|
---|
57 | uint8_t os;
|
---|
58 | } __attribute__((packed)) gzip_header_t;
|
---|
59 |
|
---|
60 | typedef struct {
|
---|
61 | uint32_t crc32;
|
---|
62 | uint32_t size;
|
---|
63 | } __attribute__((packed)) gzip_footer_t;
|
---|
64 |
|
---|
65 | /** Check GZIP signature
|
---|
66 | *
|
---|
67 | * Checks whether the source buffer start with a GZIP signature.
|
---|
68 | *
|
---|
69 | * @param[in] src Source data buffer.
|
---|
70 | * @param[in] srclen Source buffer size (bytes).
|
---|
71 | *
|
---|
72 | * @return True if GZIP signature found.
|
---|
73 | * @return False if no GZIP signature found.
|
---|
74 | *
|
---|
75 | */
|
---|
76 | bool gzip_check(const void *src, size_t srclen)
|
---|
77 | {
|
---|
78 | if ((srclen < (sizeof(gzip_header_t) + sizeof(gzip_footer_t))))
|
---|
79 | return false;
|
---|
80 |
|
---|
81 | gzip_header_t header;
|
---|
82 | memcpy(&header, src, sizeof(header));
|
---|
83 |
|
---|
84 | if ((header.id1 != GZIP_ID1) ||
|
---|
85 | (header.id2 != GZIP_ID2) ||
|
---|
86 | (header.method != GZIP_METHOD_DEFLATE) ||
|
---|
87 | ((header.flags & (~GZIP_FLAGS_MASK)) != 0))
|
---|
88 | return false;
|
---|
89 |
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /** Get uncompressed size
|
---|
94 | *
|
---|
95 | * Note that the uncompressed size is read from the GZIP footer
|
---|
96 | * (and not calculated by acutally decompressing the GZip archive).
|
---|
97 | * Thus the source of the GZip archive needs to be trusted.
|
---|
98 | *
|
---|
99 | * @param[in] src Source data buffer.
|
---|
100 | * @param[out] srclen Source buffer size (bytes).
|
---|
101 | *
|
---|
102 | * @return Uncompressed size.
|
---|
103 | *
|
---|
104 | */
|
---|
105 | size_t gzip_size(const void *src, size_t srclen)
|
---|
106 | {
|
---|
107 | if (!gzip_check(src, srclen))
|
---|
108 | return 0;
|
---|
109 |
|
---|
110 | gzip_footer_t footer;
|
---|
111 | memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
|
---|
112 |
|
---|
113 | return uint32_t_le2host(footer.size);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Expand GZIP compressed data
|
---|
117 | *
|
---|
118 | * The routine compares the output buffer size with
|
---|
119 | * the size encoded in the input stream. This
|
---|
120 | * effectively limits the size of the uncompressed
|
---|
121 | * data to 4 GiB (expanding input streams that actually
|
---|
122 | * encode more data will always fail).
|
---|
123 | *
|
---|
124 | * So far, no CRC is perfomed.
|
---|
125 | *
|
---|
126 | * @param[in] src Source data buffer.
|
---|
127 | * @param[in] srclen Source buffer size (bytes).
|
---|
128 | * @param[out] dest Destination data buffer.
|
---|
129 | * @param[out] destlen Destination buffer size (bytes).
|
---|
130 | *
|
---|
131 | * @return EOK on success.
|
---|
132 | * @return ENOENT on distance too large.
|
---|
133 | * @return EINVAL on invalid Huffman code, invalid deflate data,
|
---|
134 | * invalid compression method or invalid stream.
|
---|
135 | * @return ELIMIT on input buffer overrun.
|
---|
136 | * @return ENOMEM on output buffer overrun.
|
---|
137 | *
|
---|
138 | */
|
---|
139 | int gzip_expand(const void *src, size_t srclen, void *dest, size_t destlen)
|
---|
140 | {
|
---|
141 | if (!gzip_check(src, srclen))
|
---|
142 | return EINVAL;
|
---|
143 |
|
---|
144 | /* Decode header and footer */
|
---|
145 |
|
---|
146 | gzip_header_t header;
|
---|
147 | memcpy(&header, src, sizeof(header));
|
---|
148 |
|
---|
149 | gzip_footer_t footer;
|
---|
150 | memcpy(&footer, src + srclen - sizeof(footer), sizeof(footer));
|
---|
151 |
|
---|
152 | if (destlen != uint32_t_le2host(footer.size))
|
---|
153 | return EINVAL;
|
---|
154 |
|
---|
155 | /* Ignore extra metadata */
|
---|
156 |
|
---|
157 | const void *stream = src + sizeof(header);
|
---|
158 | size_t stream_length = srclen - sizeof(header) - sizeof(footer);
|
---|
159 |
|
---|
160 | if ((header.flags & GZIP_FLAG_FEXTRA) != 0) {
|
---|
161 | uint16_t extra_length;
|
---|
162 |
|
---|
163 | if (stream_length < sizeof(extra_length))
|
---|
164 | return EINVAL;
|
---|
165 |
|
---|
166 | memcpy(&extra_length, stream, sizeof(extra_length));
|
---|
167 | stream += sizeof(extra_length);
|
---|
168 | stream_length -= sizeof(extra_length);
|
---|
169 |
|
---|
170 | if (stream_length < extra_length)
|
---|
171 | return EINVAL;
|
---|
172 |
|
---|
173 | stream += extra_length;
|
---|
174 | stream_length -= extra_length;
|
---|
175 | }
|
---|
176 |
|
---|
177 | if ((header.flags & GZIP_FLAG_FNAME) != 0) {
|
---|
178 | while (*((uint8_t *) stream) != 0) {
|
---|
179 | if (stream_length == 0)
|
---|
180 | return EINVAL;
|
---|
181 |
|
---|
182 | stream++;
|
---|
183 | stream_length--;
|
---|
184 | }
|
---|
185 |
|
---|
186 | if (stream_length == 0)
|
---|
187 | return EINVAL;
|
---|
188 |
|
---|
189 | stream++;
|
---|
190 | stream_length--;
|
---|
191 | }
|
---|
192 |
|
---|
193 | if ((header.flags & GZIP_FLAG_FCOMMENT) != 0) {
|
---|
194 | while (*((uint8_t *) stream) != 0) {
|
---|
195 | if (stream_length == 0)
|
---|
196 | return EINVAL;
|
---|
197 |
|
---|
198 | stream++;
|
---|
199 | stream_length--;
|
---|
200 | }
|
---|
201 |
|
---|
202 | if (stream_length == 0)
|
---|
203 | return EINVAL;
|
---|
204 |
|
---|
205 | stream++;
|
---|
206 | stream_length--;
|
---|
207 | }
|
---|
208 |
|
---|
209 | if ((header.flags & GZIP_FLAG_FHCRC) != 0) {
|
---|
210 | if (stream_length < 2)
|
---|
211 | return EINVAL;
|
---|
212 |
|
---|
213 | stream += 2;
|
---|
214 | stream_length -= 2;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /* Inflate the data */
|
---|
218 | return inflate(stream, stream_length, dest, destlen);
|
---|
219 | }
|
---|