frame.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Package lz4stream provides the types that support reading and writing LZ4 data streams.
  2. package lz4stream
  3. import (
  4. "encoding/binary"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "github.com/pierrec/lz4/v4/internal/lz4block"
  9. "github.com/pierrec/lz4/v4/internal/lz4errors"
  10. "github.com/pierrec/lz4/v4/internal/xxh32"
  11. )
  12. //go:generate go run gen.go
  13. const (
  14. frameMagic uint32 = 0x184D2204
  15. frameSkipMagic uint32 = 0x184D2A50
  16. frameMagicLegacy uint32 = 0x184C2102
  17. )
  18. func NewFrame() *Frame {
  19. return &Frame{}
  20. }
  21. type Frame struct {
  22. buf [15]byte // frame descriptor needs at most 4(magic)+4+8+1=11 bytes
  23. Magic uint32
  24. Descriptor FrameDescriptor
  25. Blocks Blocks
  26. Checksum uint32
  27. checksum xxh32.XXHZero
  28. }
  29. // Reset allows reusing the Frame.
  30. // The Descriptor configuration is not modified.
  31. func (f *Frame) Reset(num int) {
  32. f.Magic = 0
  33. f.Descriptor.Checksum = 0
  34. f.Descriptor.ContentSize = 0
  35. _ = f.Blocks.close(f, num)
  36. f.Checksum = 0
  37. }
  38. func (f *Frame) InitW(dst io.Writer, num int, legacy bool) {
  39. if legacy {
  40. f.Magic = frameMagicLegacy
  41. idx := lz4block.Index(lz4block.Block8Mb)
  42. f.Descriptor.Flags.BlockSizeIndexSet(idx)
  43. } else {
  44. f.Magic = frameMagic
  45. f.Descriptor.initW()
  46. }
  47. f.Blocks.initW(f, dst, num)
  48. f.checksum.Reset()
  49. }
  50. func (f *Frame) CloseW(dst io.Writer, num int) error {
  51. if err := f.Blocks.close(f, num); err != nil {
  52. return err
  53. }
  54. if f.isLegacy() {
  55. return nil
  56. }
  57. buf := f.buf[:0]
  58. // End mark (data block size of uint32(0)).
  59. buf = append(buf, 0, 0, 0, 0)
  60. if f.Descriptor.Flags.ContentChecksum() {
  61. buf = f.checksum.Sum(buf)
  62. }
  63. _, err := dst.Write(buf)
  64. return err
  65. }
  66. func (f *Frame) isLegacy() bool {
  67. return f.Magic == frameMagicLegacy
  68. }
  69. func (f *Frame) ParseHeaders(src io.Reader) error {
  70. if f.Magic > 0 {
  71. // Header already read.
  72. return nil
  73. }
  74. newFrame:
  75. var err error
  76. if f.Magic, err = f.readUint32(src); err != nil {
  77. return err
  78. }
  79. switch m := f.Magic; {
  80. case m == frameMagic || m == frameMagicLegacy:
  81. // All 16 values of frameSkipMagic are valid.
  82. case m>>8 == frameSkipMagic>>8:
  83. skip, err := f.readUint32(src)
  84. if err != nil {
  85. return err
  86. }
  87. if _, err := io.CopyN(ioutil.Discard, src, int64(skip)); err != nil {
  88. return err
  89. }
  90. goto newFrame
  91. default:
  92. return lz4errors.ErrInvalidFrame
  93. }
  94. if err := f.Descriptor.initR(f, src); err != nil {
  95. return err
  96. }
  97. f.checksum.Reset()
  98. return nil
  99. }
  100. func (f *Frame) InitR(src io.Reader, num int) (chan []byte, error) {
  101. return f.Blocks.initR(f, num, src)
  102. }
  103. func (f *Frame) CloseR(src io.Reader) (err error) {
  104. if f.isLegacy() {
  105. return nil
  106. }
  107. if !f.Descriptor.Flags.ContentChecksum() {
  108. return nil
  109. }
  110. if f.Checksum, err = f.readUint32(src); err != nil {
  111. return err
  112. }
  113. if c := f.checksum.Sum32(); c != f.Checksum {
  114. return fmt.Errorf("%w: got %x; expected %x", lz4errors.ErrInvalidFrameChecksum, c, f.Checksum)
  115. }
  116. return nil
  117. }
  118. type FrameDescriptor struct {
  119. Flags DescriptorFlags
  120. ContentSize uint64
  121. Checksum uint8
  122. }
  123. func (fd *FrameDescriptor) initW() {
  124. fd.Flags.VersionSet(1)
  125. fd.Flags.BlockIndependenceSet(true)
  126. }
  127. func (fd *FrameDescriptor) Write(f *Frame, dst io.Writer) error {
  128. if fd.Checksum > 0 {
  129. // Header already written.
  130. return nil
  131. }
  132. buf := f.buf[:4]
  133. // Write the magic number here even though it belongs to the Frame.
  134. binary.LittleEndian.PutUint32(buf, f.Magic)
  135. if !f.isLegacy() {
  136. buf = buf[:4+2]
  137. binary.LittleEndian.PutUint16(buf[4:], uint16(fd.Flags))
  138. if fd.Flags.Size() {
  139. buf = buf[:4+2+8]
  140. binary.LittleEndian.PutUint64(buf[4+2:], fd.ContentSize)
  141. }
  142. fd.Checksum = descriptorChecksum(buf[4:])
  143. buf = append(buf, fd.Checksum)
  144. }
  145. _, err := dst.Write(buf)
  146. return err
  147. }
  148. func (fd *FrameDescriptor) initR(f *Frame, src io.Reader) error {
  149. if f.isLegacy() {
  150. idx := lz4block.Index(lz4block.Block8Mb)
  151. f.Descriptor.Flags.BlockSizeIndexSet(idx)
  152. return nil
  153. }
  154. // Read the flags and the checksum, hoping that there is not content size.
  155. buf := f.buf[:3]
  156. if _, err := io.ReadFull(src, buf); err != nil {
  157. return err
  158. }
  159. descr := binary.LittleEndian.Uint16(buf)
  160. fd.Flags = DescriptorFlags(descr)
  161. if fd.Flags.Size() {
  162. // Append the 8 missing bytes.
  163. buf = buf[:3+8]
  164. if _, err := io.ReadFull(src, buf[3:]); err != nil {
  165. return err
  166. }
  167. fd.ContentSize = binary.LittleEndian.Uint64(buf[2:])
  168. }
  169. fd.Checksum = buf[len(buf)-1] // the checksum is the last byte
  170. buf = buf[:len(buf)-1] // all descriptor fields except checksum
  171. if c := descriptorChecksum(buf); fd.Checksum != c {
  172. return fmt.Errorf("%w: got %x; expected %x", lz4errors.ErrInvalidHeaderChecksum, c, fd.Checksum)
  173. }
  174. // Validate the elements that can be.
  175. if idx := fd.Flags.BlockSizeIndex(); !idx.IsValid() {
  176. return lz4errors.ErrOptionInvalidBlockSize
  177. }
  178. return nil
  179. }
  180. func descriptorChecksum(buf []byte) byte {
  181. return byte(xxh32.ChecksumZero(buf) >> 8)
  182. }