blob: fed69f9d1b5bc2224b1460c9033a7c92407d498e [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020027#include <stdint.h>
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030028#include <stdlib.h>
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030029#include <stddef.h>
Paul Sokolovsky62798832014-06-02 16:04:26 +030030#include <string.h>
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020031#include <assert.h>
32
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020033#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030034#include "misc.h"
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020035#include "qstr.h"
36#include "obj.h"
Damien George95307432014-09-10 22:10:33 +010037#include "smallint.h"
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020038#include "binary.h"
39
40// Helpers to work with binary-encoded data
41
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030042#ifndef alignof
43#define alignof(type) offsetof(struct { char c; type t; }, t)
44#endif
45
Damien George4abff752014-08-30 14:59:21 +010046int mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030047 int size = 0;
48 int align = 1;
49 switch (struct_type) {
50 case '<': case '>':
51 switch (val_type) {
52 case 'b': case 'B':
53 size = 1; break;
54 case 'h': case 'H':
55 size = 2; break;
56 case 'i': case 'I':
57 size = 4; break;
58 case 'l': case 'L':
59 size = 4; break;
60 case 'q': case 'Q':
61 size = 8; break;
Paul Sokolovsky78fde482014-09-06 19:24:52 +030062 case 'P': case 'O': case 'S':
63 size = sizeof(void*); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030064 }
65 break;
66 case '@': {
67 // TODO:
68 // The simplest heuristic for alignment is to align by value
69 // size, but that doesn't work for "bigger than int" types,
70 // for example, long long may very well have long alignment
71 // So, we introduce separate alignment handling, but having
72 // formal support for that is different from actually supporting
73 // particular (or any) ABI.
74 switch (val_type) {
75 case BYTEARRAY_TYPECODE:
76 case 'b': case 'B':
77 align = size = 1; break;
78 case 'h': case 'H':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030079 align = alignof(short);
80 size = sizeof(short); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030081 case 'i': case 'I':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030082 align = alignof(int);
83 size = sizeof(int); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030084 case 'l': case 'L':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030085 align = alignof(long);
86 size = sizeof(long); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030087 case 'q': case 'Q':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030088 align = alignof(long long);
89 size = sizeof(long long); break;
Paul Sokolovsky62798832014-06-02 16:04:26 +030090 case 'P': case 'O': case 'S':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030091 align = alignof(void*);
92 size = sizeof(void*); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030093 }
94 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +020095 }
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030096 if (palign != NULL) {
97 *palign = align;
98 }
99 return size;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200100}
101
Damien George42f3de92014-10-03 17:44:14 +0000102mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
Damien George40f3c022014-07-03 13:25:24 +0100103 mp_int_t val = 0;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200104 switch (typecode) {
105 case 'b':
106 val = ((int8_t*)p)[index];
107 break;
108 case BYTEARRAY_TYPECODE:
109 case 'B':
110 val = ((uint8_t*)p)[index];
111 break;
112 case 'h':
113 val = ((int16_t*)p)[index];
114 break;
115 case 'H':
116 val = ((uint16_t*)p)[index];
117 break;
118 case 'i':
119 case 'l':
120 return mp_obj_new_int(((int32_t*)p)[index]);
121 case 'I':
122 case 'L':
123 return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
Damien George96056a62014-02-15 23:02:00 +0000124#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200125 case 'q':
126 case 'Q':
127 // TODO: Explode API more to cover signedness
128 return mp_obj_new_int_from_ll(((long long*)p)[index]);
129#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100130#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200131 case 'f':
132 return mp_obj_new_float(((float*)p)[index]);
133 case 'd':
134 return mp_obj_new_float(((double*)p)[index]);
135#endif
136 }
137 return MP_OBJ_NEW_SMALL_INT(val);
138}
139
Damien George95307432014-09-10 22:10:33 +0100140// The long long type is guaranteed to hold at least 64 bits, and size is at
141// most 8 (for q and Q), so we will always be able to parse the given data
142// and fit it into a long long.
Damien George9336ee32014-10-06 15:05:35 +0000143long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, const byte *src) {
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300144 int delta;
145 if (!big_endian) {
146 delta = -1;
Damien George9336ee32014-10-06 15:05:35 +0000147 src += size - 1;
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300148 } else {
149 delta = 1;
150 }
151
Damien George95307432014-09-10 22:10:33 +0100152 long long val = 0;
Damien George9336ee32014-10-06 15:05:35 +0000153 if (is_signed && *src & 0x80) {
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300154 val = -1;
155 }
156 for (uint i = 0; i < size; i++) {
157 val <<= 8;
Damien George9336ee32014-10-06 15:05:35 +0000158 val |= *src;
159 src += delta;
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300160 }
161
162 return val;
163}
164
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300165#define is_signed(typecode) (typecode > 'Z')
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +0300166mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300167 byte *p = *ptr;
Damien George4abff752014-08-30 14:59:21 +0100168 mp_uint_t align;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300169
170 int size = mp_binary_get_size(struct_type, val_type, &align);
171 if (struct_type == '@') {
172 // Make pointer aligned
Damien George40f3c022014-07-03 13:25:24 +0100173 p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300174 #if MP_ENDIANNESS_LITTLE
175 struct_type = '<';
176 #else
177 struct_type = '>';
178 #endif
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300179 }
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300180 *ptr = p + size;
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300181
Damien George95307432014-09-10 22:10:33 +0100182 long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300183
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300184 if (val_type == 'O') {
Damien George95307432014-09-10 22:10:33 +0100185 return (mp_obj_t)(mp_uint_t)val;
Paul Sokolovsky62798832014-06-02 16:04:26 +0300186 } else if (val_type == 'S') {
Damien George95307432014-09-10 22:10:33 +0100187 const char *s_val = (const char*)(mp_uint_t)val;
188 return mp_obj_new_str(s_val, strlen(s_val), false);
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300189 } else if (is_signed(val_type)) {
Damien George95307432014-09-10 22:10:33 +0100190 if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
191 return mp_obj_new_int((mp_int_t)val);
192 } else {
193 return mp_obj_new_int_from_ll(val);
194 }
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300195 } else {
Damien George95307432014-09-10 22:10:33 +0100196 if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
197 return mp_obj_new_int_from_uint((mp_uint_t)val);
198 } else {
199 return mp_obj_new_int_from_ull(val);
200 }
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300201 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300202}
203
Damien George9336ee32014-10-06 15:05:35 +0000204void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
205 if (MP_ENDIANNESS_LITTLE && !big_endian) {
206 memcpy(dest, &val, val_sz);
207 } else if (MP_ENDIANNESS_BIG && big_endian) {
208 // only copy the least-significant val_sz bytes
209 memcpy(dest, (byte*)&val + sizeof(mp_uint_t) - val_sz, val_sz);
Paul Sokolovsky5fa5ca42014-07-05 23:43:00 +0300210 } else {
Damien George9336ee32014-10-06 15:05:35 +0000211 const byte *src;
212 if (MP_ENDIANNESS_LITTLE) {
213 src = (const byte*)&val + val_sz;
214 } else {
215 src = (const byte*)&val + sizeof(mp_uint_t);
216 }
217 while (val_sz--) {
218 *dest++ = *--src;
219 }
Paul Sokolovsky5fa5ca42014-07-05 23:43:00 +0300220 }
221}
222
Paul Sokolovsky62044602014-04-19 03:13:15 +0300223void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
224 byte *p = *ptr;
Damien George4abff752014-08-30 14:59:21 +0100225 mp_uint_t align;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300226
227 int size = mp_binary_get_size(struct_type, val_type, &align);
228 if (struct_type == '@') {
229 // Make pointer aligned
Damien George40f3c022014-07-03 13:25:24 +0100230 p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
Damien George9336ee32014-10-06 15:05:35 +0000231 if (MP_ENDIANNESS_LITTLE) {
232 struct_type = '<';
233 } else {
234 struct_type = '>';
235 }
Paul Sokolovsky62044602014-04-19 03:13:15 +0300236 }
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300237 *ptr = p + size;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300238
Damien George9336ee32014-10-06 15:05:35 +0000239 mp_uint_t val;
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300240 switch (val_type) {
241 case 'O':
Damien George9336ee32014-10-06 15:05:35 +0000242 val = (mp_uint_t)val_in;
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300243 break;
244 default:
245 val = mp_obj_get_int(val_in);
246 }
247
Damien George9336ee32014-10-06 15:05:35 +0000248 mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, val);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300249}
250
Damien George42f3de92014-10-03 17:44:14 +0000251void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
Damien George71e9bfa2014-04-18 23:28:12 +0100252 switch (typecode) {
Damien Georgefb510b32014-06-01 13:32:54 +0100253#if MICROPY_PY_BUILTINS_FLOAT
Damien George71e9bfa2014-04-18 23:28:12 +0100254 case 'f':
255 ((float*)p)[index] = mp_obj_float_get(val_in);
256 break;
257 case 'd':
258 ((double*)p)[index] = mp_obj_float_get(val_in);
259 break;
260#endif
261 default:
262 mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200263 }
Damien George71e9bfa2014-04-18 23:28:12 +0100264}
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200265
Damien George42f3de92014-10-03 17:44:14 +0000266void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val) {
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200267 switch (typecode) {
268 case 'b':
269 ((int8_t*)p)[index] = val;
270 break;
271 case BYTEARRAY_TYPECODE:
272 case 'B':
273 val = ((uint8_t*)p)[index] = val;
274 break;
275 case 'h':
276 val = ((int16_t*)p)[index] = val;
277 break;
278 case 'H':
279 val = ((uint16_t*)p)[index] = val;
280 break;
281 case 'i':
282 case 'l':
283 ((int32_t*)p)[index] = val;
284 break;
285 case 'I':
286 case 'L':
287 ((uint32_t*)p)[index] = val;
288 break;
289#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
290 case 'q':
291 case 'Q':
292 assert(0);
293 ((long long*)p)[index] = val;
294 break;
295#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100296#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200297 case 'f':
Damien George71e9bfa2014-04-18 23:28:12 +0100298 ((float*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200299 break;
300 case 'd':
Damien George71e9bfa2014-04-18 23:28:12 +0100301 ((double*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200302 break;
303#endif
304 }
305}