-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackets.go
148 lines (138 loc) · 3.04 KB
/
packets.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package packets
import (
"bytes"
"encoding/binary"
"encoding/hex"
"io"
)
func ReadB64(b []byte, offset int) (uint64, error) {
if len(b) < 8+offset {
return 0, io.EOF
}
return binary.BigEndian.Uint64(b[offset : offset+8]), nil
}
func ReadB32(b []byte, offset int) (uint32, error) {
if len(b) < 4+offset {
return 0, io.EOF
}
return binary.BigEndian.Uint32(b[offset : offset+4]), nil
}
func ReadB24(b []byte, offset int) (uint32, error) {
if len(b) < 3+offset {
return 0, io.EOF
}
b = b[offset:]
return uint32(b[0])<<16 | uint32(b[1])<<8 | uint32(b[2]), nil
}
func ReadB16(b []byte, offset int) (uint16, error) {
if len(b) < 2+offset {
return 0, io.EOF
}
return binary.BigEndian.Uint16(b[offset : offset+2]), nil
}
func ReadB8(b []byte, offset int) (uint8, error) {
if len(b) < 1+offset {
return 0, io.EOF
}
return b[offset], nil
}
func ReadSB32(b []byte, offset int) (int32, error) {
if len(b) < 4+offset {
return 0, io.EOF
}
return int32(binary.BigEndian.Uint32(b[offset : offset+4])), nil
}
func ReadSlice(b []byte, off int, lim int, delim byte) (line []byte, err error) {
i := bytes.IndexByte(b[off:], delim)
end := off + i
if i < 0 {
end = len(b)
err = io.EOF
}
line = b[off:end]
return line, err
}
func WriteB64(b []byte, offset int, v uint64) error {
if len(b) < 8+offset {
return io.EOF
}
binary.BigEndian.PutUint64(b[offset:offset+8], v)
return nil
}
func WriteB32(b []byte, offset int, v uint32) error {
if len(b) < 4+offset {
return io.EOF
}
binary.BigEndian.PutUint32(b[offset:offset+4], v)
return nil
}
func WriteB24(b []byte, offset int, v uint32) error {
if len(b) < 3+offset {
return io.EOF
}
b = b[offset : offset+3]
b[0] = byte(v >> 16)
b[1] = byte(v >> 8)
b[2] = byte(v)
return nil
}
func WriteB16(b []byte, offset int, v uint16) error {
if len(b) < 2+offset {
return io.EOF
}
binary.BigEndian.PutUint16(b[offset:offset+2], v)
return nil
}
func WriteB8(b []byte, offset int, v uint8) error {
if len(b) < offset {
return io.EOF
}
b[offset] = v
return nil
}
func WriteSB32(b []byte, offset int, v int32) error {
if len(b) < 4+offset {
return io.EOF
}
binary.BigEndian.PutUint32(b[offset:offset+4], uint32(v))
return nil
}
//
// misc
//
var (
// Integer.numberOfTrailingZeros
// http://stackoverflow.com/questions/5471129/number-of-trailing-zeros
// http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup
ZerosOnRightModLookup = []int{
32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17,
0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18,
}
)
// zeros rightmost bit
func Zrb(n uint32) uint32 {
return n ^ (n & (-n))
}
// counts number of trailing zeros
func Ntz(i uint32) int {
return ZerosOnRightModLookup[(i&-i)%37]
}
/*
* Converts a string of hex to byte array
*/
func Hexit(b string) *bytes.Buffer {
s := []byte(b)
temp := []byte{0}
idx := 0
var buffer bytes.Buffer
for idx <= len(s)-2 {
_, err := hex.Decode(temp, s[idx:idx+2])
if err == nil {
buffer.Write(temp)
idx += 2
} else {
idx += 1
}
}
return &buffer
}