Pragya Arya | 324337e | 2020-02-20 14:35:08 +0530 | [diff] [blame] | 1 | // Copyright 2018 Klaus Post. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | // Based on work Copyright (c) 2013, Yann Collet, released under BSD License. |
| 5 | |
| 6 | package fse |
| 7 | |
| 8 | // byteReader provides a byte reader that reads |
| 9 | // little endian values from a byte stream. |
| 10 | // The input stream is manually advanced. |
| 11 | // The reader performs no bounds checks. |
| 12 | type byteReader struct { |
| 13 | b []byte |
| 14 | off int |
| 15 | } |
| 16 | |
| 17 | // init will initialize the reader and set the input. |
| 18 | func (b *byteReader) init(in []byte) { |
| 19 | b.b = in |
| 20 | b.off = 0 |
| 21 | } |
| 22 | |
| 23 | // advance the stream b n bytes. |
| 24 | func (b *byteReader) advance(n uint) { |
| 25 | b.off += int(n) |
| 26 | } |
| 27 | |
| 28 | // Int32 returns a little endian int32 starting at current offset. |
| 29 | func (b byteReader) Int32() int32 { |
| 30 | b2 := b.b[b.off : b.off+4 : b.off+4] |
| 31 | v3 := int32(b2[3]) |
| 32 | v2 := int32(b2[2]) |
| 33 | v1 := int32(b2[1]) |
| 34 | v0 := int32(b2[0]) |
| 35 | return v0 | (v1 << 8) | (v2 << 16) | (v3 << 24) |
| 36 | } |
| 37 | |
| 38 | // Uint32 returns a little endian uint32 starting at current offset. |
| 39 | func (b byteReader) Uint32() uint32 { |
| 40 | b2 := b.b[b.off : b.off+4 : b.off+4] |
| 41 | v3 := uint32(b2[3]) |
| 42 | v2 := uint32(b2[2]) |
| 43 | v1 := uint32(b2[1]) |
| 44 | v0 := uint32(b2[0]) |
| 45 | return v0 | (v1 << 8) | (v2 << 16) | (v3 << 24) |
| 46 | } |
| 47 | |
| 48 | // unread returns the unread portion of the input. |
| 49 | func (b byteReader) unread() []byte { |
| 50 | return b.b[b.off:] |
| 51 | } |
| 52 | |
| 53 | // remain will return the number of bytes remaining. |
| 54 | func (b byteReader) remain() int { |
| 55 | return len(b.b) - b.off |
| 56 | } |