Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 1 | // 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 | |
| 5 | package 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. |
| 11 | type byteReader struct { |
| 12 | b []byte |
| 13 | off int |
| 14 | } |
| 15 | |
| 16 | // init will initialize the reader and set the input. |
| 17 | func (b *byteReader) init(in []byte) { |
| 18 | b.b = in |
| 19 | b.off = 0 |
| 20 | } |
| 21 | |
| 22 | // advance the stream b n bytes. |
| 23 | func (b *byteReader) advance(n uint) { |
| 24 | b.off += int(n) |
| 25 | } |
| 26 | |
| 27 | // overread returns whether we have advanced too far. |
| 28 | func (b *byteReader) overread() bool { |
| 29 | return b.off > len(b.b) |
| 30 | } |
| 31 | |
| 32 | // Int32 returns a little endian int32 starting at current offset. |
| 33 | func (b byteReader) Int32() int32 { |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 34 | b2 := b.b[b.off:] |
| 35 | b2 = b2[:4] |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 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 |
| 44 | func (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. |
| 50 | func (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 | } |
David K. Bainbridge | bd6b288 | 2021-08-26 13:31:02 +0000 | [diff] [blame] | 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. |
| 70 | func (b byteReader) Uint32NC() uint32 { |
| 71 | b2 := b.b[b.off:] |
| 72 | b2 = b2[:4] |
Scott Baker | ed4efab | 2020-01-13 19:12:25 -0800 | [diff] [blame] | 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. |
| 81 | func (b byteReader) unread() []byte { |
| 82 | return b.b[b.off:] |
| 83 | } |
| 84 | |
| 85 | // remain will return the number of bytes remaining. |
| 86 | func (b byteReader) remain() int { |
| 87 | return len(b.b) - b.off |
| 88 | } |