13
tablelogAbsoluteMax = 9
22
maxMemoryUsage = tablelogAbsoluteMax + 2
24
maxTableLog = maxMemoryUsage - 2
25
maxTablesize = 1 << maxTableLog
26
maxTableMask = (1 << maxTableLog) - 1
32
type fseDecoder struct {
33
dt [maxTablesize]decSymbol
39
stateTable [256]uint16
40
norm [maxSymbolValue + 1]int16
45
func tableStep(tableSize uint32) uint32 {
46
return (tableSize >> 1) + (tableSize >> 3) + 3
50
func (s *fseDecoder) readNCount(b *byteReader, maxSymbol uint16) error {
56
return errors.New("input too small")
58
bitStream := b.Uint32NC()
59
nbBits := uint((bitStream & 0xF) + minTablelog)
60
if nbBits > tablelogAbsoluteMax {
61
println("Invalid tablelog:", nbBits)
62
return errors.New("tableLog too large")
67
s.actualTableLog = uint8(nbBits)
68
remaining := int32((1 << nbBits) + 1)
69
threshold := int32(1 << nbBits)
73
for remaining > 1 && charnum <= maxSymbol {
77
for (bitStream & 0xFFFF) == 0xFFFF {
80
if r := b.remain(); r > 5 {
83
bitStream = b.Uint32NC() >> bitCount
91
for (bitStream & 3) == 3 {
96
n0 += uint16(bitStream & 3)
99
if n0 > maxSymbolValue {
100
return errors.New("maxSymbolValue too small")
104
s.norm[uint8(charnum)] = 0
108
if r := b.remain(); r >= 7 || r-int(bitCount>>3) >= 4 {
109
b.advance(bitCount >> 3)
112
bitStream = b.Uint32NC() >> bitCount
118
max := (2*threshold - 1) - remaining
121
if int32(bitStream)&(threshold-1) < max {
122
count = int32(bitStream) & (threshold - 1)
123
if debugAsserts && nbBits < 1 {
124
panic("nbBits underflow")
126
bitCount += nbBits - 1
128
count = int32(bitStream) & (2*threshold - 1)
129
if count >= threshold {
145
s.norm[charnum&0xff] = int16(count)
147
previous0 = count == 0
148
for remaining < threshold {
153
if r := b.remain(); r >= 7 || r-int(bitCount>>3) >= 4 {
154
b.advance(bitCount >> 3)
157
bitStream = b.Uint32NC() >> (bitCount & 31)
159
bitCount -= (uint)(8 * (len(b.b) - 4 - b.off))
161
bitStream = b.Uint32() >> (bitCount & 31)
164
s.symbolLen = charnum
165
if s.symbolLen <= 1 {
166
return fmt.Errorf("symbolLen (%d) too small", s.symbolLen)
168
if s.symbolLen > maxSymbolValue+1 {
169
return fmt.Errorf("symbolLen (%d) too big", s.symbolLen)
172
return fmt.Errorf("corruption detected (remaining %d != 1)", remaining)
175
return fmt.Errorf("corruption detected (bitCount %d > 32)", bitCount)
177
if gotTotal != 1<<s.actualTableLog {
178
return fmt.Errorf("corruption detected (total %d != %d)", gotTotal, 1<<s.actualTableLog)
180
b.advance((bitCount + 7) >> 3)
182
return s.buildDtable()
191
func newDecSymbol(nbits, addBits uint8, newState uint16, baseline uint32) decSymbol {
192
return decSymbol(nbits) | (decSymbol(addBits) << 8) | (decSymbol(newState) << 16) | (decSymbol(baseline) << 32)
195
func (d decSymbol) nbBits() uint8 {
199
func (d decSymbol) addBits() uint8 {
203
func (d decSymbol) newState() uint16 {
204
return uint16(d >> 16)
207
func (d decSymbol) baseline() uint32 {
208
return uint32(d >> 32)
211
func (d decSymbol) baselineInt() int {
215
func (d *decSymbol) set(nbits, addBits uint8, newState uint16, baseline uint32) {
216
*d = decSymbol(nbits) | (decSymbol(addBits) << 8) | (decSymbol(newState) << 16) | (decSymbol(baseline) << 32)
219
func (d *decSymbol) setNBits(nBits uint8) {
220
const mask = 0xffffffffffffff00
221
*d = (*d & mask) | decSymbol(nBits)
224
func (d *decSymbol) setAddBits(addBits uint8) {
225
const mask = 0xffffffffffff00ff
226
*d = (*d & mask) | (decSymbol(addBits) << 8)
229
func (d *decSymbol) setNewState(state uint16) {
230
const mask = 0xffffffff0000ffff
231
*d = (*d & mask) | decSymbol(state)<<16
234
func (d *decSymbol) setBaseline(baseline uint32) {
235
const mask = 0xffffffff
236
*d = (*d & mask) | decSymbol(baseline)<<32
239
func (d *decSymbol) setExt(addBits uint8, baseline uint32) {
240
const mask = 0xffff00ff
241
*d = (*d & mask) | (decSymbol(addBits) << 8) | (decSymbol(baseline) << 32)
245
func decSymbolValue(symb uint8, t []baseOffset) (decSymbol, error) {
246
if int(symb) >= len(t) {
247
return 0, fmt.Errorf("rle symbol %d >= max %d", symb, len(t))
250
return newDecSymbol(0, lu.addBits, 0, lu.baseLine), nil
254
func (s *fseDecoder) setRLE(symbol decSymbol) {
256
s.maxBits = symbol.addBits()
261
func (s *fseDecoder) buildDtable() error {
262
tableSize := uint32(1 << s.actualTableLog)
263
highThreshold := tableSize - 1
264
symbolNext := s.stateTable[:256]
268
for i, v := range s.norm[:s.symbolLen] {
270
s.dt[highThreshold].setAddBits(uint8(i))
274
symbolNext[i] = uint16(v)
280
tableMask := tableSize - 1
281
step := tableStep(tableSize)
282
position := uint32(0)
283
for ss, v := range s.norm[:s.symbolLen] {
284
for i := 0; i < int(v); i++ {
285
s.dt[position].setAddBits(uint8(ss))
286
position = (position + step) & tableMask
287
for position > highThreshold {
289
position = (position + step) & tableMask
295
return errors.New("corrupted input (position != 0)")
301
tableSize := uint16(1 << s.actualTableLog)
302
for u, v := range s.dt[:tableSize] {
303
symbol := v.addBits()
304
nextState := symbolNext[symbol]
305
symbolNext[symbol] = nextState + 1
306
nBits := s.actualTableLog - byte(highBits(uint32(nextState)))
307
s.dt[u&maxTableMask].setNBits(nBits)
308
newState := (nextState << nBits) - tableSize
309
if newState > tableSize {
310
return fmt.Errorf("newState (%d) outside table size (%d)", newState, tableSize)
312
if newState == uint16(u) && nBits == 0 {
314
return fmt.Errorf("newState (%d) == oldState (%d) and no bits", newState, u)
316
s.dt[u&maxTableMask].setNewState(newState)
325
func (s *fseDecoder) transform(t []baseOffset) error {
326
tableSize := uint16(1 << s.actualTableLog)
328
for i, v := range s.dt[:tableSize] {
330
if int(add) >= len(t) {
331
return fmt.Errorf("invalid decoding table entry %d, symbol %d >= max (%d)", i, v.addBits(), len(t))
334
if lu.addBits > s.maxBits {
335
s.maxBits = lu.addBits
337
v.setExt(lu.addBits, lu.baseLine)
343
type fseState struct {
349
func (s *fseState) init(br *bitReader, tableLog uint8, dt []decSymbol) {
352
s.state = dt[br.getBits(tableLog)]
357
func (s *fseState) next(br *bitReader) {
358
lowBits := uint16(br.getBits(s.state.nbBits()))
359
s.state = s.dt[s.state.newState()+lowBits]
364
func (s *fseState) finished(br *bitReader) bool {
365
return br.finished() && s.state.nbBits() > 0
369
func (s *fseState) final() (int, uint8) {
370
return s.state.baselineInt(), s.state.addBits()
374
func (s decSymbol) final() (int, uint8) {
375
return s.baselineInt(), s.addBits()
381
func (s *fseState) nextFast(br *bitReader) (uint32, uint8) {
382
lowBits := br.get16BitsFast(s.state.nbBits())
383
s.state = s.dt[s.state.newState()+lowBits]
384
return s.state.baseline(), s.state.addBits()