Skip to content

Commit bfdc205

Browse files
pfalcondpgeorge
authored andcommitted
modubinascii: Add, with hexlify() implementation.
1 parent d96e6b1 commit bfdc205

7 files changed

Lines changed: 103 additions & 0 deletions

File tree

extmod/modubinascii.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,4 @@ extern const mp_obj_module_t mp_module_ujson;
9595
extern const mp_obj_module_t mp_module_ure;
9696
extern const mp_obj_module_t mp_module_uheapq;
9797
extern const mp_obj_module_t mp_module_uhashlib;
98+
extern const mp_obj_module_t mp_module_ubinascii;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
224224
#if MICROPY_PY_UHASHLIB
225225
{ MP_OBJ_NEW_QSTR(MP_QSTR_uhashlib), (mp_obj_t)&mp_module_uhashlib },
226226
#endif
227+
#if MICROPY_PY_UBINASCII
228+
{ MP_OBJ_NEW_QSTR(MP_QSTR_ubinascii), (mp_obj_t)&mp_module_ubinascii },
229+
#endif
227230

228231
// extra builtin modules as defined by a port
229232
MICROPY_PORT_BUILTIN_MODULES

py/mpconfig.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@ typedef double mp_float_t;
439439
#define MICROPY_PY_UHASHLIB (0)
440440
#endif
441441

442+
#ifndef MICROPY_PY_UBINASCII
443+
#define MICROPY_PY_UBINASCII (0)
444+
#endif
445+
442446
/*****************************************************************************/
443447
/* Hooks for a port to add builtins */
444448

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ PY_O_BASENAME = \
117117
../extmod/moduzlib.o \
118118
../extmod/moduheapq.o \
119119
../extmod/moduhashlib.o \
120+
../extmod/modubinascii.o \
120121

121122
# prepend the build destination prefix to the py object files
122123
PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))

py/qstrdefs.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,8 @@ Q(digest)
518518
Q(hexdigest)
519519
Q(sha256)
520520
#endif
521+
522+
#if MICROPY_PY_UBINASCII
523+
Q(ubinascii)
524+
Q(hexlify)
525+
#endif

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#define MICROPY_PY_URE (1)
6363
#define MICROPY_PY_UHEAPQ (1)
6464
#define MICROPY_PY_UHASHLIB (1)
65+
#define MICROPY_PY_UBINASCII (1)
6566

6667
// Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
6768
// names in exception messages (may require more RAM).

0 commit comments

Comments
 (0)