blob: 65688272aab4b9548fe304c5d00260742efe769a [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"
Paul Sokolovsky8bc35162014-02-14 17:16:35 +020037#include "binary.h"
38
39// Helpers to work with binary-encoded data
40
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030041#ifndef alignof
42#define alignof(type) offsetof(struct { char c; type t; }, t)
43#endif
44
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030045int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
46 int size = 0;
47 int align = 1;
48 switch (struct_type) {
49 case '<': case '>':
50 switch (val_type) {
51 case 'b': case 'B':
52 size = 1; break;
53 case 'h': case 'H':
54 size = 2; break;
55 case 'i': case 'I':
56 size = 4; break;
57 case 'l': case 'L':
58 size = 4; break;
59 case 'q': case 'Q':
60 size = 8; break;
61 }
62 break;
63 case '@': {
64 // TODO:
65 // The simplest heuristic for alignment is to align by value
66 // size, but that doesn't work for "bigger than int" types,
67 // for example, long long may very well have long alignment
68 // So, we introduce separate alignment handling, but having
69 // formal support for that is different from actually supporting
70 // particular (or any) ABI.
71 switch (val_type) {
72 case BYTEARRAY_TYPECODE:
73 case 'b': case 'B':
74 align = size = 1; break;
75 case 'h': case 'H':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030076 align = alignof(short);
77 size = sizeof(short); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030078 case 'i': case 'I':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030079 align = alignof(int);
80 size = sizeof(int); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030081 case 'l': case 'L':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030082 align = alignof(long);
83 size = sizeof(long); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030084 case 'q': case 'Q':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030085 align = alignof(long long);
86 size = sizeof(long long); break;
Paul Sokolovsky62798832014-06-02 16:04:26 +030087 case 'P': case 'O': case 'S':
Paul Sokolovskyecca53b2014-08-10 20:16:39 +030088 align = alignof(void*);
89 size = sizeof(void*); break;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030090 }
91 }
Paul Sokolovskyc2033242014-02-14 20:21:50 +020092 }
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030093 if (palign != NULL) {
94 *palign = align;
95 }
96 return size;
Paul Sokolovskyc2033242014-02-14 20:21:50 +020097}
98
Paul Sokolovskyef9124f2014-04-11 03:46:09 +030099mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
Damien George40f3c022014-07-03 13:25:24 +0100100 mp_int_t val = 0;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200101 switch (typecode) {
102 case 'b':
103 val = ((int8_t*)p)[index];
104 break;
105 case BYTEARRAY_TYPECODE:
106 case 'B':
107 val = ((uint8_t*)p)[index];
108 break;
109 case 'h':
110 val = ((int16_t*)p)[index];
111 break;
112 case 'H':
113 val = ((uint16_t*)p)[index];
114 break;
115 case 'i':
116 case 'l':
117 return mp_obj_new_int(((int32_t*)p)[index]);
118 case 'I':
119 case 'L':
120 return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
Damien George96056a62014-02-15 23:02:00 +0000121#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200122 case 'q':
123 case 'Q':
124 // TODO: Explode API more to cover signedness
125 return mp_obj_new_int_from_ll(((long long*)p)[index]);
126#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100127#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200128 case 'f':
129 return mp_obj_new_float(((float*)p)[index]);
130 case 'd':
131 return mp_obj_new_float(((double*)p)[index]);
132#endif
133 }
134 return MP_OBJ_NEW_SMALL_INT(val);
135}
136
Damien George40f3c022014-07-03 13:25:24 +0100137mp_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p) {
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300138 int delta;
139 if (!big_endian) {
140 delta = -1;
141 p += size - 1;
142 } else {
143 delta = 1;
144 }
145
Damien George40f3c022014-07-03 13:25:24 +0100146 mp_int_t val = 0;
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300147 if (is_signed && *p & 0x80) {
148 val = -1;
149 }
150 for (uint i = 0; i < size; i++) {
151 val <<= 8;
152 val |= *p;
153 p += delta;
154 }
155
156 return val;
157}
158
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300159#define is_signed(typecode) (typecode > 'Z')
Paul Sokolovsky0c43cf92014-04-11 03:47:21 +0300160mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300161 byte *p = *ptr;
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300162 uint align;
163
164 int size = mp_binary_get_size(struct_type, val_type, &align);
165 if (struct_type == '@') {
166 // Make pointer aligned
Damien George40f3c022014-07-03 13:25:24 +0100167 p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300168 #if MP_ENDIANNESS_LITTLE
169 struct_type = '<';
170 #else
171 struct_type = '>';
172 #endif
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300173 }
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300174 *ptr = p + size;
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300175
Damien George40f3c022014-07-03 13:25:24 +0100176 mp_int_t val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300177
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300178 if (val_type == 'O') {
179 return (mp_obj_t)val;
Paul Sokolovsky62798832014-06-02 16:04:26 +0300180 } else if (val_type == 'S') {
181 return mp_obj_new_str((char*)val, strlen((char*)val), false);
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300182 } else if (is_signed(val_type)) {
Paul Sokolovsky6582d642014-04-10 22:19:32 +0300183 return mp_obj_new_int(val);
184 } else {
185 return mp_obj_new_int_from_uint(val);
186 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300187}
188
Paul Sokolovsky5fa5ca42014-07-05 23:43:00 +0300189void mp_binary_set_int(uint val_sz, bool big_endian, byte *p, byte *val_ptr) {
190 int in_delta, out_delta;
191 if (big_endian) {
192 in_delta = -1;
193 out_delta = 1;
194 val_ptr += val_sz - 1;
195 } else {
196 in_delta = out_delta = 1;
197 }
198
199 for (uint i = val_sz; i > 0; i--) {
200 *p = *val_ptr;
201 p += out_delta;
202 val_ptr += in_delta;
203 }
204}
205
Paul Sokolovsky62044602014-04-19 03:13:15 +0300206void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
207 byte *p = *ptr;
208 uint align;
209
210 int size = mp_binary_get_size(struct_type, val_type, &align);
211 if (struct_type == '@') {
212 // Make pointer aligned
Damien George40f3c022014-07-03 13:25:24 +0100213 p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
Paul Sokolovsky62044602014-04-19 03:13:15 +0300214 #if MP_ENDIANNESS_LITTLE
215 struct_type = '<';
216 #else
217 struct_type = '>';
218 #endif
219 }
Paul Sokolovsky7a2f1662014-06-25 22:25:53 +0300220 *ptr = p + size;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300221
222#if MP_ENDIANNESS_BIG
223#error Not implemented
224#endif
Damien George40f3c022014-07-03 13:25:24 +0100225 mp_int_t val;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300226 byte *in = (byte*)&val;
Paul Sokolovsky0f836ef2014-04-20 05:19:10 +0300227 switch (val_type) {
228 case 'O':
229 in = (byte*)&val_in;
230 break;
231 default:
232 val = mp_obj_get_int(val_in);
233 }
234
Paul Sokolovsky5fa5ca42014-07-05 23:43:00 +0300235 mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, in);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300236}
237
Paul Sokolovskyef9124f2014-04-11 03:46:09 +0300238void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
Damien George71e9bfa2014-04-18 23:28:12 +0100239 switch (typecode) {
Damien Georgefb510b32014-06-01 13:32:54 +0100240#if MICROPY_PY_BUILTINS_FLOAT
Damien George71e9bfa2014-04-18 23:28:12 +0100241 case 'f':
242 ((float*)p)[index] = mp_obj_float_get(val_in);
243 break;
244 case 'd':
245 ((double*)p)[index] = mp_obj_float_get(val_in);
246 break;
247#endif
248 default:
249 mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200250 }
Damien George71e9bfa2014-04-18 23:28:12 +0100251}
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200252
Damien George40f3c022014-07-03 13:25:24 +0100253void mp_binary_set_val_array_from_int(char typecode, void *p, int index, mp_int_t val) {
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200254 switch (typecode) {
255 case 'b':
256 ((int8_t*)p)[index] = val;
257 break;
258 case BYTEARRAY_TYPECODE:
259 case 'B':
260 val = ((uint8_t*)p)[index] = val;
261 break;
262 case 'h':
263 val = ((int16_t*)p)[index] = val;
264 break;
265 case 'H':
266 val = ((uint16_t*)p)[index] = val;
267 break;
268 case 'i':
269 case 'l':
270 ((int32_t*)p)[index] = val;
271 break;
272 case 'I':
273 case 'L':
274 ((uint32_t*)p)[index] = val;
275 break;
276#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
277 case 'q':
278 case 'Q':
279 assert(0);
280 ((long long*)p)[index] = val;
281 break;
282#endif
Damien Georgefb510b32014-06-01 13:32:54 +0100283#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200284 case 'f':
Damien George71e9bfa2014-04-18 23:28:12 +0100285 ((float*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200286 break;
287 case 'd':
Damien George71e9bfa2014-04-18 23:28:12 +0100288 ((double*)p)[index] = val;
Paul Sokolovsky8bc35162014-02-14 17:16:35 +0200289 break;
290#endif
291 }
292}