-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversion.go
116 lines (89 loc) · 2.35 KB
/
conversion.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
package toyls
import "errors"
const (
cipherSuiteLen = 2
randomLen = 32
protocolVersionLen = 2
)
func extractUint16(src []byte) (n uint16, p []byte) {
n |= uint16(src[0]) << 8
n |= uint16(src[1])
p = src[2:]
return
}
func extractUint24(src []byte) (n uint32, p []byte) {
n |= uint32(src[0]) << 16
n |= uint32(src[1]) << 8
n |= uint32(src[2])
p = src[3:]
return
}
func extractUint32(src []byte) (n uint32, p []byte) {
n |= uint32(src[0]) << 24
n |= uint32(src[1]) << 16
n |= uint32(src[2]) << 8
n |= uint32(src[3])
p = src[4:]
return
}
func extractProtocolVersion(src []byte) (protocolVersion, []byte) {
return protocolVersion{
src[0], src[1],
}, src[2:]
}
func extractRandom(src []byte) (random, []byte) {
r := random{}
r.gmtUnixTime, src = extractUint32(src)
copy(r.randomBytes[:], src[:28])
return r, src[28:]
}
func extractSessionID(src []byte) ([]byte, []byte) {
sessionLen := int(src[0])
sessionID := make([]byte, sessionLen)
copy(sessionID, src[1:1+sessionLen])
return sessionID, src[1+sessionLen:]
}
func extractCipherSuites(src []byte) ([]cipherSuite, []byte, error) {
vectorLen, p := extractUint16(src)
if vectorLen < 2 || vectorLen > 0xfffe {
return nil, p, errors.New("The cipher suite vector should contain <2..2^16-2> bytes.")
}
n := vectorLen / cipherSuiteLen
dst := make([]cipherSuite, n)
for i := 0; i < len(dst); i++ {
s := &dst[i]
copy(s[:], p[i*2:i*2+2])
}
return dst, p[vectorLen:], nil
}
func extractCipherSuite(dst, src []byte) []byte {
copy(dst, src[:2])
return src[2:]
}
func extractCompressionMethods(src []byte) ([]byte, []byte, error) {
vectorLen := int(src[0])
if vectorLen < 1 || vectorLen > 0xff {
return nil, src[1:], errors.New("The compression methods vector should contain <1..2^8-2> bytes.")
}
compressionMethods := make([]byte, vectorLen)
copy(compressionMethods, src[1:1+vectorLen])
return compressionMethods, src[1+vectorLen:], nil
}
func writeBytesFromUint16(n uint16) (dst [2]byte) {
dst[0] = byte(n >> 8 & 0xff)
dst[1] = byte(n & 0xff)
return
}
func writeBytesFromUint24(n uint32) (dst [3]byte) {
dst[0] = byte(n >> 16 & 0xff)
dst[1] = byte(n >> 8 & 0xff)
dst[2] = byte(n & 0xff)
return
}
func writeBytesFromUint32(n uint32) (dst [4]byte) {
dst[0] = byte(n >> 24 & 0xff)
dst[1] = byte(n >> 16 & 0xff)
dst[2] = byte(n >> 8 & 0xff)
dst[3] = byte(n & 0xff)
return
}