encoder/filename: Add SCSU as tables

Instead of only adding SCSU, add it as an existing table.

Allow direct SCSU and add a, perhaps, reasonable table as well.

Add byte interfaces that doesn't base64 encode the URL as well with `EncodeBytes` and `DecodeBytes`.

Fuzz tested and decode tests added.
This commit is contained in:
Klaus Post
2021-01-04 17:09:09 +01:00
committed by Nick Craig-Wood
parent 47b69d6300
commit 424aaac2e1
8 changed files with 201 additions and 24 deletions

View File

@ -7,6 +7,7 @@ import (
"errors"
"sync"
"github.com/dop251/scsu"
"github.com/klauspost/compress/huff0"
)
@ -22,6 +23,7 @@ var customDecMu sync.Mutex
// Decode an encoded string.
func Decode(s string) (string, error) {
initCoders()
if len(s) < 1 {
return "", ErrCorrupted
}
@ -31,19 +33,25 @@ func Decode(s string) (string, error) {
}
table--
s = s[1:]
data := make([]byte, base64.URLEncoding.DecodedLen(len(s)))
n, err := base64.URLEncoding.Decode(data, ([]byte)(s))
if err != nil || n < 0 {
return "", ErrCorrupted
}
data = data[:n]
return DecodeBytes(table, data)
}
// DecodeBytes will decode raw id and data values.
func DecodeBytes(table byte, data []byte) (string, error) {
initCoders()
switch table {
case tableUncompressed:
return string(data), nil
case tableReserved:
return "", ErrUnsupported
case tableSCSUPlain:
return scsu.Decode(data)
case tableRLE:
if len(data) < 2 {
return "", ErrCorrupted
@ -79,6 +87,9 @@ func Decode(s string) (string, error) {
if err != nil {
return "", ErrCorrupted
}
if table == tableSCSU {
return scsu.Decode(name)
}
return string(name), nil
}
}