blob: eabc951aefbf8f20fa447a5a0e252b742de234f9 [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>
Damien George51dfcb42015-01-01 20:27:54 +000030
Paul Sokolovsky7e66b852015-07-05 22:37:32 +030031#include "py/runtime.h"
Damien George51dfcb42015-01-01 20:27:54 +000032#include "py/builtin.h"
33#include "py/objtuple.h"
34#include "py/binary.h"
35#include "py/parsenum.h"
Paul Sokolovskye9db8402014-04-10 03:45:38 +030036
Damien Georgeee3fd462014-05-24 23:03:12 +010037#if MICROPY_PY_STRUCT
Paul Sokolovskye9db8402014-04-10 03:45:38 +030038
Paul Sokolovsky62798832014-06-02 16:04:26 +030039/*
40 This module implements most of character typecodes from CPython, with
41 some extensions:
42
43 O - (Pointer to) an arbitrary Python object. This is useful for callback
44 data, etc. Note that you must keep reference to passed object in
45 your Python application, otherwise it may be garbage-collected,
46 and then when you get back this value from callback it may be
47 invalid (and lead to crash).
48 S - Pointer to a string (returned as a Python string). Note the
49 difference from "Ns", - the latter says "in this place of structure
50 is character data of up to N bytes length", while "S" means
51 "in this place of a structure is a pointer to zero-terminated
52 character data".
53 */
54
Paul Sokolovskye9db8402014-04-10 03:45:38 +030055STATIC char get_fmt_type(const char **fmt) {
56 char t = **fmt;
57 switch (t) {
58 case '!':
59 t = '>';
60 break;
61 case '@':
62 case '=':
63 case '<':
64 case '>':
65 break;
66 default:
67 return '@';
68 }
69 // Skip type char
70 (*fmt)++;
71 return t;
72}
73
Damien George40f3c022014-07-03 13:25:24 +010074STATIC mp_uint_t get_fmt_num(const char **p) {
Paul Sokolovskydf94b712014-05-12 23:45:50 +030075 const char *num = *p;
76 uint len = 1;
77 while (unichar_isdigit(*++num)) {
78 len++;
79 }
Damien George7d414a12015-02-08 01:57:40 +000080 mp_uint_t val = (mp_uint_t)MP_OBJ_SMALL_INT_VALUE(mp_parse_num_integer(*p, len, 10, NULL));
Paul Sokolovskydf94b712014-05-12 23:45:50 +030081 *p = num;
82 return val;
83}
84
Paul Sokolovskye9db8402014-04-10 03:45:38 +030085STATIC uint calcsize_items(const char *fmt) {
Paul Sokolovskydf94b712014-05-12 23:45:50 +030086 uint cnt = 0;
87 while (*fmt) {
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +030088 int num = 1;
89 if (unichar_isdigit(*fmt)) {
90 num = get_fmt_num(&fmt);
91 if (*fmt == 's') {
92 num = 1;
93 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +030094 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +030095 cnt += num;
96 fmt++;
Paul Sokolovskydf94b712014-05-12 23:45:50 +030097 }
98 return cnt;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030099}
100
101STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
102 const char *fmt = mp_obj_str_get_str(fmt_in);
103 char fmt_type = get_fmt_type(&fmt);
Damien George40f3c022014-07-03 13:25:24 +0100104 mp_uint_t size;
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300105 for (size = 0; *fmt; fmt++) {
Damien George4abff752014-08-30 14:59:21 +0100106 mp_uint_t align = 1;
Damien George40f3c022014-07-03 13:25:24 +0100107 mp_uint_t cnt = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300108 if (unichar_isdigit(*fmt)) {
109 cnt = get_fmt_num(&fmt);
110 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300111
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300112 mp_uint_t sz = 0;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300113 if (*fmt == 's') {
114 sz = cnt;
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300115 cnt = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300116 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300117
118 while (cnt--) {
119 // If we already have size for 's' case, don't set it again
120 if (sz == 0) {
121 sz = (mp_uint_t)mp_binary_get_size(fmt_type, *fmt, &align);
122 }
Paul Sokolovskye53fb1b2016-05-14 15:47:08 +0300123 if (sz == 0) {
124 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "unsupported format"));
125 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300126 // Apply alignment
127 size = (size + align - 1) & ~(align - 1);
128 size += sz;
129 sz = 0;
130 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300131 }
132 return MP_OBJ_NEW_SMALL_INT(size);
133}
134MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
135
Dave Hylandsa17755e2015-12-23 19:11:27 -0800136STATIC mp_obj_t struct_unpack_from(size_t n_args, const mp_obj_t *args) {
137 // unpack requires that the buffer be exactly the right size.
138 // unpack_from requires that the buffer be "big enough".
139 // Since we implement unpack and unpack_from using the same function
140 // we relax the "exact" requirement, and only implement "big enough".
141 const char *fmt = mp_obj_str_get_str(args[0]);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300142 char fmt_type = get_fmt_type(&fmt);
Dave Hylandsa17755e2015-12-23 19:11:27 -0800143 uint num_items = calcsize_items(fmt);
144 mp_obj_tuple_t *res = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_items, NULL));
Damien George57a4b4f2014-04-18 22:29:21 +0100145 mp_buffer_info_t bufinfo;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800146 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300147 byte *p = bufinfo.buf;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800148 byte *end_p = &p[bufinfo.len];
149 mp_int_t offset = 0;
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300150
Dave Hylandsa17755e2015-12-23 19:11:27 -0800151 if (n_args > 2) {
152 // offset arg provided
153 offset = mp_obj_get_int(args[2]);
154 if (offset < 0) {
155 // negative offsets are relative to the end of the buffer
156 offset = bufinfo.len + offset;
157 if (offset < 0) {
158 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small"));
159 }
160 }
161 p += offset;
162 }
163
164 for (uint i = 0; i < num_items;) {
165 if (*fmt == '\0') {
166 break;
167 }
Damien George40f3c022014-07-03 13:25:24 +0100168 mp_uint_t sz = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300169 if (unichar_isdigit(*fmt)) {
170 sz = get_fmt_num(&fmt);
171 }
Dave Hylandsa17755e2015-12-23 19:11:27 -0800172 if (p + sz > end_p) {
173 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small"));
174 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300175 mp_obj_t item;
176 if (*fmt == 's') {
177 item = mp_obj_new_bytes(p, sz);
178 p += sz;
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300179 res->items[i++] = item;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300180 } else {
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300181 while (sz--) {
182 item = mp_binary_get_val(fmt_type, *fmt, &p);
183 res->items[i++] = item;
184 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300185 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300186 fmt++;
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300187 }
Damien George999cedb2015-11-27 17:01:44 +0000188 return MP_OBJ_FROM_PTR(res);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300189}
Dave Hylandsa17755e2015-12-23 19:11:27 -0800190MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_unpack_from_obj, 2, 3, struct_unpack_from);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300191
Dave Hylandsa17755e2015-12-23 19:11:27 -0800192STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, byte* end_p, size_t n_args, const mp_obj_t *args) {
193 const char *fmt = mp_obj_str_get_str(fmt_in);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300194 char fmt_type = get_fmt_type(&fmt);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300195
Dave Hylandsa17755e2015-12-23 19:11:27 -0800196 size_t i;
197 for (i = 0; i < n_args;) {
Damien George40f3c022014-07-03 13:25:24 +0100198 mp_uint_t sz = 1;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800199 if (*fmt == '\0') {
200 break;
201 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300202 if (unichar_isdigit(*fmt)) {
203 sz = get_fmt_num(&fmt);
204 }
Dave Hylandsa17755e2015-12-23 19:11:27 -0800205 if (p + sz > end_p) {
206 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small"));
207 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300208
209 if (*fmt == 's') {
210 mp_buffer_info_t bufinfo;
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300211 mp_get_buffer_raise(args[i++], &bufinfo, MP_BUFFER_READ);
Damien George40f3c022014-07-03 13:25:24 +0100212 mp_uint_t to_copy = sz;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300213 if (bufinfo.len < to_copy) {
214 to_copy = bufinfo.len;
215 }
216 memcpy(p, bufinfo.buf, to_copy);
217 memset(p + to_copy, 0, sz - to_copy);
218 p += sz;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300219 } else {
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300220 while (sz--) {
221 mp_binary_set_val(fmt_type, *fmt, args[i++], &p);
222 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300223 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300224 fmt++;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300225 }
Dave Hylandsa17755e2015-12-23 19:11:27 -0800226}
Damien George05005f62015-01-21 22:48:37 +0000227
Dave Hylandsa17755e2015-12-23 19:11:27 -0800228STATIC mp_obj_t struct_pack(size_t n_args, const mp_obj_t *args) {
229 // TODO: "The arguments must match the values required by the format exactly."
230 mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
231 vstr_t vstr;
232 vstr_init_len(&vstr, size);
233 byte *p = (byte*)vstr.buf;
234 memset(p, 0, size);
235 byte *end_p = &p[size];
236 struct_pack_into_internal(args[0], p, end_p, n_args - 1, &args[1]);
Damien George05005f62015-01-21 22:48:37 +0000237 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300238}
Paul Sokolovsky147c80b2014-05-11 22:50:27 +0300239MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300240
Dave Hylandsa17755e2015-12-23 19:11:27 -0800241STATIC mp_obj_t struct_pack_into(size_t n_args, const mp_obj_t *args) {
242 mp_buffer_info_t bufinfo;
243 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
244 mp_int_t offset = mp_obj_get_int(args[2]);
245 if (offset < 0) {
246 // negative offsets are relative to the end of the buffer
247 offset = (mp_int_t)bufinfo.len + offset;
248 if (offset < 0) {
249 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "buffer too small"));
250 }
251 }
252 byte *p = (byte *)bufinfo.buf;
253 byte *end_p = &p[bufinfo.len];
254 p += offset;
255
256 struct_pack_into_internal(args[0], p, end_p, n_args - 3, &args[3]);
257 return mp_const_none;
258}
259MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_into_obj, 3, MP_OBJ_FUN_ARGS_MAX, struct_pack_into);
260
Damien Georgecbf76742015-11-27 13:38:15 +0000261STATIC const mp_rom_map_elem_t mp_module_struct_globals_table[] = {
262 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ustruct) },
263 { MP_ROM_QSTR(MP_QSTR_calcsize), MP_ROM_PTR(&struct_calcsize_obj) },
264 { MP_ROM_QSTR(MP_QSTR_pack), MP_ROM_PTR(&struct_pack_obj) },
Dave Hylandsa17755e2015-12-23 19:11:27 -0800265 { MP_ROM_QSTR(MP_QSTR_pack_into), MP_ROM_PTR(&struct_pack_into_obj) },
266 { MP_ROM_QSTR(MP_QSTR_unpack), MP_ROM_PTR(&struct_unpack_from_obj) },
267 { MP_ROM_QSTR(MP_QSTR_unpack_from), MP_ROM_PTR(&struct_unpack_from_obj) },
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300268};
269
Damien George3b603f22014-11-29 14:39:27 +0000270STATIC MP_DEFINE_CONST_DICT(mp_module_struct_globals, mp_module_struct_globals_table);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300271
Paul Sokolovsky3d3ef362015-05-04 16:35:40 +0300272const mp_obj_module_t mp_module_ustruct = {
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300273 .base = { &mp_type_module },
Paul Sokolovsky3d3ef362015-05-04 16:35:40 +0300274 .name = MP_QSTR_ustruct,
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300275 .globals = (mp_obj_dict_t*)&mp_module_struct_globals,
276};
277
278#endif