From 2e3a2785cdd5c63788cac7ec8b5d62105ecfe4aa Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 15 Jan 2022 15:19:59 +0100 Subject: [PATCH] extmod/modubinascii: Add newline keyword to b2a_base64 function. This allows encoding things (eg a Basic-Auth header for a request) without slicing the \n from the string, which allocates additional memory. Co-authored-by: David Lechner --- docs/library/binascii.rst | 4 ++-- extmod/modubinascii.c | 22 ++++++++++++++++------ tests/extmod/ubinascii_b2a_base64.py | 3 +++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/library/binascii.rst b/docs/library/binascii.rst index fc621a8abe..6c02f019aa 100644 --- a/docs/library/binascii.rst +++ b/docs/library/binascii.rst @@ -31,8 +31,8 @@ Functions Conforms to `RFC 2045 s.6.8 `_. Returns a bytes object. -.. function:: b2a_base64(data) +.. function:: b2a_base64(data, *, newline=True) Encode binary data in base64 format, as in `RFC 3548 `_. Returns the encoded data - followed by a newline character, as a bytes object. + followed by a newline character if newline is true, as a bytes object. diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c index fab7717c4e..b34f232c62 100644 --- a/extmod/modubinascii.c +++ b/extmod/modubinascii.c @@ -165,12 +165,20 @@ STATIC mp_obj_t mod_binascii_a2b_base64(mp_obj_t data) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj, mod_binascii_a2b_base64); -STATIC mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) { +STATIC mp_obj_t mod_binascii_b2a_base64(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_newline }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_newline, MP_ARG_BOOL, {.u_bool = true} }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + uint8_t newline = args[ARG_newline].u_bool; mp_buffer_info_t bufinfo; - mp_get_buffer_raise(data, &bufinfo, MP_BUFFER_READ); + mp_get_buffer_raise(pos_args[0], &bufinfo, MP_BUFFER_READ); vstr_t vstr; - vstr_init_len(&vstr, ((bufinfo.len != 0) ? (((bufinfo.len - 1) / 3) + 1) * 4 : 0) + 1); + vstr_init_len(&vstr, ((bufinfo.len != 0) ? (((bufinfo.len - 1) / 3) + 1) * 4 : 0) + newline); // First pass, we convert input buffer to numeric base 64 values byte *in = bufinfo.buf, *out = (byte *)vstr.buf; @@ -196,7 +204,7 @@ STATIC mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) { // Second pass, we convert number base 64 values to actual base64 ascii encoding out = (byte *)vstr.buf; - for (mp_uint_t j = vstr.len - 1; j--;) { + for (mp_uint_t j = vstr.len - newline; j--;) { if (*out < 26) { *out += 'A'; } else if (*out < 52) { @@ -212,10 +220,12 @@ STATIC mp_obj_t mod_binascii_b2a_base64(mp_obj_t data) { } out++; } - *out = '\n'; + if (newline) { + *out = '\n'; + } return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj, mod_binascii_b2a_base64); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(mod_binascii_b2a_base64_obj, 1, mod_binascii_b2a_base64); #if MICROPY_PY_UBINASCII_CRC32 #include "lib/uzlib/tinf.h" diff --git a/tests/extmod/ubinascii_b2a_base64.py b/tests/extmod/ubinascii_b2a_base64.py index 9c100f972b..3f92488969 100644 --- a/tests/extmod/ubinascii_b2a_base64.py +++ b/tests/extmod/ubinascii_b2a_base64.py @@ -20,3 +20,6 @@ print(binascii.b2a_base64(b"\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f")) print(binascii.b2a_base64(b"\x7f\x80\xff")) print(binascii.b2a_base64(b"1234ABCDabcd")) print(binascii.b2a_base64(b"\x00\x00>")) # convert into '+' + +print(binascii.b2a_base64(b"foobar", newline=True)) +print(binascii.b2a_base64(b"foobar", newline=False))