Class: Cabriolet::LIT::DirectoryBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/lit/directory_builder.rb

Overview

Builds LIT directory structure with AOLL chunks

Constant Summary collapse

DEFAULT_CHUNK_SIZE =

Chunk size for directory entries (8KB)

0x2000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunk_size: DEFAULT_CHUNK_SIZE) ⇒ DirectoryBuilder

Initialize directory builder

Parameters:

  • chunk_size (Integer) (defaults to: DEFAULT_CHUNK_SIZE) —

    Chunk size for directory entries



15
16
17
18
# File 'lib/cabriolet/lit/directory_builder.rb', line 15

def initialize(chunk_size: DEFAULT_CHUNK_SIZE)
  @chunk_size = chunk_size
  @entries = []
end

Instance Attribute Details

#chunk_size ⇒ Object (readonly)

Returns the value of attribute chunk_size.



10
11
12
# File 'lib/cabriolet/lit/directory_builder.rb', line 10

def chunk_size
  @chunk_size
end

#entries ⇒ Object (readonly)

Returns the value of attribute entries.



10
11
12
# File 'lib/cabriolet/lit/directory_builder.rb', line 10

def entries
  @entries
end

Instance Method Details

#add_entry(name:, section:, offset:, size:) ⇒ Object

Add an entry to the directory

Parameters:

  • name (String) —

    Entry name

  • section (Integer) —

    Section number

  • offset (Integer) —

    Offset within section

  • size (Integer) —

    Size in bytes



26
27
28
29
30
31
32
33
# File 'lib/cabriolet/lit/directory_builder.rb', line 26

def add_entry(name:, section:, offset:, size:)
  @entries << {
    name: name,
    section: section,
    offset: offset,
    size: size,
  }
end

#build ⇒ Hash

Build the directory structure

Returns:

  • (Hash) —

    Directory structure with entries and metadata



38
39
40
41
42
43
44
# File 'lib/cabriolet/lit/directory_builder.rb', line 38

def build
  {
    entries: @entries,
    chunk_size: @chunk_size,
    num_chunks: calculate_num_chunks,
  }
end

#build_aoll_chunk ⇒ String

Build AOLL (Archive Object List List) chunk

Returns:

  • (String) —

    Binary AOLL chunk data



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cabriolet/lit/directory_builder.rb', line 49

def build_aoll_chunk
  # Build all entry data first
  entries_data = @entries.map { |entry| encode_entry(entry) }.join

  # Calculate quickref offset (starts after entries data)
  quickref_offset = entries_data.bytesize

  # Build AOLL header
  header = Binary::LITStructures::AOLLHeader.new
  header.tag = Binary::LITStructures::Tags::AOLL
  header.quickref_offset = quickref_offset
  header.current_chunk_low = 0
  header.current_chunk_high = 0
  header.prev_chunk_low = 0xFFFFFFFF
  header.prev_chunk_high = 0xFFFFFFFF
  header.next_chunk_low = 0xFFFFFFFF
  header.next_chunk_high = 0xFFFFFFFF
  header.entries_so_far = @entries.size
  header.reserved = 0
  header.chunk_distance = 0
  header.reserved2 = 0

  header.to_binary_s + entries_data
end

#calculate_size ⇒ Integer

Calculate total size needed for directory

Returns:

  • (Integer) —

    Size in bytes



77
78
79
80
81
82
83
84
# File 'lib/cabriolet/lit/directory_builder.rb', line 77

def calculate_size
  # IFCM header + AOLL chunk + padding
  ifcm_size = Binary::LITStructures::IFCMHeader.new.to_binary_s.bytesize
  aoll_size = build_aoll_chunk.bytesize
  target_size = @chunk_size

  [ifcm_size + aoll_size, target_size].max
end