Class: Omnizip::Algorithms::Zstandard::Decoder

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/algorithms/zstandard/decoder.rb

Overview

Pure Ruby Zstandard decoder (RFC 8878)

Decodes Zstandard-compressed data according to RFC 8878.

Decoder pipeline:

  1. Parse frame header
  2. For each block:
    1. Parse block header
    2. Decode literals section
    3. Decode sequences section
    4. Execute sequences (LZ77 copy operations)
  3. Verify content checksum if present

Constant Summary

Constants included from Constants

Constants::BLOCK_HEADER_SIZE, Constants::BLOCK_MAX_SIZE, Constants::BLOCK_TYPE_COMPRESSED, Constants::BLOCK_TYPE_RAW, Constants::BLOCK_TYPE_RESERVED, Constants::BLOCK_TYPE_RLE, Constants::BUFFER_SIZE, Constants::DEFAULT_LEVEL, Constants::DEFAULT_REPEAT_OFFSETS, Constants::FSE_MAX_ACCURACY_LOG, Constants::FSE_MIN_ACCURACY_LOG, Constants::HUFFMAN_MAX_BITS, Constants::HUFFMAN_MAX_CODE_LENGTH, Constants::HUFFMAN_MAX_LOG, Constants::HUFFMAN_STANDARD_TABLE_SIZE, Constants::LITERALS_BLOCK_COMPRESSED, Constants::LITERALS_BLOCK_RAW, Constants::LITERALS_BLOCK_RLE, Constants::LITERALS_BLOCK_TREELESS, Constants::LITERALS_LENGTH_ACCURACY_LOG, Constants::LITERAL_LENGTH_TABLE, Constants::MAGIC_BYTES, Constants::MAGIC_NUMBER, Constants::MATCH_LENGTH_ACCURACY_LOG, Constants::MATCH_LENGTH_TABLE, Constants::MAX_LEVEL, Constants::MIN_LEVEL, Constants::MODE_FSE, Constants::MODE_PREDEFINED, Constants::MODE_REPEAT, Constants::MODE_RLE, Constants::OFFSET_ACCURACY_LOG, Constants::PREDEFINED_LL_DISTRIBUTION, Constants::PREDEFINED_ML_DISTRIBUTION, Constants::PREDEFINED_OFFSET_DISTRIBUTION, Constants::REPEAT_OFFSET_1, Constants::REPEAT_OFFSET_2, Constants::REPEAT_OFFSET_3, Constants::SKIPPABLE_MAGIC_BASE, Constants::SKIPPABLE_MAGIC_MASK, Constants::WINDOW_LOG_MAX, Constants::WINDOW_LOG_MIN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_stream) ⇒ Decoder

Initialize decoder

Parameters:

  • input_stream (IO)

    Input stream of compressed data



47
48
49
50
51
52
# File 'lib/omnizip/algorithms/zstandard/decoder.rb', line 47

def initialize(input_stream)
  @input_stream = input_stream
  @repeat_offsets = DEFAULT_REPEAT_OFFSETS.dup
  @previous_huffman_table = nil
  @previous_fse_tables = {}
end

Instance Attribute Details

#input_streamIO (readonly)

Returns Input stream.

Returns:

  • (IO)

    Input stream



42
43
44
# File 'lib/omnizip/algorithms/zstandard/decoder.rb', line 42

def input_stream
  @input_stream
end

Instance Method Details

#decode_streamString

Decode compressed data stream

Returns:

  • (String)

    Decompressed data



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/omnizip/algorithms/zstandard/decoder.rb', line 57

def decode_stream
  output = String.new(encoding: Encoding::BINARY)

  loop do
    # Read magic number
    magic = read_u32le

    # Check for skippable frame
    if skippable_frame?(magic)
      skip_frame
      next
    end

    # Validate magic number
    unless magic == MAGIC_NUMBER
      raise "Invalid Zstandard magic: 0x#{magic.to_s(16)}"
    end

    # Parse frame
    frame_output = decode_frame
    output << frame_output

    # Check for more frames
    break if @input_stream.eof?
  end

  output
end