2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2021-04-22 20:55:39 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2014-2017 Paul Sokolovsky
|
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2014-2019 Damien P. George
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-02-14 10:16:35 -05:00
|
|
|
#include <stdint.h>
|
2014-04-18 18:25:49 -04:00
|
|
|
#include <stdlib.h>
|
2014-08-10 13:16:39 -04:00
|
|
|
#include <stddef.h>
|
2014-06-02 09:04:26 -04:00
|
|
|
#include <string.h>
|
2014-02-14 10:16:35 -05:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 15:27:54 -05:00
|
|
|
#include "py/binary.h"
|
|
|
|
#include "py/smallint.h"
|
2015-04-25 18:16:39 -04:00
|
|
|
#include "py/objint.h"
|
2017-01-17 14:50:20 -05:00
|
|
|
#include "py/runtime.h"
|
2014-02-14 10:16:35 -05:00
|
|
|
|
2018-08-08 21:24:49 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
2014-02-14 10:16:35 -05:00
|
|
|
// Helpers to work with binary-encoded data
|
|
|
|
|
2014-08-10 13:16:39 -04:00
|
|
|
#ifndef alignof
|
|
|
|
#define alignof(type) offsetof(struct { char c; type t; }, t)
|
|
|
|
#endif
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) {
|
2015-05-10 07:04:38 -04:00
|
|
|
size_t size = 0;
|
2014-04-18 18:25:49 -04:00
|
|
|
int align = 1;
|
|
|
|
switch (struct_type) {
|
2021-03-15 09:57:36 -04:00
|
|
|
case '<':
|
|
|
|
case '>':
|
2014-04-18 18:25:49 -04:00
|
|
|
switch (val_type) {
|
2021-03-15 09:57:36 -04:00
|
|
|
case 'b':
|
|
|
|
case 'B':
|
|
|
|
case 'x':
|
|
|
|
size = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
size = 2;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
size = 4;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
size = 4;
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
size = 8;
|
|
|
|
break;
|
|
|
|
#if MICROPY_NONSTANDARD_TYPECODES
|
|
|
|
case 'P':
|
|
|
|
case 'O':
|
|
|
|
case 'S':
|
|
|
|
size = sizeof(void *);
|
|
|
|
break;
|
|
|
|
#endif
|
2014-11-21 13:16:25 -05:00
|
|
|
case 'f':
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(float);
|
|
|
|
break;
|
2014-11-21 13:16:25 -05:00
|
|
|
case 'd':
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(double);
|
|
|
|
break;
|
2014-04-18 18:25:49 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '@': {
|
|
|
|
// TODO:
|
|
|
|
// The simplest heuristic for alignment is to align by value
|
|
|
|
// size, but that doesn't work for "bigger than int" types,
|
|
|
|
// for example, long long may very well have long alignment
|
|
|
|
// So, we introduce separate alignment handling, but having
|
|
|
|
// formal support for that is different from actually supporting
|
|
|
|
// particular (or any) ABI.
|
|
|
|
switch (val_type) {
|
|
|
|
case BYTEARRAY_TYPECODE:
|
2021-03-15 09:57:36 -04:00
|
|
|
case 'b':
|
|
|
|
case 'B':
|
|
|
|
case 'x':
|
|
|
|
align = size = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
2014-08-10 13:16:39 -04:00
|
|
|
align = alignof(short);
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(short);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
2014-08-10 13:16:39 -04:00
|
|
|
align = alignof(int);
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(int);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
2014-08-10 13:16:39 -04:00
|
|
|
align = alignof(long);
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(long);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
2014-08-10 13:16:39 -04:00
|
|
|
align = alignof(long long);
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(long long);
|
|
|
|
break;
|
|
|
|
#if MICROPY_NONSTANDARD_TYPECODES
|
|
|
|
case 'P':
|
|
|
|
case 'O':
|
|
|
|
case 'S':
|
|
|
|
align = alignof(void *);
|
|
|
|
size = sizeof(void *);
|
|
|
|
break;
|
|
|
|
#endif
|
2014-11-21 13:16:25 -05:00
|
|
|
case 'f':
|
|
|
|
align = alignof(float);
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(float);
|
|
|
|
break;
|
2014-11-21 13:16:25 -05:00
|
|
|
case 'd':
|
|
|
|
align = alignof(double);
|
2021-03-15 09:57:36 -04:00
|
|
|
size = sizeof(double);
|
|
|
|
break;
|
2014-04-18 18:25:49 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-14 13:21:50 -05:00
|
|
|
}
|
2017-01-17 14:50:20 -05:00
|
|
|
|
|
|
|
if (size == 0) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("bad typecode"));
|
2017-01-17 14:50:20 -05:00
|
|
|
}
|
|
|
|
|
2014-04-18 18:25:49 -04:00
|
|
|
if (palign != NULL) {
|
|
|
|
*palign = align;
|
|
|
|
}
|
|
|
|
return size;
|
2014-02-14 13:21:50 -05:00
|
|
|
}
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) {
|
2014-07-03 08:25:24 -04:00
|
|
|
mp_int_t val = 0;
|
2014-02-14 10:16:35 -05:00
|
|
|
switch (typecode) {
|
|
|
|
case 'b':
|
2021-03-15 09:57:36 -04:00
|
|
|
val = ((signed char *)p)[index];
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case BYTEARRAY_TYPECODE:
|
|
|
|
case 'B':
|
2021-03-15 09:57:36 -04:00
|
|
|
val = ((unsigned char *)p)[index];
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'h':
|
2021-03-15 09:57:36 -04:00
|
|
|
val = ((short *)p)[index];
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'H':
|
2021-03-15 09:57:36 -04:00
|
|
|
val = ((unsigned short *)p)[index];
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'i':
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_int(((int *)p)[index]);
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'I':
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_int_from_uint(((unsigned int *)p)[index]);
|
2015-01-23 20:18:10 -05:00
|
|
|
case 'l':
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_int(((long *)p)[index]);
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'L':
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_int_from_uint(((unsigned long *)p)[index]);
|
2015-09-01 11:25:29 -04:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'q':
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_int_from_ll(((long long *)p)[index]);
|
2015-09-01 11:25:29 -04:00
|
|
|
case 'Q':
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_int_from_ull(((unsigned long long *)p)[index]);
|
2015-09-01 11:25:29 -04:00
|
|
|
#endif
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'f':
|
2020-04-09 03:05:48 -04:00
|
|
|
return mp_obj_new_float_from_f(((float *)p)[index]);
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'd':
|
2020-03-31 08:48:08 -04:00
|
|
|
return mp_obj_new_float_from_d(((double *)p)[index]);
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
#if MICROPY_NONSTANDARD_TYPECODES
|
2015-03-05 15:57:36 -05:00
|
|
|
// Extension to CPython: array of objects
|
|
|
|
case 'O':
|
2021-03-15 09:57:36 -04:00
|
|
|
return ((mp_obj_t *)p)[index];
|
2015-10-12 03:10:39 -04:00
|
|
|
// Extension to CPython: array of pointers
|
|
|
|
case 'P':
|
2021-03-15 09:57:36 -04:00
|
|
|
return mp_obj_new_int((mp_int_t)(uintptr_t)((void **)p)[index]);
|
|
|
|
#endif
|
2014-02-14 10:16:35 -05:00
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:10:33 -04:00
|
|
|
// The long long type is guaranteed to hold at least 64 bits, and size is at
|
|
|
|
// most 8 (for q and Q), so we will always be able to parse the given data
|
|
|
|
// and fit it into a long long.
|
2021-04-23 15:26:42 -04:00
|
|
|
long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const byte *src) {
|
2014-06-25 15:25:53 -04:00
|
|
|
int delta;
|
|
|
|
if (!big_endian) {
|
|
|
|
delta = -1;
|
2014-10-06 11:05:35 -04:00
|
|
|
src += size - 1;
|
2014-06-25 15:25:53 -04:00
|
|
|
} else {
|
|
|
|
delta = 1;
|
|
|
|
}
|
|
|
|
|
2014-09-10 17:10:33 -04:00
|
|
|
long long val = 0;
|
2014-10-06 11:05:35 -04:00
|
|
|
if (is_signed && *src & 0x80) {
|
2014-06-25 15:25:53 -04:00
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
for (uint i = 0; i < size; i++) {
|
2019-10-07 21:48:25 -04:00
|
|
|
val *= 256;
|
2014-10-06 11:05:35 -04:00
|
|
|
val |= *src;
|
|
|
|
src += delta;
|
2014-06-25 15:25:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-04-10 15:19:32 -04:00
|
|
|
#define is_signed(typecode) (typecode > 'Z')
|
2021-04-23 15:26:42 -04:00
|
|
|
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr) {
|
2014-04-09 20:45:38 -04:00
|
|
|
byte *p = *ptr;
|
2021-04-23 15:26:42 -04:00
|
|
|
size_t align;
|
2014-04-18 18:25:49 -04:00
|
|
|
|
2015-05-10 07:04:38 -04:00
|
|
|
size_t size = mp_binary_get_size(struct_type, val_type, &align);
|
2014-04-18 18:25:49 -04:00
|
|
|
if (struct_type == '@') {
|
2021-04-23 15:26:42 -04:00
|
|
|
// Align p relative to p_base
|
|
|
|
p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
|
2014-04-18 18:25:49 -04:00
|
|
|
#if MP_ENDIANNESS_LITTLE
|
|
|
|
struct_type = '<';
|
|
|
|
#else
|
|
|
|
struct_type = '>';
|
|
|
|
#endif
|
2014-04-09 20:45:38 -04:00
|
|
|
}
|
2014-06-25 15:25:53 -04:00
|
|
|
*ptr = p + size;
|
2014-04-10 15:19:32 -04:00
|
|
|
|
2014-09-10 17:10:33 -04:00
|
|
|
long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
|
2014-04-10 15:19:32 -04:00
|
|
|
|
2018-03-26 19:13:49 -04:00
|
|
|
if (MICROPY_NONSTANDARD_TYPECODES && (val_type == 'O')) {
|
2014-09-10 17:10:33 -04:00
|
|
|
return (mp_obj_t)(mp_uint_t)val;
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_NONSTANDARD_TYPECODES
|
2014-06-02 09:04:26 -04:00
|
|
|
} else if (val_type == 'S') {
|
2021-03-15 09:57:36 -04:00
|
|
|
const char *s_val = (const char *)(uintptr_t)(mp_uint_t)val;
|
2018-07-28 13:29:47 -04:00
|
|
|
return mp_obj_new_str(s_val, strlen(s_val));
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2015-01-24 21:50:34 -05:00
|
|
|
} else if (val_type == 'f') {
|
2021-03-15 09:57:36 -04:00
|
|
|
union { uint32_t i;
|
|
|
|
float f;
|
|
|
|
} fpu = {val};
|
2020-04-09 03:05:48 -04:00
|
|
|
return mp_obj_new_float_from_f(fpu.f);
|
2015-01-24 21:50:34 -05:00
|
|
|
} else if (val_type == 'd') {
|
2021-03-15 09:57:36 -04:00
|
|
|
union { uint64_t i;
|
|
|
|
double f;
|
|
|
|
} fpu = {val};
|
2020-03-31 08:48:08 -04:00
|
|
|
return mp_obj_new_float_from_d(fpu.f);
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2014-04-19 22:19:10 -04:00
|
|
|
} else if (is_signed(val_type)) {
|
2014-09-10 17:10:33 -04:00
|
|
|
if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
|
|
|
|
return mp_obj_new_int((mp_int_t)val);
|
|
|
|
} else {
|
|
|
|
return mp_obj_new_int_from_ll(val);
|
|
|
|
}
|
2014-04-10 15:19:32 -04:00
|
|
|
} else {
|
2014-09-10 17:10:33 -04:00
|
|
|
if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
|
|
|
|
return mp_obj_new_int_from_uint((mp_uint_t)val);
|
|
|
|
} else {
|
|
|
|
return mp_obj_new_int_from_ull(val);
|
|
|
|
}
|
2014-04-10 15:19:32 -04:00
|
|
|
}
|
2014-04-09 20:45:38 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
|
2014-10-06 11:05:35 -04:00
|
|
|
if (MP_ENDIANNESS_LITTLE && !big_endian) {
|
|
|
|
memcpy(dest, &val, val_sz);
|
|
|
|
} else if (MP_ENDIANNESS_BIG && big_endian) {
|
|
|
|
// only copy the least-significant val_sz bytes
|
2021-03-15 09:57:36 -04:00
|
|
|
memcpy(dest, (byte *)&val + sizeof(mp_uint_t) - val_sz, val_sz);
|
2014-07-05 16:43:00 -04:00
|
|
|
} else {
|
2014-10-06 11:05:35 -04:00
|
|
|
const byte *src;
|
|
|
|
if (MP_ENDIANNESS_LITTLE) {
|
2021-03-15 09:57:36 -04:00
|
|
|
src = (const byte *)&val + val_sz;
|
2014-10-06 11:05:35 -04:00
|
|
|
} else {
|
2021-03-15 09:57:36 -04:00
|
|
|
src = (const byte *)&val + sizeof(mp_uint_t);
|
2014-10-06 11:05:35 -04:00
|
|
|
}
|
|
|
|
while (val_sz--) {
|
|
|
|
*dest++ = *--src;
|
|
|
|
}
|
2014-07-05 16:43:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr) {
|
2014-04-18 20:13:15 -04:00
|
|
|
byte *p = *ptr;
|
2021-04-23 15:26:42 -04:00
|
|
|
size_t align;
|
2014-04-18 20:13:15 -04:00
|
|
|
|
2015-05-10 07:04:38 -04:00
|
|
|
size_t size = mp_binary_get_size(struct_type, val_type, &align);
|
2014-04-18 20:13:15 -04:00
|
|
|
if (struct_type == '@') {
|
2021-04-23 15:26:42 -04:00
|
|
|
// Align p relative to p_base
|
|
|
|
p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align);
|
2014-10-06 11:05:35 -04:00
|
|
|
if (MP_ENDIANNESS_LITTLE) {
|
|
|
|
struct_type = '<';
|
|
|
|
} else {
|
|
|
|
struct_type = '>';
|
|
|
|
}
|
2014-04-18 20:13:15 -04:00
|
|
|
}
|
2014-06-25 15:25:53 -04:00
|
|
|
*ptr = p + size;
|
2014-04-18 20:13:15 -04:00
|
|
|
|
2014-10-06 11:05:35 -04:00
|
|
|
mp_uint_t val;
|
2014-04-19 22:19:10 -04:00
|
|
|
switch (val_type) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_NONSTANDARD_TYPECODES
|
2014-04-19 22:19:10 -04:00
|
|
|
case 'O':
|
2014-10-06 11:05:35 -04:00
|
|
|
val = (mp_uint_t)val_in;
|
2014-04-19 22:19:10 -04:00
|
|
|
break;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2015-01-24 21:50:34 -05:00
|
|
|
case 'f': {
|
2021-03-15 09:57:36 -04:00
|
|
|
union { uint32_t i;
|
|
|
|
float f;
|
|
|
|
} fp_sp;
|
2020-03-31 08:48:08 -04:00
|
|
|
fp_sp.f = mp_obj_get_float_to_f(val_in);
|
2015-01-24 21:50:34 -05:00
|
|
|
val = fp_sp.i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd': {
|
2021-03-15 09:57:36 -04:00
|
|
|
union { uint64_t i64;
|
|
|
|
uint32_t i32[2];
|
|
|
|
double f;
|
|
|
|
} fp_dp;
|
2020-04-09 03:05:48 -04:00
|
|
|
fp_dp.f = mp_obj_get_float_to_d(val_in);
|
2015-01-24 21:50:34 -05:00
|
|
|
if (BYTES_PER_WORD == 8) {
|
|
|
|
val = fp_dp.i64;
|
|
|
|
} else {
|
|
|
|
int be = struct_type == '>';
|
|
|
|
mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]);
|
|
|
|
p += sizeof(uint32_t);
|
|
|
|
val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-05-08 23:50:35 -04:00
|
|
|
default: {
|
|
|
|
bool signed_type = is_signed(val_type);
|
2015-04-25 18:16:39 -04:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_type(val_in, &mp_type_int)) {
|
2019-05-12 00:10:53 -04:00
|
|
|
// It's a longint.
|
2019-05-08 23:50:35 -04:00
|
|
|
mp_obj_int_buffer_overflow_check(val_in, size, signed_type);
|
2015-04-25 18:16:39 -04:00
|
|
|
mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p);
|
|
|
|
return;
|
2020-02-19 08:17:13 -05:00
|
|
|
}
|
2015-04-25 18:16:39 -04:00
|
|
|
#endif
|
|
|
|
{
|
2014-12-05 18:13:52 -05:00
|
|
|
val = mp_obj_get_int(val_in);
|
2019-05-12 00:10:53 -04:00
|
|
|
// Small int checking is separate, to be fast.
|
|
|
|
mp_small_int_buffer_overflow_check(val, size, signed_type);
|
2016-12-11 23:00:06 -05:00
|
|
|
// zero/sign extend if needed
|
|
|
|
if (BYTES_PER_WORD < 8 && size > sizeof(val)) {
|
|
|
|
int c = (is_signed(val_type) && (mp_int_t)val < 0) ? 0xff : 0x00;
|
2017-08-12 08:40:49 -04:00
|
|
|
memset(p, c, size);
|
|
|
|
if (struct_type == '>') {
|
|
|
|
p += size - sizeof(val);
|
|
|
|
}
|
2015-04-25 18:51:14 -04:00
|
|
|
}
|
2021-05-04 14:40:55 -04:00
|
|
|
break;
|
2014-12-05 18:13:52 -05:00
|
|
|
}
|
2019-05-08 23:50:35 -04:00
|
|
|
}
|
2014-04-19 22:19:10 -04:00
|
|
|
}
|
|
|
|
|
2021-04-16 15:39:23 -04:00
|
|
|
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
|
2014-04-18 20:13:15 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in) {
|
2014-04-18 18:28:12 -04:00
|
|
|
switch (typecode) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-18 18:28:12 -04:00
|
|
|
case 'f':
|
2020-03-31 08:48:08 -04:00
|
|
|
((float *)p)[index] = mp_obj_get_float_to_f(val_in);
|
2014-04-18 18:28:12 -04:00
|
|
|
break;
|
|
|
|
case 'd':
|
2020-04-09 03:05:48 -04:00
|
|
|
((double *)p)[index] = mp_obj_get_float_to_d(val_in);
|
2014-04-18 18:28:12 -04:00
|
|
|
break;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
#if MICROPY_NONSTANDARD_TYPECODES
|
2015-03-05 15:57:36 -05:00
|
|
|
// Extension to CPython: array of objects
|
|
|
|
case 'O':
|
2021-03-15 09:57:36 -04:00
|
|
|
((mp_obj_t *)p)[index] = val_in;
|
2015-03-05 15:57:36 -05:00
|
|
|
break;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2019-05-08 23:50:35 -04:00
|
|
|
default: {
|
|
|
|
size_t size = mp_binary_get_size('@', typecode, NULL);
|
|
|
|
bool signed_type = is_signed(typecode);
|
|
|
|
|
2015-09-01 11:25:29 -04:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
2021-04-22 20:55:39 -04:00
|
|
|
if (mp_obj_is_type(val_in, &mp_type_int)) {
|
2019-05-12 00:10:53 -04:00
|
|
|
// It's a long int.
|
2019-05-08 23:50:35 -04:00
|
|
|
mp_obj_int_buffer_overflow_check(val_in, size, signed_type);
|
2015-09-01 11:25:29 -04:00
|
|
|
mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG,
|
2021-03-15 09:57:36 -04:00
|
|
|
size, (uint8_t *)p + index * size);
|
2015-09-01 11:25:29 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-08 23:50:35 -04:00
|
|
|
mp_int_t val = mp_obj_get_int(val_in);
|
2019-05-12 00:10:53 -04:00
|
|
|
// Small int checking is separate, to be fast.
|
|
|
|
mp_small_int_buffer_overflow_check(val, size, signed_type);
|
2019-05-08 23:50:35 -04:00
|
|
|
mp_binary_set_val_array_from_int(typecode, p, index, val);
|
|
|
|
}
|
2014-02-14 10:16:35 -05:00
|
|
|
}
|
2014-04-18 18:28:12 -04:00
|
|
|
}
|
2014-02-14 10:16:35 -05:00
|
|
|
|
2021-04-23 15:26:42 -04:00
|
|
|
void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val) {
|
2014-02-14 10:16:35 -05:00
|
|
|
switch (typecode) {
|
|
|
|
case 'b':
|
2021-03-15 09:57:36 -04:00
|
|
|
((signed char *)p)[index] = val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case BYTEARRAY_TYPECODE:
|
|
|
|
case 'B':
|
2021-03-15 09:57:36 -04:00
|
|
|
((unsigned char *)p)[index] = val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'h':
|
2021-03-15 09:57:36 -04:00
|
|
|
((short *)p)[index] = val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'H':
|
2021-03-15 09:57:36 -04:00
|
|
|
((unsigned short *)p)[index] = val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'i':
|
2021-03-15 09:57:36 -04:00
|
|
|
((int *)p)[index] = val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'I':
|
2021-03-15 09:57:36 -04:00
|
|
|
((unsigned int *)p)[index] = val;
|
2015-01-23 20:18:10 -05:00
|
|
|
break;
|
|
|
|
case 'l':
|
2021-03-15 09:57:36 -04:00
|
|
|
((long *)p)[index] = val;
|
2015-01-23 20:18:10 -05:00
|
|
|
break;
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'L':
|
2021-03-15 09:57:36 -04:00
|
|
|
((unsigned long *)p)[index] = val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
2015-09-01 11:25:29 -04:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'q':
|
2021-03-15 09:57:36 -04:00
|
|
|
((long long *)p)[index] = val;
|
2017-07-03 12:11:46 -04:00
|
|
|
break;
|
2015-09-01 11:25:29 -04:00
|
|
|
case 'Q':
|
2021-03-15 09:57:36 -04:00
|
|
|
((unsigned long long *)p)[index] = val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
2015-09-01 11:25:29 -04:00
|
|
|
#endif
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-02-14 10:16:35 -05:00
|
|
|
case 'f':
|
2020-04-13 14:56:31 -04:00
|
|
|
((float *)p)[index] = (float)val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
|
|
|
case 'd':
|
2020-04-13 14:56:31 -04:00
|
|
|
((double *)p)[index] = (double)val;
|
2014-02-14 10:16:35 -05:00
|
|
|
break;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
#if MICROPY_NONSTANDARD_TYPECODES
|
2015-10-12 03:10:39 -04:00
|
|
|
// Extension to CPython: array of pointers
|
|
|
|
case 'P':
|
2021-03-15 09:57:36 -04:00
|
|
|
((void **)p)[index] = (void *)(uintptr_t)val;
|
2017-07-03 12:11:46 -04:00
|
|
|
break;
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2014-02-14 10:16:35 -05:00
|
|
|
}
|
|
|
|
}
|