|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2013, 2014 Damien P. George |
| 7 | + * Copyright (c) 2014 Paul Sokolovsky |
| 8 | + * Copyright (c) 2017 Michael McWethy |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <assert.h> |
| 30 | +#include <string.h> |
| 31 | + |
| 32 | +#include "py/runtime.h" |
| 33 | +#include "py/builtin.h" |
| 34 | +#include "py/objtuple.h" |
| 35 | +#include "py/binary.h" |
| 36 | +#include "py/parsenum.h" |
| 37 | +#include "shared-bindings/struct/__init__.h" |
| 38 | +#include "shared-module/struct/__init__.h" |
| 39 | + |
| 40 | +//| :mod:`struct` --- manipulation of c-style data |
| 41 | +//| ======================================================== |
| 42 | +//| |
| 43 | +//| .. module:: struct |
| 44 | +//| :synopsis: byte data control |
| 45 | +//| :platform: SAMD21 |
| 46 | +//| |
| 47 | +//| This module implements a subset of the corresponding CPython module, |
| 48 | +//| as described below. For more information, refer to the original CPython |
| 49 | +//| documentation: struct. |
| 50 | +//| |
| 51 | +//| Supported size/byte order prefixes: *@*, *<*, *>*, *!*. |
| 52 | +//| |
| 53 | +//| Supported format codes: *b*, *B*, *h*, *H*, *i*, *I*, *l*, *L*, *q*, *Q*, |
| 54 | +//| *s*, *P*, *f*, *d* (the latter 2 depending on the floating-point support). |
| 55 | + |
| 56 | + |
| 57 | + //| .. function:: calcsize(fmt) |
| 58 | + //| |
| 59 | + //| Return the number of bytes needed to store the given fmt. |
| 60 | + //| |
| 61 | + |
| 62 | +STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) { |
| 63 | + |
| 64 | + return MP_OBJ_NEW_SMALL_INT(shared_modules_struct_calcsize(fmt_in)); |
| 65 | +} |
| 66 | +MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize); |
| 67 | + |
| 68 | +//| .. function:: pack(fmt, v1, v2, ...) |
| 69 | +//| |
| 70 | +//| Pack the values v1, v2, ... according to the format string fmt. |
| 71 | +//| The return value is a bytes object encoding the values. |
| 72 | +//| |
| 73 | + |
| 74 | +STATIC mp_obj_t struct_pack(size_t n_args, const mp_obj_t *args) { |
| 75 | + // TODO: "The arguments must match the values required by the format exactly." |
| 76 | + mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0])); |
| 77 | + vstr_t vstr; |
| 78 | + vstr_init_len(&vstr, size); |
| 79 | + byte *p = (byte*)vstr.buf; |
| 80 | + memset(p, 0, size); |
| 81 | + byte *end_p = &p[size]; |
| 82 | + shared_modules_struct_pack_into(args[0], p, end_p, n_args - 1, &args[1]); |
| 83 | + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); |
| 84 | +} |
| 85 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_obj, 1, MP_OBJ_FUN_ARGS_MAX, struct_pack); |
| 86 | + |
| 87 | + |
| 88 | +//| .. function:: pack_into(fmt, buffer, offset, v1, v2, ...) |
| 89 | +//| |
| 90 | +//| Pack the values v1, v2, ... according to the format string fmt into a buffer |
| 91 | +//| starting at offset. offset may be negative to count from the end of buffer. |
| 92 | +//| |
| 93 | + |
| 94 | +STATIC mp_obj_t struct_pack_into(size_t n_args, const mp_obj_t *args) { |
| 95 | + mp_buffer_info_t bufinfo; |
| 96 | + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE); |
| 97 | + mp_int_t offset = mp_obj_get_int(args[2]); |
| 98 | + if (offset < 0) { |
| 99 | + // negative offsets are relative to the end of the buffer |
| 100 | + offset = (mp_int_t)bufinfo.len + offset; |
| 101 | + if (offset < 0) { |
| 102 | + mp_raise_RuntimeError("buffer too small"); |
| 103 | + } |
| 104 | + } |
| 105 | + byte *p = (byte *)bufinfo.buf; |
| 106 | + byte *end_p = &p[bufinfo.len]; |
| 107 | + p += offset; |
| 108 | + |
| 109 | + shared_modules_struct_pack_into(args[0], p, end_p, n_args - 3, &args[3]); |
| 110 | + return mp_const_none; |
| 111 | +} |
| 112 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_pack_into_obj, 3, MP_OBJ_FUN_ARGS_MAX, struct_pack_into); |
| 113 | + |
| 114 | +//| .. function:: unpack(fmt, data) |
| 115 | +//| |
| 116 | +//| Unpack from the data according to the format string fmt. The return value |
| 117 | +//| is a tuple of the unpacked values. |
| 118 | +//| |
| 119 | + |
| 120 | +//| .. function:: unpack_from(fmt, data, offset) |
| 121 | +//| |
| 122 | +//| Unpack from the data starting at offset according to the format string fmt. |
| 123 | +//| offset may be negative to count from the end of buffer. The return value is |
| 124 | +//| a tuple of the unpacked values. |
| 125 | +//| |
| 126 | + |
| 127 | +STATIC mp_obj_t struct_unpack_from(size_t n_args, const mp_obj_t *args) { |
| 128 | + // unpack requires that the buffer be exactly the right size. |
| 129 | + // unpack_from requires that the buffer be "big enough". |
| 130 | + // Since we implement unpack and unpack_from using the same function |
| 131 | + // we relax the "exact" requirement, and only implement "big enough". |
| 132 | + mp_buffer_info_t bufinfo; |
| 133 | + mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); |
| 134 | + byte *p = bufinfo.buf; |
| 135 | + byte *end_p = &p[bufinfo.len]; |
| 136 | + |
| 137 | + if (n_args > 2) { |
| 138 | + mp_int_t offset = mp_obj_get_int(args[2]); |
| 139 | + // offset arg provided |
| 140 | + if (offset < 0) { |
| 141 | + // negative offsets are relative to the end of the buffer |
| 142 | + offset = bufinfo.len + offset; |
| 143 | + if (offset < 0) { |
| 144 | + mp_raise_RuntimeError("buffer too small"); |
| 145 | + } |
| 146 | + } |
| 147 | + p += offset; |
| 148 | + } |
| 149 | + |
| 150 | + return MP_OBJ_FROM_PTR(shared_modules_struct_unpack_from(args[0] , p, end_p)); |
| 151 | +} |
| 152 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(struct_unpack_from_obj, 2, 3, struct_unpack_from); |
| 153 | + |
| 154 | +STATIC const mp_rom_map_elem_t mp_module_struct_globals_table[] = { |
| 155 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_struct) }, |
| 156 | + { MP_ROM_QSTR(MP_QSTR_calcsize), MP_ROM_PTR(&struct_calcsize_obj) }, |
| 157 | + { MP_ROM_QSTR(MP_QSTR_pack), MP_ROM_PTR(&struct_pack_obj) }, |
| 158 | + { MP_ROM_QSTR(MP_QSTR_pack_into), MP_ROM_PTR(&struct_pack_into_obj) }, |
| 159 | + { MP_ROM_QSTR(MP_QSTR_unpack), MP_ROM_PTR(&struct_unpack_from_obj) }, |
| 160 | + { MP_ROM_QSTR(MP_QSTR_unpack_from), MP_ROM_PTR(&struct_unpack_from_obj) }, |
| 161 | +}; |
| 162 | + |
| 163 | +STATIC MP_DEFINE_CONST_DICT(mp_module_struct_globals, mp_module_struct_globals_table); |
| 164 | + |
| 165 | +const mp_obj_module_t struct_module = { |
| 166 | + .base = { &mp_type_module }, |
| 167 | + .globals = (mp_obj_dict_t*)&mp_module_struct_globals, |
| 168 | +}; |
0 commit comments