// /*************************************************************************** // Aaru Data Preservation Suite // ---------------------------------------------------------------------------- // // Filename : AppleSingle.cs // Author(s) : Natalia Portillo // // Component : Filters. // // --[ 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-2025 Natalia Portillo // ****************************************************************************/ using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; using Aaru.CommonTypes.Attributes; using Aaru.CommonTypes.Enums; using Aaru.CommonTypes.Interfaces; using Aaru.Helpers; using Aaru.Helpers.IO; using Marshal = Aaru.Helpers.Marshal; namespace Aaru.Filters; /// /// Decodes AppleSingle files [SuppressMessage("ReSharper", "UnusedMember.Local")] public sealed partial class AppleSingle : IFilter { 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; #region IFilter Members /// public string Name => Localization.AppleSingle_Name; /// public Guid Id => new("A69B20E8-F4D3-42BB-BD2B-4A7263394A05"); /// public string Author => Authors.NataliaPortillo; /// public void Close() { _bytes = null; _stream?.Close(); _isBytes = false; _isStream = false; _isPath = false; } /// public string BasePath { get; private set; } /// public DateTime CreationTime { get; private set; } /// public long DataForkLength => _dataFork.length; /// public 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 string Filename => System.IO.Path.GetFileName(BasePath); /// public DateTime LastWriteTime { get; private set; } /// public long Length => _dataFork.length + _rsrcFork.length; /// public string ParentFolder => System.IO.Path.GetDirectoryName(BasePath); /// public string Path => BasePath; /// public long ResourceForkLength => _rsrcFork.length; /// public 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 bool HasResourceFork => _rsrcFork.length > 0; /// public bool Identify(byte[] buffer) { if(buffer == null || buffer.Length < 26) return false; var hdrB = new byte[26]; Array.Copy(buffer, 0, hdrB, 0, 26); _header = Marshal.ByteArrayToStructureBigEndian
(hdrB); return _header is { magic: MAGIC, version: VERSION or VERSION2 }; } /// public bool Identify(Stream stream) { if(stream == null || stream.Length < 26) return false; var hdrB = new byte[26]; stream.Seek(0, SeekOrigin.Begin); stream.EnsureRead(hdrB, 0, 26); _header = Marshal.ByteArrayToStructureBigEndian
(hdrB); return _header is { magic: MAGIC, version: VERSION or VERSION2 }; } /// public bool Identify(string path) { if(!File.Exists(path)) return false; var fstream = new FileStream(path, FileMode.Open, FileAccess.Read); if(fstream.Length < 26) return false; var hdrB = new byte[26]; fstream.EnsureRead(hdrB, 0, 26); _header = Marshal.ByteArrayToStructureBigEndian
(hdrB); fstream.Close(); return _header is { magic: MAGIC, version: VERSION or VERSION2 }; } /// public ErrorNumber Open(byte[] buffer) { var ms = new MemoryStream(buffer); ms.Seek(0, SeekOrigin.Begin); var hdrB = new byte[26]; ms.EnsureRead(hdrB, 0, 26); _header = Marshal.ByteArrayToStructureBigEndian
(hdrB); var entries = new Entry[_header.entries]; for(var i = 0; i < _header.entries; i++) { var entry = new byte[12]; ms.EnsureRead(entry, 0, 12); entries[i] = Marshal.ByteArrayToStructureBigEndian(entry); } CreationTime = DateTime.UtcNow; LastWriteTime = CreationTime; foreach(Entry entry in entries) { switch((AppleSingleEntryID)entry.id) { case AppleSingleEntryID.DataFork: _dataFork = entry; break; case AppleSingleEntryID.FileDates: ms.Seek(entry.offset, SeekOrigin.Begin); var datesB = new byte[16]; ms.EnsureRead(datesB, 0, 16); FileDates dates = Marshal.ByteArrayToStructureBigEndian(datesB); CreationTime = DateHandlers.UnixUnsignedToDateTime(dates.creationDate); LastWriteTime = DateHandlers.UnixUnsignedToDateTime(dates.modificationDate); break; case AppleSingleEntryID.FileInfo: ms.Seek(entry.offset, SeekOrigin.Begin); var finfo = new byte[entry.length]; ms.EnsureRead(finfo, 0, finfo.Length); if(_macintoshHome.SequenceEqual(_header.homeFilesystem)) { MacFileInfo macinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.MacToDateTime(macinfo.creationDate); LastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate); } else if(_proDosHome.SequenceEqual(_header.homeFilesystem)) { ProDOSFileInfo prodosinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate); LastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate); } else if(_unixHome.SequenceEqual(_header.homeFilesystem)) { UnixFileInfo unixinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.creationDate); LastWriteTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.modificationDate); } else if(_dosHome.SequenceEqual(_header.homeFilesystem)) { DOSFileInfo dosinfo = Marshal.ByteArrayToStructureBigEndian(finfo); LastWriteTime = DateHandlers.DosToDateTime(dosinfo.modificationDate, dosinfo.modificationTime); } break; case AppleSingleEntryID.ResourceFork: _rsrcFork = entry; break; } } ms.Close(); _isBytes = true; _bytes = buffer; return ErrorNumber.NoError; } /// public ErrorNumber Open(Stream stream) { stream.Seek(0, SeekOrigin.Begin); var hdrB = new byte[26]; stream.EnsureRead(hdrB, 0, 26); _header = Marshal.ByteArrayToStructureBigEndian
(hdrB); var entries = new Entry[_header.entries]; for(var i = 0; i < _header.entries; i++) { var entry = new byte[12]; stream.EnsureRead(entry, 0, 12); entries[i] = Marshal.ByteArrayToStructureBigEndian(entry); } CreationTime = DateTime.UtcNow; LastWriteTime = CreationTime; foreach(Entry entry in entries) { switch((AppleSingleEntryID)entry.id) { case AppleSingleEntryID.DataFork: _dataFork = entry; break; case AppleSingleEntryID.FileDates: stream.Seek(entry.offset, SeekOrigin.Begin); var datesB = new byte[16]; stream.EnsureRead(datesB, 0, 16); FileDates dates = Marshal.ByteArrayToStructureBigEndian(datesB); CreationTime = DateHandlers.MacToDateTime(dates.creationDate); LastWriteTime = DateHandlers.MacToDateTime(dates.modificationDate); break; case AppleSingleEntryID.FileInfo: stream.Seek(entry.offset, SeekOrigin.Begin); var finfo = new byte[entry.length]; stream.EnsureRead(finfo, 0, finfo.Length); if(_macintoshHome.SequenceEqual(_header.homeFilesystem)) { MacFileInfo macinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.MacToDateTime(macinfo.creationDate); LastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate); } else if(_proDosHome.SequenceEqual(_header.homeFilesystem)) { ProDOSFileInfo prodosinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate); LastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate); } else if(_unixHome.SequenceEqual(_header.homeFilesystem)) { UnixFileInfo unixinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.creationDate); LastWriteTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.modificationDate); } else if(_dosHome.SequenceEqual(_header.homeFilesystem)) { DOSFileInfo dosinfo = Marshal.ByteArrayToStructureBigEndian(finfo); LastWriteTime = DateHandlers.DosToDateTime(dosinfo.modificationDate, dosinfo.modificationTime); } break; case AppleSingleEntryID.ResourceFork: _rsrcFork = entry; break; } } stream.Seek(0, SeekOrigin.Begin); _isStream = true; _stream = stream; return ErrorNumber.NoError; } /// public ErrorNumber Open(string path) { var fs = new FileStream(path, FileMode.Open, FileAccess.Read); fs.Seek(0, SeekOrigin.Begin); var hdrB = new byte[26]; fs.EnsureRead(hdrB, 0, 26); _header = Marshal.ByteArrayToStructureBigEndian
(hdrB); var entries = new Entry[_header.entries]; for(var i = 0; i < _header.entries; i++) { var entry = new byte[12]; fs.EnsureRead(entry, 0, 12); entries[i] = Marshal.ByteArrayToStructureBigEndian(entry); } CreationTime = DateTime.UtcNow; LastWriteTime = CreationTime; foreach(Entry entry in entries) { switch((AppleSingleEntryID)entry.id) { case AppleSingleEntryID.DataFork: _dataFork = entry; break; case AppleSingleEntryID.FileDates: fs.Seek(entry.offset, SeekOrigin.Begin); var datesB = new byte[16]; fs.EnsureRead(datesB, 0, 16); FileDates dates = Marshal.ByteArrayToStructureBigEndian(datesB); CreationTime = DateHandlers.MacToDateTime(dates.creationDate); LastWriteTime = DateHandlers.MacToDateTime(dates.modificationDate); break; case AppleSingleEntryID.FileInfo: fs.Seek(entry.offset, SeekOrigin.Begin); var finfo = new byte[entry.length]; fs.EnsureRead(finfo, 0, finfo.Length); if(_macintoshHome.SequenceEqual(_header.homeFilesystem)) { MacFileInfo macinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.MacToDateTime(macinfo.creationDate); LastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate); } else if(_proDosHome.SequenceEqual(_header.homeFilesystem)) { ProDOSFileInfo prodosinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate); LastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate); } else if(_unixHome.SequenceEqual(_header.homeFilesystem)) { UnixFileInfo unixinfo = Marshal.ByteArrayToStructureBigEndian(finfo); CreationTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.creationDate); LastWriteTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.modificationDate); } else if(_dosHome.SequenceEqual(_header.homeFilesystem)) { DOSFileInfo dosinfo = Marshal.ByteArrayToStructureBigEndian(finfo); LastWriteTime = DateHandlers.DosToDateTime(dosinfo.modificationDate, dosinfo.modificationTime); } break; case AppleSingleEntryID.ResourceFork: _rsrcFork = entry; break; } } fs.Close(); _isPath = true; BasePath = path; return ErrorNumber.NoError; } #endregion #region Nested type: AppleSingleEntryID 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 } #endregion #region Nested type: DOSFileInfo [StructLayout(LayoutKind.Sequential, Pack = 1)] [SwapEndian] partial struct DOSFileInfo { public ushort modificationDate; public ushort modificationTime; public ushort attributes; } #endregion #region Nested type: Entry [StructLayout(LayoutKind.Sequential, Pack = 1)] [SwapEndian] partial struct Entry { public uint id; public uint offset; public uint length; } #endregion #region Nested type: FileDates [StructLayout(LayoutKind.Sequential, Pack = 1)] [SwapEndian] partial struct FileDates { public uint creationDate; public uint modificationDate; public uint backupDate; public uint accessDate; } #endregion #region Nested type: Header [StructLayout(LayoutKind.Sequential, Pack = 1)] [SwapEndian] partial struct Header { public uint magic; public uint version; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] homeFilesystem; public ushort entries; } #endregion #region Nested type: MacFileInfo [StructLayout(LayoutKind.Sequential, Pack = 1)] [SwapEndian] partial struct MacFileInfo { public uint creationDate; public uint modificationDate; public uint backupDate; public uint accessDate; } #endregion #region Nested type: ProDOSFileInfo [StructLayout(LayoutKind.Sequential, Pack = 1)] [SwapEndian] partial struct ProDOSFileInfo { public uint creationDate; public uint modificationDate; public uint backupDate; public ushort access; public ushort fileType; public uint auxType; } #endregion #region Nested type: UnixFileInfo [StructLayout(LayoutKind.Sequential, Pack = 1)] [SwapEndian] partial struct UnixFileInfo { public uint creationDate; public uint accessDate; public uint modificationDate; } #endregion }