30ce3731a1
* denisenkom/go-mssqldb untagged -> v0.9.0 * github.com/editorconfig/editorconfig-core-go v2.3.7 -> v2.3.8 * github.com/go-testfixtures/testfixtures v3.4.0 -> v3.4.1 * github.com/mholt/archiver v3.3.2 -> v3.5.0 * github.com/olivere/elastic v7.0.20 -> v7.0.21 * github.com/urfave/cli v1.22.4 -> v1.22.5 * github.com/xanzy/go-gitlab v0.38.1 -> v0.39.0 * github.com/yuin/goldmark-meta untagged -> v1.0.0 * github.com/ethantkoenig/rupture 0a76f03a811a -> c3b3b810dc77 * github.com/jaytaylor/html2text 8fb95d837f7d -> 3577fbdbcff7 * github.com/kballard/go-shellquote cd60e84ee657 -> 95032a82bc51 * github.com/msteinert/pam 02ccfbfaf0cc -> 913b8f8cdf8b * github.com/unknwon/paginater 7748a72e0141 -> 042474bd0eae * CI.restart() Co-authored-by: techknowlogick <techknowlogick@gitea.io>
100 lines
2.2 KiB
Go
Vendored
100 lines
2.2 KiB
Go
Vendored
// +build !amd64,!arm appengine !gc noasm
|
|
|
|
package lz4block
|
|
|
|
func decodeBlock(dst, src []byte) (ret int) {
|
|
const hasError = -2
|
|
defer func() {
|
|
if recover() != nil {
|
|
ret = hasError
|
|
}
|
|
}()
|
|
|
|
var si, di uint
|
|
for {
|
|
// Literals and match lengths (token).
|
|
b := uint(src[si])
|
|
si++
|
|
|
|
// Literals.
|
|
if lLen := b >> 4; lLen > 0 {
|
|
switch {
|
|
case lLen < 0xF && si+16 < uint(len(src)):
|
|
// 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
|
|
if offset := uint(src[si]) | uint(src[si+1])<<8; mLen <= offset {
|
|
i := di - offset
|
|
end := i + 18
|
|
if end > uint(len(dst)) {
|
|
// The remaining buffer may not hold 18 bytes.
|
|
// See https://github.com/pierrec/lz4/issues/51.
|
|
end = uint(len(dst))
|
|
}
|
|
copy(dst[di:], dst[i:end])
|
|
si += 2
|
|
di += mLen
|
|
continue
|
|
}
|
|
}
|
|
case lLen == 0xF:
|
|
for src[si] == 0xFF {
|
|
lLen += 0xFF
|
|
si++
|
|
}
|
|
lLen += uint(src[si])
|
|
si++
|
|
fallthrough
|
|
default:
|
|
copy(dst[di:di+lLen], src[si:si+lLen])
|
|
si += lLen
|
|
di += lLen
|
|
}
|
|
}
|
|
if si == uint(len(src)) {
|
|
return int(di)
|
|
} else if si > uint(len(src)) {
|
|
return hasError
|
|
}
|
|
|
|
offset := uint(src[si]) | uint(src[si+1])<<8
|
|
if offset == 0 {
|
|
return hasError
|
|
}
|
|
si += 2
|
|
|
|
// Match.
|
|
mLen := b & 0xF
|
|
if mLen == 0xF {
|
|
for src[si] == 0xFF {
|
|
mLen += 0xFF
|
|
si++
|
|
}
|
|
mLen += uint(src[si])
|
|
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
|
|
}
|
|
di += uint(copy(dst[di:di+mLen], expanded[:mLen]))
|
|
}
|
|
}
|