From 1fad969389ee76138c4ae3834724e2bde550ae78 Mon Sep 17 00:00:00 2001 From: Neradoc Date: Sun, 17 Jul 2022 10:17:05 +0200 Subject: [PATCH] add unsigned ints and 64 bit types to msgpack change msgpack error message when format invalid --- locale/circuitpython.pot | 8 +++--- shared-module/msgpack/__init__.c | 42 +++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 55ab977e97..b5026f7829 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -400,10 +400,6 @@ msgstr "" msgid "3-arg pow() not supported" msgstr "" -#: shared-module/msgpack/__init__.c -msgid "64 bit types" -msgstr "" - #: ports/atmel-samd/common-hal/alarm/pin/PinAlarm.c #: ports/atmel-samd/common-hal/countio/Counter.c #: ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c @@ -1271,6 +1267,10 @@ msgstr "" msgid "Invalid data_pins[%d]" msgstr "" +#: shared-module/msgpack/__init__.c +msgid "Invalid format" +msgstr "" + #: shared-module/audiocore/WaveFile.c msgid "Invalid format chunk size" msgstr "" diff --git a/shared-module/msgpack/__init__.c b/shared-module/msgpack/__init__.c index d32550722c..3a34731d77 100644 --- a/shared-module/msgpack/__init__.c +++ b/shared-module/msgpack/__init__.c @@ -102,6 +102,16 @@ STATIC uint32_t read4(msgpack_stream_t *s) { return res; } +STATIC uint64_t read8(msgpack_stream_t *s) { + uint64_t res = 0; + read(s, &res, 8); + int n = 1; + if (*(char *)&n == 1) { + res = __builtin_bswap64(res); + } + return res; +} + STATIC size_t read_size(msgpack_stream_t *s, uint8_t len_index) { size_t res = 0; switch (len_index) { @@ -207,7 +217,7 @@ STATIC void pack_bin(msgpack_stream_t *s, const uint8_t *data, size_t len) { } } -STATIC void pack_ext(msgpack_stream_t *s, int8_t code, const uint8_t *data, size_t len) { +STATIC void pack_ext(msgpack_stream_t *s, int8_t code, const uint8_t *data, size_t len) { if (len == 1) { write1(s, 0xd4); } else if (len == 2) { @@ -424,22 +434,39 @@ STATIC mp_obj_t unpack(msgpack_stream_t *s, mp_obj_t ext_hook, bool use_list) { return unpack_bytes(s, read_size(s, code - 0xc4)); } case 0xcc: // uint8 + return MP_OBJ_NEW_SMALL_INT((uint8_t)read1(s)); case 0xd0: // int8 return MP_OBJ_NEW_SMALL_INT((int8_t)read1(s)); case 0xcd: // uint16 + return MP_OBJ_NEW_SMALL_INT((uint16_t)read2(s)); case 0xd1: // int16 return MP_OBJ_NEW_SMALL_INT((int16_t)read2(s)); case 0xce: // uint32 + return mp_obj_new_int_from_uint((uint32_t)read4(s)); case 0xd2: // int32 - return MP_OBJ_NEW_SMALL_INT((int32_t)read4(s)); - case 0xca: { - union Float { mp_float_t f; - uint32_t u; + return mp_obj_new_int((int32_t)read4(s)); + case 0xcf: // uint 64 + return mp_obj_new_int_from_ull((uint64_t)read8(s)); + case 0xd3: // int 64 + return mp_obj_new_int_from_ll((int64_t)read8(s)); + case 0xca: { // float + union Float { + mp_float_t f; + uint32_t u; }; union Float data; data.u = read4(s); return mp_obj_new_float(data.f); } + case 0xcb: { // double + union Double { + uint64_t u; + double d; + }; + union Double data; + data.u = read8(s); + return mp_obj_new_float_from_d(data.d); + } case 0xd9: case 0xda: case 0xdb: { @@ -483,11 +510,8 @@ STATIC mp_obj_t unpack(msgpack_stream_t *s, mp_obj_t ext_hook, bool use_list) { // ext 8, 16, 32 return unpack_ext(s, read_size(s, code - 0xc7), ext_hook); case 0xc1: // never used - case 0xcb: // float 64 - case 0xcf: // uint 64 - case 0xd3: // int 64 default: - mp_raise_NotImplementedError(translate("64 bit types")); + mp_raise_ValueError(translate("Invalid format")); } }