From 355c7fcbd5f26af79c862db96ccf2ca559c077f4 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 24 Jul 2020 15:55:35 +0200 Subject: [PATCH] core: introduce utils.decode_bytes This instructs the low-level code to use mp_obj_new_str_copy directly and avoids the usage of qstr_find_strn lookup. --- core/embed/extmod/modtrezorutils/modtrezorutils.c | 15 +++++++++++++++ core/mocks/generated/trezorutils.pyi | 7 +++++++ core/src/trezor/utils.py | 1 + 3 files changed, 23 insertions(+) diff --git a/core/embed/extmod/modtrezorutils/modtrezorutils.c b/core/embed/extmod/modtrezorutils/modtrezorutils.c index 8c892671b..ef50a9660 100644 --- a/core/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/core/embed/extmod/modtrezorutils/modtrezorutils.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include "py/objstr.h" #include "py/runtime.h" #include "version.h" @@ -115,6 +116,18 @@ STATIC mp_obj_t mod_trezorutils_halt(size_t n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorutils_halt_obj, 0, 1, mod_trezorutils_halt); +/// def decode_bytes(b: bytes) -> str: +/// """ +/// Decodes string from bytes +/// """ +STATIC mp_obj_t mod_trezorutils_decode_bytes(mp_obj_t b) { + mp_buffer_info_t bytes; + mp_get_buffer_raise(b, &bytes, MP_BUFFER_READ); + return mp_obj_new_str_copy(&mp_type_str, bytes.buf, bytes.len); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_decode_bytes_obj, + mod_trezorutils_decode_bytes); + #define PASTER(s) MP_QSTR_##s #define MP_QSTR(s) PASTER(s) @@ -131,6 +144,8 @@ STATIC const mp_rom_map_elem_t mp_module_trezorutils_globals_table[] = { {MP_ROM_QSTR(MP_QSTR_consteq), MP_ROM_PTR(&mod_trezorutils_consteq_obj)}, {MP_ROM_QSTR(MP_QSTR_memcpy), MP_ROM_PTR(&mod_trezorutils_memcpy_obj)}, {MP_ROM_QSTR(MP_QSTR_halt), MP_ROM_PTR(&mod_trezorutils_halt_obj)}, + {MP_ROM_QSTR(MP_QSTR_decode_bytes), + MP_ROM_PTR(&mod_trezorutils_decode_bytes_obj)}, // various built-in constants {MP_ROM_QSTR(MP_QSTR_GITREV), MP_ROM_QSTR(MP_QSTR(GITREV))}, {MP_ROM_QSTR(MP_QSTR_VERSION_MAJOR), MP_ROM_INT(VERSION_MAJOR)}, diff --git a/core/mocks/generated/trezorutils.pyi b/core/mocks/generated/trezorutils.pyi index e89fb601f..6a5c4d6f6 100644 --- a/core/mocks/generated/trezorutils.pyi +++ b/core/mocks/generated/trezorutils.pyi @@ -32,6 +32,13 @@ def halt(msg: str = None) -> None: """ Halts execution. """ + + +# extmod/modtrezorutils/modtrezorutils.c +def decode_bytes(b: bytes) -> str: + """ + Decodes string from bytes + """ GITREV: str VERSION_MAJOR: int VERSION_MINOR: int diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index 96bcfd9d5..b66c8612b 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -9,6 +9,7 @@ from trezorutils import ( # noqa: F401 VERSION_MINOR, VERSION_PATCH, consteq, + decode_bytes, halt, memcpy, )