Class: Omnizip::Formats::XzImpl::StreamHeader

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

Overview

XZ Stream Header encoder Based on XZ Utils stream_flags_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) ⇒ StreamHeader

Returns a new instance of StreamHeader.



15
16
17
# File 'lib/omnizip/formats/xz_impl/stream_header.rb', line 15

def initialize(check_type: CHECK_CRC64)
  @check_type = check_type
end

Instance Attribute Details

#check_typeObject (readonly)

Returns the value of attribute check_type.



13
14
15
# File 'lib/omnizip/formats/xz_impl/stream_header.rb', line 13

def check_type
  @check_type
end

Instance Method Details

#encodeObject

Encode stream header (12 bytes total) Format:

- Magic (6 bytes): FD 37 7A 58 5A 00
- Stream Flags (2 bytes): version + check type
- CRC32 (4 bytes): CRC32 of Stream Flags


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/omnizip/formats/xz_impl/stream_header.rb', line 24

def encode
  output = String.new(encoding: Encoding::BINARY)

  # Write magic bytes
  output << MAGIC.pack("C*")

  # Write stream flags (2 bytes)
  flags = encode_stream_flags
  output << flags

  # Write CRC32 of stream flags
  crc = Zlib.crc32(flags)
  output << [crc].pack("V") # Little-endian uint32

  output
end

#encode_stream_flagsObject



41
42
43
44
45
46
47
48
49
# File 'lib/omnizip/formats/xz_impl/stream_header.rb', line 41

def encode_stream_flags
  # Stream Flags format:
  #   Byte 0: Reserved (must be 0x00)
  #   Byte 1: Check type
  flags = String.new(encoding: Encoding::BINARY)
  flags << "\x00" # Reserved byte
  flags << [@check_type].pack("C")
  flags
end