1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2014-2017 Paul Sokolovsky |
7 | * Copyright (c) 2014-2019 Damien P. George |
8 | * |
9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | * of this software and associated documentation files (the "Software"), to deal |
11 | * in the Software without restriction, including without limitation the rights |
12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | * copies of the Software, and to permit persons to whom the Software is |
14 | * furnished to do so, subject to the following conditions: |
15 | * |
16 | * The above copyright notice and this permission notice shall be included in |
17 | * all copies or substantial portions of the Software. |
18 | * |
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | * THE SOFTWARE. |
26 | */ |
27 | |
28 | #include <stdint.h> |
29 | #include <stdlib.h> |
30 | #include <stddef.h> |
31 | #include <string.h> |
32 | #include <assert.h> |
33 | |
34 | #include "py/binary.h" |
35 | #include "py/smallint.h" |
36 | #include "py/objint.h" |
37 | #include "py/runtime.h" |
38 | |
39 | // Helpers to work with binary-encoded data |
40 | |
41 | #ifndef alignof |
42 | #define alignof(type) offsetof(struct { char c; type t; }, t) |
43 | #endif |
44 | |
45 | size_t mp_binary_get_size(char struct_type, char val_type, size_t *palign) { |
46 | size_t size = 0; |
47 | int align = 1; |
48 | switch (struct_type) { |
49 | case '<': |
50 | case '>': |
51 | switch (val_type) { |
52 | case 'b': |
53 | case 'B': |
54 | size = 1; |
55 | break; |
56 | case 'h': |
57 | case 'H': |
58 | size = 2; |
59 | break; |
60 | case 'i': |
61 | case 'I': |
62 | size = 4; |
63 | break; |
64 | case 'l': |
65 | case 'L': |
66 | size = 4; |
67 | break; |
68 | case 'q': |
69 | case 'Q': |
70 | size = 8; |
71 | break; |
72 | case 'P': |
73 | case 'O': |
74 | case 'S': |
75 | size = sizeof(void *); |
76 | break; |
77 | case 'f': |
78 | size = sizeof(float); |
79 | break; |
80 | case 'd': |
81 | size = sizeof(double); |
82 | break; |
83 | } |
84 | break; |
85 | case '@': { |
86 | // TODO: |
87 | // The simplest heuristic for alignment is to align by value |
88 | // size, but that doesn't work for "bigger than int" types, |
89 | // for example, long long may very well have long alignment |
90 | // So, we introduce separate alignment handling, but having |
91 | // formal support for that is different from actually supporting |
92 | // particular (or any) ABI. |
93 | switch (val_type) { |
94 | case BYTEARRAY_TYPECODE: |
95 | case 'b': |
96 | case 'B': |
97 | align = size = 1; |
98 | break; |
99 | case 'h': |
100 | case 'H': |
101 | align = alignof(short); |
102 | size = sizeof(short); |
103 | break; |
104 | case 'i': |
105 | case 'I': |
106 | align = alignof(int); |
107 | size = sizeof(int); |
108 | break; |
109 | case 'l': |
110 | case 'L': |
111 | align = alignof(long); |
112 | size = sizeof(long); |
113 | break; |
114 | case 'q': |
115 | case 'Q': |
116 | align = alignof(long long); |
117 | size = sizeof(long long); |
118 | break; |
119 | case 'P': |
120 | case 'O': |
121 | case 'S': |
122 | align = alignof(void *); |
123 | size = sizeof(void *); |
124 | break; |
125 | case 'f': |
126 | align = alignof(float); |
127 | size = sizeof(float); |
128 | break; |
129 | case 'd': |
130 | align = alignof(double); |
131 | size = sizeof(double); |
132 | break; |
133 | } |
134 | } |
135 | } |
136 | |
137 | if (size == 0) { |
138 | mp_raise_ValueError(MP_ERROR_TEXT("bad typecode" )); |
139 | } |
140 | |
141 | if (palign != NULL) { |
142 | *palign = align; |
143 | } |
144 | return size; |
145 | } |
146 | |
147 | mp_obj_t mp_binary_get_val_array(char typecode, void *p, size_t index) { |
148 | mp_int_t val = 0; |
149 | switch (typecode) { |
150 | case 'b': |
151 | val = ((signed char *)p)[index]; |
152 | break; |
153 | case BYTEARRAY_TYPECODE: |
154 | case 'B': |
155 | val = ((unsigned char *)p)[index]; |
156 | break; |
157 | case 'h': |
158 | val = ((short *)p)[index]; |
159 | break; |
160 | case 'H': |
161 | val = ((unsigned short *)p)[index]; |
162 | break; |
163 | case 'i': |
164 | return mp_obj_new_int(((int *)p)[index]); |
165 | case 'I': |
166 | return mp_obj_new_int_from_uint(((unsigned int *)p)[index]); |
167 | case 'l': |
168 | return mp_obj_new_int(((long *)p)[index]); |
169 | case 'L': |
170 | return mp_obj_new_int_from_uint(((unsigned long *)p)[index]); |
171 | #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
172 | case 'q': |
173 | return mp_obj_new_int_from_ll(((long long *)p)[index]); |
174 | case 'Q': |
175 | return mp_obj_new_int_from_ull(((unsigned long long *)p)[index]); |
176 | #endif |
177 | #if MICROPY_PY_BUILTINS_FLOAT |
178 | case 'f': |
179 | return mp_obj_new_float_from_f(((float *)p)[index]); |
180 | case 'd': |
181 | return mp_obj_new_float_from_d(((double *)p)[index]); |
182 | #endif |
183 | // Extension to CPython: array of objects |
184 | case 'O': |
185 | return ((mp_obj_t *)p)[index]; |
186 | // Extension to CPython: array of pointers |
187 | case 'P': |
188 | return mp_obj_new_int((mp_int_t)(uintptr_t)((void **)p)[index]); |
189 | } |
190 | return MP_OBJ_NEW_SMALL_INT(val); |
191 | } |
192 | |
193 | // The long long type is guaranteed to hold at least 64 bits, and size is at |
194 | // most 8 (for q and Q), so we will always be able to parse the given data |
195 | // and fit it into a long long. |
196 | long long mp_binary_get_int(size_t size, bool is_signed, bool big_endian, const byte *src) { |
197 | int delta; |
198 | if (!big_endian) { |
199 | delta = -1; |
200 | src += size - 1; |
201 | } else { |
202 | delta = 1; |
203 | } |
204 | |
205 | long long val = 0; |
206 | if (is_signed && *src & 0x80) { |
207 | val = -1; |
208 | } |
209 | for (uint i = 0; i < size; i++) { |
210 | val <<= 8; |
211 | val |= *src; |
212 | src += delta; |
213 | } |
214 | |
215 | return val; |
216 | } |
217 | |
218 | #define is_signed(typecode) (typecode > 'Z') |
219 | mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte *p_base, byte **ptr) { |
220 | byte *p = *ptr; |
221 | size_t align; |
222 | |
223 | size_t size = mp_binary_get_size(struct_type, val_type, &align); |
224 | if (struct_type == '@') { |
225 | // Align p relative to p_base |
226 | p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align); |
227 | #if MP_ENDIANNESS_LITTLE |
228 | struct_type = '<'; |
229 | #else |
230 | struct_type = '>'; |
231 | #endif |
232 | } |
233 | *ptr = p + size; |
234 | |
235 | long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p); |
236 | |
237 | if (val_type == 'O') { |
238 | return (mp_obj_t)(mp_uint_t)val; |
239 | } else if (val_type == 'S') { |
240 | const char *s_val = (const char *)(uintptr_t)(mp_uint_t)val; |
241 | return mp_obj_new_str(s_val, strlen(s_val)); |
242 | #if MICROPY_PY_BUILTINS_FLOAT |
243 | } else if (val_type == 'f') { |
244 | union { uint32_t i; |
245 | float f; |
246 | } fpu = {val}; |
247 | return mp_obj_new_float_from_f(fpu.f); |
248 | } else if (val_type == 'd') { |
249 | union { uint64_t i; |
250 | double f; |
251 | } fpu = {val}; |
252 | return mp_obj_new_float_from_d(fpu.f); |
253 | #endif |
254 | } else if (is_signed(val_type)) { |
255 | if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) { |
256 | return mp_obj_new_int((mp_int_t)val); |
257 | } else { |
258 | return mp_obj_new_int_from_ll(val); |
259 | } |
260 | } else { |
261 | if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) { |
262 | return mp_obj_new_int_from_uint((mp_uint_t)val); |
263 | } else { |
264 | return mp_obj_new_int_from_ull(val); |
265 | } |
266 | } |
267 | } |
268 | |
269 | void mp_binary_set_int(size_t val_sz, bool big_endian, byte *dest, mp_uint_t val) { |
270 | if (MP_ENDIANNESS_LITTLE && !big_endian) { |
271 | memcpy(dest, &val, val_sz); |
272 | } else if (MP_ENDIANNESS_BIG && big_endian) { |
273 | // only copy the least-significant val_sz bytes |
274 | memcpy(dest, (byte *)&val + sizeof(mp_uint_t) - val_sz, val_sz); |
275 | } else { |
276 | const byte *src; |
277 | if (MP_ENDIANNESS_LITTLE) { |
278 | src = (const byte *)&val + val_sz; |
279 | } else { |
280 | src = (const byte *)&val + sizeof(mp_uint_t); |
281 | } |
282 | while (val_sz--) { |
283 | *dest++ = *--src; |
284 | } |
285 | } |
286 | } |
287 | |
288 | void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte *p_base, byte **ptr) { |
289 | byte *p = *ptr; |
290 | size_t align; |
291 | |
292 | size_t size = mp_binary_get_size(struct_type, val_type, &align); |
293 | if (struct_type == '@') { |
294 | // Align p relative to p_base |
295 | p = p_base + (uintptr_t)MP_ALIGN(p - p_base, align); |
296 | if (MP_ENDIANNESS_LITTLE) { |
297 | struct_type = '<'; |
298 | } else { |
299 | struct_type = '>'; |
300 | } |
301 | } |
302 | *ptr = p + size; |
303 | |
304 | mp_uint_t val; |
305 | switch (val_type) { |
306 | case 'O': |
307 | val = (mp_uint_t)val_in; |
308 | break; |
309 | #if MICROPY_PY_BUILTINS_FLOAT |
310 | case 'f': { |
311 | union { uint32_t i; |
312 | float f; |
313 | } fp_sp; |
314 | fp_sp.f = mp_obj_get_float_to_f(val_in); |
315 | val = fp_sp.i; |
316 | break; |
317 | } |
318 | case 'd': { |
319 | union { uint64_t i64; |
320 | uint32_t i32[2]; |
321 | double f; |
322 | } fp_dp; |
323 | fp_dp.f = mp_obj_get_float_to_d(val_in); |
324 | if (MP_BYTES_PER_OBJ_WORD == 8) { |
325 | val = fp_dp.i64; |
326 | } else { |
327 | int be = struct_type == '>'; |
328 | mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]); |
329 | p += sizeof(uint32_t); |
330 | val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be]; |
331 | } |
332 | break; |
333 | } |
334 | #endif |
335 | default: |
336 | #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
337 | if (mp_obj_is_type(val_in, &mp_type_int)) { |
338 | mp_obj_int_to_bytes_impl(val_in, struct_type == '>', size, p); |
339 | return; |
340 | } |
341 | #endif |
342 | |
343 | val = mp_obj_get_int(val_in); |
344 | // zero/sign extend if needed |
345 | if (MP_BYTES_PER_OBJ_WORD < 8 && size > sizeof(val)) { |
346 | int c = (mp_int_t)val < 0 ? 0xff : 0x00; |
347 | memset(p, c, size); |
348 | if (struct_type == '>') { |
349 | p += size - sizeof(val); |
350 | } |
351 | } |
352 | break; |
353 | } |
354 | |
355 | mp_binary_set_int(MIN((size_t)size, sizeof(val)), struct_type == '>', p, val); |
356 | } |
357 | |
358 | void mp_binary_set_val_array(char typecode, void *p, size_t index, mp_obj_t val_in) { |
359 | switch (typecode) { |
360 | #if MICROPY_PY_BUILTINS_FLOAT |
361 | case 'f': |
362 | ((float *)p)[index] = mp_obj_get_float_to_f(val_in); |
363 | break; |
364 | case 'd': |
365 | ((double *)p)[index] = mp_obj_get_float_to_d(val_in); |
366 | break; |
367 | #endif |
368 | // Extension to CPython: array of objects |
369 | case 'O': |
370 | ((mp_obj_t *)p)[index] = val_in; |
371 | break; |
372 | default: |
373 | #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
374 | if (mp_obj_is_type(val_in, &mp_type_int)) { |
375 | size_t size = mp_binary_get_size('@', typecode, NULL); |
376 | mp_obj_int_to_bytes_impl(val_in, MP_ENDIANNESS_BIG, |
377 | size, (uint8_t *)p + index * size); |
378 | return; |
379 | } |
380 | #endif |
381 | mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in)); |
382 | } |
383 | } |
384 | |
385 | void mp_binary_set_val_array_from_int(char typecode, void *p, size_t index, mp_int_t val) { |
386 | switch (typecode) { |
387 | case 'b': |
388 | ((signed char *)p)[index] = val; |
389 | break; |
390 | case BYTEARRAY_TYPECODE: |
391 | case 'B': |
392 | ((unsigned char *)p)[index] = val; |
393 | break; |
394 | case 'h': |
395 | ((short *)p)[index] = val; |
396 | break; |
397 | case 'H': |
398 | ((unsigned short *)p)[index] = val; |
399 | break; |
400 | case 'i': |
401 | ((int *)p)[index] = val; |
402 | break; |
403 | case 'I': |
404 | ((unsigned int *)p)[index] = val; |
405 | break; |
406 | case 'l': |
407 | ((long *)p)[index] = val; |
408 | break; |
409 | case 'L': |
410 | ((unsigned long *)p)[index] = val; |
411 | break; |
412 | #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE |
413 | case 'q': |
414 | ((long long *)p)[index] = val; |
415 | break; |
416 | case 'Q': |
417 | ((unsigned long long *)p)[index] = val; |
418 | break; |
419 | #endif |
420 | #if MICROPY_PY_BUILTINS_FLOAT |
421 | case 'f': |
422 | ((float *)p)[index] = (float)val; |
423 | break; |
424 | case 'd': |
425 | ((double *)p)[index] = (double)val; |
426 | break; |
427 | #endif |
428 | // Extension to CPython: array of pointers |
429 | case 'P': |
430 | ((void **)p)[index] = (void *)(uintptr_t)val; |
431 | break; |
432 | } |
433 | } |
434 | |