2020-11-06 18:41:42 +00:00
|
|
|
// +build !amd64,!arm appengine !gc noasm
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2020-11-06 18:41:42 +00:00
|
|
|
package lz4block
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2021-02-28 23:08:33 +00:00
|
|
|
import "encoding/binary"
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func decodeBlock(dst, src []byte) (ret int) {
|
2021-02-28 23:08:33 +00:00
|
|
|
// Restrict capacities so we don't read or write out of bounds.
|
|
|
|
dst = dst[:len(dst):len(dst)]
|
|
|
|
src = src[:len(src):len(src)]
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
const hasError = -2
|
|
|
|
defer func() {
|
|
|
|
if recover() != nil {
|
|
|
|
ret = hasError
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-11-06 18:41:42 +00:00
|
|
|
var si, di uint
|
2020-10-16 05:06:27 +00:00
|
|
|
for {
|
|
|
|
// Literals and match lengths (token).
|
2020-11-06 18:41:42 +00:00
|
|
|
b := uint(src[si])
|
2020-10-16 05:06:27 +00:00
|
|
|
si++
|
|
|
|
|
|
|
|
// Literals.
|
|
|
|
if lLen := b >> 4; lLen > 0 {
|
|
|
|
switch {
|
2020-11-06 18:41:42 +00:00
|
|
|
case lLen < 0xF && si+16 < uint(len(src)):
|
2020-10-16 05:06:27 +00:00
|
|
|
// Shortcut 1
|
|
|
|
// if we have enough room in src and dst, and the literals length
|
|
|
|
// is small enough (0..14) then copy all 16 bytes, even if not all
|
|
|
|
// are part of the literals.
|
|
|
|
copy(dst[di:], src[si:si+16])
|
|
|
|
si += lLen
|
|
|
|
di += lLen
|
|
|
|
if mLen := b & 0xF; mLen < 0xF {
|
|
|
|
// Shortcut 2
|
|
|
|
// if the match length (4..18) fits within the literals, then copy
|
|
|
|
// all 18 bytes, even if not all are part of the literals.
|
|
|
|
mLen += 4
|
2021-02-28 23:08:33 +00:00
|
|
|
if offset := u16(src[si:]); mLen <= offset {
|
2020-10-16 05:06:27 +00:00
|
|
|
i := di - offset
|
|
|
|
end := i + 18
|
2020-11-06 18:41:42 +00:00
|
|
|
if end > uint(len(dst)) {
|
2020-10-16 05:06:27 +00:00
|
|
|
// The remaining buffer may not hold 18 bytes.
|
|
|
|
// See https://github.com/pierrec/lz4/issues/51.
|
2020-11-06 18:41:42 +00:00
|
|
|
end = uint(len(dst))
|
2020-10-16 05:06:27 +00:00
|
|
|
}
|
|
|
|
copy(dst[di:], dst[i:end])
|
|
|
|
si += 2
|
|
|
|
di += mLen
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case lLen == 0xF:
|
|
|
|
for src[si] == 0xFF {
|
|
|
|
lLen += 0xFF
|
|
|
|
si++
|
|
|
|
}
|
2020-11-06 18:41:42 +00:00
|
|
|
lLen += uint(src[si])
|
2020-10-16 05:06:27 +00:00
|
|
|
si++
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
copy(dst[di:di+lLen], src[si:si+lLen])
|
|
|
|
si += lLen
|
|
|
|
di += lLen
|
|
|
|
}
|
|
|
|
}
|
2020-11-06 18:41:42 +00:00
|
|
|
if si == uint(len(src)) {
|
|
|
|
return int(di)
|
|
|
|
} else if si > uint(len(src)) {
|
|
|
|
return hasError
|
2020-10-16 05:06:27 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 23:08:33 +00:00
|
|
|
offset := u16(src[si:])
|
2020-10-16 05:06:27 +00:00
|
|
|
if offset == 0 {
|
|
|
|
return hasError
|
|
|
|
}
|
|
|
|
si += 2
|
|
|
|
|
|
|
|
// Match.
|
|
|
|
mLen := b & 0xF
|
|
|
|
if mLen == 0xF {
|
|
|
|
for src[si] == 0xFF {
|
|
|
|
mLen += 0xFF
|
|
|
|
si++
|
|
|
|
}
|
2020-11-06 18:41:42 +00:00
|
|
|
mLen += uint(src[si])
|
2020-10-16 05:06:27 +00:00
|
|
|
si++
|
|
|
|
}
|
|
|
|
mLen += minMatch
|
|
|
|
|
|
|
|
// Copy the match.
|
|
|
|
expanded := dst[di-offset:]
|
|
|
|
if mLen > offset {
|
|
|
|
// Efficiently copy the match dst[di-offset:di] into the dst slice.
|
|
|
|
bytesToCopy := offset * (mLen / offset)
|
|
|
|
for n := offset; n <= bytesToCopy+offset; n *= 2 {
|
|
|
|
copy(expanded[n:], expanded[:n])
|
|
|
|
}
|
|
|
|
di += bytesToCopy
|
|
|
|
mLen -= bytesToCopy
|
|
|
|
}
|
2020-11-06 18:41:42 +00:00
|
|
|
di += uint(copy(dst[di:di+mLen], expanded[:mLen]))
|
2020-10-16 05:06:27 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-28 23:08:33 +00:00
|
|
|
|
|
|
|
func u16(p []byte) uint { return uint(binary.LittleEndian.Uint16(p)) }
|