-
Notifications
You must be signed in to change notification settings - Fork 13
/
stream.go
431 lines (378 loc) · 12.4 KB
/
stream.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
package kaitai
import (
"bufio"
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
)
// APIVersion defines the currently used API version.
const APIVersion = 0x0001
// ErrInvalidSizeRequested is returned when KaitaiStream methods are called
// with a size argument which does not make sense:
//
// - ReadBytes with negative number of bytes
// - ReadBitsIntBe/Le with more than 8 bytes
var ErrInvalidSizeRequested = errors.New("invalid size requested")
// A Stream represents a sequence of bytes. It encapsulates reading from files
// and memory, stores pointer to its current position, and allows
// reading/writing of various primitives.
type Stream struct {
io.ReadSeeker
buf [8]byte
// Number of bits remaining in "bits" for sequential calls to ReadBitsInt
bitsLeft int
bits uint64
}
// NewStream creates and initializes a new Buffer based on r.
func NewStream(r io.ReadSeeker) *Stream {
return &Stream{ReadSeeker: r}
}
// EOF returns true when the end of the Stream is reached.
func (k *Stream) EOF() (bool, error) {
if k.bitsLeft > 0 {
return false, nil
}
curPos, err := k.Pos()
if err != nil {
return false, err
}
isEOF := false
_, err = k.ReadU1()
if errors.Is(err, io.EOF) {
isEOF = true
err = nil
}
if err != nil {
return false, err
}
_, err = k.Seek(curPos, io.SeekStart)
if err != nil {
return false, fmt.Errorf("EOF: error seeking back to current position: %w", err)
}
return isEOF, nil
}
// Size returns the number of bytes of the stream.
func (k *Stream) Size() (int64, error) {
// Go has no internal ReadSeeker function to get current ReadSeeker size,
// thus we use the following trick.
// Remember our current position
curPos, err := k.Pos()
if err != nil {
return 0, err
}
// Seek to the end of the File object
_, err = k.Seek(0, io.SeekEnd)
if err != nil {
return 0, fmt.Errorf("Size: error seeking to end of the stream: %w", err)
}
// Remember position, which is equal to the full length
fullSize, err := k.Pos()
if err != nil {
return fullSize, err
}
// Seek back to the current position
_, err = k.Seek(curPos, io.SeekStart)
if err != nil {
return fullSize, fmt.Errorf("Size: error seeking back to current position: %w", err)
}
return fullSize, nil
}
// Pos returns the current position of the stream.
func (k *Stream) Pos() (int64, error) {
pos, err := k.Seek(0, io.SeekCurrent)
if err != nil {
return pos, fmt.Errorf("Pos: error getting current position: %w", err)
}
return pos, nil
}
// ReadU1 reads 1 byte and returns this as uint8.
func (k *Stream) ReadU1() (v uint8, err error) {
if _, err = io.ReadFull(k, k.buf[:1]); err != nil {
return 0, fmt.Errorf("ReadU1: error reading 1 byte: %w", err)
}
return k.buf[0], nil
}
// ReadU2be reads 2 bytes in big-endian order and returns those as uint16.
func (k *Stream) ReadU2be() (v uint16, err error) {
if _, err = io.ReadFull(k, k.buf[:2]); err != nil {
return 0, fmt.Errorf("ReadU2be: error reading 2 bytes: %w", err)
}
return binary.BigEndian.Uint16(k.buf[:2]), nil
}
// ReadU4be reads 4 bytes in big-endian order and returns those as uint32.
func (k *Stream) ReadU4be() (v uint32, err error) {
if _, err = io.ReadFull(k, k.buf[:4]); err != nil {
return 0, fmt.Errorf("ReadU4be: error reading 4 bytes: %w", err)
}
return binary.BigEndian.Uint32(k.buf[:4]), nil
}
// ReadU8be reads 8 bytes in big-endian order and returns those as uint64.
func (k *Stream) ReadU8be() (v uint64, err error) {
if _, err = io.ReadFull(k, k.buf[:8]); err != nil {
return 0, fmt.Errorf("ReadU8be: error reading 8 bytes: %w", err)
}
return binary.BigEndian.Uint64(k.buf[:8]), nil
}
// ReadU2le reads 2 bytes in little-endian order and returns those as uint16.
func (k *Stream) ReadU2le() (v uint16, err error) {
if _, err = io.ReadFull(k, k.buf[:2]); err != nil {
return 0, fmt.Errorf("ReadU2le: error reading 2 bytes: %w", err)
}
return binary.LittleEndian.Uint16(k.buf[:2]), nil
}
// ReadU4le reads 4 bytes in little-endian order and returns those as uint32.
func (k *Stream) ReadU4le() (v uint32, err error) {
if _, err = io.ReadFull(k, k.buf[:4]); err != nil {
return 0, fmt.Errorf("ReadU4le: error reading 4 bytes: %w", err)
}
return binary.LittleEndian.Uint32(k.buf[:4]), nil
}
// ReadU8le reads 8 bytes in little-endian order and returns those as uint64.
func (k *Stream) ReadU8le() (v uint64, err error) {
if _, err = io.ReadFull(k, k.buf[:8]); err != nil {
return 0, fmt.Errorf("ReadU8le: error reading 8 bytes: %w", err)
}
return binary.LittleEndian.Uint64(k.buf[:8]), nil
}
// ReadS1 reads 1 byte and returns this as int8.
func (k *Stream) ReadS1() (v int8, err error) {
vv, err := k.ReadU1()
return int8(vv), err
}
// ReadS2be reads 2 bytes in big-endian order and returns those as int16.
func (k *Stream) ReadS2be() (v int16, err error) {
vv, err := k.ReadU2be()
return int16(vv), err
}
// ReadS4be reads 4 bytes in big-endian order and returns those as int32.
func (k *Stream) ReadS4be() (v int32, err error) {
vv, err := k.ReadU4be()
return int32(vv), err
}
// ReadS8be reads 8 bytes in big-endian order and returns those as int64.
func (k *Stream) ReadS8be() (v int64, err error) {
vv, err := k.ReadU8be()
return int64(vv), err
}
// ReadS2le reads 2 bytes in little-endian order and returns those as int16.
func (k *Stream) ReadS2le() (v int16, err error) {
vv, err := k.ReadU2le()
return int16(vv), err
}
// ReadS4le reads 4 bytes in little-endian order and returns those as int32.
func (k *Stream) ReadS4le() (v int32, err error) {
vv, err := k.ReadU4le()
return int32(vv), err
}
// ReadS8le reads 8 bytes in little-endian order and returns those as int64.
func (k *Stream) ReadS8le() (v int64, err error) {
vv, err := k.ReadU8le()
return int64(vv), err
}
// ReadF4be reads 4 bytes in big-endian order and returns those as float32.
func (k *Stream) ReadF4be() (v float32, err error) {
vv, err := k.ReadU4be()
return math.Float32frombits(vv), err
}
// ReadF8be reads 8 bytes in big-endian order and returns those as float64.
func (k *Stream) ReadF8be() (v float64, err error) {
vv, err := k.ReadU8be()
return math.Float64frombits(vv), err
}
// ReadF4le reads 4 bytes in little-endian order and returns those as float32.
func (k *Stream) ReadF4le() (v float32, err error) {
vv, err := k.ReadU4le()
return math.Float32frombits(vv), err
}
// ReadF8le reads 8 bytes in little-endian order and returns those as float64.
func (k *Stream) ReadF8le() (v float64, err error) {
vv, err := k.ReadU8le()
return math.Float64frombits(vv), err
}
// ReadBytes reads n bytes and returns those as a byte array.
func (k *Stream) ReadBytes(n int) (b []byte, err error) {
if n < 0 {
return nil, fmt.Errorf("ReadBytes(%d): %w", n, ErrInvalidSizeRequested)
}
b = make([]byte, n)
_, err = io.ReadFull(k, b)
if err != nil {
return nil, fmt.Errorf("ReadBytes: error reading %d bytes: %w", n, err)
}
return b, nil
}
// ReadBytesFull reads all remaining bytes and returns those as a byte array.
func (k *Stream) ReadBytesFull() ([]byte, error) {
res, err := ioutil.ReadAll(k)
if err != nil {
return nil, fmt.Errorf("ReadBytesFull: error reading all bytes: %w", err)
}
return res, nil
}
// ReadBytesPadTerm reads up to size bytes. pad bytes are discarded. It
// terminates reading, when the term byte occurs. The term byte is included
// in the returned byte array when includeTerm is set.
func (k *Stream) ReadBytesPadTerm(size int, term, pad byte, includeTerm bool) ([]byte, error) {
bs, err := k.ReadBytes(size)
if err != nil {
return nil, err
}
bs = bytes.TrimRight(bs, string(pad))
i := bytes.IndexByte(bs, term)
if i != -1 {
if includeTerm {
bs = bs[:i+1]
} else {
bs = bs[:i]
}
}
return bs, nil
}
// ReadBytesTerm reads bytes until the term byte is reached. If includeTerm is
// true, the term byte is included in the returned byte slice. If consumeTerm is
// true, the stream continues after the term byte. If eosError is true, EOF
// errors result in an error.
func (k *Stream) ReadBytesTerm(term byte, includeTerm, consumeTerm, eosError bool) ([]byte, error) {
r := bufio.NewReader(k)
pos, err := k.Pos()
if err != nil {
return []byte{}, err
}
slice, err := r.ReadBytes(term)
if err != nil {
// If eosError if false, ignore io.EOF and bail out on any other error
// If eosError is true, bail out on any error, including io.EOF
if eosError || !errors.Is(err, io.EOF) {
return slice, fmt.Errorf("ReadBytesTerm: error reading bytes until term byte: %w", err)
}
}
_, err = k.Seek(pos+int64(len(slice)), io.SeekStart)
if err != nil {
return []byte{}, fmt.Errorf("ReadBytesTerm: error seeking past term byte: %w", err)
}
if !includeTerm {
slice = slice[:len(slice)-1]
}
if !consumeTerm {
_, err = k.Seek(-1, io.SeekCurrent)
if err != nil {
return slice, fmt.Errorf("ReadBytesTerm: error seeking to term byte: %w", err)
}
}
return slice, nil
}
// ReadBytesTermMulti reads chunks of len(term) bytes until it reaches a chunk
// equal to term. If includeTerm is true, term will be included in the returned
// byte slice. If consumeTerm is true, stream position will be left after the
// term, otherwise a seek will be performed to get the stream position before
// the term. If eosError is true, reaching EOF before the term is found is
// treated as an error, otherwise no error and all bytes until EOF are returned
// in this case.
func (k *Stream) ReadBytesTermMulti(term []byte, includeTerm, consumeTerm, eosError bool) ([]byte, error) {
unitSize := len(term)
r := []byte{}
c := make([]byte, unitSize)
for {
n, err := io.ReadFull(k, c)
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
if eosError {
return nil, fmt.Errorf("ReadBytesTermMulti: end of stream reached, but no terminator found")
}
r = append(r, c[:n]...)
return r, nil
}
return nil, fmt.Errorf("ReadBytesTermMulti: %w", err)
}
if bytes.Equal(c, term) {
if includeTerm {
r = append(r, c...)
}
if !consumeTerm {
_, err := k.Seek(-int64(unitSize), io.SeekCurrent)
if err != nil {
return nil, fmt.Errorf("ReadBytesTermMulti: error seeking back to terminator: %w", err)
}
}
return r, nil
}
r = append(r, c...)
}
}
// AlignToByte discards the remaining bits and starts reading bits at the
// next byte.
func (k *Stream) AlignToByte() {
k.bitsLeft = 0
k.bits = 0
}
// ReadBitsIntBe reads n-bit integer in big-endian byte order and returns it as uint64.
func (k *Stream) ReadBitsIntBe(n int) (res uint64, err error) {
res = 0
bitsNeeded := n - k.bitsLeft
k.bitsLeft = -bitsNeeded & 7 // `-bitsNeeded mod 8`
if bitsNeeded > 0 {
// 1 bit => 1 byte
// 8 bits => 1 byte
// 9 bits => 2 bytes
bytesNeeded := ((bitsNeeded - 1) / 8) + 1 // `ceil(bitsNeeded / 8)`
if bytesNeeded > 8 {
return res, fmt.Errorf("ReadBitsIntBe(%d): more than 8 bytes requested: %w", n, ErrInvalidSizeRequested)
}
_, err = io.ReadFull(k, k.buf[:bytesNeeded])
if err != nil {
return res, fmt.Errorf("ReadBitsIntBe(%d): %w", n, err)
}
for i := 0; i < bytesNeeded; i++ {
res = res<<8 | uint64(k.buf[i])
}
newBits := res
res = res>>k.bitsLeft | k.bits<<bitsNeeded
k.bits = newBits // will be masked at the end of the function
} else {
res = k.bits >> -bitsNeeded // shift unneeded bits out
}
var mask uint64 = (1 << k.bitsLeft) - 1 // `bitsLeft` is in range 0..7
k.bits &= mask
return res, nil
}
// ReadBitsInt reads n-bit integer in big-endian byte order and returns it as uint64.
//
// Deprecated: Use ReadBitsIntBe instead.
func (k *Stream) ReadBitsInt(n uint8) (res uint64, err error) {
return k.ReadBitsIntBe(int(n))
}
// ReadBitsIntLe reads n-bit integer in little-endian byte order and returns it as uint64.
func (k *Stream) ReadBitsIntLe(n int) (res uint64, err error) {
res = 0
bitsNeeded := n - k.bitsLeft
if bitsNeeded > 0 {
// 1 bit => 1 byte
// 8 bits => 1 byte
// 9 bits => 2 bytes
bytesNeeded := ((bitsNeeded - 1) / 8) + 1 // `ceil(bitsNeeded / 8)`
if bytesNeeded > 8 {
return res, fmt.Errorf("ReadBitsIntLe(%d): more than 8 bytes requested: %w", n, ErrInvalidSizeRequested)
}
_, err = io.ReadFull(k, k.buf[:bytesNeeded])
if err != nil {
return res, fmt.Errorf("ReadBitsIntLe(%d): %w", n, err)
}
for i := 0; i < bytesNeeded; i++ {
res |= uint64(k.buf[i]) << (i * 8)
}
newBits := res >> bitsNeeded
res = res<<k.bitsLeft | k.bits
k.bits = newBits
} else {
res = k.bits
k.bits >>= n
}
k.bitsLeft = -bitsNeeded & 7 // `-bitsNeeded mod 8`
var mask uint64 = (1 << n) - 1 // unlike some other languages, no problem with this in Go
res &= mask
return res, nil
}