diff --git a/docs/shared_bindings_matrix.py b/docs/shared_bindings_matrix.py index 2dad572995..7b559ad665 100644 --- a/docs/shared_bindings_matrix.py +++ b/docs/shared_bindings_matrix.py @@ -63,7 +63,7 @@ ALIASES_BRAND_NAMES = { } ADDITIONAL_MODULES = { - "_asyncio": "MICROPY_PY_UASYNCIO", + "_asyncio": "MICROPY_PY_ASYNCIO", "adafruit_bus_device": "CIRCUITPY_BUSDEVICE", "adafruit_pixelbuf": "CIRCUITPY_PIXELBUF", "array": "CIRCUITPY_ARRAY", @@ -79,7 +79,7 @@ ADDITIONAL_MODULES = { "keypad.Keys": "CIRCUITPY_KEYPAD_KEYS", "keypad.ShiftRegisterKeys": "CIRCUITPY_KEYPAD_SHIFTREGISTERKEYS", "os.getenv": "CIRCUITPY_OS_GETENV", - "select": "MICROPY_PY_USELECT_SELECT", + "select": "MICROPY_PY_SELECT_SELECT", "sys": "CIRCUITPY_SYS", "terminalio": "CIRCUITPY_DISPLAYIO", "usb": "CIRCUITPY_USB_HOST", diff --git a/extmod/modbinascii.c b/extmod/modbinascii.c index 4036a3dcda..78d4c891f8 100644 --- a/extmod/modbinascii.c +++ b/extmod/modbinascii.c @@ -32,8 +32,6 @@ #include "py/binary.h" #include "py/objstr.h" -//#include "lib/uzlib/tinf.h" - #if MICROPY_PY_BINASCII static void check_not_unicode(const mp_obj_t arg) { @@ -182,44 +180,9 @@ STATIC mp_obj_t mod_binascii_b2a_base64(size_t n_args, const mp_obj_t *pos_args, } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_binascii_b2a_base64_obj, 1, mod_binascii_b2a_base64); -// CIRCUITPY-CHANGE: uses a self-contained implementation of CRC32, -// instead of depending on uzlib, like MicroPython. - -/* - * CRC32 checksum - * - * Copyright (c) 1998-2003 by Joergen Ibsen / Jibz - * All Rights Reserved - * - * http://www.ibsensoftware.com/ - * - * This software is provided 'as-is', without any express - * or implied warranty. In no event will the authors be - * held liable for any damages arising from the use of - * this software. - * - * Permission is granted to anyone to use this software - * for any purpose, including commercial applications, - * and to alter it and redistribute it freely, subject to - * the following restrictions: - * - * 1. The origin of this software must not be - * misrepresented; you must not claim that you - * wrote the original software. If you use this - * software in a product, an acknowledgment in - * the product documentation would be appreciated - * but is not required. - * - * 2. Altered source versions must be plainly marked - * as such, and must not be misrepresented as - * being the original software. - * - * 3. This notice may not be removed or altered from - * any source distribution. - */ - -#if MICROPY_PY_UBINASCII_CRC32 -//#include "lib/uzlib/uzlib.h" +// CIRCUITPY-CHANGE: no deflate +#if MICROPY_PY_BINASCII_CRC32 +#include "lib/uzlib/uzlib.h" STATIC mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t bufinfo; @@ -241,6 +204,7 @@ STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) }, { MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) }, + // CIRCUITPY-CHANGE: no deflate #if MICROPY_PY_BINASCII_CRC32 { MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&mod_binascii_crc32_obj) }, #endif @@ -248,7 +212,7 @@ STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = { STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table); -const mp_obj_module_t mp_module_ubinascii = { +const mp_obj_module_t mp_module_binascii = { .base = { &mp_type_module }, .globals = (mp_obj_dict_t *)&mp_module_binascii_globals, }; diff --git a/extmod/modjson.c b/extmod/modjson.c index d5ab2744b2..d001f225a5 100644 --- a/extmod/modjson.c +++ b/extmod/modjson.c @@ -26,6 +26,8 @@ #include +#include "py/binary.h" +#include "py/objarray.h" #include "py/objlist.h" #include "py/objstringio.h" #include "py/parsenum.h" @@ -145,6 +147,7 @@ typedef struct _json_stream_t { STATIC byte json_stream_next(json_stream_t *s) { mp_uint_t ret = s->read(s->stream_obj, &s->cur, 1, &s->errcode); + JSON_DEBUG(" usjon_stream_next err:%2d cur: %c \n", s->errcode, s->cur); if (s->errcode != 0) { mp_raise_OSError(s->errcode); } @@ -163,7 +166,7 @@ STATIC byte json_stream_next(json_stream_t *s) { STATIC mp_uint_t json_python_readinto(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode) { (void)size; // Ignore size because we know it's always 1. - ujson_stream_t *s = obj; + json_stream_t *s = obj; if (s->start == s->end) { *errcode = 0; @@ -186,7 +189,7 @@ STATIC mp_uint_t json_python_readinto(mp_obj_t obj, void *buf, mp_uint_t size, i STATIC mp_obj_t _mod_json_load(mp_obj_t stream_obj, bool return_first_json) { const mp_stream_p_t *stream_p = mp_proto_get(MP_QSTR_protocol_stream, stream_obj); - ujson_stream_t s; + json_stream_t s; uint8_t character_buffer[CIRCUITPY_JSON_READ_CHUNK_SIZE]; if (stream_p == NULL) { s.start = 0; @@ -199,7 +202,7 @@ STATIC mp_obj_t _mod_json_load(mp_obj_t stream_obj, bool return_first_json) { s.bytearray_obj.items = character_buffer; s.python_readinto[2] = MP_OBJ_FROM_PTR(&s.bytearray_obj); s.stream_obj = &s; - s.read = ujson_python_readinto; + s.read = json_python_readinto; } else { stream_p = mp_get_stream_raise(stream_obj, MP_STREAM_OP_READ); s.stream_obj = stream_obj; diff --git a/extmod/modrandom.c b/extmod/modrandom.c index 33c77e8b93..e65f31488b 100644 --- a/extmod/modrandom.c +++ b/extmod/modrandom.c @@ -67,7 +67,7 @@ STATIC uint32_t yasmarang(void) { // End of Yasmarang -#if MICROPY_PY_URANDOM_EXTRA_FUNCS +#if MICROPY_PY_RANDOM_EXTRA_FUNCS // returns an unsigned integer below the given argument // n must not be zero @@ -85,7 +85,7 @@ STATIC uint32_t yasmarang_randbelow(uint32_t n) { #endif -STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { +STATIC mp_obj_t mod_random_getrandbits(mp_obj_t num_in) { int n = mp_obj_get_int(num_in); if (n > 32 || n < 0) { mp_raise_ValueError(MP_ERROR_TEXT("bits must be 32 or less")); @@ -98,13 +98,13 @@ STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { mask >>= (32 - n); return mp_obj_new_int_from_uint(yasmarang() & mask); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_getrandbits_obj, mod_urandom_getrandbits); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_random_getrandbits_obj, mod_random_getrandbits); -STATIC mp_obj_t mod_urandom_seed(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t mod_random_seed(size_t n_args, const mp_obj_t *args) { mp_uint_t seed; if (n_args == 0 || args[0] == mp_const_none) { - #ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC - seed = MICROPY_PY_URANDOM_SEED_INIT_FUNC; + #ifdef MICROPY_PY_RANDOM_SEED_INIT_FUNC + seed = MICROPY_PY_RANDOM_SEED_INIT_FUNC; #else mp_raise_ValueError(MP_ERROR_TEXT("no default seed")); #endif @@ -117,11 +117,11 @@ STATIC mp_obj_t mod_urandom_seed(size_t n_args, const mp_obj_t *args) { yasmarang_dat = 0; return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_seed_obj, 0, 1, mod_urandom_seed); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_random_seed_obj, 0, 1, mod_random_seed); -#if MICROPY_PY_URANDOM_EXTRA_FUNCS +#if MICROPY_PY_RANDOM_EXTRA_FUNCS -STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { +STATIC mp_obj_t mod_random_randrange(size_t n_args, const mp_obj_t *args) { mp_int_t start = mp_obj_get_int(args[0]); if (n_args == 1) { // range(stop) @@ -161,9 +161,9 @@ STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { error: mp_raise_ValueError(NULL); } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_randrange_obj, 1, 3, mod_urandom_randrange); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_random_randrange_obj, 1, 3, mod_random_randrange); -STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) { +STATIC mp_obj_t mod_random_randint(mp_obj_t a_in, mp_obj_t b_in) { mp_int_t a = mp_obj_get_int(a_in); mp_int_t b = mp_obj_get_int(b_in); if (a <= b) { @@ -172,9 +172,9 @@ STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) { mp_raise_ValueError(NULL); } } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_randint_obj, mod_urandom_randint); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_random_randint_obj, mod_random_randint); -STATIC mp_obj_t mod_urandom_choice(mp_obj_t seq) { +STATIC mp_obj_t mod_random_choice(mp_obj_t seq) { mp_int_t len = mp_obj_get_int(mp_obj_len(seq)); if (len > 0) { return mp_obj_subscr(seq, mp_obj_new_int(yasmarang_randbelow(len)), MP_OBJ_SENTINEL); @@ -182,7 +182,7 @@ STATIC mp_obj_t mod_urandom_choice(mp_obj_t seq) { mp_raise_type(&mp_type_IndexError); } } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_choice_obj, mod_urandom_choice); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_random_choice_obj, mod_random_choice); #if MICROPY_PY_BUILTINS_FLOAT @@ -199,63 +199,63 @@ STATIC mp_float_t yasmarang_float(void) { return u.f - 1; } -STATIC mp_obj_t mod_urandom_random(void) { +STATIC mp_obj_t mod_random_random(void) { return mp_obj_new_float(yasmarang_float()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom_random_obj, mod_urandom_random); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_random_random_obj, mod_random_random); -STATIC mp_obj_t mod_urandom_uniform(mp_obj_t a_in, mp_obj_t b_in) { +STATIC mp_obj_t mod_random_uniform(mp_obj_t a_in, mp_obj_t b_in) { mp_float_t a = mp_obj_get_float(a_in); mp_float_t b = mp_obj_get_float(b_in); return mp_obj_new_float(a + (b - a) * yasmarang_float()); } -STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_uniform_obj, mod_urandom_uniform); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_random_uniform_obj, mod_random_uniform); #endif -#endif // MICROPY_PY_URANDOM_EXTRA_FUNCS +#endif // MICROPY_PY_RANDOM_EXTRA_FUNCS #if SEED_ON_IMPORT -STATIC mp_obj_t mod_urandom___init__(void) { +STATIC mp_obj_t mod_random___init__(void) { // This module may be imported by more than one name so need to ensure // that it's only ever seeded once. static bool seeded = false; if (!seeded) { seeded = true; - mod_urandom_seed(0, NULL); + mod_random_seed(0, NULL); } return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom___init___obj, mod_urandom___init__); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_random___init___obj, mod_random___init__); #endif #if !MICROPY_ENABLE_DYNRUNTIME -STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_urandom) }, +STATIC const mp_rom_map_elem_t mp_module_random_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_random) }, #if SEED_ON_IMPORT - { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&mod_urandom___init___obj) }, + { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&mod_random___init___obj) }, #endif - { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_urandom_getrandbits_obj) }, - { MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_urandom_seed_obj) }, - #if MICROPY_PY_URANDOM_EXTRA_FUNCS - { MP_ROM_QSTR(MP_QSTR_randrange), MP_ROM_PTR(&mod_urandom_randrange_obj) }, - { MP_ROM_QSTR(MP_QSTR_randint), MP_ROM_PTR(&mod_urandom_randint_obj) }, - { MP_ROM_QSTR(MP_QSTR_choice), MP_ROM_PTR(&mod_urandom_choice_obj) }, + { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_random_getrandbits_obj) }, + { MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_random_seed_obj) }, + #if MICROPY_PY_RANDOM_EXTRA_FUNCS + { MP_ROM_QSTR(MP_QSTR_randrange), MP_ROM_PTR(&mod_random_randrange_obj) }, + { MP_ROM_QSTR(MP_QSTR_randint), MP_ROM_PTR(&mod_random_randint_obj) }, + { MP_ROM_QSTR(MP_QSTR_choice), MP_ROM_PTR(&mod_random_choice_obj) }, #if MICROPY_PY_BUILTINS_FLOAT - { MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mod_urandom_random_obj) }, - { MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_urandom_uniform_obj) }, + { MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mod_random_random_obj) }, + { MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_random_uniform_obj) }, #endif #endif }; -STATIC MP_DEFINE_CONST_DICT(mp_module_urandom_globals, mp_module_urandom_globals_table); +STATIC MP_DEFINE_CONST_DICT(mp_module_random_globals, mp_module_random_globals_table); -const mp_obj_module_t mp_module_urandom = { +const mp_obj_module_t mp_module_random = { .base = { &mp_type_module }, - .globals = (mp_obj_dict_t *)&mp_module_urandom_globals, + .globals = (mp_obj_dict_t *)&mp_module_random_globals, }; -MP_REGISTER_MODULE(MP_QSTR_urandom, mp_module_urandom); +MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_random, mp_module_random); #endif -#endif // MICROPY_PY_URANDOM +#endif // MICROPY_PY_RANDOM diff --git a/extmod/modre.c b/extmod/modre.c index 3327cd75b1..467371b730 100644 --- a/extmod/modre.c +++ b/extmod/modre.c @@ -85,7 +85,7 @@ STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) { } MP_DEFINE_CONST_FUN_OBJ_2(match_group_obj, match_group); -#if MICROPY_PY_URE_MATCH_GROUPS +#if MICROPY_PY_RE_MATCH_GROUPS STATIC mp_obj_t match_groups(mp_obj_t self_in) { mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in); @@ -102,7 +102,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(match_groups_obj, match_groups); #endif -#if MICROPY_PY_URE_MATCH_SPAN_START_END +#if MICROPY_PY_RE_MATCH_SPAN_START_END STATIC void match_span_helper(size_t n_args, const mp_obj_t *args, mp_obj_t span[2]) { mp_obj_match_t *self = MP_OBJ_TO_PTR(args[0]); diff --git a/extmod/modselect.c b/extmod/modselect.c index 8beb21995a..63797e0b89 100644 --- a/extmod/modselect.c +++ b/extmod/modselect.c @@ -372,8 +372,9 @@ STATIC mp_uint_t poll_set_poll_until_ready_or_timeout(poll_set_t *poll_set, size if (mp_hal_is_interrupted()) { return 0; } - // end CIRCUITPY - MICROPY_EVENT_POLL_HOOK + #ifdef MICROPY_EVENT_POLL_HOOK + MICROPY_EVENT_POLL_HOOK; + #endif } #endif diff --git a/lib/uzlib/adler32.c b/lib/uzlib/adler32.c index 65fe1181b8..1f1759493b 100644 --- a/lib/uzlib/adler32.c +++ b/lib/uzlib/adler32.c @@ -36,7 +36,7 @@ * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler */ -#include "uzlib.h" +#include "tinf.h" #define A32_BASE 65521 #define A32_NMAX 5552 diff --git a/lib/uzlib/crc32.c b/lib/uzlib/crc32.c index 1e3b1756b3..e24c643b6a 100644 --- a/lib/uzlib/crc32.c +++ b/lib/uzlib/crc32.c @@ -36,7 +36,7 @@ * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler */ -#include "uzlib.h" +#include "tinf.h" static const unsigned int tinf_crc32tab[16] = { 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, diff --git a/lib/uzlib/defl_static.c b/lib/uzlib/defl_static.c deleted file mode 100644 index 208e9ea5d2..0000000000 --- a/lib/uzlib/defl_static.c +++ /dev/null @@ -1,181 +0,0 @@ -/* - -Routines in this file are based on: -Zlib (RFC1950 / RFC1951) compression for PuTTY. - -PuTTY is copyright 1997-2014 Simon Tatham. - -Portions copyright Robert de Bath, Joris van Rantwijk, Delian -Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry, -Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus -Kuhn, Colin Watson, and CORE SDI S.A. - -Optimised for MicroPython: -Copyright (c) 2023 by Jim Mussared - -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 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 - -/* ---------------------------------------------------------------------- - * Zlib compression. We always use the static Huffman tree option. - * Mostly this is because it's hard to scan a block in advance to - * work out better trees; dynamic trees are great when you're - * compressing a large file under no significant time constraint, - * but when you're compressing little bits in real time, things get - * hairier. - * - * I suppose it's possible that I could compute Huffman trees based - * on the frequencies in the _previous_ block, as a sort of - * heuristic, but I'm not confident that the gain would balance out - * having to transmit the trees. - */ - -static void outbits(uzlib_lz77_state_t *state, unsigned long bits, int nbits) -{ - assert(state->noutbits + nbits <= 32); - state->outbits |= bits << state->noutbits; - state->noutbits += nbits; - while (state->noutbits >= 8) { - state->dest_write_cb(state->dest_write_data, state->outbits & 0xFF); - state->outbits >>= 8; - state->noutbits -= 8; - } -} - -static const unsigned char mirrornibbles[16] = { - 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe, - 0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf, -}; - -static unsigned int mirrorbyte(unsigned int b) { - return mirrornibbles[b & 0xf] << 4 | mirrornibbles[b >> 4]; -} - -static int int_log2(int x) { - int r = 0; - while (x >>= 1) { - ++r; - } - return r; -} - -static void uzlib_literal(uzlib_lz77_state_t *state, unsigned char c) -{ - if (c <= 143) { - /* 0 through 143 are 8 bits long starting at 00110000. */ - outbits(state, mirrorbyte(0x30 + c), 8); - } else { - /* 144 through 255 are 9 bits long starting at 110010000. */ - outbits(state, 1 + 2 * mirrorbyte(0x90 - 144 + c), 9); - } -} - -static void uzlib_match(uzlib_lz77_state_t *state, int distance, int len) -{ - assert(distance >= 1 && distance <= 32768); - distance -= 1; - - while (len > 0) { - int thislen; - - /* - * We can transmit matches of lengths 3 through 258 - * inclusive. So if len exceeds 258, we must transmit in - * several steps, with 258 or less in each step. - * - * Specifically: if len >= 261, we can transmit 258 and be - * sure of having at least 3 left for the next step. And if - * len <= 258, we can just transmit len. But if len == 259 - * or 260, we must transmit len-3. - */ - thislen = (len > 260 ? 258 : len <= 258 ? len : len - 3); - len -= thislen; - - assert(thislen >= 3 && thislen <= 258); - - thislen -= 3; - int lcode = 28; - int x = int_log2(thislen); - int y; - if (thislen < 255) { - if (x) { - --x; - } - y = (thislen >> (x ? x - 1 : 0)) & 3; - lcode = x * 4 + y; - } - - /* - * Transmit the length code. 256-279 are seven bits - * starting at 0000000; 280-287 are eight bits starting at - * 11000000. - */ - if (lcode <= 22) { - outbits(state, mirrorbyte((lcode + 1) * 2), 7); - } else { - outbits(state, mirrorbyte(lcode + 169), 8); - } - - /* - * Transmit the extra bits. - */ - if (thislen < 255 && x > 1) { - int extrabits = x - 1; - int lmin = (thislen >> extrabits) << extrabits; - outbits(state, thislen - lmin, extrabits); - } - - x = int_log2(distance); - y = (distance >> (x ? x - 1 : 0)) & 1; - - /* - * Transmit the distance code. Five bits starting at 00000. - */ - outbits(state, mirrorbyte((x * 2 + y) * 8), 5); - - /* - * Transmit the extra bits. - */ - if (x > 1) { - int dextrabits = x - 1; - int dmin = (distance >> dextrabits) << dextrabits; - outbits(state, distance - dmin, dextrabits); - } - } -} - -void uzlib_start_block(uzlib_lz77_state_t *state) -{ - // Final block (0b1) - // Static huffman block (0b01) - outbits(state, 3, 3); -} - -void uzlib_finish_block(uzlib_lz77_state_t *state) -{ - // Close block (0b0000000) - // Make sure all bits are flushed (0b0000000) - outbits(state, 0, 14); -} diff --git a/lib/uzlib/defl_static.h b/lib/uzlib/defl_static.h new file mode 100644 index 0000000000..292734d773 --- /dev/null +++ b/lib/uzlib/defl_static.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) uzlib authors + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be + * held liable for any damages arising from the use of + * this software. + * + * Permission is granted to anyone to use this software + * for any purpose, including commercial applications, + * and to alter it and redistribute it freely, subject to + * the following restrictions: + * + * 1. The origin of this software must not be + * misrepresented; you must not claim that you + * wrote the original software. If you use this + * software in a product, an acknowledgment in + * the product documentation would be appreciated + * but is not required. + * + * 2. Altered source versions must be plainly marked + * as such, and must not be misrepresented as + * being the original software. + * + * 3. This notice may not be removed or altered from + * any source distribution. + */ + +/* This files contains type declaration and prototypes for defl_static.c. + They may be altered/distinct from the originals used in PuTTY source + code. */ + +struct Outbuf { + unsigned char *outbuf; + int outlen, outsize; + unsigned long outbits; + int noutbits; + int comp_disabled; +}; + +void outbits(struct Outbuf *out, unsigned long bits, int nbits); +void zlib_start_block(struct Outbuf *ctx); +void zlib_finish_block(struct Outbuf *ctx); +void zlib_literal(struct Outbuf *ectx, unsigned char c); +void zlib_match(struct Outbuf *ectx, int distance, int len); diff --git a/lib/uzlib/header.c b/lib/uzlib/header.c deleted file mode 100644 index 9c48d91391..0000000000 --- a/lib/uzlib/header.c +++ /dev/null @@ -1,137 +0,0 @@ -/* - * uzlib - tiny deflate/inflate library (deflate, gzip, zlib) - * - * Copyright (c) 2003 by Joergen Ibsen / Jibz - * All Rights Reserved - * - * http://www.ibsensoftware.com/ - * - * Copyright (c) 2014-2018 by Paul Sokolovsky - * - * Optimised for MicroPython: - * Copyright (c) 2023 by Jim Mussared - * - * This software is provided 'as-is', without any express - * or implied warranty. In no event will the authors be - * held liable for any damages arising from the use of - * this software. - * - * Permission is granted to anyone to use this software - * for any purpose, including commercial applications, - * and to alter it and redistribute it freely, subject to - * the following restrictions: - * - * 1. The origin of this software must not be - * misrepresented; you must not claim that you - * wrote the original software. If you use this - * software in a product, an acknowledgment in - * the product documentation would be appreciated - * but is not required. - * - * 2. Altered source versions must be plainly marked - * as such, and must not be misrepresented as - * being the original software. - * - * 3. This notice may not be removed or altered from - * any source distribution. - */ - -#include "uzlib.h" - -#define FTEXT 1 -#define FHCRC 2 -#define FEXTRA 4 -#define FNAME 8 -#define FCOMMENT 16 - -void tinf_skip_bytes(uzlib_uncomp_t *d, int num); -uint16_t tinf_get_uint16(uzlib_uncomp_t *d); - -void tinf_skip_bytes(uzlib_uncomp_t *d, int num) -{ - while (num--) uzlib_get_byte(d); -} - -uint16_t tinf_get_uint16(uzlib_uncomp_t *d) -{ - unsigned int v = uzlib_get_byte(d); - v = (uzlib_get_byte(d) << 8) | v; - return v; -} - -int uzlib_parse_zlib_gzip_header(uzlib_uncomp_t *d, int *wbits) -{ - /* -- check format -- */ - unsigned char cmf = uzlib_get_byte(d); - unsigned char flg = uzlib_get_byte(d); - - /* check for gzip id bytes */ - if (cmf == 0x1f && flg == 0x8b) { - /* check method is deflate */ - if (uzlib_get_byte(d) != 8) return UZLIB_DATA_ERROR; - - /* get flag byte */ - flg = uzlib_get_byte(d); - - /* check that reserved bits are zero */ - if (flg & 0xe0) return UZLIB_DATA_ERROR; - - /* -- find start of compressed data -- */ - - /* skip rest of base header of 10 bytes */ - tinf_skip_bytes(d, 6); - - /* skip extra data if present */ - if (flg & FEXTRA) - { - unsigned int xlen = tinf_get_uint16(d); - tinf_skip_bytes(d, xlen); - } - - /* skip file name if present */ - if (flg & FNAME) { while (uzlib_get_byte(d)); } - - /* skip file comment if present */ - if (flg & FCOMMENT) { while (uzlib_get_byte(d)); } - - /* check header crc if present */ - if (flg & FHCRC) - { - /*unsigned int hcrc =*/ tinf_get_uint16(d); - - // TODO: Check! - // if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff)) - // return UZLIB_DATA_ERROR; - } - - /* initialize for crc32 checksum */ - d->checksum_type = UZLIB_CHKSUM_CRC; - d->checksum = ~0; - - /* gzip does not include the window size in the header, as it is expected that a - compressor will use wbits=15 (32kiB).*/ - *wbits = 15; - - return UZLIB_HEADER_GZIP; - } else { - /* check checksum */ - if ((256*cmf + flg) % 31) return UZLIB_DATA_ERROR; - - /* check method is deflate */ - if ((cmf & 0x0f) != 8) return UZLIB_DATA_ERROR; - - /* check window size is valid */ - if ((cmf >> 4) > 7) return UZLIB_DATA_ERROR; - - /* check there is no preset dictionary */ - if (flg & 0x20) return UZLIB_DATA_ERROR; - - /* initialize for adler32 checksum */ - d->checksum_type = UZLIB_CHKSUM_ADLER; - d->checksum = 1; - - *wbits = (cmf >> 4) + 8; - - return UZLIB_HEADER_ZLIB; - } -} diff --git a/lib/uzlib/lz77.c b/lib/uzlib/lz77.c deleted file mode 100644 index 117ce50f70..0000000000 --- a/lib/uzlib/lz77.c +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Simple LZ77 streaming compressor. - * - * The scheme implemented here doesn't use a hash table and instead does a brute - * force search in the history for a previous string. It is relatively slow - * (but still O(N)) but gives good compression and minimal memory usage. For a - * small history window (eg 256 bytes) it's not too slow and compresses well. - * - * MIT license; Copyright (c) 2021 Damien P. George - */ - -#include "uzlib.h" - -#include "defl_static.c" - -#define MATCH_LEN_MIN (3) -#define MATCH_LEN_MAX (258) - -// hist should be a preallocated buffer of hist_max size bytes. -// hist_max should be greater than 0 a power of 2 (ie 1, 2, 4, 8, ...). -// It's possible to pass in hist=NULL, and then the history window will be taken from the -// src passed in to uzlib_lz77_compress (this is useful when not doing streaming compression). -void uzlib_lz77_init(uzlib_lz77_state_t *state, uint8_t *hist, size_t hist_max) { - memset(state, 0, sizeof(uzlib_lz77_state_t)); - state->hist_buf = hist; - state->hist_max = hist_max; - state->hist_start = 0; - state->hist_len = 0; -} - -// Push the given byte to the history. -// Search back in the history for the maximum match of the given src data, -// with support for searching beyond the end of the history and into the src buffer -// (effectively the history and src buffer are concatenated). -static size_t uzlib_lz77_search_max_match(uzlib_lz77_state_t *state, const uint8_t *src, size_t len, size_t *longest_offset) { - size_t longest_len = 0; - for (size_t hist_search = 0; hist_search < state->hist_len; ++hist_search) { - size_t match_len; - for (match_len = 0; match_len <= MATCH_LEN_MAX && match_len < len; ++match_len) { - uint8_t hist; - if (hist_search + match_len < state->hist_len) { - hist = state->hist_buf[(state->hist_start + hist_search + match_len) & (state->hist_max - 1)]; - } else { - hist = src[hist_search + match_len - state->hist_len]; - } - if (src[match_len] != hist) { - break; - } - } - if (match_len >= MATCH_LEN_MIN && match_len > longest_len) { - longest_len = match_len; - *longest_offset = state->hist_len - hist_search; - } - } - - return longest_len; -} - -// Compress the given chunk of data. -void uzlib_lz77_compress(uzlib_lz77_state_t *state, const uint8_t *src, unsigned len) { - const uint8_t *top = src + len; - while (src < top) { - // Look for a match in the history window. - size_t match_offset = 0; - size_t match_len = uzlib_lz77_search_max_match(state, src, top - src, &match_offset); - - // Encode the literal byte or the match. - if (match_len == 0) { - uzlib_literal(state, *src); - match_len = 1; - } else { - uzlib_match(state, match_offset, match_len); - } - - // Push the bytes into the history buffer. - size_t mask = state->hist_max - 1; - while (match_len--) { - uint8_t b = *src++; - state->hist_buf[(state->hist_start + state->hist_len) & mask] = b; - if (state->hist_len == state->hist_max) { - state->hist_start = (state->hist_start + 1) & mask; - } else { - ++state->hist_len; - } - } - } -} diff --git a/lib/uzlib/tinf.h b/lib/uzlib/tinf.h new file mode 100644 index 0000000000..ae6e1c4073 --- /dev/null +++ b/lib/uzlib/tinf.h @@ -0,0 +1,3 @@ +/* Compatibility header for the original tinf lib/older versions of uzlib. + Note: may be removed in the future, please migrate to uzlib.h. */ +#include "uzlib.h" diff --git a/lib/uzlib/tinf_compat.h b/lib/uzlib/tinf_compat.h new file mode 100644 index 0000000000..f763804bd9 --- /dev/null +++ b/lib/uzlib/tinf_compat.h @@ -0,0 +1,9 @@ +/* This header contains compatibility defines for the original tinf API + and uzlib 2.x and below API. These defines are deprecated and going + to be removed in the future, so applications should migrate to new + uzlib API. */ +#define TINF_DATA struct uzlib_uncomp + +#define destSize dest_size +#define destStart dest_start +#define readSource source_read_cb diff --git a/lib/uzlib/tinfgzip.c b/lib/uzlib/tinfgzip.c new file mode 100644 index 0000000000..22b000df9a --- /dev/null +++ b/lib/uzlib/tinfgzip.c @@ -0,0 +1,110 @@ +/* + * uzlib - tiny deflate/inflate library (deflate, gzip, zlib) + * + * Copyright (c) 2003 by Joergen Ibsen / Jibz + * All Rights Reserved + * + * http://www.ibsensoftware.com/ + * + * Copyright (c) 2014-2018 by Paul Sokolovsky + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be + * held liable for any damages arising from the use of + * this software. + * + * Permission is granted to anyone to use this software + * for any purpose, including commercial applications, + * and to alter it and redistribute it freely, subject to + * the following restrictions: + * + * 1. The origin of this software must not be + * misrepresented; you must not claim that you + * wrote the original software. If you use this + * software in a product, an acknowledgment in + * the product documentation would be appreciated + * but is not required. + * + * 2. Altered source versions must be plainly marked + * as such, and must not be misrepresented as + * being the original software. + * + * 3. This notice may not be removed or altered from + * any source distribution. + */ + +#include "tinf.h" + +#define FTEXT 1 +#define FHCRC 2 +#define FEXTRA 4 +#define FNAME 8 +#define FCOMMENT 16 + +void tinf_skip_bytes(TINF_DATA *d, int num); +uint16_t tinf_get_uint16(TINF_DATA *d); + +void tinf_skip_bytes(TINF_DATA *d, int num) +{ + while (num--) uzlib_get_byte(d); +} + +uint16_t tinf_get_uint16(TINF_DATA *d) +{ + unsigned int v = uzlib_get_byte(d); + v = (uzlib_get_byte(d) << 8) | v; + return v; +} + +int uzlib_gzip_parse_header(TINF_DATA *d) +{ + unsigned char flg; + + /* -- check format -- */ + + /* check id bytes */ + if (uzlib_get_byte(d) != 0x1f || uzlib_get_byte(d) != 0x8b) return TINF_DATA_ERROR; + + /* check method is deflate */ + if (uzlib_get_byte(d) != 8) return TINF_DATA_ERROR; + + /* get flag byte */ + flg = uzlib_get_byte(d); + + /* check that reserved bits are zero */ + if (flg & 0xe0) return TINF_DATA_ERROR; + + /* -- find start of compressed data -- */ + + /* skip rest of base header of 10 bytes */ + tinf_skip_bytes(d, 6); + + /* skip extra data if present */ + if (flg & FEXTRA) + { + unsigned int xlen = tinf_get_uint16(d); + tinf_skip_bytes(d, xlen); + } + + /* skip file name if present */ + if (flg & FNAME) { while (uzlib_get_byte(d)); } + + /* skip file comment if present */ + if (flg & FCOMMENT) { while (uzlib_get_byte(d)); } + + /* check header crc if present */ + if (flg & FHCRC) + { + /*unsigned int hcrc =*/ tinf_get_uint16(d); + + // TODO: Check! +// if (hcrc != (tinf_crc32(src, start - src) & 0x0000ffff)) +// return TINF_DATA_ERROR; + } + + /* initialize for crc32 checksum */ + d->checksum_type = TINF_CHKSUM_CRC; + d->checksum = ~0; + + return TINF_OK; +} diff --git a/lib/uzlib/tinflate.c b/lib/uzlib/tinflate.c index 53536272f3..045952c755 100644 --- a/lib/uzlib/tinflate.c +++ b/lib/uzlib/tinflate.c @@ -7,9 +7,6 @@ * * Copyright (c) 2014-2018 by Paul Sokolovsky * - * Optimised for MicroPython: - * Copyright (c) 2023 by Jim Mussared - * * This software is provided 'as-is', without any express * or implied warranty. In no event will the authors be * held liable for any damages arising from the use of @@ -36,7 +33,7 @@ */ #include -#include "uzlib.h" +#include "tinf.h" #define UZLIB_DUMP_ARRAY(heading, arr, size) \ { \ @@ -47,52 +44,52 @@ printf("\n"); \ } -uint32_t tinf_get_le_uint32(uzlib_uncomp_t *d); -uint32_t tinf_get_be_uint32(uzlib_uncomp_t *d); +uint32_t tinf_get_le_uint32(TINF_DATA *d); +uint32_t tinf_get_be_uint32(TINF_DATA *d); /* --------------------------------------------------- * * -- uninitialized global data (static structures) -- * * --------------------------------------------------- */ -typedef struct { - unsigned char length_bits : 3; - unsigned short length_base : 9; - unsigned char dist_bits : 4; - unsigned short dist_base : 15; -} lookup_table_entry_t; +#ifdef RUNTIME_BITS_TABLES -const lookup_table_entry_t lookup_table[30] = { - {0, 3, 0, 1}, - {0, 4, 0, 2}, - {0, 5, 0, 3}, - {0, 6, 0, 4}, - {0, 7, 1, 5}, - {0, 8, 1, 7}, - {0, 9, 2, 9}, - {0, 10, 2, 13}, - {1, 11, 3, 17}, - {1, 13, 3, 25}, - {1, 15, 4, 33}, - {1, 17, 4, 49}, - {2, 19, 5, 65}, - {2, 23, 5, 97}, - {2, 27, 6, 129}, - {2, 31, 6, 193}, - {3, 35, 7, 257}, - {3, 43, 7, 385}, - {3, 51, 8, 513}, - {3, 59, 8, 769}, - {4, 67, 9, 1025}, - {4, 83, 9, 1537}, - {4, 99, 10, 2049}, - {4, 115, 10, 3073}, - {5, 131, 11, 4097}, - {5, 163, 11, 6145}, - {5, 195, 12, 8193}, - {5, 227, 12, 12289}, - {0, 258, 13, 16385}, - {0, 0, 13, 24577}, +/* extra bits and base tables for length codes */ +unsigned char length_bits[30]; +unsigned short length_base[30]; + +/* extra bits and base tables for distance codes */ +unsigned char dist_bits[30]; +unsigned short dist_base[30]; + +#else + +const unsigned char length_bits[30] = { + 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 1, 2, 2, 2, 2, + 3, 3, 3, 3, 4, 4, 4, 4, + 5, 5, 5, 5 }; +const unsigned short length_base[30] = { + 3, 4, 5, 6, 7, 8, 9, 10, + 11, 13, 15, 17, 19, 23, 27, 31, + 35, 43, 51, 59, 67, 83, 99, 115, + 131, 163, 195, 227, 258 +}; + +const unsigned char dist_bits[30] = { + 0, 0, 0, 0, 1, 1, 2, 2, + 3, 3, 4, 4, 5, 5, 6, 6, + 7, 7, 8, 8, 9, 9, 10, 10, + 11, 11, 12, 12, 13, 13 +}; +const unsigned short dist_base[30] = { + 1, 2, 3, 4, 5, 7, 9, 13, + 17, 25, 33, 49, 65, 97, 129, 193, + 257, 385, 513, 769, 1025, 1537, 2049, 3073, + 4097, 6145, 8193, 12289, 16385, 24577 +}; + +#endif /* special ordering of code length codes */ const unsigned char clcidx[] = { @@ -105,6 +102,25 @@ const unsigned char clcidx[] = { * -- utility functions -- * * ----------------------- */ +#ifdef RUNTIME_BITS_TABLES +/* build extra bits and base tables */ +static void tinf_build_bits_base(unsigned char *bits, unsigned short *base, int delta, int first) +{ + int i, sum; + + /* build bits table */ + for (i = 0; i < delta; ++i) bits[i] = 0; + for (i = 0; i < 30 - delta; ++i) bits[i + delta] = i / delta; + + /* build base table */ + for (sum = first, i = 0; i < 30; ++i) + { + base[i] = sum; + sum += 1 << bits[i]; + } +} +#endif + /* build the fixed huffman trees */ static void tinf_build_fixed_trees(TINF_TREE *lt, TINF_TREE *dt) { @@ -173,7 +189,7 @@ static void tinf_build_tree(TINF_TREE *t, const unsigned char *lengths, unsigned * -- decode functions -- * * ---------------------- */ -unsigned char uzlib_get_byte(uzlib_uncomp_t *d) +unsigned char uzlib_get_byte(TINF_DATA *d) { /* If end of source buffer is not reached, return next byte from source buffer. */ @@ -184,14 +200,14 @@ unsigned char uzlib_get_byte(uzlib_uncomp_t *d) /* Otherwise if there's callback and we haven't seen EOF yet, try to read next byte using it. (Note: the callback can also update ->source and ->source_limit). */ - if (d->source_read_cb && !d->eof) { - int val = d->source_read_cb(d->source_read_data); + if (d->readSource && !d->eof) { + int val = d->readSource(d); if (val >= 0) { return (unsigned char)val; } } - /* Otherwise, we hit EOF (either from ->source_read_cb() or from exhaustion + /* Otherwise, we hit EOF (either from ->readSource() or from exhaustion of the buffer), and it will be "sticky", i.e. further calls to this function will end up here too. */ d->eof = true; @@ -199,7 +215,7 @@ unsigned char uzlib_get_byte(uzlib_uncomp_t *d) return 0; } -uint32_t tinf_get_le_uint32(uzlib_uncomp_t *d) +uint32_t tinf_get_le_uint32(TINF_DATA *d) { uint32_t val = 0; int i; @@ -209,7 +225,7 @@ uint32_t tinf_get_le_uint32(uzlib_uncomp_t *d) return val; } -uint32_t tinf_get_be_uint32(uzlib_uncomp_t *d) +uint32_t tinf_get_be_uint32(TINF_DATA *d) { uint32_t val = 0; int i; @@ -220,7 +236,7 @@ uint32_t tinf_get_be_uint32(uzlib_uncomp_t *d) } /* get one bit from source stream */ -static int tinf_getbit(uzlib_uncomp_t *d) +static int tinf_getbit(TINF_DATA *d) { unsigned int bit; @@ -240,7 +256,7 @@ static int tinf_getbit(uzlib_uncomp_t *d) } /* read a num bit value from a stream and add base */ -static unsigned int tinf_read_bits(uzlib_uncomp_t *d, int num, int base) +static unsigned int tinf_read_bits(TINF_DATA *d, int num, int base) { unsigned int val = 0; @@ -258,7 +274,7 @@ static unsigned int tinf_read_bits(uzlib_uncomp_t *d, int num, int base) } /* given a data stream and a tree, decode a symbol */ -static int tinf_decode_symbol(uzlib_uncomp_t *d, TINF_TREE *t) +static int tinf_decode_symbol(TINF_DATA *d, TINF_TREE *t) { int sum = 0, cur = 0, len = 0; @@ -268,7 +284,7 @@ static int tinf_decode_symbol(uzlib_uncomp_t *d, TINF_TREE *t) cur = 2*cur + tinf_getbit(d); if (++len == TINF_ARRAY_SIZE(t->table)) { - return UZLIB_DATA_ERROR; + return TINF_DATA_ERROR; } sum += t->table[len]; @@ -279,7 +295,7 @@ static int tinf_decode_symbol(uzlib_uncomp_t *d, TINF_TREE *t) sum += cur; #if UZLIB_CONF_PARANOID_CHECKS if (sum < 0 || sum >= TINF_ARRAY_SIZE(t->trans)) { - return UZLIB_DATA_ERROR; + return TINF_DATA_ERROR; } #endif @@ -287,7 +303,7 @@ static int tinf_decode_symbol(uzlib_uncomp_t *d, TINF_TREE *t) } /* given a data stream, decode dynamic trees from it */ -static int tinf_decode_trees(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE *dt) +static int tinf_decode_trees(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt) { /* code lengths for 288 literal/len symbols and 32 dist symbols */ unsigned char lengths[288+32]; @@ -332,7 +348,7 @@ static int tinf_decode_trees(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE *dt) { case 16: /* copy previous code length 3-6 times (read 2 bits) */ - if (num == 0) return UZLIB_DATA_ERROR; + if (num == 0) return TINF_DATA_ERROR; fill_value = lengths[num - 1]; lbits = 2; break; @@ -354,7 +370,7 @@ static int tinf_decode_trees(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE *dt) /* special code length 16-18 are handled here */ length = tinf_read_bits(d, lbits, lbase); - if (num + length > hlimit) return UZLIB_DATA_ERROR; + if (num + length > hlimit) return TINF_DATA_ERROR; for (; length; --length) { lengths[num++] = fill_value; @@ -371,7 +387,7 @@ static int tinf_decode_trees(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE *dt) #if UZLIB_CONF_PARANOID_CHECKS /* Check that there's "end of block" symbol */ if (lengths[256] == 0) { - return UZLIB_DATA_ERROR; + return TINF_DATA_ERROR; } #endif @@ -379,7 +395,7 @@ static int tinf_decode_trees(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE *dt) tinf_build_tree(lt, lengths, hlit); tinf_build_tree(dt, lengths + hlit, hdist); - return UZLIB_OK; + return TINF_OK; } /* ----------------------------- * @@ -387,7 +403,7 @@ static int tinf_decode_trees(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE *dt) * ----------------------------- */ /* given a stream and two trees, inflate next byte of output */ -static int tinf_inflate_block_data(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE *dt) +static int tinf_inflate_block_data(TINF_DATA *d, TINF_TREE *lt, TINF_TREE *dt) { if (d->curlen == 0) { unsigned int offs; @@ -396,41 +412,41 @@ static int tinf_inflate_block_data(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE * //printf("huff sym: %02x\n", sym); if (d->eof) { - return UZLIB_DATA_ERROR; + return TINF_DATA_ERROR; } /* literal byte */ if (sym < 256) { TINF_PUT(d, sym); - return UZLIB_OK; + return TINF_OK; } /* end of block */ if (sym == 256) { - return UZLIB_DONE; + return TINF_DONE; } /* substring from sliding dictionary */ sym -= 257; if (sym >= 29) { - return UZLIB_DATA_ERROR; + return TINF_DATA_ERROR; } /* possibly get more bits from length code */ - d->curlen = tinf_read_bits(d, lookup_table[sym].length_bits, lookup_table[sym].length_base); + d->curlen = tinf_read_bits(d, length_bits[sym], length_base[sym]); dist = tinf_decode_symbol(d, dt); if (dist >= 30) { - return UZLIB_DATA_ERROR; + return TINF_DATA_ERROR; } /* possibly get more bits from distance code */ - offs = tinf_read_bits(d, lookup_table[dist].dist_bits, lookup_table[dist].dist_base); + offs = tinf_read_bits(d, dist_bits[dist], dist_base[dist]); /* calculate and validate actual LZ offset to use */ if (d->dict_ring) { if (offs > d->dict_size) { - return UZLIB_DICT_ERROR; + return TINF_DICT_ERROR; } /* Note: unlike full-dest-in-memory case below, we don't try to catch offset which points to not yet filled @@ -448,8 +464,8 @@ static int tinf_inflate_block_data(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE * } } else { /* catch trying to point before the start of dest buffer */ - if (offs > (unsigned int)(d->dest - d->dest_start)) { - return UZLIB_DATA_ERROR; + if (offs > (unsigned int)(d->dest - d->destStart)) { + return TINF_DATA_ERROR; } d->lzOff = -offs; } @@ -466,11 +482,11 @@ static int tinf_inflate_block_data(uzlib_uncomp_t *d, TINF_TREE *lt, TINF_TREE * d->dest++; } d->curlen--; - return UZLIB_OK; + return TINF_OK; } /* inflate next byte from uncompressed block of data */ -static int tinf_inflate_uncompressed_block(uzlib_uncomp_t *d) +static int tinf_inflate_uncompressed_block(TINF_DATA *d) { if (d->curlen == 0) { unsigned int length, invlength; @@ -482,9 +498,9 @@ static int tinf_inflate_uncompressed_block(uzlib_uncomp_t *d) invlength = uzlib_get_byte(d); invlength += 256 * uzlib_get_byte(d); /* check length */ - if (length != (~invlength & 0x0000ffff)) return UZLIB_DATA_ERROR; + if (length != (~invlength & 0x0000ffff)) return TINF_DATA_ERROR; - /* increment length to properly return UZLIB_DONE below, without + /* increment length to properly return TINF_DONE below, without producing data at the same time */ d->curlen = length + 1; @@ -493,20 +509,34 @@ static int tinf_inflate_uncompressed_block(uzlib_uncomp_t *d) } if (--d->curlen == 0) { - return UZLIB_DONE; + return TINF_DONE; } unsigned char c = uzlib_get_byte(d); TINF_PUT(d, c); - return UZLIB_OK; + return TINF_OK; } /* ---------------------- * * -- public functions -- * * ---------------------- */ +/* initialize global (static) data */ +void uzlib_init(void) +{ +#ifdef RUNTIME_BITS_TABLES + /* build extra bits and base tables */ + tinf_build_bits_base(length_bits, length_base, 4, 3); + tinf_build_bits_base(dist_bits, dist_base, 2, 1); + + /* fix a special case */ + length_bits[28] = 0; + length_base[28] = 258; +#endif +} + /* initialize decompression structure */ -void uzlib_uncompress_init(uzlib_uncomp_t *d, void *dict, unsigned int dictLen) +void uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen) { d->eof = 0; d->bitcount = 0; @@ -519,7 +549,7 @@ void uzlib_uncompress_init(uzlib_uncomp_t *d, void *dict, unsigned int dictLen) } /* inflate next output bytes from compressed stream */ -int uzlib_uncompress(uzlib_uncomp_t *d) +int uzlib_uncompress(TINF_DATA *d) { do { int res; @@ -542,7 +572,7 @@ next_blk: } else if (d->btype == 2) { /* decode trees from stream */ res = tinf_decode_trees(d, &d->ltree, &d->dtree); - if (res != UZLIB_OK) { + if (res != TINF_OK) { return res; } } @@ -562,27 +592,27 @@ next_blk: res = tinf_inflate_block_data(d, &d->ltree, &d->dtree); break; default: - return UZLIB_DATA_ERROR; + return TINF_DATA_ERROR; } - if (res == UZLIB_DONE && !d->bfinal) { + if (res == TINF_DONE && !d->bfinal) { /* the block has ended (without producing more data), but we can't return without data, so start procesing next block */ goto next_blk; } - if (res != UZLIB_OK) { + if (res != TINF_OK) { return res; } } while (d->dest < d->dest_limit); - return UZLIB_OK; + return TINF_OK; } /* inflate next output bytes from compressed stream, updating checksum, and at the end of stream, verify it */ -int uzlib_uncompress_chksum(uzlib_uncomp_t *d) +int uzlib_uncompress_chksum(TINF_DATA *d) { int res; unsigned char *data = d->dest; @@ -593,31 +623,31 @@ int uzlib_uncompress_chksum(uzlib_uncomp_t *d) switch (d->checksum_type) { - case UZLIB_CHKSUM_ADLER: + case TINF_CHKSUM_ADLER: d->checksum = uzlib_adler32(data, d->dest - data, d->checksum); break; - case UZLIB_CHKSUM_CRC: + case TINF_CHKSUM_CRC: d->checksum = uzlib_crc32(data, d->dest - data, d->checksum); break; } - if (res == UZLIB_DONE) { + if (res == TINF_DONE) { unsigned int val; switch (d->checksum_type) { - case UZLIB_CHKSUM_ADLER: + case TINF_CHKSUM_ADLER: val = tinf_get_be_uint32(d); if (d->checksum != val) { - return UZLIB_CHKSUM_ERROR; + return TINF_CHKSUM_ERROR; } break; - case UZLIB_CHKSUM_CRC: + case TINF_CHKSUM_CRC: val = tinf_get_le_uint32(d); if (~d->checksum != val) { - return UZLIB_CHKSUM_ERROR; + return TINF_CHKSUM_ERROR; } // Uncompressed size. TODO: Check val = tinf_get_le_uint32(d); diff --git a/lib/uzlib/tinfzlib.c b/lib/uzlib/tinfzlib.c new file mode 100644 index 0000000000..5cb8852fcc --- /dev/null +++ b/lib/uzlib/tinfzlib.c @@ -0,0 +1,66 @@ +/* + * uzlib - tiny deflate/inflate library (deflate, gzip, zlib) + * + * Copyright (c) 2003 by Joergen Ibsen / Jibz + * All Rights Reserved + * + * http://www.ibsensoftware.com/ + * + * Copyright (c) 2014-2018 by Paul Sokolovsky + * + * This software is provided 'as-is', without any express + * or implied warranty. In no event will the authors be + * held liable for any damages arising from the use of + * this software. + * + * Permission is granted to anyone to use this software + * for any purpose, including commercial applications, + * and to alter it and redistribute it freely, subject to + * the following restrictions: + * + * 1. The origin of this software must not be + * misrepresented; you must not claim that you + * wrote the original software. If you use this + * software in a product, an acknowledgment in + * the product documentation would be appreciated + * but is not required. + * + * 2. Altered source versions must be plainly marked + * as such, and must not be misrepresented as + * being the original software. + * + * 3. This notice may not be removed or altered from + * any source distribution. + */ + +#include "tinf.h" + +int uzlib_zlib_parse_header(TINF_DATA *d) +{ + unsigned char cmf, flg; + + /* -- get header bytes -- */ + + cmf = uzlib_get_byte(d); + flg = uzlib_get_byte(d); + + /* -- check format -- */ + + /* check checksum */ + if ((256*cmf + flg) % 31) return TINF_DATA_ERROR; + + /* check method is deflate */ + if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR; + + /* check window size is valid */ + if ((cmf >> 4) > 7) return TINF_DATA_ERROR; + + /* check there is no preset dictionary */ + if (flg & 0x20) return TINF_DATA_ERROR; + + /* initialize for adler32 checksum */ + d->checksum_type = TINF_CHKSUM_ADLER; + d->checksum = 1; + + return cmf >> 4; +} diff --git a/lib/uzlib/uzlib.h b/lib/uzlib/uzlib.h index 16984a77d3..7d8949543e 100644 --- a/lib/uzlib/uzlib.h +++ b/lib/uzlib/uzlib.h @@ -7,9 +7,6 @@ * * Copyright (c) 2014-2018 by Paul Sokolovsky * - * Optimised for MicroPython: - * Copyright (c) 2023 by Jim Mussared - * * This software is provided 'as-is', without any express * or implied warranty. In no event will the authors be * held liable for any damages arising from the use of @@ -42,27 +39,38 @@ #include #include +#include "defl_static.h" + #include "uzlib_conf.h" #if UZLIB_CONF_DEBUG_LOG #include #endif +/* calling convention */ +#ifndef TINFCC + #ifdef __WATCOMC__ + #define TINFCC __cdecl + #else + #define TINFCC + #endif +#endif + #ifdef __cplusplus extern "C" { #endif /* ok status, more data produced */ -#define UZLIB_OK 0 +#define TINF_OK 0 /* end of compressed stream reached */ -#define UZLIB_DONE 1 -#define UZLIB_DATA_ERROR (-3) -#define UZLIB_CHKSUM_ERROR (-4) -#define UZLIB_DICT_ERROR (-5) +#define TINF_DONE 1 +#define TINF_DATA_ERROR (-3) +#define TINF_CHKSUM_ERROR (-4) +#define TINF_DICT_ERROR (-5) /* checksum types */ -#define UZLIB_CHKSUM_NONE 0 -#define UZLIB_CHKSUM_ADLER 1 -#define UZLIB_CHKSUM_CRC 2 +#define TINF_CHKSUM_NONE 0 +#define TINF_CHKSUM_ADLER 1 +#define TINF_CHKSUM_CRC 2 /* helper macros */ #define TINF_ARRAY_SIZE(arr) (sizeof(arr) / sizeof(*(arr))) @@ -74,7 +82,9 @@ typedef struct { unsigned short trans[288]; /* code -> symbol translation table */ } TINF_TREE; -typedef struct _uzlib_uncomp_t { +struct uzlib_uncomp { + /* Point to the CircuitPython object that owns this decompression stream */ + void *self; /* Pointer to the next byte in the input buffer */ const unsigned char *source; /* Pointer to the next byte past the input buffer (source_limit = source + len) */ @@ -84,8 +94,7 @@ typedef struct _uzlib_uncomp_t { also return -1 in case of EOF (or irrecoverable error). Note that besides returning the next byte, it may also update source and source_limit fields, thus allowing for buffered operation. */ - void *source_read_data; - int (*source_read_cb)(void *); + int (*source_read_cb)(struct uzlib_uncomp *uncomp); unsigned int tag; unsigned int bitcount; @@ -112,7 +121,9 @@ typedef struct _uzlib_uncomp_t { TINF_TREE ltree; /* dynamic length/symbol tree */ TINF_TREE dtree; /* dynamic distance tree */ -} uzlib_uncomp_t; +}; + +#include "tinf_compat.h" #define TINF_PUT(d, c) \ { \ @@ -120,43 +131,38 @@ typedef struct _uzlib_uncomp_t { if (d->dict_ring) { d->dict_ring[d->dict_idx++] = c; if (d->dict_idx == d->dict_size) d->dict_idx = 0; } \ } -unsigned char uzlib_get_byte(uzlib_uncomp_t *d); +unsigned char TINFCC uzlib_get_byte(TINF_DATA *d); /* Decompression API */ -void uzlib_uncompress_init(uzlib_uncomp_t *d, void *dict, unsigned int dictLen); -int uzlib_uncompress(uzlib_uncomp_t *d); -int uzlib_uncompress_chksum(uzlib_uncomp_t *d); +void TINFCC uzlib_init(void); +void TINFCC uzlib_uncompress_init(TINF_DATA *d, void *dict, unsigned int dictLen); +int TINFCC uzlib_uncompress(TINF_DATA *d); +int TINFCC uzlib_uncompress_chksum(TINF_DATA *d); -#define UZLIB_HEADER_ZLIB 0 -#define UZLIB_HEADER_GZIP 1 -int uzlib_parse_zlib_gzip_header(uzlib_uncomp_t *d, int *wbits); +int TINFCC uzlib_zlib_parse_header(TINF_DATA *d); +int TINFCC uzlib_gzip_parse_header(TINF_DATA *d); /* Compression API */ -typedef struct { - void *dest_write_data; - void (*dest_write_cb)(void *data, uint8_t byte); - unsigned long outbits; - int noutbits; - uint8_t *hist_buf; - size_t hist_max; - size_t hist_start; - size_t hist_len; -} uzlib_lz77_state_t; +typedef const uint8_t *uzlib_hash_entry_t; -void uzlib_lz77_init(uzlib_lz77_state_t *state, uint8_t *hist, size_t hist_max); -void uzlib_lz77_compress(uzlib_lz77_state_t *state, const uint8_t *src, unsigned len); +struct uzlib_comp { + struct Outbuf out; -void uzlib_start_block(uzlib_lz77_state_t *state); -void uzlib_finish_block(uzlib_lz77_state_t *state); + uzlib_hash_entry_t *hash_table; + unsigned int hash_bits; + unsigned int dict_size; +}; + +void TINFCC uzlib_compress(struct uzlib_comp *c, const uint8_t *src, unsigned slen); /* Checksum API */ /* prev_sum is previous value for incremental computation, 1 initially */ -uint32_t uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum); +uint32_t TINFCC uzlib_adler32(const void *data, unsigned int length, uint32_t prev_sum); /* crc is previous value for incremental computation, 0xffffffff initially */ -uint32_t uzlib_crc32(const void *data, unsigned int length, uint32_t crc); +uint32_t TINFCC uzlib_crc32(const void *data, unsigned int length, uint32_t crc); #ifdef __cplusplus } /* extern "C" */ diff --git a/mpy-cross/mpconfigport.h b/mpy-cross/mpconfigport.h index d835e81b41..e077247011 100644 --- a/mpy-cross/mpconfigport.h +++ b/mpy-cross/mpconfigport.h @@ -95,7 +95,7 @@ // MINGW only handles these errno names. #ifdef __MINGW32__ -#define MICROPY_PY_UERRNO_LIST \ +#define MICROPY_PY_ERRNO_LIST \ X(EPERM) \ X(ENOENT) \ X(ESRCH) \ diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 71219ebcef..a63919978d 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -48,7 +48,7 @@ #define MICROPY_PY_REVERSE_SPECIAL_METHODS (0) #define MICROPY_PY_COLLECTIONS_DEQUE (0) #define MICROPY_PY_COLLECTIONS_ORDEREDDICT (0) -#define MICROPY_PY_UERRNO_LIST \ +#define MICROPY_PY_ERRNO_LIST \ X(EPERM) \ X(ENOENT) \ X(EIO) \ @@ -85,7 +85,7 @@ #define SPI_FLASH_MAX_BAUDRATE 24000000 #define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1) #define MICROPY_PY_FUNCTION_ATTRS (1) -// MICROPY_PY_UERRNO_LIST - Use the default +// MICROPY_PY_ERRNO_LIST - Use the default #endif // SAM_D5X_E5X diff --git a/ports/unix/modtime.c b/ports/unix/modtime.c index c9c98243e4..8eb5b4116a 100644 --- a/ports/unix/modtime.c +++ b/ports/unix/modtime.c @@ -203,18 +203,18 @@ STATIC mp_obj_t mod_time_mktime(mp_obj_t tuple) { MP_DEFINE_CONST_FUN_OBJ_1(mod_time_mktime_obj, mod_time_mktime); STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = { - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, { MP_ROM_QSTR(MP_QSTR_clock), MP_ROM_PTR(&mod_time_clock_obj) }, { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mod_time_sleep_obj) }, - { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_time_sleep_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_time_sleep_us_obj) }, { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mod_time_time_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, - { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, - { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_time_ticks_ms_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_time_ticks_us_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_time_ticks_cpu_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_time_ticks_add_obj) }, + { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_time_ticks_diff_obj) }, + { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_time_time_ns_obj) }, { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mod_time_gmtime_obj) }, { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mod_time_mktime_obj) }, @@ -229,4 +229,4 @@ const mp_obj_module_t mp_module_time = { MP_REGISTER_MODULE(MP_QSTR_time, mp_module_time); -#endif // MICROPY_PY_UTIME +#endif // MICROPY_PY_TIME diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index e066a1cabe..c9f85dd4ba 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -125,30 +125,30 @@ extern void common_hal_mcu_enable_interrupts(void); #define MICROPY_PY_BUILTINS_SLICE_INDICES (1) #define MICROPY_PY_BUILTINS_STR_UNICODE (1) +#define MICROPY_PY_BINASCII (CIRCUITPY_BINASCII) #define MICROPY_PY_CMATH (0) #define MICROPY_PY_COLLECTIONS (CIRCUITPY_COLLECTIONS) #define MICROPY_PY_DESCRIPTORS (1) +// In extmod +#define MICROPY_PY_ERRNO (CIRCUITPY_ERRNO) +// Uses about 80 bytes. +#define MICROPY_PY_ERRNO_ERRORCODE (CIRCUITPY_ERRNO) #define MICROPY_PY_GC (1) // Supplanted by shared-bindings/math #define MICROPY_PY_IO (CIRCUITPY_IO) +// In extmod +#define MICROPY_PY_JSON (CIRCUITPY_JSON) #define MICROPY_PY_MATH (0) #define MICROPY_PY_MICROPYTHON_MEM_INFO (0) +// Supplanted by shared-bindings/random +#define MICROPY_PY_RANDOM (0) +#define MICROPY_PY_RANDOM_EXTRA_FUNCS (0) +#define MICROPY_PY_RE (CIRCUITPY_RE) // Supplanted by shared-bindings/struct #define MICROPY_PY_STRUCT (0) #define MICROPY_PY_SYS (CIRCUITPY_SYS) #define MICROPY_PY_SYS_MAXSIZE (1) #define MICROPY_PY_SYS_STDFILES (1) -// In extmod -#define MICROPY_PY_UBINASCII (CIRCUITPY_BINASCII) -#define MICROPY_PY_UERRNO (CIRCUITPY_ERRNO) -// Uses about 80 bytes. -#define MICROPY_PY_UERRNO_ERRORCODE (CIRCUITPY_ERRNO) -// Supplanted by shared-bindings/random -#define MICROPY_PY_URANDOM (0) -#define MICROPY_PY_URANDOM_EXTRA_FUNCS (0) -// In extmod -#define MICROPY_PY_UJSON (CIRCUITPY_JSON) -#define MICROPY_PY_URE (CIRCUITPY_RE) #define MICROPY_PY___FILE__ (1) #define MICROPY_QSTR_BYTES_IN_HASH (1) @@ -253,9 +253,9 @@ typedef long mp_off_t; #ifndef MICROPY_PY_COLLECTIONS_DEQUE #define MICROPY_PY_COLLECTIONS_DEQUE (CIRCUITPY_FULL_BUILD) #endif -#define MICROPY_PY_URE_MATCH_GROUPS (CIRCUITPY_RE) -#define MICROPY_PY_URE_MATCH_SPAN_START_END (CIRCUITPY_RE) -#define MICROPY_PY_URE_SUB (CIRCUITPY_RE) +#define MICROPY_PY_RE_MATCH_GROUPS (CIRCUITPY_RE) +#define MICROPY_PY_RE_MATCH_SPAN_START_END (CIRCUITPY_RE) +#define MICROPY_PY_RE_SUB (CIRCUITPY_RE) #define CIRCUITPY_MICROPYTHON_ADVANCED (0) diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index e15383cc5f..88dd66526e 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -64,18 +64,18 @@ CFLAGS += -DMICROPY_PY_ASYNC_AWAIT=$(MICROPY_PY_ASYNC_AWAIT) # unused by CIRCUITPYTHON MICROPY_ROM_TEXT_COMPRESSION = 0 -# uasyncio +# asyncio # By default, include uasyncio if async/await are available. -MICROPY_PY_UASYNCIO ?= $(MICROPY_PY_ASYNC_AWAIT) -CFLAGS += -DMICROPY_PY_UASYNCIO=$(MICROPY_PY_UASYNCIO) +MICROPY_PY_ASYNCIO ?= $(MICROPY_PY_ASYNC_AWAIT) +CFLAGS += -DMICROPY_PY_ASYNCIO=$(MICROPY_PY_ASYNCIO) -# uasyncio normally needs select -MICROPY_PY_USELECT ?= $(MICROPY_PY_UASYNCIO) -CFLAGS += -DMICROPY_PY_USELECT=$(MICROPY_PY_USELECT) +# asyncio normally needs select +MICROPY_PY_SELECT ?= $(MICROPY_PY_ASYNCIO) +CFLAGS += -DMICROPY_PY_SELECT=$(MICROPY_PY_SELECT) # enable select.select if select is enabled. -MICROPY_PY_USELECT_SELECT ?= $(MICROPY_PY_USELECT) -CFLAGS += -DMICROPY_PY_USELECT_SELECT=$(MICROPY_PY_USELECT_SELECT) +MICROPY_PY_SELECT_SELECT ?= $(MICROPY_PY_SELECT) +CFLAGS += -DMICROPY_PY_SELECT_SELECT=$(MICROPY_PY_SELECT_SELECT) CIRCUITPY_AESIO ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO) diff --git a/py/moderrno.c b/py/moderrno.c index 9df30d68c0..4e07d9e0f8 100644 --- a/py/moderrno.c +++ b/py/moderrno.c @@ -64,7 +64,7 @@ #if MICROPY_PY_ERRNO_ERRORCODE STATIC const mp_rom_map_elem_t errorcode_table[] = { #define X(e) { MP_ROM_INT(MP_##e), MP_ROM_QSTR(MP_QSTR_##e) }, - MICROPY_PY_UERRNO_LIST + MICROPY_PY_ERRNO_LIST #undef X }; @@ -121,4 +121,53 @@ qstr mp_errno_to_str(mp_obj_t errno_val) { #endif } -#endif // MICROPY_PY_ERRNO +#endif // MICROPY_PY_UERRNO + + +// For commonly encountered errors, return human readable strings, otherwise try errno name +const char *mp_common_errno_to_str(mp_obj_t errno_val, char *buf, size_t len) { + if (!mp_obj_is_small_int(errno_val)) { + return NULL; + } + + const compressed_string_t *desc = NULL; + switch (MP_OBJ_SMALL_INT_VALUE(errno_val)) { + case EPERM: + desc = MP_ERROR_TEXT("Operation not permitted"); + break; + case ENOENT: + desc = MP_ERROR_TEXT("No such file/directory"); + break; + case EIO: + desc = MP_ERROR_TEXT("Input/output error"); + break; + case EACCES: + desc = MP_ERROR_TEXT("Permission denied"); + break; + case EEXIST: + desc = MP_ERROR_TEXT("File exists"); + break; + case ENODEV: + desc = MP_ERROR_TEXT("No such device"); + break; + case EINVAL: + desc = MP_ERROR_TEXT("Invalid argument"); + break; + case ENOSPC: + desc = MP_ERROR_TEXT("No space left on device"); + break; + case EROFS: + desc = MP_ERROR_TEXT("Read-only filesystem"); + break; + } + if (desc != NULL && decompress_length(desc) <= len) { + decompress(desc, buf); + return buf; + } + + const char *msg = ""; + #if MICROPY_PY_ERRNO + msg = qstr_str(mp_errno_to_str(errno_val)); + #endif + return msg[0] != '\0' ? msg : NULL; +} diff --git a/shared-bindings/keypad/EventQueue.c b/shared-bindings/keypad/EventQueue.c index 9293f8390b..44450ec21c 100644 --- a/shared-bindings/keypad/EventQueue.c +++ b/shared-bindings/keypad/EventQueue.c @@ -139,7 +139,7 @@ STATIC const mp_rom_map_elem_t keypad_eventqueue_locals_dict_table[] = { STATIC MP_DEFINE_CONST_DICT(keypad_eventqueue_locals_dict, keypad_eventqueue_locals_dict_table); -#if MICROPY_PY_USELECT +#if MICROPY_PY_SELECT STATIC mp_uint_t eventqueue_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { (void)errcode; keypad_eventqueue_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -170,7 +170,7 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_QSTR_EventQueue, MP_TYPE_FLAG_NONE, unary_op, keypad_eventqueue_unary_op, - #if MICROPY_PY_USELECT + #if MICROPY_PY_SELECT protocol, &eventqueue_p, #endif locals_dict, &keypad_eventqueue_locals_dict diff --git a/shared-bindings/memorymap/AddressRange.c b/shared-bindings/memorymap/AddressRange.c index fc3465a4e7..9df36db665 100644 --- a/shared-bindings/memorymap/AddressRange.c +++ b/shared-bindings/memorymap/AddressRange.c @@ -96,7 +96,7 @@ STATIC mp_obj_t memorymap_addressrange_make_new(const mp_obj_type_t *type, size_ } else if (mp_obj_is_exact_type(args[ARG_start].u_obj, &mp_type_int)) { start = mp_obj_int_get_uint_checked(args[ARG_start].u_obj); } else { - mp_obj_t arg = mp_unary_op(MP_UNARY_OP_INT, args[ARG_start].u_obj); + mp_obj_t arg = mp_unary_op(MP_UNARY_OP_INT_MAYBE, args[ARG_start].u_obj); start = mp_obj_int_get_uint_checked(arg); } size_t length =