blob: d79191b3e5d5d919c3575511bac57357f0a62d70 [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/nlr.h"
32#include "py/runtime.h"
33#include "py/binary.h"
Damien Georgef9dc6442016-05-20 12:43:32 +010034#include "extmod/modubinascii.h"
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020035
Pavol Rusnak39799f72016-07-18 18:35:33 +020036#include "uzlib/tinf.h"
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020037
Damien George4b72b3a2016-01-03 14:21:40 +000038mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args) {
Dave Hylands3ad94d62015-05-18 14:41:25 -070039 // Second argument is for an extension to allow a separator to be used
40 // between values.
Paul Sokolovsky7203b582015-12-26 02:12:15 +020041 const char *sep = NULL;
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020042 mp_buffer_info_t bufinfo;
43 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
44
Damien Georgeb86c65d2017-07-03 14:52:00 +100045 // Code below assumes non-zero buffer length when computing size with
46 // separator, so handle the zero-length case here.
47 if (bufinfo.len == 0) {
48 return mp_const_empty_bytes;
49 }
50
Damien George05005f62015-01-21 22:48:37 +000051 vstr_t vstr;
Paul Sokolovsky7203b582015-12-26 02:12:15 +020052 size_t out_len = bufinfo.len * 2;
53 if (n_args > 1) {
54 // 1-char separator between hex numbers
55 out_len += bufinfo.len - 1;
56 sep = mp_obj_str_get_str(args[1]);
57 }
58 vstr_init_len(&vstr, out_len);
Damien George05005f62015-01-21 22:48:37 +000059 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020060 for (mp_uint_t i = bufinfo.len; i--;) {
61 byte d = (*in >> 4);
62 if (d > 9) {
63 d += 'a' - '9' - 1;
64 }
65 *out++ = d + '0';
66 d = (*in++ & 0xf);
67 if (d > 9) {
68 d += 'a' - '9' - 1;
69 }
70 *out++ = d + '0';
Paul Sokolovsky7203b582015-12-26 02:12:15 +020071 if (sep != NULL && i != 0) {
72 *out++ = *sep;
73 }
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020074 }
Damien George05005f62015-01-21 22:48:37 +000075 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020076}
77MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify);
78
Daniel Campora0f716ac2015-05-21 13:59:23 +020079mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
Dave Hylands3ad94d62015-05-18 14:41:25 -070080 mp_buffer_info_t bufinfo;
81 mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
82
83 if ((bufinfo.len & 1) != 0) {
Damien George48d867b2017-06-15 11:54:41 +100084 mp_raise_ValueError("odd-length string");
Dave Hylands3ad94d62015-05-18 14:41:25 -070085 }
86 vstr_t vstr;
87 vstr_init_len(&vstr, bufinfo.len / 2);
88 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
89 byte hex_byte = 0;
90 for (mp_uint_t i = bufinfo.len; i--;) {
91 byte hex_ch = *in++;
92 if (unichar_isxdigit(hex_ch)) {
93 hex_byte += unichar_xdigit_value(hex_ch);
94 } else {
Damien George48d867b2017-06-15 11:54:41 +100095 mp_raise_ValueError("non-hex digit found");
Dave Hylands3ad94d62015-05-18 14:41:25 -070096 }
97 if (i & 1) {
98 hex_byte <<= 4;
99 } else {
100 *out++ = hex_byte;
101 hex_byte = 0;
102 }
103 }
104 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
105}
106MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj, mod_binascii_unhexlify);
107
Galen Hazelwood616986a2015-07-01 22:15:06 -0700108mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) {
109 mp_buffer_info_t bufinfo;
110 mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
111 if (bufinfo.len % 4 != 0) {
112 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incorrect padding"));
113 }
114
115 vstr_t vstr;
116 byte *in = bufinfo.buf;
117 if (bufinfo.len == 0) {
118 vstr_init_len(&vstr, 0);
119 }
120 else {
Damien George761e4c72017-07-19 13:12:10 +1000121 vstr_init_len(&vstr, ((bufinfo.len / 4) * 3) - ((in[bufinfo.len-1] == '=') ? ((in[bufinfo.len-2] == '=') ? 2 : 1 ) : 0));
Galen Hazelwood616986a2015-07-01 22:15:06 -0700122 }
123 byte *out = (byte*)vstr.buf;
124 for (mp_uint_t i = bufinfo.len; i; i -= 4) {
125 char hold[4];
126 for (int j = 4; j--;) {
127 if (in[j] >= 'A' && in[j] <= 'Z') {
128 hold[j] = in[j] - 'A';
129 } else if (in[j] >= 'a' && in[j] <= 'z') {
130 hold[j] = in[j] - 'a' + 26;
131 } else if (in[j] >= '0' && in[j] <= '9') {
132 hold[j] = in[j] - '0' + 52;
133 } else if (in[j] == '+') {
134 hold[j] = 62;
135 } else if (in[j] == '/') {
136 hold[j] = 63;
137 } else if (in[j] == '=') {
138 if (j < 2 || i > 4) {
Damien George722d4842015-07-06 11:34:29 +0000139 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incorrect padding"));
Galen Hazelwood616986a2015-07-01 22:15:06 -0700140 }
141 hold[j] = 64;
142 } else {
143 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid character"));
144 }
145 }
146 in += 4;
147
148 *out++ = (hold[0]) << 2 | (hold[1]) >> 4;
149 if (hold[2] != 64) {
150 *out++ = (hold[1] & 0x0F) << 4 | hold[2] >> 2;
151 if (hold[3] != 64) {
152 *out++ = (hold[2] & 0x03) << 6 | hold[3];
153 }
154 }
155 }
156 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
157}
158MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj, mod_binascii_a2b_base64);
159
160mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) {
161 mp_buffer_info_t bufinfo;
162 mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ);
163
164 vstr_t vstr;
165 vstr_init_len(&vstr, ((bufinfo.len != 0) ? (((bufinfo.len - 1) / 3) + 1) * 4 : 0) + 1);
Paul Sokolovskye284a952015-07-04 12:36:46 +0300166
167 // First pass, we convert input buffer to numeric base 64 values
Galen Hazelwood616986a2015-07-01 22:15:06 -0700168 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
Paul Sokolovskye284a952015-07-04 12:36:46 +0300169 mp_uint_t i;
170 for (i = bufinfo.len; i >= 3; i -= 3) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700171 *out++ = (in[0] & 0xFC) >> 2;
172 *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4;
173 *out++ = (in[1] & 0x0F) << 2 | (in[2] & 0xC0) >> 6;
174 *out++ = in[2] & 0x3F;
175 in += 3;
176 }
Paul Sokolovskye284a952015-07-04 12:36:46 +0300177 if (i != 0) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700178 *out++ = (in[0] & 0xFC) >> 2;
Paul Sokolovskye284a952015-07-04 12:36:46 +0300179 if (i == 2) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700180 *out++ = (in[0] & 0x03) << 4 | (in[1] & 0xF0) >> 4;
181 *out++ = (in[1] & 0x0F) << 2;
182 }
183 else {
184 *out++ = (in[0] & 0x03) << 4;
185 *out++ = 64;
186 }
187 *out++ = 64;
188 }
Paul Sokolovskye284a952015-07-04 12:36:46 +0300189
190 // Second pass, we convert number base 64 values to actual base64 ascii encoding
Galen Hazelwood616986a2015-07-01 22:15:06 -0700191 out = (byte*)vstr.buf;
Paul Sokolovsky7370fd52015-07-04 13:13:10 +0300192 for (mp_uint_t j = vstr.len - 1; j--;) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700193 if (*out < 26) {
194 *out += 'A';
195 } else if (*out < 52) {
196 *out += 'a' - 26;
197 } else if (*out < 62) {
198 *out += '0' - 52;
199 } else if (*out == 62) {
200 *out ='+';
201 } else if (*out == 63) {
202 *out = '/';
203 } else {
204 *out = '=';
205 }
206 out++;
207 }
208 *out = '\n';
209 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
210}
211MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64);
212
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300213#if MICROPY_PY_UBINASCII_CRC32
Pavol Rusnak39799f72016-07-18 18:35:33 +0200214mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args) {
215 mp_buffer_info_t bufinfo;
216 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
Pavol Rusnak7f5a5412016-09-21 17:19:48 +0200217 uint32_t crc = (n_args > 1) ? mp_obj_get_int_truncated(args[1]) : 0;
Pavol Rusnak39799f72016-07-18 18:35:33 +0200218 crc = uzlib_crc32(bufinfo.buf, bufinfo.len, crc ^ 0xffffffff);
Pavol Rusnak7f5a5412016-09-21 17:19:48 +0200219 return mp_obj_new_int_from_uint(crc ^ 0xffffffff);
Pavol Rusnak39799f72016-07-18 18:35:33 +0200220}
221MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj, 1, 2, mod_binascii_crc32);
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300222#endif
Pavol Rusnak39799f72016-07-18 18:35:33 +0200223
Daniel Campora0f716ac2015-05-21 13:59:23 +0200224#if MICROPY_PY_UBINASCII
225
Damien Georgecbf76742015-11-27 13:38:15 +0000226STATIC const mp_rom_map_elem_t mp_module_binascii_globals_table[] = {
227 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubinascii) },
228 { MP_ROM_QSTR(MP_QSTR_hexlify), MP_ROM_PTR(&mod_binascii_hexlify_obj) },
229 { MP_ROM_QSTR(MP_QSTR_unhexlify), MP_ROM_PTR(&mod_binascii_unhexlify_obj) },
230 { MP_ROM_QSTR(MP_QSTR_a2b_base64), MP_ROM_PTR(&mod_binascii_a2b_base64_obj) },
231 { MP_ROM_QSTR(MP_QSTR_b2a_base64), MP_ROM_PTR(&mod_binascii_b2a_base64_obj) },
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300232 #if MICROPY_PY_UBINASCII_CRC32
Pavol Rusnak39799f72016-07-18 18:35:33 +0200233 { MP_ROM_QSTR(MP_QSTR_crc32), MP_ROM_PTR(&mod_binascii_crc32_obj) },
Paul Sokolovskyc4283672016-08-24 18:28:43 +0300234 #endif
Paul Sokolovskybfdc2052014-11-29 06:19:30 +0200235};
236
Damien George3b603f22014-11-29 14:39:27 +0000237STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);
Paul Sokolovskybfdc2052014-11-29 06:19:30 +0200238
239const mp_obj_module_t mp_module_ubinascii = {
240 .base = { &mp_type_module },
Paul Sokolovskybfdc2052014-11-29 06:19:30 +0200241 .globals = (mp_obj_dict_t*)&mp_module_binascii_globals,
242};
243
244#endif //MICROPY_PY_UBINASCII