2014-10-12 11:16:06 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-10-12 11:16:06 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2016-09-02 17:07:08 -04:00
|
|
|
* Copyright (c) 2014-2016 Paul Sokolovsky
|
2014-10-12 11:16:06 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-08-16 23:05:37 -04:00
|
|
|
#include <string.h>
|
2014-10-12 11:16:06 -04:00
|
|
|
|
2015-01-01 15:35:21 -05:00
|
|
|
#include "py/runtime.h"
|
2016-09-02 17:07:08 -04:00
|
|
|
#include "py/stream.h"
|
2016-09-02 17:34:57 -04:00
|
|
|
#include "py/mperrno.h"
|
2014-10-12 11:16:06 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_UZLIB
|
|
|
|
|
|
|
|
#include "uzlib/tinf.h"
|
|
|
|
|
|
|
|
#if 0 // print debugging info
|
|
|
|
#define DEBUG_printf DEBUG_printf
|
|
|
|
#else // don't print debugging info
|
|
|
|
#define DEBUG_printf(...) (void)0
|
|
|
|
#endif
|
|
|
|
|
2016-09-02 17:07:08 -04:00
|
|
|
typedef struct _mp_obj_decompio_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t src_stream;
|
|
|
|
TINF_DATA decomp;
|
|
|
|
bool eof;
|
|
|
|
} mp_obj_decompio_t;
|
|
|
|
|
2018-11-30 15:48:03 -05:00
|
|
|
STATIC int read_src_stream(TINF_DATA *data) {
|
2020-02-26 23:36:53 -05:00
|
|
|
byte *p = (void *)data;
|
2016-09-02 17:07:08 -04:00
|
|
|
p -= offsetof(mp_obj_decompio_t, decomp);
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_obj_decompio_t *self = (mp_obj_decompio_t *)p;
|
2016-09-02 17:07:08 -04:00
|
|
|
|
2018-06-12 22:37:49 -04:00
|
|
|
const mp_stream_p_t *stream = mp_get_stream(self->src_stream);
|
2016-09-02 17:07:08 -04:00
|
|
|
int err;
|
|
|
|
byte c;
|
|
|
|
mp_uint_t out_sz = stream->read(self->src_stream, &c, 1, &err);
|
|
|
|
if (out_sz == MP_STREAM_ERROR) {
|
2016-10-06 22:52:14 -04:00
|
|
|
mp_raise_OSError(err);
|
2016-09-02 17:07:08 -04:00
|
|
|
}
|
|
|
|
if (out_sz == 0) {
|
2020-02-10 21:17:41 -05:00
|
|
|
mp_raise_type(&mp_type_EOFError);
|
2016-09-02 17:07:08 -04:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t decompio_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2016-09-04 07:44:12 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 1, 2, false);
|
2018-06-12 22:37:49 -04:00
|
|
|
mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
|
2016-09-02 17:07:08 -04:00
|
|
|
mp_obj_decompio_t *o = m_new_obj(mp_obj_decompio_t);
|
|
|
|
o->base.type = type;
|
|
|
|
memset(&o->decomp, 0, sizeof(o->decomp));
|
|
|
|
o->decomp.readSource = read_src_stream;
|
|
|
|
o->src_stream = args[0];
|
|
|
|
o->eof = false;
|
2016-09-04 07:44:12 -04:00
|
|
|
|
|
|
|
mp_int_t dict_opt = 0;
|
|
|
|
int dict_sz;
|
|
|
|
if (n_args > 1) {
|
|
|
|
dict_opt = mp_obj_get_int(args[1]);
|
|
|
|
}
|
2016-09-24 08:30:11 -04:00
|
|
|
|
|
|
|
if (dict_opt >= 16) {
|
|
|
|
int st = uzlib_gzip_parse_header(&o->decomp);
|
|
|
|
if (st != TINF_OK) {
|
|
|
|
goto header_error;
|
|
|
|
}
|
|
|
|
dict_sz = 1 << (dict_opt - 16);
|
|
|
|
} else if (dict_opt >= 0) {
|
2016-09-04 07:44:12 -04:00
|
|
|
dict_opt = uzlib_zlib_parse_header(&o->decomp);
|
|
|
|
if (dict_opt < 0) {
|
2020-02-26 23:36:53 -05:00
|
|
|
header_error:
|
2017-08-09 00:40:45 -04:00
|
|
|
mp_raise_ValueError("compression header");
|
2016-09-04 07:44:12 -04:00
|
|
|
}
|
|
|
|
dict_sz = 1 << dict_opt;
|
|
|
|
} else {
|
|
|
|
dict_sz = 1 << -dict_opt;
|
|
|
|
}
|
|
|
|
|
|
|
|
uzlib_uncompress_init(&o->decomp, m_new(byte, dict_sz), dict_sz);
|
2016-09-02 17:07:08 -04:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_uint_t decompio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
|
|
|
|
mp_obj_decompio_t *o = MP_OBJ_TO_PTR(o_in);
|
|
|
|
if (o->eof) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
o->decomp.dest = buf;
|
2020-02-26 23:36:53 -05:00
|
|
|
o->decomp.dest_limit = (byte *)buf + size;
|
2016-09-02 17:07:08 -04:00
|
|
|
int st = uzlib_uncompress_chksum(&o->decomp);
|
|
|
|
if (st == TINF_DONE) {
|
|
|
|
o->eof = true;
|
|
|
|
}
|
|
|
|
if (st < 0) {
|
2016-09-02 17:34:57 -04:00
|
|
|
*errcode = MP_EINVAL;
|
2016-09-02 17:07:08 -04:00
|
|
|
return MP_STREAM_ERROR;
|
|
|
|
}
|
2020-02-26 23:36:53 -05:00
|
|
|
return o->decomp.dest - (byte *)buf;
|
2016-09-02 17:07:08 -04:00
|
|
|
}
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2016-09-02 17:07:08 -04:00
|
|
|
STATIC const mp_rom_map_elem_t decompio_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(decompio_locals_dict, decompio_locals_dict_table);
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2016-09-02 17:07:08 -04:00
|
|
|
|
|
|
|
STATIC const mp_stream_p_t decompio_stream_p = {
|
|
|
|
.read = decompio_read,
|
|
|
|
};
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2016-09-02 17:07:08 -04:00
|
|
|
STATIC const mp_obj_type_t decompio_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_DecompIO,
|
|
|
|
.make_new = decompio_make_new,
|
|
|
|
.protocol = &decompio_stream_p,
|
2020-02-26 23:36:53 -05:00
|
|
|
.locals_dict = (void *)&decompio_locals_dict,
|
2016-09-02 17:07:08 -04:00
|
|
|
};
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2016-09-02 17:07:08 -04:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mod_uzlib_decompress(size_t n_args, const mp_obj_t *args) {
|
2014-10-12 11:16:06 -04:00
|
|
|
mp_obj_t data = args[0];
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
|
|
|
|
|
|
|
|
TINF_DATA *decomp = m_new_obj(TINF_DATA);
|
2016-08-16 23:05:37 -04:00
|
|
|
memset(decomp, 0, sizeof(*decomp));
|
2014-10-12 11:16:06 -04:00
|
|
|
DEBUG_printf("sizeof(TINF_DATA)=" UINT_FMT "\n", sizeof(*decomp));
|
2016-08-16 23:05:37 -04:00
|
|
|
uzlib_uncompress_init(decomp, NULL, 0);
|
|
|
|
mp_uint_t dest_buf_size = (bufinfo.len + 15) & ~15;
|
|
|
|
byte *dest_buf = m_new(byte, dest_buf_size);
|
2014-10-12 11:16:06 -04:00
|
|
|
|
2016-08-16 23:05:37 -04:00
|
|
|
decomp->dest = dest_buf;
|
2018-11-30 15:48:03 -05:00
|
|
|
decomp->dest_limit = dest_buf + dest_buf_size;
|
|
|
|
DEBUG_printf("uzlib: Initial out buffer: " UINT_FMT " bytes\n", dest_buf_size);
|
2014-10-12 11:16:06 -04:00
|
|
|
decomp->source = bufinfo.buf;
|
2020-02-26 23:36:53 -05:00
|
|
|
decomp->source_limit = (byte *)bufinfo.buf + bufinfo.len;
|
2014-10-12 11:16:06 -04:00
|
|
|
|
2015-01-27 19:10:53 -05:00
|
|
|
int st;
|
2016-08-16 23:05:37 -04:00
|
|
|
bool is_zlib = true;
|
|
|
|
|
2015-01-27 19:10:53 -05:00
|
|
|
if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
|
2016-08-16 23:05:37 -04:00
|
|
|
is_zlib = false;
|
2015-01-27 19:10:53 -05:00
|
|
|
}
|
2016-08-16 23:05:37 -04:00
|
|
|
|
|
|
|
if (is_zlib) {
|
|
|
|
st = uzlib_zlib_parse_header(decomp);
|
|
|
|
if (st < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
st = uzlib_uncompress_chksum(decomp);
|
|
|
|
if (st < 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (st == TINF_DONE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
size_t offset = decomp->dest - dest_buf;
|
|
|
|
dest_buf = m_renew(byte, dest_buf, dest_buf_size, dest_buf_size + 256);
|
|
|
|
dest_buf_size += 256;
|
|
|
|
decomp->dest = dest_buf + offset;
|
2018-11-30 15:48:03 -05:00
|
|
|
decomp->dest_limit = decomp->dest + 256;
|
2014-10-12 11:16:06 -04:00
|
|
|
}
|
|
|
|
|
2016-08-16 23:05:37 -04:00
|
|
|
mp_uint_t final_sz = decomp->dest - dest_buf;
|
|
|
|
DEBUG_printf("uzlib: Resizing from " UINT_FMT " to final size: " UINT_FMT " bytes\n", dest_buf_size, final_sz);
|
2020-02-26 23:36:53 -05:00
|
|
|
dest_buf = (byte *)m_renew(byte, dest_buf, dest_buf_size, final_sz);
|
2016-08-16 23:05:37 -04:00
|
|
|
mp_obj_t res = mp_obj_new_bytearray_by_ref(final_sz, dest_buf);
|
2014-10-12 11:16:06 -04:00
|
|
|
m_del_obj(TINF_DATA, decomp);
|
|
|
|
return res;
|
2016-08-16 23:05:37 -04:00
|
|
|
|
|
|
|
error:
|
2020-02-26 23:36:53 -05:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st)));
|
2014-10-12 11:16:06 -04:00
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_uzlib_decompress_obj, 1, 3, mod_uzlib_decompress);
|
|
|
|
|
2019-11-04 19:38:01 -05:00
|
|
|
#if !MICROPY_ENABLE_DYNRUNTIME
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_uzlib_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uzlib) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_decompress), MP_ROM_PTR(&mod_uzlib_decompress_obj) },
|
2016-09-02 17:07:08 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_DecompIO), MP_ROM_PTR(&decompio_type) },
|
2014-10-12 11:16:06 -04:00
|
|
|
};
|
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_uzlib_globals, mp_module_uzlib_globals_table);
|
2014-10-12 11:16:06 -04:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_uzlib = {
|
|
|
|
.base = { &mp_type_module },
|
2020-02-26 23:36:53 -05:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_uzlib_globals,
|
2014-10-12 11:16:06 -04:00
|
|
|
};
|
2019-11-04 19:38:01 -05:00
|
|
|
#endif
|
2014-10-12 11:16:06 -04:00
|
|
|
|
|
|
|
// Source files #include'd here to make sure they're compiled in
|
|
|
|
// only if module is enabled by config setting.
|
|
|
|
|
|
|
|
#include "uzlib/tinflate.c"
|
|
|
|
#include "uzlib/tinfzlib.c"
|
2016-09-24 08:30:11 -04:00
|
|
|
#include "uzlib/tinfgzip.c"
|
2014-10-12 11:16:06 -04:00
|
|
|
#include "uzlib/adler32.c"
|
2016-08-16 23:05:37 -04:00
|
|
|
#include "uzlib/crc32.c"
|
2014-10-12 11:16:06 -04:00
|
|
|
|
2014-10-12 18:35:38 -04:00
|
|
|
#endif // MICROPY_PY_UZLIB
|