Skip to content

Commit c38f615

Browse files
author
Bernhard Boser
committed
output little endian; update example
1 parent b7f46c4 commit c38f615

File tree

2 files changed

+106
-51
lines changed

2 files changed

+106
-51
lines changed

shared-bindings/msgpack/__init__.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
//|
3535
//| Example:
3636
//| import msgpack
37-
//| from io import StringIO
37+
//| from io import BytesIO
3838
//|
39-
//| s = StringIO()
40-
//| msgpack.pack({'list': [True, False, None, 1, 'abc'], 'str': 'blah'}, s)
41-
//| s.seek(0)
42-
//| print(msgpack.unpack(s))"""
39+
//| b = BytesIO()
40+
//| msgpack.pack({'list': [True, False, None, 1, 'abc'], 'str': 'blah'}, b)
41+
//| b.seek(0)
42+
//| print(msgpack.unpack(b))"""
4343
//|
4444

4545
//| def pack(obj: Any, buffer: WriteableBuffer) -> None:

shared-module/msgpack/__init__.c

Lines changed: 101 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <stdio.h>
28+
#include <inttypes.h>
2829

2930
#include "py/binary.h"
3031
#include "py/objarray.h"
@@ -66,13 +67,37 @@ STATIC void read(msgpack_stream_t *s, void *buf, mp_uint_t size) {
6667
}
6768
}
6869

70+
/*
6971
STATIC uint32_t read_bytes(msgpack_stream_t *s, mp_uint_t n_bytes) {
7072
uint32_t res = 0;
7173
read(s, &res, n_bytes);
7274
return res;
7375
}
76+
*/
77+
STATIC uint8_t read1(msgpack_stream_t *s) {
78+
uint8_t res = 0;
79+
read(s, &res, 1);
80+
return res;
81+
}
82+
83+
STATIC uint16_t read2(msgpack_stream_t *s) {
84+
uint16_t res = 0;
85+
read(s, &res, 2);
86+
int n = 1;
87+
if (*(char *)&n == 1) res = __builtin_bswap16(res);
88+
return res;
89+
}
90+
91+
STATIC uint32_t read4(msgpack_stream_t *s) {
92+
uint32_t res = 0;
93+
read(s, &res, 4);
94+
int n = 1;
95+
if (*(char *)&n == 1) res = __builtin_bswap32(res);
96+
return res;
97+
}
7498

75-
size_t read_size(msgpack_stream_t *s, uint8_t len_index) {
99+
/*
100+
STATIC size_t read_size(msgpack_stream_t *s, uint8_t len_index) {
76101
size_t n_bytes = 4;
77102
switch (len_index) {
78103
case 0: n_bytes = 1; break;
@@ -83,6 +108,17 @@ size_t read_size(msgpack_stream_t *s, uint8_t len_index) {
83108
read(s, &res, n_bytes);
84109
return res;
85110
}
111+
*/
112+
113+
STATIC size_t read_size(msgpack_stream_t *s, uint8_t len_index) {
114+
size_t res;
115+
switch (len_index) {
116+
case 0: res = (size_t)read1(s); break;
117+
case 1: res = (size_t)read2(s); break;
118+
case 2: res = (size_t)read4(s); break;
119+
}
120+
return res;
121+
}
86122

87123
////////////////////////////////////////////////////////////////
88124
// writers
@@ -97,20 +133,33 @@ STATIC void write(msgpack_stream_t *s, const void *buf, mp_uint_t size) {
97133
}
98134
}
99135

100-
STATIC void write_bytes(msgpack_stream_t *s, mp_uint_t n_bytes, uint32_t obj) {
101-
write(s, &obj, n_bytes);
136+
STATIC void write1(msgpack_stream_t *s, uint8_t obj) {
137+
write(s, &obj, 1);
138+
}
139+
140+
STATIC void write2(msgpack_stream_t *s, uint16_t obj) {
141+
int n = 1;
142+
if (*(char *)&n == 1) obj = __builtin_bswap16(obj);
143+
write(s, &obj, 2);
144+
}
145+
146+
STATIC void write4(msgpack_stream_t *s, uint32_t obj) {
147+
int n = 1;
148+
if (*(char *)&n == 1) obj = __builtin_bswap32(obj);
149+
write(s, &obj, 4);
102150
}
103151

104-
void write_size(msgpack_stream_t *s, uint8_t code, size_t size) {
152+
// compute and write msgpack size code (array structures)
153+
STATIC void write_size(msgpack_stream_t *s, uint8_t code, size_t size) {
105154
if ((uint8_t)size == size) {
106-
write_bytes(s, 1, code);
107-
write_bytes(s, 1, size);
155+
write1(s, code);
156+
write1(s, size);
108157
} else if ((uint16_t)size == size) {
109-
write_bytes(s, 1, code+1);
110-
write_bytes(s, 2, size);
158+
write1(s, code+1);
159+
write2(s, size);
111160
} else {
112-
write_bytes(s, 1, code+2);
113-
write_bytes(s, 4, size);
161+
write1(s, code+2);
162+
write4(s, size);
114163
}
115164
}
116165

@@ -136,61 +185,61 @@ STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
136185

137186
STATIC void pack_int(msgpack_stream_t *s, int32_t x) {
138187
if (x > -32 && x < 128) {
139-
write_bytes(s, 1, x);
188+
write1(s, x);
140189
} else if ((int8_t)x == x) {
141-
write_bytes(s, 1, 0xd0);
142-
write_bytes(s, 1, x);
190+
write1(s, 0xd0);
191+
write1(s, x);
143192
} else if ((int16_t)x == x) {
144-
write_bytes(s, 1, 0xd1);
145-
write_bytes(s, 2, x);
193+
write1(s, 0xd1);
194+
write2(s, x);
146195
} else {
147-
write_bytes(s, 1, 0xd2);
148-
write_bytes(s, 4, x);
196+
write1(s, 0xd2);
197+
write4(s, x);
149198
}
150199
}
151200

152201
void pack_bin(msgpack_stream_t *s, const uint8_t* data, size_t len) {
153202
write_size(s, 0xc4, len);
154203
for (size_t i=0; i<len; i++) {
155-
write_bytes(s, 1, data[i]);
204+
write1(s, data[i]);
156205
}
157206
}
158207

159208
void pack_str(msgpack_stream_t *s, const char* str, size_t len) {
160209
// size_t len = strlen(str);
161210
if (len < 32) {
162-
write_bytes(s, 1, 0b10100000 | (uint8_t)len);
211+
write1(s, 0b10100000 | (uint8_t)len);
163212
} else {
164213
write_size(s, 0xd9, len);
165214
}
166215
for (size_t l=0; l<len; l++) {
167-
write_bytes(s, 1, str[l]);
216+
write1(s, str[l]);
168217
}
169218
}
170219

171220
void pack_array(msgpack_stream_t *s, size_t len) {
172221
// only writes the header, manually write the objects after calling pack_array!
173222
if (len < 16) {
174-
write_bytes(s, 1, 0b10010000 | (uint8_t)len);
223+
write1(s, 0b10010000 | (uint8_t)len);
175224
} else if (len < 0x10000) {
176-
write_bytes(s, 1, 0xdc);
177-
write_bytes(s, 2, len);
225+
write1(s, 0xdc);
226+
write2(s, len);
178227
} else {
179-
write_bytes(s, 1, 0xdd);
180-
write_bytes(s, 4, len);
228+
write1(s, 0xdd);
229+
write4(s, len);
181230
}
182231
}
183232

184233
void pack_dict(msgpack_stream_t *s, size_t len) {
185234
// only writes the header, manually write the objects after calling pack_array!
186235
if (len < 16) {
187-
write_bytes(s, 1, 0b10000000 | (uint8_t)len);
236+
write1(s, 0b10000000 | (uint8_t)len);
188237
} else if (len < 0x10000) {
189-
write_bytes(s, 1, 0xde);
190-
write_bytes(s, 2, len);
238+
write1(s, 0xde);
239+
write2(s, len);
191240
} else {
192-
write_bytes(s, 1, 0xdf);
193-
write_bytes(s, 4, len);
241+
write1(s, 0xdf);
242+
write4(s, len);
194243
}
195244
}
196245

@@ -234,15 +283,17 @@ void pack(mp_obj_t obj, msgpack_stream_t *s) {
234283
pack(next->value, s);
235284
}
236285
} else if (mp_obj_is_float(obj)) {
237-
mp_float_t f = mp_obj_float_get(obj);
238-
write_bytes(s, 1, 0xca);
239-
write(s, &f, 4);
286+
union Float { mp_float_t f; uint32_t u; };
287+
union Float data;
288+
data.f = mp_obj_float_get(obj);
289+
write1(s, 0xca);
290+
write4(s, data.u);
240291
} else if (obj == mp_const_none) {
241-
write_bytes(s, 1, 0xc0);
292+
write1(s, 0xc0);
242293
} else if (obj == mp_const_false) {
243-
write_bytes(s, 1, 0xc2);
294+
write1(s, 0xc2);
244295
} else if (obj == mp_const_true) {
245-
write_bytes(s, 1, 0xc3);
296+
write1(s, 0xc3);
246297
} else {
247298
mp_raise_ValueError(translate("no packer"));
248299
}
@@ -252,7 +303,7 @@ void pack(mp_obj_t obj, msgpack_stream_t *s) {
252303
// unpacker
253304

254305
mp_obj_t unpack(msgpack_stream_t *s) {
255-
uint8_t code = read_bytes(s, 1);
306+
uint8_t code = read1(s);
256307
if (((code & 0b10000000) == 0) || ((code & 0b11100000) == 0b11100000)) {
257308
// int
258309
return MP_OBJ_NEW_SMALL_INT((int8_t)code);
@@ -298,17 +349,24 @@ mp_obj_t unpack(msgpack_stream_t *s) {
298349
read(s, p, size);
299350
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
300351
}
352+
case 0xcc:
353+
return MP_OBJ_NEW_SMALL_INT(read1(s));
354+
case 0xcd:
355+
return MP_OBJ_NEW_SMALL_INT(read2(s));
356+
case 0xce:
357+
return MP_OBJ_NEW_SMALL_INT(read4(s));
301358
case 0xca: {
302-
float f;
303-
read(s, &f, 4);
304-
return mp_obj_new_float(f);
359+
union Float { mp_float_t f; uint32_t u; };
360+
union Float data;
361+
data.u = read4(s);
362+
return mp_obj_new_float(data.f);
305363
}
306364
case 0xd0:
307-
return MP_OBJ_NEW_SMALL_INT((int8_t)read_bytes(s, 1));
365+
return MP_OBJ_NEW_SMALL_INT((int8_t)read1(s));
308366
case 0xd1:
309-
return MP_OBJ_NEW_SMALL_INT((int16_t)read_bytes(s, 2));
367+
return MP_OBJ_NEW_SMALL_INT((int16_t)read2(s));
310368
case 0xd2:
311-
return MP_OBJ_NEW_SMALL_INT((int32_t)read_bytes(s, 4));
369+
return MP_OBJ_NEW_SMALL_INT((int32_t)read4(s));
312370
case 0xd9:
313371
case 0xda:
314372
case 0xdb: {
@@ -345,9 +403,6 @@ mp_obj_t unpack(msgpack_stream_t *s) {
345403
case 0xc8: // ext 16
346404
case 0xc9: // ext 32
347405
case 0xcb: // float 64
348-
case 0xcc: // uint 8
349-
case 0xcd: // uint 16
350-
case 0xce: // uint 32
351406
case 0xcf: // uint 64
352407
case 0xd3: // int 64
353408
case 0xd4: // fixenxt 1

0 commit comments

Comments
 (0)