Do not build decode table for base85 until it is actually needed

Also make the tests actual tests
This commit is contained in:
Kovid Goyal
2023-09-04 21:32:25 +05:30
parent 0107d1cb89
commit 541c0cdde4
2 changed files with 75 additions and 66 deletions

View File

@@ -10,10 +10,11 @@ import (
"errors"
"io"
"strconv"
"sync"
)
// 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~
var b2c_table = [85]byte{
var encode = [85]byte{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, //0
0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, //1
@@ -23,22 +24,35 @@ var b2c_table = [85]byte{
0x60, 0x7B, 0x7C, 0x7D, 0x7E,
}
var encode [85]byte
var decode [256]byte
func init() {
copy(encode[:], b2c_table[:])
for i := 0; i < len(decode); i++ {
decode[i] = 0xFF
var decoder_array = sync.OnceValue(func() [256]byte {
var decode = [256]byte{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
}
for i := 0; i < len(encode); i++ {
decode[encode[i]] = byte(i)
}
}
return decode
})
// encodeChunk encodes 4 byte-chunk to 5 byte
// if chunk size is less then 4, then it is padded before convertion.
// return written bytes or error
// if chunk size is less then 4, then it is padded before encoding.
// return written bytes
func encodeChunk(dst, src []byte) int {
if len(src) == 0 {
return 0
@@ -78,7 +92,7 @@ var decode_base = [5]uint32{85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1}
// decodeChunk decodes 5 byte-chunk to 4 byte
// if chunk size is less then 5, then it is padded before convertion.
// return written bytes and error input index
func decodeChunk(dst, src []byte) (int, int) {
func decodeChunk(decode *[256]byte, dst, src []byte) (int, int) {
if len(src) == 0 {
return 0, 0
}
@@ -150,16 +164,17 @@ func DecodeString(src string) ([]byte, error) {
func Decode(dst, src []byte) (int, error) {
f := 0
t := 0
decode := decoder_array()
for len(src) > 0 {
if len(src) < 5 {
w, err := decodeChunk(dst, src)
w, err := decodeChunk(&decode, dst, src)
if err > 0 {
return t, CorruptInputError(f + err)
}
return t + w, nil
}
_, err := decodeChunk(dst[:4], src[:5])
_, err := decodeChunk(&decode, dst[:4], src[:5])
if err > 0 {
return t, CorruptInputError(f + err)
} else {