-
Notifications
You must be signed in to change notification settings - Fork 1
/
compress.go
68 lines (61 loc) · 1.91 KB
/
compress.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
// Copyright © 2023 Peter Haag peter@people.ops-trust.net
// All rights reserved.
//
// Use of this source code is governed by the license that can be
// found in the LICENSE file.
package nfdump
import (
"bytes"
"compress/bzip2"
"fmt"
"io"
zstd "github.com/klauspost/compress/zstd"
"github.com/pierrec/lz4/v4"
lzo "github.com/rasky/go-lzo"
)
func (nfFile *NfFile) uncompressBlock(blockHeader *DataBlockHeader) ([]byte, error) {
dataBlock := make([]byte, blockHeader.Size)
if _, err := io.ReadAtLeast(nfFile.file, dataBlock, int(blockHeader.Size)); err != nil {
return nil, fmt.Errorf("nfFile read appendix data block: %v", err)
}
switch nfFile.Header.Compression {
case NOT_COMPRESSED:
case LZO_COMPRESSED:
out, err := lzo.Decompress1X(bytes.NewReader(dataBlock), int(blockHeader.Size), BUFFSIZE)
if err != nil {
return nil, fmt.Errorf("nfFile uncompress lzo1x-1 data block: %v", err)
}
dataBlock = out
blockHeader.Size = uint32(len(out))
case BZ2_COMPRESSED:
reader := bzip2.NewReader(bytes.NewReader(dataBlock))
out := make([]byte, BUFFSIZE)
n, err := reader.Read(out)
if err != nil {
return nil, fmt.Errorf("nfFile uncompress bzip2 data block: %v", err)
}
out = out[:n]
dataBlock = out
blockHeader.Size = uint32(n)
case LZ4_COMPRESSED:
out := make([]byte, BUFFSIZE)
n, err := lz4.UncompressBlock(dataBlock, out)
if err != nil {
return nil, fmt.Errorf("nfFile uncompress lz4 data block: %v", err)
}
out = out[:n]
dataBlock = out
blockHeader.Size = uint32(n)
case ZSTD_COMPRESSED:
var decoder, _ = zstd.NewReader(nil, zstd.WithDecoderConcurrency(0))
out, err := decoder.DecodeAll(dataBlock, nil)
if err != nil {
return nil, fmt.Errorf("nfFile uncompress zstd data block: %v", err)
}
dataBlock = out
blockHeader.Size = uint32(len(out))
default:
return nil, fmt.Errorf("unknown data block compression: %d", nfFile.Header.Compression)
}
return dataBlock, nil
}