/
lzf_c.go
53 lines (46 loc) · 1.32 KB
/
lzf_c.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
// +build lzf_cgo
// Package lzf implements LZF compression algorithm.
package lzf
// #cgo CFLAGS: -O3 -DHLOG=14
// #include "src/lzf_c.c"
// #include "src/lzf_d.c"
import "C"
import "unsafe"
func p(in []byte) unsafe.Pointer {
if len(in) == 0 {
return unsafe.Pointer(nil)
}
return unsafe.Pointer(&in[0])
}
func clen(s []byte) C.uint {
return C.uint(len(s))
}
// Compress compresses `input` and puts the content in `output`.
// len(output) should have enough space for the compressed data.
// Returns the number of bytes in the `output` slice.
func Compress(input, output []byte) (outSize int, err error) {
outSize = int(C.lzf_compress(p(input), clen(input), p(output), clen(output)))
if outSize == 0 {
err = ErrInsufficientBuffer
}
return
}
// Decompress decompresses `input` and puts the content in `output`.
// len(output) should have enough space for the uncompressed data.
// Returns the number of bytes in the `output` slice.
func Decompress(input, output []byte) (outSize int, err error) {
var errCode C.int
outSize = int(C.lzf_decompress(p(input), clen(input), p(output), clen(output), &errCode))
if outSize > 0 {
return
}
switch errCode {
case C.E2BIG:
err = ErrInsufficientBuffer
case C.EINVAL:
err = ErrDataCorruption
default:
err = ErrUnknown
}
return
}