Class: Omnizip::Implementations::SevenZip::LZMA::Encoder
- Inherits:
-
Object
- Object
- Omnizip::Implementations::SevenZip::LZMA::Encoder
- Includes:
- Algorithms::LZMA::Constants
- Defined in:
- lib/omnizip/implementations/seven_zip/lzma/encoder.rb
Overview
7-Zip LZMA SDK encoder implementation.
This is the original SdkEncoder moved from algorithms/lzma/sdk_encoder.rb to the new namespace structure.
Ported from 7-Zip LZMA SDK by Igor Pavlov.
Constant Summary
Constants included from Algorithms::LZMA::Constants
Algorithms::LZMA::Constants::BIT_MODEL_TOTAL, Algorithms::LZMA::Constants::COMPRESSION_LEVEL_DEFAULT, Algorithms::LZMA::Constants::COMPRESSION_LEVEL_MAX, Algorithms::LZMA::Constants::COMPRESSION_LEVEL_MIN, Algorithms::LZMA::Constants::DICT_SIZE_MAX, Algorithms::LZMA::Constants::DICT_SIZE_MIN, Algorithms::LZMA::Constants::DIST_ALIGN_BITS, Algorithms::LZMA::Constants::DIST_ALIGN_SIZE, Algorithms::LZMA::Constants::DIST_SLOT_FAST_LIMIT, Algorithms::LZMA::Constants::END_POS_MODEL_INDEX, Algorithms::LZMA::Constants::EOS_MARKER, Algorithms::LZMA::Constants::INIT_PROBS, Algorithms::LZMA::Constants::LEN_HIGH_SYMBOLS, Algorithms::LZMA::Constants::LEN_LOW_SYMBOLS, Algorithms::LZMA::Constants::LEN_MID_SYMBOLS, Algorithms::LZMA::Constants::LIT_SIZE_MAX, Algorithms::LZMA::Constants::MATCH_LEN_MAX, Algorithms::LZMA::Constants::MATCH_LEN_MIN, Algorithms::LZMA::Constants::MOVE_BITS, Algorithms::LZMA::Constants::NUM_DIRECT_BITS, Algorithms::LZMA::Constants::NUM_DIST_SLOTS, Algorithms::LZMA::Constants::NUM_DIST_SLOT_BITS, Algorithms::LZMA::Constants::NUM_FULL_DISTANCES, Algorithms::LZMA::Constants::NUM_LEN_HIGH_BITS, Algorithms::LZMA::Constants::NUM_LEN_LOW_BITS, Algorithms::LZMA::Constants::NUM_LEN_MID_BITS, Algorithms::LZMA::Constants::NUM_LEN_TO_POS_STATES, Algorithms::LZMA::Constants::NUM_LIT_CONTEXT_BITS_MAX, Algorithms::LZMA::Constants::NUM_LIT_POS_BITS_MAX, Algorithms::LZMA::Constants::NUM_POS_BITS_MAX, Algorithms::LZMA::Constants::NUM_STATES, Algorithms::LZMA::Constants::POS_STATES_MAX, Algorithms::LZMA::Constants::START_POS_MODEL_INDEX, Algorithms::LZMA::Constants::TOP
Instance Attribute Summary collapse
-
#dict_size ⇒ Object
readonly
Returns the value of attribute dict_size.
-
#lc ⇒ Object
readonly
Returns the value of attribute lc.
-
#lp ⇒ Object
readonly
Returns the value of attribute lp.
-
#pb ⇒ Object
readonly
Returns the value of attribute pb.
Instance Method Summary collapse
-
#encode_stream(data) ⇒ Array<String, Integer>
Encode a stream of data.
-
#initialize(output, options = {}) ⇒ Encoder
constructor
Initialize the SDK-compatible encoder.
Constructor Details
#initialize(output, options = {}) ⇒ Encoder
Initialize the SDK-compatible encoder
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/omnizip/implementations/seven_zip/lzma/encoder.rb', line 48 def initialize(output, = {}) @output = output @lc = .fetch(:lc, 3) @lp = .fetch(:lp, 0) @pb = .fetch(:pb, 2) @dict_size = .fetch(:dict_size, 1 << 16) # 64KB default @level = .fetch(:level, 5) @raw_mode = .fetch(:raw_mode, false) # NEW: skip header/EOS for LZMA2 validate_parameters init_models init_coders end |
Instance Attribute Details
#dict_size ⇒ Object (readonly)
Returns the value of attribute dict_size.
36 37 38 |
# File 'lib/omnizip/implementations/seven_zip/lzma/encoder.rb', line 36 def dict_size @dict_size end |
#lc ⇒ Object (readonly)
Returns the value of attribute lc.
36 37 38 |
# File 'lib/omnizip/implementations/seven_zip/lzma/encoder.rb', line 36 def lc @lc end |
#lp ⇒ Object (readonly)
Returns the value of attribute lp.
36 37 38 |
# File 'lib/omnizip/implementations/seven_zip/lzma/encoder.rb', line 36 def lp @lp end |
#pb ⇒ Object (readonly)
Returns the value of attribute pb.
36 37 38 |
# File 'lib/omnizip/implementations/seven_zip/lzma/encoder.rb', line 36 def pb @pb end |
Instance Method Details
#encode_stream(data) ⇒ Array<String, Integer>
Encode a stream of data
Main encoding loop following SDK's LzmaEnc_CodeOneBlock logic:
- Initialize match finder with data
- Process each position: find matches, encode literals/matches
- Write EOS marker
- Flush range encoder
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/omnizip/implementations/seven_zip/lzma/encoder.rb', line 72 def encode_stream(data) input_data = data.is_a?(String) ? data : data.read # Force binary encoding to handle binary data properly # Duplicate to avoid modifying frozen strings input_data = input_data.dup.force_encoding(Encoding::BINARY) # Write LZMA header write_header(input_data.bytesize) unless @raw_mode # Initialize range encoder (7-Zip SDK version) @range_encoder = RangeEncoder.new(@output) # Initialize match finder with SDK configuration match_finder_config = Omnizip::Algorithms::LZMA::MatchFinderConfig.sdk_config( dict_size: @dict_size, level: @level, ) @match_finder = Omnizip::Algorithms::LZMA::MatchFinderFactory.create(match_finder_config) # Initialize state and dictionary @state = StateMachine.new @dict = +"" # Mutable string for dictionary @pos = 0 # Initialize repeat distances (all 1 initially, as in SDK) @reps = [1, 1, 1, 1] # Main encoding loop while @pos < input_data.bytesize # Find best match at current position match = @match_finder.find_longest_match(input_data, @pos) # Decide: literal vs match if should_encode_match?(match) encode_match(match, input_data) else encode_literal(input_data[@pos].ord, input_data) end end # Write EOS marker and flush # For LZMA2: skip EOS marker but DO flush the range encoder # The range encoder flush outputs pending bytes needed by decoder # LZMA2 uses CONTROL_END (0x00) to signal end of stream instead of LZMA EOS encode_eos_marker unless @raw_mode # Skip EOS in raw mode @range_encoder.flush # Always flush to output pending range encoder bytes # Return tuple for LZMA2: [data, bytes_for_decode] # For raw mode, return actual decode bytes (excluding flush padding) if @raw_mode [@output.string, @range_encoder.bytes_for_decode] elsif @output.respond_to?(:string) # For File output, just return bytes written (don't try to read back) # For StringIO, return the string and its size [@output.string, @output.string.bytesize] else [@range_encoder.bytes_for_decode, @range_encoder.bytes_for_decode] end end |