Base64 encoding into buffer, to reduce allocations
Description
base64 encoding is supported by binasci.b2a_base64. This implements the standard API in CPython. It always allocates a new buffer, and returns it. The allocations could be reduced/eliminated if a b2a_base64_into type API would (also) be available. This function would take an additional parameter: the buffer (bytearray) that is to be filled with encoded data. The caller must make sure it is sufficiently large (4/3 the size of the input + up to 4 bytes of padding).
This is similar to how there is struct.unpack_into, et.c.
Code Size
Existing code could be moved into the b2a_base64_into implementation. And then have b2a_base64 be a compatibility wrapper, which creates the needed buffer internally. This should make the code increase minimal.
Implementation
I hope the MicroPython maintainers or community will implement this feature
Code of Conduct
Yes, I agree
binascii: Add required argument to <int>.to_bytes(...) call.
The a2b_base64(...) function in the binascii module was failing due to an invalid call being made to the <int>.to_bytes(...) function. The <int>.to_bytes(...) function requires two arguments. The first specifies the number of bytes to return, and the second specifies the endianness of those bytes. By definition, Base64 encoding is big endian.
This change also fixed the test cases for the base64 module.
Merged, thanks. Also added testcase which exposed the issue.