forked from LinuxCppServer/gofsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
43 lines (35 loc) · 905 Bytes
/
encode.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
package gofsm
import (
"bytes"
"compress/zlib"
)
const mapper = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"
//进行zlib压缩
func deflate(input []byte) []byte {
var b bytes.Buffer
w, _ := zlib.NewWriterLevel(&b, zlib.BestCompression)
_, _ = w.Write(input)
_ = w.Close()
return b.Bytes()
}
func encode(raw string) string {
return base64Encode(deflate([]byte(raw)))
}
func base64Encode(input []byte) string {
var buffer bytes.Buffer
inputLength := len(input)
for i := 0; i < 3-inputLength%3; i++ {
input = append(input, byte(0))
}
for i := 0; i < inputLength; i += 3 {
b1, b2, b3, b4 := input[i], input[i+1], input[i+2], byte(0)
b4 = b3 & 0x3f
b3 = ((b2 & 0xf) << 2) | (b3 >> 6)
b2 = ((b1 & 0x3) << 4) | (b2 >> 4)
b1 = b1 >> 2
for _, b := range []byte{b1, b2, b3, b4} {
buffer.WriteByte(byte(mapper[b]))
}
}
return string(buffer.Bytes())
}