binary.c 8.42 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * This file is part of the Micro Python project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2013, 2014 Damien P. George
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

27
#include <stdint.h>
28
#include <stdlib.h>
29
#include <stddef.h>
30
#include <string.h>
31
32
33
#include <assert.h>

#include "mpconfig.h"
34
#include "misc.h"
35
36
37
38
39
40
#include "qstr.h"
#include "obj.h"
#include "binary.h"

// Helpers to work with binary-encoded data

41
42
43
44
#ifndef alignof
#define alignof(type) offsetof(struct { char c; type t; }, t)
#endif

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
    int size = 0;
    int align = 1;
    switch (struct_type) {
        case '<': case '>':
            switch (val_type) {
                case 'b': case 'B':
                    size = 1; break;
                case 'h': case 'H':
                    size = 2; break;
                case 'i': case 'I':
                    size = 4; break;
                case 'l': case 'L':
                    size = 4; break;
                case 'q': case 'Q':
                    size = 8; break;
            }
            break;
        case '@': {
            // TODO:
            // The simplest heuristic for alignment is to align by value
            // size, but that doesn't work for "bigger than int" types,
            // for example, long long may very well have long alignment
            // So, we introduce separate alignment handling, but having
            // formal support for that is different from actually supporting
            // particular (or any) ABI.
            switch (val_type) {
                case BYTEARRAY_TYPECODE:
                case 'b': case 'B':
                    align = size = 1; break;
                case 'h': case 'H':
76
77
                    align = alignof(short);
                    size = sizeof(short); break;
78
                case 'i': case 'I':
79
80
                    align = alignof(int);
                    size = sizeof(int); break;
81
                case 'l': case 'L':
82
83
                    align = alignof(long);
                    size = sizeof(long); break;
84
                case 'q': case 'Q':
85
86
                    align = alignof(long long);
                    size = sizeof(long long); break;
87
                case 'P': case 'O': case 'S':
88
89
                    align = alignof(void*);
                    size = sizeof(void*); break;
90
91
            }
        }
92
    }
93
94
95
96
    if (palign != NULL) {
        *palign = align;
    }
    return size;
97
98
}

99
mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
100
    mp_int_t val = 0;
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    switch (typecode) {
        case 'b':
            val = ((int8_t*)p)[index];
            break;
        case BYTEARRAY_TYPECODE:
        case 'B':
            val = ((uint8_t*)p)[index];
            break;
        case 'h':
            val = ((int16_t*)p)[index];
            break;
        case 'H':
            val = ((uint16_t*)p)[index];
            break;
        case 'i':
        case 'l':
            return mp_obj_new_int(((int32_t*)p)[index]);
        case 'I':
        case 'L':
            return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
121
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
122
123
124
125
126
        case 'q':
        case 'Q':
            // TODO: Explode API more to cover signedness
            return mp_obj_new_int_from_ll(((long long*)p)[index]);
#endif
127
#if MICROPY_PY_BUILTINS_FLOAT
128
129
130
131
132
133
134
135
136
        case 'f':
            return mp_obj_new_float(((float*)p)[index]);
        case 'd':
            return mp_obj_new_float(((double*)p)[index]);
#endif
    }
    return MP_OBJ_NEW_SMALL_INT(val);
}

137
mp_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p) {
138
139
140
141
142
143
144
145
    int delta;
    if (!big_endian) {
        delta = -1;
        p += size - 1;
    } else {
        delta = 1;
    }

146
    mp_int_t val = 0;
147
148
149
150
151
152
153
154
155
156
157
158
    if (is_signed && *p & 0x80) {
        val = -1;
    }
    for (uint i = 0; i < size; i++) {
        val <<= 8;
        val |= *p;
        p += delta;
    }

    return val;
}

159
#define is_signed(typecode) (typecode > 'Z')
160
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
161
    byte *p = *ptr;
162
163
164
165
166
    uint align;

    int size = mp_binary_get_size(struct_type, val_type, &align);
    if (struct_type == '@') {
        // Make pointer aligned
167
        p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
168
169
170
171
172
        #if MP_ENDIANNESS_LITTLE
        struct_type = '<';
        #else
        struct_type = '>';
        #endif
173
    }
174
    *ptr = p + size;
175

176
    mp_int_t val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
177

178
179
    if (val_type == 'O') {
        return (mp_obj_t)val;
180
181
    } else if (val_type == 'S') {
        return mp_obj_new_str((char*)val, strlen((char*)val), false);
182
    } else if (is_signed(val_type)) {
183
184
185
186
        return mp_obj_new_int(val);
    } else {
        return mp_obj_new_int_from_uint(val);
    }
187
188
}

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
void mp_binary_set_int(uint val_sz, bool big_endian, byte *p, byte *val_ptr) {
    int in_delta, out_delta;
    if (big_endian) {
        in_delta = -1;
        out_delta = 1;
        val_ptr += val_sz - 1;
    } else {
        in_delta = out_delta = 1;
    }

    for (uint i = val_sz; i > 0; i--) {
        *p = *val_ptr;
        p += out_delta;
        val_ptr += in_delta;
    }
}

206
207
208
209
210
211
212
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
    byte *p = *ptr;
    uint align;

    int size = mp_binary_get_size(struct_type, val_type, &align);
    if (struct_type == '@') {
        // Make pointer aligned
213
        p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1));
214
215
216
217
218
219
        #if MP_ENDIANNESS_LITTLE
        struct_type = '<';
        #else
        struct_type = '>';
        #endif
    }
220
    *ptr = p + size;
221
222
223
224

#if MP_ENDIANNESS_BIG
#error Not implemented
#endif
225
    mp_int_t val;
226
    byte *in = (byte*)&val;
227
228
229
230
231
232
233
234
    switch (val_type) {
        case 'O':
            in = (byte*)&val_in;
            break;
        default:
            val = mp_obj_get_int(val_in);
    }

235
    mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, in);
236
237
}

238
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
239
    switch (typecode) {
240
#if MICROPY_PY_BUILTINS_FLOAT
241
242
243
244
245
246
247
248
249
        case 'f':
            ((float*)p)[index] = mp_obj_float_get(val_in);
            break;
        case 'd':
            ((double*)p)[index] = mp_obj_float_get(val_in);
            break;
#endif
        default:
            mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
250
    }
251
}
252

253
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, mp_int_t val) {
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
    switch (typecode) {
        case 'b':
            ((int8_t*)p)[index] = val;
            break;
        case BYTEARRAY_TYPECODE:
        case 'B':
            val = ((uint8_t*)p)[index] = val;
            break;
        case 'h':
            val = ((int16_t*)p)[index] = val;
            break;
        case 'H':
            val = ((uint16_t*)p)[index] = val;
            break;
        case 'i':
        case 'l':
            ((int32_t*)p)[index] = val;
            break;
        case 'I':
        case 'L':
            ((uint32_t*)p)[index] = val;
            break;
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
        case 'q':
        case 'Q':
            assert(0);
            ((long long*)p)[index] = val;
            break;
#endif
283
#if MICROPY_PY_BUILTINS_FLOAT
284
        case 'f':
285
            ((float*)p)[index] = val;
286
287
            break;
        case 'd':
288
            ((double*)p)[index] = val;
289
290
291
292
            break;
#endif
    }
}