Skip to content

Commit 510296f

Browse files
committed
modzlibd: Decompress part of "zlib" module, based on miniz tinfl.c .
1 parent 75ec22b commit 510296f

7 files changed

Lines changed: 125 additions & 1 deletion

File tree

extmod/modzlibd.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2014 Paul Sokolovsky
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
#include <unistd.h>
29+
#include <string.h>
30+
#include <time.h>
31+
#include <sys/time.h>
32+
#include <math.h>
33+
34+
#include "mpconfig.h"
35+
#include "misc.h"
36+
#include "qstr.h"
37+
#include "obj.h"
38+
#include "runtime.h"
39+
40+
#if MICROPY_PY_ZLIBD
41+
42+
#include "miniz/tinfl.c"
43+
44+
#if 0 // print debugging info
45+
#define DEBUG_printf DEBUG_printf
46+
#else // don't print debugging info
47+
#define DEBUG_printf(...) (void)0
48+
#endif
49+
50+
STATIC mp_obj_t mod_zlib_decompress(uint n_args, mp_obj_t *args) {
51+
mp_obj_t data = args[0];
52+
mp_buffer_info_t bufinfo;
53+
mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
54+
tinfl_decompressor *decomp = m_new_obj(tinfl_decompressor);
55+
tinfl_init(decomp);
56+
DEBUG_printf("sizeof(tinfl_decompressor)=%d\n", sizeof(tinfl_decompressor));
57+
58+
byte *out = m_new(byte, bufinfo.len);
59+
size_t out_len = bufinfo.len;
60+
size_t in_buf_ofs = 0, dst_buf_ofs = 0;
61+
size_t dst_buf_sz = bufinfo.len;
62+
63+
while (1) {
64+
size_t in_buf_sz = bufinfo.len - in_buf_ofs;
65+
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);
66+
tinfl_status st = tinfl_decompress(decomp,
67+
bufinfo.buf + in_buf_ofs, &in_buf_sz,
68+
out, out + dst_buf_ofs, &dst_buf_sz,
69+
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF | TINFL_FLAG_PARSE_ZLIB_HEADER);
70+
DEBUG_printf("tinfl out: st=%d, in_sz=%d, out_sz=%d\n", st, in_buf_sz, dst_buf_sz);
71+
in_buf_ofs += in_buf_sz;
72+
dst_buf_ofs += dst_buf_sz;
73+
if (st != TINFL_STATUS_HAS_MORE_OUTPUT) {
74+
break;
75+
}
76+
out = m_renew(byte, out, out_len, dst_buf_ofs + 256);
77+
out_len = dst_buf_ofs + 256;
78+
dst_buf_sz = out_len - dst_buf_ofs;
79+
}
80+
81+
m_del_obj(tinfl_decompressor, decomp);
82+
return mp_obj_new_bytearray_by_ref(dst_buf_ofs, out);
83+
}
84+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_zlib_decompress_obj, 1, 3, mod_zlib_decompress);
85+
86+
STATIC const mp_map_elem_t mp_module_zlib_globals_table[] = {
87+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_zlibd) },
88+
{ MP_OBJ_NEW_QSTR(MP_QSTR_decompress), (mp_obj_t)&mod_zlib_decompress_obj },
89+
};
90+
91+
STATIC const mp_obj_dict_t mp_module_zlib_globals = {
92+
.base = {&mp_type_dict},
93+
.map = {
94+
.all_keys_are_qstrs = 1,
95+
.table_is_fixed_array = 1,
96+
.used = MP_ARRAY_SIZE(mp_module_zlib_globals_table),
97+
.alloc = MP_ARRAY_SIZE(mp_module_zlib_globals_table),
98+
.table = (mp_map_elem_t*)mp_module_zlib_globals_table,
99+
},
100+
};
101+
102+
const mp_obj_module_t mp_module_zlibd = {
103+
.base = { &mp_type_module },
104+
.name = MP_QSTR_zlibd,
105+
.globals = (mp_obj_dict_t*)&mp_module_zlib_globals,
106+
};
107+
108+
#endif //MICROPY_PY_ZLIBD

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,4 @@ extern struct _dummy_t mp_sys_stderr_obj;
8989

9090
// extmod modules
9191
extern const mp_obj_module_t mp_module_uctypes;
92+
extern const mp_obj_module_t mp_module_zlibd;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
196196
#if MICROPY_PY_UCTYPES
197197
{ MP_OBJ_NEW_QSTR(MP_QSTR_uctypes), (mp_obj_t)&mp_module_uctypes },
198198
#endif
199+
#if MICROPY_PY_ZLIBD
200+
{ MP_OBJ_NEW_QSTR(MP_QSTR_zlibd), (mp_obj_t)&mp_module_zlibd },
201+
#endif
199202

200203
// extra builtin modules as defined by a port
201204
MICROPY_PORT_BUILTIN_MODULES

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,15 @@ typedef double mp_float_t;
368368

369369

370370
// Extended modules
371+
371372
#ifndef MICROPY_PY_UCTYPES
372373
#define MICROPY_PY_UCTYPES (0)
373374
#endif
374375

376+
#ifndef MICROPY_PY_ZLIBD
377+
#define MICROPY_PY_ZLIBD (0)
378+
#endif
379+
375380
/*****************************************************************************/
376381
/* Hooks for a port to add builtins */
377382

py/py.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ PY_O_BASENAME = \
104104
smallint.o \
105105
pfenv.o \
106106
pfenv_printf.o \
107-
../extmod/moductypes.o
107+
../extmod/moductypes.o \
108+
../extmod/modzlibd.o \
108109

109110
# prepend the build destination prefix to the py object files
110111
PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))

py/qstrdefs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,8 @@ Q(getter)
450450
Q(setter)
451451
Q(deleter)
452452
#endif
453+
454+
#if MICROPY_PY_ZLIBD
455+
Q(zlibd)
456+
Q(decompress)
457+
#endif

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#define MICROPY_PY_GC_COLLECT_RETVAL (1)
5555

5656
#define MICROPY_PY_UCTYPES (1)
57+
#define MICROPY_PY_ZLIBD (1)
5758

5859
// Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
5960
// names in exception messages (may require more RAM).

0 commit comments

Comments
 (0)