|
| 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 "nlr.h" |
| 38 | +#include "obj.h" |
| 39 | +#include "runtime.h" |
| 40 | + |
| 41 | +#if MICROPY_PY_UZLIB |
| 42 | + |
| 43 | +#include "uzlib/tinf.h" |
| 44 | + |
| 45 | +#if 0 // print debugging info |
| 46 | +#define DEBUG_printf DEBUG_printf |
| 47 | +#else // don't print debugging info |
| 48 | +#define DEBUG_printf(...) (void)0 |
| 49 | +#endif |
| 50 | + |
| 51 | +STATIC int mod_uzlib_grow_buf(TINF_DATA *d, unsigned alloc_req) |
| 52 | +{ |
| 53 | + if (alloc_req < 256) { |
| 54 | + alloc_req = 256; |
| 55 | + } |
| 56 | + DEBUG_printf("uzlib: Resizing buffer to " UINT_FMT " bytes\n", d->destSize + alloc_req); |
| 57 | + d->destStart = m_renew(byte, d->destStart, d->destSize, d->destSize + alloc_req); |
| 58 | + d->destSize += alloc_req; |
| 59 | + return 0; |
| 60 | +} |
| 61 | + |
| 62 | +STATIC mp_obj_t mod_uzlib_decompress(uint n_args, mp_obj_t *args) { |
| 63 | + mp_obj_t data = args[0]; |
| 64 | + mp_buffer_info_t bufinfo; |
| 65 | + mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); |
| 66 | + |
| 67 | + TINF_DATA *decomp = m_new_obj(TINF_DATA); |
| 68 | + DEBUG_printf("sizeof(TINF_DATA)=" UINT_FMT "\n", sizeof(*decomp)); |
| 69 | + |
| 70 | + decomp->destStart = m_new(byte, bufinfo.len); |
| 71 | + decomp->destSize = bufinfo.len; |
| 72 | + decomp->destGrow = mod_uzlib_grow_buf; |
| 73 | + decomp->source = bufinfo.buf; |
| 74 | + |
| 75 | + int st = tinf_zlib_uncompress_dyn(decomp, bufinfo.len); |
| 76 | + if (st != 0) { |
| 77 | + nlr_raise(mp_obj_new_exception_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st))); |
| 78 | + } |
| 79 | + |
| 80 | + mp_obj_t res = mp_obj_new_bytearray_by_ref(decomp->dest - decomp->destStart, decomp->destStart); |
| 81 | + m_del_obj(TINF_DATA, decomp); |
| 82 | + return res; |
| 83 | +} |
| 84 | +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_uzlib_decompress_obj, 1, 3, mod_uzlib_decompress); |
| 85 | + |
| 86 | +STATIC const mp_map_elem_t mp_module_uzlib_globals_table[] = { |
| 87 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_uzlib) }, |
| 88 | + { MP_OBJ_NEW_QSTR(MP_QSTR_decompress), (mp_obj_t)&mod_uzlib_decompress_obj }, |
| 89 | +}; |
| 90 | + |
| 91 | +STATIC const mp_obj_dict_t mp_module_uzlib_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_uzlib_globals_table), |
| 97 | + .alloc = MP_ARRAY_SIZE(mp_module_uzlib_globals_table), |
| 98 | + .table = (mp_map_elem_t*)mp_module_uzlib_globals_table, |
| 99 | + }, |
| 100 | +}; |
| 101 | + |
| 102 | +const mp_obj_module_t mp_module_uzlib = { |
| 103 | + .base = { &mp_type_module }, |
| 104 | + .name = MP_QSTR_uzlib, |
| 105 | + .globals = (mp_obj_dict_t*)&mp_module_uzlib_globals, |
| 106 | +}; |
| 107 | + |
| 108 | +// Source files #include'd here to make sure they're compiled in |
| 109 | +// only if module is enabled by config setting. |
| 110 | + |
| 111 | +#include "uzlib/tinflate.c" |
| 112 | +#include "uzlib/tinfzlib.c" |
| 113 | +#include "uzlib/adler32.c" |
| 114 | + |
| 115 | +#endif //MICROPY_PY_UZLIB |
0 commit comments