-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlittleendianreader.go
461 lines (356 loc) · 12.7 KB
/
littleendianreader.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package byteio
// File automatically generated with ./gen.sh.
import (
"io"
"math"
)
// LittleEndianReader wraps a io.Reader to provide methods
// to make it easier to Read fundamental types.
type LittleEndianReader struct {
io.Reader
buffer [9]byte
}
// ReadBool Reads a boolean.
func (e *LittleEndianReader) ReadBool() (bool, int, error) {
b, n, err := e.ReadUint8()
return b != 0, n, err
}
// ReadInt8 Reads a 8 bit int as a int8 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt8() (int8, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:1])
if err != nil {
return 0, n, err
}
return int8(e.buffer[0]), 1, nil
}
// ReadInt16 Reads a 16 bit int as a int16 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt16() (int16, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:2])
if err != nil {
return 0, n, err
}
return int16(uint16(e.buffer[0]) | uint16(e.buffer[1])<<8), 2, nil
}
// ReadInt24 Reads a 24 bit int as a int32 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt24() (int32, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:3])
if err != nil {
return 0, n, err
}
return int32(uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16), 3, nil
}
// ReadInt32 Reads a 32 bit int as a int32 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt32() (int32, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:4])
if err != nil {
return 0, n, err
}
return int32(uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16 | uint32(e.buffer[3])<<24), 4, nil
}
// ReadInt40 Reads a 40 bit int as a int64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt40() (int64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:5])
if err != nil {
return 0, n, err
}
return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32), 5, nil
}
// ReadInt48 Reads a 48 bit int as a int64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt48() (int64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:6])
if err != nil {
return 0, n, err
}
return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40), 6, nil
}
// ReadInt56 Reads a 56 bit int as a int64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt56() (int64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:7])
if err != nil {
return 0, n, err
}
return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48), 7, nil
}
// ReadInt64 Reads a 64 bit int as a int64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadInt64() (int64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:8])
if err != nil {
return 0, n, err
}
return int64(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48 | uint64(e.buffer[7])<<56), 8, nil
}
// ReadUint8 Reads a 8 bit uint as a uint8 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint8() (uint8, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:1])
if err != nil {
return 0, n, err
}
return e.buffer[0], 1, nil
}
// ReadUint16 Reads a 16 bit uint as a uint16 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint16() (uint16, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:2])
if err != nil {
return 0, n, err
}
return uint16(e.buffer[0]) | uint16(e.buffer[1])<<8, 2, nil
}
// ReadUint24 Reads a 24 bit uint as a uint32 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint24() (uint32, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:3])
if err != nil {
return 0, n, err
}
return uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16, 3, nil
}
// ReadUint32 Reads a 32 bit uint as a uint32 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint32() (uint32, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:4])
if err != nil {
return 0, n, err
}
return uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16 | uint32(e.buffer[3])<<24, 4, nil
}
// ReadUint40 Reads a 40 bit uint as a uint64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint40() (uint64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:5])
if err != nil {
return 0, n, err
}
return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32, 5, nil
}
// ReadUint48 Reads a 48 bit uint as a uint64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint48() (uint64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:6])
if err != nil {
return 0, n, err
}
return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40, 6, nil
}
// ReadUint56 Reads a 56 bit uint as a uint64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint56() (uint64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:7])
if err != nil {
return 0, n, err
}
return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48, 7, nil
}
// ReadUint64 Reads a 64 bit uint as a uint64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadUint64() (uint64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:8])
if err != nil {
return 0, n, err
}
return uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48 | uint64(e.buffer[7])<<56, 8, nil
}
// ReadFloat32 Reads a 32 bit float as a float32 using the underlying io.Reader.
func (e *LittleEndianReader) ReadFloat32() (float32, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:4])
if err != nil {
return 0, n, err
}
return math.Float32frombits(uint32(e.buffer[0]) | uint32(e.buffer[1])<<8 | uint32(e.buffer[2])<<16 | uint32(e.buffer[3])<<24), 4, nil
}
// ReadFloat64 Reads a 64 bit float as a float64 using the underlying io.Reader.
func (e *LittleEndianReader) ReadFloat64() (float64, int, error) {
n, err := io.ReadFull(e.Reader, e.buffer[:8])
if err != nil {
return 0, n, err
}
return math.Float64frombits(uint64(e.buffer[0]) | uint64(e.buffer[1])<<8 | uint64(e.buffer[2])<<16 | uint64(e.buffer[3])<<24 | uint64(e.buffer[4])<<32 | uint64(e.buffer[5])<<40 | uint64(e.buffer[6])<<48 | uint64(e.buffer[7])<<56), 8, nil
}
// ReadBytes Reads a []byte.
func (e *LittleEndianReader) ReadBytes(size int) ([]byte, int, error) {
buf := make([]byte, size)
n, err := io.ReadFull(e, buf)
return buf[:n], n, err
}
// ReadBytesX Reads the length of the Bytes, using ReadUintX and then Reads the bytes.
func (e *LittleEndianReader) ReadBytesX() ([]byte, int, error) {
size, n, err := e.ReadUintX()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes8 Reads the length of the Bytes, using ReadUint8 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes8() ([]byte, int, error) {
size, n, err := e.ReadUint8()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes16 Reads the length of the Bytes, using ReadUint16 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes16() ([]byte, int, error) {
size, n, err := e.ReadUint16()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes24 Reads the length of the Bytes, using ReadUint24 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes24() ([]byte, int, error) {
size, n, err := e.ReadUint24()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes32 Reads the length of the Bytes, using ReadUint32 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes32() ([]byte, int, error) {
size, n, err := e.ReadUint32()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes40 Reads the length of the Bytes, using ReadUint40 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes40() ([]byte, int, error) {
size, n, err := e.ReadUint40()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes48 Reads the length of the Bytes, using ReadUint48 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes48() ([]byte, int, error) {
size, n, err := e.ReadUint48()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes56 Reads the length of the Bytes, using ReadUint56 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes56() ([]byte, int, error) {
size, n, err := e.ReadUint56()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadBytes64 Reads the length of the Bytes, using ReadUint64 and then Reads the bytes.
func (e *LittleEndianReader) ReadBytes64() ([]byte, int, error) {
size, n, err := e.ReadUint64()
if err != nil {
return nil, n, err
}
p, m, err := e.ReadBytes(int(size))
return p, n + m, err
}
// ReadString Reads a string.
func (e *LittleEndianReader) ReadString(size int) (string, int, error) {
buf := make([]byte, size)
n, err := io.ReadFull(e, buf)
return string(buf[:n]), n, err
}
// ReadStringX Reads the length of the String, using ReadUintX and then Reads the bytes.
func (e *LittleEndianReader) ReadStringX() (string, int, error) {
size, n, err := e.ReadUintX()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString8 Reads the length of the String, using ReadUint8 and then Reads the bytes.
func (e *LittleEndianReader) ReadString8() (string, int, error) {
size, n, err := e.ReadUint8()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString16 Reads the length of the String, using ReadUint16 and then Reads the bytes.
func (e *LittleEndianReader) ReadString16() (string, int, error) {
size, n, err := e.ReadUint16()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString24 Reads the length of the String, using ReadUint24 and then Reads the bytes.
func (e *LittleEndianReader) ReadString24() (string, int, error) {
size, n, err := e.ReadUint24()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString32 Reads the length of the String, using ReadUint32 and then Reads the bytes.
func (e *LittleEndianReader) ReadString32() (string, int, error) {
size, n, err := e.ReadUint32()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString40 Reads the length of the String, using ReadUint40 and then Reads the bytes.
func (e *LittleEndianReader) ReadString40() (string, int, error) {
size, n, err := e.ReadUint40()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString48 Reads the length of the String, using ReadUint48 and then Reads the bytes.
func (e *LittleEndianReader) ReadString48() (string, int, error) {
size, n, err := e.ReadUint48()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString56 Reads the length of the String, using ReadUint56 and then Reads the bytes.
func (e *LittleEndianReader) ReadString56() (string, int, error) {
size, n, err := e.ReadUint56()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString64 Reads the length of the String, using ReadUint64 and then Reads the bytes.
func (e *LittleEndianReader) ReadString64() (string, int, error) {
size, n, err := e.ReadUint64()
if err != nil {
return "", n, err
}
p, m, err := e.ReadString(int(size))
return p, n + m, err
}
// ReadString0 Reads the bytes of the string until a 0 byte is read.
func (e *LittleEndianReader) ReadString0() (string, int, error) {
var (
n int
err error
d []byte
)
for {
var (
m int
p byte
)
p, m, err = e.ReadUint8()
n += m
if err != nil || p == 0 {
break
}
d = append(d, p)
}
return string(d), n, err
}