2012-07-06 16:45:49 +00:00
|
|
|
package com.webcodepro.shrinkit;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 by David Schmidt
|
|
|
|
* david__schmidt at users.sourceforge.net
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2012-07-31 21:06:01 +00:00
|
|
|
import com.webcodepro.applecommander.storage.Disk;
|
2012-08-08 03:10:57 +00:00
|
|
|
import com.webcodepro.applecommander.storage.FileEntry;
|
2012-07-31 21:06:01 +00:00
|
|
|
import com.webcodepro.applecommander.storage.FormattedDisk;
|
2012-07-06 16:45:49 +00:00
|
|
|
import com.webcodepro.applecommander.storage.StorageBundle;
|
2012-07-31 21:06:01 +00:00
|
|
|
import com.webcodepro.applecommander.storage.os.prodos.ProdosFileEntry;
|
|
|
|
import com.webcodepro.applecommander.storage.os.prodos.ProdosFormatDisk;
|
|
|
|
import com.webcodepro.applecommander.storage.physical.ByteArrayImageLayout;
|
|
|
|
import com.webcodepro.applecommander.storage.physical.ImageOrder;
|
|
|
|
import com.webcodepro.applecommander.storage.physical.ProdosOrder;
|
2012-08-08 03:10:57 +00:00
|
|
|
import com.webcodepro.applecommander.ui.ac.Name;
|
2012-07-06 16:45:49 +00:00
|
|
|
import com.webcodepro.applecommander.util.TextBundle;
|
|
|
|
import com.webcodepro.shrinkit.io.LittleEndianByteInputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some higher-level utilities for dealing with a NuFX archive.
|
|
|
|
*
|
|
|
|
* @author david__schmidt at users.sourceforge.net
|
|
|
|
*/
|
|
|
|
public class Utilities
|
|
|
|
{
|
|
|
|
/**
|
2012-07-31 21:06:01 +00:00
|
|
|
* Interpret a NuFile/NuFX/Shrinkit archive as a full disk image.
|
2012-08-01 14:40:18 +00:00
|
|
|
*
|
|
|
|
* @return byte[] buffer containing full disk of data; null if unable to
|
|
|
|
* read
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
* if the filename is not able to be read
|
|
|
|
* @throws IOException
|
|
|
|
* the file has some malformed-ness about it
|
2012-07-06 16:45:49 +00:00
|
|
|
*/
|
2012-08-01 14:40:18 +00:00
|
|
|
public static byte[] unpackSHKFile(String fileName) throws IOException
|
2012-08-12 17:30:52 +00:00
|
|
|
{
|
|
|
|
return unpackSHKFile(fileName, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interpret a NuFile/NuFX/Shrinkit archive as a full disk image.
|
|
|
|
*
|
|
|
|
* @return byte[] buffer containing full disk of data; null if unable to
|
|
|
|
* read
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
* if the filename is not able to be read
|
|
|
|
* @throws IOException
|
|
|
|
* the file has some malformed-ness about it
|
|
|
|
*/
|
|
|
|
public static byte[] unpackSHKFile(String fileName, int startBlocks) throws IOException
|
2012-08-01 14:40:18 +00:00
|
|
|
{
|
2012-07-06 16:45:49 +00:00
|
|
|
TextBundle textBundle = StorageBundle.getInstance();
|
2012-07-31 21:06:01 +00:00
|
|
|
byte dmgBuffer[] = null;
|
2012-07-06 16:45:49 +00:00
|
|
|
File file = new File(fileName);
|
2012-08-01 14:40:18 +00:00
|
|
|
if (file.isDirectory() || !file.canRead())
|
|
|
|
{
|
2012-07-06 16:45:49 +00:00
|
|
|
throw new IllegalArgumentException(textBundle.format("NotAFile", fileName, 1)); //$NON-NLS-1$
|
|
|
|
}
|
|
|
|
InputStream is = new FileInputStream(file);
|
|
|
|
NuFileArchive a = new NuFileArchive(is);
|
2012-08-01 14:40:18 +00:00
|
|
|
// If we need to build a disk to hold files (i.e. .shk vs. .sdk), how big would that disk need to be?
|
2012-07-31 21:06:01 +00:00
|
|
|
int newDiskSize = Disk.sizeToFit(a.getArchiveSize());
|
2012-08-12 17:30:52 +00:00
|
|
|
if (startBlocks > 0)
|
|
|
|
newDiskSize = startBlocks*512;
|
2012-07-31 21:06:01 +00:00
|
|
|
ByteArrayImageLayout layout = new ByteArrayImageLayout(newDiskSize);
|
|
|
|
ImageOrder imageOrder = new ProdosOrder(layout);
|
2012-08-01 14:40:18 +00:00
|
|
|
// Create a new disk in anticipation of unpacking files - we don't actually know if we'll need it yet, though.
|
|
|
|
FormattedDisk[] disks = ProdosFormatDisk.create(fileName, "APPLECOMMANDER", imageOrder); //$NON-NLS-1$
|
|
|
|
// Make some typing easier... get a handle to the disk we created, with ProdosFormatDisk extensions.
|
|
|
|
ProdosFormatDisk pdDisk = (ProdosFormatDisk) disks[0];
|
2012-08-01 09:28:37 +00:00
|
|
|
ThreadRecord dataFork, resourceFork;
|
2012-08-01 14:40:18 +00:00
|
|
|
for (HeaderBlock b : a.getHeaderBlocks())
|
|
|
|
{
|
2012-07-31 21:06:01 +00:00
|
|
|
ProdosFileEntry newFile = null;
|
2012-08-01 09:28:37 +00:00
|
|
|
dataFork = null;
|
|
|
|
resourceFork = null;
|
2012-08-01 14:40:18 +00:00
|
|
|
for (ThreadRecord r : b.getThreadRecords())
|
|
|
|
{
|
2012-07-06 16:45:49 +00:00
|
|
|
try
|
|
|
|
{
|
2012-08-01 14:40:18 +00:00
|
|
|
switch (r.getThreadKind())
|
|
|
|
{
|
2012-07-31 21:06:01 +00:00
|
|
|
case ASCII_TEXT:
|
|
|
|
break;
|
|
|
|
case ALLOCATED_SPACE:
|
|
|
|
break;
|
|
|
|
case APPLE_IIGS_ICON:
|
|
|
|
break;
|
|
|
|
case CREATE_DIRECTORY:
|
|
|
|
break;
|
|
|
|
case DATA_FORK:
|
2012-08-01 14:40:18 +00:00
|
|
|
// This is a normal-ish file - hang on to the thread record
|
2012-08-01 09:28:37 +00:00
|
|
|
dataFork = r;
|
2012-07-31 21:06:01 +00:00
|
|
|
break;
|
|
|
|
case DISK_IMAGE:
|
|
|
|
dmgBuffer = readThread(r);
|
|
|
|
break;
|
|
|
|
case RESOURCE_FORK:
|
2012-08-01 09:28:37 +00:00
|
|
|
// This is a resource fork - we're talking GSOS FST here
|
|
|
|
resourceFork = r;
|
2012-07-31 21:06:01 +00:00
|
|
|
break;
|
|
|
|
case FILENAME:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Hmmm, this should not occur - but let us not fret about it.
|
|
|
|
break;
|
2012-07-06 16:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2012-08-12 17:30:52 +00:00
|
|
|
throw new IOException(ex.getMessage());
|
2012-07-06 16:45:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-01 09:28:37 +00:00
|
|
|
try
|
|
|
|
{
|
2012-08-01 14:40:18 +00:00
|
|
|
if (dataFork != null)
|
|
|
|
{
|
2012-08-08 03:10:57 +00:00
|
|
|
Name name = new Name(b.getFilename());
|
|
|
|
newFile = (ProdosFileEntry)name.createEntry(pdDisk);
|
2012-08-01 14:40:18 +00:00
|
|
|
if (newFile != null)
|
|
|
|
{
|
|
|
|
if (resourceFork != null)
|
|
|
|
{
|
|
|
|
// If we have a resource fork in addition to a data fork,
|
|
|
|
// then we've got a GSOS storage type $5.
|
|
|
|
newFile.setFileData(readThread(dataFork), readThread(resourceFork));
|
|
|
|
newFile.setStorageType(0x05);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We have a traditional file, no resource fork.
|
|
|
|
newFile.setFileData(readThread(dataFork));
|
|
|
|
}
|
2012-08-11 20:57:44 +00:00
|
|
|
newFile.setFilename(b.getFinalFilename());
|
2012-08-01 14:40:18 +00:00
|
|
|
newFile.setFiletype(b.getFileType());
|
|
|
|
newFile.setAuxiliaryType((int) b.getExtraType());
|
|
|
|
// TODO: dates differ by a month or so from what CiderPress reports.
|
|
|
|
newFile.setCreationDate(b.getCreateWhen());
|
|
|
|
newFile.setLastModificationDate(b.getModWhen());
|
|
|
|
newFile = null;
|
2012-08-01 09:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2012-08-12 17:30:52 +00:00
|
|
|
throw new IOException(ex.getMessage());
|
2012-07-26 21:00:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-31 21:06:01 +00:00
|
|
|
if (dmgBuffer != null)
|
2012-08-01 14:40:18 +00:00
|
|
|
{
|
|
|
|
// Disk images take precedence... if they have both disk images and files, just return the disk.
|
2012-07-31 21:06:01 +00:00
|
|
|
return dmgBuffer;
|
2012-08-01 14:40:18 +00:00
|
|
|
}
|
2012-07-31 21:06:01 +00:00
|
|
|
else
|
2012-08-01 14:40:18 +00:00
|
|
|
return imageOrder.readBytes(0, newDiskSize);
|
2012-07-26 21:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-31 21:06:01 +00:00
|
|
|
* readThread
|
|
|
|
*
|
|
|
|
* Reads the data from a thread
|
2012-08-01 14:40:18 +00:00
|
|
|
*
|
2012-07-31 21:06:01 +00:00
|
|
|
* @returns byte[] buffer
|
2012-07-26 21:00:58 +00:00
|
|
|
*/
|
2012-08-01 14:40:18 +00:00
|
|
|
public static byte[] readThread(ThreadRecord thread) throws IOException
|
|
|
|
{
|
2012-07-31 21:06:01 +00:00
|
|
|
thread.readThreadData(new LittleEndianByteInputStream(thread.getRawInputStream()));
|
|
|
|
InputStream fis = thread.getInputStream();
|
2012-08-01 14:40:18 +00:00
|
|
|
byte[] buffer = new byte[(int) (thread.getThreadEof())];
|
|
|
|
fis.read(buffer, 0, buffer.length);
|
2012-07-31 21:06:01 +00:00
|
|
|
fis.close();
|
2012-07-06 16:45:49 +00:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
}
|