hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit c43b34f8013b3366708cd438c86d4cd1c11049d6
parent 876b5929ab8a17c0f9c8dbbf5235ad5134f8bab0
Author: Drew DeVault <sir@cmpwn.com>
Date:   Tue, 29 Mar 2022 22:51:58 +0200

encoding::base64: add encode, decode

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Mencoding/base64/base64.ha | 25+++++++++++++++++++++++++
1 file changed, 25 insertions(+), 0 deletions(-)

diff --git a/encoding/base64/base64.ha b/encoding/base64/base64.ha @@ -167,6 +167,19 @@ export fn encodeslice(enc: *encoding, in: []u8) []u8 = { return bufio::buffer(&out); }; +// Encodes base64 data using the given alphabet and writes it to a stream, +// returning the number of bytes of data written (i.e. len(buf)). +export fn encode( + out: io::handle, + enc: *encoding, + buf: []u8, +) (size | io::error) = { + // TODO: rename new_encoder to newencoder etc; reverse params + const enc = new_encoder(enc, out); + defer io::close(&enc); + return io::writeall(&enc, buf)?; +}; + // Encodes a byte slice in base 64, using the given encoding, returning a // string. The caller must free the return value. export fn encodestr(enc: *encoding, in: []u8) str = { @@ -346,6 +359,18 @@ export fn decodestr(enc: *encoding, in: str) ([]u8 | errors::invalid) = { return decodeslice(enc, strings::toutf8(in)); }; +// Decodes base64 data from a stream using the given alphabet, returning the +// number of bytes of bytes read (i.e. len(buf)). +export fn decode( + in: io::handle, + enc: *encoding, + buf: []u8, +) (size | io::EOF | io::error) = { + const enc = new_decoder(enc, in); + defer io::close(&enc); + return io::readall(&enc, buf)?; +}; + @test fn decode() void = { // RFC 4648 test vectors const cases: [_](str, str, *encoding) = [