1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00

加快图片UUID生成 减少内存占用

This commit is contained in:
Lin 2021-03-06 19:55:05 +08:00
parent a2937b8b01
commit 8c0c269c72
3 changed files with 43 additions and 30 deletions

View File

@ -17,9 +17,7 @@ func isZero(a []byte) bool { // MAGIC
return *(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&a)).Data)) == 0 return *(*uint64)(unsafe.Pointer((*reflect.SliceHeader)(unsafe.Pointer(&a)).Data)) == 0
} }
type TEA struct { type TEA [4]uint32
key [4]uint32
}
// http://bbs.chinaunix.net/thread-583468-1-1.html // http://bbs.chinaunix.net/thread-583468-1-1.html
// 感谢xichen大佬对TEA的解释 // 感谢xichen大佬对TEA的解释
@ -138,8 +136,8 @@ var sumTable = [0x10]uint32{
func (t *TEA) encode(src, dst []byte) { func (t *TEA) encode(src, dst []byte) {
v0, v1 := unpack(src) v0, v1 := unpack(src)
for i := 0; i < 0x10; i++ { for i := 0; i < 0x10; i++ {
v0 += ((v1 << 4) + t.key[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t.key[1]) v0 += ((v1 << 4) + t[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t[1])
v1 += ((v0 << 4) + t.key[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t.key[3]) v1 += ((v0 << 4) + t[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t[3])
} }
repack(dst, v0, v1) repack(dst, v0, v1)
} }
@ -149,8 +147,8 @@ func (t *TEA) encode(src, dst []byte) {
func (t *TEA) decode(src, dst []byte) { func (t *TEA) decode(src, dst []byte) {
v0, v1 := unpack(src) v0, v1 := unpack(src)
for i := 0xf; i >= 0; i-- { for i := 0xf; i >= 0; i-- {
v1 -= ((v0 << 4) + t.key[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t.key[3]) v1 -= ((v0 << 4) + t[2]) ^ (v0 + sumTable[i]) ^ ((v0 >> 5) + t[3])
v0 -= ((v1 << 4) + t.key[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t.key[1]) v0 -= ((v1 << 4) + t[0]) ^ (v1 + sumTable[i]) ^ ((v1 >> 5) + t[1])
} }
repack(dst, v0, v1) repack(dst, v0, v1)
} }
@ -161,9 +159,9 @@ func NewTeaCipher(key []byte) *TEA {
return nil return nil
} }
t := new(TEA) t := new(TEA)
t.key[3] = binary.BigEndian.Uint32(key[12:]) t[3] = binary.BigEndian.Uint32(key[12:])
t.key[2] = binary.BigEndian.Uint32(key[8:]) t[2] = binary.BigEndian.Uint32(key[8:])
t.key[1] = binary.BigEndian.Uint32(key[4:]) t[1] = binary.BigEndian.Uint32(key[4:])
t.key[0] = binary.BigEndian.Uint32(key[0:]) t[0] = binary.BigEndian.Uint32(key[0:])
return t return t
} }

View File

@ -6,10 +6,9 @@ import (
"compress/zlib" "compress/zlib"
binary2 "encoding/binary" binary2 "encoding/binary"
"encoding/hex" "encoding/hex"
"fmt" "github.com/Mrs4s/MiraiGo/utils"
"io" "io"
"net" "net"
"strings"
) )
func ZlibUncompress(src []byte) []byte { func ZlibUncompress(src []byte) []byte {
@ -47,24 +46,40 @@ func GZipUncompress(src []byte) []byte {
} }
func CalculateImageResourceId(md5 []byte) string { func CalculateImageResourceId(md5 []byte) string {
return strings.ToUpper(fmt.Sprintf( id := make([]byte, 36+6)[:0]
"{%s}.png", GenUUID(md5), id = append(id, '{')
)) AppendUUID(id[1:], md5)
id = id[:37]
id = append(id, "}.png"...)
return utils.B2S(bytes.ToUpper(id))
} }
func GenUUID(uuid []byte) string { func GenUUID(uuid []byte) []byte {
u := uuid[0:16] return AppendUUID(nil, uuid)
buf := make([]byte, 36) }
hex.Encode(buf[0:], u[0:4])
buf[8] = '-' func AppendUUID(dst []byte, uuid []byte) []byte {
hex.Encode(buf[9:], u[4:6]) _ = uuid[15]
buf[13] = '-' if cap(dst) > 36 {
hex.Encode(buf[14:], u[6:8]) dst = dst[:36]
buf[18] = '-' dst[8] = '-'
hex.Encode(buf[19:], u[8:10]) dst[13] = '-'
buf[23] = '-' dst[18] = '-'
hex.Encode(buf[24:], u[10:16]) dst[23] = '-'
return string(buf) } else { // Need Grow
dst = append(dst, "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"...)
}
hex.Encode(dst[0:], uuid[0:4])
hex.Encode(dst[9:], uuid[4:6])
hex.Encode(dst[14:], uuid[6:8])
hex.Encode(dst[19:], uuid[8:10])
hex.Encode(dst[24:], uuid[10:16])
return dst
}
func genUUID(uuid []byte, dst []byte) {
} }
func ToIPV4Address(arr []byte) string { func ToIPV4Address(arr []byte) string {

View File

@ -173,7 +173,7 @@ func GenRandomDevice() {
rand.Read(r) rand.Read(r)
SystemDeviceInfo.Display = []byte("MIRAI." + utils.RandomStringRange(6, NumberRange) + ".001") SystemDeviceInfo.Display = []byte("MIRAI." + utils.RandomStringRange(6, NumberRange) + ".001")
SystemDeviceInfo.FingerPrint = []byte("mamoe/mirai/mirai:10/MIRAI.200122.001/" + utils.RandomStringRange(7, NumberRange) + ":user/release-keys") SystemDeviceInfo.FingerPrint = []byte("mamoe/mirai/mirai:10/MIRAI.200122.001/" + utils.RandomStringRange(7, NumberRange) + ":user/release-keys")
SystemDeviceInfo.BootId = []byte(binary.GenUUID(r)) SystemDeviceInfo.BootId = binary.GenUUID(r)
SystemDeviceInfo.ProcVersion = []byte("Linux version 3.0.31-" + utils.RandomString(8) + " (android-build@xxx.xxx.xxx.xxx.com)") SystemDeviceInfo.ProcVersion = []byte("Linux version 3.0.31-" + utils.RandomString(8) + " (android-build@xxx.xxx.xxx.xxx.com)")
rand.Read(r) rand.Read(r)
t := md5.Sum(r) t := md5.Sum(r)