Class: Omnizip::Formats::XzImpl::BlockEncoder

Inherits:
Object
  • Object
show all
Includes:
Omnizip::Formats::XzConst
Defined in:
lib/omnizip/formats/xz_impl/block_encoder.rb

Overview

XZ Block encoder Based on XZ Utils block_header_encoder.c

Constant Summary

Constants included from Omnizip::Formats::XzConst

Omnizip::Formats::XzConst::BACKWARD_SIZE_MAX, Omnizip::Formats::XzConst::BACKWARD_SIZE_MIN, Omnizip::Formats::XzConst::BLOCK_HEADER_SIZE_MAX, Omnizip::Formats::XzConst::BLOCK_HEADER_SIZE_MIN, Omnizip::Formats::XzConst::CHECK_CRC32, Omnizip::Formats::XzConst::CHECK_CRC64, Omnizip::Formats::XzConst::CHECK_NONE, Omnizip::Formats::XzConst::CHECK_SHA256, Omnizip::Formats::XzConst::FILTERS_MAX, Omnizip::Formats::XzConst::FILTER_LZMA2, Omnizip::Formats::XzConst::FOOTER_MAGIC, Omnizip::Formats::XzConst::INDEX_INDICATOR, Omnizip::Formats::XzConst::MAGIC, Omnizip::Formats::XzConst::STREAM_FLAGS_SIZE, Omnizip::Formats::XzConst::STREAM_FOOTER_SIZE, Omnizip::Formats::XzConst::STREAM_HEADER_SIZE, Omnizip::Formats::XzConst::VLI_BYTES_MAX, Omnizip::Formats::XzConst::VLI_UNKNOWN

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(check_type: CHECK_CRC64, dict_size: 8 * 1024 * 1024, include_block_sizes: false) ⇒ BlockEncoder

Returns a new instance of BlockEncoder.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/omnizip/formats/xz_impl/block_encoder.rb', line 16

def initialize(check_type: CHECK_CRC64, dict_size: 8 * 1024 * 1024,
include_block_sizes: false)
  @check_type = check_type
  @dict_size = dict_size
  @uncompressed_size = 0
  @compressed_size = 0
  # For simple single-block streams, omit size fields from block header
  # This matches XZ Utils behavior for basic compression
  # Multi-block streams should set this to true for seeking support
  @include_block_sizes = include_block_sizes
end

Instance Attribute Details

#compressed_sizeObject (readonly)

Returns the value of attribute compressed_size.



14
15
16
# File 'lib/omnizip/formats/xz_impl/block_encoder.rb', line 14

def compressed_size
  @compressed_size
end

#uncompressed_sizeObject (readonly)

Returns the value of attribute uncompressed_size.



14
15
16
# File 'lib/omnizip/formats/xz_impl/block_encoder.rb', line 14

def uncompressed_size
  @uncompressed_size
end

Instance Method Details

#encode_block(input_data) ⇒ Object

Encode a block with LZMA2 compression Returns: { header: String, data: String, padding: String, check: String, compressed_size: Integer, uncompressed_size: Integer }



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/omnizip/formats/xz_impl/block_encoder.rb', line 30

def encode_block(input_data)
  @uncompressed_size = input_data.bytesize

  # Compress data with LZMA2
  compressed_data = compress_with_lzma2(input_data)
  @compressed_size = compressed_data.bytesize

  # Encode block header
  header = encode_block_header

  # Calculate check value
  check = calculate_check(input_data)

  # Add padding to align block
  padding = calculate_padding(header.bytesize + compressed_data.bytesize)

  {
    header: header,
    data: compressed_data,
    padding: "\x00" * padding,
    check: check,
    compressed_size: @compressed_size,
    uncompressed_size: @uncompressed_size,
  }
end

#unpadded_sizeObject

Get unpadded block size (for index)



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/omnizip/formats/xz_impl/block_encoder.rb', line 57

def unpadded_size
  # Unpadded size = actual header + compressed data + check
  # Note: "Unpadded" means EXCLUDING the block padding (padding after compressed data)
  # but INCLUDES the check value
  actual_header_size = calculate_actual_header_size
  check_size = case @check_type
               when CHECK_NONE then 0
               when CHECK_CRC32 then 4
               when CHECK_CRC64 then 8
               else 8
               end
  actual_header_size + @compressed_size + check_size
end