Debian Codegraph @

huff0/decompress_generic.go

raw
1 //go:build (!amd64 && !arm64) || appengine || !gc || noasm
2
3 // This file contains a generic implementation of Decoder.Decompress4X.
4 package huff0
5
6 import (
7 "errors"
8 "fmt"
9 )
10
11 // Decompress4X will decompress a 4X encoded stream.
12 // The length of the supplied input must match the end of a block exactly.
13 // The *capacity* of the dst slice must match the destination size of
14 // the uncompressed data exactly.
15 func (d *Decoder) Decompress4X(dst, src []byte) ([]byte, error) {
16 if len(d.dt.single) == 0 {
17 return nil, errors.New("no table loaded")
18 }
19 if len(src) < 6+(4*1) {
20 return nil, errors.New("input too small")
21 }
22 if use8BitTables && d.actualTableLog <= 8 {
23 return d.decompress4X8bit(dst, src)
24 }
25
26 var br [4]bitReaderShifted
27 // Decode "jump table"
28 start := 6
29 for i := 0; i < 3; i++ {
30 length := int(src[i*2]) | (int(src[i*2+1]) << 8)
31 if start+length >= len(src) {
32 return nil, errors.New("truncated input (or invalid offset)")
33 }
34 err := br[i].init(src[start : start+length])
35 if err != nil {
36 return nil, err
37 }
38 start += length
39 }
40 err := br[3].init(src[start:])
41 if err != nil {
42 return nil, err
43 }
44
45 // destination, offset to match first output
46 dstSize := cap(dst)
47 dst = dst[:dstSize]
48 out := dst
49 dstEvery := (dstSize + 3) / 4
50
51 const tlSize = 1 << tableLogMax
52 const tlMask = tlSize - 1
53 single := d.dt.single[:tlSize]
54
55 // Use temp table to avoid bound checks/append penalty.
56 buf := d.buffer()
57 var off uint8
58 var decoded int
59
60 // Decode 2 values from each decoder/loop.
61 const bufoff = 256
62 for {
63 if br[0].off < 4 || br[1].off < 4 || br[2].off < 4 || br[3].off < 4 {
64 break
65 }
66
67 {
68 const stream = 0
69 const stream2 = 1
70 br[stream].fillFast()
71 br[stream2].fillFast()
72
73 val := br[stream].peekBitsFast(d.actualTableLog)
74 val2 := br[stream2].peekBitsFast(d.actualTableLog)
75 v := single[val&tlMask]
76 v2 := single[val2&tlMask]
77 br[stream].advance(uint8(v.entry))
78 br[stream2].advance(uint8(v2.entry))
79 buf[stream][off] = uint8(v.entry >> 8)
80 buf[stream2][off] = uint8(v2.entry >> 8)
81
82 val = br[stream].peekBitsFast(d.actualTableLog)
83 val2 = br[stream2].peekBitsFast(d.actualTableLog)
84 v = single[val&tlMask]
85 v2 = single[val2&tlMask]
86 br[stream].advance(uint8(v.entry))
87 br[stream2].advance(uint8(v2.entry))
88 buf[stream][off+1] = uint8(v.entry >> 8)
89 buf[stream2][off+1] = uint8(v2.entry >> 8)
90 }
91
92 {
93 const stream = 2
94 const stream2 = 3
95 br[stream].fillFast()
96 br[stream2].fillFast()
97
98 val := br[stream].peekBitsFast(d.actualTableLog)
99 val2 := br[stream2].peekBitsFast(d.actualTableLog)
100 v := single[val&tlMask]
101 v2 := single[val2&tlMask]
102 br[stream].advance(uint8(v.entry))
103 br[stream2].advance(uint8(v2.entry))
104 buf[stream][off] = uint8(v.entry >> 8)
105 buf[stream2][off] = uint8(v2.entry >> 8)
106
107 val = br[stream].peekBitsFast(d.actualTableLog)
108 val2 = br[stream2].peekBitsFast(d.actualTableLog)
109 v = single[val&tlMask]
110 v2 = single[val2&tlMask]
111 br[stream].advance(uint8(v.entry))
112 br[stream2].advance(uint8(v2.entry))
113 buf[stream][off+1] = uint8(v.entry >> 8)
114 buf[stream2][off+1] = uint8(v2.entry >> 8)
115 }
116
117 off += 2
118
119 if off == 0 {
120 if bufoff > dstEvery {
121 d.bufs.Put(buf)
122 return nil, errors.New("corruption detected: stream overrun 1")
123 }
124 // There must at least be 3 buffers left.
125 if len(out)-bufoff < dstEvery*3 {
126 d.bufs.Put(buf)
127 return nil, errors.New("corruption detected: stream overrun 2")
128 }
129 //copy(out, buf[0][:])
130 //copy(out[dstEvery:], buf[1][:])
131 //copy(out[dstEvery*2:], buf[2][:])
132 //copy(out[dstEvery*3:], buf[3][:])
133 *(*[bufoff]byte)(out) = buf[0]
134 *(*[bufoff]byte)(out[dstEvery:]) = buf[1]
135 *(*[bufoff]byte)(out[dstEvery*2:]) = buf[2]
136 *(*[bufoff]byte)(out[dstEvery*3:]) = buf[3]
137 out = out[bufoff:]
138 decoded += bufoff * 4
139 }
140 }
141 if off > 0 {
142 ioff := int(off)
143 if len(out) < dstEvery*3+ioff {
144 d.bufs.Put(buf)
145 return nil, errors.New("corruption detected: stream overrun 3")
146 }
147 copy(out, buf[0][:off])
148 copy(out[dstEvery:], buf[1][:off])
149 copy(out[dstEvery*2:], buf[2][:off])
150 copy(out[dstEvery*3:], buf[3][:off])
151 decoded += int(off) * 4
152 out = out[off:]
153 }
154
155 // Decode remaining.
156 remainBytes := dstEvery - (decoded / 4)
157 for i := range br {
158 offset := dstEvery * i
159 endsAt := offset + remainBytes
160 if endsAt > len(out) {
161 endsAt = len(out)
162 }
163 br := &br[i]
164 bitsLeft := br.remaining()
165 for bitsLeft > 0 {
166 br.fill()
167 if offset >= endsAt {
168 d.bufs.Put(buf)
169 return nil, errors.New("corruption detected: stream overrun 4")
170 }
171
172 // Read value and increment offset.
173 val := br.peekBitsFast(d.actualTableLog)
174 v := single[val&tlMask].entry
175 nBits := uint8(v)
176 br.advance(nBits)
177 bitsLeft -= uint(nBits)
178 out[offset] = uint8(v >> 8)
179 offset++
180 }
181 if offset != endsAt {
182 d.bufs.Put(buf)
183 return nil, fmt.Errorf("corruption detected: short output block %d, end %d != %d", i, offset, endsAt)
184 }
185 decoded += offset - dstEvery*i
186 err = br.close()
187 if err != nil {
188 return nil, err
189 }
190 }
191 d.bufs.Put(buf)
192 if dstSize != decoded {
193 return nil, errors.New("corruption detected: short output block")
194 }
195 return dst, nil
196 }
197
198 // Decompress1X will decompress a 1X encoded stream.
199 // The cap of the output buffer will be the maximum decompressed size.
200 // The length of the supplied input must match the end of a block exactly.
201 func (d *Decoder) Decompress1X(dst, src []byte) ([]byte, error) {
202 if len(d.dt.single) == 0 {
203 return nil, errors.New("no table loaded")
204 }
205 if use8BitTables && d.actualTableLog <= 8 {
206 return d.decompress1X8Bit(dst, src)
207 }
208 var br bitReaderShifted
209 err := br.init(src)
210 if err != nil {
211 return dst, err
212 }
213 maxDecodedSize := cap(dst)
214 dst = dst[:0]
215
216 // Avoid bounds check by always having full sized table.
217 const tlSize = 1 << tableLogMax
218 const tlMask = tlSize - 1
219 dt := d.dt.single[:tlSize]
220
221 // Use temp table to avoid bound checks/append penalty.
222 bufs := d.buffer()
223 buf := &bufs[0]
224 var off uint8
225
226 for br.off >= 8 {
227 br.fillFast()
228 v := dt[br.peekBitsFast(d.actualTableLog)&tlMask]
229 br.advance(uint8(v.entry))
230 buf[off+0] = uint8(v.entry >> 8)
231
232 v = dt[br.peekBitsFast(d.actualTableLog)&tlMask]
233 br.advance(uint8(v.entry))
234 buf[off+1] = uint8(v.entry >> 8)
235
236 // Refill
237 br.fillFast()
238
239 v = dt[br.peekBitsFast(d.actualTableLog)&tlMask]
240 br.advance(uint8(v.entry))
241 buf[off+2] = uint8(v.entry >> 8)
242
243 v = dt[br.peekBitsFast(d.actualTableLog)&tlMask]
244 br.advance(uint8(v.entry))
245 buf[off+3] = uint8(v.entry >> 8)
246
247 off += 4
248 if off == 0 {
249 if len(dst)+256 > maxDecodedSize {
250 br.close()
251 d.bufs.Put(bufs)
252 return nil, ErrMaxDecodedSizeExceeded
253 }
254 dst = append(dst, buf[:]...)
255 }
256 }
257
258 if len(dst)+int(off) > maxDecodedSize {
259 d.bufs.Put(bufs)
260 br.close()
261 return nil, ErrMaxDecodedSizeExceeded
262 }
263 dst = append(dst, buf[:off]...)
264
265 // br < 8, so uint8 is fine
266 bitsLeft := uint8(br.off)*8 + 64 - br.bitsRead
267 for bitsLeft > 0 {
268 br.fill()
269 if false && br.bitsRead >= 32 {
270 if br.off >= 4 {
271 v := br.in[br.off-4:]
272 v = v[:4]
273 low := (uint32(v[0])) | (uint32(v[1]) << 8) | (uint32(v[2]) << 16) | (uint32(v[3]) << 24)
274 br.value = (br.value << 32) | uint64(low)
275 br.bitsRead -= 32
276 br.off -= 4
277 } else {
278 for br.off > 0 {
279 br.value = (br.value << 8) | uint64(br.in[br.off-1])
280 br.bitsRead -= 8
281 br.off--
282 }
283 }
284 }
285 if len(dst) >= maxDecodedSize {
286 d.bufs.Put(bufs)
287 br.close()
288 return nil, ErrMaxDecodedSizeExceeded
289 }
290 v := d.dt.single[br.peekBitsFast(d.actualTableLog)&tlMask]
291 nBits := uint8(v.entry)
292 br.advance(nBits)
293 bitsLeft -= nBits
294 dst = append(dst, uint8(v.entry>>8))
295 }
296 d.bufs.Put(bufs)
297 return dst, br.close()
298 }