Skip to content

Commit fd5d51c

Browse files
committed
common/bitutil: added data compression algorithm
1 parent 2ec5cf1 commit fd5d51c

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

‎common/bitutil/compress.go‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
17+
package bitutil
18+
19+
/*
20+
The compression algorithm implemented by CompressBytes and DecompressBytes is
21+
optimized for "sparse" input data which contains a lot of zero bytes. Decompression
22+
requires knowledge of the decompressed data length. Compression works as follows:
23+
24+
if data only contains zeroes,
25+
CompressBytes(data) == nil
26+
otherwise if len(data) <= 1,
27+
CompressBytes(data) == data
28+
otherwise:
29+
CompressBytes(data) == append(CompressBytes(nonZeroBits(data)), nonZeroBytes(data)...)
30+
where
31+
nonZeroBits(data) is a bit vector with len(data) bits (MSB first):
32+
nonZeroBits(data)[i/8] && (1 << (7-i%8)) != 0 if data[i] != 0
33+
len(nonZeroBits(data)) == (len(data)+7)/8
34+
nonZeroBytes(data) contains the non-zero bytes of data in the same order
35+
*/
36+
37+
// CompressBytes compresses the input byte slice
38+
func CompressBytes(data []byte) []byte {
39+
if len(data) == 0 {
40+
return nil
41+
}
42+
if len(data) == 1 {
43+
if data[0] == 0 {
44+
return nil
45+
} else {
46+
return data
47+
}
48+
}
49+
50+
bitsLen := (len(data) + 7) / 8
51+
nonZeroBits := make([]byte, bitsLen)
52+
nonZeroBytes := make([]byte, 0, len(data))
53+
for i, b := range data {
54+
if b != 0 {
55+
nonZeroBytes = append(nonZeroBytes, b)
56+
nonZeroBits[i/8] |= 1 << byte(7-i%8)
57+
}
58+
}
59+
if len(nonZeroBytes) == 0 {
60+
return nil
61+
}
62+
return append(CompressBytes(nonZeroBits), nonZeroBytes...)
63+
}
64+
65+
// DecompressBytes decompresses data with a known target size.
66+
// In addition to the decompressed output, the function returns the length of
67+
// compressed input data corresponding to the output. The input slice may be longer.
68+
// If the input slice is too short, (nil, -1) is returned.
69+
func DecompressBytes(data []byte, targetLen int) ([]byte, int) {
70+
decomp := make([]byte, targetLen)
71+
if len(data) == 0 {
72+
return decomp, 0
73+
}
74+
if targetLen == 1 {
75+
return data[0:1], 1
76+
}
77+
78+
bitsLen := (targetLen + 7) / 8
79+
nonZeroBits, ptr := DecompressBytes(data, bitsLen)
80+
if ptr < 0 {
81+
return nil, -1
82+
}
83+
for i, _ := range decomp {
84+
if nonZeroBits[i/8]&(1<<byte(7-i%8)) != 0 {
85+
if ptr == len(data) {
86+
return nil, -1
87+
}
88+
decomp[i] = data[ptr]
89+
ptr++
90+
}
91+
}
92+
return decomp, ptr
93+
}

0 commit comments

Comments
 (0)