circuitpython/shared-module/msgpack/__init__.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

525 lines
16 KiB
C
Raw Normal View History

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2019 Bernhard Boser
*
* 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
* AUTHORS OR 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 <stdio.h>
2020-11-12 15:28:40 -05:00
#include <inttypes.h>
2020-12-07 18:16:16 -05:00
#include "py/obj.h"
#include "py/binary.h"
#include "py/objarray.h"
#include "py/objlist.h"
#include "py/objstringio.h"
#include "py/parsenum.h"
#include "py/runtime.h"
#include "py/stream.h"
2020-12-07 18:16:16 -05:00
#include "shared-bindings/msgpack/ExtType.h"
#include "shared-bindings/msgpack/__init__.h"
#include "shared-module/msgpack/__init__.h"
////////////////////////////////////////////////////////////////
// stream management
typedef struct _msgpack_stream_t {
mp_obj_t stream_obj;
mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
int errcode;
} msgpack_stream_t;
STATIC msgpack_stream_t get_stream(mp_obj_t stream_obj, int flags) {
const mp_stream_p_t *stream_p = mp_get_stream_raise(stream_obj, flags);
msgpack_stream_t s = {stream_obj, stream_p->read, stream_p->write, 0};
return s;
}
////////////////////////////////////////////////////////////////
// readers
2020-12-07 18:16:16 -05:00
STATIC void read(msgpack_stream_t *s, void *buf, mp_uint_t size) {
2021-03-15 09:57:36 -04:00
if (size == 0) {
return;
}
mp_uint_t ret = s->read(s->stream_obj, buf, size, &s->errcode);
if (s->errcode != 0) {
mp_raise_OSError(s->errcode);
}
if (ret == 0) {
mp_raise_msg(&mp_type_EOFError, NULL);
}
if (ret < size) {
mp_raise_ValueError(translate("short read"));
}
}
2020-11-12 15:28:40 -05:00
STATIC uint8_t read1(msgpack_stream_t *s) {
uint8_t res = 0;
2020-12-07 18:16:16 -05:00
read(s, &res, 1);
2020-11-12 15:28:40 -05:00
return res;
}
STATIC uint16_t read2(msgpack_stream_t *s) {
uint16_t res = 0;
2020-12-07 18:16:16 -05:00
read(s, &res, 2);
2020-11-12 15:28:40 -05:00
int n = 1;
2021-03-15 09:57:36 -04:00
if (*(char *)&n == 1) {
res = __builtin_bswap16(res);
}
2020-11-12 15:28:40 -05:00
return res;
}
STATIC uint32_t read4(msgpack_stream_t *s) {
uint32_t res = 0;
2020-12-07 18:16:16 -05:00
read(s, &res, 4);
2020-11-12 15:28:40 -05:00
int n = 1;
2021-03-15 09:57:36 -04:00
if (*(char *)&n == 1) {
res = __builtin_bswap32(res);
}
2020-11-12 15:28:40 -05:00
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;
}
2020-11-12 15:28:40 -05:00
STATIC size_t read_size(msgpack_stream_t *s, uint8_t len_index) {
size_t res = 0;
2020-11-12 15:28:40 -05:00
switch (len_index) {
2021-03-15 09:57:36 -04:00
case 0:
res = (size_t)read1(s);
break;
case 1:
res = (size_t)read2(s);
break;
case 2:
res = (size_t)read4(s);
break;
2020-11-12 15:28:40 -05:00
}
return res;
}
////////////////////////////////////////////////////////////////
// writers
2020-12-07 18:16:16 -05:00
STATIC void write(msgpack_stream_t *s, const void *buf, mp_uint_t size) {
mp_uint_t ret = s->write(s->stream_obj, buf, size, &s->errcode);
if (s->errcode != 0) {
mp_raise_OSError(s->errcode);
}
if (ret == 0) {
mp_raise_msg(&mp_type_EOFError, NULL);
}
}
2020-11-12 15:28:40 -05:00
STATIC void write1(msgpack_stream_t *s, uint8_t obj) {
2020-12-07 18:16:16 -05:00
write(s, &obj, 1);
2020-11-12 15:28:40 -05:00
}
STATIC void write2(msgpack_stream_t *s, uint16_t obj) {
int n = 1;
2021-03-15 09:57:36 -04:00
if (*(char *)&n == 1) {
obj = __builtin_bswap16(obj);
}
2020-12-07 18:16:16 -05:00
write(s, &obj, 2);
2020-11-12 15:28:40 -05:00
}
STATIC void write4(msgpack_stream_t *s, uint32_t obj) {
int n = 1;
2021-03-15 09:57:36 -04:00
if (*(char *)&n == 1) {
obj = __builtin_bswap32(obj);
}
2020-12-07 18:16:16 -05:00
write(s, &obj, 4);
}
2020-11-12 15:28:40 -05:00
// compute and write msgpack size code (array structures)
STATIC void write_size(msgpack_stream_t *s, uint8_t code, size_t size) {
if ((uint8_t)size == size) {
2020-11-12 15:28:40 -05:00
write1(s, code);
write1(s, size);
} else if ((uint16_t)size == size) {
2021-03-15 09:57:36 -04:00
write1(s, code + 1);
2020-11-12 15:28:40 -05:00
write2(s, size);
} else {
2021-03-15 09:57:36 -04:00
write1(s, code + 2);
2020-11-12 15:28:40 -05:00
write4(s, size);
}
}
////////////////////////////////////////////////////////////////
// packers
// This is a helper function to iterate through a dictionary. The state of
// the iteration is held in *cur and should be initialised with zero for the
// first call. Will return NULL when no more elements are available.
STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
size_t max = dict->map.alloc;
mp_map_t *map = &dict->map;
for (size_t i = *cur; i < max; i++) {
if (mp_map_slot_is_filled(map, i)) {
*cur = i + 1;
return &(map->table[i]);
}
}
return NULL;
}
STATIC void pack_int(msgpack_stream_t *s, int32_t x) {
if (x > -32 && x < 128) {
2020-11-12 15:28:40 -05:00
write1(s, x);
} else if ((int8_t)x == x) {
2020-11-12 15:28:40 -05:00
write1(s, 0xd0);
write1(s, x);
} else if ((int16_t)x == x) {
2020-11-12 15:28:40 -05:00
write1(s, 0xd1);
write2(s, x);
} else {
2020-11-12 15:28:40 -05:00
write1(s, 0xd2);
write4(s, x);
}
}
2021-03-15 09:57:36 -04:00
STATIC void pack_bin(msgpack_stream_t *s, const uint8_t *data, size_t len) {
write_size(s, 0xc4, len);
2021-03-15 09:57:36 -04:00
if (len > 0) {
write(s, data, len);
}
}
STATIC void pack_ext(msgpack_stream_t *s, int8_t code, const uint8_t *data, size_t len) {
2020-12-07 18:16:16 -05:00
if (len == 1) {
write1(s, 0xd4);
} else if (len == 2) {
write1(s, 0xd5);
} else if (len == 4) {
write1(s, 0xd6);
} else if (len == 8) {
write1(s, 0xd7);
} else if (len == 16) {
write1(s, 0xd8);
} else {
write_size(s, 0xc7, len);
}
write1(s, code); // type byte
2021-03-15 09:57:36 -04:00
if (len > 0) {
write(s, data, len);
}
2020-12-07 18:16:16 -05:00
}
2021-03-15 09:57:36 -04:00
STATIC void pack_str(msgpack_stream_t *s, const char *str, size_t len) {
if (len < 32) {
2020-11-12 15:28:40 -05:00
write1(s, 0b10100000 | (uint8_t)len);
} else {
write_size(s, 0xd9, len);
}
2021-03-15 09:57:36 -04:00
if (len > 0) {
write(s, str, len);
}
}
2020-12-07 18:16:16 -05:00
STATIC void pack_array(msgpack_stream_t *s, size_t len) {
// only writes the header, manually write the objects after calling pack_array!
if (len < 16) {
2020-11-12 15:28:40 -05:00
write1(s, 0b10010000 | (uint8_t)len);
} else if (len < 0x10000) {
2020-11-12 15:28:40 -05:00
write1(s, 0xdc);
write2(s, len);
} else {
2020-11-12 15:28:40 -05:00
write1(s, 0xdd);
write4(s, len);
}
}
2020-12-07 18:16:16 -05:00
STATIC void pack_dict(msgpack_stream_t *s, size_t len) {
// only writes the header, manually write the objects after calling pack_array!
if (len < 16) {
2020-11-12 15:28:40 -05:00
write1(s, 0b10000000 | (uint8_t)len);
} else if (len < 0x10000) {
2020-11-12 15:28:40 -05:00
write1(s, 0xde);
write2(s, len);
} else {
2020-11-12 15:28:40 -05:00
write1(s, 0xdf);
write4(s, len);
}
}
2020-12-07 18:16:16 -05:00
STATIC void pack(mp_obj_t obj, msgpack_stream_t *s, mp_obj_t default_handler) {
if (mp_obj_is_small_int(obj)) {
// int
int32_t x = MP_OBJ_SMALL_INT_VALUE(obj);
pack_int(s, x);
} else if (mp_obj_is_str(obj)) {
// string
size_t len;
const char *data = mp_obj_str_get_data(obj, &len);
pack_str(s, data, len);
} else if (mp_obj_is_type(obj, &mod_msgpack_exttype_type)) {
2020-12-07 18:16:16 -05:00
mod_msgpack_extype_obj_t *ext = MP_OBJ_TO_PTR(obj);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(ext->data, &bufinfo, MP_BUFFER_READ);
pack_ext(s, ext->code, bufinfo.buf, bufinfo.len);
} else if (mp_obj_is_type(obj, &mp_type_tuple)) {
// tuple
mp_obj_tuple_t *self = MP_OBJ_TO_PTR(obj);
pack_array(s, self->len);
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < self->len; i++) {
2020-12-07 18:16:16 -05:00
pack(self->items[i], s, default_handler);
}
} else if (mp_obj_is_type(obj, &mp_type_list)) {
// list (layout differs from tuple)
mp_obj_list_t *self = MP_OBJ_TO_PTR(obj);
pack_array(s, self->len);
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < self->len; i++) {
2020-12-07 18:16:16 -05:00
pack(self->items[i], s, default_handler);
}
} else if (mp_obj_is_type(obj, &mp_type_dict)) {
// dict
mp_obj_dict_t *self = MP_OBJ_TO_PTR(obj);
pack_dict(s, self->map.used);
size_t cur = 0;
mp_map_elem_t *next = NULL;
while ((next = dict_iter_next(self, &cur)) != NULL) {
2020-12-07 18:16:16 -05:00
pack(next->key, s, default_handler);
pack(next->value, s, default_handler);
}
} else if (mp_obj_is_float(obj)) {
2021-03-15 09:57:36 -04:00
union Float { mp_float_t f;
uint32_t u;
};
2020-11-12 15:28:40 -05:00
union Float data;
data.f = mp_obj_float_get(obj);
write1(s, 0xca);
write4(s, data.u);
} else if (obj == mp_const_none) {
2020-11-12 15:28:40 -05:00
write1(s, 0xc0);
} else if (obj == mp_const_false) {
2020-11-12 15:28:40 -05:00
write1(s, 0xc2);
} else if (obj == mp_const_true) {
2020-11-12 15:28:40 -05:00
write1(s, 0xc3);
} else {
2021-01-18 13:13:16 -05:00
mp_buffer_info_t bufinfo;
if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
// bytes (bin type)
pack_bin(s, bufinfo.buf, bufinfo.len);
} else if (default_handler != mp_const_none) {
2020-12-07 18:16:16 -05:00
// set default_handler to mp_const_none to avoid infinite recursion
// this also precludes some valid outputs
pack(mp_call_function_1(default_handler, obj), s, mp_const_none);
} else {
mp_raise_ValueError(translate("no default packer"));
}
}
}
////////////////////////////////////////////////////////////////
// unpacker
2020-12-07 18:16:16 -05:00
STATIC mp_obj_t unpack(msgpack_stream_t *s, mp_obj_t ext_hook, bool use_list);
STATIC mp_obj_t unpack_array_elements(msgpack_stream_t *s, size_t size, mp_obj_t ext_hook, bool use_list) {
if (use_list) {
mp_obj_list_t *t = MP_OBJ_TO_PTR(mp_obj_new_list(size, NULL));
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < size; i++) {
2020-12-07 18:16:16 -05:00
t->items[i] = unpack(s, ext_hook, use_list);
}
return MP_OBJ_FROM_PTR(t);
} else {
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(size, NULL));
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < size; i++) {
2020-12-07 18:16:16 -05:00
t->items[i] = unpack(s, ext_hook, use_list);
}
return MP_OBJ_FROM_PTR(t);
}
}
STATIC mp_obj_t unpack_bytes(msgpack_stream_t *s, size_t size) {
vstr_t vstr;
vstr_init_len(&vstr, size);
2021-03-15 09:57:36 -04:00
byte *p = (byte *)vstr.buf;
2021-01-18 13:13:16 -05:00
// read in chunks: (some drivers - e.g. UART) limit the
// maximum number of bytes that can be read at once
// read(s, p, size);
while (size > 0) {
int n = size > 256 ? 256 : size;
read(s, p, n);
size -= n;
p += n;
}
2023-10-03 15:03:59 -04:00
return mp_obj_new_bytes_from_vstr(&vstr);
2020-12-07 18:16:16 -05:00
}
STATIC mp_obj_t unpack_ext(msgpack_stream_t *s, size_t size, mp_obj_t ext_hook) {
int8_t code = read1(s);
mp_obj_t data = unpack_bytes(s, size);
if (ext_hook != mp_const_none) {
return mp_call_function_2(ext_hook, MP_OBJ_NEW_SMALL_INT(code), data);
} else {
mod_msgpack_extype_obj_t *o = mp_obj_malloc(mod_msgpack_extype_obj_t, &mod_msgpack_exttype_type);
2020-12-07 18:16:16 -05:00
o->code = code;
o->data = data;
return MP_OBJ_FROM_PTR(o);
}
}
STATIC mp_obj_t unpack(msgpack_stream_t *s, mp_obj_t ext_hook, bool use_list) {
2020-11-12 15:28:40 -05:00
uint8_t code = read1(s);
if (((code & 0b10000000) == 0) || ((code & 0b11100000) == 0b11100000)) {
// int
return MP_OBJ_NEW_SMALL_INT((int8_t)code);
}
if ((code & 0b11100000) == 0b10100000) {
// str
size_t len = code & 0b11111;
// allocate on stack; len < 32
char str[len];
2020-12-07 18:16:16 -05:00
read(s, &str, len);
return mp_obj_new_str(str, len);
}
if ((code & 0b11110000) == 0b10010000) {
2020-12-07 18:16:16 -05:00
// array (list / tuple)
return unpack_array_elements(s, code & 0b1111, ext_hook, use_list);
}
if ((code & 0b11110000) == 0b10000000) {
// map (dict)
size_t len = code & 0b1111;
mp_obj_dict_t *d = MP_OBJ_TO_PTR(mp_obj_new_dict(len));
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < len; i++) {
2020-12-07 18:16:16 -05:00
mp_obj_dict_store(d, unpack(s, ext_hook, use_list), unpack(s, ext_hook, use_list));
}
return MP_OBJ_FROM_PTR(d);
}
switch (code) {
2021-03-15 09:57:36 -04:00
case 0xc0:
return mp_const_none;
case 0xc2:
return mp_const_false;
case 0xc3:
return mp_const_true;
case 0xc4:
case 0xc5:
case 0xc6: {
// bin 8, 16, 32
2021-03-15 09:57:36 -04:00
return unpack_bytes(s, read_size(s, code - 0xc4));
}
2020-12-07 18:16:16 -05:00
case 0xcc: // uint8
return MP_OBJ_NEW_SMALL_INT((uint8_t)read1(s));
2020-12-07 18:16:16 -05:00
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));
2020-12-07 18:16:16 -05:00
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));
2020-12-07 18:16:16 -05:00
case 0xd2: // int32
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;
2021-03-15 09:57:36 -04:00
};
2020-11-12 15:28:40 -05:00
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: {
// str 8, 16, 32
2021-03-15 09:57:36 -04:00
size_t size = read_size(s, code - 0xd9);
vstr_t vstr;
vstr_init_len(&vstr, size);
2021-03-15 09:57:36 -04:00
byte *p = (byte *)vstr.buf;
2020-12-07 18:16:16 -05:00
read(s, p, size);
2023-10-03 15:03:59 -04:00
return mp_obj_new_str_from_vstr(&vstr);
}
case 0xde:
case 0xdf: {
// map 16 & 32
size_t len = read_size(s, code - 0xde + 1);
mp_obj_dict_t *d = MP_OBJ_TO_PTR(mp_obj_new_dict(len));
2021-03-15 09:57:36 -04:00
for (size_t i = 0; i < len; i++) {
2020-12-07 18:16:16 -05:00
mp_obj_dict_store(d, unpack(s, ext_hook, use_list), unpack(s, ext_hook, use_list));
}
return MP_OBJ_FROM_PTR(d);
}
case 0xdc:
case 0xdd: {
// array 16 & 32
2020-12-07 18:16:16 -05:00
size_t size = read_size(s, code - 0xdc + 1);
return unpack_array_elements(s, size, ext_hook, use_list);
}
case 0xd4: // fixenxt 1
2020-12-07 18:16:16 -05:00
return unpack_ext(s, 1, ext_hook);
case 0xd5: // fixenxt 2
2020-12-07 18:16:16 -05:00
return unpack_ext(s, 2, ext_hook);
case 0xd6: // fixenxt 4
2020-12-07 18:16:16 -05:00
return unpack_ext(s, 4, ext_hook);
case 0xd7: // fixenxt 8
2020-12-07 18:16:16 -05:00
return unpack_ext(s, 8, ext_hook);
case 0xd8: // fixenxt 16
2020-12-07 18:16:16 -05:00
return unpack_ext(s, 16, ext_hook);
case 0xc7: // ext 8
case 0xc8: // ext 16
case 0xc9:
// ext 8, 16, 32
2021-03-15 09:57:36 -04:00
return unpack_ext(s, read_size(s, code - 0xc7), ext_hook);
2020-12-07 18:16:16 -05:00
case 0xc1: // never used
default:
mp_raise_ValueError(translate("Invalid format"));
}
}
2020-12-07 18:16:16 -05:00
void common_hal_msgpack_pack(mp_obj_t obj, mp_obj_t stream_obj, mp_obj_t default_handler) {
msgpack_stream_t stream = get_stream(stream_obj, MP_STREAM_OP_WRITE);
2020-12-07 18:16:16 -05:00
pack(obj, &stream, default_handler);
}
2020-12-07 18:16:16 -05:00
mp_obj_t common_hal_msgpack_unpack(mp_obj_t stream_obj, mp_obj_t ext_hook, bool use_list) {
msgpack_stream_t stream = get_stream(stream_obj, MP_STREAM_OP_READ);
2020-12-07 18:16:16 -05:00
return unpack(&stream, ext_hook, use_list);
}