forked from Mrs4s/MiraiGo
/
utils.go
132 lines (114 loc) · 2.47 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package binary
import (
"bytes"
binary2 "encoding/binary"
"encoding/hex"
"io"
"net"
"github.com/klauspost/compress/gzip"
"github.com/klauspost/compress/zlib"
"github.com/Mrs4s/MiraiGo/utils"
)
type GzipWriter struct {
w *gzip.Writer
buf *bytes.Buffer
}
func (w *GzipWriter) Write(p []byte) (int, error) {
return w.w.Write(p)
}
func (w *GzipWriter) Close() error {
return w.w.Close()
}
func (w *GzipWriter) Bytes() []byte {
return w.buf.Bytes()
}
func ZlibUncompress(src []byte) []byte {
b := bytes.NewReader(src)
var out bytes.Buffer
r, _ := zlib.NewReader(b)
defer r.Close()
io.Copy(&out, r)
return out.Bytes()
}
func ZlibCompress(data []byte) []byte {
zw := acquireZlibWriter()
_, _ = zw.w.Write(data)
_ = zw.w.Close()
ret := append([]byte(nil), zw.buf.Bytes()...)
releaseZlibWriter(zw)
return ret
}
func GZipCompress(data []byte) []byte {
gw := AcquireGzipWriter()
_, _ = gw.Write(data)
_ = gw.Close()
ret := append([]byte(nil), gw.buf.Bytes()...)
ReleaseGzipWriter(gw)
return ret
}
func GZipUncompress(src []byte) []byte {
b := bytes.NewReader(src)
var out bytes.Buffer
r, _ := gzip.NewReader(b)
defer r.Close()
_, _ = io.Copy(&out, r)
return out.Bytes()
}
func CalculateImageResourceId(md5 []byte) string {
id := make([]byte, 36+6)[:0]
id = append(id, '{')
AppendUUID(id[1:], md5)
id = id[:37]
id = append(id, "}.png"...)
return utils.B2S(bytes.ToUpper(id))
}
func GenUUID(uuid []byte) []byte {
return AppendUUID(nil, uuid)
}
func AppendUUID(dst []byte, uuid []byte) []byte {
_ = uuid[15]
if cap(dst) > 36 {
dst = dst[:36]
dst[8] = '-'
dst[13] = '-'
dst[18] = '-'
dst[23] = '-'
} 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 ToIPV4Address(arr []byte) string {
ip := (net.IP)(arr)
return ip.String()
}
func UInt32ToIPV4Address(i uint32) string {
ip := net.IP{0, 0, 0, 0}
binary2.LittleEndian.PutUint32(ip, i)
return ip.String()
}
func ToChunkedBytesF(b []byte, size int, f func([]byte)) {
r := NewReader(b)
for r.Len() >= size {
f(r.ReadBytes(size))
}
if r.Len() > 0 {
f(r.ReadAvailable())
}
}
func ToBytes(i interface{}) []byte {
return NewWriterF(func(w *Writer) {
// TODO: more types
switch t := i.(type) {
case int16:
w.WriteUInt16(uint16(t))
case int32:
w.WriteUInt32(uint32(t))
}
})
}