Class: Omnizip::Implementations::SevenZip::LZMA::StateMachine

Inherits:
Algorithms::LZMA::State show all
Defined in:
lib/omnizip/implementations/seven_zip/lzma/state_machine.rb

Overview

7-Zip LZMA SDK state machine implementation.

This is the original SdkStateMachine moved from algorithms/lzma/sdk_state_machine.rb to the new namespace structure.

Ported from 7-Zip LZMA SDK by Igor Pavlov.

Constant Summary collapse

CATEGORY_LITERAL =

State categories (SDK classification)

:literal
CATEGORY_MATCH =

States 0-6

:match
CATEGORY_REP =

States 7-9

:rep
CATEGORY_SHORT_REP =

State 8, 11

:short_rep

Constants inherited from Algorithms::LZMA::State

Algorithms::LZMA::State::LIT_STATES, Algorithms::LZMA::State::MATCH_STATES, Algorithms::LZMA::State::NUM_STATES, Algorithms::LZMA::State::REP_STATES, Algorithms::LZMA::State::SHORT_REP_STATES

Instance Attribute Summary

Attributes inherited from Algorithms::LZMA::State

#index

Instance Method Summary collapse

Methods inherited from Algorithms::LZMA::State

#initialize, #literal?, #match?, #reset, #to_i, #update_literal, #update_match, #update_rep, #update_short_rep

Constructor Details

This class inherits a constructor from Omnizip::Algorithms::LZMA::State

Instance Method Details

#categorySymbol

Get state category

Categorizes states for debugging and encoder logic. The SDK doesn't expose this directly but uses state ranges in various encoding decisions.

Returns:

  • (Symbol)

    State category



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/omnizip/implementations/seven_zip/lzma/state_machine.rb', line 100

def category
  case @index
  when 0..6
    CATEGORY_LITERAL
  when 7, 10
    CATEGORY_MATCH
  when 8, 11
    CATEGORY_REP
  when 9
    CATEGORY_SHORT_REP
  else
    raise "Invalid state: #{@index}"
  end
end

#dupStateMachine

Create a copy of this state

Overrides parent to return StateMachine instance

Returns:



120
121
122
# File 'lib/omnizip/implementations/seven_zip/lzma/state_machine.rb', line 120

def dup
  StateMachine.new(@index)
end

#is_char_state?Boolean

Check if current state is a character state

Character states (0-6) occur after literal encoding. The SDK uses this to determine probability model selection. This is SDK's IsCharState() macro.

Returns:

  • (Boolean)

    True if state < 7



47
48
49
# File 'lib/omnizip/implementations/seven_zip/lzma/state_machine.rb', line 47

def is_char_state?
  @index < 7
end

#literal_stateInteger

Get literal state index for probability model selection

The SDK uses a simplified state value for literal encoding:

  • States 0-3 map to themselves (0-3)
  • States 4-6 map to 4-6
  • States 7+ map to state - 3 (4-9)

This creates 10 possible literal contexts (0-9) from 12 states. From LzmaEnc.c: litState = (state < 4) ? state : (state - (state < 10 ? 3 : 6))

Returns:

  • (Integer)

    Literal state index (0-9)



69
70
71
72
73
74
75
76
77
# File 'lib/omnizip/implementations/seven_zip/lzma/state_machine.rb', line 69

def literal_state
  if @index < 4
    @index
  elsif @index < 10
    @index - 3
  else
    @index - 6
  end
end

#use_matched_literal?Boolean

Check if matched literal mode should be used

XZ Utils logic (lzma_decoder.c, lzma_common.h):

  • if (is_literal_state(state)) → use UNMATCHED literal
  • else → use MATCHED literal
  • is_literal_state(state) = (state < LIT_STATES) where LIT_STATES = 7
  • States 0-6: literal states (unmatched)
  • States 7-11: non-literal states (matched after rep/match)

Returns:

  • (Boolean)

    True if state >= 7 (non-literal state)



89
90
91
# File 'lib/omnizip/implementations/seven_zip/lzma/state_machine.rb', line 89

def use_matched_literal?
  @index >= 7
end

#valueInteger

Get state value (alias for index)

Returns:

  • (Integer)

    Current state index



54
55
56
# File 'lib/omnizip/implementations/seven_zip/lzma/state_machine.rb', line 54

def value
  @index
end

#would_use_matched_literal?Boolean

Check if state would use matched literal after match

Helper method for encoder to determine encoding path. Checks if encoding a match NOW would result in matched literal NEXT.

Returns:

  • (Boolean)

    True if state would transition to matched literal state



130
131
132
133
134
# File 'lib/omnizip/implementations/seven_zip/lzma/state_machine.rb', line 130

def would_use_matched_literal?
  # After a match, we transition to MATCH_STATES[@index]
  next_state = MATCH_STATES[@index]
  next_state >= 7
end