handle invalid block numbers

This commit is contained in:
Denis Molony 2018-07-23 06:52:41 +10:00
parent d9950b4f60
commit 904db9632c
8 changed files with 86 additions and 72 deletions

View File

@ -293,10 +293,10 @@ public abstract class AbstractFormattedDisk implements FormattedDisk
@Override @Override
public void setSectorType (int block, SectorType type) public void setSectorType (int block, SectorType type)
{ {
if (block >= sectorTypes.length) if (block < sectorTypes.length)
System.out.println ("Invalid block number: " + block);
else
sectorTypes[block] = type; sectorTypes[block] = type;
else
System.out.println ("setSectorType: Invalid block number: " + block);
} }
// Override this so that the correct sector type can be displayed // Override this so that the correct sector type can be displayed

View File

@ -29,7 +29,7 @@ public class AppleDisk implements Disk
private final int tracks; // usually 35 for floppy disks private final int tracks; // usually 35 for floppy disks
private int sectors; // 8 or 16 private int sectors; // 8 or 16
private int blocks; // 280 or 560 private int blocks; // 280 or 560 for floppy disks, higher for HD
private final int trackSize; // 4096 private final int trackSize; // 4096
public int sectorSize; // 256 or 512 public int sectorSize; // 256 or 512
@ -460,9 +460,10 @@ public class AppleDisk implements Disk
{ {
if (!isValidAddress (block)) if (!isValidAddress (block))
{ {
System.out.printf ("Invalid block : %d of %d%n", block, this.blocks); System.out.printf ("getDiskAddress: Invalid block : %d of %d%n", block,
this.blocks);
return null; return null;
// return new AppleDiskAddress (this, 0); this was looping 26/07/2016 // return new AppleDiskAddress (this, 0); // this was looping 26/07/2016
} }
return new AppleDiskAddress (this, block); return new AppleDiskAddress (this, block);
} }
@ -483,9 +484,10 @@ public class AppleDisk implements Disk
@Override @Override
public boolean isValidAddress (int block) public boolean isValidAddress (int block)
{ {
if (block < 0 || block >= this.blocks) // if (block < 0 || block >= this.blocks)
return false; // return false;
return true; // return true;
return block >= 0 && block < this.blocks;
} }
@Override @Override

View File

@ -394,8 +394,8 @@ class DataPanel extends JTabbedPane
if (sectors.size () == 1) if (sectors.size () == 1)
{ {
DiskAddress da = sectors.get (0); DiskAddress da = sectors.get (0);
// if (da != null) if (da != null)
setDataSource (event.getFormattedDisk ().getFormattedSector (da)); setDataSource (event.getFormattedDisk ().getFormattedSector (da));
} }
else else
setDataSource (new SectorList (event.getFormattedDisk (), sectors)); setDataSource (new SectorList (event.getFormattedDisk (), sectors));

View File

@ -78,8 +78,8 @@ class DiskLayoutImage extends DiskPanel implements Scrollable, RedoListener
if (sectors != null && sectors.size () > 0) if (sectors != null && sectors.size () > 0)
{ {
DiskAddress da = sectors.size () == 1 ? sectors.get (0) : sectors.get (1); DiskAddress da = sectors.size () == 1 ? sectors.get (0) : sectors.get (1);
// if (da != null) if (da != null)
scrollRectToVisible (layoutDetails.getLocation (da)); scrollRectToVisible (layoutDetails.getLocation (da));
} }
repaint (); repaint ();
} }

View File

@ -8,6 +8,7 @@ import com.bytezone.diskbrowser.applefile.*;
import com.bytezone.diskbrowser.appleworks.AppleworksADBFile; import com.bytezone.diskbrowser.appleworks.AppleworksADBFile;
import com.bytezone.diskbrowser.appleworks.AppleworksSSFile; import com.bytezone.diskbrowser.appleworks.AppleworksSSFile;
import com.bytezone.diskbrowser.appleworks.AppleworksWPFile; import com.bytezone.diskbrowser.appleworks.AppleworksWPFile;
import com.bytezone.diskbrowser.disk.Disk;
import com.bytezone.diskbrowser.disk.DiskAddress; import com.bytezone.diskbrowser.disk.DiskAddress;
import com.bytezone.diskbrowser.gui.DataSource; import com.bytezone.diskbrowser.gui.DataSource;
import com.bytezone.diskbrowser.utilities.HexFormatter; import com.bytezone.diskbrowser.utilities.HexFormatter;
@ -31,11 +32,18 @@ class FileEntry extends CatalogEntry implements ProdosConstants
private final List<DiskAddress> indexBlocks = new ArrayList<DiskAddress> (); private final List<DiskAddress> indexBlocks = new ArrayList<DiskAddress> ();
private boolean invalid; private boolean invalid;
private FileEntry link; private FileEntry link;
// private final int maxBlocks;
private final Disk appleDisk;
public FileEntry (ProdosDisk fDisk, byte[] entryBuffer, DirectoryHeader parent, public FileEntry (ProdosDisk fDisk, byte[] entryBuffer, DirectoryHeader parent,
int parentBlock) int parentBlock)
{ {
super (fDisk, entryBuffer); super (fDisk, entryBuffer);
// maxBlocks = fDisk.getDisk ().getTotalBlocks ();
appleDisk = fDisk.getDisk ();
assert parent != null; assert parent != null;
this.parentDirectory = parent; this.parentDirectory = parent;
this.catalogBlock = this.disk.getDiskAddress (parentBlock); this.catalogBlock = this.disk.getDiskAddress (parentBlock);
@ -49,9 +57,6 @@ class FileEntry extends CatalogEntry implements ProdosConstants
modified = HexFormatter.getAppleDate (entryBuffer, 0x21); modified = HexFormatter.getAppleDate (entryBuffer, 0x21);
// headerPointer = HexFormatter.unsignedShort (entryBuffer, 0x25); // headerPointer = HexFormatter.unsignedShort (entryBuffer, 0x25);
if (isGSOSFile ()) // I think this is wrong
System.out.printf ("************************************ %s is GS/OS%n", name);
switch (storageType) switch (storageType)
{ {
case SEEDLING: case SEEDLING:
@ -59,18 +64,18 @@ class FileEntry extends CatalogEntry implements ProdosConstants
break; break;
case SAPLING: case SAPLING:
if (isGSOSFile ()) // not sure why this exists // if (isGSOSFile ()) // not sure why this exists
traverseGEOSIndex (keyPtr); // traverseGEOSIndex (keyPtr);
else // else
addDataBlocks (storageType, keyPtr); addDataBlocks (storageType, keyPtr);
break; break;
case TREE: case TREE:
masterIndexBlock = disk.getDiskAddress (keyPtr); masterIndexBlock = disk.getDiskAddress (keyPtr);
if (isGSOSFile ()) // not sure why this exists // if (isGSOSFile ()) // not sure why this exists
traverseGEOSMasterIndex (keyPtr); // traverseGEOSMasterIndex (keyPtr);
else // else
addDataBlocks (storageType, keyPtr); addDataBlocks (storageType, keyPtr);
break; break;
case GSOS_EXTENDED_FILE: case GSOS_EXTENDED_FILE:
@ -166,7 +171,10 @@ class FileEntry extends CatalogEntry implements ProdosConstants
byte[] buffer = disk.readSector (blockPtr); byte[] buffer = disk.readSector (blockPtr);
for (int i = 0; i < 256; i++) for (int i = 0; i < 256; i++)
blocks.add ((buffer[i] & 0xFF) | ((buffer[i + 0x100] & 0xFF) << 8)); {
int blockNo = (buffer[i] & 0xFF) | ((buffer[i + 0x100] & 0xFF) << 8);
blocks.add (disk.isValidAddress (blockNo) ? blockNo : 0);
}
} }
return blocks; return blocks;
@ -186,53 +194,56 @@ class FileEntry extends CatalogEntry implements ProdosConstants
List<Integer> blocks = new ArrayList<Integer> (highest + 1); List<Integer> blocks = new ArrayList<Integer> (highest + 1);
for (int i = 0; i <= highest; i++) for (int i = 0; i <= highest; i++)
blocks.add ((buffer[i] & 0xFF) | ((buffer[i + 256] & 0xFF) << 8)); {
int blockNo = (buffer[i] & 0xFF) | ((buffer[i + 256] & 0xFF) << 8);
blocks.add (disk.isValidAddress (blockNo) ? blockNo : 0);
}
return blocks; return blocks;
} }
// should be removed // should be removed
private boolean isGSOSFile () // private boolean isGSOSFile ()
{ // {
// return ((fileType & 0xF0) == 0x80); // // return ((fileType & 0xF0) == 0x80);
if ((fileType & 0xF0) == 0x80) // if ((fileType & 0xF0) == 0x80)
System.out.println ("GS/OS file: " + name); // System.out.println ("GS/OS file: " + name);
return false; // return false;
} // }
// should be removed // should be removed
private void traverseGEOSMasterIndex (int keyPtr) // private void traverseGEOSMasterIndex (int keyPtr)
{ // {
byte[] buffer = disk.readSector (keyPtr); // master index // byte[] buffer = disk.readSector (keyPtr); // master index
for (int i = 0; i < 0x80; i++) // for (int i = 0; i < 0x80; i++)
{ // {
int block = HexFormatter.intValue (buffer[i], buffer[i + 256]); // int block = HexFormatter.intValue (buffer[i], buffer[i + 256]);
if (block == 0) // if (block == 0)
break; // break;
if (block == 0xFFFF) // if (block == 0xFFFF)
continue; // continue;
traverseGEOSIndex (block); // traverseGEOSIndex (block);
} // }
} // }
// should be removed // should be removed
private void traverseGEOSIndex (int keyPtr) // private void traverseGEOSIndex (int keyPtr)
{ // {
parentDisk.setSectorType (keyPtr, parentDisk.indexSector); // parentDisk.setSectorType (keyPtr, parentDisk.indexSector);
indexBlocks.add (disk.getDiskAddress (keyPtr)); // indexBlocks.add (disk.getDiskAddress (keyPtr));
byte[] buffer = disk.readSector (keyPtr); // byte[] buffer = disk.readSector (keyPtr);
//
for (int i = 0; i < 0x80; i++) // for (int i = 0; i < 0x80; i++)
{ // {
int block = HexFormatter.intValue (buffer[i], buffer[i + 256]); // int block = HexFormatter.intValue (buffer[i], buffer[i + 256]);
if (block == 0) // if (block == 0)
break; // break;
if (block == 0xFFFF) // if (block == 0xFFFF)
continue; // continue;
parentDisk.setSectorType (block, parentDisk.dataSector); // parentDisk.setSectorType (block, parentDisk.dataSector);
dataBlocks.add (disk.getDiskAddress (block)); // dataBlocks.add (disk.getDiskAddress (block));
} // }
} // }
@Override @Override
public DataSource getDataSource () public DataSource getDataSource ()
@ -249,7 +260,8 @@ class FileEntry extends CatalogEntry implements ProdosConstants
if (fileType == FILE_TYPE_TEXT && auxType > 0) // random access file if (fileType == FILE_TYPE_TEXT && auxType > 0) // random access file
return getRandomAccessTextFile (); return getRandomAccessTextFile ();
byte[] buffer = isGSOSFile () ? getGEOSBuffer () : getBuffer (); // byte[] buffer = isGSOSFile () ? getGEOSBuffer () : getBuffer ();
byte[] buffer = getBuffer ();
byte[] exactBuffer = getExactBuffer (buffer); byte[] exactBuffer = getExactBuffer (buffer);
try try
@ -388,11 +400,9 @@ class FileEntry extends CatalogEntry implements ProdosConstants
file = new FileSystemTranslator (name, exactBuffer); file = new FileSystemTranslator (name, exactBuffer);
break; break;
case FILE_TYPE_PASCAL_VOLUME:
file = new DefaultAppleFile (name, exactBuffer);
break;
case FILE_TYPE_FINDER: case FILE_TYPE_FINDER:
case FILE_TYPE_PASCAL_VOLUME:
case FILE_TYPE_GEO:
file = new DefaultAppleFile (name, exactBuffer); file = new DefaultAppleFile (name, exactBuffer);
break; break;

View File

@ -11,6 +11,7 @@ public interface ProdosConstants
int FILE_TYPE_ASP = 0x1B; int FILE_TYPE_ASP = 0x1B;
int FILE_TYPE_DESCRIPTOR_TABLE = 0x42; int FILE_TYPE_DESCRIPTOR_TABLE = 0x42;
int FILE_TYPE_GWP = 0x50; int FILE_TYPE_GWP = 0x50;
int FILE_TYPE_GEO = 0x82;
int FILE_TYPE_IIGS_SOURCE = 0xB0; int FILE_TYPE_IIGS_SOURCE = 0xB0;
int FILE_TYPE_IIGS_OBJECT = 0xB1; int FILE_TYPE_IIGS_OBJECT = 0xB1;
// int FILE_TYPE_FORKED_FILE = 0xB3; // S16 // int FILE_TYPE_FORKED_FILE = 0xB3; // S16

View File

@ -94,8 +94,6 @@ public class ProdosDisk extends AbstractFormattedDisk
if (storageType == 0) // deleted or unused if (storageType == 0) // deleted or unused
continue; continue;
System.out.println ("here");
byte[] entry = new byte[ProdosConstants.ENTRY_SIZE]; byte[] entry = new byte[ProdosConstants.ENTRY_SIZE];
System.arraycopy (sectorBuffer, ptr, entry, 0, ProdosConstants.ENTRY_SIZE); System.arraycopy (sectorBuffer, ptr, entry, 0, ProdosConstants.ENTRY_SIZE);

View File

@ -25,8 +25,11 @@ class ProdosIndexSector extends AbstractSector
text.append ( text.append (
String.format ("%02X %02X %02X", i, buffer[i], buffer[i + 256])); String.format ("%02X %02X %02X", i, buffer[i], buffer[i + 256]));
if (buffer[i] != 0 || buffer[i + 256] != 0) if (buffer[i] != 0 || buffer[i + 256] != 0)
text.append (String.format (" %s%n", {
"block " + HexFormatter.intValue (buffer[i], buffer[i + 256]))); int blockNo = HexFormatter.intValue (buffer[i], buffer[i + 256]);
String valid = disk.isValidAddress (blockNo) ? "" : " *** invalid ***";
text.append (String.format (" %s%s%n", "block " + blockNo, valid));
}
else else
text.append ("\n"); text.append ("\n");
} }