Skip to content

Commit 7c62de3

Browse files
authored
Merge pull request #8335 from BJap/zlib-decompress-bug
Fix gzip Decompression Support
2 parents 60a0a3d + ae181d6 commit 7c62de3

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

extmod/moduzlib.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,18 @@ STATIC mp_obj_t mod_uzlib_decompress(size_t n_args, const mp_obj_t *args) {
175175
decomp->source_limit = (byte *)bufinfo.buf + bufinfo.len;
176176

177177
int st;
178-
bool is_zlib = true;
178+
mp_int_t wbits = 0;
179179

180-
if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
181-
is_zlib = false;
180+
if (n_args > 1) {
181+
wbits = MP_OBJ_SMALL_INT_VALUE(args[1]);
182182
}
183183

184-
if (is_zlib) {
184+
if (wbits >= 16) {
185+
st = uzlib_gzip_parse_header(decomp);
186+
if (st < 0) {
187+
goto error;
188+
}
189+
} else if (wbits >= 0) {
185190
st = uzlib_zlib_parse_header(decomp);
186191
if (st < 0) {
187192
goto error;

shared-bindings/zlib/__init__.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@
7171
//| ...
7272
//|
7373
STATIC mp_obj_t zlib_decompress(size_t n_args, const mp_obj_t *args) {
74-
bool is_zlib = true;
75-
if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
76-
is_zlib = false;
74+
mp_int_t wbits = 0;
75+
if (n_args > 1) {
76+
wbits = MP_OBJ_SMALL_INT_VALUE(args[1]);
7777
}
7878

79-
return common_hal_zlib_decompress(args[0], is_zlib);
79+
return common_hal_zlib_decompress(args[0], wbits);
8080
}
8181
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(zlib_decompress_obj, 1, 3, zlib_decompress);
8282

shared-bindings/zlib/__init__.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
2929

30-
mp_obj_t common_hal_zlib_decompress(mp_obj_t data, bool is_zlib);
30+
mp_obj_t common_hal_zlib_decompress(mp_obj_t data, mp_int_t wbits);
3131

3232
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H

shared-module/zlib/__init__.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
#define DEBUG_printf(...) (void)0
4949
#endif
5050

51-
mp_obj_t common_hal_zlib_decompress(mp_obj_t data, bool is_zlib) {
51+
mp_obj_t common_hal_zlib_decompress(mp_obj_t data, mp_int_t wbits) {
5252
mp_buffer_info_t bufinfo;
5353
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
5454

@@ -66,7 +66,12 @@ mp_obj_t common_hal_zlib_decompress(mp_obj_t data, bool is_zlib) {
6666
decomp->source_limit = (unsigned char *)bufinfo.buf + bufinfo.len;
6767
int st;
6868

69-
if (is_zlib) {
69+
if (wbits >= 16) {
70+
st = uzlib_gzip_parse_header(decomp);
71+
if (st < 0) {
72+
goto error;
73+
}
74+
} else if (wbits >= 0) {
7075
st = uzlib_zlib_parse_header(decomp);
7176
if (st < 0) {
7277
goto error;

0 commit comments

Comments
 (0)