lz4.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Package lz4 implements reading and writing lz4 compressed data (a frame),
  2. // as specified in http://fastcompression.blogspot.fr/2013/04/lz4-streaming-format-final.html.
  3. //
  4. // Although the block level compression and decompression functions are exposed and are fully compatible
  5. // with the lz4 block format definition, they are low level and should not be used directly.
  6. // For a complete description of an lz4 compressed block, see:
  7. // http://fastcompression.blogspot.fr/2011/05/lz4-explained.html
  8. //
  9. // See https://github.com/lz4/lz4 for the reference C implementation.
  10. //
  11. package lz4
  12. import (
  13. "github.com/pierrec/lz4/v4/internal/lz4block"
  14. "github.com/pierrec/lz4/v4/internal/lz4errors"
  15. )
  16. func _() {
  17. // Safety checks for duplicated elements.
  18. var x [1]struct{}
  19. _ = x[lz4block.CompressionLevel(Fast)-lz4block.Fast]
  20. _ = x[Block64Kb-BlockSize(lz4block.Block64Kb)]
  21. _ = x[Block256Kb-BlockSize(lz4block.Block256Kb)]
  22. _ = x[Block1Mb-BlockSize(lz4block.Block1Mb)]
  23. _ = x[Block4Mb-BlockSize(lz4block.Block4Mb)]
  24. }
  25. // CompressBlockBound returns the maximum size of a given buffer of size n, when not compressible.
  26. func CompressBlockBound(n int) int {
  27. return lz4block.CompressBlockBound(n)
  28. }
  29. // UncompressBlock uncompresses the source buffer into the destination one,
  30. // and returns the uncompressed size.
  31. //
  32. // The destination buffer must be sized appropriately.
  33. //
  34. // An error is returned if the source data is invalid or the destination buffer is too small.
  35. func UncompressBlock(src, dst []byte) (int, error) {
  36. return lz4block.UncompressBlock(src, dst)
  37. }
  38. // CompressBlock compresses the source buffer into the destination one.
  39. // This is the fast version of LZ4 compression and also the default one.
  40. //
  41. // The argument hashTable is scratch space for a hash table used by the
  42. // compressor. If provided, it should have length at least 1<<16. If it is
  43. // shorter (or nil), CompressBlock allocates its own hash table.
  44. //
  45. // The size of the compressed data is returned.
  46. //
  47. // If the destination buffer size is lower than CompressBlockBound and
  48. // the compressed size is 0 and no error, then the data is incompressible.
  49. //
  50. // An error is returned if the destination buffer is too small.
  51. func CompressBlock(src, dst []byte, hashTable []int) (int, error) {
  52. return lz4block.CompressBlock(src, dst, hashTable)
  53. }
  54. // CompressBlockHC compresses the source buffer src into the destination dst
  55. // with max search depth (use 0 or negative value for no max).
  56. //
  57. // CompressBlockHC compression ratio is better than CompressBlock but it is also slower.
  58. //
  59. // The size of the compressed data is returned.
  60. //
  61. // If the destination buffer size is lower than CompressBlockBound and
  62. // the compressed size is 0 and no error, then the data is incompressible.
  63. //
  64. // An error is returned if the destination buffer is too small.
  65. func CompressBlockHC(src, dst []byte, depth CompressionLevel, hashTable, chainTable []int) (int, error) {
  66. return lz4block.CompressBlockHC(src, dst, lz4block.CompressionLevel(depth), hashTable, chainTable)
  67. }
  68. const (
  69. // ErrInvalidSourceShortBuffer is returned by UncompressBlock or CompressBLock when a compressed
  70. // block is corrupted or the destination buffer is not large enough for the uncompressed data.
  71. ErrInvalidSourceShortBuffer = lz4errors.ErrInvalidSourceShortBuffer
  72. // ErrInvalidFrame is returned when reading an invalid LZ4 archive.
  73. ErrInvalidFrame = lz4errors.ErrInvalidFrame
  74. // ErrInternalUnhandledState is an internal error.
  75. ErrInternalUnhandledState = lz4errors.ErrInternalUnhandledState
  76. // ErrInvalidHeaderChecksum is returned when reading a frame.
  77. ErrInvalidHeaderChecksum = lz4errors.ErrInvalidHeaderChecksum
  78. // ErrInvalidBlockChecksum is returned when reading a frame.
  79. ErrInvalidBlockChecksum = lz4errors.ErrInvalidBlockChecksum
  80. // ErrInvalidFrameChecksum is returned when reading a frame.
  81. ErrInvalidFrameChecksum = lz4errors.ErrInvalidFrameChecksum
  82. // ErrOptionInvalidCompressionLevel is returned when the supplied compression level is invalid.
  83. ErrOptionInvalidCompressionLevel = lz4errors.ErrOptionInvalidCompressionLevel
  84. // ErrOptionClosedOrError is returned when an option is applied to a closed or in error object.
  85. ErrOptionClosedOrError = lz4errors.ErrOptionClosedOrError
  86. // ErrOptionInvalidBlockSize is returned when
  87. ErrOptionInvalidBlockSize = lz4errors.ErrOptionInvalidBlockSize
  88. // ErrOptionNotApplicable is returned when trying to apply an option to an object not supporting it.
  89. ErrOptionNotApplicable = lz4errors.ErrOptionNotApplicable
  90. // ErrWriterNotClosed is returned when attempting to reset an unclosed writer.
  91. ErrWriterNotClosed = lz4errors.ErrWriterNotClosed
  92. )