Class: Omnizip::Algorithms::LZMA::Lzma1Encoder

Inherits:
Implementations::XZUtils::LZMA2::Encoder show all
Defined in:
lib/omnizip/algorithms/lzma/lzma1_encoder.rb

Overview

Raw LZMA1 stream encoder (one continuous range-coded stream with the end-of-stream marker), reusing the XZ Utils symbol coders. This is the payload for the legacy .lzma container and the lzip member body.

Constant Summary collapse

UINT32_MAX =
0xFFFFFFFF
REPS =
4

Constants inherited from Implementations::XZUtils::LZMA2::Encoder

Implementations::XZUtils::LZMA2::Encoder::COMPRESSED_MAX, Implementations::XZUtils::LZMA2::Encoder::UNCOMPRESSED_CHUNK_MAX, Implementations::XZUtils::LZMA2::Encoder::UNCOMPRESSED_MAX

Constants included from Constants

Constants::BIT_MODEL_TOTAL, Constants::COMPRESSION_LEVEL_DEFAULT, Constants::COMPRESSION_LEVEL_MAX, Constants::COMPRESSION_LEVEL_MIN, Constants::DICT_SIZE_MAX, Constants::DICT_SIZE_MIN, Constants::DIST_ALIGN_BITS, Constants::DIST_ALIGN_SIZE, Constants::DIST_SLOT_FAST_LIMIT, Constants::END_POS_MODEL_INDEX, Constants::EOS_MARKER, Constants::INIT_PROBS, Constants::LEN_HIGH_SYMBOLS, Constants::LEN_LOW_SYMBOLS, Constants::LEN_MID_SYMBOLS, Constants::LIT_SIZE_MAX, Constants::MATCH_LEN_MAX, Constants::MATCH_LEN_MIN, Constants::MOVE_BITS, Constants::NUM_DIRECT_BITS, Constants::NUM_DIST_SLOTS, Constants::NUM_DIST_SLOT_BITS, Constants::NUM_FULL_DISTANCES, Constants::NUM_LEN_HIGH_BITS, Constants::NUM_LEN_LOW_BITS, Constants::NUM_LEN_MID_BITS, Constants::NUM_LEN_TO_POS_STATES, Constants::NUM_LIT_CONTEXT_BITS_MAX, Constants::NUM_LIT_POS_BITS_MAX, Constants::NUM_POS_BITS_MAX, Constants::NUM_STATES, Constants::POS_STATES_MAX, Constants::START_POS_MODEL_INDEX, Constants::TOP

Constants inherited from Implementations::Base::LZMA2EncoderBase

Implementations::Base::LZMA2EncoderBase::COMPRESSED_MAX, Implementations::Base::LZMA2EncoderBase::UNCOMPRESSED_MAX

Instance Attribute Summary

Attributes inherited from Implementations::Base::LZMA2EncoderBase

#dict_size, #lc, #lp, #pb

Instance Method Summary collapse

Methods inherited from Implementations::XZUtils::LZMA2::Encoder

#implementation_name, #initialize

Methods inherited from Implementations::Base::LZMA2EncoderBase

#implementation_name, #initialize, #standalone?

Constructor Details

This class inherits a constructor from Omnizip::Implementations::XZUtils::LZMA2::Encoder

Instance Method Details

#encode(input, emit_eopm: true) ⇒ String

Encode input as a complete standalone LZMA1 stream, terminated by the end-of-stream marker (dist = 0xFFFFFFFF, length 2) so decoders that do not know the size can stop.

rubocop:disable-next Metrics/MethodLength

Parameters:

  • input (String)
  • emit_eopm (Boolean) (defaults to: true)

    omit when the container carries the uncompressed size (the legacy .lzma form)

Returns:

  • (String)

    binary stream



45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/omnizip/algorithms/lzma/lzma1_encoder.rb', line 45

def encode(input, emit_eopm: true)
  @match_finder.reset
  output_buffer = StringIO.new
  output_buffer.set_encoding(Encoding::BINARY)
  encoder = XzRangeEncoder.new(output_buffer)
  @match_finder.feed(input)
  @match_finder.skip(@match_finder.buffer.bytesize)

  start_pos = 0
  @current_start_pos = start_pos
  pos = 0
  while pos < input.bytesize
    encode_queued_symbols(encoder, output_buffer)

    match_pos = start_pos + pos
    distance, length = @optimal.find_optimal(
      match_pos, @match_finder, @state, @state.reps, @models
    )

    if distance == UINT32_MAX || length == 1
      encode_literal(input.getbyte(pos), encoder, match_pos)
      pos += 1
    elsif distance < REPS
      encode_repeated_match(distance, length, encoder, match_pos,
                            match_pos)
      pos += length
    else
      encode_match(distance - REPS, length, encoder, match_pos,
                   match_pos, input)
      pos += length
    end
  end

  encode_eopm(encoder, output_buffer, input.bytesize) if emit_eopm
  encode_queued_symbols(encoder, output_buffer)
  encoder.queue_flush
  encode_queued_symbols(encoder, output_buffer)
  output_buffer.string
end