1
0
Fork 0
forked from forgejo/forgejo
This commit is contained in:
techknowlogick 2021-02-28 18:08:33 -05:00 committed by GitHub
parent 030646eea4
commit 47f6a4ec3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
947 changed files with 26119 additions and 7062 deletions

View file

@ -140,25 +140,22 @@ readMatchlenLoop:
BEQ readMatchlenLoop
readMatchlenDone:
ADD minMatch, len
// Bounds check dst+len and match = dst-offset.
// Bounds check dst+len+minMatch and match = dst-offset.
ADD dst, len, tmp1
ADD minMatch, tmp1
CMP dstend, tmp1
//BHI shortDst // Uncomment for distinct error codes.
SUB offset, dst, match
CMP.LS match, dstorig
BHI corrupt
// If the offset is at least four (len is, because of minMatch),
// do a four-way unrolled byte copy loop. Using MOVD instead of four
// byte loads is much faster, but to remain portable we'd have to
// align match first, which in turn is too expensive.
CMP $4, offset
BLO copyMatch
SUB $4, len
// Since len+minMatch is at least four, we can do a 4× unrolled
// byte copy loop. Using MOVW instead of four byte loads is faster,
// but to remain portable we'd have to align match first, which is
// too expensive. By alternating loads and stores, we also handle
// the case offset < 4.
copyMatch4:
SUB.S $4, len
MOVBU.P 4(match), tmp1
MOVB.P tmp1, 4(dst)
MOVBU -3(match), tmp2
@ -167,7 +164,6 @@ copyMatch4:
MOVB tmp3, -2(dst)
MOVBU -1(match), tmp1
MOVB tmp1, -1(dst)
SUB.S $4, len
BPL copyMatch4
// Restore len, which is now negative.
@ -175,7 +171,7 @@ copyMatch4:
BEQ copyMatchDone
copyMatch:
// Simple byte-at-a-time copy.
// Finish with a byte-at-a-time copy.
SUB.S $1, len
MOVBU.P 1(match), tmp2
MOVB.P tmp2, 1(dst)

View file

@ -2,7 +2,13 @@
package lz4block
import "encoding/binary"
func decodeBlock(dst, src []byte) (ret int) {
// Restrict capacities so we don't read or write out of bounds.
dst = dst[:len(dst):len(dst)]
src = src[:len(src):len(src)]
const hasError = -2
defer func() {
if recover() != nil {
@ -32,7 +38,7 @@ func decodeBlock(dst, src []byte) (ret int) {
// 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 {
if offset := u16(src[si:]); mLen <= offset {
i := di - offset
end := i + 18
if end > uint(len(dst)) {
@ -66,7 +72,7 @@ func decodeBlock(dst, src []byte) (ret int) {
return hasError
}
offset := uint(src[si]) | uint(src[si+1])<<8
offset := u16(src[si:])
if offset == 0 {
return hasError
}
@ -98,3 +104,5 @@ func decodeBlock(dst, src []byte) (ret int) {
di += uint(copy(dst[di:di+mLen], expanded[:mLen]))
}
}
func u16(p []byte) uint { return uint(binary.LittleEndian.Uint16(p)) }