Module: Omnizip::Algorithms::Zstandard::FSE::Interleaved

Defined in:
lib/omnizip/algorithms/zstandard/fse/interleaved.rb

Overview

2-state interleaved FSE stream decoder (RFC 8878 ยง4.2.1.2, "FSE compression of Huffman weights"; mirrors the C reference FSE_decompress_usingDTable).

Symbols are produced until the bitstream overflows; after an overflow following one state's transition, one final symbol is decoded from the other state without transitioning.

Class Method Summary collapse

Class Method Details

.check_length!(out, max_output) ⇒ Object



82
83
84
85
86
87
# File 'lib/omnizip/algorithms/zstandard/fse/interleaved.rb', line 82

def check_length!(out, max_output)
  return unless out.length >= max_output

  raise Omnizip::DecompressionError,
        "FSE decode exceeded max output #{max_output}"
end

.decode_stream(table, bitstream_bytes, max_output) ⇒ Array<Integer>

Decode a 2-state interleaved FSE stream.

Parameters:

  • table (Table)
  • bitstream_bytes (String)
  • max_output (Integer)

    cap on decoded length (corrupt input protection)

Returns:

  • (Array<Integer>)

    decoded symbols



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/omnizip/algorithms/zstandard/fse/interleaved.rb', line 44

def decode_stream(table, bitstream_bytes, max_output)
  bs = BitStream.new(bitstream_bytes)
  accuracy_log = table.accuracy_log

  s1 = bs.read_bits(accuracy_log)
  bs.reload
  s2 = bs.read_bits(accuracy_log)
  bs.reload

  out = []

  loop do
    check_length!(out, max_output)
    e1 = table[s1]
    out << e1.symbol
    s1 = e1.baseline + bs.read_bits(e1.num_bits)

    if bs.reload_status == BitStream::OVERFLOW
      check_length!(out, max_output)
      out << table[s2].symbol
      break
    end

    check_length!(out, max_output)
    e2 = table[s2]
    out << e2.symbol
    s2 = e2.baseline + bs.read_bits(e2.num_bits)

    if bs.reload_status == BitStream::OVERFLOW
      check_length!(out, max_output)
      out << table[s1].symbol
      break
    end
  end

  out
end