2017-05-19 20:28:49 +01:00
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
// Aaru Data Preservation Suite
|
2016-09-05 21:22:04 +01:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Filename : AppleSingle.cs
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
//
|
2017-12-19 03:50:57 +00:00
|
|
|
// Component : Filters.
|
2016-09-05 21:22:04 +01:00
|
|
|
//
|
|
|
|
|
// --[ 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
//
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2024-12-19 10:45:18 +00:00
|
|
|
// Copyright © 2011-2025 Natalia Portillo
|
2016-09-05 21:22:04 +01:00
|
|
|
// ****************************************************************************/
|
2017-12-19 19:33:46 +00:00
|
|
|
|
2016-09-05 21:22:04 +01:00
|
|
|
using System;
|
2020-07-20 07:47:12 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2016-09-05 21:22:04 +01:00
|
|
|
using System.IO;
|
2017-12-19 19:33:46 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2025-10-21 04:00:00 +01:00
|
|
|
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;
|
2023-10-07 21:29:49 +01:00
|
|
|
using Aaru.Helpers.IO;
|
2020-02-27 00:33:26 +00:00
|
|
|
using Marshal = Aaru.Helpers.Marshal;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
namespace Aaru.Filters;
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
/// <summary>Decodes AppleSingle files</summary>
|
|
|
|
|
[SuppressMessage("ReSharper", "UnusedMember.Local")]
|
2025-10-21 04:00:00 +01:00
|
|
|
public sealed partial class AppleSingle : IFilter
|
2016-09-05 21:22:04 +01:00
|
|
|
{
|
2023-09-29 18:27:27 +01:00
|
|
|
const uint MAGIC = 0x00051600;
|
|
|
|
|
const uint VERSION = 0x00010000;
|
|
|
|
|
const uint VERSION2 = 0x00020000;
|
|
|
|
|
readonly byte[] _dosHome = "MS-DOS "u8.ToArray();
|
|
|
|
|
readonly byte[] _macintoshHome = "Macintosh "u8.ToArray();
|
|
|
|
|
readonly byte[] _osxHome = "Mac OS X "u8.ToArray();
|
|
|
|
|
readonly byte[] _proDosHome = "ProDOS "u8.ToArray();
|
|
|
|
|
readonly byte[] _unixHome = "Unix "u8.ToArray();
|
|
|
|
|
readonly byte[] _vmsHome = "VAX VMS "u8.ToArray();
|
|
|
|
|
byte[] _bytes;
|
|
|
|
|
Entry _dataFork;
|
|
|
|
|
Header _header;
|
|
|
|
|
bool _isBytes, _isStream, _isPath;
|
|
|
|
|
Entry _rsrcFork;
|
|
|
|
|
Stream _stream;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
#region IFilter Members
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-11-28 10:15:47 +00:00
|
|
|
public string Name => Localization.AppleSingle_Name;
|
2023-10-03 23:23:41 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Guid Id => new("A69B20E8-F4D3-42BB-BD2B-4A7263394A05");
|
2023-10-03 23:23:41 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
2022-11-28 10:15:47 +00:00
|
|
|
public string Author => Authors.NataliaPortillo;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2021-08-17 14:40:37 +01:00
|
|
|
/// <inheritdoc />
|
2022-03-06 13:29:38 +00:00
|
|
|
public void Close()
|
2016-09-05 21:22:04 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
_bytes = null;
|
|
|
|
|
_stream?.Close();
|
|
|
|
|
_isBytes = false;
|
|
|
|
|
_isStream = false;
|
|
|
|
|
_isPath = false;
|
|
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string BasePath { get; private set; }
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public DateTime CreationTime { get; private set; }
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public long DataForkLength => _dataFork.length;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Stream GetDataForkStream()
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_dataFork.length == 0) return null;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_isBytes) return new OffsetStream(_bytes, _dataFork.offset, _dataFork.offset + _dataFork.length - 1);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_isStream) return new OffsetStream(_stream, _dataFork.offset, _dataFork.offset + _dataFork.length - 1);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_isPath)
|
2023-10-03 23:23:41 +01:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
return new OffsetStream(BasePath,
|
|
|
|
|
FileMode.Open,
|
|
|
|
|
FileAccess.Read,
|
|
|
|
|
_dataFork.offset,
|
2022-03-06 13:29:38 +00:00
|
|
|
_dataFork.offset + _dataFork.length - 1);
|
2023-10-03 23:23:41 +01:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string Filename => System.IO.Path.GetFileName(BasePath);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public DateTime LastWriteTime { get; private set; }
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public long Length => _dataFork.length + _rsrcFork.length;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string ParentFolder => System.IO.Path.GetDirectoryName(BasePath);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public string Path => BasePath;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public long ResourceForkLength => _rsrcFork.length;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public Stream GetResourceForkStream()
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_rsrcFork.length == 0) return null;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_isBytes) return new OffsetStream(_bytes, _rsrcFork.offset, _rsrcFork.offset + _rsrcFork.length - 1);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(_isStream) return new OffsetStream(_stream, _rsrcFork.offset, _rsrcFork.offset + _rsrcFork.length - 1);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
if(_isPath)
|
2023-10-03 23:23:41 +01:00
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
return new OffsetStream(BasePath,
|
|
|
|
|
FileMode.Open,
|
|
|
|
|
FileAccess.Read,
|
|
|
|
|
_rsrcFork.offset,
|
2022-03-06 13:29:38 +00:00
|
|
|
_rsrcFork.offset + _rsrcFork.length - 1);
|
2023-10-03 23:23:41 +01:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool HasResourceFork => _rsrcFork.length > 0;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool Identify(byte[] buffer)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(buffer == null || buffer.Length < 26) return false;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var hdrB = new byte[26];
|
2022-03-06 13:29:38 +00:00
|
|
|
Array.Copy(buffer, 0, hdrB, 0, 26);
|
2025-10-21 11:43:05 +01:00
|
|
|
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
return _header is { magic: MAGIC, version: VERSION or VERSION2 };
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool Identify(Stream stream)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(stream == null || stream.Length < 26) return false;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var hdrB = new byte[26];
|
2022-03-06 13:29:38 +00:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(hdrB, 0, 26);
|
2025-10-21 11:43:05 +01:00
|
|
|
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
return _header is { magic: MAGIC, version: VERSION or VERSION2 };
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public bool Identify(string path)
|
|
|
|
|
{
|
2024-05-01 04:05:22 +01:00
|
|
|
if(!File.Exists(path)) return false;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
var fstream = new FileStream(path, FileMode.Open, FileAccess.Read);
|
2020-11-07 01:13:48 +00:00
|
|
|
|
2024-05-01 04:05:22 +01:00
|
|
|
if(fstream.Length < 26) return false;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var hdrB = new byte[26];
|
2022-11-14 09:43:16 +00:00
|
|
|
fstream.EnsureRead(hdrB, 0, 26);
|
2025-10-21 11:43:05 +01:00
|
|
|
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fstream.Close();
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
return _header is { magic: MAGIC, version: VERSION or VERSION2 };
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber Open(byte[] buffer)
|
|
|
|
|
{
|
|
|
|
|
var ms = new MemoryStream(buffer);
|
|
|
|
|
ms.Seek(0, SeekOrigin.Begin);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var hdrB = new byte[26];
|
2022-11-14 09:43:16 +00:00
|
|
|
ms.EnsureRead(hdrB, 0, 26);
|
2025-10-21 11:43:05 +01:00
|
|
|
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var entries = new Entry[_header.entries];
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
for(var i = 0; i < _header.entries; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2023-10-03 23:23:41 +01:00
|
|
|
var entry = new byte[12];
|
2022-11-14 09:43:16 +00:00
|
|
|
ms.EnsureRead(entry, 0, 12);
|
2025-10-21 11:43:05 +01:00
|
|
|
entries[i] = Marshal.ByteArrayToStructureBigEndian<Entry>(entry);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
CreationTime = DateTime.UtcNow;
|
|
|
|
|
LastWriteTime = CreationTime;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(Entry entry in entries)
|
2023-10-03 23:23:41 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
switch((AppleSingleEntryID)entry.id)
|
2016-09-05 21:22:04 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case AppleSingleEntryID.DataFork:
|
|
|
|
|
_dataFork = entry;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.FileDates:
|
|
|
|
|
ms.Seek(entry.offset, SeekOrigin.Begin);
|
2023-10-03 23:23:41 +01:00
|
|
|
var datesB = new byte[16];
|
2022-11-14 09:43:16 +00:00
|
|
|
ms.EnsureRead(datesB, 0, 16);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2025-10-21 11:43:05 +01:00
|
|
|
FileDates dates = Marshal.ByteArrayToStructureBigEndian<FileDates>(datesB);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.UnixUnsignedToDateTime(dates.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.UnixUnsignedToDateTime(dates.modificationDate);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.FileInfo:
|
|
|
|
|
ms.Seek(entry.offset, SeekOrigin.Begin);
|
2023-10-03 23:23:41 +01:00
|
|
|
var finfo = new byte[entry.length];
|
2022-11-14 09:43:16 +00:00
|
|
|
ms.EnsureRead(finfo, 0, finfo.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(_macintoshHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
MacFileInfo macinfo = Marshal.ByteArrayToStructureBigEndian<MacFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(macinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_proDosHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-11-24 20:12:10 +00:00
|
|
|
ProDOSFileInfo prodosinfo = Marshal.ByteArrayToStructureBigEndian<ProDOSFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_unixHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
UnixFileInfo unixinfo = Marshal.ByteArrayToStructureBigEndian<UnixFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_dosHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
DOSFileInfo dosinfo = Marshal.ByteArrayToStructureBigEndian<DOSFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
LastWriteTime = DateHandlers.DosToDateTime(dosinfo.modificationDate, dosinfo.modificationTime);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.ResourceFork:
|
|
|
|
|
_rsrcFork = entry;
|
|
|
|
|
|
|
|
|
|
break;
|
2016-09-05 21:22:04 +01:00
|
|
|
}
|
2023-10-03 23:23:41 +01:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
ms.Close();
|
|
|
|
|
_isBytes = true;
|
|
|
|
|
_bytes = buffer;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber Open(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var hdrB = new byte[26];
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(hdrB, 0, 26);
|
2025-10-21 11:43:05 +01:00
|
|
|
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var entries = new Entry[_header.entries];
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
for(var i = 0; i < _header.entries; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2023-10-03 23:23:41 +01:00
|
|
|
var entry = new byte[12];
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(entry, 0, 12);
|
2025-10-21 11:43:05 +01:00
|
|
|
entries[i] = Marshal.ByteArrayToStructureBigEndian<Entry>(entry);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
CreationTime = DateTime.UtcNow;
|
|
|
|
|
LastWriteTime = CreationTime;
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
foreach(Entry entry in entries)
|
2023-10-03 23:23:41 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
switch((AppleSingleEntryID)entry.id)
|
2016-09-05 21:22:04 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case AppleSingleEntryID.DataFork:
|
|
|
|
|
_dataFork = entry;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.FileDates:
|
|
|
|
|
stream.Seek(entry.offset, SeekOrigin.Begin);
|
2023-10-03 23:23:41 +01:00
|
|
|
var datesB = new byte[16];
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(datesB, 0, 16);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2025-10-21 11:43:05 +01:00
|
|
|
FileDates dates = Marshal.ByteArrayToStructureBigEndian<FileDates>(datesB);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(dates.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(dates.modificationDate);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.FileInfo:
|
|
|
|
|
stream.Seek(entry.offset, SeekOrigin.Begin);
|
2023-10-03 23:23:41 +01:00
|
|
|
var finfo = new byte[entry.length];
|
2022-11-14 09:43:16 +00:00
|
|
|
stream.EnsureRead(finfo, 0, finfo.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(_macintoshHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
MacFileInfo macinfo = Marshal.ByteArrayToStructureBigEndian<MacFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(macinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_proDosHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-11-24 20:12:10 +00:00
|
|
|
ProDOSFileInfo prodosinfo = Marshal.ByteArrayToStructureBigEndian<ProDOSFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_unixHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
UnixFileInfo unixinfo = Marshal.ByteArrayToStructureBigEndian<UnixFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_dosHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
DOSFileInfo dosinfo = Marshal.ByteArrayToStructureBigEndian<DOSFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
LastWriteTime = DateHandlers.DosToDateTime(dosinfo.modificationDate, dosinfo.modificationTime);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.ResourceFork:
|
|
|
|
|
_rsrcFork = entry;
|
|
|
|
|
|
|
|
|
|
break;
|
2016-09-05 21:22:04 +01:00
|
|
|
}
|
2023-10-03 23:23:41 +01:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
_isStream = true;
|
|
|
|
|
_stream = stream;
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public ErrorNumber Open(string path)
|
|
|
|
|
{
|
|
|
|
|
var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
|
|
|
|
|
fs.Seek(0, SeekOrigin.Begin);
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var hdrB = new byte[26];
|
2022-11-14 09:43:16 +00:00
|
|
|
fs.EnsureRead(hdrB, 0, 26);
|
2025-10-21 11:43:05 +01:00
|
|
|
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
|
2020-02-29 18:03:35 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
var entries = new Entry[_header.entries];
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
for(var i = 0; i < _header.entries; i++)
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2023-10-03 23:23:41 +01:00
|
|
|
var entry = new byte[12];
|
2022-11-14 09:43:16 +00:00
|
|
|
fs.EnsureRead(entry, 0, 12);
|
2025-10-21 11:43:05 +01:00
|
|
|
entries[i] = Marshal.ByteArrayToStructureBigEndian<Entry>(entry);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CreationTime = DateTime.UtcNow;
|
|
|
|
|
LastWriteTime = CreationTime;
|
|
|
|
|
|
|
|
|
|
foreach(Entry entry in entries)
|
2023-10-03 23:23:41 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
switch((AppleSingleEntryID)entry.id)
|
2016-09-05 21:22:04 +01:00
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
case AppleSingleEntryID.DataFork:
|
|
|
|
|
_dataFork = entry;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.FileDates:
|
|
|
|
|
fs.Seek(entry.offset, SeekOrigin.Begin);
|
2023-10-03 23:23:41 +01:00
|
|
|
var datesB = new byte[16];
|
2022-11-14 09:43:16 +00:00
|
|
|
fs.EnsureRead(datesB, 0, 16);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2025-10-21 11:43:05 +01:00
|
|
|
FileDates dates = Marshal.ByteArrayToStructureBigEndian<FileDates>(datesB);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(dates.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(dates.modificationDate);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.FileInfo:
|
|
|
|
|
fs.Seek(entry.offset, SeekOrigin.Begin);
|
2023-10-03 23:23:41 +01:00
|
|
|
var finfo = new byte[entry.length];
|
2022-11-14 09:43:16 +00:00
|
|
|
fs.EnsureRead(finfo, 0, finfo.Length);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
if(_macintoshHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
MacFileInfo macinfo = Marshal.ByteArrayToStructureBigEndian<MacFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(macinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_proDosHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-11-24 20:12:10 +00:00
|
|
|
ProDOSFileInfo prodosinfo = Marshal.ByteArrayToStructureBigEndian<ProDOSFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_unixHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
UnixFileInfo unixinfo = Marshal.ByteArrayToStructureBigEndian<UnixFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
CreationTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.creationDate);
|
|
|
|
|
LastWriteTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.modificationDate);
|
|
|
|
|
}
|
|
|
|
|
else if(_dosHome.SequenceEqual(_header.homeFilesystem))
|
|
|
|
|
{
|
2025-10-21 11:43:05 +01:00
|
|
|
DOSFileInfo dosinfo = Marshal.ByteArrayToStructureBigEndian<DOSFileInfo>(finfo);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
2022-03-07 07:36:44 +00:00
|
|
|
LastWriteTime = DateHandlers.DosToDateTime(dosinfo.modificationDate, dosinfo.modificationTime);
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case AppleSingleEntryID.ResourceFork:
|
|
|
|
|
_rsrcFork = entry;
|
|
|
|
|
|
|
|
|
|
break;
|
2016-09-05 21:22:04 +01:00
|
|
|
}
|
2023-10-03 23:23:41 +01:00
|
|
|
}
|
2016-09-05 21:22:04 +01:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
fs.Close();
|
|
|
|
|
_isPath = true;
|
|
|
|
|
BasePath = path;
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
return ErrorNumber.NoError;
|
|
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Nested type: AppleSingleEntryID
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
enum AppleSingleEntryID : uint
|
|
|
|
|
{
|
2023-10-03 23:23:41 +01:00
|
|
|
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
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Nested type: DOSFileInfo
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2025-10-21 04:00:00 +01:00
|
|
|
[SwapEndian]
|
2025-10-21 10:28:31 +01:00
|
|
|
partial struct DOSFileInfo
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-21 04:00:00 +01:00
|
|
|
public ushort modificationDate;
|
|
|
|
|
public ushort modificationTime;
|
|
|
|
|
public ushort attributes;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
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)]
|
2025-10-21 04:00:00 +01:00
|
|
|
[SwapEndian]
|
2025-10-21 10:28:31 +01:00
|
|
|
partial struct Entry
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-21 04:00:00 +01:00
|
|
|
public uint id;
|
|
|
|
|
public uint offset;
|
|
|
|
|
public uint length;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Nested type: FileDates
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2025-10-21 04:00:00 +01:00
|
|
|
[SwapEndian]
|
2025-10-21 10:28:31 +01:00
|
|
|
partial struct FileDates
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-21 04:00:00 +01:00
|
|
|
public uint creationDate;
|
|
|
|
|
public uint modificationDate;
|
|
|
|
|
public uint backupDate;
|
|
|
|
|
public uint accessDate;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Nested type: Header
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2025-10-21 04:00:00 +01:00
|
|
|
[SwapEndian]
|
2025-10-21 10:28:31 +01:00
|
|
|
partial struct Header
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-21 04:00:00 +01:00
|
|
|
public uint magic;
|
|
|
|
|
public uint version;
|
2023-10-03 23:23:41 +01:00
|
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
|
2025-10-21 04:00:00 +01:00
|
|
|
public byte[] homeFilesystem;
|
|
|
|
|
public ushort entries;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Nested type: MacFileInfo
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2025-10-21 04:00:00 +01:00
|
|
|
[SwapEndian]
|
2025-10-21 10:28:31 +01:00
|
|
|
partial struct MacFileInfo
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-21 04:00:00 +01:00
|
|
|
public uint creationDate;
|
|
|
|
|
public uint modificationDate;
|
|
|
|
|
public uint backupDate;
|
|
|
|
|
public uint accessDate;
|
2022-03-06 13:29:38 +00:00
|
|
|
}
|
2017-12-24 02:43:49 +00:00
|
|
|
|
2023-10-03 23:23:41 +01:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Nested type: ProDOSFileInfo
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2025-10-21 04:00:00 +01:00
|
|
|
[SwapEndian]
|
2025-10-21 10:28:31 +01:00
|
|
|
partial struct ProDOSFileInfo
|
2022-03-06 13:29:38 +00:00
|
|
|
{
|
2025-10-21 04:00:00 +01:00
|
|
|
public uint creationDate;
|
|
|
|
|
public uint modificationDate;
|
|
|
|
|
public uint backupDate;
|
|
|
|
|
public ushort access;
|
|
|
|
|
public ushort fileType;
|
|
|
|
|
public uint auxType;
|
2016-09-05 21:22:04 +01:00
|
|
|
}
|
2023-10-03 23:23:41 +01:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Nested type: UnixFileInfo
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
2025-10-21 04:00:00 +01:00
|
|
|
[SwapEndian]
|
2025-10-21 10:28:31 +01:00
|
|
|
partial struct UnixFileInfo
|
2023-10-03 23:23:41 +01:00
|
|
|
{
|
2025-10-21 04:00:00 +01:00
|
|
|
public uint creationDate;
|
|
|
|
|
public uint accessDate;
|
|
|
|
|
public uint modificationDate;
|
2023-10-03 23:23:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2017-12-19 20:33:03 +00:00
|
|
|
}
|