Class: XABA::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/xaba/container.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data_entriesObject

Returns the value of attribute data_entries.



5
6
7
# File 'lib/xaba/container.rb', line 5

def data_entries
  @data_entries
end

#descriptorsObject

Returns the value of attribute descriptors.



5
6
7
# File 'lib/xaba/container.rb', line 5

def descriptors
  @descriptors
end

#file_headerObject

Returns the value of attribute file_header.



5
6
7
# File 'lib/xaba/container.rb', line 5

def file_header
  @file_header
end

#hash32_entriesObject

Returns the value of attribute hash32_entries.



5
6
7
# File 'lib/xaba/container.rb', line 5

def hash32_entries
  @hash32_entries
end

#hash64_entriesObject

Returns the value of attribute hash64_entries.



5
6
7
# File 'lib/xaba/container.rb', line 5

def hash64_entries
  @hash64_entries
end

Class Method Details

.read(fname) ⇒ Object



56
57
58
59
60
# File 'lib/xaba/container.rb', line 56

def self.read(fname)
  File.open(fname, "rb") do |f|
    new.read(f)
  end
end

Instance Method Details

#read(f) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/xaba/container.rb', line 7

def read(f)
  return if f.nil?

  @file_header = FileHeader.read(f)
  @descriptors = @file_header.local_entry_count.times.map { AssemblyDescriptor.read(f) }
  @hash32_entries = @file_header.local_entry_count.times.map { HashEntry.read(f) }
  @hash64_entries = @file_header.local_entry_count.times.map { HashEntry.read(f) }

  @data_entries = @descriptors.map do |d|
    f.seek d.data_offset
    DataEntry.read(f).tap do |de|
      de.data = f.read(d.data_size - DataEntry::SIZE)
      if d.config_data_offset != 0 && d.config_data_size != 0
        f.seek d.config_data_offset
        de.config = f.read(d.config_data_size)
      end
    end
  end
  self
end

#replace!(idx, newdata) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/xaba/container.rb', line 28

def replace!(idx, newdata)
  compressed_data = LZ4.block_compress(newdata)
  delta = compressed_data.bytesize - data_entries[idx].data.bytesize
  descriptors[idx].data_size += delta
  data_entries[idx].original_size = newdata.bytesize
  data_entries[idx].data = compressed_data

  descriptors[idx + 1..].each do |d|
    d.data_offset += delta
  end
end

#write(f) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/xaba/container.rb', line 40

def write(f)
  f.write @file_header.pack
  @descriptors.each do |d|
    f.write d.pack
  end
  @hash32_entries.each do |e|
    f.write e.pack
  end
  @hash64_entries.each do |e|
    f.write e.pack
  end
  @data_entries.each do |e|
    f.write e.pack
  end
end