blob: 8256a50cf2042935843eeba8acfc5c44f4528486 [file] [log] [blame]
Paul Sokolovskybfdc2052014-11-29 06:19:30 +02001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Paul Sokolovskybfdc2052014-11-29 06:19:30 +02003 *
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 George3765ea42015-01-01 20:35:21 +000031#include "py/runtime.h"
32#include "py/binary.h"
Damien Georgef9dc6442016-05-20 12:43:32 +010033#include "extmod/modubinascii.h"
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020034
Damien George4b72b3a2016-01-03 14:21:40 +000035mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
Dave Hylands3ad94d62015-05-18 14:41:25 -070036 // Second argument is for an extension to allow a separator to be used
37 // between values.
Paul Sokolovsky7203b582015-12-26 02:12:15 +020038 const char *sep = NULL;
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020039 mp_buffer_info_t bufinfo;
40 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
41
Damien Georgeb86c65d2017-07-03 14:52:00 +100042 // Code below assumes non-zero buffer length when computing size with
43 // separator, so handle the zero-length case here.
44 if (bufinfo.len == 0) {
45 return mp_const_empty_bytes;
46 }
47
Damien George05005f62015-01-21 22:48:37 +000048 vstr_t vstr;
Paul Sokolovsky7203b582015-12-26 02:12:15 +020049 size_t out_len = bufinfo.len * 2;
50 if (n_args > 1) {
51 // 1-char separator between hex numbers
52 out_len += bufinfo.len - 1;
53 sep = mp_obj_str_get_str(args[1]);
54 }
55 vstr_init_len(&vstr, out_len);
Damien George05005f62015-01-21 22:48:37 +000056 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020057 for (mp_uint_t i = bufinfo.len; i--;) {
58 byte d = (*in >> 4);
59 if (d > 9) {
60 d += 'a' - '9' - 1;
61 }
62 *out++ = d + '0';
63 d = (*in++ & 0xf);
64 if (d > 9) {
65 d += 'a' - '9' - 1;
66 }
67 *out++ = d + '0';
Paul Sokolovsky7203b582015-12-26 02:12:15 +020068 if (sep != NULL && i != 0) {
69 *out++ = *sep;
70 }
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020071 }
Damien George05005f62015-01-21 22:48:37 +000072 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020073}
74MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify);
75
Daniel Campora0f716ac2015-05-21 13:59:23 +020076mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
Dave Hylands3ad94d62015-05-18 14:41:25 -070077 mp_buffer_info_t bufinfo;
78 mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
79
80 if ((bufinfo.len & 1) != 0) {
Damien George48d867b2017-06-15 11:54:41 +100081 mp_raise_ValueError("odd-length string");
Dave Hylands3ad94d62015-05-18 14:41:25 -070082 }
83 vstr_t vstr;
84 vstr_init_len(&vstr, bufinfo.len / 2);
85 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
86 byte hex_byte = 0;
87 for (mp_uint_t i = bufinfo.len; i--;) {
88 byte hex_ch = *in++;
89 if (unichar_isxdigit(hex_ch)) {
90 hex_byte += unichar_xdigit_value(hex_ch);
91 } else {
Damien George48d867b2017-06-15 11:54:41 +100092 mp_raise_ValueError("non-hex digit found");
Dave Hylands3ad94d62015-05-18 14:41:25 -070093 }
94 if (i & 1) {
95 hex_byte <<= 4;
96 } else {
97 *out++ = hex_byte;
98 hex_byte = 0;
99 }
100 }
101 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
102}
103MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj, mod_binascii_unhexlify);
104
Alex Robbinsc89254f2017-08-04 17:30:34 -0500105// If ch is a character in the base64 alphabet, and is not a pad character, then
106// the corresponding integer between 0 and 63, inclusively, is returned.
107// Otherwise, -1 is returned.
108static int mod_binascii_sextet(byte ch) {
109 if (ch >= 'A' && ch <= 'Z') {
110 return ch - 'A';
111 } else if (ch >= 'a' && ch <= 'z') {
112 return ch - 'a' + 26;
113 } else if (ch >= '0' && ch <= '9') {
114 return ch - '0' + 52;
115 } else if (ch == '+') {
116 return 62;
117 } else if (ch == '/') {
118 return 63;
119 } else {
120 return -1;
121 }
122}
123
Galen Hazelwood616986a2015-07-01 22:15:06 -0700124mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) {
125 mp_buffer_info_t bufinfo;
126 mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
Alex Robbinsc89254f2017-08-04 17:30:34 -0500127 byte *in = bufinfo.buf;
128
129 vstr_t vstr;
130 vstr_init(&vstr, (bufinfo.len / 4) * 3 + 1); // Potentially over-allocate
131 byte *out = (byte *)vstr.buf;
132
133 uint shift = 0;
134 int nbits = 0; // Number of meaningful bits in shift
135 bool hadpad = false; // Had a pad character since last valid character
136 for (size_t i = 0; i < bufinfo.len; i++) {
137 if (in[i] == '=') {
138 if ((nbits == 2) || ((nbits == 4) && hadpad)) {
139 nbits = 0;
140 break;
141 }
142 hadpad = true;
143 }
144
145 int sextet = mod_binascii_sextet(in[i]);
146 if (sextet == -1) {
147 continue;
148 }
149 hadpad = false;
150 shift = (shift << 6) | sextet;
151 nbits += 6;
152
153 if (nbits >= 8) {
154 nbits -= 8;
155 out[vstr.len++] = (shift >> nbits) & 0xFF;
156 }
157 }
158
159 if (nbits) {
Javier Candeira35a1fea2017-08-09 14:40:45 +1000160 mp_raise_ValueError("incorrect padding");
Galen Hazelwood616986a2015-07-01 22:15:06 -0700161 }
162
Galen Hazelwood616986a2015-07-01 22:15:06 -0700163 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
164}
165MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj, mod_binascii_a2b_base64);
166
167mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) {
168 mp_buffer_info_t bufinfo;
169 mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
170
171 vstr_t vstr;
172 vstr_init_len(&vstr, ((bufinfo.len != 0) ? (((bufinfo.len - 1) / 3) + 1) * 4 : 0) + 1);
Paul Sokolovskye284a952015-07-04 12:36:46 +0300173
174 // First pass, we convert input buffer to numeric base 64 values
Galen Hazelwood616986a2015-07-01 22:15:06 -0700175 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
Paul Sokolovskye284a952015-07-04 12:36:46 +0300176 mp_uint_t i;
177 for (i = bufinfo.len; i >= 3; i -= 3) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700178 *out++ = (in[0] & 0xFC) >> 2;
179 *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4;
180 *out++ = (in[1] & 0x0F) << 2 | (in[2] & 0xC0) >> 6;
181 *out++ = in[2] & 0x3F;
182 in += 3;
183 }
Paul Sokolovskye284a952015-07-04 12:36:46 +0300184 if (i != 0) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700185 *out++ = (in[0] & 0xFC) >> 2;
Paul Sokolovskye284a952015-07-04 12:36:46 +0300186 if (i == 2) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700187 *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4;
188 *out++ = (in[1] & 0x0F) << 2;
189 }
190 else {
191 *out++ = (in[0] & 0x03) << 4;
192 *out++ = 64;
193 }
Damien George1c6b4422017-08-21 22:05:09 +1000194 *out = 64;
Galen Hazelwood616986a2015-07-01 22:15:06 -0700195 }
Paul Sokolovskye284a952015-07-04 12:36:46 +0300196
197 // Second pass, we convert number base 64 values to actual base64 ascii encoding
Galen Hazelwood616986a2015-07-01 22:15:06 -0700198 out = (byte*)vstr.buf;
Paul Sokolovsky7370fd52015-07-04 13:13:10 +0300199 for (mp_uint_t j = vstr.len - 1; j--;) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700200 if (*out < 26) {
201 *out += 'A';
202 } else if (*out < 52) {
203 *out += 'a' - 26;
204 } else if (*out < 62) {
205 *out += '0' - 52;
206 } else if (*out == 62) {
207 *out ='+';
208 } else if (*out == 63) {
209 *out = '/';
210 } else {
211 *out = '=';
212 }
213 out++;
214 }
215 *out = '\n';
216 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
217}
218MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64);
219
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300220#if MICROPY_PY_UBINASCII_CRC32
Damien George09547f02017-08-31 14:10:49 +1000221#include "uzlib/tinf.h"
222
Pavol Rusnak39799f72016-07-18 18:35:33 +0200223mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) {
224 mp_buffer_info_t bufinfo;
225 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
Pavol Rusnak7f5a5412016-09-21 17:19:48 +0200226 uint32_t crc = (n_args > 1) ? mp_obj_get_int_truncated(args[1]) : 0;
Pavol Rusnak39799f72016-07-18 18:35:33 +0200227 crc = uzlib_crc32(bufinfo.buf, bufinfo.len, crc ^ 0xffffffff);
Pavol Rusnak7f5a5412016-09-21 17:19:48 +0200228 return mp_obj_new_int_from_uint(crc ^ 0xffffffff);
Pavol Rusnak39799f72016-07-18 18:35:33 +0200229}
230MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_binascii_crc32);
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300231#endif
Pavol Rusnak39799f72016-07-18 18:35:33 +0200232
Daniel Campora0f716ac2015-05-21 13:59:23 +0200233#if MICROPY_PY_UBINASCII
234
Damien Georgecbf76742015-11-27 13:38:15 +0000235STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = {
236 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubinascii) },
237 { MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) },
238 { MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) },
239 { MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) },
240 { MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) },
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300241 #if MICROPY_PY_UBINASCII_CRC32
Pavol Rusnak39799f72016-07-18 18:35:33 +0200242 { MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&mod_binascii_crc32_obj) },
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300243 #endif
Paul Sokolovskybfdc2052014-11-29 06:19:30 +0200244};
245
Damien George3b603f22014-11-29 14:39:27 +0000246STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);
Paul Sokolovskybfdc2052014-11-29 06:19:30 +0200247
248const mp_obj_module_t mp_module_ubinascii = {
249 .base = { &mp_type_module },
Paul Sokolovskybfdc2052014-11-29 06:19:30 +0200250 .globals = (mp_obj_dict_t*)&mp_module_binascii_globals,
251};
252
253#endif //MICROPY_PY_UBINASCII