Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 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 | |
Damien George | 3765ea4 | 2015-01-01 20:35:21 +0000 | [diff] [blame] | 31 | #include "py/nlr.h" |
| 32 | #include "py/runtime.h" |
| 33 | #include "py/binary.h" |
Daniel Campora | 0f716ac | 2015-05-21 13:59:23 +0200 | [diff] [blame] | 34 | #include "modubinascii.h" |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 35 | |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 36 | |
Daniel Campora | 0f716ac | 2015-05-21 13:59:23 +0200 | [diff] [blame] | 37 | mp_obj_t mod_binascii_hexlify(mp_uint_t n_args, const mp_obj_t *args) { |
Dave Hylands | 3ad94d6 | 2015-05-18 14:41:25 -0700 | [diff] [blame] | 38 | // Second argument is for an extension to allow a separator to be used |
| 39 | // between values. |
Paul Sokolovsky | 7203b58 | 2015-12-26 02:12:15 +0200 | [diff] [blame^] | 40 | const char *sep = NULL; |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 41 | mp_buffer_info_t bufinfo; |
| 42 | mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); |
| 43 | |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 44 | vstr_t vstr; |
Paul Sokolovsky | 7203b58 | 2015-12-26 02:12:15 +0200 | [diff] [blame^] | 45 | size_t out_len = bufinfo.len * 2; |
| 46 | if (n_args > 1) { |
| 47 | // 1-char separator between hex numbers |
| 48 | out_len += bufinfo.len - 1; |
| 49 | sep = mp_obj_str_get_str(args[1]); |
| 50 | } |
| 51 | vstr_init_len(&vstr, out_len); |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 52 | byte *in = bufinfo.buf, *out = (byte*)vstr.buf; |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 53 | for (mp_uint_t i = bufinfo.len; i--;) { |
| 54 | byte d = (*in >> 4); |
| 55 | if (d > 9) { |
| 56 | d += 'a' - '9' - 1; |
| 57 | } |
| 58 | *out++ = d + '0'; |
| 59 | d = (*in++ & 0xf); |
| 60 | if (d > 9) { |
| 61 | d += 'a' - '9' - 1; |
| 62 | } |
| 63 | *out++ = d + '0'; |
Paul Sokolovsky | 7203b58 | 2015-12-26 02:12:15 +0200 | [diff] [blame^] | 64 | if (sep != NULL && i != 0) { |
| 65 | *out++ = *sep; |
| 66 | } |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 67 | } |
Damien George | 05005f6 | 2015-01-21 22:48:37 +0000 | [diff] [blame] | 68 | return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 69 | } |
| 70 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify); |
| 71 | |
Daniel Campora | 0f716ac | 2015-05-21 13:59:23 +0200 | [diff] [blame] | 72 | mp_obj_t mod_binascii_unhexlify(mp_obj_t data) { |
Dave Hylands | 3ad94d6 | 2015-05-18 14:41:25 -0700 | [diff] [blame] | 73 | mp_buffer_info_t bufinfo; |
| 74 | mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); |
| 75 | |
| 76 | if ((bufinfo.len & 1) != 0) { |
| 77 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "odd-length string")); |
| 78 | } |
| 79 | vstr_t vstr; |
| 80 | vstr_init_len(&vstr, bufinfo.len / 2); |
| 81 | byte *in = bufinfo.buf, *out = (byte*)vstr.buf; |
| 82 | byte hex_byte = 0; |
| 83 | for (mp_uint_t i = bufinfo.len; i--;) { |
| 84 | byte hex_ch = *in++; |
| 85 | if (unichar_isxdigit(hex_ch)) { |
| 86 | hex_byte += unichar_xdigit_value(hex_ch); |
| 87 | } else { |
| 88 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "non-hex digit found")); |
| 89 | } |
| 90 | if (i & 1) { |
| 91 | hex_byte <<= 4; |
| 92 | } else { |
| 93 | *out++ = hex_byte; |
| 94 | hex_byte = 0; |
| 95 | } |
| 96 | } |
| 97 | return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); |
| 98 | } |
| 99 | MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj, mod_binascii_unhexlify); |
| 100 | |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 101 | mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) { |
| 102 | mp_buffer_info_t bufinfo; |
| 103 | mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); |
| 104 | if (bufinfo.len % 4 != 0) { |
| 105 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incorrect padding")); |
| 106 | } |
| 107 | |
| 108 | vstr_t vstr; |
| 109 | byte *in = bufinfo.buf; |
| 110 | if (bufinfo.len == 0) { |
| 111 | vstr_init_len(&vstr, 0); |
| 112 | } |
| 113 | else { |
| 114 | vstr_init_len(&vstr, ((bufinfo.len / 4) * 3) - ((in[bufinfo.len-1] == '=') ? ((in[bufinfo.len-2] == '=') ? 2 : 1 ) : 0)); |
| 115 | } |
| 116 | byte *out = (byte*)vstr.buf; |
| 117 | for (mp_uint_t i = bufinfo.len; i; i -= 4) { |
| 118 | char hold[4]; |
| 119 | for (int j = 4; j--;) { |
| 120 | if (in[j] >= 'A' && in[j] <= 'Z') { |
| 121 | hold[j] = in[j] - 'A'; |
| 122 | } else if (in[j] >= 'a' && in[j] <= 'z') { |
| 123 | hold[j] = in[j] - 'a' + 26; |
| 124 | } else if (in[j] >= '0' && in[j] <= '9') { |
| 125 | hold[j] = in[j] - '0' + 52; |
| 126 | } else if (in[j] == '+') { |
| 127 | hold[j] = 62; |
| 128 | } else if (in[j] == '/') { |
| 129 | hold[j] = 63; |
| 130 | } else if (in[j] == '=') { |
| 131 | if (j < 2 || i > 4) { |
Damien George | 722d484 | 2015-07-06 11:34:29 +0000 | [diff] [blame] | 132 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incorrect padding")); |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 133 | } |
| 134 | hold[j] = 64; |
| 135 | } else { |
| 136 | nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid character")); |
| 137 | } |
| 138 | } |
| 139 | in += 4; |
| 140 | |
| 141 | *out++ = (hold[0]) << 2 | (hold[1]) >> 4; |
| 142 | if (hold[2] != 64) { |
| 143 | *out++ = (hold[1] & 0x0F) << 4 | hold[2] >> 2; |
| 144 | if (hold[3] != 64) { |
| 145 | *out++ = (hold[2] & 0x03) << 6 | hold[3]; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); |
| 150 | } |
| 151 | MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj, mod_binascii_a2b_base64); |
| 152 | |
| 153 | mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) { |
| 154 | mp_buffer_info_t bufinfo; |
| 155 | mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); |
| 156 | |
| 157 | vstr_t vstr; |
| 158 | vstr_init_len(&vstr, ((bufinfo.len != 0) ? (((bufinfo.len - 1) / 3) + 1) * 4 : 0) + 1); |
Paul Sokolovsky | e284a95 | 2015-07-04 12:36:46 +0300 | [diff] [blame] | 159 | |
| 160 | // First pass, we convert input buffer to numeric base 64 values |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 161 | byte *in = bufinfo.buf, *out = (byte*)vstr.buf; |
Paul Sokolovsky | e284a95 | 2015-07-04 12:36:46 +0300 | [diff] [blame] | 162 | mp_uint_t i; |
| 163 | for (i = bufinfo.len; i >= 3; i -= 3) { |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 164 | *out++ = (in[0] & 0xFC) >> 2; |
| 165 | *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4; |
| 166 | *out++ = (in[1] & 0x0F) << 2 | (in[2] & 0xC0) >> 6; |
| 167 | *out++ = in[2] & 0x3F; |
| 168 | in += 3; |
| 169 | } |
Paul Sokolovsky | e284a95 | 2015-07-04 12:36:46 +0300 | [diff] [blame] | 170 | if (i != 0) { |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 171 | *out++ = (in[0] & 0xFC) >> 2; |
Paul Sokolovsky | e284a95 | 2015-07-04 12:36:46 +0300 | [diff] [blame] | 172 | if (i == 2) { |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 173 | *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4; |
| 174 | *out++ = (in[1] & 0x0F) << 2; |
| 175 | } |
| 176 | else { |
| 177 | *out++ = (in[0] & 0x03) << 4; |
| 178 | *out++ = 64; |
| 179 | } |
| 180 | *out++ = 64; |
| 181 | } |
Paul Sokolovsky | e284a95 | 2015-07-04 12:36:46 +0300 | [diff] [blame] | 182 | |
| 183 | // Second pass, we convert number base 64 values to actual base64 ascii encoding |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 184 | out = (byte*)vstr.buf; |
Paul Sokolovsky | 7370fd5 | 2015-07-04 13:13:10 +0300 | [diff] [blame] | 185 | for (mp_uint_t j = vstr.len - 1; j--;) { |
Galen Hazelwood | 616986a | 2015-07-01 22:15:06 -0700 | [diff] [blame] | 186 | if (*out < 26) { |
| 187 | *out += 'A'; |
| 188 | } else if (*out < 52) { |
| 189 | *out += 'a' - 26; |
| 190 | } else if (*out < 62) { |
| 191 | *out += '0' - 52; |
| 192 | } else if (*out == 62) { |
| 193 | *out ='+'; |
| 194 | } else if (*out == 63) { |
| 195 | *out = '/'; |
| 196 | } else { |
| 197 | *out = '='; |
| 198 | } |
| 199 | out++; |
| 200 | } |
| 201 | *out = '\n'; |
| 202 | return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); |
| 203 | } |
| 204 | MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64); |
| 205 | |
Daniel Campora | 0f716ac | 2015-05-21 13:59:23 +0200 | [diff] [blame] | 206 | #if MICROPY_PY_UBINASCII |
| 207 | |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 208 | STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = { |
| 209 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubinascii) }, |
| 210 | { MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) }, |
| 211 | { MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) }, |
| 212 | { MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) }, |
| 213 | { MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) }, |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 214 | }; |
| 215 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame] | 216 | STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table); |
Paul Sokolovsky | bfdc205 | 2014-11-29 06:19:30 +0200 | [diff] [blame] | 217 | |
| 218 | const mp_obj_module_t mp_module_ubinascii = { |
| 219 | .base = { &mp_type_module }, |
| 220 | .name = MP_QSTR_ubinascii, |
| 221 | .globals = (mp_obj_dict_t*)&mp_module_binascii_globals, |
| 222 | }; |
| 223 | |
| 224 | #endif //MICROPY_PY_UBINASCII |