blob: 8d440063816a59c194ede0ff13f672319cdd8c2e [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>
Paul Sokolovskye9db8402014-04-10 03:45:38 +030030#include "mpconfig.h"
Paul Sokolovsky59c675a2014-06-21 22:43:22 +030031#include "misc.h"
Paul Sokolovskye9db8402014-04-10 03:45:38 +030032#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
Damien Georgeee3fd462014-05-24 23:03:12 +010040#if MICROPY_PY_STRUCT
Paul Sokolovskye9db8402014-04-10 03:45:38 +030041
Paul Sokolovsky62798832014-06-02 16:04:26 +030042/*
43 This module implements most of character typecodes from CPython, with
44 some extensions:
45
46 O - (Pointer to) an arbitrary Python object. This is useful for callback
47 data, etc. Note that you must keep reference to passed object in
48 your Python application, otherwise it may be garbage-collected,
49 and then when you get back this value from callback it may be
50 invalid (and lead to crash).
51 S - Pointer to a string (returned as a Python string). Note the
52 difference from "Ns", - the latter says "in this place of structure
53 is character data of up to N bytes length", while "S" means
54 "in this place of a structure is a pointer to zero-terminated
55 character data".
56 */
57
Paul Sokolovskye9db8402014-04-10 03:45:38 +030058STATIC char get_fmt_type(const char **fmt) {
59 char t = **fmt;
60 switch (t) {
61 case '!':
62 t = '>';
63 break;
64 case '@':
65 case '=':
66 case '<':
67 case '>':
68 break;
69 default:
70 return '@';
71 }
72 // Skip type char
73 (*fmt)++;
74 return t;
75}
76
Damien George40f3c022014-07-03 13:25:24 +010077STATIC mp_uint_t get_fmt_num(const char **p) {
Paul Sokolovskydf94b712014-05-12 23:45:50 +030078 const char *num = *p;
79 uint len = 1;
80 while (unichar_isdigit(*++num)) {
81 len++;
82 }
Damien George40f3c022014-07-03 13:25:24 +010083 mp_uint_t val = (mp_uint_t)MP_OBJ_SMALL_INT_VALUE(mp_parse_num_integer(*p, len, 10));
Paul Sokolovskydf94b712014-05-12 23:45:50 +030084 *p = num;
85 return val;
86}
87
Paul Sokolovskye9db8402014-04-10 03:45:38 +030088STATIC uint calcsize_items(const char *fmt) {
Paul Sokolovskydf94b712014-05-12 23:45:50 +030089 uint cnt = 0;
90 while (*fmt) {
91 // TODO supports size spec only for "s"
92 if (!unichar_isdigit(*fmt++)) {
93 cnt++;
94 }
95 }
96 return cnt;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030097}
98
99STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
100 const char *fmt = mp_obj_str_get_str(fmt_in);
101 char fmt_type = get_fmt_type(&fmt);
Damien George40f3c022014-07-03 13:25:24 +0100102 mp_uint_t size;
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300103 for (size = 0; *fmt; fmt++) {
Damien George4abff752014-08-30 14:59:21 +0100104 mp_uint_t align = 1;
Damien George40f3c022014-07-03 13:25:24 +0100105 mp_uint_t cnt = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300106 if (unichar_isdigit(*fmt)) {
107 cnt = get_fmt_num(&fmt);
108 }
109 if (cnt > 1) {
110 // TODO: count spec support only for string len
111 assert(*fmt == 's');
112 }
113
Damien George40f3c022014-07-03 13:25:24 +0100114 mp_uint_t sz;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300115 if (*fmt == 's') {
116 sz = cnt;
117 } else {
Damien George40f3c022014-07-03 13:25:24 +0100118 sz = (mp_uint_t)mp_binary_get_size(fmt_type, *fmt, &align);
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300119 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300120 // TODO
Damien George40f3c022014-07-03 13:25:24 +0100121 assert(sz != (mp_uint_t)-1);
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300122 // Apply alignment
123 size = (size + align - 1) & ~(align - 1);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300124 size += sz;
125 }
126 return MP_OBJ_NEW_SMALL_INT(size);
127}
128MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
129
130STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
131 // TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
132 const char *fmt = mp_obj_str_get_str(fmt_in);
133 char fmt_type = get_fmt_type(&fmt);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300134 uint size = calcsize_items(fmt);
135 mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
Damien George57a4b4f2014-04-18 22:29:21 +0100136 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100137 mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300138 byte *p = bufinfo.buf;
139
140 for (uint i = 0; i < size; i++) {
Damien George40f3c022014-07-03 13:25:24 +0100141 mp_uint_t sz = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300142 if (unichar_isdigit(*fmt)) {
143 sz = get_fmt_num(&fmt);
144 }
145 if (sz > 1) {
146 // TODO: size spec support only for string len
147 assert(*fmt == 's');
148 }
149 mp_obj_t item;
150 if (*fmt == 's') {
151 item = mp_obj_new_bytes(p, sz);
152 p += sz;
153 fmt++;
154 } else {
155 item = mp_binary_get_val(fmt_type, *fmt++, &p);
156 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300157 res->items[i] = item;
158 }
159 return res;
160}
161MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
162
Paul Sokolovsky62044602014-04-19 03:13:15 +0300163STATIC mp_obj_t struct_pack(uint n_args, mp_obj_t *args) {
164 // TODO: "The arguments must match the values required by the format exactly."
165 const char *fmt = mp_obj_str_get_str(args[0]);
166 char fmt_type = get_fmt_type(&fmt);
167 int size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
168 byte *p;
169 mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p);
170 memset(p, 0, size);
171
172 for (uint i = 1; i < n_args; i++) {
Damien George40f3c022014-07-03 13:25:24 +0100173 mp_uint_t sz = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300174 if (unichar_isdigit(*fmt)) {
175 sz = get_fmt_num(&fmt);
176 }
177 if (sz > 1) {
178 // TODO: size spec support only for string len
179 assert(*fmt == 's');
180 }
181
182 if (*fmt == 's') {
183 mp_buffer_info_t bufinfo;
184 mp_get_buffer_raise(args[i], &bufinfo, MP_BUFFER_READ);
Damien George40f3c022014-07-03 13:25:24 +0100185 mp_uint_t to_copy = sz;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300186 if (bufinfo.len < to_copy) {
187 to_copy = bufinfo.len;
188 }
189 memcpy(p, bufinfo.buf, to_copy);
190 memset(p + to_copy, 0, sz - to_copy);
191 p += sz;
192 fmt++;
193 } else {
194 mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
195 }
Paul Sokolovsky62044602014-04-19 03:13:15 +0300196 }
197 return res;
198}
Paul Sokolovsky147c80b2014-05-11 22:50:27 +0300199MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300200
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300201STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
202 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) },
203 { MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj },
Paul Sokolovsky62044602014-04-19 03:13:15 +0300204 { MP_OBJ_NEW_QSTR(MP_QSTR_pack), (mp_obj_t)&struct_pack_obj },
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300205 { MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
206};
207
208STATIC const mp_obj_dict_t mp_module_struct_globals = {
209 .base = {&mp_type_dict},
210 .map = {
211 .all_keys_are_qstrs = 1,
212 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200213 .used = MP_ARRAY_SIZE(mp_module_struct_globals_table),
214 .alloc = MP_ARRAY_SIZE(mp_module_struct_globals_table),
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300215 .table = (mp_map_elem_t*)mp_module_struct_globals_table,
216 },
217};
218
219const mp_obj_module_t mp_module_struct = {
220 .base = { &mp_type_module },
221 .name = MP_QSTR_struct,
222 .globals = (mp_obj_dict_t*)&mp_module_struct_globals,
223};
224
225#endif