|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2014 Paul Sokolovsky |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdio.h> |
| 28 | +#include <assert.h> |
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "mpconfig.h" |
| 32 | +#include "nlr.h" |
| 33 | +#include "misc.h" |
| 34 | +#include "qstr.h" |
| 35 | +#include "obj.h" |
| 36 | +#include "runtime.h" |
| 37 | +#include "binary.h" |
| 38 | + |
| 39 | +#if MICROPY_PY_UBINASCII |
| 40 | + |
| 41 | +STATIC mp_obj_t mod_binascii_hexlify(mp_uint_t n_args, const mp_obj_t *args) { |
| 42 | + mp_buffer_info_t bufinfo; |
| 43 | + mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
| 44 | + |
| 45 | + byte *in = bufinfo.buf, *out; |
| 46 | + mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, bufinfo.len * 2, &out); |
| 47 | + for (mp_uint_t i = bufinfo.len; i--;) { |
| 48 | + byte d = (*in >> 4); |
| 49 | + if (d > 9) { |
| 50 | + d += 'a' - '9' - 1; |
| 51 | + } |
| 52 | + *out++ = d + '0'; |
| 53 | + d = (*in++ & 0xf); |
| 54 | + if (d > 9) { |
| 55 | + d += 'a' - '9' - 1; |
| 56 | + } |
| 57 | + *out++ = d + '0'; |
| 58 | + } |
| 59 | + return mp_obj_str_builder_end(o); |
| 60 | +} |
| 61 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify); |
| 62 | + |
| 63 | +STATIC const mp_map_elem_t mp_module_binascii_globals_table[] = { |
| 64 | + { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ubinascii) }, |
| 65 | + { MP_OBJ_NEW_QSTR(MP_QSTR_hexlify), (mp_obj_t)&mod_binascii_hexlify_obj }, |
| 66 | +// { MP_OBJ_NEW_QSTR(MP_QSTR_unhexlify), (mp_obj_t)&mod_binascii_unhexlify_obj }, |
| 67 | +// { MP_OBJ_NEW_QSTR(MP_QSTR_a2b_base64), (mp_obj_t)&mod_binascii_a2b_base64_obj }, |
| 68 | +// { MP_OBJ_NEW_QSTR(MP_QSTR_b2a_base64), (mp_obj_t)&mod_binascii_b2a_base64_obj }, |
| 69 | +}; |
| 70 | + |
| 71 | +STATIC const mp_obj_dict_t mp_module_binascii_globals = { |
| 72 | + .base = {&mp_type_dict}, |
| 73 | + .map = { |
| 74 | + .all_keys_are_qstrs = 1, |
| 75 | + .table_is_fixed_array = 1, |
| 76 | + .used = MP_ARRAY_SIZE(mp_module_binascii_globals_table), |
| 77 | + .alloc = MP_ARRAY_SIZE(mp_module_binascii_globals_table), |
| 78 | + .table = (mp_map_elem_t*)mp_module_binascii_globals_table, |
| 79 | + }, |
| 80 | +}; |
| 81 | + |
| 82 | +const mp_obj_module_t mp_module_ubinascii = { |
| 83 | + .base = { &mp_type_module }, |
| 84 | + .name = MP_QSTR_ubinascii, |
| 85 | + .globals = (mp_obj_dict_t*)&mp_module_binascii_globals, |
| 86 | +}; |
| 87 | + |
| 88 | +#endif //MICROPY_PY_UBINASCII |
0 commit comments