Class: Omnizip::Algorithms::Zstandard::SequenceExecutor

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

Overview

Sequence executor (RFC 8878 Section 3.1.2.2.3)

Executes decoded sequences to produce output.

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSequenceExecutor

Initialize with default repeat offsets



270
271
272
# File 'lib/omnizip/algorithms/zstandard/sequences.rb', line 270

def initialize
  @repeat_offsets = DEFAULT_REPEAT_OFFSETS.dup
end

Class Method Details

.execute(literals, sequences) ⇒ String

Execute sequences to produce decompressed output

Parameters:

  • literals (String)

    Decoded literals

  • sequences (Array<Hash>)

    Decoded sequences

Returns:

  • (String)

    Decompressed output



264
265
266
267
# File 'lib/omnizip/algorithms/zstandard/sequences.rb', line 264

def self.execute(literals, sequences)
  executor = new
  executor.execute(literals, sequences)
end

Instance Method Details

#execute(literals, sequences) ⇒ String

Execute sequences

Parameters:

  • literals (String)

    Decoded literals

  • sequences (Array<Hash>)

    Decoded sequences

Returns:

  • (String)

    Decompressed output



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/omnizip/algorithms/zstandard/sequences.rb', line 279

def execute(literals, sequences)
  output = String.new(encoding: Encoding::BINARY)
  lit_pos = 0

  sequences.each do |seq|
    ll = seq[:literals_length] || 0
    ml = seq[:match_length] || 0
    offset_code = seq[:offset] || 0

    # Copy literals
    if ll.positive? && lit_pos < literals.length
      copy_len = [ll, literals.length - lit_pos].min
      output << literals.slice(lit_pos, copy_len)
      lit_pos += copy_len
    end

    # Handle match
    if ml.positive?
      offset = resolve_offset(offset_code)

      if offset.positive? && offset <= output.length
        # Copy match from output history
        match_str = output.slice(-offset, [ml, offset].min) || ""
        # If match is longer than offset, we need to copy byte by byte
        while match_str.length < ml && output.length.positive?
          match_str << match_str[-offset] if offset <= match_str.length
        end
        output << match_str.slice(0, ml)
      end
    end
  end

  # Copy remaining literals (last sequence has no match)
  if lit_pos < literals.length
    output << literals.slice(lit_pos..-1)
  end

  output
end