Merge pull request #8335 from BJap/zlib-decompress-bug

Fix gzip Decompression Support
This commit is contained in:
Scott Shawcroft 2023-08-29 12:58:09 -07:00 committed by GitHub
commit 7c62de35dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 11 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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);
@ -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;