blob: 69c7279e370e622d5f9ea5d59b7e6b13c533eaf3 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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
Damien George79d5acb2017-09-01 10:53:29 +100085STATIC size_t calc_size_items(const char *fmt, size_t *total_sz) {
Paul Sokolovskye9db8402014-04-10 03:45:38 +030086 char fmt_type = get_fmt_type(&fmt);
Damien George79d5acb2017-09-01 10:53:29 +100087 size_t total_cnt = 0;
88 size_t size;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030089 for (size = 0; *fmt; fmt++) {
Damien George40f3c022014-07-03 13:25:24 +010090 mp_uint_t cnt = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +030091 if (unichar_isdigit(*fmt)) {
92 cnt = get_fmt_num(&fmt);
93 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +030094
Paul Sokolovskydf94b712014-05-12 23:45:50 +030095 if (*fmt == 's') {
Damien George79d5acb2017-09-01 10:53:29 +100096 total_cnt += 1;
Damien George715ee9d2016-05-28 23:27:38 +010097 size += cnt;
98 } else {
Damien George79d5acb2017-09-01 10:53:29 +100099 total_cnt += cnt;
Damien Georgec348e792019-09-02 13:09:44 +1000100 size_t align;
Damien George715ee9d2016-05-28 23:27:38 +0100101 size_t sz = mp_binary_get_size(fmt_type, *fmt, &align);
Damien George715ee9d2016-05-28 23:27:38 +0100102 while (cnt--) {
103 // Apply alignment
104 size = (size + align - 1) & ~(align - 1);
105 size += sz;
106 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300107 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300108 }
Damien George79d5acb2017-09-01 10:53:29 +1000109 *total_sz = size;
110 return total_cnt;
111}
112
113STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
114 const char *fmt = mp_obj_str_get_str(fmt_in);
115 size_t size;
116 calc_size_items(fmt, &size);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300117 return MP_OBJ_NEW_SMALL_INT(size);
118}
119MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
120
Dave Hylandsa17755e2015-12-23 19:11:27 -0800121STATIC mp_obj_t struct_unpack_from(size_t n_args, const mp_obj_t *args) {
122 // unpack requires that the buffer be exactly the right size.
123 // unpack_from requires that the buffer be "big enough".
124 // Since we implement unpack and unpack_from using the same function
125 // we relax the "exact" requirement, and only implement "big enough".
126 const char *fmt = mp_obj_str_get_str(args[0]);
Damien George79d5acb2017-09-01 10:53:29 +1000127 size_t total_sz;
128 size_t num_items = calc_size_items(fmt, &total_sz);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300129 char fmt_type = get_fmt_type(&fmt);
Dave Hylandsa17755e2015-12-23 19:11:27 -0800130 mp_obj_tuple_t *res = MP_OBJ_TO_PTR(mp_obj_new_tuple(num_items, NULL));
Damien George57a4b4f2014-04-18 22:29:21 +0100131 mp_buffer_info_t bufinfo;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800132 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300133 byte *p = bufinfo.buf;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800134 byte *end_p = &p[bufinfo.len];
135 mp_int_t offset = 0;
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300136
Dave Hylandsa17755e2015-12-23 19:11:27 -0800137 if (n_args > 2) {
138 // offset arg provided
139 offset = mp_obj_get_int(args[2]);
140 if (offset < 0) {
141 // negative offsets are relative to the end of the buffer
142 offset = bufinfo.len + offset;
143 if (offset < 0) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100144 mp_raise_ValueError(MP_ERROR_TEXT("buffer too small"));
Dave Hylandsa17755e2015-12-23 19:11:27 -0800145 }
146 }
147 p += offset;
148 }
Tom McDermott1022f9c2019-08-05 15:15:28 +1000149 byte *p_base = p;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800150
Damien George79d5acb2017-09-01 10:53:29 +1000151 // Check that the input buffer is big enough to unpack all the values
152 if (p + total_sz > end_p) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100153 mp_raise_ValueError(MP_ERROR_TEXT("buffer too small"));
Damien George79d5acb2017-09-01 10:53:29 +1000154 }
155
156 for (size_t i = 0; i < num_items;) {
157 mp_uint_t cnt = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300158 if (unichar_isdigit(*fmt)) {
Damien George79d5acb2017-09-01 10:53:29 +1000159 cnt = get_fmt_num(&fmt);
Dave Hylandsa17755e2015-12-23 19:11:27 -0800160 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300161 mp_obj_t item;
162 if (*fmt == 's') {
Damien George79d5acb2017-09-01 10:53:29 +1000163 item = mp_obj_new_bytes(p, cnt);
164 p += cnt;
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300165 res->items[i++] = item;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300166 } else {
Damien George79d5acb2017-09-01 10:53:29 +1000167 while (cnt--) {
Tom McDermott1022f9c2019-08-05 15:15:28 +1000168 item = mp_binary_get_val(fmt_type, *fmt, p_base, &p);
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300169 res->items[i++] = item;
170 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300171 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300172 fmt++;
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300173 }
Damien George999cedb2015-11-27 17:01:44 +0000174 return MP_OBJ_FROM_PTR(res);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300175}
Dave Hylandsa17755e2015-12-23 19:11:27 -0800176MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_unpack_from_obj, 2, 3, struct_unpack_from);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300177
Damien George2daacc52017-09-01 11:11:09 +1000178// This function assumes there is enough room in p to store all the values
179STATIC void struct_pack_into_internal(mp_obj_t fmt_in, byte *p, size_t n_args, const mp_obj_t *args) {
Dave Hylandsa17755e2015-12-23 19:11:27 -0800180 const char *fmt = mp_obj_str_get_str(fmt_in);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300181 char fmt_type = get_fmt_type(&fmt);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300182
Damien George24c3e9b2019-09-02 12:57:51 +1000183 byte *p_base = p;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800184 size_t i;
185 for (i = 0; i < n_args;) {
Damien George2daacc52017-09-01 11:11:09 +1000186 mp_uint_t cnt = 1;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800187 if (*fmt == '\0') {
Damien Georgedffa3832016-10-07 12:54:14 +1100188 // more arguments given than used by format string; CPython raises struct.error here
Dave Hylandsa17755e2015-12-23 19:11:27 -0800189 break;
190 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300191 if (unichar_isdigit(*fmt)) {
Damien George2daacc52017-09-01 11:11:09 +1000192 cnt = get_fmt_num(&fmt);
Dave Hylandsa17755e2015-12-23 19:11:27 -0800193 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300194
195 if (*fmt == 's') {
196 mp_buffer_info_t bufinfo;
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300197 mp_get_buffer_raise(args[i++], &bufinfo, MP_BUFFER_READ);
Damien George2daacc52017-09-01 11:11:09 +1000198 mp_uint_t to_copy = cnt;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300199 if (bufinfo.len < to_copy) {
200 to_copy = bufinfo.len;
201 }
202 memcpy(p, bufinfo.buf, to_copy);
Damien George2daacc52017-09-01 11:11:09 +1000203 memset(p + to_copy, 0, cnt - to_copy);
204 p += cnt;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300205 } else {
Damien George793d8262017-09-01 10:10:51 +1000206 // If we run out of args then we just finish; CPython would raise struct.error
Damien George2daacc52017-09-01 11:11:09 +1000207 while (cnt-- && i < n_args) {
Damien George24c3e9b2019-09-02 12:57:51 +1000208 mp_binary_set_val(fmt_type, *fmt, args[i++], p_base, &p);
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300209 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300210 }
Paul Sokolovsky2b080cf2015-10-31 18:42:35 +0300211 fmt++;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300212 }
Dave Hylandsa17755e2015-12-23 19:11:27 -0800213}
Damien George05005f62015-01-21 22:48:37 +0000214
Dave Hylandsa17755e2015-12-23 19:11:27 -0800215STATIC mp_obj_t struct_pack(size_t n_args, const mp_obj_t *args) {
216 // TODO: "The arguments must match the values required by the format exactly."
217 mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
218 vstr_t vstr;
219 vstr_init_len(&vstr, size);
Damien George69661f32020-02-27 15:36:53 +1100220 byte *p = (byte *)vstr.buf;
Dave Hylandsa17755e2015-12-23 19:11:27 -0800221 memset(p, 0, size);
Damien George2daacc52017-09-01 11:11:09 +1000222 struct_pack_into_internal(args[0], p, n_args - 1, &args[1]);
Damien George05005f62015-01-21 22:48:37 +0000223 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300224}
Paul Sokolovsky147c80b2014-05-11 22:50:27 +0300225MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300226
Dave Hylandsa17755e2015-12-23 19:11:27 -0800227STATIC mp_obj_t struct_pack_into(size_t n_args, const mp_obj_t *args) {
228 mp_buffer_info_t bufinfo;
229 mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
230 mp_int_t offset = mp_obj_get_int(args[2]);
231 if (offset < 0) {
232 // negative offsets are relative to the end of the buffer
233 offset = (mp_int_t)bufinfo.len + offset;
234 if (offset < 0) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100235 mp_raise_ValueError(MP_ERROR_TEXT("buffer too small"));
Dave Hylandsa17755e2015-12-23 19:11:27 -0800236 }
237 }
238 byte *p = (byte *)bufinfo.buf;
239 byte *end_p = &p[bufinfo.len];
240 p += offset;
241
Damien George2daacc52017-09-01 11:11:09 +1000242 // Check that the output buffer is big enough to hold all the values
243 mp_int_t sz = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
244 if (p + sz > end_p) {
Jim Mussareddef76fe2020-03-02 22:35:22 +1100245 mp_raise_ValueError(MP_ERROR_TEXT("buffer too small"));
Damien George2daacc52017-09-01 11:11:09 +1000246 }
247
248 struct_pack_into_internal(args[0], p, n_args - 3, &args[3]);
Dave Hylandsa17755e2015-12-23 19:11:27 -0800249 return mp_const_none;
250}
251MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_into_obj, 3, MP_OBJ_FUN_ARGS_MAX, struct_pack_into);
252
Damien Georgecbf76742015-11-27 13:38:15 +0000253STATIC const mp_rom_map_elem_t mp_module_struct_globals_table[] = {
254 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ustruct) },
255 { MP_ROM_QSTR(MP_QSTR_calcsize), MP_ROM_PTR(&struct_calcsize_obj) },
256 { MP_ROM_QSTR(MP_QSTR_pack), MP_ROM_PTR(&struct_pack_obj) },
Dave Hylandsa17755e2015-12-23 19:11:27 -0800257 { MP_ROM_QSTR(MP_QSTR_pack_into), MP_ROM_PTR(&struct_pack_into_obj) },
258 { MP_ROM_QSTR(MP_QSTR_unpack), MP_ROM_PTR(&struct_unpack_from_obj) },
259 { MP_ROM_QSTR(MP_QSTR_unpack_from), MP_ROM_PTR(&struct_unpack_from_obj) },
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300260};
261
Damien George3b603f22014-11-29 14:39:27 +0000262STATIC MP_DEFINE_CONST_DICT(mp_module_struct_globals, mp_module_struct_globals_table);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300263
Paul Sokolovsky3d3ef362015-05-04 16:35:40 +0300264const mp_obj_module_t mp_module_ustruct = {
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300265 .base = { &mp_type_module },
Damien George69661f32020-02-27 15:36:53 +1100266 .globals = (mp_obj_dict_t *)&mp_module_struct_globals,
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300267};
268
Damien Georgeefe23ac2022-05-31 22:56:11 +1000269MP_REGISTER_MODULE(MP_QSTR_ustruct, mp_module_ustruct);
Jim Mussaredd8d3e6a2022-04-20 16:14:22 +1000270
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300271#endif