|
| 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 |
0 commit comments