python-adfs
changeset 61:2b6d3c4cafe0
Used more "portable" ways to read unsigned bytes and words, thanks to the
struct module.
| author | David Boddie <david@boddie.org.uk> |
|---|---|
| date | Sun Apr 10 01:31:57 2005 +0200 |
| parents | e979580ce9c6 |
| children | d4f12a23145a |
| files | ADFSlib.py |
| diffstat | 1 files changed, 58 insertions(+), 23 deletions(-) [+] |
line diff
1.1 --- a/ADFSlib.py Mon Jul 21 03:27:20 2003 +0200 1.2 +++ b/ADFSlib.py Sun Apr 10 01:31:57 2005 +0200 1.3 @@ -30,7 +30,7 @@ 1.4 __version__ = "0.21" 1.5 1.6 1.7 -import os, string 1.8 +import os, string, struct 1.9 1.10 1.11 INFORM = 0 1.12 @@ -76,7 +76,9 @@ 1.13 self.ntracks = 160 1.14 self.nsectors = 16 # per track 1.15 self.sector_size = 256 # in bytes 1.16 - interleave = 0 # 1 1.17 + # Interleave was originally 1 then 0 (from 1.18 + # ~/Private/ADFS/LFormat.htm). 1.19 + interleave = 1 1.20 self.disc_type = 'adl' 1.21 1.22 elif length == 819200: 1.23 @@ -156,6 +158,32 @@ 1.24 self.root_name, self.files = self.read_old_catalogue(2*self.sector_size) 1.25 1.26 1.27 + # Little endian reading 1.28 + 1.29 + def read_signed_word(self, s): 1.30 + 1.31 + return struct.unpack("<i", s)[0] 1.32 + 1.33 + def read_unsigned_word(self, s): 1.34 + 1.35 + return struct.unpack("<I", s)[0] 1.36 + 1.37 + def read_signed_byte(self, s): 1.38 + 1.39 + return struct.unpack("<b", s)[0] 1.40 + 1.41 + def read_unsigned_byte(self, s): 1.42 + 1.43 + return struct.unpack("<B", s)[0] 1.44 + 1.45 + def read_unsigned_half_word(self, s): 1.46 + 1.47 + return struct.unpack("<H", s)[0] 1.48 + 1.49 + def read_signed_half_word(self, s): 1.50 + 1.51 + return struct.unpack("<h", s)[0] 1.52 + 1.53 def str2num(self, size, s): 1.54 1.55 i = 0 1.56 @@ -356,14 +384,14 @@ 1.57 density = 'unknown' 1.58 1.59 # Length of ID fields in the disc map 1.60 - idlen = self.str2num(1, self.sectors[offset + 4]) 1.61 + idlen = self.read_unsigned_byte(self.sectors[offset + 4]) 1.62 # Number of bytes per map bit. 1.63 - bytes_per_bit = 2 ** self.str2num(1, self.sectors[offset + 5]) 1.64 + bytes_per_bit = 2 ** self.read_unsigned_byte(self.sectors[offset + 5]) 1.65 # LowSector 1.66 # StartUp 1.67 # LinkBits 1.68 # BitSize (size of ID field?) 1.69 - bit_size = self.str2num(1, self.sectors[offset + 6 : offset + 7]) 1.70 + bit_size = self.read_unsigned_byte(self.sectors[offset + 6 : offset + 7]) 1.71 #print "Bit size: %s" % hex(bit_size) 1.72 # RASkew 1.73 # BootOpt 1.74 @@ -376,9 +404,9 @@ 1.75 # SequenceSides 1.76 # DoubleStep 1.77 # DiscSize 1.78 - disc_size = self.str2num(4, self.sectors[offset + 16 : offset + 20]) 1.79 + disc_size = self.read_unsigned_word(self.sectors[offset + 16 : offset + 20]) 1.80 # DiscId 1.81 - disc_id = self.str2num(2, self.sectors[offset + 20 : offset + 22]) 1.82 + disc_id = self.read_unsigned_half_word(self.sectors[offset + 20 : offset + 22]) 1.83 # DiscName 1.84 disc_name = string.strip(self.sectors[offset + 22 : offset + 32]) 1.85 1.86 @@ -392,7 +420,7 @@ 1.87 def read_disc_info(self): 1.88 1.89 checksum = ord(self.sectors[0]) 1.90 - first_free = self.str2num(2, self.sectors[1:3]) 1.91 + first_free = self.read_unsigned_half_word(self.sectors[1:3]) 1.92 1.93 if self.disc_type == 'adE': 1.94 1.95 @@ -483,7 +511,7 @@ 1.96 1.97 # If there is enough space left in this zone to allow 1.98 # further fragments then read the next two bytes. 1.99 - value = self.str2num(2, self.sectors[a:a+2]) 1.100 + value = self.read_unsigned_half_word(self.sectors[a:a+2]) 1.101 1.102 entry = value & 0x7fff 1.103 1.104 @@ -606,7 +634,7 @@ 1.105 1.106 # Start by reading the offset in bits from the start of the header 1.107 # of the first item of free space in the map. 1.108 - offset = self.str2num(2, self.sectors[a:a+2]) 1.109 + offset = self.read_unsigned_half_word(self.sectors[a:a+2]) 1.110 1.111 # The top bit is apparently always set, so mask it off and convert 1.112 # the result into bytes. * Not sure if this is the case for 1.113 @@ -626,7 +654,7 @@ 1.114 while a < next_zone: 1.115 1.116 # Read the offset to the next free fragment in this zone. 1.117 - offset = self.str2num(2, self.sectors[a:a+2]) 1.118 + offset = self.read_unsigned_half_word(self.sectors[a:a+2]) 1.119 1.120 # Convert this to a byte offset. 1.121 next = ((offset & 0x7fff) >> 3) 1.122 @@ -638,7 +666,7 @@ 1.123 1.124 c = b + 1 1.125 1.126 - value = self.str2num(1, self.sectors[b]) 1.127 + value = self.read_unsigned_byte(self.sectors[b]) 1.128 1.129 if (value & 0x80) != 0: 1.130 1.131 @@ -674,6 +702,13 @@ 1.132 1.133 elif self.disc_type == 'adEbig': 1.134 1.135 + # I can't remember where the rationale for this calculation 1.136 + # came from or where the necessary information was obtained. 1.137 + # It probably came from one of the WSS files, such as 1.138 + # Formats.htm or Formats2.htm which imply that the F format 1.139 + # uses 512 byte sectors (see the 0x200 value below) and 1.140 + # indicate that F format uses 4 zones rather than 1. 1.141 + 1.142 upper = (entry & 0x7f00) >> 8 1.143 1.144 if upper > 1: 1.145 @@ -804,7 +839,7 @@ 1.146 3, self.sectors[self.sector_size-4:self.sector_size-1] 1.147 ) 1.148 1.149 - checksum0 = self.str2num(1, self.sectors[self.sector_size-1]) 1.150 + checksum0 = self.read_unsigned_byte(self.sectors[self.sector_size-1]) 1.151 1.152 base = self.sector_size 1.153 1.154 @@ -820,9 +855,9 @@ 1.155 2, self.sectors[base+self.sector_size-5:base+self.sector_size-3] 1.156 ) 1.157 1.158 - boot = self.str2num(1, self.sectors[base+self.sector_size-3]) 1.159 + boot = self.read_unsigned_byte(self.sectors[base+self.sector_size-3]) 1.160 1.161 - checksum1 = self.str2num(1, self.sectors[base+self.sector_size-1]) 1.162 + checksum1 = self.read_unsigned_byte(self.sectors[base+self.sector_size-1]) 1.163 1.164 1.165 def read_old_catalogue(self, base): 1.166 @@ -859,9 +894,9 @@ 1.167 1.168 name = self.safe(self.sectors[head+p:head+p+10]) 1.169 1.170 - load = self.str2num(4, self.sectors[head+p+10:head+p+14]) 1.171 - exe = self.str2num(4, self.sectors[head+p+14:head+p+18]) 1.172 - length = self.str2num(4, self.sectors[head+p+18:head+p+22]) 1.173 + load = self.read_unsigned_word(self.sectors[head+p+10:head+p+14]) 1.174 + exe = self.read_unsigned_word(self.sectors[head+p+14:head+p+18]) 1.175 + length = self.read_unsigned_word(self.sectors[head+p+18:head+p+22]) 1.176 1.177 if self.disc_type == 'adD': 1.178 inddiscadd = 256 * self.str2num( 1.179 @@ -872,7 +907,7 @@ 1.180 3, self.sectors[head+p+22:head+p+25] 1.181 ) 1.182 1.183 - olddirobseq = self.str2num(1, self.sectors[head+p+25]) 1.184 + olddirobseq = self.read_unsigned_byte(self.sectors[head+p+25]) 1.185 1.186 #print string.expandtabs( 1.187 # "%s\t%s\t%s\t%s" % ( 1.188 @@ -1081,9 +1116,9 @@ 1.189 1.190 #print hex(head+p), name 1.191 1.192 - load = self.str2num(4, self.sectors[head+p+10:head+p+14]) 1.193 - exe = self.str2num(4, self.sectors[head+p+14:head+p+18]) 1.194 - length = self.str2num(4, self.sectors[head+p+18:head+p+22]) 1.195 + load = self.read_unsigned_word(self.sectors[head+p+10:head+p+14]) 1.196 + exe = self.read_unsigned_word(self.sectors[head+p+14:head+p+18]) 1.197 + length = self.read_unsigned_word(self.sectors[head+p+18:head+p+22]) 1.198 1.199 #print hex(ord(self.sectors[head+p+22])), \ 1.200 # hex(ord(self.sectors[head+p+23])), \ 1.201 @@ -1092,7 +1127,7 @@ 1.202 inddiscadd = self.read_new_address( 1.203 self.sectors[head+p+22:head+p+25] 1.204 ) 1.205 - newdiratts = self.str2num(1, self.sectors[head+p+25]) 1.206 + newdiratts = self.read_unsigned_byte(self.sectors[head+p+25]) 1.207 1.208 if inddiscadd == -1: 1.209
