lz4_hc.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package lz4
  2. // #cgo CFLAGS: -O3
  3. // #include "src/lz4hc.h"
  4. // #include "src/lz4hc.c"
  5. import "C"
  6. import (
  7. "fmt"
  8. )
  9. // CompressHC compresses in and puts the content in out. len(out)
  10. // should have enough space for the compressed data (use CompressBound
  11. // to calculate). Returns the number of bytes in the out slice. Determines
  12. // the compression level automatically.
  13. func CompressHC(in, out []byte) (int, error) {
  14. // 0 automatically sets the compression level.
  15. return CompressHCLevel(in, out, 0)
  16. }
  17. // CompressHCLevel compresses in at the given compression level and puts the
  18. // content in out. len(out) should have enough space for the compressed data
  19. // (use CompressBound to calculate). Returns the number of bytes in the out
  20. // slice. To automatically choose the compression level, use 0. Otherwise, use
  21. // any value in the inclusive range 1 (worst) through 16 (best). Most
  22. // applications will prefer CompressHC.
  23. func CompressHCLevel(in, out []byte, level int) (outSize int, err error) {
  24. // LZ4HC does not handle empty buffers. Pass through to Compress.
  25. if len(in) == 0 || len(out) == 0 {
  26. return Compress(in, out)
  27. }
  28. outSize = int(C.LZ4_compressHC2_limitedOutput(p(in), p(out), clen(in), clen(out), C.int(level)))
  29. if outSize == 0 {
  30. err = fmt.Errorf("insufficient space for compression")
  31. }
  32. return
  33. }