blob: e4dac31e96f5e5a9cceafb520c2939a5009e5602 [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;
Damien Georgec9f8f652014-11-21 18:16:25 +000064 case 'f':
65 size = sizeof(float); break;
66 case 'd':
67 size = sizeof(double); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030068 }
69 break;
70 case '@': {
71 // TODO:
72 // The simplest heuristic for alignment is to align by value
73 // size, but that doesn't work for "bigger than int" types,
74 // for example, long long may very well have long alignment
75 // So, we introduce separate alignment handling, but having
76 // formal support for that is different from actually supporting
77 // particular (or any) ABI.
78 switch (val_type) {
79 case BYTEARRAY_TYPECODE:
80 case 'b': case 'B':
81 align = size = 1; break;
82 case 'h': case 'H':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030083 align = alignof(short);
84 size = sizeof(short); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030085 case 'i': case 'I':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030086 align = alignof(int);
87 size = sizeof(int); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030088 case 'l': case 'L':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030089 align = alignof(long);
90 size = sizeof(long); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030091 case 'q': case 'Q':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030092 align = alignof(long long);
93 size = sizeof(long long); break;
Paul Sokolovsky62798832014-06-02 16:04:26 +030094 case 'P': case 'O': case 'S':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030095 align = alignof(void*);
96 size = sizeof(void*); break;
Damien Georgec9f8f652014-11-21 18:16:25 +000097 case 'f':
98 align = alignof(float);
99 size = sizeof(float); break;
100 case 'd':
101 align = alignof(double);
102 size = sizeof(double); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300103 }
104 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200105 }
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300106 if (palign != NULL) {
107 *palign = align;
108 }
109 return size;
Paul Sokolovskyc2033242014-02-14 20:21:50 +0200110}
111
Damien George42f3de92014-10-03 17:44:14 +0000112mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
Damien George40f3c022014-07-03 13:25:24 +0100113 mp_int_t val = 0;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200114 switch (typecode) {
115 case 'b':
116 val = ((int8_t*)p)[index];
117 break;
118 case BYTEARRAY_TYPECODE:
119 case 'B':
120 val = ((uint8_t*)p)[index];
121 break;
122 case 'h':
123 val = ((int16_t*)p)[index];
124 break;
125 case 'H':
126 val = ((uint16_t*)p)[index];
127 break;
128 case 'i':
129 case 'l':
130 return mp_obj_new_int(((int32_t*)p)[index]);
131 case 'I':
132 case 'L':
133 return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
Damien George96056a62014-02-15 23:02:00 +0000134#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200135 case 'q':
136 case 'Q':
137 // TODO: Explode API more to cover signedness
138 return mp_obj_new_int_from_ll(((long long*)p)[index]);
139#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100140#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200141 case 'f':
142 return mp_obj_new_float(((float*)p)[index]);
143 case 'd':
144 return mp_obj_new_float(((double*)p)[index]);
145#endif
146 }
147 return MP_OBJ_NEW_SMALL_INT(val);
148}
149
Damien George95307432014-09-10 22:10:33 +0100150// The long long type is guaranteed to hold at least 64 bits, and size is at
151// most 8 (for q and Q), so we will always be able to parse the given data
152// and fit it into a long long.
Damien George9336ee32014-10-06 15:05:35 +0000153long 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 +0300154 int delta;
155 if (!big_endian) {
156 delta = -1;
Damien George9336ee32014-10-06 15:05:35 +0000157 src += size - 1;
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300158 } else {
159 delta = 1;
160 }
161
Damien George95307432014-09-10 22:10:33 +0100162 long long val = 0;
Damien George9336ee32014-10-06 15:05:35 +0000163 if (is_signed && *src & 0x80) {
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300164 val = -1;
165 }
166 for (uint i = 0; i < size; i++) {
167 val <<= 8;
Damien George9336ee32014-10-06 15:05:35 +0000168 val |= *src;
169 src += delta;
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300170 }
171
172 return val;
173}
174
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300175#define is_signed(typecode) (typecode > 'Z')
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +0300176mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300177 byte *p = *ptr;
Damien George4abff752014-08-30 14:59:21 +0100178 mp_uint_t align;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300179
180 int size = mp_binary_get_size(struct_type, val_type, &align);
181 if (struct_type == '@') {
182 // Make pointer aligned
Damien George40f3c022014-07-03 13:25:24 +0100183 p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300184 #if MP_ENDIANNESS_LITTLE
185 struct_type = '<';
186 #else
187 struct_type = '>';
188 #endif
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300189 }
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300190 *ptr = p + size;
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300191
Damien George95307432014-09-10 22:10:33 +0100192 long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300193
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300194 if (val_type == 'O') {
Damien George95307432014-09-10 22:10:33 +0100195 return (mp_obj_t)(mp_uint_t)val;
Paul Sokolovsky62798832014-06-02 16:04:26 +0300196 } else if (val_type == 'S') {
Damien George95307432014-09-10 22:10:33 +0100197 const char *s_val = (const char*)(mp_uint_t)val;
198 return mp_obj_new_str(s_val, strlen(s_val), false);
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300199 } else if (is_signed(val_type)) {
Damien George95307432014-09-10 22:10:33 +0100200 if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
201 return mp_obj_new_int((mp_int_t)val);
202 } else {
203 return mp_obj_new_int_from_ll(val);
204 }
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300205 } else {
Damien George95307432014-09-10 22:10:33 +0100206 if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) {
207 return mp_obj_new_int_from_uint((mp_uint_t)val);
208 } else {
209 return mp_obj_new_int_from_ull(val);
210 }
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300211 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300212}
213
Damien George9336ee32014-10-06 15:05:35 +0000214void mp_binary_set_int(mp_uint_t val_sz, bool big_endian, byte *dest, mp_uint_t val) {
215 if (MP_ENDIANNESS_LITTLE && !big_endian) {
216 memcpy(dest, &val, val_sz);
217 } else if (MP_ENDIANNESS_BIG && big_endian) {
218 // only copy the least-significant val_sz bytes
219 memcpy(dest, (byte*)&val + sizeof(mp_uint_t) - val_sz, val_sz);
Paul Sokolovsky5fa5ca42014-07-05 23:43:00 +0300220 } else {
Damien George9336ee32014-10-06 15:05:35 +0000221 const byte *src;
222 if (MP_ENDIANNESS_LITTLE) {
223 src = (const byte*)&val + val_sz;
224 } else {
225 src = (const byte*)&val + sizeof(mp_uint_t);
226 }
227 while (val_sz--) {
228 *dest++ = *--src;
229 }
Paul Sokolovsky5fa5ca42014-07-05 23:43:00 +0300230 }
231}
232
Paul Sokolovsky62044602014-04-19 03:13:15 +0300233void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
234 byte *p = *ptr;
Damien George4abff752014-08-30 14:59:21 +0100235 mp_uint_t align;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300236
237 int size = mp_binary_get_size(struct_type, val_type, &align);
238 if (struct_type == '@') {
239 // Make pointer aligned
Damien George40f3c022014-07-03 13:25:24 +0100240 p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
Damien George9336ee32014-10-06 15:05:35 +0000241 if (MP_ENDIANNESS_LITTLE) {
242 struct_type = '<';
243 } else {
244 struct_type = '>';
245 }
Paul Sokolovsky62044602014-04-19 03:13:15 +0300246 }
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300247 *ptr = p + size;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300248
Damien George9336ee32014-10-06 15:05:35 +0000249 mp_uint_t val;
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300250 switch (val_type) {
251 case 'O':
Damien George9336ee32014-10-06 15:05:35 +0000252 val = (mp_uint_t)val_in;
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300253 break;
254 default:
Damien Georgebe6d8be2014-12-05 23:13:52 +0000255 // we handle large ints here by calling the truncated accessor
256 if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
257 val = mp_obj_int_get_truncated(val_in);
258 } else {
259 val = mp_obj_get_int(val_in);
260 }
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300261 }
262
Damien George9336ee32014-10-06 15:05:35 +0000263 mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, val);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300264}
265
Damien George42f3de92014-10-03 17:44:14 +0000266void 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 +0100267 switch (typecode) {
Damien Georgefb510b32014-06-01 13:32:54 +0100268#if MICROPY_PY_BUILTINS_FLOAT
Damien George71e9bfa2014-04-18 23:28:12 +0100269 case 'f':
Damien Georgec9f8f652014-11-21 18:16:25 +0000270 ((float*)p)[index] = mp_obj_get_float(val_in);
Damien George71e9bfa2014-04-18 23:28:12 +0100271 break;
272 case 'd':
Damien Georgec9f8f652014-11-21 18:16:25 +0000273 ((double*)p)[index] = mp_obj_get_float(val_in);
Damien George71e9bfa2014-04-18 23:28:12 +0100274 break;
275#endif
276 default:
277 mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200278 }
Damien George71e9bfa2014-04-18 23:28:12 +0100279}
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200280
Damien George42f3de92014-10-03 17:44:14 +0000281void 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 +0200282 switch (typecode) {
283 case 'b':
284 ((int8_t*)p)[index] = val;
285 break;
286 case BYTEARRAY_TYPECODE:
287 case 'B':
288 val = ((uint8_t*)p)[index] = val;
289 break;
290 case 'h':
291 val = ((int16_t*)p)[index] = val;
292 break;
293 case 'H':
294 val = ((uint16_t*)p)[index] = val;
295 break;
296 case 'i':
297 case 'l':
298 ((int32_t*)p)[index] = val;
299 break;
300 case 'I':
301 case 'L':
302 ((uint32_t*)p)[index] = val;
303 break;
304#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
305 case 'q':
306 case 'Q':
307 assert(0);
308 ((long long*)p)[index] = val;
309 break;
310#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100311#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200312 case 'f':
Damien George71e9bfa2014-04-18 23:28:12 +0100313 ((float*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200314 break;
315 case 'd':
Damien George71e9bfa2014-04-18 23:28:12 +0100316 ((double*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200317 break;
318#endif
319 }
320}