1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 11:07:40 +08:00

binary: better bce for NewTeaCipher

This commit is contained in:
wdvxdr 2022-04-14 21:37:04 +08:00
parent 6e7053381e
commit 1b7e3d8580
No known key found for this signature in database
GPG Key ID: 703F8C071DE7A1B6
2 changed files with 17 additions and 13 deletions

View File

@ -8,6 +8,7 @@ import (
type TEA [4]uint32 type TEA [4]uint32
// randuint32 returns a lock free uint32 value. // randuint32 returns a lock free uint32 value.
//
//go:linkname randuint32 runtime.fastrand //go:linkname randuint32 runtime.fastrand
func randuint32() uint32 func randuint32() uint32
@ -95,6 +96,7 @@ func (t *TEA) encode(n uint64) uint64 {
} }
// 每次8字节 // 每次8字节
//
//go:nosplit //go:nosplit
func (t *TEA) decode(n uint64) uint64 { func (t *TEA) decode(n uint64) uint64 {
v0, v1 := uint32(n>>32), uint32(n) v0, v1 := uint32(n>>32), uint32(n)
@ -141,9 +143,9 @@ func NewTeaCipher(key []byte) (t TEA) {
if len(key) != 16 { if len(key) != 16 {
return TEA{} return TEA{}
} }
t[3] = binary.BigEndian.Uint32(key[12:]) t[3] = binary.BigEndian.Uint32(key[12:16])
t[2] = binary.BigEndian.Uint32(key[8:]) t[2] = binary.BigEndian.Uint32(key[8:12])
t[1] = binary.BigEndian.Uint32(key[4:]) t[1] = binary.BigEndian.Uint32(key[4:8])
t[0] = binary.BigEndian.Uint32(key[0:]) t[0] = binary.BigEndian.Uint32(key[:4])
return t return t
} }

View File

@ -8,19 +8,21 @@ import (
var etx = []byte{0x29} var etx = []byte{0x29}
// newFrame 包格式 // newFrame 包格式
// * STX: 0x28(40) //
// * head length // - STX: 0x28(40)
// * body length // - head length
// * head data // - body length
// * body data // - head data
// * ETX: 0x29(41) // - body data
// - ETX: 0x29(41)
//
// 节省内存, 可被go runtime优化为writev操作 // 节省内存, 可被go runtime优化为writev操作
func newFrame(head []byte, body []byte) net.Buffers { func newFrame(head []byte, body []byte) net.Buffers {
buffers := make(net.Buffers, 4) buffers := make(net.Buffers, 4)
// buffer0 format: // buffer0 format:
// * STX // - STX
// * head length // - head length
// * body length // - body length
buffer0 := make([]byte, 9) buffer0 := make([]byte, 9)
buffer0[0] = 0x28 buffer0[0] = 0x28
binary.BigEndian.PutUint32(buffer0[1:], uint32(len(head))) binary.BigEndian.PutUint32(buffer0[1:], uint32(len(head)))