Tighten up image detection a little more

This commit is contained in:
2012-07-06 21:14:54 +00:00
parent 852522d58d
commit e09b66a7dd

View File

@ -95,7 +95,7 @@ public class Disk {
private String filename; private String filename;
private boolean newImage = false; private boolean newImage = false;
private ByteArrayImageLayout diskImageManager; private ByteArrayImageLayout diskImageManager;
private ImageOrder imageOrder; private ImageOrder imageOrder = null;
/** /**
* Get the supported file filters supported by the Disk interface. * Get the supported file filters supported by the Disk interface.
@ -175,14 +175,15 @@ public class Disk {
*/ */
public Disk(String filename) throws IOException { public Disk(String filename) throws IOException {
this.filename = filename; this.filename = filename;
File file = new File(filename);
int diskSize = 0; int diskSize = 0;
byte[] diskImage = null; byte[] diskImage = null;
if (isSDK()) { if (isSDK()) {
// If we have an SDK, unpack it and branch around all this nonsense // If we have an SDK, unpack it and send along the byte array
diskImage = com.webcodepro.shrinkit.Utilities.unpackSDKFile(filename); diskImage = com.webcodepro.shrinkit.Utilities.unpackSDKFile(filename);
diskSize = diskImage.length; diskSize = diskImage.length;
} else { } else {
File file = new File(filename);
diskSize = (int) file.length(); diskSize = (int) file.length();
InputStream input = new FileInputStream(file); InputStream input = new FileInputStream(file);
if (isCompressed()) { if (isCompressed()) {
@ -206,6 +207,9 @@ public class Disk {
ImageOrder dosOrder = new DosOrder(diskImageManager); ImageOrder dosOrder = new DosOrder(diskImageManager);
ImageOrder proDosOrder = new ProdosOrder(diskImageManager); ImageOrder proDosOrder = new ProdosOrder(diskImageManager);
if (isSDK()) {
imageOrder = proDosOrder; // SDKs are always in ProDOS order
} else {
/* /*
* First step: test physical disk orders for viable file systems. * First step: test physical disk orders for viable file systems.
*/ */
@ -252,6 +256,7 @@ public class Disk {
} }
} }
} }
}
/** /**
* Test the image order to see if we can recognize a file system. Returns: 0 * Test the image order to see if we can recognize a file system. Returns: 0
@ -486,9 +491,14 @@ public class Disk {
*/ */
public boolean isProdosFormat() { public boolean isProdosFormat() {
byte[] prodosVolumeDirectory = readBlock(2); byte[] prodosVolumeDirectory = readBlock(2);
int volDirEntryLength = prodosVolumeDirectory[0x23];
int volDirEntriesPerBlock = prodosVolumeDirectory[0x24];
return prodosVolumeDirectory[0] == 0 && return prodosVolumeDirectory[0] == 0 &&
prodosVolumeDirectory[1] == 0 && prodosVolumeDirectory[1] == 0 &&
(prodosVolumeDirectory[4]&0xf0) == 0xf0; (prodosVolumeDirectory[4]&0xf0) == 0xf0 &&
(volDirEntryLength * volDirEntriesPerBlock <= 512)
;
} }
/** /**
@ -497,17 +507,44 @@ public class Disk {
* different characteristics. This just tests 140KB images. * different characteristics. This just tests 140KB images.
*/ */
public boolean isDosFormat() { public boolean isDosFormat() {
if (!is140KbDisk()) return false; boolean good = false;
if (!is140KbDisk()) {
return false;
}
try {
byte[] vtoc = readSector(17, 0); byte[] vtoc = readSector(17, 0);
return (imageOrder.isSizeApprox(APPLE_140KB_DISK) good = (imageOrder.isSizeApprox(APPLE_140KB_DISK)
|| imageOrder.isSizeApprox(APPLE_140KB_NIBBLE_DISK)) || imageOrder.isSizeApprox(APPLE_140KB_NIBBLE_DISK))
&& vtoc[0x01] == 17 // expect catalog to start on track 17 && vtoc[0x01] == 17 // expect catalog to start on track 17
// can vary && vtoc[0x02] == 15 // expect catalog to start on sector 15 (140KB disk only!) // can vary && vtoc[0x02] == 15 // expect catalog to start on sector 15 (140KB disk only!)
&& vtoc[0x27] == 122 // expect 122 tract/sector pairs per sector && vtoc[0x27] == 122 // expect 122 track/sector pairs per sector
&& vtoc[0x34] == 35 // expect 35 tracks per disk (140KB disk only!) && vtoc[0x34] == 35 // expect 35 tracks per disk (140KB disk only!)
&& vtoc[0x35] == 16; // expect 16 sectors per disk (140KB disk only!) && vtoc[0x35] == 16 // expect 16 sectors per disk (140KB disk only!)
// && vtoc[0x36] == 0 // bytes per sector (low byte) ;
// && vtoc[0x37] == 1; // bytes per sector (high byte) if (good) {
int catTrack = vtoc[0x01]; // Pull out the first catalog track/sector
int catSect = vtoc[0x02];
byte[] cat = readSector(catTrack, catSect);
if (catTrack == cat[1] && catSect == cat[2] + 1) {
// Still good... let's follow one more
catTrack = cat[1];
catSect = cat[2];
cat = readSector(catTrack, catSect);
if (catTrack == cat[1] && catSect == cat[2] + 1) {
good = true;
} else {
good = false;
}
}
}
} catch (Exception ex) {
/*
* If we get various exceptions from reading tracks and sectors, then we
* definitely don't have a valid DOS image.
*/
good = false;
}
return good;
} }
/** /**