bytereader.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // advance the stream b n bytes.
  20. func (b *byteReader) advance(n uint) {
  21. b.off += int(n)
  22. }
  23. // Int32 returns a little endian int32 starting at current offset.
  24. func (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. // Uint32 returns a little endian uint32 starting at current offset.
  32. func (b byteReader) Uint32() uint32 {
  33. v3 := uint32(b.b[b.off+3])
  34. v2 := uint32(b.b[b.off+2])
  35. v1 := uint32(b.b[b.off+1])
  36. v0 := uint32(b.b[b.off])
  37. return (v3 << 24) | (v2 << 16) | (v1 << 8) | v0
  38. }
  39. // unread returns the unread portion of the input.
  40. func (b byteReader) unread() []byte {
  41. return b.b[b.off:]
  42. }
  43. // remain will return the number of bytes remaining.
  44. func (b byteReader) remain() int {
  45. return len(b.b) - b.off
  46. }