aboutsummaryrefslogtreecommitdiff
path: root/py/binary.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-06-25 22:25:53 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-06-25 23:34:44 +0300
commit7a2f16694907fbe8bf5445e30d061f4bb1f9517a (patch)
tree4093e385115977e865b462826f92a7a3cf345244 /py/binary.c
parent5aa740c3e210e4f2f641db0bb2bef49eda31228f (diff)
modstruct: Fix alignment handling issues.
Also, factor out mp_binary_get_int() function.
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c44
1 files changed, 25 insertions, 19 deletions
diff --git a/py/binary.c b/py/binary.c
index 9eab01c4e..bc06f9b6f 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -125,6 +125,28 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
return MP_OBJ_NEW_SMALL_INT(val);
}
+machine_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p) {
+ int delta;
+ if (!big_endian) {
+ delta = -1;
+ p += size - 1;
+ } else {
+ delta = 1;
+ }
+
+ machine_int_t val = 0;
+ if (is_signed && *p & 0x80) {
+ val = -1;
+ }
+ for (uint i = 0; i < size; i++) {
+ val <<= 8;
+ val |= *p;
+ p += delta;
+ }
+
+ 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;
@@ -140,26 +162,10 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
struct_type = '>';
#endif
}
+ *ptr = p + size;
- 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;
- }
+ machine_int_t val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
- *ptr += size;
if (val_type == 'O') {
return (mp_obj_t)val;
} else if (val_type == 'S') {
@@ -185,6 +191,7 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
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) {