Skip to content

Add unsigned ints and 64 bit types to msgpack.unpack #7802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
Expand Down
42 changes: 33 additions & 9 deletions shared-module/msgpack/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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"));
}
}

Expand Down