2016-02-22 14:44:12 -05:00
|
|
|
package lz4
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-04-10 09:41:22 -04:00
|
|
|
// ErrInvalidSourceShortBuffer is returned by UncompressBlock or CompressBLock when a compressed
|
|
|
|
// block is corrupted or the destination buffer is not large enough for the uncompressed data.
|
|
|
|
ErrInvalidSourceShortBuffer = errors.New("lz4: invalid source or destination buffer too short")
|
|
|
|
// ErrInvalid is returned when reading an invalid LZ4 archive.
|
|
|
|
ErrInvalid = errors.New("lz4: bad magic number")
|
2016-02-22 14:44:12 -05:00
|
|
|
)
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// blockHash hashes 4 bytes into a value < winSize.
|
|
|
|
func blockHash(x uint32) uint32 {
|
|
|
|
const hasher uint32 = 2654435761 // Knuth multiplicative hash.
|
|
|
|
return x * hasher >> hashShift
|
|
|
|
}
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
// CompressBlockBound returns the maximum size of a given buffer of size n, when not compressible.
|
|
|
|
func CompressBlockBound(n int) int {
|
|
|
|
return n + n/255 + 16
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// UncompressBlock uncompresses the source buffer into the destination one,
|
|
|
|
// and returns the uncompressed size.
|
2016-02-22 14:44:12 -05:00
|
|
|
//
|
|
|
|
// The destination buffer must be sized appropriately.
|
|
|
|
//
|
|
|
|
// An error is returned if the source data is invalid or the destination buffer is too small.
|
2019-04-10 09:41:22 -04:00
|
|
|
func UncompressBlock(src, dst []byte) (si int, err error) {
|
|
|
|
defer func() {
|
|
|
|
// It is now faster to let the runtime panic and recover on out of bound slice access
|
|
|
|
// than checking indices as we go along.
|
|
|
|
if recover() != nil {
|
|
|
|
err = ErrInvalidSourceShortBuffer
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
sn := len(src)
|
2016-02-22 14:44:12 -05:00
|
|
|
if sn == 0 {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
var di int
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
for {
|
2019-04-10 09:41:22 -04:00
|
|
|
// Literals and match lengths (token).
|
|
|
|
b := int(src[si])
|
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Literals.
|
|
|
|
if lLen := b >> 4; lLen > 0 {
|
2016-02-22 14:44:12 -05:00
|
|
|
if lLen == 0xF {
|
|
|
|
for src[si] == 0xFF {
|
|
|
|
lLen += 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
lLen += int(src[si])
|
2019-04-10 09:41:22 -04:00
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
i := si
|
|
|
|
si += lLen
|
|
|
|
di += copy(dst[di:], src[i:si])
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
if si >= sn {
|
|
|
|
return di, nil
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
si++
|
|
|
|
_ = src[si] // Bound check elimination.
|
|
|
|
offset := int(src[si-1]) | int(src[si])<<8
|
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Match.
|
|
|
|
mLen := b & 0xF
|
2016-02-22 14:44:12 -05:00
|
|
|
if mLen == 0xF {
|
|
|
|
for src[si] == 0xFF {
|
|
|
|
mLen += 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
mLen += int(src[si])
|
2019-04-10 09:41:22 -04:00
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
mLen += minMatch
|
|
|
|
|
|
|
|
// Copy the match.
|
|
|
|
i := di - offset
|
|
|
|
if offset > 0 && mLen >= offset {
|
|
|
|
// Efficiently copy the match dst[di-offset:di] into the dst slice.
|
|
|
|
bytesToCopy := offset * (mLen / offset)
|
|
|
|
expanded := dst[i:]
|
|
|
|
for n := offset; n <= bytesToCopy+offset; n *= 2 {
|
|
|
|
copy(expanded[n:], expanded[:n])
|
|
|
|
}
|
|
|
|
di += bytesToCopy
|
|
|
|
mLen -= bytesToCopy
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
di += copy(dst[di:], dst[i:i+mLen])
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// CompressBlock compresses the source buffer into the destination one.
|
2016-02-22 14:44:12 -05:00
|
|
|
// This is the fast version of LZ4 compression and also the default one.
|
2019-04-10 09:41:22 -04:00
|
|
|
// The size of hashTable must be at least 64Kb.
|
2016-02-22 14:44:12 -05:00
|
|
|
//
|
|
|
|
// The size of the compressed data is returned. If it is 0 and no error, then the data is incompressible.
|
|
|
|
//
|
|
|
|
// An error is returned if the destination buffer is too small.
|
2019-04-10 09:41:22 -04:00
|
|
|
func CompressBlock(src, dst []byte, hashTable []int) (di int, err error) {
|
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
|
|
|
err = ErrInvalidSourceShortBuffer
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
sn, dn := len(src)-mfLimit, len(dst)
|
2019-04-10 09:41:22 -04:00
|
|
|
if sn <= 0 || dn == 0 {
|
2016-02-22 14:44:12 -05:00
|
|
|
return 0, nil
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
var si int
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Fast scan strategy: the hash table only stores the last 4 bytes sequences.
|
|
|
|
// const accInit = 1 << skipStrength
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
anchor := si // Position of the current literals.
|
|
|
|
// acc := accInit // Variable step: improves performance on non-compressible data.
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
for si < sn {
|
|
|
|
// Hash the next 4 bytes (sequence)...
|
|
|
|
match := binary.LittleEndian.Uint32(src[si:])
|
|
|
|
h := blockHash(match)
|
|
|
|
|
|
|
|
ref := hashTable[h]
|
|
|
|
hashTable[h] = si
|
|
|
|
if ref >= sn { // Invalid reference (dirty hashtable).
|
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
offset := si - ref
|
2019-04-10 09:41:22 -04:00
|
|
|
if offset <= 0 || offset >= winSize || // Out of window.
|
|
|
|
match != binary.LittleEndian.Uint32(src[ref:]) { // Hash collision on different matches.
|
|
|
|
// si += acc >> skipStrength
|
|
|
|
// acc++
|
|
|
|
si++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match found.
|
|
|
|
// acc = accInit
|
|
|
|
lLen := si - anchor // Literal length.
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Encode match length part 1.
|
2016-02-22 14:44:12 -05:00
|
|
|
si += minMatch
|
2019-04-10 09:41:22 -04:00
|
|
|
mLen := si // Match length has minMatch already.
|
|
|
|
// Find the longest match, first looking by batches of 8 bytes.
|
|
|
|
for si < sn && binary.LittleEndian.Uint64(src[si:]) == binary.LittleEndian.Uint64(src[si-offset:]) {
|
|
|
|
si += 8
|
|
|
|
}
|
|
|
|
// Then byte by byte.
|
|
|
|
for si < sn && src[si] == src[si-offset] {
|
2016-02-22 14:44:12 -05:00
|
|
|
si++
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
mLen = si - mLen
|
|
|
|
if mLen < 0xF {
|
|
|
|
dst[di] = byte(mLen)
|
|
|
|
} else {
|
|
|
|
dst[di] = 0xF
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Encode literals length.
|
2016-02-22 14:44:12 -05:00
|
|
|
if lLen < 0xF {
|
|
|
|
dst[di] |= byte(lLen << 4)
|
|
|
|
} else {
|
|
|
|
dst[di] |= 0xF0
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
l := lLen - 0xF
|
|
|
|
for ; l >= 0xFF; l -= 0xFF {
|
|
|
|
dst[di] = 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
dst[di] = byte(l)
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Literals.
|
|
|
|
copy(dst[di:], src[anchor:anchor+lLen])
|
|
|
|
di += lLen + 2
|
2016-02-22 14:44:12 -05:00
|
|
|
anchor = si
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Encode offset.
|
|
|
|
_ = dst[di] // Bound check elimination.
|
2016-02-22 14:44:12 -05:00
|
|
|
dst[di-2], dst[di-1] = byte(offset), byte(offset>>8)
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Encode match length part 2.
|
2016-02-22 14:44:12 -05:00
|
|
|
if mLen >= 0xF {
|
|
|
|
for mLen -= 0xF; mLen >= 0xFF; mLen -= 0xFF {
|
|
|
|
dst[di] = 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
dst[di] = byte(mLen)
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if anchor == 0 {
|
2019-04-10 09:41:22 -04:00
|
|
|
// Incompressible.
|
2016-02-22 14:44:12 -05:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Last literals.
|
2016-02-22 14:44:12 -05:00
|
|
|
lLen := len(src) - anchor
|
|
|
|
if lLen < 0xF {
|
|
|
|
dst[di] = byte(lLen << 4)
|
|
|
|
} else {
|
|
|
|
dst[di] = 0xF0
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
|
|
|
for lLen -= 0xF; lLen >= 0xFF; lLen -= 0xFF {
|
2016-02-22 14:44:12 -05:00
|
|
|
dst[di] = 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
dst[di] = byte(lLen)
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Write the last literals.
|
|
|
|
if di >= anchor {
|
|
|
|
// Incompressible.
|
2016-02-22 14:44:12 -05:00
|
|
|
return 0, nil
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
di += copy(dst[di:], src[anchor:])
|
2016-02-22 14:44:12 -05:00
|
|
|
return di, nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// CompressBlockHC compresses the source buffer src into the destination dst
|
|
|
|
// with max search depth (use 0 or negative value for no max).
|
|
|
|
//
|
2016-02-22 14:44:12 -05:00
|
|
|
// CompressBlockHC compression ratio is better than CompressBlock but it is also slower.
|
|
|
|
//
|
|
|
|
// The size of the compressed data is returned. If it is 0 and no error, then the data is not compressible.
|
|
|
|
//
|
|
|
|
// An error is returned if the destination buffer is too small.
|
2019-04-10 09:41:22 -04:00
|
|
|
func CompressBlockHC(src, dst []byte, depth int) (di int, err error) {
|
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
|
|
|
err = ErrInvalidSourceShortBuffer
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-02-22 14:44:12 -05:00
|
|
|
sn, dn := len(src)-mfLimit, len(dst)
|
2019-04-10 09:41:22 -04:00
|
|
|
if sn <= 0 || dn == 0 {
|
2016-02-22 14:44:12 -05:00
|
|
|
return 0, nil
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
var si int
|
|
|
|
|
|
|
|
// hashTable: stores the last position found for a given hash
|
|
|
|
// chaingTable: stores previous positions for a given hash
|
|
|
|
var hashTable, chainTable [winSize]int
|
|
|
|
|
|
|
|
if depth <= 0 {
|
|
|
|
depth = winSize
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
anchor := si
|
2019-04-10 09:41:22 -04:00
|
|
|
for si < sn {
|
|
|
|
// Hash the next 4 bytes (sequence).
|
|
|
|
match := binary.LittleEndian.Uint32(src[si:])
|
|
|
|
h := blockHash(match)
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Follow the chain until out of window and give the longest match.
|
2016-02-22 14:44:12 -05:00
|
|
|
mLen := 0
|
|
|
|
offset := 0
|
2019-04-10 09:41:22 -04:00
|
|
|
for next, try := hashTable[h], depth; try > 0 && next > 0 && si-next < winSize; next = chainTable[next&winMask] {
|
|
|
|
// The first (mLen==0) or next byte (mLen>=minMatch) at current match length
|
|
|
|
// must match to improve on the match length.
|
|
|
|
if src[next+mLen] != src[si+mLen] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ml := 0
|
|
|
|
// Compare the current position with a previous with the same hash.
|
|
|
|
for ml < sn-si && binary.LittleEndian.Uint64(src[next+ml:]) == binary.LittleEndian.Uint64(src[si+ml:]) {
|
|
|
|
ml += 8
|
|
|
|
}
|
|
|
|
for ml < sn-si && src[next+ml] == src[si+ml] {
|
|
|
|
ml++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
if ml+1 < minMatch || ml <= mLen {
|
|
|
|
// Match too small (<minMath) or smaller than the current match.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Found a longer match, keep its position and length.
|
|
|
|
mLen = ml
|
|
|
|
offset = si - next
|
|
|
|
// Try another previous position with the same hash.
|
|
|
|
try--
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
chainTable[si&winMask] = hashTable[h]
|
2019-04-10 09:41:22 -04:00
|
|
|
hashTable[h] = si
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// No match found.
|
2016-02-22 14:44:12 -05:00
|
|
|
if mLen == 0 {
|
|
|
|
si++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Match found.
|
|
|
|
// Update hash/chain tables with overlapping bytes:
|
|
|
|
// si already hashed, add everything from si+1 up to the match length.
|
|
|
|
winStart := si + 1
|
|
|
|
if ws := si + mLen - winSize; ws > winStart {
|
|
|
|
winStart = ws
|
|
|
|
}
|
|
|
|
for si, ml := winStart, si+mLen; si < ml; {
|
|
|
|
match >>= 8
|
|
|
|
match |= uint32(src[si+3]) << 24
|
|
|
|
h := blockHash(match)
|
2016-02-22 14:44:12 -05:00
|
|
|
chainTable[si&winMask] = hashTable[h]
|
|
|
|
hashTable[h] = si
|
2019-04-10 09:41:22 -04:00
|
|
|
si++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
lLen := si - anchor
|
|
|
|
si += mLen
|
2019-04-10 09:41:22 -04:00
|
|
|
mLen -= minMatch // Match length does not include minMatch.
|
2016-02-22 14:44:12 -05:00
|
|
|
|
|
|
|
if mLen < 0xF {
|
|
|
|
dst[di] = byte(mLen)
|
|
|
|
} else {
|
|
|
|
dst[di] = 0xF
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Encode literals length.
|
2016-02-22 14:44:12 -05:00
|
|
|
if lLen < 0xF {
|
|
|
|
dst[di] |= byte(lLen << 4)
|
|
|
|
} else {
|
|
|
|
dst[di] |= 0xF0
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
l := lLen - 0xF
|
|
|
|
for ; l >= 0xFF; l -= 0xFF {
|
|
|
|
dst[di] = 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
dst[di] = byte(l)
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Literals.
|
|
|
|
copy(dst[di:], src[anchor:anchor+lLen])
|
|
|
|
di += lLen
|
2016-02-22 14:44:12 -05:00
|
|
|
anchor = si
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Encode offset.
|
|
|
|
di += 2
|
2016-02-22 14:44:12 -05:00
|
|
|
dst[di-2], dst[di-1] = byte(offset), byte(offset>>8)
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Encode match length part 2.
|
2016-02-22 14:44:12 -05:00
|
|
|
if mLen >= 0xF {
|
|
|
|
for mLen -= 0xF; mLen >= 0xFF; mLen -= 0xFF {
|
|
|
|
dst[di] = 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
dst[di] = byte(mLen)
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if anchor == 0 {
|
2019-04-10 09:41:22 -04:00
|
|
|
// Incompressible.
|
2016-02-22 14:44:12 -05:00
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Last literals.
|
2016-02-22 14:44:12 -05:00
|
|
|
lLen := len(src) - anchor
|
|
|
|
if lLen < 0xF {
|
|
|
|
dst[di] = byte(lLen << 4)
|
|
|
|
} else {
|
|
|
|
dst[di] = 0xF0
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
lLen -= 0xF
|
|
|
|
for ; lLen >= 0xFF; lLen -= 0xFF {
|
|
|
|
dst[di] = 0xFF
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
}
|
|
|
|
dst[di] = byte(lLen)
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
di++
|
2016-02-22 14:44:12 -05:00
|
|
|
|
2019-04-10 09:41:22 -04:00
|
|
|
// Write the last literals.
|
|
|
|
if di >= anchor {
|
|
|
|
// Incompressible.
|
2016-02-22 14:44:12 -05:00
|
|
|
return 0, nil
|
|
|
|
}
|
2019-04-10 09:41:22 -04:00
|
|
|
di += copy(dst[di:], src[anchor:])
|
2016-02-22 14:44:12 -05:00
|
|
|
return di, nil
|
|
|
|
}
|