mirror of
https://github.com/containers/skopeo.git
synced 2025-09-22 18:37:21 +00:00
vendor github.com/containers/image/v5@v5.5.1
Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
784
vendor/github.com/klauspost/compress/huff0/decompress.go
generated
vendored
784
vendor/github.com/klauspost/compress/huff0/decompress.go
generated
vendored
@@ -25,6 +25,9 @@ type dEntryDouble struct {
|
||||
len uint8
|
||||
}
|
||||
|
||||
// Uses special code for all tables that are < 8 bits.
|
||||
const use8BitTables = true
|
||||
|
||||
// ReadTable will read a table from the input.
|
||||
// The size of the input may be larger than the table definition.
|
||||
// Any content remaining after the table definition will be returned.
|
||||
@@ -83,6 +86,7 @@ func ReadTable(in []byte, s *Scratch) (s2 *Scratch, remain []byte, err error) {
|
||||
}
|
||||
v2 := v & 15
|
||||
rankStats[v2]++
|
||||
// (1 << (v2-1)) is slower since the compiler cannot prove that v2 isn't 0.
|
||||
weightTotal += (1 << v2) >> 1
|
||||
}
|
||||
if weightTotal == 0 {
|
||||
@@ -142,12 +146,14 @@ func ReadTable(in []byte, s *Scratch) (s2 *Scratch, remain []byte, err error) {
|
||||
d := dEntrySingle{
|
||||
entry: uint16(s.actualTableLog+1-w) | (uint16(n) << 8),
|
||||
}
|
||||
single := s.dt.single[rankStats[w] : rankStats[w]+length]
|
||||
rank := &rankStats[w]
|
||||
single := s.dt.single[*rank : *rank+length]
|
||||
for i := range single {
|
||||
single[i] = d
|
||||
}
|
||||
rankStats[w] += length
|
||||
*rank += length
|
||||
}
|
||||
|
||||
return s, in, nil
|
||||
}
|
||||
|
||||
@@ -208,7 +214,10 @@ func (d *Decoder) Decompress1X(dst, src []byte) ([]byte, error) {
|
||||
if len(d.dt.single) == 0 {
|
||||
return nil, errors.New("no table loaded")
|
||||
}
|
||||
var br bitReader
|
||||
if use8BitTables && d.actualTableLog <= 8 {
|
||||
return d.decompress1X8Bit(dst, src)
|
||||
}
|
||||
var br bitReaderShifted
|
||||
err := br.init(src)
|
||||
if err != nil {
|
||||
return dst, err
|
||||
@@ -216,17 +225,6 @@ func (d *Decoder) Decompress1X(dst, src []byte) ([]byte, error) {
|
||||
maxDecodedSize := cap(dst)
|
||||
dst = dst[:0]
|
||||
|
||||
decode := func() byte {
|
||||
val := br.peekBitsFast(d.actualTableLog) /* note : actualTableLog >= 1 */
|
||||
v := d.dt.single[val]
|
||||
br.bitsRead += uint8(v.entry)
|
||||
return uint8(v.entry >> 8)
|
||||
}
|
||||
hasDec := func(v dEntrySingle) byte {
|
||||
br.bitsRead += uint8(v.entry)
|
||||
return uint8(v.entry >> 8)
|
||||
}
|
||||
|
||||
// Avoid bounds check by always having full sized table.
|
||||
const tlSize = 1 << tableLogMax
|
||||
const tlMask = tlSize - 1
|
||||
@@ -238,11 +236,25 @@ func (d *Decoder) Decompress1X(dst, src []byte) ([]byte, error) {
|
||||
|
||||
for br.off >= 8 {
|
||||
br.fillFast()
|
||||
buf[off+0] = hasDec(dt[br.peekBitsFast(d.actualTableLog)&tlMask])
|
||||
buf[off+1] = hasDec(dt[br.peekBitsFast(d.actualTableLog)&tlMask])
|
||||
v := dt[br.peekBitsFast(d.actualTableLog)&tlMask]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+0] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekBitsFast(d.actualTableLog)&tlMask]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+1] = uint8(v.entry >> 8)
|
||||
|
||||
// Refill
|
||||
br.fillFast()
|
||||
buf[off+2] = hasDec(dt[br.peekBitsFast(d.actualTableLog)&tlMask])
|
||||
buf[off+3] = hasDec(dt[br.peekBitsFast(d.actualTableLog)&tlMask])
|
||||
|
||||
v = dt[br.peekBitsFast(d.actualTableLog)&tlMask]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+2] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekBitsFast(d.actualTableLog)&tlMask]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+3] = uint8(v.entry >> 8)
|
||||
|
||||
off += 4
|
||||
if off == 0 {
|
||||
if len(dst)+256 > maxDecodedSize {
|
||||
@@ -259,13 +271,196 @@ func (d *Decoder) Decompress1X(dst, src []byte) ([]byte, error) {
|
||||
}
|
||||
dst = append(dst, buf[:off]...)
|
||||
|
||||
for !br.finished() {
|
||||
// br < 8, so uint8 is fine
|
||||
bitsLeft := uint8(br.off)*8 + 64 - br.bitsRead
|
||||
for bitsLeft > 0 {
|
||||
br.fill()
|
||||
if false && br.bitsRead >= 32 {
|
||||
if br.off >= 4 {
|
||||
v := br.in[br.off-4:]
|
||||
v = v[:4]
|
||||
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
|
||||
br.value = (br.value << 32) | uint64(low)
|
||||
br.bitsRead -= 32
|
||||
br.off -= 4
|
||||
} else {
|
||||
for br.off > 0 {
|
||||
br.value = (br.value << 8) | uint64(br.in[br.off-1])
|
||||
br.bitsRead -= 8
|
||||
br.off--
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(dst) >= maxDecodedSize {
|
||||
br.close()
|
||||
return nil, ErrMaxDecodedSizeExceeded
|
||||
}
|
||||
dst = append(dst, decode())
|
||||
v := d.dt.single[br.peekBitsFast(d.actualTableLog)&tlMask]
|
||||
nBits := uint8(v.entry)
|
||||
br.advance(nBits)
|
||||
bitsLeft -= nBits
|
||||
dst = append(dst, uint8(v.entry>>8))
|
||||
}
|
||||
return dst, br.close()
|
||||
}
|
||||
|
||||
// decompress1X8Bit will decompress a 1X encoded stream with tablelog <= 8.
|
||||
// The cap of the output buffer will be the maximum decompressed size.
|
||||
// The length of the supplied input must match the end of a block exactly.
|
||||
func (d *Decoder) decompress1X8Bit(dst, src []byte) ([]byte, error) {
|
||||
if d.actualTableLog == 8 {
|
||||
return d.decompress1X8BitExactly(dst, src)
|
||||
}
|
||||
var br bitReaderBytes
|
||||
err := br.init(src)
|
||||
if err != nil {
|
||||
return dst, err
|
||||
}
|
||||
maxDecodedSize := cap(dst)
|
||||
dst = dst[:0]
|
||||
|
||||
// Avoid bounds check by always having full sized table.
|
||||
dt := d.dt.single[:256]
|
||||
|
||||
// Use temp table to avoid bound checks/append penalty.
|
||||
var buf [256]byte
|
||||
var off uint8
|
||||
|
||||
shift := (8 - d.actualTableLog) & 7
|
||||
|
||||
//fmt.Printf("mask: %b, tl:%d\n", mask, d.actualTableLog)
|
||||
for br.off >= 4 {
|
||||
br.fillFast()
|
||||
v := dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+0] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+1] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+2] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+3] = uint8(v.entry >> 8)
|
||||
|
||||
off += 4
|
||||
if off == 0 {
|
||||
if len(dst)+256 > maxDecodedSize {
|
||||
br.close()
|
||||
return nil, ErrMaxDecodedSizeExceeded
|
||||
}
|
||||
dst = append(dst, buf[:]...)
|
||||
}
|
||||
}
|
||||
|
||||
if len(dst)+int(off) > maxDecodedSize {
|
||||
br.close()
|
||||
return nil, ErrMaxDecodedSizeExceeded
|
||||
}
|
||||
dst = append(dst, buf[:off]...)
|
||||
|
||||
// br < 4, so uint8 is fine
|
||||
bitsLeft := int8(uint8(br.off)*8 + (64 - br.bitsRead))
|
||||
for bitsLeft > 0 {
|
||||
if br.bitsRead >= 64-8 {
|
||||
for br.off > 0 {
|
||||
br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
|
||||
br.bitsRead -= 8
|
||||
br.off--
|
||||
}
|
||||
}
|
||||
if len(dst) >= maxDecodedSize {
|
||||
br.close()
|
||||
return nil, ErrMaxDecodedSizeExceeded
|
||||
}
|
||||
v := dt[br.peekByteFast()>>shift]
|
||||
nBits := uint8(v.entry)
|
||||
br.advance(nBits)
|
||||
bitsLeft -= int8(nBits)
|
||||
dst = append(dst, uint8(v.entry>>8))
|
||||
}
|
||||
return dst, br.close()
|
||||
}
|
||||
|
||||
// decompress1X8Bit will decompress a 1X encoded stream with tablelog <= 8.
|
||||
// The cap of the output buffer will be the maximum decompressed size.
|
||||
// The length of the supplied input must match the end of a block exactly.
|
||||
func (d *Decoder) decompress1X8BitExactly(dst, src []byte) ([]byte, error) {
|
||||
var br bitReaderBytes
|
||||
err := br.init(src)
|
||||
if err != nil {
|
||||
return dst, err
|
||||
}
|
||||
maxDecodedSize := cap(dst)
|
||||
dst = dst[:0]
|
||||
|
||||
// Avoid bounds check by always having full sized table.
|
||||
dt := d.dt.single[:256]
|
||||
|
||||
// Use temp table to avoid bound checks/append penalty.
|
||||
var buf [256]byte
|
||||
var off uint8
|
||||
|
||||
const shift = 0
|
||||
|
||||
//fmt.Printf("mask: %b, tl:%d\n", mask, d.actualTableLog)
|
||||
for br.off >= 4 {
|
||||
br.fillFast()
|
||||
v := dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+0] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+1] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+2] = uint8(v.entry >> 8)
|
||||
|
||||
v = dt[br.peekByteFast()>>shift]
|
||||
br.advance(uint8(v.entry))
|
||||
buf[off+3] = uint8(v.entry >> 8)
|
||||
|
||||
off += 4
|
||||
if off == 0 {
|
||||
if len(dst)+256 > maxDecodedSize {
|
||||
br.close()
|
||||
return nil, ErrMaxDecodedSizeExceeded
|
||||
}
|
||||
dst = append(dst, buf[:]...)
|
||||
}
|
||||
}
|
||||
|
||||
if len(dst)+int(off) > maxDecodedSize {
|
||||
br.close()
|
||||
return nil, ErrMaxDecodedSizeExceeded
|
||||
}
|
||||
dst = append(dst, buf[:off]...)
|
||||
|
||||
// br < 4, so uint8 is fine
|
||||
bitsLeft := int8(uint8(br.off)*8 + (64 - br.bitsRead))
|
||||
for bitsLeft > 0 {
|
||||
if br.bitsRead >= 64-8 {
|
||||
for br.off > 0 {
|
||||
br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
|
||||
br.bitsRead -= 8
|
||||
br.off--
|
||||
}
|
||||
}
|
||||
if len(dst) >= maxDecodedSize {
|
||||
br.close()
|
||||
return nil, ErrMaxDecodedSizeExceeded
|
||||
}
|
||||
v := dt[br.peekByteFast()>>shift]
|
||||
nBits := uint8(v.entry)
|
||||
br.advance(nBits)
|
||||
bitsLeft -= int8(nBits)
|
||||
dst = append(dst, uint8(v.entry>>8))
|
||||
}
|
||||
return dst, br.close()
|
||||
}
|
||||
@@ -274,15 +469,18 @@ func (d *Decoder) Decompress1X(dst, src []byte) ([]byte, error) {
|
||||
// The length of the supplied input must match the end of a block exactly.
|
||||
// The *capacity* of the dst slice must match the destination size of
|
||||
// the uncompressed data exactly.
|
||||
func (s *Decoder) Decompress4X(dst, src []byte) ([]byte, error) {
|
||||
if len(s.dt.single) == 0 {
|
||||
func (d *Decoder) Decompress4X(dst, src []byte) ([]byte, error) {
|
||||
if len(d.dt.single) == 0 {
|
||||
return nil, errors.New("no table loaded")
|
||||
}
|
||||
if len(src) < 6+(4*1) {
|
||||
return nil, errors.New("input too small")
|
||||
}
|
||||
if use8BitTables && d.actualTableLog <= 8 {
|
||||
return d.decompress4X8bit(dst, src)
|
||||
}
|
||||
|
||||
var br [4]bitReader
|
||||
var br [4]bitReaderShifted
|
||||
start := 6
|
||||
for i := 0; i < 3; i++ {
|
||||
length := int(src[i*2]) | (int(src[i*2+1]) << 8)
|
||||
@@ -308,14 +506,7 @@ func (s *Decoder) Decompress4X(dst, src []byte) ([]byte, error) {
|
||||
|
||||
const tlSize = 1 << tableLogMax
|
||||
const tlMask = tlSize - 1
|
||||
single := s.dt.single[:tlSize]
|
||||
|
||||
decode := func(br *bitReader) byte {
|
||||
val := br.peekBitsFast(s.actualTableLog) /* note : actualTableLog >= 1 */
|
||||
v := single[val&tlMask]
|
||||
br.bitsRead += uint8(v.entry)
|
||||
return uint8(v.entry >> 8)
|
||||
}
|
||||
single := d.dt.single[:tlSize]
|
||||
|
||||
// Use temp table to avoid bound checks/append penalty.
|
||||
var buf [256]byte
|
||||
@@ -324,66 +515,63 @@ func (s *Decoder) Decompress4X(dst, src []byte) ([]byte, error) {
|
||||
|
||||
// Decode 2 values from each decoder/loop.
|
||||
const bufoff = 256 / 4
|
||||
bigloop:
|
||||
for {
|
||||
for i := range br {
|
||||
br := &br[i]
|
||||
if br.off < 4 {
|
||||
break bigloop
|
||||
}
|
||||
br.fillFast()
|
||||
if br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4 {
|
||||
break
|
||||
}
|
||||
|
||||
{
|
||||
const stream = 0
|
||||
val := br[stream].peekBitsFast(s.actualTableLog)
|
||||
const stream2 = 1
|
||||
br[stream].fillFast()
|
||||
br[stream2].fillFast()
|
||||
|
||||
val := br[stream].peekBitsFast(d.actualTableLog)
|
||||
v := single[val&tlMask]
|
||||
br[stream].bitsRead += uint8(v.entry)
|
||||
|
||||
val2 := br[stream].peekBitsFast(s.actualTableLog)
|
||||
v2 := single[val2&tlMask]
|
||||
buf[off+bufoff*stream+1] = uint8(v2.entry >> 8)
|
||||
br[stream].advance(uint8(v.entry))
|
||||
buf[off+bufoff*stream] = uint8(v.entry >> 8)
|
||||
br[stream].bitsRead += uint8(v2.entry)
|
||||
}
|
||||
|
||||
{
|
||||
const stream = 1
|
||||
val := br[stream].peekBitsFast(s.actualTableLog)
|
||||
v := single[val&tlMask]
|
||||
br[stream].bitsRead += uint8(v.entry)
|
||||
|
||||
val2 := br[stream].peekBitsFast(s.actualTableLog)
|
||||
val2 := br[stream2].peekBitsFast(d.actualTableLog)
|
||||
v2 := single[val2&tlMask]
|
||||
buf[off+bufoff*stream+1] = uint8(v2.entry >> 8)
|
||||
buf[off+bufoff*stream] = uint8(v.entry >> 8)
|
||||
br[stream].bitsRead += uint8(v2.entry)
|
||||
br[stream2].advance(uint8(v2.entry))
|
||||
buf[off+bufoff*stream2] = uint8(v2.entry >> 8)
|
||||
|
||||
val = br[stream].peekBitsFast(d.actualTableLog)
|
||||
v = single[val&tlMask]
|
||||
br[stream].advance(uint8(v.entry))
|
||||
buf[off+bufoff*stream+1] = uint8(v.entry >> 8)
|
||||
|
||||
val2 = br[stream2].peekBitsFast(d.actualTableLog)
|
||||
v2 = single[val2&tlMask]
|
||||
br[stream2].advance(uint8(v2.entry))
|
||||
buf[off+bufoff*stream2+1] = uint8(v2.entry >> 8)
|
||||
}
|
||||
|
||||
{
|
||||
const stream = 2
|
||||
val := br[stream].peekBitsFast(s.actualTableLog)
|
||||
const stream2 = 3
|
||||
br[stream].fillFast()
|
||||
br[stream2].fillFast()
|
||||
|
||||
val := br[stream].peekBitsFast(d.actualTableLog)
|
||||
v := single[val&tlMask]
|
||||
br[stream].bitsRead += uint8(v.entry)
|
||||
|
||||
val2 := br[stream].peekBitsFast(s.actualTableLog)
|
||||
v2 := single[val2&tlMask]
|
||||
buf[off+bufoff*stream+1] = uint8(v2.entry >> 8)
|
||||
br[stream].advance(uint8(v.entry))
|
||||
buf[off+bufoff*stream] = uint8(v.entry >> 8)
|
||||
br[stream].bitsRead += uint8(v2.entry)
|
||||
}
|
||||
|
||||
{
|
||||
const stream = 3
|
||||
val := br[stream].peekBitsFast(s.actualTableLog)
|
||||
v := single[val&tlMask]
|
||||
br[stream].bitsRead += uint8(v.entry)
|
||||
|
||||
val2 := br[stream].peekBitsFast(s.actualTableLog)
|
||||
val2 := br[stream2].peekBitsFast(d.actualTableLog)
|
||||
v2 := single[val2&tlMask]
|
||||
buf[off+bufoff*stream+1] = uint8(v2.entry >> 8)
|
||||
buf[off+bufoff*stream] = uint8(v.entry >> 8)
|
||||
br[stream].bitsRead += uint8(v2.entry)
|
||||
br[stream2].advance(uint8(v2.entry))
|
||||
buf[off+bufoff*stream2] = uint8(v2.entry >> 8)
|
||||
|
||||
val = br[stream].peekBitsFast(d.actualTableLog)
|
||||
v = single[val&tlMask]
|
||||
br[stream].advance(uint8(v.entry))
|
||||
buf[off+bufoff*stream+1] = uint8(v.entry >> 8)
|
||||
|
||||
val2 = br[stream2].peekBitsFast(d.actualTableLog)
|
||||
v2 = single[val2&tlMask]
|
||||
br[stream2].advance(uint8(v2.entry))
|
||||
buf[off+bufoff*stream2+1] = uint8(v2.entry >> 8)
|
||||
}
|
||||
|
||||
off += 2
|
||||
@@ -422,12 +610,456 @@ bigloop:
|
||||
for i := range br {
|
||||
offset := dstEvery * i
|
||||
br := &br[i]
|
||||
for !br.finished() {
|
||||
bitsLeft := br.off*8 + uint(64-br.bitsRead)
|
||||
for bitsLeft > 0 {
|
||||
br.fill()
|
||||
if false && br.bitsRead >= 32 {
|
||||
if br.off >= 4 {
|
||||
v := br.in[br.off-4:]
|
||||
v = v[:4]
|
||||
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
|
||||
br.value = (br.value << 32) | uint64(low)
|
||||
br.bitsRead -= 32
|
||||
br.off -= 4
|
||||
} else {
|
||||
for br.off > 0 {
|
||||
br.value = (br.value << 8) | uint64(br.in[br.off-1])
|
||||
br.bitsRead -= 8
|
||||
br.off--
|
||||
}
|
||||
}
|
||||
}
|
||||
// end inline...
|
||||
if offset >= len(out) {
|
||||
return nil, errors.New("corruption detected: stream overrun 4")
|
||||
}
|
||||
out[offset] = decode(br)
|
||||
|
||||
// Read value and increment offset.
|
||||
val := br.peekBitsFast(d.actualTableLog)
|
||||
v := single[val&tlMask].entry
|
||||
nBits := uint8(v)
|
||||
br.advance(nBits)
|
||||
bitsLeft -= uint(nBits)
|
||||
out[offset] = uint8(v >> 8)
|
||||
offset++
|
||||
}
|
||||
decoded += offset - dstEvery*i
|
||||
err = br.close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if dstSize != decoded {
|
||||
return nil, errors.New("corruption detected: short output block")
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// Decompress4X will decompress a 4X encoded stream.
|
||||
// The length of the supplied input must match the end of a block exactly.
|
||||
// The *capacity* of the dst slice must match the destination size of
|
||||
// the uncompressed data exactly.
|
||||
func (d *Decoder) decompress4X8bit(dst, src []byte) ([]byte, error) {
|
||||
if d.actualTableLog == 8 {
|
||||
return d.decompress4X8bitExactly(dst, src)
|
||||
}
|
||||
|
||||
var br [4]bitReaderBytes
|
||||
start := 6
|
||||
for i := 0; i < 3; i++ {
|
||||
length := int(src[i*2]) | (int(src[i*2+1]) << 8)
|
||||
if start+length >= len(src) {
|
||||
return nil, errors.New("truncated input (or invalid offset)")
|
||||
}
|
||||
err := br[i].init(src[start : start+length])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
start += length
|
||||
}
|
||||
err := br[3].init(src[start:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// destination, offset to match first output
|
||||
dstSize := cap(dst)
|
||||
dst = dst[:dstSize]
|
||||
out := dst
|
||||
dstEvery := (dstSize + 3) / 4
|
||||
|
||||
shift := (8 - d.actualTableLog) & 7
|
||||
|
||||
const tlSize = 1 << 8
|
||||
const tlMask = tlSize - 1
|
||||
single := d.dt.single[:tlSize]
|
||||
|
||||
// Use temp table to avoid bound checks/append penalty.
|
||||
var buf [256]byte
|
||||
var off uint8
|
||||
var decoded int
|
||||
|
||||
// Decode 4 values from each decoder/loop.
|
||||
const bufoff = 256 / 4
|
||||
for {
|
||||
if br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4 {
|
||||
break
|
||||
}
|
||||
|
||||
{
|
||||
// Interleave 2 decodes.
|
||||
const stream = 0
|
||||
const stream2 = 1
|
||||
br[stream].fillFast()
|
||||
br[stream2].fillFast()
|
||||
|
||||
v := single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 := single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+1] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+1] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+2] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+3] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+3] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
}
|
||||
|
||||
{
|
||||
const stream = 2
|
||||
const stream2 = 3
|
||||
br[stream].fillFast()
|
||||
br[stream2].fillFast()
|
||||
|
||||
v := single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 := single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+1] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+1] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+2] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+3] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+3] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
}
|
||||
|
||||
off += 4
|
||||
|
||||
if off == bufoff {
|
||||
if bufoff > dstEvery {
|
||||
return nil, errors.New("corruption detected: stream overrun 1")
|
||||
}
|
||||
copy(out, buf[:bufoff])
|
||||
copy(out[dstEvery:], buf[bufoff:bufoff*2])
|
||||
copy(out[dstEvery*2:], buf[bufoff*2:bufoff*3])
|
||||
copy(out[dstEvery*3:], buf[bufoff*3:bufoff*4])
|
||||
off = 0
|
||||
out = out[bufoff:]
|
||||
decoded += 256
|
||||
// There must at least be 3 buffers left.
|
||||
if len(out) < dstEvery*3 {
|
||||
return nil, errors.New("corruption detected: stream overrun 2")
|
||||
}
|
||||
}
|
||||
}
|
||||
if off > 0 {
|
||||
ioff := int(off)
|
||||
if len(out) < dstEvery*3+ioff {
|
||||
return nil, errors.New("corruption detected: stream overrun 3")
|
||||
}
|
||||
copy(out, buf[:off])
|
||||
copy(out[dstEvery:dstEvery+ioff], buf[bufoff:bufoff*2])
|
||||
copy(out[dstEvery*2:dstEvery*2+ioff], buf[bufoff*2:bufoff*3])
|
||||
copy(out[dstEvery*3:dstEvery*3+ioff], buf[bufoff*3:bufoff*4])
|
||||
decoded += int(off) * 4
|
||||
out = out[off:]
|
||||
}
|
||||
|
||||
// Decode remaining.
|
||||
for i := range br {
|
||||
offset := dstEvery * i
|
||||
br := &br[i]
|
||||
bitsLeft := int(br.off*8) + int(64-br.bitsRead)
|
||||
for bitsLeft > 0 {
|
||||
if br.finished() {
|
||||
return nil, io.ErrUnexpectedEOF
|
||||
}
|
||||
if br.bitsRead >= 56 {
|
||||
if br.off >= 4 {
|
||||
v := br.in[br.off-4:]
|
||||
v = v[:4]
|
||||
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
|
||||
br.value |= uint64(low) << (br.bitsRead - 32)
|
||||
br.bitsRead -= 32
|
||||
br.off -= 4
|
||||
} else {
|
||||
for br.off > 0 {
|
||||
br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
|
||||
br.bitsRead -= 8
|
||||
br.off--
|
||||
}
|
||||
}
|
||||
}
|
||||
// end inline...
|
||||
if offset >= len(out) {
|
||||
return nil, errors.New("corruption detected: stream overrun 4")
|
||||
}
|
||||
|
||||
// Read value and increment offset.
|
||||
v := single[br.peekByteFast()>>shift].entry
|
||||
nBits := uint8(v)
|
||||
br.advance(nBits)
|
||||
bitsLeft -= int(nBits)
|
||||
out[offset] = uint8(v >> 8)
|
||||
offset++
|
||||
}
|
||||
decoded += offset - dstEvery*i
|
||||
err = br.close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if dstSize != decoded {
|
||||
return nil, errors.New("corruption detected: short output block")
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
// Decompress4X will decompress a 4X encoded stream.
|
||||
// The length of the supplied input must match the end of a block exactly.
|
||||
// The *capacity* of the dst slice must match the destination size of
|
||||
// the uncompressed data exactly.
|
||||
func (d *Decoder) decompress4X8bitExactly(dst, src []byte) ([]byte, error) {
|
||||
var br [4]bitReaderBytes
|
||||
start := 6
|
||||
for i := 0; i < 3; i++ {
|
||||
length := int(src[i*2]) | (int(src[i*2+1]) << 8)
|
||||
if start+length >= len(src) {
|
||||
return nil, errors.New("truncated input (or invalid offset)")
|
||||
}
|
||||
err := br[i].init(src[start : start+length])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
start += length
|
||||
}
|
||||
err := br[3].init(src[start:])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// destination, offset to match first output
|
||||
dstSize := cap(dst)
|
||||
dst = dst[:dstSize]
|
||||
out := dst
|
||||
dstEvery := (dstSize + 3) / 4
|
||||
|
||||
const shift = 0
|
||||
const tlSize = 1 << 8
|
||||
const tlMask = tlSize - 1
|
||||
single := d.dt.single[:tlSize]
|
||||
|
||||
// Use temp table to avoid bound checks/append penalty.
|
||||
var buf [256]byte
|
||||
var off uint8
|
||||
var decoded int
|
||||
|
||||
// Decode 4 values from each decoder/loop.
|
||||
const bufoff = 256 / 4
|
||||
for {
|
||||
if br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4 {
|
||||
break
|
||||
}
|
||||
|
||||
{
|
||||
// Interleave 2 decodes.
|
||||
const stream = 0
|
||||
const stream2 = 1
|
||||
br[stream].fillFast()
|
||||
br[stream2].fillFast()
|
||||
|
||||
v := single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 := single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+1] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+1] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+2] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+3] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+3] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
}
|
||||
|
||||
{
|
||||
const stream = 2
|
||||
const stream2 = 3
|
||||
br[stream].fillFast()
|
||||
br[stream2].fillFast()
|
||||
|
||||
v := single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 := single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+1] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+1] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+2] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+2] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
|
||||
v = single[br[stream].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream+3] = uint8(v >> 8)
|
||||
br[stream].advance(uint8(v))
|
||||
|
||||
v2 = single[br[stream2].peekByteFast()>>shift].entry
|
||||
buf[off+bufoff*stream2+3] = uint8(v2 >> 8)
|
||||
br[stream2].advance(uint8(v2))
|
||||
}
|
||||
|
||||
off += 4
|
||||
|
||||
if off == bufoff {
|
||||
if bufoff > dstEvery {
|
||||
return nil, errors.New("corruption detected: stream overrun 1")
|
||||
}
|
||||
copy(out, buf[:bufoff])
|
||||
copy(out[dstEvery:], buf[bufoff:bufoff*2])
|
||||
copy(out[dstEvery*2:], buf[bufoff*2:bufoff*3])
|
||||
copy(out[dstEvery*3:], buf[bufoff*3:bufoff*4])
|
||||
off = 0
|
||||
out = out[bufoff:]
|
||||
decoded += 256
|
||||
// There must at least be 3 buffers left.
|
||||
if len(out) < dstEvery*3 {
|
||||
return nil, errors.New("corruption detected: stream overrun 2")
|
||||
}
|
||||
}
|
||||
}
|
||||
if off > 0 {
|
||||
ioff := int(off)
|
||||
if len(out) < dstEvery*3+ioff {
|
||||
return nil, errors.New("corruption detected: stream overrun 3")
|
||||
}
|
||||
copy(out, buf[:off])
|
||||
copy(out[dstEvery:dstEvery+ioff], buf[bufoff:bufoff*2])
|
||||
copy(out[dstEvery*2:dstEvery*2+ioff], buf[bufoff*2:bufoff*3])
|
||||
copy(out[dstEvery*3:dstEvery*3+ioff], buf[bufoff*3:bufoff*4])
|
||||
decoded += int(off) * 4
|
||||
out = out[off:]
|
||||
}
|
||||
|
||||
// Decode remaining.
|
||||
for i := range br {
|
||||
offset := dstEvery * i
|
||||
br := &br[i]
|
||||
bitsLeft := int(br.off*8) + int(64-br.bitsRead)
|
||||
for bitsLeft > 0 {
|
||||
if br.finished() {
|
||||
return nil, io.ErrUnexpectedEOF
|
||||
}
|
||||
if br.bitsRead >= 56 {
|
||||
if br.off >= 4 {
|
||||
v := br.in[br.off-4:]
|
||||
v = v[:4]
|
||||
low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
|
||||
br.value |= uint64(low) << (br.bitsRead - 32)
|
||||
br.bitsRead -= 32
|
||||
br.off -= 4
|
||||
} else {
|
||||
for br.off > 0 {
|
||||
br.value |= uint64(br.in[br.off-1]) << (br.bitsRead - 8)
|
||||
br.bitsRead -= 8
|
||||
br.off--
|
||||
}
|
||||
}
|
||||
}
|
||||
// end inline...
|
||||
if offset >= len(out) {
|
||||
return nil, errors.New("corruption detected: stream overrun 4")
|
||||
}
|
||||
|
||||
// Read value and increment offset.
|
||||
v := single[br.peekByteFast()>>shift].entry
|
||||
nBits := uint8(v)
|
||||
br.advance(nBits)
|
||||
bitsLeft -= int(nBits)
|
||||
out[offset] = uint8(v >> 8)
|
||||
offset++
|
||||
}
|
||||
decoded += offset - dstEvery*i
|
||||
|
Reference in New Issue
Block a user