bytereader.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. package huff0
  6. // byteReader provides a byte reader that reads
  7. // little endian values from a byte stream.
  8. // The input stream is manually advanced.
  9. // The reader performs no bounds checks.
  10. type byteReader struct {
  11. b []byte
  12. off int
  13. }
  14. // init will initialize the reader and set the input.
  15. func (b *byteReader) init(in []byte) {
  16. b.b = in
  17. b.off = 0
  18. }
  19. // Int32 returns a little endian int32 starting at current offset.
  20. func (b byteReader) Int32() int32 {
  21. v3 := int32(b.b[b.off+3])
  22. v2 := int32(b.b[b.off+2])
  23. v1 := int32(b.b[b.off+1])
  24. v0 := int32(b.b[b.off])
  25. return (v3 << 24) | (v2 << 16) | (v1 << 8) | v0
  26. }
  27. // Uint32 returns a little endian uint32 starting at current offset.
  28. func (b byteReader) Uint32() uint32 {
  29. v3 := uint32(b.b[b.off+3])
  30. v2 := uint32(b.b[b.off+2])
  31. v1 := uint32(b.b[b.off+1])
  32. v0 := uint32(b.b[b.off])
  33. return (v3 << 24) | (v2 << 16) | (v1 << 8) | v0
  34. }
  35. // remain will return the number of bytes remaining.
  36. func (b byteReader) remain() int {
  37. return len(b.b) - b.off
  38. }