From 510296f25ae53e5732fe98cdd4b68bfeea15204a Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Fri, 8 Aug 2014 22:51:40 +0300 Subject: modzlibd: Decompress part of "zlib" module, based on miniz tinfl.c . --- extmod/modzlibd.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 extmod/modzlibd.c (limited to 'extmod/modzlibd.c') diff --git a/extmod/modzlibd.c b/extmod/modzlibd.c new file mode 100644 index 000000000..2b9c9f5b2 --- /dev/null +++ b/extmod/modzlibd.c @@ -0,0 +1,108 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2014 Paul Sokolovsky + * + * 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 +#include +#include +#include +#include +#include + +#include "mpconfig.h" +#include "misc.h" +#include "qstr.h" +#include "obj.h" +#include "runtime.h" + +#if MICROPY_PY_ZLIBD + +#include "miniz/tinfl.c" + +#if 0 // print debugging info +#define DEBUG_printf DEBUG_printf +#else // don't print debugging info +#define DEBUG_printf(...) (void)0 +#endif + +STATIC mp_obj_t mod_zlib_decompress(uint n_args, mp_obj_t *args) { + mp_obj_t data = args[0]; + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); + tinfl_decompressor *decomp = m_new_obj(tinfl_decompressor); + tinfl_init(decomp); + DEBUG_printf("sizeof(tinfl_decompressor)=%d\n", sizeof(tinfl_decompressor)); + + byte *out = m_new(byte, bufinfo.len); + size_t out_len = bufinfo.len; + size_t in_buf_ofs = 0, dst_buf_ofs = 0; + size_t dst_buf_sz = bufinfo.len; + + while (1) { + size_t in_buf_sz = bufinfo.len - in_buf_ofs; + DEBUG_printf("tinfl in: in_ofs=%d in_sz=%d dst_ofs=%d, dst_sz=%d\n", in_buf_ofs, in_buf_sz, dst_buf_ofs, dst_buf_sz); + tinfl_status st = tinfl_decompress(decomp, + bufinfo.buf + in_buf_ofs, &in_buf_sz, + out, out + dst_buf_ofs, &dst_buf_sz, + TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | TINFL_FLAG_PARSE_ZLIB_HEADER); + DEBUG_printf("tinfl out: st=%d, in_sz=%d, out_sz=%d\n", st, in_buf_sz, dst_buf_sz); + in_buf_ofs += in_buf_sz; + dst_buf_ofs += dst_buf_sz; + if (st != TINFL_STATUS_HAS_MORE_OUTPUT) { + break; + } + out = m_renew(byte, out, out_len, dst_buf_ofs + 256); + out_len = dst_buf_ofs + 256; + dst_buf_sz = out_len - dst_buf_ofs; + } + + m_del_obj(tinfl_decompressor, decomp); + return mp_obj_new_bytearray_by_ref(dst_buf_ofs, out); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_zlib_decompress_obj, 1, 3, mod_zlib_decompress); + +STATIC const mp_map_elem_t mp_module_zlib_globals_table[] = { + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_zlibd) }, + { MP_OBJ_NEW_QSTR(MP_QSTR_decompress), (mp_obj_t)&mod_zlib_decompress_obj }, +}; + +STATIC const mp_obj_dict_t mp_module_zlib_globals = { + .base = {&mp_type_dict}, + .map = { + .all_keys_are_qstrs = 1, + .table_is_fixed_array = 1, + .used = MP_ARRAY_SIZE(mp_module_zlib_globals_table), + .alloc = MP_ARRAY_SIZE(mp_module_zlib_globals_table), + .table = (mp_map_elem_t*)mp_module_zlib_globals_table, + }, +}; + +const mp_obj_module_t mp_module_zlibd = { + .base = { &mp_type_module }, + .name = MP_QSTR_zlibd, + .globals = (mp_obj_dict_t*)&mp_module_zlib_globals, +}; + +#endif //MICROPY_PY_ZLIBD -- cgit v1.2.3 From 1ddd844815014d01f342489ced131bc73980e0be Mon Sep 17 00:00:00 2001 From: Damien George Date: Tue, 12 Aug 2014 23:23:53 +0100 Subject: extmod: Finish rename of zlib to zlibd; enable zlibd on stmhal. --- extmod/modzlibd.c | 18 +++++++++--------- stmhal/mpconfigport.h | 1 + tests/extmod/zlib_decompress.py | 16 ---------------- tests/extmod/zlibd_decompress.py | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 25 deletions(-) delete mode 100644 tests/extmod/zlib_decompress.py create mode 100644 tests/extmod/zlibd_decompress.py (limited to 'extmod/modzlibd.c') diff --git a/extmod/modzlibd.c b/extmod/modzlibd.c index 2b9c9f5b2..ca21d26fe 100644 --- a/extmod/modzlibd.c +++ b/extmod/modzlibd.c @@ -47,7 +47,7 @@ #define DEBUG_printf(...) (void)0 #endif -STATIC mp_obj_t mod_zlib_decompress(uint n_args, mp_obj_t *args) { +STATIC mp_obj_t mod_zlibd_decompress(uint n_args, mp_obj_t *args) { mp_obj_t data = args[0]; mp_buffer_info_t bufinfo; mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); @@ -81,28 +81,28 @@ STATIC mp_obj_t mod_zlib_decompress(uint n_args, mp_obj_t *args) { m_del_obj(tinfl_decompressor, decomp); return mp_obj_new_bytearray_by_ref(dst_buf_ofs, out); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_zlib_decompress_obj, 1, 3, mod_zlib_decompress); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_zlibd_decompress_obj, 1, 3, mod_zlibd_decompress); -STATIC const mp_map_elem_t mp_module_zlib_globals_table[] = { +STATIC const mp_map_elem_t mp_module_zlibd_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_zlibd) }, - { MP_OBJ_NEW_QSTR(MP_QSTR_decompress), (mp_obj_t)&mod_zlib_decompress_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_decompress), (mp_obj_t)&mod_zlibd_decompress_obj }, }; -STATIC const mp_obj_dict_t mp_module_zlib_globals = { +STATIC const mp_obj_dict_t mp_module_zlibd_globals = { .base = {&mp_type_dict}, .map = { .all_keys_are_qstrs = 1, .table_is_fixed_array = 1, - .used = MP_ARRAY_SIZE(mp_module_zlib_globals_table), - .alloc = MP_ARRAY_SIZE(mp_module_zlib_globals_table), - .table = (mp_map_elem_t*)mp_module_zlib_globals_table, + .used = MP_ARRAY_SIZE(mp_module_zlibd_globals_table), + .alloc = MP_ARRAY_SIZE(mp_module_zlibd_globals_table), + .table = (mp_map_elem_t*)mp_module_zlibd_globals_table, }, }; const mp_obj_module_t mp_module_zlibd = { .base = { &mp_type_module }, .name = MP_QSTR_zlibd, - .globals = (mp_obj_dict_t*)&mp_module_zlib_globals, + .globals = (mp_obj_dict_t*)&mp_module_zlibd_globals, }; #endif //MICROPY_PY_ZLIBD diff --git a/stmhal/mpconfigport.h b/stmhal/mpconfigport.h index a70782f6d..f51ba0d62 100644 --- a/stmhal/mpconfigport.h +++ b/stmhal/mpconfigport.h @@ -52,6 +52,7 @@ #define MICROPY_PY_IO (1) #define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_UCTYPES (1) +#define MICROPY_PY_ZLIBD (1) #define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1) #define MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE (0) diff --git a/tests/extmod/zlib_decompress.py b/tests/extmod/zlib_decompress.py deleted file mode 100644 index db9610832..000000000 --- a/tests/extmod/zlib_decompress.py +++ /dev/null @@ -1,16 +0,0 @@ -try: - import zlib -except ImportError: - import zlibd as zlib - -PATTERNS = [ - # Packed results produced by CPy's zlib.compress() - (b'0', b'x\x9c3\x00\x00\x001\x001'), - (b'a', b'x\x9cK\x04\x00\x00b\x00b'), - (b'0' * 100, b'x\x9c30\xa0=\x00\x00\xb3q\x12\xc1'), - (bytes(range(64)), b'x\x9cc`dbfaec\xe7\xe0\xe4\xe2\xe6\xe1\xe5\xe3\x17\x10\x14\x12\x16\x11\x15\x13\x97\x90\x94\x92\x96\x91\x95\x93WPTRVQUS\xd7\xd0\xd4\xd2\xd6\xd1\xd5\xd370426153\xb7\xb0\xb4\xb2\xb6\xb1\xb5\xb3\x07\x00\xaa\xe0\x07\xe1'), -] - -for unpacked, packed in PATTERNS: - assert zlib.decompress(packed) == unpacked - print(unpacked) diff --git a/tests/extmod/zlibd_decompress.py b/tests/extmod/zlibd_decompress.py new file mode 100644 index 000000000..db9610832 --- /dev/null +++ b/tests/extmod/zlibd_decompress.py @@ -0,0 +1,16 @@ +try: + import zlib +except ImportError: + import zlibd as zlib + +PATTERNS = [ + # Packed results produced by CPy's zlib.compress() + (b'0', b'x\x9c3\x00\x00\x001\x001'), + (b'a', b'x\x9cK\x04\x00\x00b\x00b'), + (b'0' * 100, b'x\x9c30\xa0=\x00\x00\xb3q\x12\xc1'), + (bytes(range(64)), b'x\x9cc`dbfaec\xe7\xe0\xe4\xe2\xe6\xe1\xe5\xe3\x17\x10\x14\x12\x16\x11\x15\x13\x97\x90\x94\x92\x96\x91\x95\x93WPTRVQUS\xd7\xd0\xd4\xd2\xd6\xd1\xd5\xd370426153\xb7\xb0\xb4\xb2\xb6\xb1\xb5\xb3\x07\x00\xaa\xe0\x07\xe1'), +] + +for unpacked, packed in PATTERNS: + assert zlib.decompress(packed) == unpacked + print(unpacked) -- cgit v1.2.3