writer_legacy.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package lz4
  2. import (
  3. "encoding/binary"
  4. "io"
  5. )
  6. // WriterLegacy implements the LZ4Demo frame decoder.
  7. type WriterLegacy struct {
  8. Header
  9. // Handler called when a block has been successfully read.
  10. // It provides the number of bytes read.
  11. OnBlockDone func(size int)
  12. dst io.Writer // Destination.
  13. data []byte // Data to be compressed + buffer for compressed data.
  14. idx int // Index into data.
  15. hashtable [winSize]int // Hash table used in CompressBlock().
  16. }
  17. // NewWriterLegacy returns a new LZ4 encoder for the legacy frame format.
  18. // No access to the underlying io.Writer is performed.
  19. // The supplied Header is checked at the first Write.
  20. // It is ok to change it before the first Write but then not until a Reset() is performed.
  21. func NewWriterLegacy(dst io.Writer) *WriterLegacy {
  22. z := new(WriterLegacy)
  23. z.Reset(dst)
  24. return z
  25. }
  26. // Write compresses data from the supplied buffer into the underlying io.Writer.
  27. // Write does not return until the data has been written.
  28. func (z *WriterLegacy) Write(buf []byte) (int, error) {
  29. if !z.Header.done {
  30. if err := z.writeHeader(); err != nil {
  31. return 0, err
  32. }
  33. }
  34. if debugFlag {
  35. debug("input buffer len=%d index=%d", len(buf), z.idx)
  36. }
  37. zn := len(z.data)
  38. var n int
  39. for len(buf) > 0 {
  40. if z.idx == 0 && len(buf) >= zn {
  41. // Avoid a copy as there is enough data for a block.
  42. if err := z.compressBlock(buf[:zn]); err != nil {
  43. return n, err
  44. }
  45. n += zn
  46. buf = buf[zn:]
  47. continue
  48. }
  49. // Accumulate the data to be compressed.
  50. m := copy(z.data[z.idx:], buf)
  51. n += m
  52. z.idx += m
  53. buf = buf[m:]
  54. if debugFlag {
  55. debug("%d bytes copied to buf, current index %d", n, z.idx)
  56. }
  57. if z.idx < len(z.data) {
  58. // Buffer not filled.
  59. if debugFlag {
  60. debug("need more data for compression")
  61. }
  62. return n, nil
  63. }
  64. // Buffer full.
  65. if err := z.compressBlock(z.data); err != nil {
  66. return n, err
  67. }
  68. z.idx = 0
  69. }
  70. return n, nil
  71. }
  72. // writeHeader builds and writes the header to the underlying io.Writer.
  73. func (z *WriterLegacy) writeHeader() error {
  74. // Legacy has fixed 8MB blocksizes
  75. // https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md#legacy-frame
  76. bSize := 2 * blockSize4M
  77. buf := make([]byte, 2*bSize, 2*bSize)
  78. z.data = buf[:bSize] // Uncompressed buffer is the first half.
  79. z.idx = 0
  80. // Header consists of one mageic number, write it out.
  81. if err := binary.Write(z.dst, binary.LittleEndian, frameMagicLegacy); err != nil {
  82. return err
  83. }
  84. z.Header.done = true
  85. if debugFlag {
  86. debug("wrote header %v", z.Header)
  87. }
  88. return nil
  89. }
  90. // compressBlock compresses a block.
  91. func (z *WriterLegacy) compressBlock(data []byte) error {
  92. bSize := 2 * blockSize4M
  93. zdata := z.data[bSize:cap(z.data)]
  94. // The compressed block size cannot exceed the input's.
  95. var zn int
  96. if level := z.Header.CompressionLevel; level != 0 {
  97. zn, _ = CompressBlockHC(data, zdata, level)
  98. } else {
  99. zn, _ = CompressBlock(data, zdata, z.hashtable[:])
  100. }
  101. if debugFlag {
  102. debug("block compression %d => %d", len(data), zn)
  103. }
  104. zdata = zdata[:zn]
  105. // Write the block.
  106. if err := binary.Write(z.dst, binary.LittleEndian, uint32(zn)); err != nil {
  107. return err
  108. }
  109. written, err := z.dst.Write(zdata)
  110. if err != nil {
  111. return err
  112. }
  113. if h := z.OnBlockDone; h != nil {
  114. h(written)
  115. }
  116. return nil
  117. }
  118. // Flush flushes any pending compressed data to the underlying writer.
  119. // Flush does not return until the data has been written.
  120. // If the underlying writer returns an error, Flush returns that error.
  121. func (z *WriterLegacy) Flush() error {
  122. if debugFlag {
  123. debug("flush with index %d", z.idx)
  124. }
  125. if z.idx == 0 {
  126. return nil
  127. }
  128. data := z.data[:z.idx]
  129. z.idx = 0
  130. return z.compressBlock(data)
  131. }
  132. // Close closes the WriterLegacy, flushing any unwritten data to the underlying io.Writer, but does not close the underlying io.Writer.
  133. func (z *WriterLegacy) Close() error {
  134. if !z.Header.done {
  135. if err := z.writeHeader(); err != nil {
  136. return err
  137. }
  138. }
  139. if err := z.Flush(); err != nil {
  140. return err
  141. }
  142. if debugFlag {
  143. debug("writing last empty block")
  144. }
  145. return nil
  146. }
  147. // Reset clears the state of the WriterLegacy z such that it is equivalent to its
  148. // initial state from NewWriterLegacy, but instead writing to w.
  149. // No access to the underlying io.Writer is performed.
  150. func (z *WriterLegacy) Reset(w io.Writer) {
  151. z.Header.Reset()
  152. z.dst = w
  153. z.idx = 0
  154. // reset hashtable to ensure deterministic output.
  155. for i := range z.hashtable {
  156. z.hashtable[i] = 0
  157. }
  158. }