summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c115
1 files changed, 71 insertions, 44 deletions
diff --git a/py/binary.c b/py/binary.c
index 95e82a688d..1ddf4569b9 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -39,7 +39,7 @@ int mp_binary_get_size(char typecode) {
return -1;
}
-mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
+mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
machine_int_t val = 0;
switch (typecode) {
case 'b':
@@ -77,53 +77,80 @@ mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
return MP_OBJ_NEW_SMALL_INT(val);
}
-mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr) {
- machine_int_t val = 0;
+#define is_signed(typecode) (typecode > 'Z')
+mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
byte *p = *ptr;
- switch (typecode) {
- case 'b':
- val = (int8_t)*p++;
- break;
- case BYTEARRAY_TYPECODE:
- case 'B':
- val = *p++;
- break;
- case 'h':
- val = (int16_t)((p[1] << 8) | p[0]);
- break;
- case 'H':
- val = (p[1] << 8) | p[0];
- break;
- case 'i':
- case 'l':
- val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
- *ptr = p + 4;
- return mp_obj_new_int(val);
- case 'I':
- case 'L':
- val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
- *ptr = p + 4;
- return mp_obj_new_int_from_uint(val);
-#if 0 //TODO
-#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
- case 'q':
- case 'Q':
- // TODO: Explode API more to cover signedness
- return mp_obj_new_int_from_ll(((long long*)p)[index]);
-#endif
-#if MICROPY_ENABLE_FLOAT
- case 'f':
- return mp_obj_new_float(((float*)p)[index]);
- case 'd':
- return mp_obj_new_float(((double*)p)[index]);
-#endif
-#endif
+ uint size = 0;
+ 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;
+ }
+ 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.
+ uint align = 0;
+ switch (val_type) {
+ case 'b': case 'B':
+ align = size = 1; break;
+ case 'h': case 'H':
+ align = size = sizeof(short); break;
+ case 'i': case 'I':
+ align = size = sizeof(int); break;
+ case 'l': case 'L':
+ align = size = sizeof(long); break;
+ }
+ // Make pointer aligned
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
+ #if MP_ENDIANNESS_LITTLE
+ struct_type = '<';
+ #else
+ struct_type = '>';
+ #endif
+ break;
+ }
+ }
+
+ int delta;
+ if (struct_type == '<') {
+ delta = -1;
+ p += size - 1;
+ } else {
+ delta = 1;
+ }
+
+ machine_int_t val = 0;
+ if (is_signed(val_type) && *p & 0x80) {
+ val = -1;
+ }
+ for (uint i = 0; i < size; i++) {
+ val <<= 8;
+ val |= *p;
+ p += delta;
+ }
+
+ *ptr += size;
+ if (is_signed(val_type)) {
+ return mp_obj_new_int(val);
+ } else {
+ return mp_obj_new_int_from_uint(val);
}
- *ptr = p;
- return MP_OBJ_NEW_SMALL_INT(val);
}
-void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
+void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
machine_int_t val = 0;
if (MP_OBJ_IS_INT(val_in)) {
val = mp_obj_int_get(val_in);