diff --git a/README.md b/README.md index 8552b4f..edf6800 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ # go-base16384 base16384 interface of golang + +# Functions + +## func Encode(b []byte) []byte +Encode b to utf16be. +## func Decode(b []byte) []byte +Decode from encoded b. +## func UTF16be2utf8(b []byte) ([]byte, error) +Display the result. +## func UTF82utf16be(b []byte) ([]byte, error) +Turn the result to its original coding form to decode. + +# Usage +## As package +Just include it in your project. +## As lib +Copy this repo to `$GOPATH/src`, then execute +```bash +go install -buildmode=shared -linkshared std +go install -buildmode=shared -linkshared base14 +``` +Now you can use +```bash +go build -linkshared ... +``` \ No newline at end of file diff --git a/base1432.go b/base1432.go new file mode 100644 index 0000000..205e895 --- /dev/null +++ b/base1432.go @@ -0,0 +1,29 @@ +// +build 386 arm + +// Package base14 base16384 的 go 接口 +package base14 + +// #cgo CFLAGS: -I/usr/local/include +// #cgo LDFLAGS: -L/usr/local/lib -lbase14 +// #include +// #include +import "C" +import ( + "unsafe" +) + +func Encode(b []byte) []byte { + ld := C.encode((*C.uchar)(unsafe.Pointer(&b[0])), (uint)(len(b))) + ldlen := uintptr(ld.len) + // []byte 的数据结构 + e := [3]uintptr{uintptr(unsafe.Pointer(ld.data)), ldlen, ldlen} + return *(*[]byte)(unsafe.Pointer(&e)) +} + +func Decode(b []byte) []byte { + ld := C.decode((*C.uchar)(unsafe.Pointer(&b[0])), (uint)(len(b))) + ldlen := uintptr(ld.len) + // []byte 的数据结构 + e := [3]uintptr{uintptr(unsafe.Pointer(ld.data)), ldlen, ldlen} + return *(*[]byte)(unsafe.Pointer(&e)) +} diff --git a/base1464.go b/base1464.go new file mode 100644 index 0000000..3ff1f8f --- /dev/null +++ b/base1464.go @@ -0,0 +1,29 @@ +// +build amd64 arm64 + +// Package base14 base16384 的 go 接口 +package base14 + +// #cgo CFLAGS: -I/usr/local/include +// #cgo LDFLAGS: -L/usr/local/lib -lbase14 +// #include +// #include +import "C" +import ( + "unsafe" +) + +func Encode(b []byte) []byte { + ld := C.encode((*C.uchar)(unsafe.Pointer(&b[0])), (C.ulong)(len(b))) + ldlen := uintptr(ld.len) + // []byte 的数据结构 + e := [3]uintptr{uintptr(unsafe.Pointer(ld.data)), ldlen, ldlen} + return *(*[]byte)(unsafe.Pointer(&e)) +} + +func Decode(b []byte) []byte { + ld := C.decode((*C.uchar)(unsafe.Pointer(&b[0])), (C.ulong)(len(b))) + ldlen := uintptr(ld.len) + // []byte 的数据结构 + e := [3]uintptr{uintptr(unsafe.Pointer(ld.data)), ldlen, ldlen} + return *(*[]byte)(unsafe.Pointer(&e)) +} diff --git a/conv.go b/conv.go new file mode 100644 index 0000000..1cc32f9 --- /dev/null +++ b/conv.go @@ -0,0 +1,15 @@ +package base14 + +import ( + "golang.org/x/text/encoding/unicode" +) + +var format = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM) + +func UTF16be2utf8(b []byte) ([]byte, error) { + return format.NewDecoder().Bytes(b) +} + +func UTF82utf16be(b []byte) ([]byte, error) { + return format.NewEncoder().Bytes(b) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8d5766a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/fumiama/go-base16384 + +go 1.16 + +require golang.org/x/text v0.3.6 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3c12b4f --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +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/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=