2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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 15:16:35 +00:00
|
|
|
#include <stdint.h>
|
2014-04-18 23:25:49 +01:00
|
|
|
#include <stdlib.h>
|
2014-08-10 18:16:39 +01:00
|
|
|
#include <stddef.h>
|
2014-06-02 14:04:26 +01:00
|
|
|
#include <string.h>
|
2014-02-14 15:16:35 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-01 20:27:54 +00:00
|
|
|
#include "py/binary.h"
|
|
|
|
#include "py/smallint.h"
|
2014-02-14 15:16:35 +00:00
|
|
|
|
|
|
|
// Helpers to work with binary-encoded data
|
|
|
|
|
2014-08-10 18:16:39 +01:00
|
|
|
#ifndef alignof
|
|
|
|
#define alignof(type) offsetof(struct { char c; type t; }, t)
|
|
|
|
#endif
|
|
|
|
|
2014-08-30 14:59:21 +01:00
|
|
|
int mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
|
2014-04-18 23:25:49 +01:00
|
|
|
int size = 0;
|
|
|
|
int align = 1;
|
|
|
|
switch (struct_type) {
|
|
|
|
case '<': case '>':
|
|
|
|
switch (val_type) {
|
|
|
|
case 'b': case 'B':
|
|
|
|
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;
|
2014-09-06 17:24:52 +01:00
|
|
|
case 'P': case 'O': case 'S':
|
|
|
|
size = sizeof(void*); break;
|
2014-11-21 18:16:25 +00:00
|
|
|
case 'f':
|
|
|
|
size = sizeof(float); break;
|
|
|
|
case 'd':
|
|
|
|
size = sizeof(double); break;
|
2014-04-18 23:25:49 +01: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:
|
|
|
|
case 'b': case 'B':
|
|
|
|
align = size = 1; break;
|
|
|
|
case 'h': case 'H':
|
2014-08-10 18:16:39 +01:00
|
|
|
align = alignof(short);
|
|
|
|
size = sizeof(short); break;
|
2014-04-18 23:25:49 +01:00
|
|
|
case 'i': case 'I':
|
2014-08-10 18:16:39 +01:00
|
|
|
align = alignof(int);
|
|
|
|
size = sizeof(int); break;
|
2014-04-18 23:25:49 +01:00
|
|
|
case 'l': case 'L':
|
2014-08-10 18:16:39 +01:00
|
|
|
align = alignof(long);
|
|
|
|
size = sizeof(long); break;
|
2014-04-18 23:25:49 +01:00
|
|
|
case 'q': case 'Q':
|
2014-08-10 18:16:39 +01:00
|
|
|
align = alignof(long long);
|
|
|
|
size = sizeof(long long); break;
|
2014-06-02 14:04:26 +01:00
|
|
|
case 'P': case 'O': case 'S':
|
2014-08-10 18:16:39 +01:00
|
|
|
align = alignof(void*);
|
|
|
|
size = sizeof(void*); break;
|
2014-11-21 18:16:25 +00:00
|
|
|
case 'f':
|
|
|
|
align = alignof(float);
|
|
|
|
size = sizeof(float); break;
|
|
|
|
case 'd':
|
|
|
|
align = alignof(double);
|
|
|
|
size = sizeof(double); break;
|
2014-04-18 23:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-14 18:21:50 +00:00
|
|
|
}
|
2014-04-18 23:25:49 +01:00
|
|
|
if (palign != NULL) {
|
|
|
|
*palign = align;
|
|
|
|
}
|
|
|
|
return size;
|
2014-02-14 18:21:50 +00:00
|
|
|
}
|
|
|
|
|
2014-10-03 18:44:14 +01:00
|
|
|
mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
|
2014-07-03 13:25:24 +01:00
|
|
|
mp_int_t val = 0;
|
2014-02-14 15:16:35 +00:00
|
|
|
switch (typecode) {
|
|
|
|
case 'b':
|
2015-01-24 01:18:10 +00:00
|
|
|
val = ((signed char*)p)[index];
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case BYTEARRAY_TYPECODE:
|
|
|
|
case 'B':
|
2015-01-24 01:18:10 +00:00
|
|
|
val = ((unsigned char*)p)[index];
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
2015-01-24 01:18:10 +00:00
|
|
|
val = ((short*)p)[index];
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'H':
|
2015-01-24 01:18:10 +00:00
|
|
|
val = ((unsigned short*)p)[index];
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
2015-01-24 01:18:10 +00:00
|
|
|
return mp_obj_new_int(((int*)p)[index]);
|
2014-02-14 15:16:35 +00:00
|
|
|
case 'I':
|
2015-01-24 01:18:10 +00:00
|
|
|
return mp_obj_new_int_from_uint(((unsigned int*)p)[index]);
|
|
|
|
case 'l':
|
|
|
|
return mp_obj_new_int(((long*)p)[index]);
|
2014-02-14 15:16:35 +00:00
|
|
|
case 'L':
|
2015-01-24 01:18:10 +00:00
|
|
|
return mp_obj_new_int_from_uint(((unsigned long*)p)[index]);
|
2014-02-15 23:02:00 +00:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
2014-02-14 15:16:35 +00:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
// TODO: Explode API more to cover signedness
|
|
|
|
return mp_obj_new_int_from_ll(((long long*)p)[index]);
|
|
|
|
#endif
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-02-14 15:16:35 +00:00
|
|
|
case 'f':
|
|
|
|
return mp_obj_new_float(((float*)p)[index]);
|
|
|
|
case 'd':
|
|
|
|
return mp_obj_new_float(((double*)p)[index]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:10:33 +01: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.
|
2014-10-06 16:05:35 +01:00
|
|
|
long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, const byte *src) {
|
2014-06-25 20:25:53 +01:00
|
|
|
int delta;
|
|
|
|
if (!big_endian) {
|
|
|
|
delta = -1;
|
2014-10-06 16:05:35 +01:00
|
|
|
src += size - 1;
|
2014-06-25 20:25:53 +01:00
|
|
|
} else {
|
|
|
|
delta = 1;
|
|
|
|
}
|
|
|
|
|
2014-09-10 22:10:33 +01:00
|
|
|
long long val = 0;
|
2014-10-06 16:05:35 +01:00
|
|
|
if (is_signed && *src & 0x80) {
|
2014-06-25 20:25:53 +01:00
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
for (uint i = 0; i < size; i++) {
|
|
|
|
val <<= 8;
|
2014-10-06 16:05:35 +01:00
|
|
|
val |= *src;
|
|
|
|
src += delta;
|
2014-06-25 20:25:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:19:32 +01:00
|
|
|
#define is_signed(typecode) (typecode > 'Z')
|
2014-04-11 01:47:21 +01:00
|
|
|
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
|
2014-04-10 01:45:38 +01:00
|
|
|
byte *p = *ptr;
|
2014-08-30 14:59:21 +01:00
|
|
|
mp_uint_t align;
|
2014-04-18 23:25:49 +01:00
|
|
|
|
|
|
|
int size = mp_binary_get_size(struct_type, val_type, &align);
|
|
|
|
if (struct_type == '@') {
|
|
|
|
// Make pointer aligned
|
2014-07-03 13:25:24 +01:00
|
|
|
p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
|
2014-04-18 23:25:49 +01:00
|
|
|
#if MP_ENDIANNESS_LITTLE
|
|
|
|
struct_type = '<';
|
|
|
|
#else
|
|
|
|
struct_type = '>';
|
|
|
|
#endif
|
2014-04-10 01:45:38 +01:00
|
|
|
}
|
2014-06-25 20:25:53 +01:00
|
|
|
*ptr = p + size;
|
2014-04-10 20:19:32 +01:00
|
|
|
|
2014-09-10 22:10:33 +01:00
|
|
|
long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
|
2014-04-10 20:19:32 +01:00
|
|
|
|
2014-04-20 03:19:10 +01:00
|
|
|
if (val_type == 'O') {
|
2014-09-10 22:10:33 +01:00
|
|
|
return (mp_obj_t)(mp_uint_t)val;
|
2014-06-02 14:04:26 +01:00
|
|
|
} else if (val_type == 'S') {
|
2014-09-10 22:10:33 +01:00
|
|
|
const char *s_val = (const char*)(mp_uint_t)val;
|
|
|
|
return mp_obj_new_str(s_val, strlen(s_val), false);
|
2015-01-25 02:50:34 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
} else if (val_type == 'f') {
|
|
|
|
union { uint32_t i; float f; } fpu = {val};
|
|
|
|
return mp_obj_new_float(fpu.f);
|
|
|
|
} else if (val_type == 'd') {
|
|
|
|
union { uint64_t i; double f; } fpu = {val};
|
|
|
|
return mp_obj_new_float(fpu.f);
|
|
|
|
#endif
|
2014-04-20 03:19:10 +01:00
|
|
|
} else if (is_signed(val_type)) {
|
2014-09-10 22:10:33 +01: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 20:19:32 +01:00
|
|
|
} else {
|
2014-09-10 22:10:33 +01: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 20:19:32 +01:00
|
|
|
}
|
2014-04-10 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
2014-10-06 16:05:35 +01:00
|
|
|
void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
|
|
|
|
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
|
|
|
|
memcpy(dest, (byte*)&val + sizeof(mp_uint_t) - val_sz, val_sz);
|
2014-07-05 21:43:00 +01:00
|
|
|
} else {
|
2014-10-06 16:05:35 +01:00
|
|
|
const byte *src;
|
|
|
|
if (MP_ENDIANNESS_LITTLE) {
|
|
|
|
src = (const byte*)&val + val_sz;
|
|
|
|
} else {
|
|
|
|
src = (const byte*)&val + sizeof(mp_uint_t);
|
|
|
|
}
|
|
|
|
while (val_sz--) {
|
|
|
|
*dest++ = *--src;
|
|
|
|
}
|
2014-07-05 21:43:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-19 01:13:15 +01:00
|
|
|
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
|
|
|
|
byte *p = *ptr;
|
2014-08-30 14:59:21 +01:00
|
|
|
mp_uint_t align;
|
2014-04-19 01:13:15 +01:00
|
|
|
|
|
|
|
int size = mp_binary_get_size(struct_type, val_type, &align);
|
|
|
|
if (struct_type == '@') {
|
|
|
|
// Make pointer aligned
|
2014-07-03 13:25:24 +01:00
|
|
|
p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
|
2014-10-06 16:05:35 +01:00
|
|
|
if (MP_ENDIANNESS_LITTLE) {
|
|
|
|
struct_type = '<';
|
|
|
|
} else {
|
|
|
|
struct_type = '>';
|
|
|
|
}
|
2014-04-19 01:13:15 +01:00
|
|
|
}
|
2014-06-25 20:25:53 +01:00
|
|
|
*ptr = p + size;
|
2014-04-19 01:13:15 +01:00
|
|
|
|
2014-10-06 16:05:35 +01:00
|
|
|
mp_uint_t val;
|
2014-04-20 03:19:10 +01:00
|
|
|
switch (val_type) {
|
|
|
|
case 'O':
|
2014-10-06 16:05:35 +01:00
|
|
|
val = (mp_uint_t)val_in;
|
2014-04-20 03:19:10 +01:00
|
|
|
break;
|
2015-01-25 02:50:34 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
case 'f': {
|
|
|
|
union { uint32_t i; float f; } fp_sp;
|
|
|
|
fp_sp.f = mp_obj_get_float(val_in);
|
|
|
|
val = fp_sp.i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'd': {
|
|
|
|
union { uint64_t i64; uint32_t i32[2]; double f; } fp_dp;
|
|
|
|
fp_dp.f = mp_obj_get_float(val_in);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-20 03:19:10 +01:00
|
|
|
default:
|
2014-12-05 23:13:52 +00:00
|
|
|
// we handle large ints here by calling the truncated accessor
|
|
|
|
if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
|
|
|
|
val = mp_obj_int_get_truncated(val_in);
|
|
|
|
} else {
|
|
|
|
val = mp_obj_get_int(val_in);
|
|
|
|
}
|
2014-04-20 03:19:10 +01:00
|
|
|
}
|
|
|
|
|
2015-01-16 17:47:07 +00:00
|
|
|
mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val);
|
2014-04-19 01:13:15 +01:00
|
|
|
}
|
|
|
|
|
2014-10-03 18:44:14 +01:00
|
|
|
void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
|
2014-04-18 23:28:12 +01:00
|
|
|
switch (typecode) {
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-18 23:28:12 +01:00
|
|
|
case 'f':
|
2014-11-21 18:16:25 +00:00
|
|
|
((float*)p)[index] = mp_obj_get_float(val_in);
|
2014-04-18 23:28:12 +01:00
|
|
|
break;
|
|
|
|
case 'd':
|
2014-11-21 18:16:25 +00:00
|
|
|
((double*)p)[index] = mp_obj_get_float(val_in);
|
2014-04-18 23:28:12 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
|
2014-02-14 15:16:35 +00:00
|
|
|
}
|
2014-04-18 23:28:12 +01:00
|
|
|
}
|
2014-02-14 15:16:35 +00:00
|
|
|
|
2014-10-03 18:44:14 +01:00
|
|
|
void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val) {
|
2014-02-14 15:16:35 +00:00
|
|
|
switch (typecode) {
|
|
|
|
case 'b':
|
2015-01-24 01:18:10 +00:00
|
|
|
((signed char*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case BYTEARRAY_TYPECODE:
|
|
|
|
case 'B':
|
2015-01-24 01:18:10 +00:00
|
|
|
((unsigned char*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
2015-01-24 01:18:10 +00:00
|
|
|
((short*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'H':
|
2015-01-24 01:18:10 +00:00
|
|
|
((unsigned short*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'i':
|
2015-01-24 01:18:10 +00:00
|
|
|
((int*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'I':
|
2015-01-24 01:18:10 +00:00
|
|
|
((unsigned int*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
((long*)p)[index] = val;
|
|
|
|
break;
|
2014-02-14 15:16:35 +00:00
|
|
|
case 'L':
|
2015-01-24 01:18:10 +00:00
|
|
|
((unsigned long*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
assert(0);
|
|
|
|
((long long*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
#endif
|
2014-06-01 13:32:54 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-02-14 15:16:35 +00:00
|
|
|
case 'f':
|
2014-04-18 23:28:12 +01:00
|
|
|
((float*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2014-04-18 23:28:12 +01:00
|
|
|
((double*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|