Files
Aaru/Aaru.Filters/PCExchange.cs

289 lines
11 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2016-09-12 01:13:12 +01:00
// ----------------------------------------------------------------------------
//
// Filename : PCExchange.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// 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/>.
//
// ----------------------------------------------------------------------------
2024-12-19 10:45:18 +00:00
// Copyright © 2011-2025 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;
2020-07-20 07:47:12 +01:00
using System.Diagnostics.CodeAnalysis;
2016-09-12 01:13:12 +01:00
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Aaru.CommonTypes.Attributes;
2021-09-16 04:42:14 +01:00
using Aaru.CommonTypes.Enums;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
using Marshal = System.Runtime.InteropServices.Marshal;
2016-09-12 01:13:12 +01:00
namespace Aaru.Filters;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Decodes PCExchange files</summary>
[SuppressMessage("ReSharper", "UnusedMember.Local")]
public sealed partial class PcExchange : IFilter
2022-03-06 13:29:38 +00:00
{
const string FILE_ID = "FILEID.DAT";
const string FINDER_INFO = "FINDER.DAT";
const string RESOURCES = "RESOURCE.FRK";
string _dataPath;
string _rsrcPath;
2016-09-12 01:13:12 +01:00
2023-10-03 23:23:41 +01:00
#region IFilter Members
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string Name => Localization.PcExchange_Name;
2023-10-03 23:23:41 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Guid Id => new("9264EB9F-D634-4F9B-BE12-C24CD44988C6");
2023-10-03 23:23:41 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string Author => Authors.NataliaPortillo;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public void Close() {}
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string BasePath { get; private set; }
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public DateTime CreationTime { get; private set; }
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public long DataForkLength { get; private set; }
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Stream GetDataForkStream() => new FileStream(_dataPath, FileMode.Open, FileAccess.Read);
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string Filename => System.IO.Path.GetFileName(BasePath);
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public DateTime LastWriteTime { get; private set; }
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public long Length => DataForkLength + ResourceForkLength;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string ParentFolder => System.IO.Path.GetDirectoryName(BasePath);
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public string Path => BasePath;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public long ResourceForkLength { get; private set; }
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public Stream GetResourceForkStream() => new FileStream(_rsrcPath, FileMode.Open, FileAccess.Read);
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool HasResourceFork => _rsrcPath != null;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(byte[] buffer) => false;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(Stream stream) => false;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public bool Identify(string path)
{
string parentFolder = System.IO.Path.GetDirectoryName(path);
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
parentFolder ??= "";
2016-09-12 01:13:12 +01:00
2024-05-01 04:05:22 +01:00
if(!File.Exists(System.IO.Path.Combine(parentFolder, FINDER_INFO))) return false;
2016-09-12 01:13:12 +01:00
2024-05-01 04:05:22 +01:00
if(!Directory.Exists(System.IO.Path.Combine(parentFolder, RESOURCES))) return false;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
string baseFilename = System.IO.Path.GetFileName(path);
2016-09-12 01:13:12 +01:00
2023-10-03 23:23:41 +01:00
var dataFound = false;
var rsrcFound = false;
2020-02-29 18:03:35 +00:00
2024-05-01 04:05:22 +01:00
var finderDatStream = new FileStream(System.IO.Path.Combine(parentFolder, FINDER_INFO),
FileMode.Open,
2022-03-06 13:29:38 +00:00
FileAccess.Read);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
while(finderDatStream.Position + 0x5C <= finderDatStream.Length)
{
2023-10-03 23:23:41 +01:00
var datEntry = new Entry();
var datEntryB = new byte[Marshal.SizeOf(datEntry)];
finderDatStream.EnsureRead(datEntryB, 0, Marshal.SizeOf(datEntry));
datEntry = Helpers.Marshal.ByteArrayToStructureBigEndian<Entry>(datEntryB);
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
// TODO: Add support for encoding on filters
string macName = StringHandlers.PascalToString(datEntry.macName, Encoding.GetEncoding("macintosh"));
2020-02-29 18:03:35 +00:00
2023-10-03 23:23:41 +01:00
var tmpDosNameB = new byte[8];
var tmpDosExtB = new byte[3];
2022-03-06 13:29:38 +00:00
Array.Copy(datEntry.dosName, 0, tmpDosNameB, 0, 8);
2023-10-03 23:23:41 +01:00
Array.Copy(datEntry.dosName, 8, tmpDosExtB, 0, 3);
2016-09-12 01:13:12 +01:00
string dosName = Encoding.ASCII.GetString(tmpDosNameB).Trim() +
"." +
2022-03-06 13:29:38 +00:00
Encoding.ASCII.GetString(tmpDosExtB).Trim();
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
string dosNameLow = dosName.ToLower(CultureInfo.CurrentCulture);
2016-09-12 01:13:12 +01:00
2024-05-01 04:05:22 +01:00
if(baseFilename != macName && baseFilename != dosName && baseFilename != dosNameLow) continue;
2022-03-06 13:29:38 +00:00
dataFound |=
2022-03-07 07:36:44 +00:00
File.Exists(System.IO.Path.Combine(parentFolder, macName ?? throw new InvalidOperationException())) ||
File.Exists(System.IO.Path.Combine(parentFolder, dosName)) ||
2022-03-06 13:29:38 +00:00
File.Exists(System.IO.Path.Combine(parentFolder, dosNameLow));
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
rsrcFound |= File.Exists(System.IO.Path.Combine(parentFolder, RESOURCES, dosName)) ||
File.Exists(System.IO.Path.Combine(parentFolder, RESOURCES, dosNameLow));
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
break;
2016-09-12 01:13:12 +01:00
}
2022-03-06 13:29:38 +00:00
finderDatStream.Close();
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
return dataFound && rsrcFound;
}
2020-02-29 18:03:35 +00:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(byte[] buffer) => ErrorNumber.NotSupported;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(Stream stream) => ErrorNumber.NotSupported;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
public ErrorNumber Open(string path)
{
string parentFolder = System.IO.Path.GetDirectoryName(path);
string baseFilename = System.IO.Path.GetFileName(path);
2022-03-06 13:29:38 +00:00
parentFolder ??= "";
2024-05-01 04:05:22 +01:00
var finderDatStream = new FileStream(System.IO.Path.Combine(parentFolder, FINDER_INFO),
FileMode.Open,
2022-03-06 13:29:38 +00:00
FileAccess.Read);
2022-03-06 13:29:38 +00:00
while(finderDatStream.Position + 0x5C <= finderDatStream.Length)
{
2023-10-03 23:23:41 +01:00
var datEntry = new Entry();
var datEntryB = new byte[Marshal.SizeOf(datEntry)];
finderDatStream.EnsureRead(datEntryB, 0, Marshal.SizeOf(datEntry));
datEntry = Helpers.Marshal.ByteArrayToStructureBigEndian<Entry>(datEntryB);
2022-03-06 13:29:38 +00:00
string macName = StringHandlers.PascalToString(datEntry.macName, Encoding.GetEncoding("macintosh"));
2023-10-03 23:23:41 +01:00
var tmpDosNameB = new byte[8];
var tmpDosExtB = new byte[3];
2022-03-06 13:29:38 +00:00
Array.Copy(datEntry.dosName, 0, tmpDosNameB, 0, 8);
2023-10-03 23:23:41 +01:00
Array.Copy(datEntry.dosName, 8, tmpDosExtB, 0, 3);
2022-03-06 13:29:38 +00:00
string dosName = Encoding.ASCII.GetString(tmpDosNameB).Trim() +
"." +
2022-03-06 13:29:38 +00:00
Encoding.ASCII.GetString(tmpDosExtB).Trim();
string dosNameLow = dosName.ToLower(CultureInfo.CurrentCulture);
2024-05-01 04:05:22 +01:00
if(baseFilename != macName && baseFilename != dosName && baseFilename != dosNameLow) continue;
2022-03-06 13:29:38 +00:00
if(File.Exists(System.IO.Path.Combine(parentFolder, macName ?? throw new InvalidOperationException())))
_dataPath = System.IO.Path.Combine(parentFolder, macName);
else if(File.Exists(System.IO.Path.Combine(parentFolder, dosName)))
_dataPath = System.IO.Path.Combine(parentFolder, dosName);
else if(File.Exists(System.IO.Path.Combine(parentFolder, dosNameLow)))
_dataPath = System.IO.Path.Combine(parentFolder, dosNameLow);
else
_dataPath = null;
if(File.Exists(System.IO.Path.Combine(parentFolder, RESOURCES, dosName)))
_rsrcPath = System.IO.Path.Combine(parentFolder, RESOURCES, dosName);
else if(File.Exists(System.IO.Path.Combine(parentFolder, RESOURCES, dosNameLow)))
_rsrcPath = System.IO.Path.Combine(parentFolder, RESOURCES, dosNameLow);
else
_rsrcPath = null;
LastWriteTime = DateHandlers.MacToDateTime(datEntry.modificationDate);
CreationTime = DateHandlers.MacToDateTime(datEntry.creationDate);
break;
}
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
DataForkLength = new FileInfo(_dataPath ?? throw new InvalidOperationException()).Length;
ResourceForkLength = new FileInfo(_rsrcPath ?? throw new InvalidOperationException()).Length;
2016-09-12 01:13:12 +01:00
2022-03-06 13:29:38 +00:00
BasePath = path;
2022-03-06 13:29:38 +00:00
finderDatStream.Close();
2021-09-15 13:03:42 +01:00
2022-03-06 13:29:38 +00:00
return ErrorNumber.NoError;
}
2023-10-03 23:23:41 +01:00
#endregion
#region Nested type: Entry
2022-03-06 13:29:38 +00:00
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[SwapEndian]
partial struct Entry
2022-03-06 13:29:38 +00:00
{
/// <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>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public byte[] macName;
2022-03-06 13:29:38 +00:00
/// <summary>File type</summary>
public uint type;
2022-03-06 13:29:38 +00:00
/// <summary>File creator</summary>
public uint creator;
2022-03-06 13:29:38 +00:00
/// <summary>Finder flags</summary>
public ushort fdFlags;
2022-03-06 13:29:38 +00:00
/// <summary>File's icon vertical position within its window</summary>
public ushort verticalPosition;
2022-03-06 13:29:38 +00:00
/// <summary>File's icon horizontal position within its window</summary>
public ushort horizontalPosition;
2022-03-06 13:29:38 +00:00
/// <summary>Unknown, all bytes are empty but last, except in volume's label entry</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 18)]
public byte[] unknown1;
2022-03-06 13:29:38 +00:00
/// <summary>File's creation date</summary>
public uint creationDate;
2022-03-06 13:29:38 +00:00
/// <summary>File's modification date</summary>
public uint modificationDate;
2022-03-06 13:29:38 +00:00
/// <summary>File's last backup date</summary>
public uint backupDate;
2022-03-06 13:29:38 +00:00
/// <summary>Unknown, but is unique, starts 0x7FFFFFFF and counts in reverse. Probably file ID for alias look up?</summary>
public uint unknown2;
2022-03-06 13:29:38 +00:00
/// <summary>Name as in FAT entry (not LFN). Resource fork file is always using this name, never LFN.</summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 11)]
public byte[] dosName;
2022-03-06 13:29:38 +00:00
/// <summary>Unknown, flags?</summary>
public byte unknown3;
2016-09-12 01:13:12 +01:00
}
2023-10-03 23:23:41 +01:00
#endregion
2017-12-19 20:33:03 +00:00
}