blob: 4dcab8d232775c0f46e348837e70df7f7bcc847f [file] [log] [blame]
khenaidoo7d3c5582021-08-11 18:09:44 -04001// 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
6package huff0
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.
12type byteReader struct {
13 b []byte
14 off int
15}
16
17// init will initialize the reader and set the input.
18func (b *byteReader) init(in []byte) {
19 b.b = in
20 b.off = 0
21}
22
khenaidoo7d3c5582021-08-11 18:09:44 -040023// Int32 returns a little endian int32 starting at current offset.
24func (b byteReader) Int32() int32 {
25 v3 := int32(b.b[b.off+3])
26 v2 := int32(b.b[b.off+2])
27 v1 := int32(b.b[b.off+1])
28 v0 := int32(b.b[b.off])
29 return (v3 << 24) | (v2 << 16) | (v1 << 8) | v0
30}
31
32// Uint32 returns a little endian uint32 starting at current offset.
33func (b byteReader) Uint32() uint32 {
34 v3 := uint32(b.b[b.off+3])
35 v2 := uint32(b.b[b.off+2])
36 v1 := uint32(b.b[b.off+1])
37 v0 := uint32(b.b[b.off])
38 return (v3 << 24) | (v2 << 16) | (v1 << 8) | v0
39}
40
khenaidoo7d3c5582021-08-11 18:09:44 -040041// remain will return the number of bytes remaining.
42func (b byteReader) remain() int {
43 return len(b.b) - b.off
44}