Example #1
0
File: lz4.go Project: rhakt/lz4
// Compress : compresses
func Compress(in, out []byte) (outSize int, err error) {
	outSize = int(C.LZ4_compress_limitedOutput(p(in), p(out), clen(in), clen(out)))
	if outSize <= 0 {
		err = fmt.Errorf("fail to compress")
	}
	return
}
Example #2
0
// Compress compresses in and puts the content in out. len(out)
// should have enough space for the compressed data (use CompressBound
// to calculate). Returns the number of bytes in the out slice.
func Compress(in []byte, out []byte) (outSize int, err error) {
	outSize = int(C.LZ4_compress_limitedOutput(p(in), p(out), clen(in), clen(out)))
	if outSize == 0 {
		err = fmt.Errorf("Insufficient space for compression")
	}
	return
}
Example #3
0
func lz4CompressSpeed(src []byte, dst []byte, maxSize uint32) (int, error) {
	if len(src) == 0 {
		return 0, nil
	}
	n := C.LZ4_compress_limitedOutput((*C.char)(unsafe.Pointer(&src[0])), (*C.char)(unsafe.Pointer(&dst[0])), C.int(len(src)), C.int(maxSize))
	if n <= 0 {
		return 0, errors.New("lz4: data corruption")
	}
	return int(n), nil
}
Example #4
0
// Compresses `input` and puts the content in `output`. `output` will be
// re-allocated if there is not sufficient space to store the maximum length
// compressed output.
func Compress(input []byte, output *[]byte) error {
	cb := CompressBound(input)
	if cap(*output) < cb {
		*output = make([]byte, 0, cb)
	}

	ip, op := charp(&input), charp(output)
	resultlen := int(C.LZ4_compress_limitedOutput(ip, op,
		C.int(len(input)), C.int(cap(*output))))
	if resultlen > cap(*output) {
		return fmt.Errorf("LZ4 overran compression buffer. This shouldn't happen. "+
			"Expected: %d, got %d", cap(*output), resultlen)
	}
	*output = (*output)[0:resultlen]
	return nil
}
Example #5
0
/*
Compress 'isize' bytes from 'source' into an output buffer 'dest' of maximum size 'maxOutputSize'.
If it cannot achieve it, compression will stop, and result of the function will be zero.
This function never writes outside of provided output buffer.

isize  : is the input size. Max supported value is ~1.9GB
maxOutputSize : is the size of the destination buffer (which must be already allocated)
return : the number of bytes written in buffer 'dest'
or 0 if the compression fails
*/
func CompressLimitedOutput(source, dest []byte, isize, maxOutputSize int) int {
	return int(C.LZ4_compress_limitedOutput(cast(source), cast(dest), C.int(isize), C.int(maxOutputSize)))
}