forked from dotabuff/manta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
311 lines (260 loc) · 6.1 KB
/
reader.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package manta
import (
"encoding/binary"
"fmt"
"math"
)
// reader performs read operations against a buffer
type reader struct {
buf []byte
size uint32
pos uint32
bitVal uint64 // value of the remaining bits in the current byte
bitCount uint32 // number of remaining bits in the current byte
}
// newReader creates a new reader object for the given buffer
func newReader(buf []byte) *reader {
return &reader{buf, uint32(len(buf)), 0, 0, 0}
}
// remBits calculates the number of unread bits in the buffer
func (r *reader) remBits() uint32 {
return r.remBytes() + r.bitCount
}
func (r *reader) position() string {
if r.bitCount > 0 {
return fmt.Sprintf("%d.%d", r.pos-1, 8-r.bitCount)
}
return fmt.Sprintf("%d", r.pos)
}
// remBytes calculates the number of unread bytes in the buffer
func (r *reader) remBytes() uint32 {
return r.size - r.pos
}
// nextByte reads the next byte from the buffer
func (r *reader) nextByte() byte {
r.pos += 1
if r.pos > r.size {
_panicf("nextByte: insufficient buffer (%d of %d)", r.pos, r.size)
}
return r.buf[r.pos-1]
}
// readBits returns the uint32 value for the given number of sequential bits
func (r *reader) readBits(n uint32) uint32 {
for n > r.bitCount {
r.bitVal |= uint64(r.nextByte()) << r.bitCount
r.bitCount += 8
}
x := (r.bitVal & ((1 << n) - 1))
r.bitVal >>= n
r.bitCount -= n
return uint32(x)
}
// readByte reads a single byte
func (r *reader) readByte() byte {
// Fast path if we're byte aligned
if r.bitCount == 0 {
return r.nextByte()
}
return byte(r.readBits(8))
}
// readBytes reads the given number of bytes
func (r *reader) readBytes(n uint32) []byte {
// Fast path if we're byte aligned
if r.bitCount == 0 {
r.pos += n
if r.pos > r.size {
_panicf("readBytes: insufficient buffer (%d of %d)", r.pos, r.size)
}
return r.buf[r.pos-n : r.pos]
}
buf := make([]byte, n)
for i := uint32(0); i < n; i++ {
buf[i] = byte(r.readBits(8))
}
return buf
}
// readLeUint32 reads an little-endian uint32
func (r *reader) readLeUint32() uint32 {
return binary.LittleEndian.Uint32(r.readBytes(4))
}
// readLeUint64 reads a little-endian uint64
func (r *reader) readLeUint64() uint64 {
return binary.LittleEndian.Uint64(r.readBytes(8))
}
// readVarUint64 reads an unsigned 32-bit varint
func (r *reader) readVarUint32() uint32 {
var x, s uint32
for {
b := uint32(r.readByte())
x |= (b & 0x7F) << s
s += 7
if ((b & 0x80) == 0) || (s == 35) {
break
}
}
return x
}
// readVarInt64 reads a signed 32-bit varint
func (r *reader) readVarInt32() int32 {
ux := r.readVarUint32()
x := int32(ux >> 1)
if ux&1 != 0 {
x = ^x
}
return x
}
// readVarUint64 reads an unsigned 64-bit varint
func (r *reader) readVarUint64() uint64 {
var x, s uint64
for i := 0; ; i++ {
b := r.readByte()
if b < 0x80 {
if i > 9 || i == 9 && b > 1 {
_panicf("read overflow: varint overflows uint64")
}
return x | uint64(b)<<s
}
x |= uint64(b&0x7f) << s
s += 7
}
}
// readVarInt64 reads a signed 64-bit varint
func (r *reader) readVarInt64() int64 {
ux := r.readVarUint64()
x := int64(ux >> 1)
if ux&1 != 0 {
x = ^x
}
return x
}
// readBoolean reads and interprets single bit as true or false
func (r *reader) readBoolean() bool {
return r.readBits(1) == 1
}
// readFloat reads an IEEE 754 float
func (r *reader) readFloat() float32 {
return math.Float32frombits(r.readLeUint32())
}
// readUBitVar reads a variable length uint32 with encoding in last to bits of 6 bit group
func (r *reader) readUBitVar() uint32 {
ret := r.readBits(6)
switch ret & 0x30 {
case 16:
ret = (ret & 15) | (r.readBits(4) << 4)
break
case 32:
ret = (ret & 15) | (r.readBits(8) << 4)
break
case 48:
ret = (ret & 15) | (r.readBits(28) << 4)
break
}
return ret
}
// readUBitVarFP reads a variable length uint32 encoded using fieldpath encoding
func (r *reader) readUBitVarFP() uint32 {
if r.readBoolean() {
return r.readBits(2)
}
if r.readBoolean() {
return r.readBits(4)
}
if r.readBoolean() {
return r.readBits(10)
}
if r.readBoolean() {
return r.readBits(17)
}
return r.readBits(31)
}
func (r *reader) readUBitVarFieldPath() int {
return int(r.readUBitVarFP())
}
// readStringN reads a string of a given length
func (r *reader) readStringN(n uint32) string {
return string(r.readBytes(n))
}
// readString reads a null terminated string
func (r *reader) readString() string {
buf := make([]byte, 0)
for {
b := r.readByte()
if b == 0 {
break
}
buf = append(buf, b)
}
return string(buf)
}
// readCoord reads a coord as a float32
func (r *reader) readCoord() float32 {
value := float32(0.0)
intval := r.readBits(1)
fractval := r.readBits(1)
signbit := false
if intval != 0 || fractval != 0 {
signbit = r.readBoolean()
if intval != 0 {
intval = r.readBits(14) + 1
}
if fractval != 0 {
fractval = r.readBits(5)
}
value = float32(intval) + float32(fractval)*(1.0/(1<<5))
// Fixup the sign if negative.
if signbit {
value = -value
}
}
return value
}
// readAngle reads a bit angle of the given size
func (r *reader) readAngle(n uint32) float32 {
return float32(r.readBits(n)) * 360.0 / float32(int(1<<n))
}
// readNormal reads a normalized float vector
func (r *reader) readNormal() float32 {
isNeg := r.readBoolean()
len := r.readBits(11)
ret := float32(len) * float32(1.0/(float32(1<<11)-1.0))
if isNeg {
return -ret
} else {
return ret
}
}
// read3BitNormal reads a normalized float vector
func (r *reader) read3BitNormal() []float32 {
ret := []float32{0.0, 0.0, 0.0}
hasX := r.readBoolean()
haxY := r.readBoolean()
if hasX {
ret[0] = r.readNormal()
}
if haxY {
ret[1] = r.readNormal()
}
negZ := r.readBoolean()
prodsum := ret[0]*ret[0] + ret[1]*ret[1]
if prodsum < 1.0 {
ret[2] = float32(math.Sqrt(float64(1.0 - prodsum)))
} else {
ret[2] = 0.0
}
if negZ {
ret[2] = -ret[2]
}
return ret
}
// readBitsAsBytes reads the given number of bits in groups of bytes
func (r *reader) readBitsAsBytes(n uint32) []byte {
tmp := make([]byte, 0)
for n >= 8 {
tmp = append(tmp, r.readByte())
n -= 8
}
if n > 0 {
tmp = append(tmp, byte(r.readBits(n)))
}
return tmp
}