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

View File

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