lz4.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Package lz4 implements reading and writing lz4 compressed data.
  2. //
  3. // The package supports both the LZ4 stream format,
  4. // as specified in http://fastcompression.blogspot.fr/2013/04/lz4-streaming-format-final.html,
  5. // and the LZ4 block format, defined at
  6. // http://fastcompression.blogspot.fr/2011/05/lz4-explained.html.
  7. //
  8. // See https://github.com/lz4/lz4 for the reference C implementation.
  9. package lz4
  10. import (
  11. "github.com/pierrec/lz4/v4/internal/lz4block"
  12. "github.com/pierrec/lz4/v4/internal/lz4errors"
  13. )
  14. func _() {
  15. // Safety checks for duplicated elements.
  16. var x [1]struct{}
  17. _ = x[lz4block.CompressionLevel(Fast)-lz4block.Fast]
  18. _ = x[Block64Kb-BlockSize(lz4block.Block64Kb)]
  19. _ = x[Block256Kb-BlockSize(lz4block.Block256Kb)]
  20. _ = x[Block1Mb-BlockSize(lz4block.Block1Mb)]
  21. _ = x[Block4Mb-BlockSize(lz4block.Block4Mb)]
  22. }
  23. // CompressBlockBound returns the maximum size of a given buffer of size n, when not compressible.
  24. func CompressBlockBound(n int) int {
  25. return lz4block.CompressBlockBound(n)
  26. }
  27. // UncompressBlock uncompresses the source buffer into the destination one,
  28. // and returns the uncompressed size.
  29. //
  30. // The destination buffer must be sized appropriately.
  31. //
  32. // An error is returned if the source data is invalid or the destination buffer is too small.
  33. func UncompressBlock(src, dst []byte) (int, error) {
  34. return lz4block.UncompressBlock(src, dst, nil)
  35. }
  36. // UncompressBlockWithDict uncompresses the source buffer into the destination one using a
  37. // dictionary, and returns the uncompressed size.
  38. //
  39. // The destination buffer must be sized appropriately.
  40. //
  41. // An error is returned if the source data is invalid or the destination buffer is too small.
  42. func UncompressBlockWithDict(src, dst, dict []byte) (int, error) {
  43. return lz4block.UncompressBlock(src, dst, dict)
  44. }
  45. // A Compressor compresses data into the LZ4 block format.
  46. // It uses a fast compression algorithm.
  47. //
  48. // A Compressor is not safe for concurrent use by multiple goroutines.
  49. //
  50. // Use a Writer to compress into the LZ4 stream format.
  51. type Compressor struct{ c lz4block.Compressor }
  52. // CompressBlock compresses the source buffer src into the destination dst.
  53. //
  54. // If compression is successful, the first return value is the size of the
  55. // compressed data, which is always >0.
  56. //
  57. // If dst has length at least CompressBlockBound(len(src)), compression always
  58. // succeeds. Otherwise, the first return value is zero. The error return is
  59. // non-nil if the compressed data does not fit in dst, but it might fit in a
  60. // larger buffer that is still smaller than CompressBlockBound(len(src)). The
  61. // return value (0, nil) means the data is likely incompressible and a buffer
  62. // of length CompressBlockBound(len(src)) should be passed in.
  63. func (c *Compressor) CompressBlock(src, dst []byte) (int, error) {
  64. return c.c.CompressBlock(src, dst)
  65. }
  66. // CompressBlock compresses the source buffer into the destination one.
  67. // This is the fast version of LZ4 compression and also the default one.
  68. //
  69. // The argument hashTable is scratch space for a hash table used by the
  70. // compressor. If provided, it should have length at least 1<<16. If it is
  71. // shorter (or nil), CompressBlock allocates its own hash table.
  72. //
  73. // The size of the compressed data is returned.
  74. //
  75. // If the destination buffer size is lower than CompressBlockBound and
  76. // the compressed size is 0 and no error, then the data is incompressible.
  77. //
  78. // An error is returned if the destination buffer is too small.
  79. // CompressBlock is equivalent to Compressor.CompressBlock.
  80. // The final argument is ignored and should be set to nil.
  81. //
  82. // This function is deprecated. Use a Compressor instead.
  83. func CompressBlock(src, dst []byte, _ []int) (int, error) {
  84. return lz4block.CompressBlock(src, dst)
  85. }
  86. // A CompressorHC compresses data into the LZ4 block format.
  87. // Its compression ratio is potentially better than that of a Compressor,
  88. // but it is also slower and requires more memory.
  89. //
  90. // A Compressor is not safe for concurrent use by multiple goroutines.
  91. //
  92. // Use a Writer to compress into the LZ4 stream format.
  93. type CompressorHC struct {
  94. // Level is the maximum search depth for compression.
  95. // Values <= 0 mean no maximum.
  96. Level CompressionLevel
  97. c lz4block.CompressorHC
  98. }
  99. // CompressBlock compresses the source buffer src into the destination dst.
  100. //
  101. // If compression is successful, the first return value is the size of the
  102. // compressed data, which is always >0.
  103. //
  104. // If dst has length at least CompressBlockBound(len(src)), compression always
  105. // succeeds. Otherwise, the first return value is zero. The error return is
  106. // non-nil if the compressed data does not fit in dst, but it might fit in a
  107. // larger buffer that is still smaller than CompressBlockBound(len(src)). The
  108. // return value (0, nil) means the data is likely incompressible and a buffer
  109. // of length CompressBlockBound(len(src)) should be passed in.
  110. func (c *CompressorHC) CompressBlock(src, dst []byte) (int, error) {
  111. return c.c.CompressBlock(src, dst, lz4block.CompressionLevel(c.Level))
  112. }
  113. // CompressBlockHC is equivalent to CompressorHC.CompressBlock.
  114. // The final two arguments are ignored and should be set to nil.
  115. //
  116. // This function is deprecated. Use a CompressorHC instead.
  117. func CompressBlockHC(src, dst []byte, depth CompressionLevel, _, _ []int) (int, error) {
  118. return lz4block.CompressBlockHC(src, dst, lz4block.CompressionLevel(depth))
  119. }
  120. const (
  121. // ErrInvalidSourceShortBuffer is returned by UncompressBlock or CompressBLock when a compressed
  122. // block is corrupted or the destination buffer is not large enough for the uncompressed data.
  123. ErrInvalidSourceShortBuffer = lz4errors.ErrInvalidSourceShortBuffer
  124. // ErrInvalidFrame is returned when reading an invalid LZ4 archive.
  125. ErrInvalidFrame = lz4errors.ErrInvalidFrame
  126. // ErrInternalUnhandledState is an internal error.
  127. ErrInternalUnhandledState = lz4errors.ErrInternalUnhandledState
  128. // ErrInvalidHeaderChecksum is returned when reading a frame.
  129. ErrInvalidHeaderChecksum = lz4errors.ErrInvalidHeaderChecksum
  130. // ErrInvalidBlockChecksum is returned when reading a frame.
  131. ErrInvalidBlockChecksum = lz4errors.ErrInvalidBlockChecksum
  132. // ErrInvalidFrameChecksum is returned when reading a frame.
  133. ErrInvalidFrameChecksum = lz4errors.ErrInvalidFrameChecksum
  134. // ErrOptionInvalidCompressionLevel is returned when the supplied compression level is invalid.
  135. ErrOptionInvalidCompressionLevel = lz4errors.ErrOptionInvalidCompressionLevel
  136. // ErrOptionClosedOrError is returned when an option is applied to a closed or in error object.
  137. ErrOptionClosedOrError = lz4errors.ErrOptionClosedOrError
  138. // ErrOptionInvalidBlockSize is returned when
  139. ErrOptionInvalidBlockSize = lz4errors.ErrOptionInvalidBlockSize
  140. // ErrOptionNotApplicable is returned when trying to apply an option to an object not supporting it.
  141. ErrOptionNotApplicable = lz4errors.ErrOptionNotApplicable
  142. // ErrWriterNotClosed is returned when attempting to reset an unclosed writer.
  143. ErrWriterNotClosed = lz4errors.ErrWriterNotClosed
  144. )