2023-03-28 23:55:57 +03:00
|
|
|
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
|
2023-01-17 20:46:43 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2022-09-27 09:25:50 +03:00
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// Module base32 implements base32 encoding as specified by RFC 4648.
|
2022-09-27 09:25:50 +03:00
|
|
|
// Based off: https://github.com/golang/go/blob/master/src/encoding/base32/base32.go
|
|
|
|
// Last commit: https://github.com/golang/go/commit/e1b62efaf33988a5153510898d37309cee78f26e
|
|
|
|
|
|
|
|
// TODO: standardize fn naming conventions & strip newlines on input & clean up an go remnant's
|
|
|
|
|
|
|
|
module base32
|
|
|
|
|
2022-09-27 09:29:44 +03:00
|
|
|
pub const (
|
|
|
|
std_padding = `=` // Standard padding character
|
|
|
|
no_padding = u8(-1) // No padding
|
|
|
|
std_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.bytes()
|
|
|
|
hex_alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUV'.bytes()
|
2022-09-27 09:25:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Encoding {
|
|
|
|
padding_char u8
|
2022-09-27 09:29:44 +03:00
|
|
|
alphabet []u8
|
2022-09-27 09:25:50 +03:00
|
|
|
mut:
|
2022-09-27 09:29:44 +03:00
|
|
|
decode_map [256]u8
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// decode_string_to_string decodes a V string `src` using Base32
|
|
|
|
// and returns the decoded string or a `corrupt_input_error_msg` error.
|
2023-04-06 04:09:05 +03:00
|
|
|
pub fn decode_string_to_string(src string) !string {
|
2022-09-27 09:25:50 +03:00
|
|
|
return decode_to_string(src.bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// decode_to_string decodes a byte array `src` using Base32
|
|
|
|
// and returns the decoded string or a `corrupt_input_error_msg` error.
|
2023-04-06 04:09:05 +03:00
|
|
|
pub fn decode_to_string(src []u8) !string {
|
|
|
|
res := decode(src)!
|
2022-09-27 09:25:50 +03:00
|
|
|
return res.bytestr()
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// decode decodes a byte array `src` using Base32
|
|
|
|
// and returns the decoded bytes or a `corrupt_input_error_msg` error.
|
2023-04-06 04:09:05 +03:00
|
|
|
pub fn decode(src []u8) ![]u8 {
|
2022-09-27 09:29:44 +03:00
|
|
|
mut e := new_encoding(base32.std_alphabet)
|
2022-09-27 09:25:50 +03:00
|
|
|
return e.decode(src)
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// encode_string_to_string encodes the V string `src` using Base32
|
|
|
|
// and returns the encoded bytes as a V string.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn encode_string_to_string(src string) string {
|
|
|
|
return encode_to_string(src.bytes())
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// encode_to_string encodes a byte array `src` using Base32 and
|
|
|
|
// returns the encoded bytes as a V string.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn encode_to_string(src []u8) string {
|
|
|
|
return encode(src).bytestr()
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// encode encodes a byte array `src` using Base32 and returns the
|
|
|
|
// encoded bytes.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn encode(src []u8) []u8 {
|
2022-09-27 09:29:44 +03:00
|
|
|
e := new_encoding(base32.std_alphabet)
|
|
|
|
return e.encode(src)
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// encode_to_string encodes the Base32 encoding of `src` with
|
|
|
|
// the encoding `enc` and returns the encoded bytes as a V string.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn (enc &Encoding) encode_to_string(src []u8) string {
|
|
|
|
return enc.encode(src).bytestr()
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// encode_string_to_string encodes a V string `src` using Base32 with
|
|
|
|
// the encoding `enc` and returns the encoded bytes as a V string.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn (enc &Encoding) encode_string_to_string(src string) string {
|
|
|
|
return enc.encode(src.bytes()).bytestr()
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// new_std_encoding creates a standard Base32 `Encoding` as defined in
|
|
|
|
// RFC 4648.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn new_std_encoding() Encoding {
|
2022-09-27 09:29:44 +03:00
|
|
|
return new_encoding_with_padding(base32.std_alphabet, base32.std_padding)
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// new_std_encoding creates a standard Base32 `Encoding` identical to
|
|
|
|
// `new_std_encoding` but with a specified character `padding`,
|
|
|
|
// or `no_padding` to disable padding.
|
|
|
|
// The `padding` character must not be '\r' or '\n', must not
|
|
|
|
// be contained in the `Encoding`'s alphabet and must be a rune equal or
|
|
|
|
// below '\xff'.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn new_std_encoding_with_padding(padding u8) Encoding {
|
2022-09-27 09:29:44 +03:00
|
|
|
return new_encoding_with_padding(base32.std_alphabet, padding)
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// new_encoding returns a Base32 `Encoding` with standard
|
|
|
|
// `alphabet`s and standard padding.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn new_encoding(alphabet []u8) Encoding {
|
2022-09-27 09:29:44 +03:00
|
|
|
return new_encoding_with_padding(alphabet, base32.std_padding)
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// new_encoding_with_padding returns a Base32 `Encoding` with specified
|
|
|
|
// encoding `alphabet`s and a specified `padding_char`.
|
|
|
|
// The `padding_char` must not be '\r' or '\n', must not
|
|
|
|
// be contained in the `Encoding`'s alphabet and must be a rune equal or
|
|
|
|
// below '\xff'.
|
2022-09-27 09:25:50 +03:00
|
|
|
pub fn new_encoding_with_padding(alphabet []u8, padding_char u8) Encoding {
|
|
|
|
if padding_char == `\r` || padding_char == `\n` || padding_char > 0xff {
|
|
|
|
panic('invalid padding')
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < alphabet.len; i++ {
|
|
|
|
if alphabet[i] == padding_char {
|
|
|
|
panic('padding contained in alphabet')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mut decode_map := [256]u8{}
|
2022-09-27 09:29:44 +03:00
|
|
|
for i in 0 .. alphabet.len {
|
2022-09-27 09:25:50 +03:00
|
|
|
decode_map[alphabet[i]] = u8(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Encoding{
|
|
|
|
alphabet: alphabet
|
|
|
|
padding_char: padding_char
|
|
|
|
decode_map: decode_map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// encode encodes `src` using the encoding `enc`, writing
|
|
|
|
// and returning encoded_len(src.len) u8s.
|
|
|
|
//
|
|
|
|
// The encoding pads the output to a multiple of 8 u8s,
|
|
|
|
// so encode is not appropriate for use on individual blocks
|
|
|
|
// of a large data stream.
|
2022-09-27 09:25:50 +03:00
|
|
|
fn (enc &Encoding) encode(src []u8) []u8 {
|
2022-09-27 09:29:44 +03:00
|
|
|
mut buf := []u8{len: enc.encoded_len(src.len)}
|
|
|
|
mut dst := unsafe { buf }
|
2022-09-27 09:25:50 +03:00
|
|
|
enc.encode_(src, mut dst)
|
|
|
|
return buf
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// encode_ encodes `src` using the encoding `enc`, writing
|
|
|
|
// encoded_len(src.len) u8s to `dst`.
|
2022-09-27 09:25:50 +03:00
|
|
|
//
|
|
|
|
// The encoding pads the output to a multiple of 8 u8s,
|
2023-01-17 20:46:43 +03:00
|
|
|
// so encode_ is not appropriate for use on individual blocks
|
|
|
|
// of a large data stream.
|
2022-09-27 09:25:50 +03:00
|
|
|
fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
|
|
|
|
mut src := unsafe { src_ }
|
|
|
|
for src.len > 0 {
|
|
|
|
mut b := [8]u8{}
|
|
|
|
|
|
|
|
// Unpack 8x 5-bit source blocks into a 5 u8
|
|
|
|
// destination quantum
|
|
|
|
if src.len > 4 {
|
|
|
|
b[7] = src[4] & 0x1F
|
|
|
|
b[6] = src[4] >> 5
|
|
|
|
}
|
|
|
|
if src.len >= 4 {
|
|
|
|
b[6] |= (src[3] << 3) & 0x1F
|
|
|
|
b[5] = (src[3] >> 2) & 0x1F
|
|
|
|
b[4] = src[3] >> 7
|
|
|
|
}
|
|
|
|
if src.len >= 3 {
|
|
|
|
b[4] |= (src[2] << 1) & 0x1F
|
|
|
|
b[3] = (src[2] >> 4) & 0x1F
|
|
|
|
}
|
|
|
|
if src.len >= 2 {
|
|
|
|
b[3] |= (src[1] << 4) & 0x1F
|
|
|
|
b[2] = (src[1] >> 1) & 0x1F
|
|
|
|
b[1] = (src[1] >> 6) & 0x1F
|
|
|
|
}
|
|
|
|
if src.len >= 1 {
|
|
|
|
b[1] |= (src[0] << 2) & 0x1F
|
|
|
|
b[0] = src[0] >> 3
|
|
|
|
}
|
|
|
|
|
|
|
|
// Encode 5-bit blocks using the base32 alphabet
|
|
|
|
if dst.len >= 8 {
|
|
|
|
// Common case, unrolled for extra performance
|
2022-09-27 09:29:44 +03:00
|
|
|
dst[0] = enc.alphabet[b[0] & 31]
|
|
|
|
dst[1] = enc.alphabet[b[1] & 31]
|
|
|
|
dst[2] = enc.alphabet[b[2] & 31]
|
|
|
|
dst[3] = enc.alphabet[b[3] & 31]
|
|
|
|
dst[4] = enc.alphabet[b[4] & 31]
|
|
|
|
dst[5] = enc.alphabet[b[5] & 31]
|
|
|
|
dst[6] = enc.alphabet[b[6] & 31]
|
|
|
|
dst[7] = enc.alphabet[b[7] & 31]
|
2022-09-27 09:25:50 +03:00
|
|
|
} else {
|
|
|
|
for i := 0; i < dst.len; i++ {
|
2022-09-27 09:29:44 +03:00
|
|
|
dst[i] = enc.alphabet[b[i] & 31]
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pad the final quantum
|
|
|
|
if src.len < 5 {
|
2022-09-27 09:29:44 +03:00
|
|
|
if enc.padding_char == base32.no_padding {
|
2022-09-27 09:25:50 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
dst[7] = enc.padding_char
|
|
|
|
if src.len < 4 {
|
|
|
|
dst[6] = enc.padding_char
|
|
|
|
dst[5] = enc.padding_char
|
|
|
|
if src.len < 3 {
|
|
|
|
dst[4] = enc.padding_char
|
|
|
|
if src.len < 2 {
|
|
|
|
dst[3] = enc.padding_char
|
|
|
|
dst[2] = enc.padding_char
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
src = src[5..]
|
|
|
|
dst = dst[8..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 09:29:44 +03:00
|
|
|
fn (enc &Encoding) encoded_len(n int) int {
|
|
|
|
if enc.padding_char == base32.no_padding {
|
|
|
|
return (n * 8 + 4) / 5
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|
|
|
|
return (n + 4) / 5 * 8
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// decode_string decodes a V string `src` using Base32 with the encoding `enc`
|
|
|
|
// and returns the decoded bytes or a `corrupt_input_error_msg` error.
|
2023-04-06 04:09:05 +03:00
|
|
|
pub fn (enc &Encoding) decode_string(src string) ![]u8 {
|
2022-09-27 09:25:50 +03:00
|
|
|
return enc.decode(src.bytes())
|
|
|
|
// mut buf := strip_newlines(src.bytes())
|
|
|
|
// mut dst := unsafe { buf }
|
|
|
|
// // l := strip_newlines(mut buf)
|
|
|
|
// n, _ := enc.decode_(buf, mut dst)?
|
|
|
|
// return buf[..n]
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// decode_string_to_string decodes a V string `src` using Base32 with the
|
|
|
|
// encoding `enc` and returns the decoded V string or a `corrupt_input_error_msg` error.
|
2023-04-06 04:09:05 +03:00
|
|
|
pub fn (enc &Encoding) decode_string_to_string(src string) !string {
|
|
|
|
decoded := enc.decode_string(src)!
|
2022-09-27 09:25:50 +03:00
|
|
|
return decoded.bytestr()
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// decode decodes `src` using the encoding `enc`. It returns the decoded bytes
|
|
|
|
// written or a `corrupt_input_error_msg` error.
|
|
|
|
// New line characters (\r and \n) are ignored.
|
2023-04-06 04:09:05 +03:00
|
|
|
pub fn (enc &Encoding) decode(src []u8) ![]u8 {
|
2022-09-27 09:25:50 +03:00
|
|
|
mut buf := []u8{len: src.len}
|
2022-09-27 09:29:44 +03:00
|
|
|
// mut dst := unsafe { buf }
|
2022-09-27 09:25:50 +03:00
|
|
|
// l := strip_newlines(mut dst, src)
|
|
|
|
// n, _ := enc.decode_(src[..l], mut dst) or {
|
|
|
|
// src := strip_newlines(src_)
|
2022-09-27 09:29:44 +03:00
|
|
|
n, _ := enc.decode_(src, mut buf) or { return err }
|
2022-09-27 09:25:50 +03:00
|
|
|
return buf[..n]
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// decode_ returns the number of bytes written and a boolean value, which
|
2022-09-27 09:25:50 +03:00
|
|
|
// indicates if end-of-message padding was encountered and thus any
|
|
|
|
// additional data is an error. This method assumes that src has been
|
|
|
|
// stripped of all supported whitespace (`\r` and `\n`).
|
2023-04-06 04:09:05 +03:00
|
|
|
fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) !(int, bool) {
|
2022-09-27 09:25:50 +03:00
|
|
|
mut src := unsafe { src_ }
|
|
|
|
mut n := 0
|
|
|
|
mut end := false
|
|
|
|
// Lift the nil check outside of the loop.
|
|
|
|
// _ = enc.decode_map
|
|
|
|
|
|
|
|
mut dsti := 0
|
|
|
|
olen := src.len
|
|
|
|
|
|
|
|
for src.len > 0 && !end {
|
|
|
|
// Decode quantum using the base32 alphabet
|
|
|
|
mut dbuf := [8]u8{}
|
|
|
|
mut dlen := 8
|
|
|
|
|
|
|
|
for j := 0; j < 8; {
|
|
|
|
if src.len == 0 {
|
2022-09-27 09:29:44 +03:00
|
|
|
if enc.padding_char != base32.no_padding {
|
2022-09-27 09:25:50 +03:00
|
|
|
// We have reached the end and are missing padding
|
|
|
|
// return n, false, corrupt_input_error(olen - src.len - j)
|
|
|
|
return error(corrupt_input_error_msg(olen - src.len - j))
|
|
|
|
}
|
|
|
|
// We have reached the end and are not expecting any padding
|
|
|
|
dlen, end = j, true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
in0 := src[0]
|
|
|
|
src = src[1..]
|
|
|
|
if in0 == enc.padding_char && j >= 2 && src.len < 8 {
|
|
|
|
// We`ve reached the end and there`s padding
|
2022-09-27 09:29:44 +03:00
|
|
|
if src.len + j < 8 - 1 {
|
2022-09-27 09:25:50 +03:00
|
|
|
// not enough padding
|
|
|
|
// return n, false, corrupt_input_error(olen)
|
|
|
|
return error(corrupt_input_error_msg(olen))
|
|
|
|
}
|
2022-09-27 09:29:44 +03:00
|
|
|
for k := 0; k < 8 - 1 - j; k++ {
|
2022-09-27 09:25:50 +03:00
|
|
|
if src.len > k && src[k] != enc.padding_char {
|
|
|
|
// incorrect padding
|
|
|
|
// return n, false, corrupt_input_error(olen - src.len + k - 1)
|
|
|
|
return error(corrupt_input_error_msg(olen - src.len + k - 1))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dlen, end = j, true
|
|
|
|
// 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not
|
|
|
|
// valid dlen values. See RFC 4648 Section 6 'Base 32 Encoding' listing
|
|
|
|
// the five valid padding lengths, and Section 9 'Illustrations and
|
|
|
|
// Examples' for an illustration for how the 1st, 3rd and 6th base32
|
|
|
|
// src u8s do not yield enough information to decode a dst u8.
|
|
|
|
if dlen == 1 || dlen == 3 || dlen == 6 {
|
|
|
|
// return n, false, corrupt_input_error(olen - src.len - 1)
|
|
|
|
return error(corrupt_input_error_msg(olen - src.len - 1))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
2022-09-27 09:29:44 +03:00
|
|
|
|
2022-09-27 09:25:50 +03:00
|
|
|
dbuf[j] = enc.decode_map[in0]
|
|
|
|
if dbuf[j] == 0xFF {
|
|
|
|
// return n, false, corrupt_input_error(olen - src.len - 1)
|
|
|
|
return error(corrupt_input_error_msg(olen - src.len - 1))
|
|
|
|
}
|
|
|
|
j++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pack 8x 5-bit source blocks into 5 u8 destination
|
|
|
|
// quantum
|
|
|
|
if dlen == 8 {
|
2022-09-27 09:29:44 +03:00
|
|
|
dst[dsti + 4] = dbuf[6] << 5 | dbuf[7]
|
2022-09-27 09:25:50 +03:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
if dlen >= 7 {
|
2022-09-27 09:29:44 +03:00
|
|
|
dst[dsti + 3] = dbuf[4] << 7 | dbuf[5] << 2 | dbuf[6] >> 3
|
2022-09-27 09:25:50 +03:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
if dlen >= 5 {
|
2022-09-27 09:29:44 +03:00
|
|
|
dst[dsti + 2] = dbuf[3] << 4 | dbuf[4] >> 1
|
2022-09-27 09:25:50 +03:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
if dlen >= 4 {
|
2022-09-27 09:29:44 +03:00
|
|
|
dst[dsti + 1] = dbuf[1] << 6 | dbuf[2] << 1 | dbuf[3] >> 4
|
2022-09-27 09:25:50 +03:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
if dlen >= 2 {
|
2022-09-27 09:29:44 +03:00
|
|
|
dst[dsti + 0] = dbuf[0] << 3 | dbuf[1] >> 2
|
2022-09-27 09:25:50 +03:00
|
|
|
n++
|
|
|
|
}
|
|
|
|
dsti += 5
|
|
|
|
}
|
|
|
|
return n, end
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:46:43 +03:00
|
|
|
// strip_newlines removes newline characters and returns the number
|
2022-09-27 09:25:50 +03:00
|
|
|
// of non-newline characters copied to dst.
|
|
|
|
// fn strip_newlines(mut dst []u8, src []byte) int {
|
|
|
|
// mut offset := 0
|
|
|
|
// for b in src {
|
|
|
|
// if b in [`\r`, `\n`] {
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// dst[offset] = b
|
|
|
|
// offset++
|
|
|
|
// }
|
|
|
|
// return offset
|
|
|
|
// }
|
|
|
|
fn strip_newlines(src []u8) []u8 {
|
|
|
|
mut dst := []u8{}
|
|
|
|
for b in src {
|
|
|
|
if b in [`\r`, `\n`] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dst << b
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
|
|
|
fn corrupt_input_error_msg(e int) string {
|
|
|
|
// return error('illegal base32 data at input byte ' + strconv.FormatInt(int64(e), 10)
|
2022-11-15 16:53:13 +03:00
|
|
|
return 'illegal base32 data at input byte ${e}'
|
2022-09-27 09:25:50 +03:00
|
|
|
}
|