bits.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2014-2022 Ulrich Kunitz. 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. package xz
  5. import (
  6. "errors"
  7. "io"
  8. )
  9. // putUint32LE puts the little-endian representation of x into the first
  10. // four bytes of p.
  11. func putUint32LE(p []byte, x uint32) {
  12. p[0] = byte(x)
  13. p[1] = byte(x >> 8)
  14. p[2] = byte(x >> 16)
  15. p[3] = byte(x >> 24)
  16. }
  17. // putUint64LE puts the little-endian representation of x into the first
  18. // eight bytes of p.
  19. func putUint64LE(p []byte, x uint64) {
  20. p[0] = byte(x)
  21. p[1] = byte(x >> 8)
  22. p[2] = byte(x >> 16)
  23. p[3] = byte(x >> 24)
  24. p[4] = byte(x >> 32)
  25. p[5] = byte(x >> 40)
  26. p[6] = byte(x >> 48)
  27. p[7] = byte(x >> 56)
  28. }
  29. // uint32LE converts a little endian representation to an uint32 value.
  30. func uint32LE(p []byte) uint32 {
  31. return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 |
  32. uint32(p[3])<<24
  33. }
  34. // putUvarint puts a uvarint representation of x into the byte slice.
  35. func putUvarint(p []byte, x uint64) int {
  36. i := 0
  37. for x >= 0x80 {
  38. p[i] = byte(x) | 0x80
  39. x >>= 7
  40. i++
  41. }
  42. p[i] = byte(x)
  43. return i + 1
  44. }
  45. // errOverflow indicates an overflow of the 64-bit unsigned integer.
  46. var errOverflowU64 = errors.New("xz: uvarint overflows 64-bit unsigned integer")
  47. // readUvarint reads a uvarint from the given byte reader.
  48. func readUvarint(r io.ByteReader) (x uint64, n int, err error) {
  49. const maxUvarintLen = 10
  50. var s uint
  51. i := 0
  52. for {
  53. b, err := r.ReadByte()
  54. if err != nil {
  55. return x, i, err
  56. }
  57. i++
  58. if i > maxUvarintLen {
  59. return x, i, errOverflowU64
  60. }
  61. if b < 0x80 {
  62. if i == maxUvarintLen && b > 1 {
  63. return x, i, errOverflowU64
  64. }
  65. return x | uint64(b)<<s, i, nil
  66. }
  67. x |= uint64(b&0x7f) << s
  68. s += 7
  69. }
  70. }