Skip to content

Commit d75a3cd

Browse files
jimmodpgeorge
authored andcommitted
lib/uzlib: Combine zlib/gzip header parsing to allow auto-detect.
This supports `wbits` values between +40 to +47. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]>
1 parent c2b8e6e commit d75a3cd

File tree

6 files changed

+140
-181
lines changed

6 files changed

+140
-181
lines changed

lib/uzlib/header.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* uzlib - tiny deflate/inflate library (deflate, gzip, zlib)
3+
*
4+
* Copyright (c) 2003 by Joergen Ibsen / Jibz
5+
* All Rights Reserved
6+
*
7+
* http://www.ibsensoftware.com/
8+
*
9+
* Copyright (c) 2014-2018 by Paul Sokolovsky
10+
*
11+
* Optimised for MicroPython:
12+
* Copyright (c) 2023 by Jim Mussared
13+
*
14+
* This software is provided 'as-is', without any express
15+
* or implied warranty. In no event will the authors be
16+
* held liable for any damages arising from the use of
17+
* this software.
18+
*
19+
* Permission is granted to anyone to use this software
20+
* for any purpose, including commercial applications,
21+
* and to alter it and redistribute it freely, subject to
22+
* the following restrictions:
23+
*
24+
* 1. The origin of this software must not be
25+
* misrepresented; you must not claim that you
26+
* wrote the original software. If you use this
27+
* software in a product, an acknowledgment in
28+
* the product documentation would be appreciated
29+
* but is not required.
30+
*
31+
* 2. Altered source versions must be plainly marked
32+
* as such, and must not be misrepresented as
33+
* being the original software.
34+
*
35+
* 3. This notice may not be removed or altered from
36+
* any source distribution.
37+
*/
38+
39+
#include "uzlib.h"
40+
41+
#define FTEXT 1
42+
#define FHCRC 2
43+
#define FEXTRA 4
44+
#define FNAME 8
45+
#define FCOMMENT 16
46+
47+
void tinf_skip_bytes(uzlib_uncomp_t *d, int num);
48+
uint16_t tinf_get_uint16(uzlib_uncomp_t *d);
49+
50+
void tinf_skip_bytes(uzlib_uncomp_t *d, int num)
51+
{
52+
while (num--) uzlib_get_byte(d);
53+
}
54+
55+
uint16_t tinf_get_uint16(uzlib_uncomp_t *d)
56+
{
57+
unsigned int v = uzlib_get_byte(d);
58+
v = (uzlib_get_byte(d) << 8) | v;
59+
return v;
60+
}
61+
62+
int uzlib_parse_zlib_gzip_header(uzlib_uncomp_t *d, int *wbits)
63+
{
64+
/* -- check format -- */
65+
unsigned char cmf = uzlib_get_byte(d);
66+
unsigned char flg = uzlib_get_byte(d);
67+
68+
/* check for gzip id bytes */
69+
if (cmf == 0x1f && flg == 0x8b) {
70+
/* check method is deflate */
71+
if (uzlib_get_byte(d) != 8) return UZLIB_DATA_ERROR;
72+
73+
/* get flag byte */
74+
flg = uzlib_get_byte(d);
75+
76+
/* check that reserved bits are zero */
77+
if (flg & 0xe0) return UZLIB_DATA_ERROR;
78+
79+
/* -- find start of compressed data -- */
80+
81+
/* skip rest of base header of 10 bytes */
82+
tinf_skip_bytes(d, 6);
83+
84+
/* skip extra data if present */
85+
if (flg & FEXTRA)
86+
{
87+
unsigned int xlen = tinf_get_uint16(d);
88+
tinf_skip_bytes(d, xlen);
89+
}
90+
91+
/* skip file name if present */
92+
if (flg & FNAME) { while (uzlib_get_byte(d)); }
93+
94+
/* skip file comment if present */
95+
if (flg & FCOMMENT) { while (uzlib_get_byte(d)); }
96+
97+
/* check header crc if present */
98+
if (flg & FHCRC)
99+
{
100+
/*unsigned int hcrc =*/ tinf_get_uint16(d);
101+
102+
// TODO: Check!
103+
// if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff))
104+
// return UZLIB_DATA_ERROR;
105+
}
106+
107+
/* initialize for crc32 checksum */
108+
d->checksum_type = UZLIB_CHKSUM_CRC;
109+
d->checksum = ~0;
110+
111+
return UZLIB_HEADER_GZIP;
112+
} else {
113+
/* check checksum */
114+
if ((256*cmf + flg) % 31) return UZLIB_DATA_ERROR;
115+
116+
/* check method is deflate */
117+
if ((cmf & 0x0f) != 8) return UZLIB_DATA_ERROR;
118+
119+
/* check window size is valid */
120+
if ((cmf >> 4) > 7) return UZLIB_DATA_ERROR;
121+
122+
/* check there is no preset dictionary */
123+
if (flg & 0x20) return UZLIB_DATA_ERROR;
124+
125+
/* initialize for adler32 checksum */
126+
d->checksum_type = UZLIB_CHKSUM_ADLER;
127+
d->checksum = 1;
128+
129+
*wbits = (cmf >> 4) + 8;
130+
131+
return UZLIB_HEADER_ZLIB;
132+
}
133+
}

lib/uzlib/tinfgzip.c

Lines changed: 0 additions & 110 deletions
This file was deleted.

lib/uzlib/tinfzlib.c

Lines changed: 0 additions & 66 deletions
This file was deleted.

lib/uzlib/uzlib.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,9 @@ void uzlib_uncompress_init(uzlib_uncomp_t *d, void *dict, unsigned int dictLen);
130130
int uzlib_uncompress(uzlib_uncomp_t *d);
131131
int uzlib_uncompress_chksum(uzlib_uncomp_t *d);
132132

133-
int uzlib_zlib_parse_header(uzlib_uncomp_t *d);
134-
int uzlib_gzip_parse_header(uzlib_uncomp_t *d);
133+
#define UZLIB_HEADER_ZLIB 0
134+
#define UZLIB_HEADER_GZIP 1
135+
int uzlib_parse_zlib_gzip_header(uzlib_uncomp_t *d, int *wbits);
135136

136137
/* Compression API */
137138

ports/stm32/mboot/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ LIB_SRC_C += \
105105
lib/oofatfs/ffunicode.c \
106106
lib/uzlib/adler32.c \
107107
lib/uzlib/crc32.c \
108-
lib/uzlib/tinfgzip.c \
108+
lib/uzlib/header.c \
109109
lib/uzlib/tinflate.c
110110

111111
SRC_C += \

ports/stm32/mboot/gzstream.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ int gz_stream_init_from_stream(void *stream_data, stream_read_t stream_read) {
7878
memset(&gz_stream.decomp, 0, sizeof(gz_stream.decomp));
7979
gz_stream.decomp.source_read_cb = gz_stream_read_src;
8080

81-
int st = uzlib_gzip_parse_header(&gz_stream.decomp);
82-
if (st != UZLIB_OK) {
81+
int header_wbits;
82+
int st = uzlib_parse_zlib_gzip_header(&gz_stream.decomp, &header_wbits);
83+
if (st != UZLIB_HEADER_GZIP) {
8384
return -MBOOT_ERRNO_GUNZIP_FAILED;
8485
}
8586

0 commit comments

Comments
 (0)