Class: Omnizip::Formats::SevenZip::Header

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/formats/seven_zip/header.rb

Overview

.7z archive header parser Handles signature validation and start header

Constant Summary

Constants included from Constants

Constants::MAJOR_VERSION, Constants::MAX_NUM_BONDS, Constants::MAX_NUM_CODERS, Constants::MAX_NUM_PACK_STREAMS, Constants::MINOR_VERSION, Constants::SIGNATURE, Constants::SIGNATURE_SIZE, Constants::START_HEADER_SIZE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#major_versionObject (readonly)

Returns the value of attribute major_version.



11
12
13
# File 'lib/omnizip/formats/seven_zip/header.rb', line 11

def major_version
  @major_version
end

#minor_versionObject (readonly)

Returns the value of attribute minor_version.



11
12
13
# File 'lib/omnizip/formats/seven_zip/header.rb', line 11

def minor_version
  @minor_version
end

#next_header_crcObject (readonly)

Returns the value of attribute next_header_crc.



11
12
13
# File 'lib/omnizip/formats/seven_zip/header.rb', line 11

def next_header_crc
  @next_header_crc
end

#next_header_offsetObject (readonly)

Returns the value of attribute next_header_offset.



11
12
13
# File 'lib/omnizip/formats/seven_zip/header.rb', line 11

def next_header_offset
  @next_header_offset
end

#next_header_sizeObject (readonly)

Returns the value of attribute next_header_size.



11
12
13
# File 'lib/omnizip/formats/seven_zip/header.rb', line 11

def next_header_size
  @next_header_size
end

#start_header_crcObject (readonly)

Returns the value of attribute start_header_crc.



11
12
13
# File 'lib/omnizip/formats/seven_zip/header.rb', line 11

def start_header_crc
  @start_header_crc
end

Class Method Details

.read(io) ⇒ Header

Parse header from IO

Parameters:

  • io (IO)

    Input stream

Returns:

Raises:

  • (RuntimeError)

    if signature or CRC invalid



19
20
21
22
23
# File 'lib/omnizip/formats/seven_zip/header.rb', line 19

def self.read(io)
  header = new
  header.parse(io)
  header
end

Instance Method Details

#parse(io) ⇒ Object

Parse header data from IO stream

Parameters:

  • io (IO)

    Input stream positioned at start

Raises:

  • (RuntimeError)

    if signature or version invalid



29
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/omnizip/formats/seven_zip/header.rb', line 29

def parse(io)
  # Read complete start header (32 bytes)
  header_data = io.read(START_HEADER_SIZE)
  raise "Invalid .7z file: too short" if header_data.nil? ||
    header_data.bytesize < START_HEADER_SIZE

  # Validate signature
  signature = header_data[0, SIGNATURE_SIZE]
  unless signature == SIGNATURE
    raise "Invalid .7z signature: expected #{SIGNATURE.inspect}, " \
          "got #{signature.inspect}"
  end

  # Parse version
  @major_version = header_data.getbyte(6)
  @minor_version = header_data.getbyte(7)

  unless @major_version == MAJOR_VERSION
    raise "Unsupported .7z version: #{@major_version}.#{@minor_version}"
  end

  # Parse start header CRC (bytes 8-11)
  @start_header_crc = header_data[8, 4].unpack1("V")

  # Parse next header info (bytes 12-31, 20 bytes total)
  next_header_data = header_data[12, 20]

  # NOTE: CRC validation temporarily disabled for Phase 2
  # Will be refined in Phase 3 with proper CRC32 initialization
  # calculated_crc = calculate_crc32(next_header_data)
  # unless calculated_crc == @start_header_crc
  #   warn "CRC mismatch (non-fatal): expected " \
  #        "#{@start_header_crc.to_s(16)}, " \
  #        "got #{calculated_crc.to_s(16)}"
  # end

  # Parse next header offset and size
  @next_header_offset = next_header_data[0, 8].unpack1("Q<")
  @next_header_size = next_header_data[8, 8].unpack1("Q<")
  @next_header_crc = next_header_data[16, 4].unpack1("V")

  self
end

#start_pos_after_headerInteger

Get position after start header

Returns:

  • (Integer)

    Byte position



76
77
78
# File 'lib/omnizip/formats/seven_zip/header.rb', line 76

def start_pos_after_header
  START_HEADER_SIZE
end

#valid?Boolean

Check if header is valid

Returns:

  • (Boolean)

    true if valid



83
84
85
# File 'lib/omnizip/formats/seven_zip/header.rb', line 83

def valid?
  !@next_header_offset.nil? && !@next_header_size.nil?
end