blob: 1ba974d55d3b5879b5f5b3b099b9e16496696971 [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) {
88 // TODO supports size spec only for "s"
89 if (!unichar_isdigit(*fmt++)) {
90 cnt++;
91 }
92 }
93 return cnt;
Paul Sokolovskye9db8402014-04-10 03:45:38 +030094}
95
96STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
97 const char *fmt = mp_obj_str_get_str(fmt_in);
98 char fmt_type = get_fmt_type(&fmt);
Damien George40f3c022014-07-03 13:25:24 +010099 mp_uint_t size;
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300100 for (size = 0; *fmt; fmt++) {
Damien George4abff752014-08-30 14:59:21 +0100101 mp_uint_t align = 1;
Damien George40f3c022014-07-03 13:25:24 +0100102 mp_uint_t cnt = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300103 if (unichar_isdigit(*fmt)) {
104 cnt = get_fmt_num(&fmt);
105 }
106 if (cnt > 1) {
107 // TODO: count spec support only for string len
Paul Sokolovsky7e66b852015-07-05 22:37:32 +0300108 if (*fmt != 's') {
109 mp_not_implemented("count>1");
110 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300111 }
112
Damien George40f3c022014-07-03 13:25:24 +0100113 mp_uint_t sz;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300114 if (*fmt == 's') {
115 sz = cnt;
116 } else {
Damien George40f3c022014-07-03 13:25:24 +0100117 sz = (mp_uint_t)mp_binary_get_size(fmt_type, *fmt, &align);
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300118 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300119 // TODO
Damien George40f3c022014-07-03 13:25:24 +0100120 assert(sz != (mp_uint_t)-1);
Paul Sokolovsky1355cf42014-04-19 01:25:49 +0300121 // Apply alignment
122 size = (size + align - 1) & ~(align - 1);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300123 size += sz;
124 }
125 return MP_OBJ_NEW_SMALL_INT(size);
126}
127MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
128
129STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
130 // TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
131 const char *fmt = mp_obj_str_get_str(fmt_in);
132 char fmt_type = get_fmt_type(&fmt);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300133 uint size = calcsize_items(fmt);
134 mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
Damien George57a4b4f2014-04-18 22:29:21 +0100135 mp_buffer_info_t bufinfo;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100136 mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300137 byte *p = bufinfo.buf;
138
139 for (uint i = 0; i < size; i++) {
Damien George40f3c022014-07-03 13:25:24 +0100140 mp_uint_t sz = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300141 if (unichar_isdigit(*fmt)) {
142 sz = get_fmt_num(&fmt);
143 }
144 if (sz > 1) {
145 // TODO: size spec support only for string len
Paul Sokolovsky7e66b852015-07-05 22:37:32 +0300146 if (*fmt != 's') {
147 mp_not_implemented("count>1");
148 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300149 }
150 mp_obj_t item;
151 if (*fmt == 's') {
152 item = mp_obj_new_bytes(p, sz);
153 p += sz;
154 fmt++;
155 } else {
156 item = mp_binary_get_val(fmt_type, *fmt++, &p);
157 }
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300158 res->items[i] = item;
159 }
160 return res;
161}
162MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
163
Damien George9336ee32014-10-06 15:05:35 +0000164STATIC mp_obj_t struct_pack(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky62044602014-04-19 03:13:15 +0300165 // TODO: "The arguments must match the values required by the format exactly."
166 const char *fmt = mp_obj_str_get_str(args[0]);
167 char fmt_type = get_fmt_type(&fmt);
Damien George42f3de92014-10-03 17:44:14 +0000168 mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
Damien George05005f62015-01-21 22:48:37 +0000169 vstr_t vstr;
170 vstr_init_len(&vstr, size);
171 byte *p = (byte*)vstr.buf;
Paul Sokolovsky62044602014-04-19 03:13:15 +0300172 memset(p, 0, size);
173
Damien George9336ee32014-10-06 15:05:35 +0000174 for (mp_uint_t i = 1; i < n_args; i++) {
Damien George40f3c022014-07-03 13:25:24 +0100175 mp_uint_t sz = 1;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300176 if (unichar_isdigit(*fmt)) {
177 sz = get_fmt_num(&fmt);
178 }
179 if (sz > 1) {
180 // TODO: size spec support only for string len
Paul Sokolovsky7e66b852015-07-05 22:37:32 +0300181 if (*fmt != 's') {
182 mp_not_implemented("count>1");
183 }
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300184 }
185
186 if (*fmt == 's') {
187 mp_buffer_info_t bufinfo;
188 mp_get_buffer_raise(args[i], &bufinfo, MP_BUFFER_READ);
Damien George40f3c022014-07-03 13:25:24 +0100189 mp_uint_t to_copy = sz;
Paul Sokolovskydf94b712014-05-12 23:45:50 +0300190 if (bufinfo.len < to_copy) {
191 to_copy = bufinfo.len;
192 }
193 memcpy(p, bufinfo.buf, to_copy);
194 memset(p + to_copy, 0, sz - to_copy);
195 p += sz;
196 fmt++;
197 } else {
198 mp_binary_set_val(fmt_type, *fmt++, args[i], &p);
199 }
Paul Sokolovsky62044602014-04-19 03:13:15 +0300200 }
Damien George05005f62015-01-21 22:48:37 +0000201
202 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300203}
Paul Sokolovsky147c80b2014-05-11 22:50:27 +0300204MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack);
Paul Sokolovsky62044602014-04-19 03:13:15 +0300205
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300206STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
Paul Sokolovsky3d3ef362015-05-04 16:35:40 +0300207 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ustruct) },
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300208 { MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj },
Paul Sokolovsky62044602014-04-19 03:13:15 +0300209 { MP_OBJ_NEW_QSTR(MP_QSTR_pack), (mp_obj_t)&struct_pack_obj },
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300210 { MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
211};
212
Damien George3b603f22014-11-29 14:39:27 +0000213STATIC MP_DEFINE_CONST_DICT(mp_module_struct_globals, mp_module_struct_globals_table);
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300214
Paul Sokolovsky3d3ef362015-05-04 16:35:40 +0300215const mp_obj_module_t mp_module_ustruct = {
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300216 .base = { &mp_type_module },
Paul Sokolovsky3d3ef362015-05-04 16:35:40 +0300217 .name = MP_QSTR_ustruct,
Paul Sokolovskye9db8402014-04-10 03:45:38 +0300218 .globals = (mp_obj_dict_t*)&mp_module_struct_globals,
219};
220
221#endif