Added error checking for valid VTOC

This commit is contained in:
Rob McMullen 2016-03-28 15:48:46 -07:00
parent 66e7c14ddb
commit 5465edcfdf
2 changed files with 25 additions and 0 deletions

View File

@ -202,6 +202,23 @@ class AtariDosDiskImage(DiskImageBase):
('unused','<u2'), ('unused','<u2'),
]) ])
def calc_vtoc_code(self):
# From AA post: http://atariage.com/forums/topic/179868-mydos-vtoc-size/
num = 1 + (self.total_sectors + 80) / (self.header.sector_size * 8)
if header.sector_size == 128:
if num == 1:
code = 2
else:
if num & 1:
num += 1
code = ((num + 1) / 2) + 2
else:
if self.total_sectors < 1024:
code = 2
else:
code = 2 + num
return code
def get_vtoc(self): def get_vtoc(self):
data, style = self.get_sectors(360) data, style = self.get_sectors(360)
values = data[0:5].view(dtype=self.vtoc_type)[0] values = data[0:5].view(dtype=self.vtoc_type)[0]
@ -211,7 +228,11 @@ class AtariDosDiskImage(DiskImageBase):
else: else:
num = (code * 2) - 3 num = (code * 2) - 3
self.first_vtoc = 360 - num + 1 self.first_vtoc = 360 - num + 1
self.assert_valid_sector(self.first_vtoc)
self.num_vtoc = num self.num_vtoc = num
if num < 0 or num > self.calc_vtoc_code():
raise InvalidDiskImage("Invalid number of VTOC sectors: %d" % num)
self.total_sectors = values[1] self.total_sectors = values[1]
self.unused_sectors = values[2] self.unused_sectors = values[2]
if self.header.image_size == 133120: if self.header.image_size == 133120:

View File

@ -130,6 +130,10 @@ class DiskImageBase(object):
self.get_directory() self.get_directory()
self.check_sane() self.check_sane()
def assert_valid_sector(self, sector):
if not self.header.sector_is_valid(sector):
raise ByteNotInFile166("Sector %d out of range" % sector)
def check_sane(self): def check_sane(self):
if not self.all_sane: if not self.all_sane:
raise InvalidDiskImage("Invalid directory entries; may be boot disk") raise InvalidDiskImage("Invalid directory entries; may be boot disk")