dmolony-DiskBrowser/src/com/bytezone/diskbrowser/dos/AbstractCatalogEntry.java

362 lines
12 KiB
Java
Raw Normal View History

2015-06-01 09:35:51 +00:00
package com.bytezone.diskbrowser.dos;
import java.util.ArrayList;
import java.util.List;
import com.bytezone.diskbrowser.applefile.*;
import com.bytezone.diskbrowser.disk.Disk;
import com.bytezone.diskbrowser.disk.DiskAddress;
import com.bytezone.diskbrowser.disk.FormattedDisk;
import com.bytezone.diskbrowser.dos.DosDisk.FileType;
import com.bytezone.diskbrowser.gui.DataSource;
2016-02-24 21:11:14 +00:00
import com.bytezone.diskbrowser.utilities.HexFormatter;
2015-06-01 09:35:51 +00:00
abstract class AbstractCatalogEntry implements AppleFileSource
{
2016-02-25 07:45:24 +00:00
protected Disk disk;
protected DosDisk dosDisk;
protected String name;
protected String catalogName;
2015-06-01 09:35:51 +00:00
2016-02-25 07:45:24 +00:00
protected FileType fileType;
protected int reportedSize;
protected boolean locked;
2015-06-01 09:35:51 +00:00
protected DataSource appleFile;
protected DiskAddress catalogSectorDA;
protected final List<DiskAddress> dataSectors = new ArrayList<DiskAddress> ();
protected final List<DiskAddress> tsSectors = new ArrayList<DiskAddress> ();
2016-12-31 09:34:15 +00:00
private CatalogEntry link;
2016-02-05 00:23:53 +00:00
public AbstractCatalogEntry (DosDisk dosDisk, DiskAddress catalogSector,
byte[] entryBuffer)
2015-06-01 09:35:51 +00:00
{
this.dosDisk = dosDisk;
this.disk = dosDisk.getDisk ();
this.catalogSectorDA = catalogSector;
reportedSize = HexFormatter.intValue (entryBuffer[33], entryBuffer[34]);
int type = entryBuffer[2] & 0xFF;
locked = (type & 0x80) > 0;
this.disk = dosDisk.getDisk ();
if ((type & 0x7F) == 0)
fileType = FileType.Text;
else if ((type & 0x01) > 0)
fileType = FileType.IntegerBasic;
else if ((type & 0x02) > 0)
fileType = FileType.ApplesoftBasic;
else if ((type & 0x04) > 0)
fileType = FileType.Binary;
else if ((type & 0x08) > 0)
fileType = FileType.SS;
else if ((type & 0x10) > 0)
fileType = FileType.Relocatable;
else if ((type & 0x20) > 0)
fileType = FileType.AA;
else if ((type & 0x40) > 0)
fileType = FileType.BB;
else
System.out.println ("Unknown file type : " + (type & 0x7F));
name = getName ("", entryBuffer);
// CATALOG command only formats the LO byte - see Beneath Apple DOS pp4-6
2016-02-05 00:23:53 +00:00
String base = String.format ("%s%s %03d ", (locked) ? "*" : " ", getFileType (),
2016-12-12 07:43:19 +00:00
(entryBuffer[33] & 0xFF));
2015-06-01 09:35:51 +00:00
catalogName = getName (base, entryBuffer);
}
private String getName (String base, byte[] buffer)
{
StringBuilder text = new StringBuilder (base);
int max = buffer[0] == (byte) 0xFF ? 32 : 33;
for (int i = 3; i < max; i++)
{
int c = buffer[i] & 0xFF;
if (c == 136 && !base.isEmpty ()) // allow backspaces
2015-06-01 09:35:51 +00:00
{
if (text.length () > 0)
text.deleteCharAt (text.length () - 1);
continue;
}
if (c > 127)
c -= c < 160 ? 64 : 128;
if (c < 32)
2016-12-17 08:34:47 +00:00
text.append ("^" + (char) (c + 64)); // non-printable ascii
2015-06-01 09:35:51 +00:00
else
2016-12-17 08:34:47 +00:00
text.append ((char) c); // standard ascii
2015-06-01 09:35:51 +00:00
}
while (text.length () > 0 && text.charAt (text.length () - 1) == ' ')
2016-12-17 08:34:47 +00:00
text.deleteCharAt (text.length () - 1); // rtrim()
2015-06-01 09:35:51 +00:00
return text.toString ();
}
protected String getFileType ()
{
switch (this.fileType)
{
case Text:
return "T";
case IntegerBasic:
return "I";
case ApplesoftBasic:
return "A";
case Binary:
return "B";
case SS: // what is this?
2015-06-01 09:35:51 +00:00
return "S";
case Relocatable:
return "R";
case AA: // what is this?
2015-06-01 09:35:51 +00:00
return "A";
case BB: // what is this?
2015-06-01 09:35:51 +00:00
return "B";
default:
System.out.println ("Unknown file type : " + fileType);
return "?";
}
}
// maybe this should be in the FormattedDisk
// maybe DiskAddress should have a 'valid' flag
protected DiskAddress getValidAddress (byte[] buffer, int offset)
{
if (disk.isValidAddress (buffer[offset], buffer[offset + 1]))
return disk.getDiskAddress (buffer[offset], buffer[offset + 1]);
return null;
}
@Override
public DataSource getDataSource ()
{
if (appleFile != null)
return appleFile;
byte[] buffer = disk.readSectors (dataSectors);
int reportedLength;
if (buffer.length == 0)
{
appleFile = new DefaultAppleFile (name, buffer);
return appleFile;
}
try
{
2016-02-05 00:23:53 +00:00
byte[] exactBuffer;
2015-06-01 09:35:51 +00:00
switch (this.fileType)
{
case Text:
if (VisicalcFile.isVisicalcFile (buffer))
appleFile = new VisicalcFile (name, buffer);
else
appleFile = new TextFile (name, buffer);
break;
2016-02-05 00:23:53 +00:00
2015-06-01 09:35:51 +00:00
case IntegerBasic:
reportedLength = HexFormatter.intValue (buffer[0], buffer[1]);
2016-02-05 00:23:53 +00:00
exactBuffer = new byte[reportedLength];
2015-06-01 09:35:51 +00:00
System.arraycopy (buffer, 2, exactBuffer, 0, reportedLength);
appleFile = new IntegerBasicProgram (name, exactBuffer);
break;
2016-02-05 00:23:53 +00:00
2015-06-01 09:35:51 +00:00
case ApplesoftBasic:
reportedLength = HexFormatter.intValue (buffer[0], buffer[1]);
exactBuffer = new byte[reportedLength];
if (reportedLength > buffer.length)
reportedLength = buffer.length - 2;
System.arraycopy (buffer, 2, exactBuffer, 0, reportedLength);
appleFile = new BasicProgram (name, exactBuffer);
break;
2016-02-05 00:23:53 +00:00
case Binary: // binary file
case Relocatable: // relocatable binary file
2016-03-23 23:37:59 +00:00
// if (buffer.length == 0)
// appleFile = new AssemblerProgram (name, buffer, 0);
// else
// {
int loadAddress = HexFormatter.intValue (buffer[0], buffer[1]);
reportedLength = HexFormatter.intValue (buffer[2], buffer[3]);
if (reportedLength == 0)
2015-06-01 09:35:51 +00:00
{
2016-03-23 23:37:59 +00:00
System.out.println (name.trim () + " reported length : 0 - reverting to "
+ (buffer.length - 4));
reportedLength = buffer.length - 4;
}
// buffer is a multiple of the block size, so it usually needs to be reduced
if ((reportedLength + 4) <= buffer.length)
{
exactBuffer = new byte[reportedLength];
// extraBuffer = new byte[buffer.length - reportedLength - 4];
// System.arraycopy (buffer, reportedLength + 4, extraBuffer, 0,
// extraBuffer.length);
}
else
exactBuffer = new byte[buffer.length - 4]; // reported length is too long
2016-02-05 00:23:53 +00:00
2016-03-23 23:37:59 +00:00
System.arraycopy (buffer, 4, exactBuffer, 0, exactBuffer.length);
2017-03-19 09:52:36 +00:00
if ((name.endsWith (".FONT") || name.endsWith (" FONT")
|| name.endsWith (".SET") || name.startsWith ("ASCII."))
&& FontFile.isFont (exactBuffer))
2016-12-17 08:34:47 +00:00
appleFile = new FontFile (name, exactBuffer);
else if (ShapeTable.isShapeTable (exactBuffer))
2016-03-23 23:37:59 +00:00
appleFile = new ShapeTable (name, exactBuffer);
2016-12-12 07:43:19 +00:00
else if (name.endsWith (".S"))
appleFile = new MerlinSource (name, exactBuffer);
2017-02-01 21:15:30 +00:00
else if (HiResImage.isGif (exactBuffer)) // buffer?
2016-12-31 09:34:15 +00:00
appleFile = new OriginalHiResImage (name, exactBuffer, loadAddress);
2017-02-01 21:15:30 +00:00
else if (HiResImage.isPng (exactBuffer)) // buffer?
appleFile = new OriginalHiResImage (name, exactBuffer, loadAddress);
else if (name.endsWith (".BMP") && HiResImage.isBmp (buffer))
appleFile = new OriginalHiResImage (name, buffer, loadAddress);
2017-01-08 23:36:10 +00:00
else if (name.endsWith (".PAC"))
appleFile = new DoubleHiResImage (name, exactBuffer);
2016-12-31 09:34:15 +00:00
else if (link != null)
{
byte[] auxBuffer = link.disk.readSectors (link.dataSectors);
byte[] exactAuxBuffer = getExactBuffer (auxBuffer);
if (name.endsWith (".AUX"))
appleFile = new DoubleHiResImage (name, exactAuxBuffer, exactBuffer);
else
appleFile = new DoubleHiResImage (name, exactBuffer, exactAuxBuffer);
}
2016-03-23 23:37:59 +00:00
else if (loadAddress == 0x2000 || loadAddress == 0x4000)
{
2016-12-17 08:34:47 +00:00
if (reportedLength > 0x1F00 && reportedLength <= 0x4000)
2016-12-31 09:34:15 +00:00
appleFile = new OriginalHiResImage (name, exactBuffer, loadAddress);
2016-12-17 08:34:47 +00:00
else if (isScrunched (reportedLength))
2016-12-31 09:34:15 +00:00
appleFile = new OriginalHiResImage (name, exactBuffer, loadAddress, true);
2015-06-01 09:35:51 +00:00
else
appleFile = new AssemblerProgram (name, exactBuffer, loadAddress);
}
2016-03-23 23:37:59 +00:00
else
{
appleFile = new AssemblerProgram (name, exactBuffer, loadAddress);
if ((exactBuffer.length + 4) < buffer.length)
2016-12-12 07:43:19 +00:00
((AssemblerProgram) appleFile).setExtraBuffer (buffer,
exactBuffer.length + 4, buffer.length - (exactBuffer.length + 4));
2016-03-23 23:37:59 +00:00
}
2015-06-01 09:35:51 +00:00
break;
2016-02-05 00:23:53 +00:00
case SS: // what is this?
2015-06-01 09:35:51 +00:00
System.out.println ("SS file");
appleFile = new DefaultAppleFile (name, buffer);
break;
2016-02-05 00:23:53 +00:00
case AA: // what is this?
2015-06-01 09:35:51 +00:00
System.out.println ("AA file");
appleFile = new DefaultAppleFile (name, buffer);
break;
2016-02-05 00:23:53 +00:00
case BB: // what is this?
2016-03-23 23:37:59 +00:00
loadAddress = HexFormatter.intValue (buffer[0], buffer[1]);
2015-06-01 09:35:51 +00:00
reportedLength = HexFormatter.intValue (buffer[2], buffer[3]);
exactBuffer = new byte[reportedLength];
System.arraycopy (buffer, 4, exactBuffer, 0, reportedLength);
appleFile = new SimpleText2 (name, exactBuffer, loadAddress);
break;
2016-02-05 00:23:53 +00:00
2015-06-01 09:35:51 +00:00
default:
System.out.println ("Unknown file type : " + fileType);
appleFile = new DefaultAppleFile (name, buffer);
break;
}
}
catch (Exception e)
{
appleFile = new ErrorMessageFile (name, buffer, e);
e.printStackTrace ();
}
return appleFile;
}
2016-12-31 09:34:15 +00:00
private byte[] getExactBuffer (byte[] buffer)
{
byte[] exactBuffer;
int loadAddress = HexFormatter.intValue (buffer[0], buffer[1]);
int reportedLength = HexFormatter.intValue (buffer[2], buffer[3]);
if (reportedLength == 0)
{
System.out.println (
name.trim () + " reported length : 0 - reverting to " + (buffer.length - 4));
reportedLength = buffer.length - 4;
}
// buffer is a multiple of the block size, so it usually needs to be reduced
if ((reportedLength + 4) <= buffer.length)
{
exactBuffer = new byte[reportedLength];
// extraBuffer = new byte[buffer.length - reportedLength - 4];
// System.arraycopy (buffer, reportedLength + 4, extraBuffer, 0,
// extraBuffer.length);
}
else
exactBuffer = new byte[buffer.length - 4]; // reported length is too long
System.arraycopy (buffer, 4, exactBuffer, 0, exactBuffer.length);
return exactBuffer;
}
2016-12-17 08:34:47 +00:00
private boolean isScrunched (int reportedLength)
{
if ((name.equals ("FLY LOGO") || name.equals ("FLY LOGO SCRUNCHED"))
&& reportedLength == 0x14FA)
return true;
2016-12-22 22:41:28 +00:00
// if (name.endsWith (".PAC"))
// return true;
2016-12-17 08:34:47 +00:00
if (name.equals ("BBROS LOGO SCRUNCHED") && reportedLength == 0x0FED)
return true;
return false;
}
2016-02-25 21:49:22 +00:00
@Override
public boolean contains (DiskAddress da)
2015-06-01 09:35:51 +00:00
{
for (DiskAddress sector : tsSectors)
if (sector.matches (da))
2015-06-01 09:35:51 +00:00
return true;
for (DiskAddress sector : dataSectors)
// random access files may have gaps, and thus null sectors
if (sector != null && sector.matches (da))
2015-06-01 09:35:51 +00:00
return true;
return false;
}
@Override
public String getUniqueName ()
{
// this might not be unique if the file has been deleted
return name;
}
@Override
public FormattedDisk getFormattedDisk ()
{
return dosDisk;
}
@Override
public List<DiskAddress> getSectors ()
{
List<DiskAddress> sectors = new ArrayList<DiskAddress> ();
sectors.add (catalogSectorDA);
sectors.addAll (tsSectors);
sectors.addAll (dataSectors);
return sectors;
}
2016-12-31 09:34:15 +00:00
void link (CatalogEntry catalogEntry)
{
this.link = catalogEntry;
}
2015-06-01 09:35:51 +00:00
@Override
public String toString ()
{
return catalogName;
}
}