-
Notifications
You must be signed in to change notification settings - Fork 0
/
mozlz4.go
66 lines (51 loc) · 1.28 KB
/
mozlz4.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
// Copyright (c) 2024 cions
// Licensed under the MIT License. See LICENSE for details.
package mozlz4
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/pierrec/lz4/v4"
)
var HEADER = []byte("mozLz40\x00")
var ErrInvalid = errors.New("not a mozlz4 file")
func Compress(data []byte) ([]byte, error) {
buf := make([]byte, 0, len(HEADER)+4+lz4.CompressBlockBound(len(data)))
buf = append(buf, HEADER...)
buf, err := binary.Append(buf, binary.LittleEndian, uint32(len(data)))
if err != nil {
return nil, err
}
if len(data) != 0 {
var c lz4.Compressor
n, err := c.CompressBlock(data, buf[len(buf):cap(buf)])
if err != nil {
return nil, err
}
buf = buf[:len(buf)+n]
}
return buf, nil
}
func Decompress(data []byte) ([]byte, error) {
data, found := bytes.CutPrefix(data, HEADER)
if !found {
return nil, ErrInvalid
}
var size uint32
if n, err := binary.Decode(data, binary.LittleEndian, &size); err != nil {
return nil, ErrInvalid
} else {
data = data[n:]
}
if size == 0 {
return []byte{}, nil
}
buf := make([]byte, size)
if n, err := lz4.UncompressBlock(data, buf); err != nil {
return nil, err
} else if uint32(n) != size {
return nil, fmt.Errorf("uncompressed size mismatch: expected %v, but got %v", size, n)
}
return buf, nil
}