summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-19 03:13:15 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-19 03:22:08 +0300
commit6204460461cc21b27861fa89b2423119f8cdce88 (patch)
treea47370140e792aa9722bced667b0922e22122647 /py/binary.c
parent504e23388ce68fcb1ac80a7c411979718686af7c (diff)
downloadmicropython-6204460461cc21b27861fa89b2423119f8cdce88.tar.gz
micropython-6204460461cc21b27861fa89b2423119f8cdce88.zip
modstruct: Initial implementation of struct.pack().
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/py/binary.c b/py/binary.c
index 702a9cceb3..ee95d56e44 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -138,6 +138,45 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
}
+void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, 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
+ }
+
+#if MP_ENDIANNESS_BIG
+#error Not implemented
+#endif
+ machine_int_t val = mp_obj_int_get_checked(val_in);
+ byte *in = (byte*)&val;
+ int in_delta, out_delta;
+ uint val_sz = MIN(size, sizeof(val));
+ if (struct_type == '>') {
+ in_delta = -1;
+ out_delta = 1;
+ in += val_sz - 1;
+ } else {
+ in_delta = out_delta = 1;
+ }
+
+ for (uint i = val_sz; i > 0; i--) {
+ *p = *in;
+ p += out_delta;
+ in += in_delta;
+ }
+
+ *ptr += size;
+}
+
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
switch (typecode) {
#if MICROPY_ENABLE_FLOAT