diff --git a/DiscImageChef.DiscImages/CDRWin.cs b/DiscImageChef.DiscImages/CDRWin.cs index 0015dd6d6..9dfe0bfbb 100644 --- a/DiscImageChef.DiscImages/CDRWin.cs +++ b/DiscImageChef.DiscImages/CDRWin.cs @@ -1284,7 +1284,7 @@ namespace DiscImageChef.ImagePlugins } catch(Exception ex) { - DicConsole.ErrorWriteLine("Exception trying to identify image file {0}", imageFilter); + DicConsole.ErrorWriteLine("Exception trying to identify image file {0}", imageFilter.GetFilename()); DicConsole.ErrorWriteLine("Exception: {0}", ex.Message); DicConsole.ErrorWriteLine("Stack trace: {0}", ex.StackTrace); return false; diff --git a/DiscImageChef.DiscImages/ChangeLog b/DiscImageChef.DiscImages/ChangeLog index dc9dd05f3..13ddfa88f 100644 --- a/DiscImageChef.DiscImages/ChangeLog +++ b/DiscImageChef.DiscImages/ChangeLog @@ -1,3 +1,7 @@ +2016-09-05 Natalia Portillo + + * CDRWin.cs: Show correct filename from filter. + 2016-09-05 Natalia Portillo * GDI.cs: diff --git a/DiscImageChef.Filters/AppleDouble.cs b/DiscImageChef.Filters/AppleDouble.cs new file mode 100644 index 000000000..5cc0e4d99 --- /dev/null +++ b/DiscImageChef.Filters/AppleDouble.cs @@ -0,0 +1,599 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : AppleDouble.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Provides a filter to open AppleDouble files. +// +// --[ License ] -------------------------------------------------------------- +// +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 2.1 of the +// License, or (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Linq; +using System.Text; + +namespace DiscImageChef.Filters +{ + public class AppleDouble : Filter + { + enum AppleDoubleEntryID : uint + { + Invalid = 0, + DataFork = 1, + ResourceFork = 2, + RealName = 3, + Comment = 4, + Icon = 5, + ColorIcon = 6, + FileInfo = 7, + FileDates = 8, + FinderInfo = 9, + MacFileInfo = 10, + ProDOSFileInfo = 11, + DOSFileInfo = 12, + ShortName = 13, + AFPFileInfo = 14, + DirectoryID = 15 + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleDoubleHeader + { + public uint magic; + public uint version; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[] homeFilesystem; + public ushort entries; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleDoubleEntry + { + public uint id; + public uint offset; + public uint length; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleDoubleFileDates + { + public uint creationDate; + public uint modificationDate; + public uint backupDate; + public uint accessDate; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleDoubleMacFileInfo + { + public uint creationDate; + public uint modificationDate; + public uint backupDate; + public uint accessDate; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleDoubleUNIXFileInfo + { + public uint creationDate; + public uint accessDate; + public uint modificationDate; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleDoubleDOSFileInfo + { + public ushort modificationDate; + public ushort modificationTime; + public ushort attributes; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleDoubleProDOSFileInfo + { + public uint creationDate; + public uint modificationDate; + public uint backupDate; + public ushort access; + public ushort fileType; + public uint auxType; + } + + const uint AppleDoubleMagic = 0x00051607; + const uint AppleDoubleVersion = 0x00010000; + const uint AppleDoubleVersion2 = 0x00020000; + + readonly byte[] MacintoshHome = { 0x4D, 0x61, 0x63, 0x69, 0x6E, 0x74, 0x6F, 0x73, 0x68, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] ProDOSHome = { 0x50, 0x72, 0x6F, 0x44, 0x4F, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] DOSHome = { 0x4D, 0x53, 0x2D, 0x44, 0x4F, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] UNIXHome = { 0x55, 0x6E, 0x69, 0x78, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] VMXHome = { 0x56, 0x41, 0x58, 0x20, 0x56, 0x4D, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] OSXHome = { 0x4D, 0x61, 0x63, 0x20, 0x4F, 0x53, 0x20, 0x58, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + + AppleDoubleEntry dataFork; + AppleDoubleEntry rsrcFork; + bool opened; + string basePath; + string headerPath; + DateTime lastWriteTime; + DateTime creationTime; + AppleDoubleHeader header; + + public AppleDouble() + { + Name = "AppleDouble"; + UUID = new Guid("1B2165EE-C9DF-4B21-BBBB-9E5892B2DF4D"); + } + + public override void Close() + { + opened = false; + } + + public override string GetBasePath() + { + return basePath; + } + + public override DateTime GetCreationTime() + { + return creationTime; + } + + public override long GetDataForkLength() + { + return dataFork.length; + } + + public override Stream GetDataForkStream() + { + return new FileStream(basePath, FileMode.Open, FileAccess.Read); + } + + public override string GetFilename() + { + return basePath != null ? Path.GetFileName(basePath) : null; + } + + public override DateTime GetLastWriteTime() + { + return lastWriteTime; + } + + public override long GetLength() + { + return dataFork.length + rsrcFork.length; + } + + public override string GetParentFolder() + { + return Path.GetDirectoryName(basePath); + } + + public override string GetPath() + { + return basePath; + } + + public override long GetResourceForkLength() + { + return rsrcFork.length; + } + + public override Stream GetResourceForkStream() + { + if(rsrcFork.length == 0) + return null; + + return new OffsetStream(headerPath, FileMode.Open, FileAccess.Read, rsrcFork.offset, rsrcFork.offset + rsrcFork.length - 1); + } + + public override bool HasResourceFork() + { + return rsrcFork.length > 0; + } + + public override bool Identify(byte[] buffer) + { + // Now way to have two files in a single byte array + return false; + } + + public override bool Identify(Stream stream) + { + // Now way to have two files in a single stream + return false; + } + + public override bool Identify(string path) + { + // Prepend data fork name with "R." + string ProDosAppleDouble; + // Prepend data fork name with '%' + string UNIXAppleDouble; + // Change file extension to ADF + string DOSAppleDouble; + // Change file extension to adf + string DOSAppleDoubleLower; + // Store AppleDouble header file in ".AppleDouble" folder with same name + string NetatalkAppleDouble; + // Store AppleDouble header file in "resource.frk" folder with same name + string DAVEAppleDouble; + // Prepend data fork name with "._" + string OSXAppleDouble; + + string filename = Path.GetFileName(path); + string filenameNoExt = Path.GetFileNameWithoutExtension(path); + string parentFolder = Path.GetDirectoryName(path); + + ProDosAppleDouble = Path.Combine(parentFolder, "R." + filename); + UNIXAppleDouble = Path.Combine(parentFolder, "%" + filename); + DOSAppleDouble = Path.Combine(parentFolder, filenameNoExt + ".ADF"); + DOSAppleDoubleLower = Path.Combine(parentFolder, filenameNoExt + ".adf"); + NetatalkAppleDouble = Path.Combine(parentFolder, ".AppleDouble", filename); + DAVEAppleDouble = Path.Combine(parentFolder, "resource.frk", filename); + OSXAppleDouble = Path.Combine(parentFolder, "._" + filename); + + // Check AppleDouble created by A/UX in ProDOS filesystem + if(File.Exists(ProDosAppleDouble)) + { + FileStream prodosStream = new FileStream(ProDosAppleDouble, FileMode.Open, FileAccess.Read); + if(prodosStream != null && prodosStream.Length > 26) + { + byte[] prodos_b = new byte[26]; + prodosStream.Read(prodos_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(prodos_b); + prodosStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + return true; + } + } + + // Check AppleDouble created by A/UX in UFS filesystem + if(File.Exists(UNIXAppleDouble)) + { + FileStream unixStream = new FileStream(UNIXAppleDouble, FileMode.Open, FileAccess.Read); + if(unixStream != null && unixStream.Length > 26) + { + byte[] unix_b = new byte[26]; + unixStream.Read(unix_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(unix_b); + unixStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + return true; + } + } + + // Check AppleDouble created by A/UX in FAT filesystem + if(File.Exists(DOSAppleDouble)) + { + FileStream dosStream = new FileStream(DOSAppleDouble, FileMode.Open, FileAccess.Read); + if(dosStream != null && dosStream.Length > 26) + { + byte[] dos_b = new byte[26]; + dosStream.Read(dos_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(dos_b); + dosStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + return true; + } + } + + // Check AppleDouble created by A/UX in case preserving FAT filesystem + if(File.Exists(DOSAppleDoubleLower)) + { + FileStream doslStream = new FileStream(DOSAppleDoubleLower, FileMode.Open, FileAccess.Read); + if(doslStream != null && doslStream.Length > 26) + { + byte[] dosl_b = new byte[26]; + doslStream.Read(dosl_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(dosl_b); + doslStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + return true; + } + } + + // Check AppleDouble created by Netatalk + if(File.Exists(NetatalkAppleDouble)) + { + FileStream netatalkStream = new FileStream(NetatalkAppleDouble, FileMode.Open, FileAccess.Read); + if(netatalkStream != null && netatalkStream.Length > 26) + { + byte[] netatalk_b = new byte[26]; + netatalkStream.Read(netatalk_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(netatalk_b); + netatalkStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + return true; + } + } + + // Check AppleDouble created by DAVE + if(File.Exists(DAVEAppleDouble)) + { + FileStream daveStream = new FileStream(DAVEAppleDouble, FileMode.Open, FileAccess.Read); + if(daveStream != null && daveStream.Length > 26) + { + byte[] dave_b = new byte[26]; + daveStream.Read(dave_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(dave_b); + daveStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + return true; + } + } + + // Check AppleDouble created by Mac OS X + if(File.Exists(OSXAppleDouble)) + { + FileStream osxStream = new FileStream(OSXAppleDouble, FileMode.Open, FileAccess.Read); + if(osxStream != null && osxStream.Length > 26) + { + byte[] osx_b = new byte[26]; + osxStream.Read(osx_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(osx_b); + osxStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + return true; + } + } + + return false; + } + + public override bool IsOpened() + { + return opened; + } + + public override void Open(byte[] buffer) + { + // Now way to have two files in a single byte array + throw new NotSupportedException(); + } + + public override void Open(Stream stream) + { + // Now way to have two files in a single stream + throw new NotSupportedException(); + } + + public override void Open(string path) + { + // Prepend data fork name with "R." + string ProDosAppleDouble; + // Prepend data fork name with '%' + string UNIXAppleDouble; + // Change file extension to ADF + string DOSAppleDouble; + // Change file extension to adf + string DOSAppleDoubleLower; + // Store AppleDouble header file in ".AppleDouble" folder with same name + string NetatalkAppleDouble; + // Store AppleDouble header file in "resource.frk" folder with same name + string DAVEAppleDouble; + // Prepend data fork name with "._" + string OSXAppleDouble; + + string filename = Path.GetFileName(path); + string filenameNoExt = Path.GetFileNameWithoutExtension(path); + string parentFolder = Path.GetDirectoryName(path); + + ProDosAppleDouble = Path.Combine(parentFolder, "R." + filename); + UNIXAppleDouble = Path.Combine(parentFolder, "%" + filename); + DOSAppleDouble = Path.Combine(parentFolder, filenameNoExt + ".ADF"); + DOSAppleDoubleLower = Path.Combine(parentFolder, filenameNoExt + ".adf"); + NetatalkAppleDouble = Path.Combine(parentFolder, ".AppleDouble", filename); + DAVEAppleDouble = Path.Combine(parentFolder, "resource.frk", filename); + OSXAppleDouble = Path.Combine(parentFolder, "._" + filename); + + // Check AppleDouble created by A/UX in ProDOS filesystem + if(File.Exists(ProDosAppleDouble)) + { + FileStream prodosStream = new FileStream(ProDosAppleDouble, FileMode.Open, FileAccess.Read); + if(prodosStream != null && prodosStream.Length > 26) + { + byte[] prodos_b = new byte[26]; + prodosStream.Read(prodos_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(prodos_b); + prodosStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + headerPath = ProDosAppleDouble; + } + } + + // Check AppleDouble created by A/UX in UFS filesystem + if(File.Exists(UNIXAppleDouble)) + { + FileStream unixStream = new FileStream(UNIXAppleDouble, FileMode.Open, FileAccess.Read); + if(unixStream != null && unixStream.Length > 26) + { + byte[] unix_b = new byte[26]; + unixStream.Read(unix_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(unix_b); + unixStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + headerPath = UNIXAppleDouble; + } + } + + // Check AppleDouble created by A/UX in FAT filesystem + if(File.Exists(DOSAppleDouble)) + { + FileStream dosStream = new FileStream(DOSAppleDouble, FileMode.Open, FileAccess.Read); + if(dosStream != null && dosStream.Length > 26) + { + byte[] dos_b = new byte[26]; + dosStream.Read(dos_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(dos_b); + dosStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + headerPath = DOSAppleDouble; + } + } + + // Check AppleDouble created by A/UX in case preserving FAT filesystem + if(File.Exists(DOSAppleDoubleLower)) + { + FileStream doslStream = new FileStream(DOSAppleDoubleLower, FileMode.Open, FileAccess.Read); + if(doslStream != null && doslStream.Length > 26) + { + byte[] dosl_b = new byte[26]; + doslStream.Read(dosl_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(dosl_b); + doslStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + headerPath = DOSAppleDoubleLower; + } + } + + // Check AppleDouble created by Netatalk + if(File.Exists(NetatalkAppleDouble)) + { + FileStream netatalkStream = new FileStream(NetatalkAppleDouble, FileMode.Open, FileAccess.Read); + if(netatalkStream != null && netatalkStream.Length > 26) + { + byte[] netatalk_b = new byte[26]; + netatalkStream.Read(netatalk_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(netatalk_b); + netatalkStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + headerPath = NetatalkAppleDouble; + } + } + + // Check AppleDouble created by DAVE + if(File.Exists(DAVEAppleDouble)) + { + FileStream daveStream = new FileStream(DAVEAppleDouble, FileMode.Open, FileAccess.Read); + if(daveStream != null && daveStream.Length > 26) + { + byte[] dave_b = new byte[26]; + daveStream.Read(dave_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(dave_b); + daveStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + headerPath = DAVEAppleDouble; + } + } + + // Check AppleDouble created by Mac OS X + if(File.Exists(OSXAppleDouble)) + { + FileStream osxStream = new FileStream(OSXAppleDouble, FileMode.Open, FileAccess.Read); + if(osxStream != null && osxStream.Length > 26) + { + byte[] osx_b = new byte[26]; + osxStream.Read(osx_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(osx_b); + osxStream.Close(); + if(header.magic == AppleDoubleMagic && (header.version == AppleDoubleVersion || header.version == AppleDoubleVersion2)) + headerPath = OSXAppleDouble; + } + } + + FileStream fs = new FileStream(headerPath, FileMode.Open, FileAccess.Read); + fs.Seek(0, SeekOrigin.Begin); + + byte[] hdr_b = new byte[26]; + fs.Read(hdr_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(hdr_b); + + AppleDoubleEntry[] entries = new AppleDoubleEntry[header.entries]; + for(int i = 0; i < header.entries; i++) + { + byte[] entry = new byte[12]; + fs.Read(entry, 0, 12); + entries[i] = BigEndianMarshal.ByteArrayToStructureBigEndian(entry); + } + + creationTime = DateTime.UtcNow; + lastWriteTime = creationTime; + foreach(AppleDoubleEntry entry in entries) + { + switch((AppleDoubleEntryID)entry.id) + { + case AppleDoubleEntryID.DataFork: + // AppleDouble have datafork in separated file + break; + case AppleDoubleEntryID.FileDates: + fs.Seek(entry.offset, SeekOrigin.Begin); + byte[] dates_b = new byte[16]; + fs.Read(dates_b, 0, 16); + AppleDoubleFileDates dates = BigEndianMarshal.ByteArrayToStructureBigEndian(dates_b); + creationTime = DateHandlers.AppleDoubleToDateTime(dates.creationDate); + lastWriteTime = DateHandlers.AppleDoubleToDateTime(dates.modificationDate); + break; + case AppleDoubleEntryID.FileInfo: + fs.Seek(entry.offset, SeekOrigin.Begin); + byte[] finfo = new byte[entry.length]; + fs.Read(finfo, 0, finfo.Length); + if(MacintoshHome.SequenceEqual(header.homeFilesystem)) + { + AppleDoubleMacFileInfo macinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(macinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate); + } + else if(ProDOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleDoubleProDOSFileInfo prodosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate); + } + else if(UNIXHome.SequenceEqual(header.homeFilesystem)) + { + AppleDoubleUNIXFileInfo unixinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.creationDate); + lastWriteTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.modificationDate); + } + else if(DOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleDoubleDOSFileInfo dosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + lastWriteTime = DateHandlers.DOSToDateTime(dosinfo.modificationDate, dosinfo.modificationTime); + } + break; + case AppleDoubleEntryID.ResourceFork: + rsrcFork = entry; + break; + } + } + + dataFork = new AppleDoubleEntry(); + dataFork.id = (uint)AppleDoubleEntryID.DataFork; + if(File.Exists(path)) + { + FileStream dataFs = new FileStream(path, FileMode.Open, FileAccess.Read); + dataFork.length = (uint)dataFs.Length; + dataFs.Close(); + } + + fs.Close(); + opened = true; + basePath = path; + } + } +} diff --git a/DiscImageChef.Filters/AppleSingle.cs b/DiscImageChef.Filters/AppleSingle.cs new file mode 100644 index 000000000..589063eae --- /dev/null +++ b/DiscImageChef.Filters/AppleSingle.cs @@ -0,0 +1,508 @@ +// /*************************************************************************** +// The Disc Image Chef +// ---------------------------------------------------------------------------- +// +// Filename : AppleSingle.cs +// Author(s) : Natalia Portillo +// +// Component : Component +// +// --[ Description ] ---------------------------------------------------------- +// +// Provides a filter to open AppleSingle files. +// +// --[ License ] -------------------------------------------------------------- +// +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 2.1 of the +// License, or (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2016 Natalia Portillo +// ****************************************************************************/ +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Linq; + +namespace DiscImageChef.Filters +{ + public class AppleSingle : Filter + { + enum AppleSingleEntryID : uint + { + Invalid = 0, + DataFork = 1, + ResourceFork = 2, + RealName = 3, + Comment = 4, + Icon = 5, + ColorIcon = 6, + FileInfo = 7, + FileDates = 8, + FinderInfo = 9, + MacFileInfo = 10, + ProDOSFileInfo = 11, + DOSFileInfo = 12, + ShortName = 13, + AFPFileInfo = 14, + DirectoryID = 15 + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleSingleHeader + { + public uint magic; + public uint version; + [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] + public byte[] homeFilesystem; + public ushort entries; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleSingleEntry + { + public uint id; + public uint offset; + public uint length; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleSingleFileDates + { + public uint creationDate; + public uint modificationDate; + public uint backupDate; + public uint accessDate; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleSingleMacFileInfo + { + public uint creationDate; + public uint modificationDate; + public uint backupDate; + public uint accessDate; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleSingleUNIXFileInfo + { + public uint creationDate; + public uint accessDate; + public uint modificationDate; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleSingleDOSFileInfo + { + public ushort modificationDate; + public ushort modificationTime; + public ushort attributes; + } + + [StructLayout(LayoutKind.Sequential, Pack = 1)] + struct AppleSingleProDOSFileInfo + { + public uint creationDate; + public uint modificationDate; + public uint backupDate; + public ushort access; + public ushort fileType; + public uint auxType; + } + + const uint AppleSingleMagic = 0x00051600; + const uint AppleSingleVersion = 0x00010000; + const uint AppleSingleVersion2 = 0x00020000; + + readonly byte[] MacintoshHome = { 0x4D, 0x61, 0x63, 0x69, 0x6E, 0x74, 0x6F, 0x73, 0x68, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] ProDOSHome = { 0x50, 0x72, 0x6F, 0x44, 0x4F, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] DOSHome = { 0x4D, 0x53, 0x2D, 0x44, 0x4F, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] UNIXHome = { 0x55, 0x6E, 0x69, 0x78, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] VMXHome = { 0x56, 0x41, 0x58, 0x20, 0x56, 0x4D, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + readonly byte[] OSXHome = { 0x4D, 0x61, 0x63, 0x20, 0x4F, 0x53, 0x20, 0x58, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20 }; + + AppleSingleEntry dataFork; + AppleSingleEntry rsrcFork; + byte[] bytes; + Stream stream; + bool isBytes, isStream, isPath, opened; + string basePath; + DateTime lastWriteTime; + DateTime creationTime; + AppleSingleHeader header; + + public AppleSingle() + { + Name = "AppleSingle"; + UUID = new Guid("A69B20E8-F4D3-42BB-BD2B-4A7263394A05"); + } + + public override void Close() + { + bytes = null; + if(stream != null) + stream.Close(); + isBytes = false; + isStream = false; + isPath = false; + opened = false; + } + + public override string GetBasePath() + { + return basePath; + } + + public override DateTime GetCreationTime() + { + return creationTime; + } + + public override long GetDataForkLength() + { + return dataFork.length; + } + + public override Stream GetDataForkStream() + { + if(dataFork.length == 0) + return null; + + if(isBytes) + return new OffsetStream(bytes, dataFork.offset, dataFork.offset + dataFork.length - 1); + if(isStream) + return new OffsetStream(stream, dataFork.offset, dataFork.offset + dataFork.length - 1); + if(isPath) + return new OffsetStream(basePath, FileMode.Open, FileAccess.Read, dataFork.offset, dataFork.offset + dataFork.length - 1); + + return null; + } + + public override string GetFilename() + { + return basePath != null ? Path.GetFileName(basePath) : null; + } + + public override DateTime GetLastWriteTime() + { + return lastWriteTime; + } + + public override long GetLength() + { + return dataFork.length + rsrcFork.length; + } + + public override string GetParentFolder() + { + return Path.GetDirectoryName(basePath); + } + + public override string GetPath() + { + return basePath; + } + + public override long GetResourceForkLength() + { + return rsrcFork.length; + } + + public override Stream GetResourceForkStream() + { + if(rsrcFork.length == 0) + return null; + + if(isBytes) + return new OffsetStream(bytes, rsrcFork.offset, rsrcFork.offset + rsrcFork.length - 1); + if(isStream) + return new OffsetStream(stream, rsrcFork.offset, rsrcFork.offset + rsrcFork.length - 1); + if(isPath) + return new OffsetStream(basePath, FileMode.Open, FileAccess.Read, rsrcFork.offset, rsrcFork.offset + rsrcFork.length - 1); + + return null; + } + + public override bool HasResourceFork() + { + return rsrcFork.length > 0; + } + + public override bool Identify(byte[] buffer) + { + if(buffer == null || buffer.Length < 26) + return false; + + byte[] hdr_b = new byte[26]; + Array.Copy(buffer, 0, hdr_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(hdr_b); + + return header.magic == AppleSingleMagic && (header.version == AppleSingleVersion || header.version == AppleSingleVersion2); + } + + public override bool Identify(Stream stream) + { + if(stream == null || stream.Length < 26) + return false; + + byte[] hdr_b = new byte[26]; + stream.Seek(0, SeekOrigin.Begin); + stream.Read(hdr_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(hdr_b); + + return header.magic == AppleSingleMagic && (header.version == AppleSingleVersion || header.version == AppleSingleVersion2); + } + + public override bool Identify(string path) + { + FileStream fstream = new FileStream(path, FileMode.Open, FileAccess.Read); + if(fstream == null || fstream.Length < 26) + return false; + + byte[] hdr_b = new byte[26]; + fstream.Read(hdr_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(hdr_b); + + fstream.Close(); + return header.magic == AppleSingleMagic && (header.version == AppleSingleVersion || header.version == AppleSingleVersion2); + } + + public override bool IsOpened() + { + return opened; + } + + public override void Open(byte[] buffer) + { + MemoryStream ms = new MemoryStream(buffer); + ms.Seek(0, SeekOrigin.Begin); + + byte[] hdr_b = new byte[26]; + ms.Read(hdr_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(hdr_b); + + AppleSingleEntry[] entries = new AppleSingleEntry[header.entries]; + for(int i = 0; i < header.entries; i++) + { + byte[] entry = new byte[12]; + ms.Read(entry, 0, 12); + entries[i] = BigEndianMarshal.ByteArrayToStructureBigEndian(entry); + } + + creationTime = DateTime.UtcNow; + lastWriteTime = creationTime; + foreach(AppleSingleEntry entry in entries) + { + switch((AppleSingleEntryID)entry.id) + { + case AppleSingleEntryID.DataFork: + dataFork = entry; + break; + case AppleSingleEntryID.FileDates: + ms.Seek(entry.offset, SeekOrigin.Begin); + byte[] dates_b = new byte[16]; + ms.Read(dates_b, 0, 16); + AppleSingleFileDates dates = BigEndianMarshal.ByteArrayToStructureBigEndian(dates_b); + creationTime = DateHandlers.AppleDoubleToDateTime(dates.creationDate); + lastWriteTime = DateHandlers.AppleDoubleToDateTime(dates.modificationDate); + break; + case AppleSingleEntryID.FileInfo: + ms.Seek(entry.offset, SeekOrigin.Begin); + byte[] finfo = new byte[entry.length]; + ms.Read(finfo, 0, finfo.Length); + if(MacintoshHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleMacFileInfo macinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(macinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate); + } + else if(ProDOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleProDOSFileInfo prodosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate); + } + else if(UNIXHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleUNIXFileInfo unixinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.creationDate); + lastWriteTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.modificationDate); + } + else if(DOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleDOSFileInfo dosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + lastWriteTime = DateHandlers.DOSToDateTime(dosinfo.modificationDate, dosinfo.modificationTime); + } + break; + case AppleSingleEntryID.ResourceFork: + rsrcFork = entry; + break; + } + } + + ms.Close(); + opened = true; + isBytes = true; + bytes = buffer; + } + + public override void Open(Stream stream) + { + stream.Seek(0, SeekOrigin.Begin); + + byte[] hdr_b = new byte[26]; + stream.Read(hdr_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(hdr_b); + + AppleSingleEntry[] entries = new AppleSingleEntry[header.entries]; + for(int i = 0; i < header.entries; i++) + { + byte[] entry = new byte[12]; + stream.Read(entry, 0, 12); + entries[i] = BigEndianMarshal.ByteArrayToStructureBigEndian(entry); + } + + creationTime = DateTime.UtcNow; + lastWriteTime = creationTime; + foreach(AppleSingleEntry entry in entries) + { + switch((AppleSingleEntryID)entry.id) + { + case AppleSingleEntryID.DataFork: + dataFork = entry; + break; + case AppleSingleEntryID.FileDates: + stream.Seek(entry.offset, SeekOrigin.Begin); + byte[] dates_b = new byte[16]; + stream.Read(dates_b, 0, 16); + AppleSingleFileDates dates = BigEndianMarshal.ByteArrayToStructureBigEndian(dates_b); + creationTime = DateHandlers.MacToDateTime(dates.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(dates.modificationDate); + break; + case AppleSingleEntryID.FileInfo: + stream.Seek(entry.offset, SeekOrigin.Begin); + byte[] finfo = new byte[entry.length]; + stream.Read(finfo, 0, finfo.Length); + if(MacintoshHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleMacFileInfo macinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(macinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate); + } + else if(ProDOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleProDOSFileInfo prodosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate); + } + else if(UNIXHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleUNIXFileInfo unixinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.creationDate); + lastWriteTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.modificationDate); + } + else if(DOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleDOSFileInfo dosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + lastWriteTime = DateHandlers.DOSToDateTime(dosinfo.modificationDate, dosinfo.modificationTime); + } + break; + case AppleSingleEntryID.ResourceFork: + rsrcFork = entry; + break; + } + } + + stream.Seek(0, SeekOrigin.Begin); + opened = true; + isStream = true; + this.stream = stream; + } + + public override void Open(string path) + { + FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); + fs.Seek(0, SeekOrigin.Begin); + + byte[] hdr_b = new byte[26]; + fs.Read(hdr_b, 0, 26); + header = BigEndianMarshal.ByteArrayToStructureBigEndian(hdr_b); + + AppleSingleEntry[] entries = new AppleSingleEntry[header.entries]; + for(int i = 0; i < header.entries; i++) + { + byte[] entry = new byte[12]; + fs.Read(entry, 0, 12); + entries[i] = BigEndianMarshal.ByteArrayToStructureBigEndian(entry); + } + + creationTime = DateTime.UtcNow; + lastWriteTime = creationTime; + foreach(AppleSingleEntry entry in entries) + { + switch((AppleSingleEntryID)entry.id) + { + case AppleSingleEntryID.DataFork: + dataFork = entry; + break; + case AppleSingleEntryID.FileDates: + fs.Seek(entry.offset, SeekOrigin.Begin); + byte[] dates_b = new byte[16]; + fs.Read(dates_b, 0, 16); + AppleSingleFileDates dates = BigEndianMarshal.ByteArrayToStructureBigEndian(dates_b); + creationTime = DateHandlers.MacToDateTime(dates.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(dates.modificationDate); + break; + case AppleSingleEntryID.FileInfo: + fs.Seek(entry.offset, SeekOrigin.Begin); + byte[] finfo = new byte[entry.length]; + fs.Read(finfo, 0, finfo.Length); + if(MacintoshHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleMacFileInfo macinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(macinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate); + } + else if(ProDOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleProDOSFileInfo prodosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate); + lastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate); + } + else if(UNIXHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleUNIXFileInfo unixinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + creationTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.creationDate); + lastWriteTime = DateHandlers.UNIXUnsignedToDateTime(unixinfo.modificationDate); + } + else if(DOSHome.SequenceEqual(header.homeFilesystem)) + { + AppleSingleDOSFileInfo dosinfo = BigEndianMarshal.ByteArrayToStructureBigEndian(finfo); + lastWriteTime = DateHandlers.DOSToDateTime(dosinfo.modificationDate, dosinfo.modificationTime); + } + break; + case AppleSingleEntryID.ResourceFork: + rsrcFork = entry; + break; + } + } + + fs.Close(); + opened = true; + isPath = true; + basePath = path; + } + } +} + diff --git a/DiscImageChef.Filters/ChangeLog b/DiscImageChef.Filters/ChangeLog index f5c8ee7fe..abd635436 100644 --- a/DiscImageChef.Filters/ChangeLog +++ b/DiscImageChef.Filters/ChangeLog @@ -1,3 +1,10 @@ +2016-09-05 Natalia Portillo + + * AppleDouble.cs: + * AppleSingle.cs: + * DiscImageChef.Filters.csproj: Added AppleSingle and + AppleDouble filters. + 2016-09-05 Natalia Portillo * GZip.cs: diff --git a/DiscImageChef.Filters/DiscImageChef.Filters.csproj b/DiscImageChef.Filters/DiscImageChef.Filters.csproj index bcf3b2010..371857f56 100644 --- a/DiscImageChef.Filters/DiscImageChef.Filters.csproj +++ b/DiscImageChef.Filters/DiscImageChef.Filters.csproj @@ -39,6 +39,8 @@ + + diff --git a/DiscImageChef.Helpers/ChangeLog b/DiscImageChef.Helpers/ChangeLog index 0374ccada..d688afe15 100644 --- a/DiscImageChef.Helpers/ChangeLog +++ b/DiscImageChef.Helpers/ChangeLog @@ -1,3 +1,7 @@ +2016-09-05 Natalia Portillo + + * DateHandlers.cs: Added AppleSingle and AppleDouble filters. + 2016-09-02 Natalia Portillo * DateHandlers.cs: Adds support for NILFS2 filesystem. diff --git a/DiscImageChef.Helpers/DateHandlers.cs b/DiscImageChef.Helpers/DateHandlers.cs index da2f873af..e634e713d 100644 --- a/DiscImageChef.Helpers/DateHandlers.cs +++ b/DiscImageChef.Helpers/DateHandlers.cs @@ -43,6 +43,7 @@ namespace DiscImageChef // Day 0 of Julian Date system static readonly DateTime JulianEpoch = new DateTime(1858, 11, 17, 0, 0, 0); static readonly DateTime AmigaEpoch = new DateTime(1978, 1, 1, 0, 0, 0); + static readonly DateTime AppleDoubleEpoch = new DateTime(1970, 1, 1, 0, 0, 0); public static DateTime MacToDateTime(ulong MacTimeStamp) { @@ -192,6 +193,11 @@ namespace DiscImageChef return temp; } + + public static DateTime AppleDoubleToDateTime(ulong AppleDoubleTimeStamp) + { + return AppleDoubleEpoch.AddSeconds(AppleDoubleTimeStamp); + } } } diff --git a/README.md b/README.md index 9833b7997..0be1fdf56 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,8 @@ Supported checksums Supported filters ================= * GZip +* AppleDouble +* AppleSingle Changelog ========= diff --git a/TODO b/TODO index 69bcfbb5f..210275f49 100644 --- a/TODO +++ b/TODO @@ -70,8 +70,6 @@ UDIF plugin: Filters: --- Add support for BZip2 compressed files --- Add support for XZ compressed files ---- Add support for AppleSingle files ---- Add support for AppleDouble files --- Add support for MacBinary files --- Add support for ZIP archives --- Add support for 7Z archives \ No newline at end of file diff --git a/docs/AppleSingle_AppleDouble_v1.pdf b/docs/AppleSingle_AppleDouble_v1.pdf new file mode 100644 index 000000000..d1e6a0d06 Binary files /dev/null and b/docs/AppleSingle_AppleDouble_v1.pdf differ diff --git a/docs/AppleSingle_AppleDouble_v2.pdf b/docs/AppleSingle_AppleDouble_v2.pdf new file mode 100644 index 000000000..77d04cef5 Binary files /dev/null and b/docs/AppleSingle_AppleDouble_v2.pdf differ