compress.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2017 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package bitutil
  17. import "errors"
  18. var (
  19. // ErrMissingData is returned from decompression if the byte referenced by
  20. // the bitset header overflows the input data.
  21. ErrMissingData = errors.New("missing bytes on input")
  22. // ErrUnreferencedData is returned from decompression if not all bytes were used
  23. // up from the input data after decompressing it.
  24. ErrUnreferencedData = errors.New("extra bytes on input")
  25. // ErrExceededTarget is returned from decompression if the bitset header has
  26. // more bits defined than the number of target buffer space available.
  27. ErrExceededTarget = errors.New("target data size exceeded")
  28. // ErrZeroContent is returned from decompression if a data byte referenced in
  29. // the bitset header is actually a zero byte.
  30. ErrZeroContent = errors.New("zero byte in input content")
  31. )
  32. // The compression algorithm implemented by CompressBytes and DecompressBytes is
  33. // optimized for sparse input data which contains a lot of zero bytes. Decompression
  34. // requires knowledge of the decompressed data length.
  35. //
  36. // Compression works as follows:
  37. //
  38. // if data only contains zeroes,
  39. // CompressBytes(data) == nil
  40. // otherwise if len(data) <= 1,
  41. // CompressBytes(data) == data
  42. // otherwise:
  43. // CompressBytes(data) == append(CompressBytes(nonZeroBitset(data)), nonZeroBytes(data)...)
  44. // where
  45. // nonZeroBitset(data) is a bit vector with len(data) bits (MSB first):
  46. // nonZeroBitset(data)[i/8] && (1 << (7-i%8)) != 0 if data[i] != 0
  47. // len(nonZeroBitset(data)) == (len(data)+7)/8
  48. // nonZeroBytes(data) contains the non-zero bytes of data in the same order
  49. // CompressBytes compresses the input byte slice according to the sparse bitset
  50. // representation algorithm.
  51. func CompressBytes(data []byte) []byte {
  52. // Empty slices get compressed to nil
  53. if len(data) == 0 {
  54. return nil
  55. }
  56. // One byte slices compress to nil or retain the single byte
  57. if len(data) == 1 {
  58. if data[0] == 0 {
  59. return nil
  60. }
  61. return data
  62. }
  63. // Calculate the bitset of set bytes, and gather the non-zero bytes
  64. nonZeroBitset := make([]byte, (len(data)+7)/8)
  65. nonZeroBytes := make([]byte, 0, len(data))
  66. for i, b := range data {
  67. if b != 0 {
  68. nonZeroBytes = append(nonZeroBytes, b)
  69. nonZeroBitset[i/8] |= 1 << byte(7-i%8)
  70. }
  71. }
  72. if len(nonZeroBytes) == 0 {
  73. return nil
  74. }
  75. return append(CompressBytes(nonZeroBitset), nonZeroBytes...)
  76. }
  77. // DecompressBytes decompresses data with a known target size. In addition to the
  78. // decompressed output, the function returns the length of compressed input data
  79. // corresponding to the output as the input slice may be longer.
  80. func DecompressBytes(data []byte, target int) ([]byte, error) {
  81. out, size, err := decompressBytes(data, target)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if size != len(data) {
  86. return nil, ErrUnreferencedData
  87. }
  88. return out, nil
  89. }
  90. // decompressBytes decompresses data with a known target size. In addition to the
  91. // decompressed output, the function returns the length of compressed input data
  92. // corresponding to the output as the input slice may be longer.
  93. func decompressBytes(data []byte, target int) ([]byte, int, error) {
  94. // Sanity check 0 targets to avoid infinite recursion
  95. if target == 0 {
  96. return nil, 0, nil
  97. }
  98. // Handle the zero and single byte corner cases
  99. decomp := make([]byte, target)
  100. if len(data) == 0 {
  101. return decomp, 0, nil
  102. }
  103. if target == 1 {
  104. decomp[0] = data[0] // copy to avoid referencing the input slice
  105. if data[0] != 0 {
  106. return decomp, 1, nil
  107. }
  108. return decomp, 0, nil
  109. }
  110. // Decompress the bitset of set bytes and distribute the non zero bytes
  111. nonZeroBitset, ptr, err := decompressBytes(data, (target+7)/8)
  112. if err != nil {
  113. return nil, ptr, err
  114. }
  115. for i := 0; i < 8*len(nonZeroBitset); i++ {
  116. if nonZeroBitset[i/8]&(1<<byte(7-i%8)) != 0 {
  117. // Make sure we have enough data to push into the correct slot
  118. if ptr >= len(data) {
  119. return nil, 0, ErrMissingData
  120. }
  121. if i >= len(decomp) {
  122. return nil, 0, ErrExceededTarget
  123. }
  124. // Make sure the data is valid and push into the slot
  125. if data[ptr] == 0 {
  126. return nil, 0, ErrZeroContent
  127. }
  128. decomp[i] = data[ptr]
  129. ptr++
  130. }
  131. }
  132. return decomp, ptr, nil
  133. }