From fadfc42c90696e784b17aba1ed86faaadaa4bdbc Mon Sep 17 00:00:00 2001 From: schwarzlichtbezirk Date: Thu, 24 Aug 2023 13:43:05 +0300 Subject: [PATCH] strcast --- LICENSE | 2 +- go.mod | 2 +- go.sum | 4 ++-- luawpk/script_test.go | 2 +- strcast.go | 19 +++++++++++++++++++ tagset.go | 2 +- wpk.go | 4 ++-- wpk_test.go | 4 ++-- writer.go | 2 +- 9 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 strcast.go diff --git a/LICENSE b/LICENSE index 1f348ca..bfe1279 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) schwarzlichtbezirk, 2021 +Copyright (c) schwarzlichtbezirk, 2021-2023 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/go.mod b/go.mod index fe9ed8f..2ed36f4 100644 --- a/go.mod +++ b/go.mod @@ -9,4 +9,4 @@ require ( gopkg.in/djherbis/times.v1 v1.3.0 ) -require golang.org/x/sys v0.10.0 // indirect +require golang.org/x/sys v0.11.0 // indirect diff --git a/go.sum b/go.sum index baf7ac3..26285cc 100644 --- a/go.sum +++ b/go.sum @@ -4,7 +4,7 @@ github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/yuin/gopher-lua v1.1.0 h1:BojcDhfyDWgU2f2TOzYK/g5p2gxMrku8oupLDqlnSqE= github.com/yuin/gopher-lua v1.1.0/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= -golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= -golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM= +golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/djherbis/times.v1 v1.3.0 h1:uxMS4iMtH6Pwsxog094W0FYldiNnfY/xba00vq6C2+o= gopkg.in/djherbis/times.v1 v1.3.0/go.mod h1:AQlg6unIsrsCEdQYhTzERy542dz6SFdQFZFv6mUY0P8= diff --git a/luawpk/script_test.go b/luawpk/script_test.go index 5f970b6..ae96d60 100644 --- a/luawpk/script_test.go +++ b/luawpk/script_test.go @@ -58,7 +58,7 @@ func CheckPackage(t *testing.T, wptname, wpfname string) { } var orig []byte - if orig, err = os.ReadFile(mediadir + string(link)); err != nil { + if orig, err = os.ReadFile(mediadir + wpk.B2S(link)); err != nil { t.Fatal(err) } diff --git a/strcast.go b/strcast.go new file mode 100644 index 0000000..8cfe9b6 --- /dev/null +++ b/strcast.go @@ -0,0 +1,19 @@ +package wpk + +import ( + "reflect" + "unsafe" +) + +func B2S(b []byte) string { + return *(*string)(unsafe.Pointer(&b)) +} + +func S2B(s string) (b []byte) { + bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) + sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) + bh.Data = sh.Data + bh.Cap = sh.Len + bh.Len = sh.Len + return b +} diff --git a/tagset.go b/tagset.go index aa470e7..517caaa 100644 --- a/tagset.go +++ b/tagset.go @@ -13,7 +13,7 @@ type TagRaw []byte // TagStr tag converter. func (t TagRaw) TagStr() (string, bool) { - return string(t), true + return B2S(t), true } // StrTag is string tag constructor. diff --git a/wpk.go b/wpk.go index 50a6069..99ad4be 100644 --- a/wpk.go +++ b/wpk.go @@ -181,14 +181,14 @@ type Header struct { // IsReady determines that package is ready for read the data. func (hdr *Header) IsReady() error { // can not read file tags table for opened on write single-file package. - if string(hdr.signature[:]) == SignBuild { + if B2S(hdr.signature[:]) == SignBuild { if hdr.datoffset != 0 { return ErrSignPre } return nil } // can not read file tags table on any incorrect signature - if string(hdr.signature[:]) != SignReady { + if B2S(hdr.signature[:]) != SignReady { return ErrSignBad } return nil diff --git a/wpk_test.go b/wpk_test.go index 858d6a7..cb1e7e4 100644 --- a/wpk_test.go +++ b/wpk_test.go @@ -29,7 +29,7 @@ var testpkgt = wpk.TempPath("testpack.wpt") var testpkgf = wpk.TempPath("testpack.wpf") var memdata = map[string][]byte{ - "sample.txt": []byte("The quick brown fox jumps over the lazy dog"), + "sample.txt": wpk.S2B("The quick brown fox jumps over the lazy dog"), "array.dat": { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, @@ -71,7 +71,7 @@ func CheckPackage(t *testing.T, fwpt, fwpf *os.File, tagsnum int) { var orig []byte if isfile { - if orig, err = os.ReadFile(mediadir + string(link)); err != nil { + if orig, err = os.ReadFile(mediadir + wpk.B2S(link)); err != nil { t.Fatal(err) } } else { diff --git a/writer.go b/writer.go index 46e781a..9a36561 100644 --- a/writer.go +++ b/writer.go @@ -56,7 +56,7 @@ func (ftt *FTT) Append(wpt, wpf io.WriteSeeker) (err error) { return } // rewrite prebuild signature - if err = binary.Write(wpt, binary.LittleEndian, []byte(SignBuild)); err != nil { + if err = binary.Write(wpt, binary.LittleEndian, S2B(SignBuild)); err != nil { return } // go to tags table start to replace it by new data