-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
281 lines (250 loc) · 5.1 KB
/
frame.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// frame.go
// Copyright (C) 2017 weirdgiraffe <[email protected]>
//
// Distributed under terms of the MIT license.
//
package lz4
import (
"fmt"
"io"
"github.com/OneOfOne/xxhash"
)
const lz4Magic uint32 = 0x184d2204
const (
lz64KB = 65536
lz256KB = 262144
lz1MB = 1048576
lz4MB = 4194304
)
type Decompressor struct {
r io.Reader
buf [8]byte
}
func NewDecompressor() *Decompressor {
return &Decompressor{}
}
type FrameDesc struct {
Independent bool
HasBlockChecksum bool
HasContentChecksum bool
BlockMaxSize int
ContentSize uint64
}
const (
lzFLGByte = 0
lzBDByte = 1
lzMaxFrameDescLen = 11
)
func readFrameDesc(r io.Reader) (f *FrameDesc, err error) {
var b [lzMaxFrameDescLen]byte
// read FLG byte + BD byte + (HC byte or first byte of ContentSize)
err = read(r, b[:3])
if err != nil {
return
}
// check version is 01
if b[lzFLGByte]&0xc0 != 0x40 {
err = fmt.Errorf("FrameDesc: version must be 01")
return
}
// check reserved bits are 0
if b[lzFLGByte]&0x03 != 0 && b[lzBDByte]&0x8f != 0 {
err = fmt.Errorf("FrameDesc: reserved bits must be zero")
return
}
bSize := lzBlockMaxSize(b[lzBDByte] & 0x70 >> 4)
if bSize == -1 {
err = fmt.Errorf("FrameDesc: unsupported Block Maximum size")
return
}
// optional ContentSize field
var contentSize uint64
i := lzBDByte + 1
if b[lzFLGByte]&0x08 != 0 {
// first ContentSize byte is already in b, read rest + HC byte
err = read(r, b[i+1:i+9])
if err != nil {
return
}
contentSize = leUint64(b[i : i+8])
i += 8
}
// HC byte
hChecksum := byte((xxhash.Checksum32(b[:i]) >> 8) & 0xff)
if hChecksum != b[i] {
err = fmt.Errorf(
"FrameDesc: checksum mismatch %02x != %02x",
hChecksum, b[i],
)
return
}
return &FrameDesc{
Independent: b[lzFLGByte]&0x20 != 0,
HasBlockChecksum: b[lzFLGByte]&0x10 != 0,
HasContentChecksum: b[lzFLGByte]&0x04 != 0,
BlockMaxSize: bSize,
ContentSize: contentSize,
}, nil
}
func (d *Decompressor) readBlock(block []byte) (err error) {
err = read(d.r, block)
if err != nil {
return
}
return nil
}
func (d *Decompressor) Decompress(r io.Reader, w io.Writer) (err error) {
d.r = r
var magic uint32
magic, err = d.readUint32()
if magic != lz4Magic {
err = fmt.Errorf("Decompress: Frame magic not match")
return
}
var desc *FrameDesc
desc, err = readFrameDesc(d.r)
if err != nil {
return
}
in := make([]byte, desc.BlockMaxSize)
out := make([]byte, desc.BlockMaxSize)
var cMust *xxhash.XXHash32
if desc.HasContentChecksum {
cMust = xxhash.New32()
}
var bLen, n int
var compressed bool
for {
bLen, compressed, err = d.readBlockLen(desc.BlockMaxSize)
if err != nil {
return
}
if bLen == 0 { // EndMark
break
}
err = d.readBlock(in[:bLen])
if err != nil {
return
}
if desc.HasBlockChecksum {
var bChecksum uint32
bChecksum, err = d.readUint32()
if err != nil {
return
}
must := xxhash.Checksum32(in[:bLen])
if bChecksum != must {
err = fmt.Errorf("DecodeFrame: Block checksum mismatch")
return
}
}
if compressed {
n, err = DecompressBlock(in[:bLen], out)
if err != nil {
return
}
} else {
n = copy(out, in[:bLen])
}
if desc.HasContentChecksum {
_, err = cMust.Write(out[:n])
if err != nil {
return err
}
}
err = write(w, out[:n])
if err != nil {
return
}
}
if desc.HasContentChecksum {
var cChecksum uint32
cChecksum, err = d.readUint32()
if err != nil {
return err
}
if cChecksum != cMust.Sum32() {
err = fmt.Errorf("DecodeFrame: Content checksum mismatch")
return
}
}
return nil
}
func (d *Decompressor) readUint64() (uint64, error) {
err := read(d.r, d.buf[:8])
if err != nil {
return 0, err
}
return leUint64(d.buf[:8]), nil
}
func (d *Decompressor) readUint32() (uint32, error) {
err := read(d.r, d.buf[:4])
if err != nil {
return 0, err
}
return leUint32(d.buf[:4]), nil
}
func read(r io.Reader, b []byte) error {
n, err := r.Read(b)
if err != nil {
return err
}
if n != len(b) {
return fmt.Errorf("Could not read enough data (need %d) got EOF", len(b))
}
return err
}
func write(w io.Writer, b []byte) error {
n, err := w.Write(b)
if err != nil {
return err
}
if n != len(b) {
return fmt.Errorf("Decomressor: schrinked write")
}
return err
}
func (d *Decompressor) readBlockLen(maxLen int) (len int, compressed bool, err error) {
ulen, err := d.readUint32()
if err != nil {
return
}
compressed = (ulen & 0x80000000) == 0
len = int(ulen & 0x7fffffff)
if len > maxLen {
err = fmt.Errorf("DecompressFrame: malformed block size")
return
}
return len, compressed, nil
}
func lzBlockMaxSize(b byte) int {
switch b {
case 4:
return lz64KB
case 5:
return lz256KB
case 6:
return lz1MB
case 7:
return lz4MB
default:
return -1
}
}
func leUint32(b []byte) uint32 {
return uint32(b[0]) |
uint32(b[1])<<8 |
uint32(b[2])<<16 |
uint32(b[3])<<24
}
func leUint64(b []byte) uint64 {
return uint64(b[0]) |
uint64(b[1])<<8 |
uint64(b[2])<<16 |
uint64(b[3])<<24 |
uint64(b[4])<<32 |
uint64(b[5])<<40 |
uint64(b[6])<<48 |
uint64(b[7])<<52
}