blob: 95a282e2ec1746a8bf774d97bf017809a7d4899c [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
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
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
Paul Sokolovskye9db8402014-04-10 03:45:38 +030028#include <assert.h>
29#include <string.h>
30#include "misc.h"
31#include "mpconfig.h"
32#include "qstr.h"
33#include "obj.h"
34#include "builtin.h"
35#include "objtuple.h"
Paul Sokolovsky62044602014-04-19 03:13:15 +030036#include "objstr.h"
Paul Sokolovskye9db8402014-04-10 03:45:38 +030037#include "binary.h"
Paul Sokolovskydf94b712014-05-12 23:45:50 +030038#include "parsenum.h"
Paul Sokolovskye9db8402014-04-10 03:45:38 +030039
40#if MICROPY_ENABLE_MOD_STRUCT
41
42STATIC char get_fmt_type(const char **fmt) {
43 char t = **fmt;
44 switch (t) {
45 case '!':
46 t = '>';
47 break;
48 case '@':
49 case '=':
50 case '<':
51 case '>':
52 break;
53 default:
54 return '@';
55 }
56 // Skip type char
57 (*fmt)++;
58 return t;
59}
60
Paul Sokolovskydf94b712014-05-12 23:45:50 +030061STATIC machine_uint_t get_fmt_num(const char **p) {
62 const char *num = *p;
63 uint len = 1;
64 while (unichar_isdigit(*++num)) {
65 len++;
66 }
67 machine_uint_t val = (machine_uint_t)MP_OBJ_SMALL_INT_VALUE(mp_parse_num_integer(*p, len, 10));
68 *p = num;
69 return val;
70}
71
Paul Sokolovskye9db8402014-04-10 03:45:38 +030072STATIC uint calcsize_items(const char *fmt) {
Paul Sokolovskydf94b712014-05-12 23:45:50 +030073 uint cnt = 0;
74 while (*fmt) {
75 // TODO supports size spec only for "s"
76 if (!unichar_isdigit(*fmt++)) {
77 cnt++;
78 }
79 }
80 return cnt;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030081}
82
83STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
84 const char *fmt = mp_obj_str_get_str(fmt_in);
85 char fmt_type = get_fmt_type(&fmt);
Damien Georgebf8ae4d2014-04-10 13:53:31 +010086 machine_uint_t size;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030087 for (size = 0; *fmt; fmt++) {
Paul Sokolovsky1355cf42014-04-19 01:25:49 +030088 uint align;
Paul Sokolovskydf94b712014-05-12 23:45:50 +030089 machine_uint_t cnt = 1;
90 if (unichar_isdigit(*fmt)) {
91 cnt = get_fmt_num(&fmt);
92 }
93 if (cnt > 1) {
94 // TODO: count spec support only for string len
95 assert(*fmt == 's');
96 }
97
98 machine_uint_t sz;
99 if (*fmt == 's') {
100 sz = cnt;
101 } else {
102 sz = (machine_uint_t)mp_binary_get_size(fmt_type, *fmt, &align);
103 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300104 // TODO
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300105 assert(sz != (machine_uint_t)-1);
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300106 // Apply alignment
107 size = (size + align - 1) & ~(align - 1);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300108 size += sz;
109 }
110 return MP_OBJ_NEW_SMALL_INT(size);
111}
112MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
113
114STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
115 // TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
116 const char *fmt = mp_obj_str_get_str(fmt_in);
117 char fmt_type = get_fmt_type(&fmt);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300118 uint size = calcsize_items(fmt);
119 mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
Damien George57a4b4f2014-04-18 22:29:21 +0100120 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100121 mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300122 byte *p = bufinfo.buf;
123
124 for (uint i = 0; i < size; i++) {
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300125 machine_uint_t sz = 1;
126 if (unichar_isdigit(*fmt)) {
127 sz = get_fmt_num(&fmt);
128 }
129 if (sz > 1) {
130 // TODO: size spec support only for string len
131 assert(*fmt == 's');
132 }
133 mp_obj_t item;
134 if (*fmt == 's') {
135 item = mp_obj_new_bytes(p, sz);
136 p += sz;
137 fmt++;
138 } else {
139 item = mp_binary_get_val(fmt_type, *fmt++, &p);
140 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300141 res->items[i] = item;
142 }
143 return res;
144}
145MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
146
Paul Sokolovsky62044602014-04-19 03:13:15 +0300147STATIC mp_obj_t struct_pack(uint n_args, mp_obj_t *args) {
148 // TODO: "The arguments must match the values required by the format exactly."
149 const char *fmt = mp_obj_str_get_str(args[0]);
150 char fmt_type = get_fmt_type(&fmt);
151 int size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
152 byte *p;
153 mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p);
154 memset(p, 0, size);
155
156 for (uint i = 1; i < n_args; i++) {
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300157 machine_uint_t sz = 1;
158 if (unichar_isdigit(*fmt)) {
159 sz = get_fmt_num(&fmt);
160 }
161 if (sz > 1) {
162 // TODO: size spec support only for string len
163 assert(*fmt == 's');
164 }
165
166 if (*fmt == 's') {
167 mp_buffer_info_t bufinfo;
168 mp_get_buffer_raise(args[i], &bufinfo, MP_BUFFER_READ);
169 machine_uint_t to_copy = sz;
170 if (bufinfo.len < to_copy) {
171 to_copy = bufinfo.len;
172 }
173 memcpy(p, bufinfo.buf, to_copy);
174 memset(p + to_copy, 0, sz - to_copy);
175 p += sz;
176 fmt++;
177 } else {
178 mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
179 }
Paul Sokolovsky62044602014-04-19 03:13:15 +0300180 }
181 return res;
182}
Paul Sokolovsky147c80b2014-05-11 22:50:27 +0300183MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300184
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300185STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
186 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) },
187 { MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj },
Paul Sokolovsky62044602014-04-19 03:13:15 +0300188 { MP_OBJ_NEW_QSTR(MP_QSTR_pack), (mp_obj_t)&struct_pack_obj },
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300189 { MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
190};
191
192STATIC const mp_obj_dict_t mp_module_struct_globals = {
193 .base = {&mp_type_dict},
194 .map = {
195 .all_keys_are_qstrs = 1,
196 .table_is_fixed_array = 1,
Damien George6d3c5e42014-04-26 10:47:29 +0100197 .used = ARRAY_SIZE(mp_module_struct_globals_table),
198 .alloc = ARRAY_SIZE(mp_module_struct_globals_table),
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300199 .table = (mp_map_elem_t*)mp_module_struct_globals_table,
200 },
201};
202
203const mp_obj_module_t mp_module_struct = {
204 .base = { &mp_type_module },
205 .name = MP_QSTR_struct,
206 .globals = (mp_obj_dict_t*)&mp_module_struct_globals,
207};
208
209#endif