Class: Omnizip::Algorithms::LZMA::LZMAState

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/lzma/lzma_state.rb

Overview

LZMA State Machine Ported from XZ Utils lzma_common.h and lzma_decoder.c

Constant Summary collapse

TRANSITIONS =

State transition table (from lzma_decoder.c)

{
  # Literal after literal (matches XZ Utils update_literal macro)
  update_literal: {
    0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 1, 5 => 2,
    6 => 3, 7 => 4, 8 => 5, 9 => 6, 10 => 4, 11 => 5
  }.freeze,

  # Matched literal (literal after match, matches XZ Utils update_literal_matched macro)
  # Only called when previous state was NOT a literal (states 7-11)
  update_literal_matched: {
    0 => 0, 1 => 0, 2 => 0, 3 => 0, 4 => 1, 5 => 2,
    6 => 3, 7 => 4, 8 => 5, 9 => 6, 10 => 4, 11 => 5
  }.freeze,

  # Regular match
  update_match: {
    0 => 7, 1 => 7, 2 => 7, 3 => 7, 4 => 7, 5 => 7,
    6 => 7, 7 => 10, 8 => 10, 9 => 10, 10 => 10, 11 => 10
  }.freeze,

  # Repeat match
  update_rep: {
    0 => 8, 1 => 8, 2 => 8, 3 => 8, 4 => 8, 5 => 8,
    6 => 8, 7 => 11, 8 => 11, 9 => 11, 10 => 11, 11 => 11
  }.freeze,

  # Short repeat (length=1)
  update_short_rep: {
    0 => 9, 1 => 9, 2 => 9, 3 => 9, 4 => 9, 5 => 9,
    6 => 9, 7 => 11, 8 => 11, 9 => 11, 10 => 11, 11 => 11
  }.freeze,

  # Long repeat (length>1)
  # Ported from XZ Utils: state < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP
  # where LIT_STATES=7, STATE_LIT_LONGREP=8, STATE_NONLIT_REP=11
  update_long_rep: {
    0 => 8, 1 => 8, 2 => 8, 3 => 8, 4 => 8, 5 => 8,
    6 => 8, 7 => 11, 8 => 11, 9 => 11, 10 => 11, 11 => 11
  }.freeze,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = 0) ⇒ LZMAState

Returns a new instance of LZMAState.



53
54
55
56
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 53

def initialize(value = 0)
  @value = value
  @reps = [0, 0, 0, 0] # Initial repeat distances (matches XZ Utils)
end

Instance Attribute Details

#repsObject (readonly)

Returns the value of attribute reps.



51
52
53
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 51

def reps
  @reps
end

#valueObject (readonly)

Returns the value of attribute value.



51
52
53
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 51

def value
  @value
end

Instance Method Details

#rotate_reps!(distance) ⇒ Object

Repeat distance rotation



100
101
102
103
104
105
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 100

def rotate_reps!(distance)
  @reps[3] = @reps[2]
  @reps[2] = @reps[1]
  @reps[1] = @reps[0]
  @reps[0] = distance
end

#update_literal!Object

After encoding a literal



59
60
61
62
63
64
65
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 59

def update_literal!
  @value = if use_matched_literal?
             TRANSITIONS[:update_literal_matched][@value]
           else
             TRANSITIONS[:update_literal][@value]
           end
end

#update_long_rep!Object

After encoding a long rep (length>1) Ported from XZ Utils update_long_rep macro



86
87
88
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 86

def update_long_rep!
  @value = TRANSITIONS[:update_long_rep][@value]
end

#update_match!(distance) ⇒ Object

After encoding a regular match



68
69
70
71
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 68

def update_match!(distance)
  @value = TRANSITIONS[:update_match][@value]
  rotate_reps!(distance)
end

#update_rep!(rep_index) ⇒ Object

After encoding a repeat match



74
75
76
77
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 74

def update_rep!(rep_index)
  @value = TRANSITIONS[:update_rep][@value]
  rotate_reps_for_rep!(rep_index)
end

#update_short_rep!Object

After encoding a short rep (length=1)



80
81
82
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 80

def update_short_rep!
  @value = TRANSITIONS[:update_short_rep][@value]
end

#use_matched_literal?Boolean

Check if we should use matched literal encoding XZ Utils logic: is_literal_state(state) = (state < LIT_STATES) where LIT_STATES = 7 States 0-6: literal states (use unmatched literal) States 7-11: non-literal states (use matched literal)

Returns:

  • (Boolean)


95
96
97
# File 'lib/omnizip/algorithms/lzma/lzma_state.rb', line 95

def use_matched_literal?
  @value >= 7
end