blob: 2c4fca17fa1d7ec9328a09cf8b5553c661e586ad [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001// Copyright 2019+ Klaus Post. All rights reserved.
2// License information can be found in the LICENSE file.
3// Based on work by Yann Collet, released under BSD License.
4
5package zstd
6
7// byteReader provides a byte reader that reads
8// little endian values from a byte stream.
9// The input stream is manually advanced.
10// The reader performs no bounds checks.
11type byteReader struct {
12 b []byte
13 off int
14}
15
16// init will initialize the reader and set the input.
17func (b *byteReader) init(in []byte) {
18 b.b = in
19 b.off = 0
20}
21
22// advance the stream b n bytes.
23func (b *byteReader) advance(n uint) {
24 b.off += int(n)
25}
26
27// overread returns whether we have advanced too far.
28func (b *byteReader) overread() bool {
29 return b.off > len(b.b)
30}
31
32// Int32 returns a little endian int32 starting at current offset.
33func (b byteReader) Int32() int32 {
34 b2 := b.b[b.off:]
35 b2 = b2[:4]
36 v3 := int32(b2[3])
37 v2 := int32(b2[2])
38 v1 := int32(b2[1])
39 v0 := int32(b2[0])
40 return v0 | (v1 << 8) | (v2 << 16) | (v3 << 24)
41}
42
43// Uint8 returns the next byte
44func (b *byteReader) Uint8() uint8 {
45 v := b.b[b.off]
46 return v
47}
48
49// Uint32 returns a little endian uint32 starting at current offset.
50func (b byteReader) Uint32() uint32 {
51 if r := b.remain(); r < 4 {
52 // Very rare
53 v := uint32(0)
54 for i := 1; i <= r; i++ {
55 v = (v << 8) | uint32(b.b[len(b.b)-i])
56 }
57 return v
58 }
59 b2 := b.b[b.off:]
60 b2 = b2[:4]
61 v3 := uint32(b2[3])
62 v2 := uint32(b2[2])
63 v1 := uint32(b2[1])
64 v0 := uint32(b2[0])
65 return v0 | (v1 << 8) | (v2 << 16) | (v3 << 24)
66}
67
68// Uint32NC returns a little endian uint32 starting at current offset.
69// The caller must be sure if there are at least 4 bytes left.
70func (b byteReader) Uint32NC() uint32 {
71 b2 := b.b[b.off:]
72 b2 = b2[:4]
73 v3 := uint32(b2[3])
74 v2 := uint32(b2[2])
75 v1 := uint32(b2[1])
76 v0 := uint32(b2[0])
77 return v0 | (v1 << 8) | (v2 << 16) | (v3 << 24)
78}
79
80// unread returns the unread portion of the input.
81func (b byteReader) unread() []byte {
82 return b.b[b.off:]
83}
84
85// remain will return the number of bytes remaining.
86func (b byteReader) remain() int {
87 return len(b.b) - b.off
88}