summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c50
1 files changed, 28 insertions, 22 deletions
diff --git a/py/binary.c b/py/binary.c
index 833d9c85ad..d755bc86e0 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -29,8 +29,8 @@
#include <string.h>
#include <assert.h>
-#include "misc.h"
#include "mpconfig.h"
+#include "misc.h"
#include "qstr.h"
#include "obj.h"
#include "binary.h"
@@ -125,24 +125,9 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
return MP_OBJ_NEW_SMALL_INT(val);
}
-#define is_signed(typecode) (typecode > 'Z')
-mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
- byte *p = *ptr;
- uint align;
-
- int size = mp_binary_get_size(struct_type, val_type, &align);
- if (struct_type == '@') {
- // Make pointer aligned
- p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
- #if MP_ENDIANNESS_LITTLE
- struct_type = '<';
- #else
- struct_type = '>';
- #endif
- }
-
+machine_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p) {
int delta;
- if (struct_type == '<') {
+ if (!big_endian) {
delta = -1;
p += size - 1;
} else {
@@ -150,7 +135,7 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
machine_int_t val = 0;
- if (is_signed(val_type) && *p & 0x80) {
+ if (is_signed && *p & 0x80) {
val = -1;
}
for (uint i = 0; i < size; i++) {
@@ -159,7 +144,28 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
p += delta;
}
- *ptr += size;
+ return val;
+}
+
+#define is_signed(typecode) (typecode > 'Z')
+mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
+ byte *p = *ptr;
+ uint align;
+
+ int size = mp_binary_get_size(struct_type, val_type, &align);
+ if (struct_type == '@') {
+ // Make pointer aligned
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~((machine_uint_t)align - 1));
+ #if MP_ENDIANNESS_LITTLE
+ struct_type = '<';
+ #else
+ struct_type = '>';
+ #endif
+ }
+ *ptr = p + size;
+
+ machine_int_t val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
+
if (val_type == 'O') {
return (mp_obj_t)val;
} else if (val_type == 'S') {
@@ -178,13 +184,14 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
int size = mp_binary_get_size(struct_type, val_type, &align);
if (struct_type == '@') {
// Make pointer aligned
- p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~((machine_uint_t)align - 1));
#if MP_ENDIANNESS_LITTLE
struct_type = '<';
#else
struct_type = '>';
#endif
}
+ *ptr = p + size;
#if MP_ENDIANNESS_BIG
#error Not implemented
@@ -215,7 +222,6 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
in += in_delta;
}
- *ptr += size;
}
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {