This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfastlz.go
62 lines (49 loc) · 1.41 KB
/
fastlz.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
package fastlz
// #include <stdlib.h>
// #include "fastlz.h"
import "C"
// Other imports
import (
"errors"
"fmt"
"unsafe"
)
// Compress applies fastlz_compress function to input
func Compress(input []byte) ([]byte, error) {
length := len(input)
if length == 0 {
return nil, errors.New("fastlz: empty input")
}
// Output buffer
output := make([]byte, int(float64(length)*1.4))
// Run
num := C.fastlz_compress(unsafe.Pointer(&input[0]), C.int(length), unsafe.Pointer(&output[0]))
// Empty compression result
if num == 0 {
return nil, errors.New("fastlz: compression error, empty result")
}
return output[:num], nil
}
// Decompress applies fastlz_decompress function to input
func Decompress(input []byte, maxOut uint) ([]byte, error) {
length := len(input)
if length == 0 {
return nil, errors.New("fastlz: empty input")
}
if maxOut == 0 {
return nil, errors.New("fastlz: invalid max out value")
}
// Output buffer
output := make([]byte, int(maxOut))
// Run
num := C.fastlz_decompress(unsafe.Pointer(&input[0]), C.int(length), unsafe.Pointer(&output[0]), C.int(maxOut))
goNum := int(num)
// Empty decompression result
if goNum == 0 {
return nil, errors.New("fastlz: decompression error, empty result")
}
if goNum != int(maxOut) {
return nil, fmt.Errorf("fastlz: decompression error, shit happens! Max out: %d, num: %d, input data:%#v", maxOut, goNum, input)
}
return output[:goNum], nil
}