| 1 |
//go:build (amd64 || arm64) && !appengine && !noasm && gc |
| 2 |
|
| 3 |
// This file contains the specialisation of Decoder.Decompress4X |
| 4 |
// and Decoder.Decompress1X that use an asm implementation of their main loops. |
| 5 |
// The asm function stubs and any per-arch dispatch live in decompress_amd64.go |
| 6 |
// and decompress_arm64.go. |
| 7 |
package huff0 |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
) |
| 13 |
|
| 14 |
// fallback8BitSize is the size where using Go version is faster. |
| 15 |
const fallback8BitSize = 800 |
| 16 |
|
| 17 |
type decompress4xContext struct { |
| 18 |
pbr *[4]bitReaderShifted |
| 19 |
peekBits uint8 |
| 20 |
out *byte |
| 21 |
dstEvery int |
| 22 |
tbl *dEntrySingle |
| 23 |
decoded int |
| 24 |
limit *byte |
| 25 |
} |
| 26 |
|
| 27 |
// Decompress4X will decompress a 4X encoded stream. |
| 28 |
// The length of the supplied input must match the end of a block exactly. |
| 29 |
// The *capacity* of the dst slice must match the destination size of |
| 30 |
// the uncompressed data exactly. |
| 31 |
func (d *Decoder) Decompress4X(dst, src []byte) ([]byte, error) { |
| 32 |
if len(d.dt.single) == 0 { |
| 33 |
return nil, errors.New("no table loaded") |
| 34 |
} |
| 35 |
if len(src) < 6+(4*1) { |
| 36 |
return nil, errors.New("input too small") |
| 37 |
} |
| 38 |
|
| 39 |
use8BitTables := d.actualTableLog <= 8 |
| 40 |
if cap(dst) < fallback8BitSize && use8BitTables { |
| 41 |
return d.decompress4X8bit(dst, src) |
| 42 |
} |
| 43 |
|
| 44 |
var br [4]bitReaderShifted |
| 45 |
// Decode "jump table" |
| 46 |
start := 6 |
| 47 |
for i := range 3 { |
| 48 |
length := int(src[i*2]) | (int(src[i*2+1]) << 8) |
| 49 |
if start+length >= len(src) { |
| 50 |
return nil, errors.New("truncated input (or invalid offset)") |
| 51 |
} |
| 52 |
err := br[i].init(src[start : start+length]) |
| 53 |
if err != nil { |
| 54 |
return nil, err |
| 55 |
} |
| 56 |
start += length |
| 57 |
} |
| 58 |
err := br[3].init(src[start:]) |
| 59 |
if err != nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
|
| 63 |
// destination, offset to match first output |
| 64 |
dstSize := cap(dst) |
| 65 |
dst = dst[:dstSize] |
| 66 |
out := dst |
| 67 |
dstEvery := (dstSize + 3) / 4 |
| 68 |
|
| 69 |
const tlSize = 1 << tableLogMax |
| 70 |
const tlMask = tlSize - 1 |
| 71 |
single := d.dt.single[:tlSize] |
| 72 |
|
| 73 |
var decoded int |
| 74 |
|
| 75 |
if len(out) > 4*4 && !(br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4) { |
| 76 |
ctx := decompress4xContext{ |
| 77 |
pbr: &br, |
| 78 |
peekBits: uint8((64 - d.actualTableLog) & 63), // see: bitReaderShifted.peekBitsFast() |
| 79 |
out: &out[0], |
| 80 |
dstEvery: dstEvery, |
| 81 |
tbl: &single[0], |
| 82 |
limit: &out[dstEvery-4], // Always stop decoding when first buffer gets here to avoid writing OOB on last. |
| 83 |
} |
| 84 |
if use8BitTables { |
| 85 |
decompress4x_8b_main_loop_asm(&ctx) |
| 86 |
} else { |
| 87 |
decompress4x_main_loop_asm(&ctx) |
| 88 |
} |
| 89 |
|
| 90 |
decoded = ctx.decoded |
| 91 |
out = out[decoded/4:] |
| 92 |
} |
| 93 |
|
| 94 |
// Decode remaining. |
| 95 |
remainBytes := dstEvery - (decoded / 4) |
| 96 |
for i := range br { |
| 97 |
offset := dstEvery * i |
| 98 |
endsAt := min(offset+remainBytes, len(out)) |
| 99 |
br := &br[i] |
| 100 |
bitsLeft := br.remaining() |
| 101 |
for bitsLeft > 0 { |
| 102 |
br.fill() |
| 103 |
if offset >= endsAt { |
| 104 |
return nil, errors.New("corruption detected: stream overrun 4") |
| 105 |
} |
| 106 |
|
| 107 |
// Read value and increment offset. |
| 108 |
val := br.peekBitsFast(d.actualTableLog) |
| 109 |
v := single[val&tlMask].entry |
| 110 |
nBits := uint8(v) |
| 111 |
br.advance(nBits) |
| 112 |
bitsLeft -= uint(nBits) |
| 113 |
out[offset] = uint8(v >> 8) |
| 114 |
offset++ |
| 115 |
} |
| 116 |
if offset != endsAt { |
| 117 |
return nil, fmt.Errorf("corruption detected: short output block %d, end %d != %d", i, offset, endsAt) |
| 118 |
} |
| 119 |
decoded += offset - dstEvery*i |
| 120 |
err = br.close() |
| 121 |
if err != nil { |
| 122 |
return nil, err |
| 123 |
} |
| 124 |
} |
| 125 |
if dstSize != decoded { |
| 126 |
return nil, errors.New("corruption detected: short output block") |
| 127 |
} |
| 128 |
return dst, nil |
| 129 |
} |
| 130 |
|
| 131 |
type decompress1xContext struct { |
| 132 |
pbr *bitReaderShifted |
| 133 |
peekBits uint8 |
| 134 |
out *byte |
| 135 |
outCap int |
| 136 |
tbl *dEntrySingle |
| 137 |
decoded int |
| 138 |
} |
| 139 |
|
| 140 |
// Error reported by asm implementations |
| 141 |
const error_max_decoded_size_exeeded = -1 |
| 142 |
|
| 143 |
// Decompress1X will decompress a 1X encoded stream. |
| 144 |
// The cap of the output buffer will be the maximum decompressed size. |
| 145 |
// The length of the supplied input must match the end of a block exactly. |
| 146 |
func (d *Decoder) Decompress1X(dst, src []byte) ([]byte, error) { |
| 147 |
if len(d.dt.single) == 0 { |
| 148 |
return nil, errors.New("no table loaded") |
| 149 |
} |
| 150 |
var br bitReaderShifted |
| 151 |
err := br.init(src) |
| 152 |
if err != nil { |
| 153 |
return dst, err |
| 154 |
} |
| 155 |
maxDecodedSize := cap(dst) |
| 156 |
dst = dst[:maxDecodedSize] |
| 157 |
|
| 158 |
const tlSize = 1 << tableLogMax |
| 159 |
const tlMask = tlSize - 1 |
| 160 |
|
| 161 |
if maxDecodedSize >= 4 { |
| 162 |
ctx := decompress1xContext{ |
| 163 |
pbr: &br, |
| 164 |
out: &dst[0], |
| 165 |
outCap: maxDecodedSize, |
| 166 |
peekBits: uint8((64 - d.actualTableLog) & 63), // see: bitReaderShifted.peekBitsFast() |
| 167 |
tbl: &d.dt.single[0], |
| 168 |
} |
| 169 |
|
| 170 |
decompress1x_main_loop_asm(&ctx) |
| 171 |
if ctx.decoded == error_max_decoded_size_exeeded { |
| 172 |
return nil, ErrMaxDecodedSizeExceeded |
| 173 |
} |
| 174 |
|
| 175 |
dst = dst[:ctx.decoded] |
| 176 |
} |
| 177 |
|
| 178 |
// br < 8, so uint8 is fine |
| 179 |
bitsLeft := uint8(br.off)*8 + 64 - br.bitsRead |
| 180 |
for bitsLeft > 0 { |
| 181 |
br.fill() |
| 182 |
if len(dst) >= maxDecodedSize { |
| 183 |
br.close() |
| 184 |
return nil, ErrMaxDecodedSizeExceeded |
| 185 |
} |
| 186 |
v := d.dt.single[br.peekBitsFast(d.actualTableLog)&tlMask] |
| 187 |
nBits := uint8(v.entry) |
| 188 |
br.advance(nBits) |
| 189 |
bitsLeft -= nBits |
| 190 |
dst = append(dst, uint8(v.entry>>8)) |
| 191 |
} |
| 192 |
return dst, br.close() |
| 193 |
} |