Skip to content

Commit

Permalink
add big-endian support
Browse files Browse the repository at this point in the history
  • Loading branch information
IncSW committed Aug 21, 2021
1 parent d2fafcc commit 38885e4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 28 deletions.
18 changes: 6 additions & 12 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,39 +273,33 @@ func (e *Encoding) decode(dst []byte, src []byte) int {
}
}
}
up := (*[3]byte)(unsafe.Pointer(&u))
up := (*[4]byte)(unsafe.Pointer(&u))
switch l {
case 4:
if !e.pad && op-opstart+3 > dstlen {
return 0
}
u = ctou32(ip)
u = (e.lutXd0[byte(u)] | e.lutXd1[byte(u>>8)] | e.lutXd2[byte(u>>16)] | e.lutXd3[u>>24])
*(*byte)(unsafe.Pointer(op)) = up[0]
op++
*(*byte)(unsafe.Pointer(op)) = up[1]
op++
*(*byte)(unsafe.Pointer(op)) = up[2]
op++
putTail(op, up, 3)
op += 3
cu |= u
break
case 3:
if !e.pad && op-opstart+2 > dstlen {
return 0
}
u = e.lutXd0[*(*byte)(unsafe.Pointer(ip + 0))] | e.lutXd1[*(*byte)(unsafe.Pointer(ip + 1))] | e.lutXd2[*(*byte)(unsafe.Pointer(ip + 2))]
*(*byte)(unsafe.Pointer(op)) = up[0]
op++
*(*byte)(unsafe.Pointer(op)) = up[1]
op++
putTail(op, up, 2)
op += 2
cu |= u
break
case 2:
if !e.pad && op-opstart >= dstlen {
return 0
}
u = e.lutXd0[*(*byte)(unsafe.Pointer(ip + 0))] | e.lutXd1[*(*byte)(unsafe.Pointer(ip + 1))]
*(*byte)(unsafe.Pointer(op)) = up[0]
putTail(op, up, 1)
op++
cu |= u
break
Expand Down
20 changes: 20 additions & 0 deletions decoder_be.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build armbe || arm64be || mips || mips64 || mips64p32 || ppc || ppc64 || sparc || sparc64 || s390 || s390x
// +build armbe arm64be mips mips64 mips64p32 ppc ppc64 sparc sparc64 s390 s390x

package base64

import "unsafe"

func putTail(ptr uintptr, tail *[4]byte, n int) {
switch n {
case 3:
*(*byte)(unsafe.Pointer(ptr)) = tail[3]
*(*byte)(unsafe.Pointer(ptr + 1)) = tail[2]
*(*byte)(unsafe.Pointer(ptr + 2)) = tail[1]
case 2:
*(*byte)(unsafe.Pointer(ptr)) = tail[3]
*(*byte)(unsafe.Pointer(ptr + 1)) = tail[2]
case 1:
*(*byte)(unsafe.Pointer(ptr)) = tail[3]
}
}
20 changes: 20 additions & 0 deletions decoder_le.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build 386 || amd64 || amd64p32 || arm || arm64 || mipsle || mis64le || mips64p32le || ppc64le || riscv || riscv64 || wasm
// +build 386 amd64 amd64p32 arm arm64 mipsle mis64le mips64p32le ppc64le riscv riscv64 wasm

package base64

import "unsafe"

func putTail(ptr uintptr, tail *[4]byte, n int) {
switch n {
case 3:
*(*byte)(unsafe.Pointer(ptr)) = tail[0]
*(*byte)(unsafe.Pointer(ptr + 1)) = tail[1]
*(*byte)(unsafe.Pointer(ptr + 2)) = tail[2]
case 2:
*(*byte)(unsafe.Pointer(ptr)) = tail[0]
*(*byte)(unsafe.Pointer(ptr + 1)) = tail[1]
case 1:
*(*byte)(unsafe.Pointer(ptr)) = tail[0]
}
}
16 changes: 0 additions & 16 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package base64

import (
"math"
"math/bits"
"unsafe"
)

Expand Down Expand Up @@ -30,21 +29,6 @@ func s2b(value string) (b []byte) {
return b
}

//go:nosplit
func bswap32(ptr uintptr) uint32 {
return bits.ReverseBytes32(*(*uint32)(unsafe.Pointer(ptr)))
}

//go:nosplit
func stou32(cp uintptr, x uint32) {
*(*uint32)(unsafe.Pointer(cp)) = x
}

//go:nosplit
func ctou32(cp uintptr) uint32 {
return *(*uint32)(unsafe.Pointer(cp))
}

func makeLuts(lutSe [64]byte) ([4096]uint32, [256]uint32, [256]uint32, [256]uint32, [256]uint32) {
lutXe := [4096]uint32{}
lutXd0 := [256]uint32{}
Expand Down
24 changes: 24 additions & 0 deletions utils_be.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build armbe || arm64be || mips || mips64 || mips64p32 || ppc || ppc64 || sparc || sparc64 || s390 || s390x
// +build armbe arm64be mips mips64 mips64p32 ppc ppc64 sparc sparc64 s390 s390x

package base64

import (
"math/bits"
"unsafe"
)

//go:nosplit
func bswap32(ptr uintptr) uint32 {
return *(*uint32)(unsafe.Pointer(ptr))
}

//go:nosplit
func stou32(cp uintptr, x uint32) {
*(*uint32)(unsafe.Pointer(cp)) = bits.ReverseBytes32(x)
}

//go:nosplit
func ctou32(cp uintptr) uint32 {
return bits.ReverseBytes32(*(*uint32)(unsafe.Pointer(cp)))
}
24 changes: 24 additions & 0 deletions utils_le.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//go:build 386 || amd64 || amd64p32 || arm || arm64 || mipsle || mis64le || mips64p32le || ppc64le || riscv || riscv64 || wasm
// +build 386 amd64 amd64p32 arm arm64 mipsle mis64le mips64p32le ppc64le riscv riscv64 wasm

package base64

import (
"math/bits"
"unsafe"
)

//go:nosplit
func bswap32(ptr uintptr) uint32 {
return bits.ReverseBytes32(*(*uint32)(unsafe.Pointer(ptr)))
}

//go:nosplit
func stou32(cp uintptr, x uint32) {
*(*uint32)(unsafe.Pointer(cp)) = x
}

//go:nosplit
func ctou32(cp uintptr) uint32 {
return *(*uint32)(unsafe.Pointer(cp))
}

0 comments on commit 38885e4

Please sign in to comment.