1
0
mirror of https://github.com/fumiama/go-base16384.git synced 2026-06-06 09:10:38 +08:00

适配所有架构

This commit is contained in:
fumiama
2022-02-10 13:58:39 +08:00
parent 86dc5b5b23
commit d84411a4ef
6 changed files with 37 additions and 78 deletions

View File

@@ -1,20 +1,14 @@
// +build amd64 arm64 ppc64le mips64le 386 arm mipsle
// Package base14 base16384 的 go 接口
package base14
import (
"unsafe"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)
import "encoding/binary"
func EncodeString(s string) []byte {
return Encode(helper.StringToBytes(s))
return Encode(StringToBytes(s))
}
func DecodeString(d []byte) string {
return helper.BytesToString(Decode(d))
return BytesToString(Decode(d))
}
func Encode(b []byte) (encd []byte) {
@@ -33,8 +27,7 @@ func Encode(b []byte) (encd []byte) {
outlen += 10
}
encd = make([]byte, outlen, outlen+8) //冗余的8B用于可能的结尾的覆盖
vals := (*slice)(unsafe.Pointer(&encd)).Data
var n uintptr
var n int
i := 0
for ; i <= len(b)-7; i += 7 {
sum := 0x000000000000003f & ((uint64)(b[i]) >> 2)
@@ -46,7 +39,7 @@ func Encode(b []byte) (encd []byte) {
sum |= ((uint64)(b[i+5]) << 48) & 0x003f000000000000
sum |= ((uint64)(b[i+6]) << 56) & 0xff00000000000000
sum += 0x004e004e004e004e
*(*uint64)(unsafe.Pointer(uintptr(vals) + n)) = sum
binary.LittleEndian.PutUint64(encd[n:], sum)
n += 8
}
if offset > 0 {
@@ -73,7 +66,9 @@ func Encode(b []byte) (encd []byte) {
}
}
sum += 0x004e004e004e004e
*(*uint64)(unsafe.Pointer(uintptr(vals) + n)) = sum
var tmp [8]byte
binary.LittleEndian.PutUint64(tmp[:], sum)
copy(encd[n:], tmp[:])
encd[outlen-2] = '='
encd[outlen-1] = byte(offset)
}
@@ -100,11 +95,10 @@ func Decode(b []byte) (decd []byte) {
}
outlen = outlen/8*7 + offset
decd = make([]byte, outlen)
vals := (*slice)(unsafe.Pointer(&b)).Data
var n uintptr
i := 0
for ; i <= len(decd)-7; n += 8 {
sum := *(*uint64)(unsafe.Pointer(uintptr(vals) + n)) - 0x004e004e004e004e
sum := binary.LittleEndian.Uint64(b[n:]) - 0x004e004e004e004e
decd[i] = byte(((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14))
i++
decd[i] = byte(((sum & 0x0000000000003f00) >> 6) | ((sum & 0x0000000000300000) >> 20))
@@ -121,7 +115,8 @@ func Decode(b []byte) (decd []byte) {
i++
}
if offset > 0 {
sum := *(*uint64)(unsafe.Pointer(uintptr(vals) + n)) - 0x000000000000004e
b = append(b, make([]byte, offset)...)
sum := binary.LittleEndian.Uint64(b[n:]) - 0x000000000000004e
decd[i] = byte(((sum & 0x000000000000003f) << 2) | ((sum & 0x000000000000c000) >> 14))
i++
if offset > 1 {
@@ -143,7 +138,6 @@ func Decode(b []byte) (decd []byte) {
decd[i] = byte(((sum & 0x0000030000000000) >> 34) | ((sum & 0x003f000000000000) >> 48))
i++
}
}
}
}

View File

@@ -10,6 +10,9 @@ func TestBase14(t *testing.T) {
es, err := UTF16be2utf8(e)
if err == nil {
t.Log(string(es))
if string(es) != "蜮嘎惢磦筢貊豔耹嫹桊涖犧蟦癎摖壥禦籋萷犸粹瘛榞梄螢圓因苧璡屨灇炀瞸瘊暍严帉戀㴃" {
t.Fail()
}
d, err := UTF82utf16be(es)
if string(d) == string(e) {
if err == nil {

View File

@@ -1,19 +0,0 @@
// +build !386,!arm,!mipsle,!amd64,!arm64,!ppc64le,!mips64le
package base14
func EncodeString(s string) []byte {
return []byte{"stub!"}
}
func DecodeString(s string) []byte {
return []byte{"stub!"}
}
func Encode(b []byte) []byte {
return []byte{"stub!"}
}
func Decode(b []byte) []byte {
return []byte{"stub!"}
}

5
go.mod
View File

@@ -2,7 +2,4 @@ module github.com/fumiama/go-base16384
go 1.16
require (
github.com/wdvxdr1123/ZeroBot v1.3.2
golang.org/x/text v0.3.6
)
require golang.org/x/text v0.3.7

40
go.sum
View File

@@ -1,39 +1,3 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
github.com/t-tomalak/logrus-easy-formatter v0.0.0-20190827215021-c074f06c5816/go.mod h1:tzym/CEb5jnFI+Q0k4Qq3+LvRF4gO3E2pxS8fHP8jcA=
github.com/tidwall/gjson v1.8.0/go.mod h1:5/xDoumyyDNerp2U36lyolv46b3uF/9Bu6OfyQ9GImk=
github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.1.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/wdvxdr1123/ZeroBot v1.3.2 h1:EFZNb3awNbwxRtmDkWv3PH6Z9rUV6ZLFa3hBmRMRRCA=
github.com/wdvxdr1123/ZeroBot v1.3.2/go.mod h1:i2DIqQjtjE+3gvVi9r9sc+QpNaUuyTXx/HNXXayIpwI=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

20
helper.go Normal file
View File

@@ -0,0 +1,20 @@
package base14
import (
"unsafe"
)
// BytesToString 没有内存开销的转换
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
// StringToBytes 没有内存开销的转换
func StringToBytes(s string) (b []byte) {
bh := (*slice)(unsafe.Pointer(&b))
sh := (*slice)(unsafe.Pointer(&s))
bh.Data = sh.Data
bh.Len = sh.Len
bh.Cap = sh.Len
return b
}