forked from Mrs4s/MiraiGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
101 lines (90 loc) · 1.8 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
package binary
import (
"bytes"
"compress/gzip"
"compress/zlib"
binary2 "encoding/binary"
"encoding/hex"
"fmt"
"io"
"net"
"strings"
)
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 {
buf := new(bytes.Buffer)
w := zlib.NewWriter(buf)
_, _ = w.Write(data)
w.Close()
return buf.Bytes()
}
func GZipCompress(data []byte) []byte {
buf := new(bytes.Buffer)
w := gzip.NewWriter(buf)
_, _ = w.Write(data)
_ = w.Close()
return buf.Bytes()
}
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 {
return strings.ToUpper(fmt.Sprintf(
"{%s}.png", GenUUID(md5),
))
}
func GenUUID(uuid []byte) string {
u := uuid[0:16]
buf := make([]byte, 36)
hex.Encode(buf[0:], u[0:4])
buf[8] = '-'
hex.Encode(buf[9:], u[4:6])
buf[13] = '-'
hex.Encode(buf[14:], u[6:8])
buf[18] = '-'
hex.Encode(buf[19:], u[8:10])
buf[23] = '-'
hex.Encode(buf[24:], u[10:16])
return string(buf)
}
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))
}
})
}