// Package lz4stream provides the types that support reading and writing LZ4 data streams.
package lz4stream import ( ) //go:generate go run gen.go const ( frameMagic uint32 = 0x184D2204 frameSkipMagic uint32 = 0x184D2A50 frameMagicLegacy uint32 = 0x184C2102 ) func () *Frame { return &Frame{} } type Frame struct { buf [15]byte // frame descriptor needs at most 2(flags)+8(size)+4(dict id)+1(checksum)=15 bytes Magic uint32 Descriptor FrameDescriptor Blocks Blocks Checksum uint32 checksum xxh32.XXHZero size uint64 // uncompressed bytes read or written so far, checked against Descriptor.ContentSize } // unexpectedEOF is for reads that must not hit the end of the source, // because the frame is not complete yet. func unexpectedEOF( error) error { if == io.EOF { return io.ErrUnexpectedEOF } return } // Reset allows reusing the Frame. // The Descriptor configuration is not modified. func ( *Frame) ( int) { .Magic = 0 .Descriptor.Checksum = 0 .Descriptor.ContentSize = 0 _ = .Blocks.close(, ) .Checksum = 0 } func ( *Frame) ( io.Writer, int, bool) { if { .Magic = frameMagicLegacy } else { .Magic = frameMagic .Descriptor.initW() } .Blocks.initW(, , ) .checksum.Reset() .size = 0 } func ( *Frame) ( io.Writer, int) error { if := .Blocks.close(, ); != nil { return } if .isLegacy() { return nil } if .Descriptor.Flags.Size() && .size != .Descriptor.ContentSize { // Leave the frame without its end mark: it is invalid anyway. return fmt.Errorf("%w: wrote %d; expected %d", lz4errors.ErrInvalidContentSize, .size, .Descriptor.ContentSize) } := .buf[:0] // End mark (data block size of uint32(0)). = append(, 0, 0, 0, 0) if .Descriptor.Flags.ContentChecksum() { = .checksum.Sum() } , := .Write() return } func ( *Frame) () bool { return .Magic == frameMagicLegacy } // BlockSizeIndex returns the block size of the frame; legacy frames // have no descriptors and return 8Mb. func ( *Frame) () lz4block.BlockSizeIndex { if .isLegacy() { return lz4block.Index(lz4block.Block8Mb) } return .Descriptor.Flags.BlockSizeIndex() } func ( *Frame) ( io.Reader) error { if .Magic > 0 { // Header already read. return nil } : var error if .Magic, = .readUint32(); != nil { return } switch := .Magic; { case == frameMagic || == frameMagicLegacy: // All 16 values of frameSkipMagic are valid. case >>8 == frameSkipMagic>>8: , := .readUint32() if != nil { return unexpectedEOF() } if , := io.CopyN(io.Discard, , int64()); != nil { return unexpectedEOF() } goto default: return lz4errors.ErrInvalidFrame } if := .Descriptor.initR(, ); != nil { return unexpectedEOF() } .checksum.Reset() .size = 0 return nil } func ( *Frame) ( io.Reader, int) (chan []byte, error) { return .Blocks.initR(, , ) } func ( *Frame) ( io.Reader) ( error) { if .isLegacy() { return nil } := if := .Blocks.reader; != nil { // The async reader checksums and counts on its own copy of the // frame. Its goroutines are done by the time its channel closes, // which is before the caller sees end of stream and gets here. = &.frame } if .Descriptor.Flags.ContentChecksum() { if .Checksum, = .readUint32(); != nil { return unexpectedEOF() } if := .checksum.Sum32(); != .Checksum { return fmt.Errorf("%w: got %x; expected %x", lz4errors.ErrInvalidFrameChecksum, , .Checksum) } } if .Descriptor.Flags.Size() && .size != .Descriptor.ContentSize { return fmt.Errorf("%w: got %d; expected %d", lz4errors.ErrInvalidContentSize, .size, .Descriptor.ContentSize) } return nil } type FrameDescriptor struct { Flags DescriptorFlags ContentSize uint64 Checksum uint8 } func ( *FrameDescriptor) () { .Flags.VersionSet(1) .Flags.BlockIndependenceSet(true) } func ( *FrameDescriptor) ( *Frame, io.Writer) error { if .Checksum > 0 { // Header already written. return nil } := .buf[:4] // Write the magic number here even though it belongs to the Frame. binary.LittleEndian.PutUint32(, .Magic) if !.isLegacy() { = [:4+2] binary.LittleEndian.PutUint16([4:], uint16(.Flags)) if .Flags.Size() { = [:4+2+8] binary.LittleEndian.PutUint64([4+2:], .ContentSize) } .Checksum = descriptorChecksum([4:]) = append(, .Checksum) } , := .Write() return } func ( *FrameDescriptor) ( *Frame, io.Reader) error { if .isLegacy() { .Flags = 0 // no descriptor to read: clear whatever the previous frame left return nil } // Read the flags and the checksum, hoping that there is not content size. := .buf[:3] if , := io.ReadFull(, ); != nil { return } := binary.LittleEndian.Uint16() .Flags = DescriptorFlags() // The optional fields follow the flags, before the checksum. := 0 if .Flags.Size() { += 8 } if .Flags.dictID() { += 4 } if > 0 { = [:3+] if , := io.ReadFull(, [3:]); != nil { return } } if .Flags.Size() { .ContentSize = binary.LittleEndian.Uint64([2:]) } .Checksum = [len()-1] // the checksum is the last byte = [:len()-1] // all descriptor fields except checksum if := descriptorChecksum(); .Checksum != { return fmt.Errorf("%w: got %x; expected %x", lz4errors.ErrInvalidHeaderChecksum, , .Checksum) } // Validate the elements that can be. if := .Flags.Version(); != 1 { return fmt.Errorf("%w: version %d", lz4errors.ErrInvalidFrameDescriptor, ) } if .Flags&descriptorReserved != 0 { return fmt.Errorf("%w: reserved bits %#04x set", lz4errors.ErrInvalidFrameDescriptor, uint16(.Flags&descriptorReserved)) } if .Flags.dictID() { // Frame dictionaries are not supported: decoding without one would // produce garbage. return fmt.Errorf("%w: dictionary ID %#x", lz4errors.ErrInvalidFrameDescriptor, binary.LittleEndian.Uint32([len()-4:])) } if := .Flags.BlockSizeIndex(); !.IsValid() { return lz4errors.ErrOptionInvalidBlockSize } return nil } // Bits of the FLG (low) and BD (high) descriptor bytes that the format // reserves, and the dictionary ID flag, which the generated DescriptorFlags // accessors leave out. const ( descriptorReserved DescriptorFlags = 1<<1 | 0xF<<8 | 1<<15 descriptorDictID DescriptorFlags = 1 << 0 ) func ( DescriptorFlags) () bool { return &descriptorDictID != 0 } func descriptorChecksum( []byte) byte { return byte(xxh32.ChecksumZero() >> 8) }