Skip to content

Commit

Permalink
Merge pull request #8335 from BJap/zlib-decompress-bug
Browse files Browse the repository at this point in the history
Fix gzip Decompression Support
  • Loading branch information
tannewt authored Aug 29, 2023
2 parents 60a0a3d + ae181d6 commit 7c62de3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
13 changes: 9 additions & 4 deletions extmod/moduzlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,18 @@ STATIC mp_obj_t mod_uzlib_decompress(size_t n_args, const mp_obj_t *args) {
decomp->source_limit = (byte *)bufinfo.buf + bufinfo.len;

int st;
bool is_zlib = true;
mp_int_t wbits = 0;

if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
is_zlib = false;
if (n_args > 1) {
wbits = MP_OBJ_SMALL_INT_VALUE(args[1]);
}

if (is_zlib) {
if (wbits >= 16) {
st = uzlib_gzip_parse_header(decomp);
if (st < 0) {
goto error;
}
} else if (wbits >= 0) {
st = uzlib_zlib_parse_header(decomp);
if (st < 0) {
goto error;
Expand Down
8 changes: 4 additions & 4 deletions shared-bindings/zlib/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@
//| ...
//|
STATIC mp_obj_t zlib_decompress(size_t n_args, const mp_obj_t *args) {
bool is_zlib = true;
if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
is_zlib = false;
mp_int_t wbits = 0;
if (n_args > 1) {
wbits = MP_OBJ_SMALL_INT_VALUE(args[1]);
}

return common_hal_zlib_decompress(args[0], is_zlib);
return common_hal_zlib_decompress(args[0], wbits);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(zlib_decompress_obj, 1, 3, zlib_decompress);

Expand Down
2 changes: 1 addition & 1 deletion shared-bindings/zlib/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H

mp_obj_t common_hal_zlib_decompress(mp_obj_t data, bool is_zlib);
mp_obj_t common_hal_zlib_decompress(mp_obj_t data, mp_int_t wbits);

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ZLIB___INIT___H
9 changes: 7 additions & 2 deletions shared-module/zlib/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
#define DEBUG_printf(...) (void)0
#endif

mp_obj_t common_hal_zlib_decompress(mp_obj_t data, bool is_zlib) {
mp_obj_t common_hal_zlib_decompress(mp_obj_t data, mp_int_t wbits) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);

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

if (is_zlib) {
if (wbits >= 16) {
st = uzlib_gzip_parse_header(decomp);
if (st < 0) {
goto error;
}
} else if (wbits >= 0) {
st = uzlib_zlib_parse_header(decomp);
if (st < 0) {
goto error;
Expand Down

0 comments on commit 7c62de3

Please sign in to comment.