github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/binary/be-in-be-cpu.go (about) 1 // For license and copyright information please see LEGAL file in repository 2 3 //go:build armbe || arm64be || mips || mips64 || mips64p32 || ppc || ppc64 || s390 || s390x || sparc || sparc64 4 5 package binary 6 7 import ( 8 "unsafe" 9 ) 10 11 // BigEndian is the big-endian implementation to get|set from be binary to be cpu 12 var BigEndian bigEndian 13 14 type bigEndian struct{} 15 16 func (bigEndian) Uint16(b []byte) uint16 { return uint16(b[0]) | uint16(b[1])<<8 } 17 func (bigEndian) Uint32(b []byte) uint32 { 18 return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24 19 } 20 func (bigEndian) Uint64(b []byte) uint64 { 21 return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | 22 uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56 23 } 24 25 func (bigEndian) PutUint16(b []byte, v uint16) { 26 b[0] = byte(v) 27 b[1] = byte(v >> 8) 28 } 29 func (bigEndian) PutUint32(b []byte, v uint32) { 30 b[0] = byte(v) 31 b[1] = byte(v >> 8) 32 b[2] = byte(v >> 16) 33 b[3] = byte(v >> 24) 34 } 35 func (bigEndian) PutUint64(b []byte, v uint64) { 36 b[0] = byte(v) 37 b[1] = byte(v >> 8) 38 b[2] = byte(v >> 16) 39 b[3] = byte(v >> 24) 40 b[4] = byte(v >> 32) 41 b[5] = byte(v >> 40) 42 b[6] = byte(v >> 48) 43 b[7] = byte(v >> 56) 44 } 45 46 func init() { 47 i := uint32(1) 48 b := (*[4]byte)(unsafe.Pointer(&i)) 49 if b[0] == 1 { 50 panic("Expect BigEndian CPU but have LittleEndian CPU that cause many problem in other packages") 51 } 52 }