moduzlib.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014-2016 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. #include <stdio.h>
  27. #include <string.h>
  28. #include "py/runtime.h"
  29. #include "py/stream.h"
  30. #include "py/mperrno.h"
  31. #if MICROPY_PY_UZLIB
  32. #include "uzlib/tinf.h"
  33. #if 0 // print debugging info
  34. #define DEBUG_printf DEBUG_printf
  35. #else // don't print debugging info
  36. #define DEBUG_printf(...) (void)0
  37. #endif
  38. typedef struct _mp_obj_decompio_t {
  39. mp_obj_base_t base;
  40. mp_obj_t src_stream;
  41. TINF_DATA decomp;
  42. bool eof;
  43. } mp_obj_decompio_t;
  44. STATIC unsigned char read_src_stream(TINF_DATA *data) {
  45. byte *p = (void*)data;
  46. p -= offsetof(mp_obj_decompio_t, decomp);
  47. mp_obj_decompio_t *self = (mp_obj_decompio_t*)p;
  48. const mp_stream_p_t *stream = mp_get_stream(self->src_stream);
  49. int err;
  50. byte c;
  51. mp_uint_t out_sz = stream->read(self->src_stream, &c, 1, &err);
  52. if (out_sz == MP_STREAM_ERROR) {
  53. mp_raise_OSError(err);
  54. }
  55. if (out_sz == 0) {
  56. nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
  57. }
  58. return c;
  59. }
  60. 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) {
  61. mp_arg_check_num(n_args, n_kw, 1, 2, false);
  62. mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
  63. mp_obj_decompio_t *o = m_new_obj(mp_obj_decompio_t);
  64. o->base.type = type;
  65. memset(&o->decomp, 0, sizeof(o->decomp));
  66. o->decomp.readSource = read_src_stream;
  67. o->src_stream = args[0];
  68. o->eof = false;
  69. mp_int_t dict_opt = 0;
  70. int dict_sz;
  71. if (n_args > 1) {
  72. dict_opt = mp_obj_get_int(args[1]);
  73. }
  74. if (dict_opt >= 16) {
  75. int st = uzlib_gzip_parse_header(&o->decomp);
  76. if (st != TINF_OK) {
  77. goto header_error;
  78. }
  79. dict_sz = 1 << (dict_opt - 16);
  80. } else if (dict_opt >= 0) {
  81. dict_opt = uzlib_zlib_parse_header(&o->decomp);
  82. if (dict_opt < 0) {
  83. header_error:
  84. mp_raise_ValueError("compression header");
  85. }
  86. dict_sz = 1 << dict_opt;
  87. } else {
  88. dict_sz = 1 << -dict_opt;
  89. }
  90. uzlib_uncompress_init(&o->decomp, m_new(byte, dict_sz), dict_sz);
  91. return MP_OBJ_FROM_PTR(o);
  92. }
  93. STATIC mp_uint_t decompio_read(mp_obj_t o_in, void *buf, mp_uint_t size, int *errcode) {
  94. mp_obj_decompio_t *o = MP_OBJ_TO_PTR(o_in);
  95. if (o->eof) {
  96. return 0;
  97. }
  98. o->decomp.dest = buf;
  99. o->decomp.destSize = size;
  100. int st = uzlib_uncompress_chksum(&o->decomp);
  101. if (st == TINF_DONE) {
  102. o->eof = true;
  103. }
  104. if (st < 0) {
  105. *errcode = MP_EINVAL;
  106. return MP_STREAM_ERROR;
  107. }
  108. return o->decomp.dest - (byte*)buf;
  109. }
  110. STATIC const mp_rom_map_elem_t decompio_locals_dict_table[] = {
  111. { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
  112. { MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
  113. { MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
  114. };
  115. STATIC MP_DEFINE_CONST_DICT(decompio_locals_dict, decompio_locals_dict_table);
  116. STATIC const mp_stream_p_t decompio_stream_p = {
  117. .read = decompio_read,
  118. };
  119. STATIC const mp_obj_type_t decompio_type = {
  120. { &mp_type_type },
  121. .name = MP_QSTR_DecompIO,
  122. .make_new = decompio_make_new,
  123. .protocol = &decompio_stream_p,
  124. .locals_dict = (void*)&decompio_locals_dict,
  125. };
  126. STATIC mp_obj_t mod_uzlib_decompress(size_t n_args, const mp_obj_t *args) {
  127. mp_obj_t data = args[0];
  128. mp_buffer_info_t bufinfo;
  129. mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
  130. TINF_DATA *decomp = m_new_obj(TINF_DATA);
  131. memset(decomp, 0, sizeof(*decomp));
  132. DEBUG_printf("sizeof(TINF_DATA)=" UINT_FMT "\n", sizeof(*decomp));
  133. uzlib_uncompress_init(decomp, NULL, 0);
  134. mp_uint_t dest_buf_size = (bufinfo.len + 15) & ~15;
  135. byte *dest_buf = m_new(byte, dest_buf_size);
  136. decomp->dest = dest_buf;
  137. decomp->destSize = dest_buf_size;
  138. DEBUG_printf("uzlib: Initial out buffer: " UINT_FMT " bytes\n", decomp->destSize);
  139. decomp->source = bufinfo.buf;
  140. int st;
  141. bool is_zlib = true;
  142. if (n_args > 1 && MP_OBJ_SMALL_INT_VALUE(args[1]) < 0) {
  143. is_zlib = false;
  144. }
  145. if (is_zlib) {
  146. st = uzlib_zlib_parse_header(decomp);
  147. if (st < 0) {
  148. goto error;
  149. }
  150. }
  151. while (1) {
  152. st = uzlib_uncompress_chksum(decomp);
  153. if (st < 0) {
  154. goto error;
  155. }
  156. if (st == TINF_DONE) {
  157. break;
  158. }
  159. size_t offset = decomp->dest - dest_buf;
  160. dest_buf = m_renew(byte, dest_buf, dest_buf_size, dest_buf_size + 256);
  161. dest_buf_size += 256;
  162. decomp->dest = dest_buf + offset;
  163. decomp->destSize = 256;
  164. }
  165. mp_uint_t final_sz = decomp->dest - dest_buf;
  166. DEBUG_printf("uzlib: Resizing from " UINT_FMT " to final size: " UINT_FMT " bytes\n", dest_buf_size, final_sz);
  167. dest_buf = (byte*)m_renew(byte, dest_buf, dest_buf_size, final_sz);
  168. mp_obj_t res = mp_obj_new_bytearray_by_ref(final_sz, dest_buf);
  169. m_del_obj(TINF_DATA, decomp);
  170. return res;
  171. error:
  172. nlr_raise(mp_obj_new_exception_arg1(&mp_type_ValueError, MP_OBJ_NEW_SMALL_INT(st)));
  173. }
  174. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_uzlib_decompress_obj, 1, 3, mod_uzlib_decompress);
  175. STATIC const mp_rom_map_elem_t mp_module_uzlib_globals_table[] = {
  176. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uzlib) },
  177. { MP_ROM_QSTR(MP_QSTR_decompress), MP_ROM_PTR(&mod_uzlib_decompress_obj) },
  178. { MP_ROM_QSTR(MP_QSTR_DecompIO), MP_ROM_PTR(&decompio_type) },
  179. };
  180. STATIC MP_DEFINE_CONST_DICT(mp_module_uzlib_globals, mp_module_uzlib_globals_table);
  181. const mp_obj_module_t mp_module_uzlib = {
  182. .base = { &mp_type_module },
  183. .globals = (mp_obj_dict_t*)&mp_module_uzlib_globals,
  184. };
  185. // Source files #include'd here to make sure they're compiled in
  186. // only if module is enabled by config setting.
  187. #include "uzlib/tinflate.c"
  188. #include "uzlib/tinfzlib.c"
  189. #include "uzlib/tinfgzip.c"
  190. #include "uzlib/adler32.c"
  191. #include "uzlib/crc32.c"
  192. #endif // MICROPY_PY_UZLIB