1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

encoding.base32: vfmt code

This commit is contained in:
Joe Conigliaro 2022-09-27 16:29:44 +10:00
parent 5415c4f75e
commit 51a92d170f
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
2 changed files with 45 additions and 52 deletions

View File

@ -10,7 +10,7 @@
module base32
pub const(
pub const (
std_padding = `=` // Standard padding character
no_padding = u8(-1) // No padding
std_alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'.bytes()
@ -34,7 +34,7 @@ pub fn decode_to_string(src []u8) ?string {
}
pub fn decode(src []u8) ?[]u8 {
mut e := new_encoding(std_alphabet)
mut e := new_encoding(base32.std_alphabet)
return e.decode(src)
}
@ -47,7 +47,7 @@ pub fn encode_to_string(src []u8) string {
}
pub fn encode(src []u8) []u8 {
e := new_encoding(std_alphabet)
e := new_encoding(base32.std_alphabet)
return e.encode(src)
}
@ -60,15 +60,15 @@ pub fn (enc &Encoding) encode_string_to_string(src string) string {
}
pub fn new_std_encoding() Encoding {
return new_encoding_with_padding(std_alphabet, std_padding)
return new_encoding_with_padding(base32.std_alphabet, base32.std_padding)
}
pub fn new_std_encoding_with_padding(padding u8) Encoding {
return new_encoding_with_padding(std_alphabet, padding)
return new_encoding_with_padding(base32.std_alphabet, padding)
}
pub fn new_encoding(alphabet []u8) Encoding {
return new_encoding_with_padding(alphabet, std_padding)
return new_encoding_with_padding(alphabet, base32.std_padding)
}
pub fn new_encoding_with_padding(alphabet []u8, padding_char u8) Encoding {
@ -83,7 +83,7 @@ pub fn new_encoding_with_padding(alphabet []u8, padding_char u8) Encoding {
}
mut decode_map := [256]u8{}
for i in 0..alphabet.len {
for i in 0 .. alphabet.len {
decode_map[alphabet[i]] = u8(i)
}
@ -140,23 +140,23 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
// Encode 5-bit blocks using the base32 alphabet
if dst.len >= 8 {
// Common case, unrolled for extra performance
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]
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]
} else {
for i := 0; i < dst.len; i++ {
dst[i] = enc.alphabet[b[i]&31]
dst[i] = enc.alphabet[b[i] & 31]
}
}
// Pad the final quantum
if src.len < 5 {
if enc.padding_char == no_padding {
if enc.padding_char == base32.no_padding {
break
}
@ -181,8 +181,8 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
}
fn (enc &Encoding) encoded_len(n int) int {
if enc.padding_char == no_padding {
return (n*8 + 4) / 5
if enc.padding_char == base32.no_padding {
return (n * 8 + 4) / 5
}
return (n + 4) / 5 * 8
}
@ -207,9 +207,7 @@ pub fn (enc &Encoding) decode(src []u8) ?[]u8 {
// l := strip_newlines(mut dst, src)
// n, _ := enc.decode_(src[..l], mut dst) or {
// src := strip_newlines(src_)
n, _ := enc.decode_(src, mut buf) or {
return err
}
n, _ := enc.decode_(src, mut buf) or { return err }
return buf[..n]
}
@ -233,9 +231,8 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
mut dlen := 8
for j := 0; j < 8; {
if src.len == 0 {
if enc.padding_char != no_padding {
if enc.padding_char != base32.no_padding {
// 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))
@ -248,12 +245,12 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
src = src[1..]
if in0 == enc.padding_char && j >= 2 && src.len < 8 {
// We`ve reached the end and there`s padding
if src.len+j < 8-1 {
if src.len + j < 8 - 1 {
// not enough padding
// return n, false, corrupt_input_error(olen)
return error(corrupt_input_error_msg(olen))
}
for k := 0; k < 8-1-j; k++ {
for k := 0; k < 8 - 1 - j; k++ {
if src.len > k && src[k] != enc.padding_char {
// incorrect padding
// return n, false, corrupt_input_error(olen - src.len + k - 1)
@ -284,23 +281,23 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) ?(int, bool) {
// Pack 8x 5-bit source blocks into 5 u8 destination
// quantum
if dlen == 8 {
dst[dsti+4] = dbuf[6]<<5 | dbuf[7]
dst[dsti + 4] = dbuf[6] << 5 | dbuf[7]
n++
}
if dlen >= 7 {
dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3
dst[dsti + 3] = dbuf[4] << 7 | dbuf[5] << 2 | dbuf[6] >> 3
n++
}
if dlen >= 5 {
dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1
dst[dsti + 2] = dbuf[3] << 4 | dbuf[4] >> 1
n++
}
if dlen >= 4 {
dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4
dst[dsti + 1] = dbuf[1] << 6 | dbuf[2] << 1 | dbuf[3] >> 4
n++
}
if dlen >= 2 {
dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2
dst[dsti + 0] = dbuf[0] << 3 | dbuf[1] >> 2
n++
}
dsti += 5
@ -332,7 +329,6 @@ fn strip_newlines(src []u8) []u8 {
return dst
}
fn corrupt_input_error_msg(e int) string {
// return error('illegal base32 data at input byte ' + strconv.FormatInt(int64(e), 10)
return 'illegal base32 data at input byte $e'

View File

@ -1,6 +1,5 @@
import encoding.base32
// TODO: add more tests
fn test_encode_and_decode() {
@ -9,9 +8,7 @@ fn test_encode_and_decode() {
encoded := base32.encode_string_to_string(input)
assert encoded == 'NBSWY3DPEB3A===='
decoded := base32.decode_string_to_string(encoded) or {
panic('error decoding: $err')
}
decoded := base32.decode_string_to_string(encoded) or { panic('error decoding: $err') }
assert decoded == input
encoder_no_padding := base32.new_std_encoding_with_padding(base32.no_padding)