Merge pull request #7802 from Neradoc/nera-add-formats-to-msgpack

Add unsigned ints and 64 bit types to msgpack.unpack
This commit is contained in:
Dan Halbert 2023-03-28 10:18:10 -04:00 committed by GitHub
commit 9a0fa5cd1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 13 deletions

View File

@ -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 ""

View File

@ -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) {
@ -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;
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"));
}
}