1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-29 17:48:10 +00:00
trezor-firmware/core/embed/extmod/trezorobj.c
Martin Milata 3fb3f108cc chore(vendor): bump micropython to 1.19.1
Relevant micropython commits:
  01374d941f9d7398e35990b574769b20c6779457 py/mpconfig.h: Define initial templates for "feature levels".
  7b89ad8dbf432ab51eea6d138e179bf51394c786 py/vm: Add a fast path for LOAD_ATTR on instance types.
  68219a295c75457c096ac42dbe8411b84e1e1a51 stm32: Enable LOAD_ATTR fast path, and map lookup caching on >M0.
  e0bf4611c3a8b23b3c52e6a7804aac341ac3a87d py: Only search frozen modules when '.frozen' is found in sys.path.
  f2040bfc7ee033e48acef9f289790f3b4e6b74e5 py: Rework bytecode and .mpy file format to be mostly static data.
  926b554dafffa1e9bd80aa12fea5c621221c9d79 extmod/moduos: Create general uos module to be used by all ports.
  2b409ef8a46015f8f3bd20bc44e644637dbe9bd3 unix/moduos: Convert module to use extmod version.
  47f634300c5572571816817f16836113c98814ae py: Change makemoduledefs process so it uses output of qstr extraction.
  0e7bfc88c6ac6b5d64240f91183a3cfe2ab67ade all: Use mp_obj_malloc everywhere it's applicable.
  2a6ba47110be88ff1e1f5abd1bd76c353447884c py/obj: Add static safety checks to mp_obj_is_type().
2023-04-21 13:14:07 +02:00

95 lines
2.6 KiB
C

/*
* This file is part of the Trezor project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include "memzero.h"
#include "py/obj.h"
#include "py/objint.h"
#include "py/objstr.h"
#include "py/runtime.h"
static bool mpz_as_ll_checked(const mpz_t *i, long long *value) {
// Analogue of `mpz_as_int_checked` from mpz.c
unsigned long long val = 0;
mpz_dig_t *d = i->dig + i->len;
while (d-- > i->dig) {
if (val > (~0x8000000000000000 >> MPZ_DIG_SIZE)) {
// will overflow
*value = 0;
return false;
}
val = (val << MPZ_DIG_SIZE) | *d;
}
if (i->neg != 0) {
val = -val;
}
*value = val;
return true;
}
bool trezor_obj_get_ll_checked(mp_obj_t obj, long long *value) {
if (mp_obj_is_small_int(obj)) {
// Value is fitting in a small int range. Return it directly.
*value = MP_OBJ_SMALL_INT_VALUE(obj);
return true;
} else if (mp_obj_is_int(obj)) {
// Value is not fitting into small int range, but is an integer.
mp_obj_int_t *self = MP_OBJ_TO_PTR(obj);
// Try to get the long long value out of the MPZ struct.
return mpz_as_ll_checked(&self->mpz, value);
} else {
// Value is not integer.
*value = 0;
return false;
}
}
mp_obj_t trezor_obj_call_protected(void (*func)(void *), void *arg) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
(*func)(arg);
nlr_pop();
return NULL;
} else {
return MP_OBJ_FROM_PTR(nlr.ret_val);
}
}
mp_obj_t trezor_obj_str_from_rom_text(const char *str) {
// taken from mp_obj_new_exception_msg
mp_obj_str_t *o_str = m_new_obj_maybe(mp_obj_str_t);
if (o_str == NULL) return NULL;
o_str->base.type = &mp_type_str;
o_str->len = strlen(str);
o_str->data = (const byte *)str;
#if MICROPY_ROM_TEXT_COMPRESSION
o_str->hash = 0; // will be computed only if string object is accessed
#else
o_str->hash = qstr_compute_hash(o_str->data, o_str->len);
#endif
return MP_OBJ_FROM_PTR(o_str);
}