From 5ca7f1fe87052ffe9f18a8b134ba190038cf51f5 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Thu, 11 Feb 2021 10:41:29 +0100 Subject: [PATCH] encoder/filename: Wrap scsu package --- lib/encoder/filename/decode.go | 5 ++--- lib/encoder/filename/decode_test.go | 9 ++++++++- lib/encoder/filename/encode.go | 3 +-- lib/encoder/filename/scsu.go | 16 ++++++++++++++++ lib/encoder/filename/scsu_go113.go | 10 ++++++++++ 5 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 lib/encoder/filename/scsu.go create mode 100644 lib/encoder/filename/scsu_go113.go diff --git a/lib/encoder/filename/decode.go b/lib/encoder/filename/decode.go index 6e859fd31..291d34cc7 100644 --- a/lib/encoder/filename/decode.go +++ b/lib/encoder/filename/decode.go @@ -7,7 +7,6 @@ import ( "errors" "sync" - "github.com/dop251/scsu" "github.com/klauspost/compress/huff0" ) @@ -51,7 +50,7 @@ func DecodeBytes(table byte, data []byte) (string, error) { case tableReserved: return "", ErrUnsupported case tableSCSUPlain: - return scsu.Decode(data) + return scsuDecode(data) case tableRLE: if len(data) < 2 { return "", ErrCorrupted @@ -88,7 +87,7 @@ func DecodeBytes(table byte, data []byte) (string, error) { return "", ErrCorrupted } if table == tableSCSU { - return scsu.Decode(name) + return scsuDecode(name) } return string(name), nil } diff --git a/lib/encoder/filename/decode_test.go b/lib/encoder/filename/decode_test.go index f11247230..9fff86c5f 100644 --- a/lib/encoder/filename/decode_test.go +++ b/lib/encoder/filename/decode_test.go @@ -1,6 +1,9 @@ package filename -import "testing" +import ( + "runtime" + "testing" +) func TestDecode(t *testing.T) { tests := []struct { @@ -117,6 +120,10 @@ func TestDecode(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got, err := Decode(tt.encoded) if (err != nil) != tt.wantErr { + if err != nil && err.Error() == scsuNotEnabled && runtime.Version() < "go1.13" { + t.Skip(err.Error()) + return + } if tt.encoded == "" && tt.want != "" { proposed := Encode(tt.want) table := decodeMap[proposed[0]] - 1 diff --git a/lib/encoder/filename/encode.go b/lib/encoder/filename/encode.go index 031dbd07a..33d029902 100644 --- a/lib/encoder/filename/encode.go +++ b/lib/encoder/filename/encode.go @@ -4,7 +4,6 @@ import ( "encoding/base64" "encoding/binary" - "github.com/dop251/scsu" "github.com/klauspost/compress/huff0" ) @@ -38,7 +37,7 @@ func EncodeBytes(s string) (table byte, payload []byte) { if i == tableSCSU { var err error olen := len(org) - org, err = scsu.EncodeStrict(s, make([]byte, 0, len(org))) + org, err = scsuEncodeStrict(s, make([]byte, 0, len(org))) if err != nil || olen <= len(org) { continue } diff --git a/lib/encoder/filename/scsu.go b/lib/encoder/filename/scsu.go new file mode 100644 index 000000000..10aaa2b13 --- /dev/null +++ b/lib/encoder/filename/scsu.go @@ -0,0 +1,16 @@ +package filename + +import "errors" + +const scsuNotEnabled = "scsu encoding not enabled in this build due to old go version" + +// Functions wrap scsu package, since it doesn't build on old Go versions. +// Remove once v1.13 is minimum supported version. + +var scsuDecode = func(b []byte) (string, error) { + return "", errors.New(scsuNotEnabled) +} + +var scsuEncodeStrict = func(src string, dst []byte) ([]byte, error) { + return nil, errors.New(scsuNotEnabled) +} diff --git a/lib/encoder/filename/scsu_go113.go b/lib/encoder/filename/scsu_go113.go new file mode 100644 index 000000000..62ee6c75c --- /dev/null +++ b/lib/encoder/filename/scsu_go113.go @@ -0,0 +1,10 @@ +// +build go1.13 + +package filename + +import "github.com/dop251/scsu" + +func init() { + scsuDecode = scsu.Decode + scsuEncodeStrict = scsu.EncodeStrict +}