Class: MppReader::Cfbf::Fat

Inherits:
Object
  • Object
show all
Defined in:
lib/mpp_reader/cfbf/fat.rb

Overview

The File Allocation Table: maps each sector index to the next sector in its chain. Built from the header DIFAT plus chained DIFAT sectors.

Instance Method Summary collapse

Constructor Details

#initialize(data, header) ⇒ Fat

Returns a new instance of Fat.



6
7
8
9
10
# File 'lib/mpp_reader/cfbf/fat.rb', line 6

def initialize(data, header)
  @data = data
  @header = header
  @entries = build_entries
end

Instance Method Details

#chain(start) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mpp_reader/cfbf/fat.rb', line 21

def chain(start)
  sectors = []
  seen = {}
  sector = start
  while sector != ENDOFCHAIN
    raise CorruptFileError, "FAT chain cycle at sector #{sector}" if seen[sector]
    if sector >= @entries.size
      raise CorruptFileError, "FAT chain references invalid sector #{sector}"
    end
    seen[sector] = true
    sectors << sector
    sector = @entries[sector]
  end
  sectors
end

#read_chain(start) ⇒ Object



37
38
39
# File 'lib/mpp_reader/cfbf/fat.rb', line 37

def read_chain(start)
  chain(start).map { |s| sector_bytes(s) }.join
end

#sector_bytes(sector) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/mpp_reader/cfbf/fat.rb', line 12

def sector_bytes(sector)
  offset = (sector + 1) * @header.sector_size
  bytes = @data.byteslice(offset, @header.sector_size)
  if bytes.nil? || bytes.empty?
    raise CorruptFileError, "sector #{sector} beyond end of file"
  end
  bytes
end