khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1 | package lz4 |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | ) |
| 8 | |
| 9 | // ReaderLegacy implements the LZ4Demo frame decoder. |
| 10 | // The Header is set after the first call to Read(). |
| 11 | type ReaderLegacy struct { |
| 12 | Header |
| 13 | // Handler called when a block has been successfully read. |
| 14 | // It provides the number of bytes read. |
| 15 | OnBlockDone func(size int) |
| 16 | |
| 17 | lastBlock bool |
| 18 | buf [8]byte // Scrap buffer. |
| 19 | pos int64 // Current position in src. |
| 20 | src io.Reader // Source. |
| 21 | zdata []byte // Compressed data. |
| 22 | data []byte // Uncompressed data. |
| 23 | idx int // Index of unread bytes into data. |
| 24 | skip int64 // Bytes to skip before next read. |
| 25 | dpos int64 // Position in dest |
| 26 | } |
| 27 | |
| 28 | // NewReaderLegacy returns a new LZ4Demo frame decoder. |
| 29 | // No access to the underlying io.Reader is performed. |
| 30 | func NewReaderLegacy(src io.Reader) *ReaderLegacy { |
| 31 | r := &ReaderLegacy{src: src} |
| 32 | return r |
| 33 | } |
| 34 | |
| 35 | // readHeader checks the frame magic number and parses the frame descriptoz. |
| 36 | // Skippable frames are supported even as a first frame although the LZ4 |
| 37 | // specifications recommends skippable frames not to be used as first frames. |
| 38 | func (z *ReaderLegacy) readLegacyHeader() error { |
| 39 | z.lastBlock = false |
| 40 | magic, err := z.readUint32() |
| 41 | if err != nil { |
| 42 | z.pos += 4 |
| 43 | if err == io.ErrUnexpectedEOF { |
| 44 | return io.EOF |
| 45 | } |
| 46 | return err |
| 47 | } |
| 48 | if magic != frameMagicLegacy { |
| 49 | return ErrInvalid |
| 50 | } |
| 51 | z.pos += 4 |
| 52 | |
| 53 | // Legacy has fixed 8MB blocksizes |
| 54 | // https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md#legacy-frame |
| 55 | bSize := blockSize4M * 2 |
| 56 | |
| 57 | // Allocate the compressed/uncompressed buffers. |
| 58 | // The compressed buffer cannot exceed the uncompressed one. |
| 59 | if n := 2 * bSize; cap(z.zdata) < n { |
| 60 | z.zdata = make([]byte, n, n) |
| 61 | } |
| 62 | if debugFlag { |
| 63 | debug("header block max size size=%d", bSize) |
| 64 | } |
| 65 | z.zdata = z.zdata[:bSize] |
| 66 | z.data = z.zdata[:cap(z.zdata)][bSize:] |
| 67 | z.idx = len(z.data) |
| 68 | |
| 69 | z.Header.done = true |
| 70 | if debugFlag { |
| 71 | debug("header read: %v", z.Header) |
| 72 | } |
| 73 | |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | // Read decompresses data from the underlying source into the supplied buffer. |
| 78 | // |
| 79 | // Since there can be multiple streams concatenated, Header values may |
| 80 | // change between calls to Read(). If that is the case, no data is actually read from |
| 81 | // the underlying io.Reader, to allow for potential input buffer resizing. |
| 82 | func (z *ReaderLegacy) Read(buf []byte) (int, error) { |
| 83 | if debugFlag { |
| 84 | debug("Read buf len=%d", len(buf)) |
| 85 | } |
| 86 | if !z.Header.done { |
| 87 | if err := z.readLegacyHeader(); err != nil { |
| 88 | return 0, err |
| 89 | } |
| 90 | if debugFlag { |
| 91 | debug("header read OK compressed buffer %d / %d uncompressed buffer %d : %d index=%d", |
| 92 | len(z.zdata), cap(z.zdata), len(z.data), cap(z.data), z.idx) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if len(buf) == 0 { |
| 97 | return 0, nil |
| 98 | } |
| 99 | |
| 100 | if z.idx == len(z.data) { |
| 101 | // No data ready for reading, process the next block. |
| 102 | if debugFlag { |
| 103 | debug(" reading block from writer %d %d", z.idx, blockSize4M*2) |
| 104 | } |
| 105 | |
| 106 | // Reset uncompressed buffer |
| 107 | z.data = z.zdata[:cap(z.zdata)][len(z.zdata):] |
| 108 | |
| 109 | bLen, err := z.readUint32() |
| 110 | if err != nil { |
| 111 | return 0, err |
| 112 | } |
| 113 | if debugFlag { |
| 114 | debug(" bLen %d (0x%x) offset = %d (0x%x)", bLen, bLen, z.pos, z.pos) |
| 115 | } |
| 116 | z.pos += 4 |
| 117 | |
| 118 | // Legacy blocks are always compressed, even when detrimental |
| 119 | if debugFlag { |
| 120 | debug(" compressed block size %d", bLen) |
| 121 | } |
| 122 | |
| 123 | if int(bLen) > cap(z.data) { |
| 124 | return 0, fmt.Errorf("lz4: invalid block size: %d", bLen) |
| 125 | } |
| 126 | zdata := z.zdata[:bLen] |
| 127 | if _, err := io.ReadFull(z.src, zdata); err != nil { |
| 128 | return 0, err |
| 129 | } |
| 130 | z.pos += int64(bLen) |
| 131 | |
| 132 | n, err := UncompressBlock(zdata, z.data) |
| 133 | if err != nil { |
| 134 | return 0, err |
| 135 | } |
| 136 | |
| 137 | z.data = z.data[:n] |
| 138 | if z.OnBlockDone != nil { |
| 139 | z.OnBlockDone(n) |
| 140 | } |
| 141 | |
| 142 | z.idx = 0 |
| 143 | |
| 144 | // Legacy blocks are fixed to 8MB, if we read a decompressed block smaller than this |
| 145 | // it means we've reached the end... |
| 146 | if n < blockSize4M*2 { |
| 147 | z.lastBlock = true |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | if z.skip > int64(len(z.data[z.idx:])) { |
| 152 | z.skip -= int64(len(z.data[z.idx:])) |
| 153 | z.dpos += int64(len(z.data[z.idx:])) |
| 154 | z.idx = len(z.data) |
| 155 | return 0, nil |
| 156 | } |
| 157 | |
| 158 | z.idx += int(z.skip) |
| 159 | z.dpos += z.skip |
| 160 | z.skip = 0 |
| 161 | |
| 162 | n := copy(buf, z.data[z.idx:]) |
| 163 | z.idx += n |
| 164 | z.dpos += int64(n) |
| 165 | if debugFlag { |
| 166 | debug("%v] copied %d bytes to input (%d:%d)", z.lastBlock, n, z.idx, len(z.data)) |
| 167 | } |
| 168 | if z.lastBlock && len(z.data) == z.idx { |
| 169 | return n, io.EOF |
| 170 | } |
| 171 | return n, nil |
| 172 | } |
| 173 | |
| 174 | // Seek implements io.Seeker, but supports seeking forward from the current |
| 175 | // position only. Any other seek will return an error. Allows skipping output |
| 176 | // bytes which aren't needed, which in some scenarios is faster than reading |
| 177 | // and discarding them. |
| 178 | // Note this may cause future calls to Read() to read 0 bytes if all of the |
| 179 | // data they would have returned is skipped. |
| 180 | func (z *ReaderLegacy) Seek(offset int64, whence int) (int64, error) { |
| 181 | if offset < 0 || whence != io.SeekCurrent { |
| 182 | return z.dpos + z.skip, ErrUnsupportedSeek |
| 183 | } |
| 184 | z.skip += offset |
| 185 | return z.dpos + z.skip, nil |
| 186 | } |
| 187 | |
| 188 | // Reset discards the Reader's state and makes it equivalent to the |
| 189 | // result of its original state from NewReader, but reading from r instead. |
| 190 | // This permits reusing a Reader rather than allocating a new one. |
| 191 | func (z *ReaderLegacy) Reset(r io.Reader) { |
| 192 | z.Header = Header{} |
| 193 | z.pos = 0 |
| 194 | z.src = r |
| 195 | z.zdata = z.zdata[:0] |
| 196 | z.data = z.data[:0] |
| 197 | z.idx = 0 |
| 198 | } |
| 199 | |
| 200 | // readUint32 reads an uint32 into the supplied buffer. |
| 201 | // The idea is to make use of the already allocated buffers avoiding additional allocations. |
| 202 | func (z *ReaderLegacy) readUint32() (uint32, error) { |
| 203 | buf := z.buf[:4] |
| 204 | _, err := io.ReadFull(z.src, buf) |
| 205 | x := binary.LittleEndian.Uint32(buf) |
| 206 | return x, err |
| 207 | } |