@@ -175,15 +175,19 @@ def entries(self) -> list[int]:
175175 self .asif .fh .seek (self .offset + 8 )
176176 return c_asif .uint64 [self .asif ._num_tables ](self .asif .fh )
177177
178- def table (self , index : int ) -> Table :
178+ def table (self , index : int ) -> Table | None :
179179 """Get a table from the directory.
180180
181181 Args:
182182 index: Index of the table in the directory.
183183 """
184184 if index >= self .asif ._num_tables :
185185 raise IndexError ("Table index out of range" )
186- return Table (self .asif , index , self .entries [index ] * self .asif .chunk_size )
186+
187+ if (entry := self .entries [index ]) == 0 :
188+ return None
189+
190+ return Table (self .asif , index , entry * self .asif .chunk_size )
187191
188192
189193class Table :
@@ -251,25 +255,27 @@ def __init__(self, asif: ASIF, reserved: bool = False):
251255 def _read (self , offset : int , length : int ) -> bytes :
252256 result = []
253257 while length :
254- table = self .directory .table (offset // self .asif ._size_per_table )
255-
256- # Calculate the relative chunk index within the table
257- relative_block_index = (offset // self .asif .block_size ) - (table .virtual_offset // self .asif .block_size )
258- relative_chunk_index = relative_block_index // self .asif ._blocks_per_chunk
259-
260- # Calculate the chunk group
261- chunk_group = relative_chunk_index // self .asif ._num_chunks_per_group
262- # Each chunk group has a bitmap entry, so we need to account for that in the entry index
263- entry_index = relative_chunk_index + chunk_group
264-
265- chunk = table .entries [entry_index ] & 0x7FFFFFFFFFFFFF
266-
267- read_length = min (length , self .asif .chunk_size )
268- if chunk == 0 :
258+ if (table := self .directory .table (offset // self .asif ._size_per_table )) is None :
259+ read_length = min (length , self .asif ._size_per_table )
269260 result .append (b"\x00 " * read_length )
270261 else :
271- self .asif .fh .seek (chunk * self .asif .chunk_size )
272- result .append (self .asif .fh .read (read_length ))
262+ # Calculate the relative chunk index within the table
263+ relative_block_index = (offset // self .asif .block_size ) - (table .virtual_offset // self .asif .block_size )
264+ relative_chunk_index = relative_block_index // self .asif ._blocks_per_chunk
265+
266+ # Calculate the chunk group
267+ chunk_group = relative_chunk_index // self .asif ._num_chunks_per_group
268+ # Each chunk group has a bitmap entry, so we need to account for that in the entry index
269+ entry_index = relative_chunk_index + chunk_group
270+
271+ chunk = table .entries [entry_index ] & 0x7FFFFFFFFFFFFF
272+
273+ read_length = min (length , self .asif .chunk_size )
274+ if chunk == 0 :
275+ result .append (b"\x00 " * read_length )
276+ else :
277+ self .asif .fh .seek (chunk * self .asif .chunk_size )
278+ result .append (self .asif .fh .read (read_length ))
273279
274280 offset += read_length
275281 length -= read_length
0 commit comments