forked from tuneinsight/lattigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
316 lines (236 loc) · 7.88 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
312
313
314
315
316
package buffer
import (
"encoding/binary"
"fmt"
"unsafe"
)
// ReadAsUint64 reads an uint64 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint64[T any](r Reader, c *T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint64(r, (*uint64)(unsafe.Pointer(c)))
}
// ReadAsUint32 reads an uint32 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint32[T any](r Reader, c *T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint32(r, (*uint32)(unsafe.Pointer(c)))
}
// ReadAsUint16 reads an uint16 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint16[T any](r Reader, c *T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint16(r, (*uint16)(unsafe.Pointer(c)))
}
// ReadAsUint8 reads an uint8 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint8[T any](r Reader, c *T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint8(r, (*uint8)(unsafe.Pointer(c)))
}
// ReadAsUint64Slice reads a slice of uint64 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint64Slice[T any](r Reader, c []T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint64Slice(r, *(*[]uint64)(unsafe.Pointer(&c)))
}
// ReadAsUint32Slice reads a slice of uint32 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint32Slice[T any](r Reader, c []T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint32Slice(r, *(*[]uint32)(unsafe.Pointer(&c)))
}
// ReadAsUint16Slice reads a slice of uint16 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint16Slice[T any](r Reader, c []T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint16Slice(r, *(*[]uint16)(unsafe.Pointer(&c)))
}
// ReadAsUint8Slice reads a slice of uint8 from r and stores the result into c with pointer type casting into type T.
func ReadAsUint8Slice[T any](r Reader, c []T) (n int64, err error) {
/* #nosec G103 -- behavior and consequences well understood, pointer type cast */
return ReadUint8Slice(r, *(*[]uint8)(unsafe.Pointer(&c)))
}
// Read reads a slice of bytes from r and copies it on c.
func Read(r Reader, c []byte) (n int64, err error) {
slice, err := r.Peek(len(c))
if err != nil {
return int64(len(slice)), err
}
copy(c, slice)
nint, err := r.Discard(len(c))
return int64(nint), err
}
// ReadUint8 reads a byte from r and stores the result into *c.
func ReadUint8(r Reader, c *uint8) (n int64, err error) {
if c == nil {
return 0, fmt.Errorf("cannot ReadUint8: c is nil")
}
slice, err := r.Peek(1)
if err != nil {
return int64(len(slice)), err
}
// Reads one byte
*c = uint8(slice[0])
nint, err := r.Discard(1)
return int64(nint), err
}
// ReadUint8Slice reads a slice of byte from r and stores the result into c.
func ReadUint8Slice(r Reader, c []uint8) (n int64, err error) {
nint, err := r.Read(c)
return int64(nint), err
}
// ReadUint16 reads a uint16 from r and stores the result into *c.
func ReadUint16(r Reader, c *uint16) (n int64, err error) {
if c == nil {
return 0, fmt.Errorf("cannot ReadUint16: c is nil")
}
slice, err := r.Peek(2)
if err != nil {
return int64(len(slice)), err
}
// Reads one byte
*c = binary.LittleEndian.Uint16(slice)
nint, err := r.Discard(2)
return int64(nint), err
}
// ReadUint16Slice reads a slice of uint16 from r and stores the result into c.
func ReadUint16Slice(r Reader, c []uint16) (n int64, err error) {
// c is empty, return
if len(c) == 0 {
return
}
var slice []byte
size := r.Size()
if len(c)<<1 < size {
size = len(c) << 1
}
// Then returns the written bytes
if slice, err = r.Peek(size); err != nil {
return int64(len(slice)), err
}
buffered := len(slice) >> 1
// If the slice to write on is equal or smaller than the amount peaked
if N := len(c); N <= buffered {
for i, j := 0, 0; i < N; i, j = i+1, j+2 {
c[i] = binary.LittleEndian.Uint16(slice[j:])
}
nint, err := r.Discard(N << 1) // Discards what was read
return int64(nint), err
}
// Decodes the maximum
for i, j := 0, 0; i < buffered; i, j = i+1, j+2 {
c[i] = binary.LittleEndian.Uint16(slice[j:])
}
// Discard what was peeked
var inc int
if inc, err = r.Discard(len(slice)); err != nil {
return n + int64(inc), err
}
n += int64(inc)
// Recurses on the remaining slice to fill
var inc64 int64
inc64, err = ReadUint16Slice(r, c[buffered:])
return n + inc64, err
}
// ReadUint32 reads a uint32 from r and stores the result into *c.
func ReadUint32(r Reader, c *uint32) (n int64, err error) {
if c == nil {
return 0, fmt.Errorf("cannot ReadUint32: c is nil")
}
slice, err := r.Peek(4)
if err != nil {
return int64(len(slice)), err
}
// Reads one byte
*c = binary.LittleEndian.Uint32(slice)
nint, err := r.Discard(4)
return int64(nint), err
}
// ReadUint32Slice reads a slice of uint32 from r and stores the result into c.
func ReadUint32Slice(r Reader, c []uint32) (n int64, err error) {
// c is empty, return
if len(c) == 0 {
return
}
var slice []byte
// Avoid EOF
size := r.Size()
if len(c)<<2 < size {
size = len(c) << 2
}
// Then returns the written bytes
if slice, err = r.Peek(size); err != nil {
return int64(len(slice)), err
}
buffered := len(slice) >> 2
// If the slice to write on is equal or smaller than the amount peaked
if N := len(c); N <= buffered {
for i, j := 0, 0; i < N; i, j = i+1, j+4 {
c[i] = binary.LittleEndian.Uint32(slice[j:])
}
nint, err := r.Discard(N << 2) // Discards what was read
return int64(nint), err
}
// Decodes the maximum
for i, j := 0, 0; i < buffered; i, j = i+1, j+4 {
c[i] = binary.LittleEndian.Uint32(slice[j:])
}
// Discard what was peeked
var inc int
if inc, err = r.Discard(len(slice)); err != nil {
return n + int64(inc), err
}
n += int64(inc)
// Recurses on the remaining slice to fill
var inc64 int64
inc64, err = ReadUint32Slice(r, c[buffered:])
return n + inc64, err
}
// ReadUint64 reads a uint64 from r and stores the result into c.
func ReadUint64(r Reader, c *uint64) (n int64, err error) {
if c == nil {
return 0, fmt.Errorf("cannot ReadUint64: c is nil")
}
bytes, err := r.Peek(8)
if err != nil {
return int64(len(bytes)), err
}
// Reads one byte
*c = binary.LittleEndian.Uint64(bytes)
nint, err := r.Discard(8)
return int64(nint), err
}
// ReadUint64Slice reads a slice of uint64 from r and stores the result into c.
func ReadUint64Slice(r Reader, c []uint64) (n int64, err error) {
// c is empty, return
if len(c) == 0 {
return
}
var slice []byte
// Avoid EOF
size := r.Size()
if len(c)<<3 < size {
size = len(c) << 3
}
// Then returns the written bytes
if slice, err = r.Peek(size); err != nil {
return int64(len(slice)), err
}
buffered := len(slice) >> 3
// If the slice to write on is equal or smaller than the amount peaked
if N := len(c); N <= buffered {
for i, j := 0, 0; i < N; i, j = i+1, j+8 {
c[i] = binary.LittleEndian.Uint64(slice[j:])
}
nint, err := r.Discard(N << 3) // Discards what was read
return int64(nint), err
}
// Decodes the maximum
for i, j := 0, 0; i < buffered; i, j = i+1, j+8 {
c[i] = binary.LittleEndian.Uint64(slice[j:])
}
// Discard what was peeked
var inc int
if inc, err = r.Discard(len(slice)); err != nil {
return n + int64(inc), err
}
n += int64(inc)
// Recurses on the remaining slice to fill
var inc64 int64
inc64, err = ReadUint64Slice(r, c[buffered:])
return n + inc64, err
}