2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2016-09-12 01:13:12 +01:00
|
|
|
// The Disc Image Chef
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : PCExchange.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Filters.
|
2016-09-12 01:13:12 +01:00
|
|
|
//
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Provides a filter to open handle files written by PCExchange in FAT
|
|
|
|
|
// volumes
|
|
|
|
|
//
|
|
|
|
|
// --[ 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2020-01-03 17:51:30 +00:00
|
|
|
// Copyright © 2011-2020 Natalia Portillo
|
2016-09-12 01:13:12 +01:00
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
2016-09-12 01:13:12 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Aaru.CommonTypes.Interfaces;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2020-02-27 00:33:26 +00:00
|
|
|
namespace Aaru.Filters
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
2017-12-23 04:06:02 +00:00
|
|
|
/// <summary>
|
2017-12-24 02:43:49 +00:00
|
|
|
/// Decodes PCExchange files
|
2017-12-23 04:06:02 +00:00
|
|
|
/// </summary>
|
2017-12-26 06:05:12 +00:00
|
|
|
public class PCExchange : IFilter
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
const string FILE_ID = "FILEID.DAT";
|
2017-12-22 16:17:40 +00:00
|
|
|
const string FINDER_INFO = "FINDER.DAT";
|
2018-06-22 08:08:38 +01:00
|
|
|
const string RESOURCES = "RESOURCE.FRK";
|
|
|
|
|
string basePath;
|
|
|
|
|
DateTime creationTime;
|
|
|
|
|
long dataLen;
|
|
|
|
|
string dataPath;
|
|
|
|
|
DateTime lastWriteTime;
|
|
|
|
|
|
|
|
|
|
bool opened;
|
|
|
|
|
long rsrcLen;
|
2017-12-24 02:43:49 +00:00
|
|
|
string rsrcPath;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string Name => "PCExchange";
|
|
|
|
|
public Guid Id => new Guid("9264EB9F-D634-4F9B-BE12-C24CD44988C6");
|
|
|
|
|
public string Author => "Natalia Portillo";
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Close()
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
|
|
|
|
opened = false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string GetBasePath() => basePath;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public DateTime GetCreationTime() => creationTime;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public long GetDataForkLength() => dataLen;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public Stream GetDataForkStream() => new FileStream(dataPath, FileMode.Open, FileAccess.Read);
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string GetFilename() => Path.GetFileName(basePath);
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public DateTime GetLastWriteTime() => lastWriteTime;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public long GetLength() => dataLen + rsrcLen;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string GetParentFolder() => Path.GetDirectoryName(basePath);
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public string GetPath() => basePath;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public long GetResourceForkLength() => rsrcLen;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public Stream GetResourceForkStream() => new FileStream(rsrcPath, FileMode.Open, FileAccess.Read);
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public bool HasResourceFork() => rsrcPath != null;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public bool Identify(byte[] buffer) => false;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public bool Identify(Stream stream) => false;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public bool Identify(string path)
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
|
|
|
|
string parentFolder = Path.GetDirectoryName(path);
|
|
|
|
|
|
2019-05-30 19:56:12 +01:00
|
|
|
parentFolder = parentFolder ?? "";
|
|
|
|
|
|
|
|
|
|
if(!File.Exists(Path.Combine(parentFolder, FINDER_INFO))) return false;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-12-22 16:17:40 +00:00
|
|
|
if(!Directory.Exists(Path.Combine(parentFolder, RESOURCES))) return false;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
|
|
|
|
string baseFilename = Path.GetFileName(path);
|
|
|
|
|
|
|
|
|
|
bool dataFound = false;
|
|
|
|
|
bool rsrcFound = false;
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
FileStream finderDatStream =
|
2017-12-22 16:17:40 +00:00
|
|
|
new FileStream(Path.Combine(parentFolder, FINDER_INFO), FileMode.Open, FileAccess.Read);
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-06-29 23:41:14 +01:00
|
|
|
while(finderDatStream.Position + 0x5C <= finderDatStream.Length)
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
PCExchangeEntry datEntry = new PCExchangeEntry();
|
|
|
|
|
byte[] datEntry_b = new byte[Marshal.SizeOf(datEntry)];
|
2016-09-12 01:13:12 +01:00
|
|
|
finderDatStream.Read(datEntry_b, 0, Marshal.SizeOf(datEntry));
|
2020-02-27 00:33:26 +00:00
|
|
|
datEntry = Aaru.Helpers.Marshal.ByteArrayToStructureBigEndian<PCExchangeEntry>(datEntry_b);
|
2017-06-06 21:23:20 +01:00
|
|
|
// TODO: Add support for encoding on filters
|
2018-06-22 08:08:38 +01:00
|
|
|
string macName =
|
|
|
|
|
StringHandlers.PascalToString(datEntry.macName, Encoding.GetEncoding("macintosh"));
|
2016-09-12 01:13:12 +01:00
|
|
|
byte[] tmpDosName_b = new byte[8];
|
2018-06-22 08:08:38 +01:00
|
|
|
byte[] tmpDosExt_b = new byte[3];
|
2016-09-12 01:13:12 +01:00
|
|
|
Array.Copy(datEntry.dosName, 0, tmpDosName_b, 0, 8);
|
2018-06-22 08:08:38 +01:00
|
|
|
Array.Copy(datEntry.dosName, 8, tmpDosExt_b, 0, 3);
|
2017-12-20 17:26:28 +00:00
|
|
|
string dosName = Encoding.ASCII.GetString(tmpDosName_b).Trim() + "." +
|
|
|
|
|
Encoding.ASCII.GetString(tmpDosExt_b).Trim();
|
2016-09-12 01:13:12 +01:00
|
|
|
string dosNameLow = dosName.ToLower(CultureInfo.CurrentCulture);
|
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(baseFilename != macName && baseFilename != dosName && baseFilename != dosNameLow) continue;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-12-24 02:43:49 +00:00
|
|
|
dataFound |=
|
|
|
|
|
File.Exists(Path.Combine(parentFolder, macName ?? throw new InvalidOperationException())) ||
|
2018-06-22 08:08:38 +01:00
|
|
|
File.Exists(Path.Combine(parentFolder, dosName)) ||
|
2017-12-24 02:43:49 +00:00
|
|
|
File.Exists(Path.Combine(parentFolder, dosNameLow));
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-12-22 16:17:40 +00:00
|
|
|
rsrcFound |= File.Exists(Path.Combine(parentFolder, RESOURCES, dosName)) ||
|
|
|
|
|
File.Exists(Path.Combine(parentFolder, RESOURCES, dosNameLow));
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
break;
|
2016-09-12 01:13:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finderDatStream.Close();
|
|
|
|
|
|
|
|
|
|
return dataFound && rsrcFound;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-29 22:15:43 +01:00
|
|
|
public bool IsOpened() => opened;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(byte[] buffer)
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(Stream stream)
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 07:28:40 +00:00
|
|
|
public void Open(string path)
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
|
|
|
|
string parentFolder = Path.GetDirectoryName(path);
|
|
|
|
|
string baseFilename = Path.GetFileName(path);
|
|
|
|
|
|
2019-05-30 19:56:12 +01:00
|
|
|
parentFolder = parentFolder ?? "";
|
|
|
|
|
|
2017-12-19 20:33:03 +00:00
|
|
|
FileStream finderDatStream =
|
2019-05-30 19:56:12 +01:00
|
|
|
new FileStream(Path.Combine(parentFolder, FINDER_INFO), FileMode.Open, FileAccess.Read);
|
2016-09-12 01:13:12 +01:00
|
|
|
|
2017-06-29 23:41:14 +01:00
|
|
|
while(finderDatStream.Position + 0x5C <= finderDatStream.Length)
|
2016-09-12 01:13:12 +01:00
|
|
|
{
|
2018-06-22 08:08:38 +01:00
|
|
|
PCExchangeEntry datEntry = new PCExchangeEntry();
|
|
|
|
|
byte[] datEntry_b = new byte[Marshal.SizeOf(datEntry)];
|
2016-09-12 01:13:12 +01:00
|
|
|
finderDatStream.Read(datEntry_b, 0, Marshal.SizeOf(datEntry));
|
2020-02-27 00:33:26 +00:00
|
|
|
datEntry = Aaru.Helpers.Marshal.ByteArrayToStructureBigEndian<PCExchangeEntry>(datEntry_b);
|
2018-06-22 08:08:38 +01:00
|
|
|
string macName =
|
|
|
|
|
StringHandlers.PascalToString(datEntry.macName, Encoding.GetEncoding("macintosh"));
|
2016-09-12 01:13:12 +01:00
|
|
|
byte[] tmpDosName_b = new byte[8];
|
2018-06-22 08:08:38 +01:00
|
|
|
byte[] tmpDosExt_b = new byte[3];
|
2016-09-12 01:13:12 +01:00
|
|
|
Array.Copy(datEntry.dosName, 0, tmpDosName_b, 0, 8);
|
2018-06-22 08:08:38 +01:00
|
|
|
Array.Copy(datEntry.dosName, 8, tmpDosExt_b, 0, 3);
|
2017-12-20 17:26:28 +00:00
|
|
|
string dosName = Encoding.ASCII.GetString(tmpDosName_b).Trim() + "." +
|
|
|
|
|
Encoding.ASCII.GetString(tmpDosExt_b).Trim();
|
2016-09-12 01:13:12 +01:00
|
|
|
string dosNameLow = dosName.ToLower(CultureInfo.CurrentCulture);
|
|
|
|
|
|
2017-12-21 06:06:19 +00:00
|
|
|
if(baseFilename != macName && baseFilename != dosName && baseFilename != dosNameLow) continue;
|
|
|
|
|
|
2017-12-24 02:43:49 +00:00
|
|
|
if(File.Exists(Path.Combine(parentFolder, macName ?? throw new InvalidOperationException())))
|
|
|
|
|
dataPath = Path.Combine(parentFolder, macName);
|
2017-12-21 06:06:19 +00:00
|
|
|
else if(File.Exists(Path.Combine(parentFolder, dosName)))
|
|
|
|
|
dataPath = Path.Combine(parentFolder, dosName);
|
|
|
|
|
else if(File.Exists(Path.Combine(parentFolder, dosNameLow)))
|
2018-06-22 08:08:38 +01:00
|
|
|
dataPath = Path.Combine(parentFolder, dosNameLow);
|
2017-12-21 06:06:19 +00:00
|
|
|
else dataPath = null;
|
|
|
|
|
|
2017-12-22 16:17:40 +00:00
|
|
|
if(File.Exists(Path.Combine(parentFolder, RESOURCES, dosName)))
|
|
|
|
|
rsrcPath = Path.Combine(parentFolder, RESOURCES, dosName);
|
|
|
|
|
else if(File.Exists(Path.Combine(parentFolder, RESOURCES, dosNameLow)))
|
2018-06-22 08:08:38 +01:00
|
|
|
rsrcPath = Path.Combine(parentFolder, RESOURCES, dosNameLow);
|
2017-12-21 06:06:19 +00:00
|
|
|
else rsrcPath = null;
|
|
|
|
|
|
|
|
|
|
lastWriteTime = DateHandlers.MacToDateTime(datEntry.modificationDate);
|
2018-06-22 08:08:38 +01:00
|
|
|
creationTime = DateHandlers.MacToDateTime(datEntry.creationDate);
|
2017-12-21 06:06:19 +00:00
|
|
|
|
|
|
|
|
break;
|
2016-09-12 01:13:12 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-21 14:41:38 +00:00
|
|
|
dataLen = new FileInfo(dataPath ?? throw new InvalidOperationException()).Length;
|
|
|
|
|
rsrcLen = new FileInfo(rsrcPath ?? throw new InvalidOperationException()).Length;
|
2016-09-12 01:13:12 +01:00
|
|
|
|
|
|
|
|
basePath = path;
|
2018-06-22 08:08:38 +01:00
|
|
|
opened = true;
|
2017-06-29 23:41:14 +01:00
|
|
|
|
|
|
|
|
finderDatStream.Close();
|
2016-09-12 01:13:12 +01:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
struct PCExchangeEntry
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Name in Macintosh. If PCExchange version supports FAT's LFN they are the same.
|
|
|
|
|
/// Illegal characters for FAT get substituted with '_' both here and in FAT's LFN entry.
|
|
|
|
|
/// </summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly byte[] macName;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// File type
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly uint type;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// File creator
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly uint creator;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Finder flags
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly ushort fdFlags;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// File's icon vertical position within its window
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly ushort verticalPosition;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// File's icon horizontal position within its window
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly ushort horizontalPosition;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Unknown, all bytes are empty but last, except in volume's label entry
|
|
|
|
|
/// </summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 18)]
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly byte[] unknown1;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// File's creation date
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly uint creationDate;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// File's modification date
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly uint modificationDate;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// File's last backup date
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly uint backupDate;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Unknown, but is unique, starts 0x7FFFFFFF and counts in reverse.
|
|
|
|
|
/// Probably file ID for alias look up?
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly uint unknown2;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Name as in FAT entry (not LFN).
|
|
|
|
|
/// Resource fork file is always using this name, never LFN.
|
|
|
|
|
/// </summary>
|
2018-06-22 08:08:38 +01:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly byte[] dosName;
|
2017-12-24 02:43:49 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Unknown, flags?
|
|
|
|
|
/// </summary>
|
2019-05-30 19:56:12 +01:00
|
|
|
public readonly byte unknown3;
|
2017-12-24 02:43:49 +00:00
|
|
|
}
|
2016-09-12 01:13:12 +01:00
|
|
|
}
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|