blob: 12cd266870339531c5d129bd7cefcba4dc4f58d8 [file] [log] [blame]
Paul Sokolovskybfdc2052014-11-29 06:19:30 +02001/*
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 George3765ea42015-01-01 20:35:21 +000031#include "py/nlr.h"
32#include "py/runtime.h"
33#include "py/binary.h"
Daniel Campora0f716ac2015-05-21 13:59:23 +020034#include "modubinascii.h"
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020035
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020036
Daniel Campora0f716ac2015-05-21 13:59:23 +020037mp_obj_t mod_binascii_hexlify(mp_uint_t n_args, const mp_obj_t *args) {
Dave Hylands3ad94d62015-05-18 14:41:25 -070038 // Second argument is for an extension to allow a separator to be used
39 // between values.
Paul Sokolovsky7203b582015-12-26 02:12:15 +020040 const char *sep = NULL;
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020041 mp_buffer_info_t bufinfo;
42 mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
43
Damien George05005f62015-01-21 22:48:37 +000044 vstr_t vstr;
Paul Sokolovsky7203b582015-12-26 02:12:15 +020045 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 George05005f62015-01-21 22:48:37 +000052 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020053 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 Sokolovsky7203b582015-12-26 02:12:15 +020064 if (sep != NULL && i != 0) {
65 *out++ = *sep;
66 }
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020067 }
Damien George05005f62015-01-21 22:48:37 +000068 return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
Paul Sokolovskybfdc2052014-11-29 06:19:30 +020069}
70MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify);
71
Daniel Campora0f716ac2015-05-21 13:59:23 +020072mp_obj_t mod_binascii_unhexlify(mp_obj_t data) {
Dave Hylands3ad94d62015-05-18 14:41:25 -070073 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}
99MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj, mod_binascii_unhexlify);
100
Galen Hazelwood616986a2015-07-01 22:15:06 -0700101mp_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 George722d4842015-07-06 11:34:29 +0000132 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "incorrect padding"));
Galen Hazelwood616986a2015-07-01 22:15:06 -0700133 }
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}
151MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj, mod_binascii_a2b_base64);
152
153mp_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 Sokolovskye284a952015-07-04 12:36:46 +0300159
160 // First pass, we convert input buffer to numeric base 64 values
Galen Hazelwood616986a2015-07-01 22:15:06 -0700161 byte *in = bufinfo.buf, *out = (byte*)vstr.buf;
Paul Sokolovskye284a952015-07-04 12:36:46 +0300162 mp_uint_t i;
163 for (i = bufinfo.len; i >= 3; i -= 3) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700164 *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 Sokolovskye284a952015-07-04 12:36:46 +0300170 if (i != 0) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700171 *out++ = (in[0] & 0xFC) >> 2;
Paul Sokolovskye284a952015-07-04 12:36:46 +0300172 if (i == 2) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700173 *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 Sokolovskye284a952015-07-04 12:36:46 +0300182
183 // Second pass, we convert number base 64 values to actual base64 ascii encoding
Galen Hazelwood616986a2015-07-01 22:15:06 -0700184 out = (byte*)vstr.buf;
Paul Sokolovsky7370fd52015-07-04 13:13:10 +0300185 for (mp_uint_t j = vstr.len - 1; j--;) {
Galen Hazelwood616986a2015-07-01 22:15:06 -0700186 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}
204MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64);
205
Daniel Campora0f716ac2015-05-21 13:59:23 +0200206#if MICROPY_PY_UBINASCII
207
Damien Georgecbf76742015-11-27 13:38:15 +0000208STATIC 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 Sokolovskybfdc2052014-11-29 06:19:30 +0200214};
215
Damien George3b603f22014-11-29 14:39:27 +0000216STATIC MP_DEFINE_CONST_DICT(mp_module_binascii_globals, mp_module_binascii_globals_table);
Paul Sokolovskybfdc2052014-11-29 06:19:30 +0200217
218const 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