Files
Aaru/Aaru.Filters/AppleDouble.cs

683 lines
26 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
// ----------------------------------------------------------------------------
//
// Filename : AppleDouble.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Filters.
//
// --[ 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 <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
2017-12-19 19:33:46 +00:00
using System;
2020-07-20 07:47:12 +01:00
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
2017-12-19 19:33:46 +00:00
using System.Runtime.InteropServices;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2021-09-15 13:03:42 +01:00
using Aaru.CommonTypes.Structs;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2020-02-27 00:33:26 +00:00
namespace Aaru.Filters
{
2021-08-17 14:40:37 +01:00
/// <inheritdoc />
2020-02-29 18:03:35 +00:00
/// <summary>Decodes AppleDouble files</summary>
2020-07-20 07:47:12 +01:00
[SuppressMessage("ReSharper", "UnusedMember.Local")]
2020-07-22 13:20:25 +01:00
public sealed class AppleDouble : IFilter
{
2020-07-20 21:11:32 +01:00
const uint MAGIC = 0x00051607;
const uint VERSION = 0x00010000;
const uint VERSION2 = 0x00020000;
readonly byte[] _dosHome =
2018-08-29 22:15:43 +01:00
{
0x4D, 0x53, 0x2D, 0x44, 0x4F, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
2020-07-20 21:11:32 +01:00
readonly byte[] _macintoshHome =
2018-08-29 22:15:43 +01:00
{
0x4D, 0x61, 0x63, 0x69, 0x6E, 0x74, 0x6F, 0x73, 0x68, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
2020-07-20 21:11:32 +01:00
readonly byte[] _osxHome =
2018-08-29 22:15:43 +01:00
{
0x4D, 0x61, 0x63, 0x20, 0x4F, 0x53, 0x20, 0x58, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
2020-07-20 21:11:32 +01:00
readonly byte[] _proDosHome =
2018-08-29 22:15:43 +01:00
{
0x50, 0x72, 0x6F, 0x44, 0x4F, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
2020-07-20 21:11:32 +01:00
readonly byte[] _unixHome =
2018-08-29 22:15:43 +01:00
{
0x55, 0x6E, 0x69, 0x78, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
2020-07-20 21:11:32 +01:00
readonly byte[] _vmsHome =
2018-08-29 22:15:43 +01:00
{
0x56, 0x41, 0x58, 0x20, 0x56, 0x4D, 0x53, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20
};
2021-09-15 13:03:42 +01:00
Entry _dataFork;
Header _header;
string _headerPath;
Entry _rsrcFork;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-08-17 21:23:10 +01:00
public string Name => "AppleDouble";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public Guid Id => new("1B2165EE-C9DF-4B21-BBBB-9E5892B2DF4D");
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public string Author => "Natalia Portillo";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public void Close() {}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public string BasePath { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public DateTime CreationTime { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public long DataForkLength => _dataFork.length;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public Stream GetDataForkStream() => new FileStream(BasePath, FileMode.Open, FileAccess.Read);
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public string Filename => System.IO.Path.GetFileName(BasePath);
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public DateTime LastWriteTime { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public long Length => _dataFork.length + _rsrcFork.length;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public string ParentFolder => System.IO.Path.GetDirectoryName(BasePath);
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public string Path => BasePath;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public long ResourceForkLength => _rsrcFork.length;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public Stream GetResourceForkStream()
{
2020-07-20 21:11:32 +01:00
if(_rsrcFork.length == 0)
2020-02-29 18:03:35 +00:00
return null;
2020-07-20 21:11:32 +01:00
return new OffsetStream(_headerPath, FileMode.Open, FileAccess.Read, _rsrcFork.offset,
2021-08-17 14:27:19 +01:00
_rsrcFork.offset + _rsrcFork.length - 1);
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public bool HasResourceFork => _rsrcFork.length > 0;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public bool Identify(byte[] buffer) => false;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public bool Identify(Stream stream) => false;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(string path)
{
2021-09-15 13:03:42 +01:00
string filename = System.IO.Path.GetFileName(path);
string filenameNoExt = System.IO.Path.GetFileNameWithoutExtension(path);
string parentFolder = System.IO.Path.GetDirectoryName(path);
2018-06-20 22:22:21 +01:00
2020-07-22 13:20:25 +01:00
parentFolder ??= "";
2020-02-29 18:03:35 +00:00
if(filename is null ||
filenameNoExt is null)
return false;
// Prepend data fork name with "R."
2021-09-15 13:03:42 +01:00
string proDosAppleDouble = System.IO.Path.Combine(parentFolder, "R." + filename);
2020-02-29 18:03:35 +00:00
// Prepend data fork name with '%'
2021-09-15 13:03:42 +01:00
string unixAppleDouble = System.IO.Path.Combine(parentFolder, "%" + filename);
2020-02-29 18:03:35 +00:00
// Change file extension to ADF
2021-09-15 13:03:42 +01:00
string dosAppleDouble = System.IO.Path.Combine(parentFolder, filenameNoExt + ".ADF");
2020-02-29 18:03:35 +00:00
// Change file extension to adf
2021-09-15 13:03:42 +01:00
string dosAppleDoubleLower = System.IO.Path.Combine(parentFolder, filenameNoExt + ".adf");
2020-02-29 18:03:35 +00:00
// Store AppleDouble header file in ".AppleDouble" folder with same name
2021-09-15 13:03:42 +01:00
string netatalkAppleDouble = System.IO.Path.Combine(parentFolder, ".AppleDouble", filename);
2020-02-29 18:03:35 +00:00
// Store AppleDouble header file in "resource.frk" folder with same name
2021-09-15 13:03:42 +01:00
string daveAppleDouble = System.IO.Path.Combine(parentFolder, "resource.frk", filename);
2020-02-29 18:03:35 +00:00
// Prepend data fork name with "._"
2021-09-15 13:03:42 +01:00
string osxAppleDouble = System.IO.Path.Combine(parentFolder, "._" + filename);
2020-02-29 18:03:35 +00:00
// Adds ".rsrc" extension
2021-09-15 13:03:42 +01:00
string unArAppleDouble = System.IO.Path.Combine(parentFolder, filename + ".rsrc");
// Check AppleDouble created by A/UX in ProDOS filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(proDosAppleDouble))
{
2020-07-20 21:11:32 +01:00
var prodosStream = new FileStream(proDosAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(prodosStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] prodosB = new byte[26];
prodosStream.Read(prodosB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(prodosB);
prodosStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
2020-02-29 18:03:35 +00:00
return true;
}
}
// Check AppleDouble created by A/UX in UFS filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(unixAppleDouble))
{
2020-07-20 21:11:32 +01:00
var unixStream = new FileStream(unixAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(unixStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] unixB = new byte[26];
unixStream.Read(unixB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(unixB);
unixStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
2020-02-29 18:03:35 +00:00
return true;
}
}
// Check AppleDouble created by A/UX in FAT filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(dosAppleDouble))
{
2020-07-20 21:11:32 +01:00
var dosStream = new FileStream(dosAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(dosStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] dosB = new byte[26];
dosStream.Read(dosB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(dosB);
dosStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
2020-02-29 18:03:35 +00:00
return true;
}
}
// Check AppleDouble created by A/UX in case preserving FAT filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(dosAppleDoubleLower))
{
2020-07-20 21:11:32 +01:00
var doslStream = new FileStream(dosAppleDoubleLower, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(doslStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] doslB = new byte[26];
doslStream.Read(doslB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(doslB);
doslStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
2020-02-29 18:03:35 +00:00
return true;
}
}
// Check AppleDouble created by Netatalk
2020-07-20 21:11:32 +01:00
if(File.Exists(netatalkAppleDouble))
{
2020-07-20 21:11:32 +01:00
var netatalkStream = new FileStream(netatalkAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(netatalkStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] netatalkB = new byte[26];
netatalkStream.Read(netatalkB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(netatalkB);
netatalkStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
2020-02-29 18:03:35 +00:00
return true;
}
}
// Check AppleDouble created by DAVE
2020-07-20 21:11:32 +01:00
if(File.Exists(daveAppleDouble))
{
2020-07-20 21:11:32 +01:00
var daveStream = new FileStream(daveAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(daveStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] daveB = new byte[26];
daveStream.Read(daveB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(daveB);
daveStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
2020-02-29 18:03:35 +00:00
return true;
}
}
// Check AppleDouble created by Mac OS X
2020-07-20 21:11:32 +01:00
if(File.Exists(osxAppleDouble))
{
2020-07-20 21:11:32 +01:00
var osxStream = new FileStream(osxAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(osxStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] osxB = new byte[26];
osxStream.Read(osxB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(osxB);
osxStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
2020-02-29 18:03:35 +00:00
return true;
}
}
// Check AppleDouble created by UnAr (from The Unarchiver)
2020-07-20 21:11:32 +01:00
if(!File.Exists(unArAppleDouble))
2020-02-29 18:03:35 +00:00
return false;
2020-07-20 21:11:32 +01:00
var unarStream = new FileStream(unArAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(unarStream.Length <= 26)
return false;
2020-07-20 21:11:32 +01:00
byte[] unarB = new byte[26];
unarStream.Read(unarB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(unarB);
unarStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return _header.magic == MAGIC && (_header.version == VERSION || _header.version == VERSION2);
}
2020-02-29 18:03:35 +00:00
// Now way to have two files in a single byte array
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public Errno Open(byte[] buffer) => Errno.NotSupported;
2020-02-29 18:03:35 +00:00
// Now way to have two files in a single stream
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public Errno Open(Stream stream) => Errno.NotSupported;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2021-09-15 13:03:42 +01:00
public Errno Open(string path)
{
2021-09-15 13:03:42 +01:00
string filename = System.IO.Path.GetFileName(path);
string filenameNoExt = System.IO.Path.GetFileNameWithoutExtension(path);
string parentFolder = System.IO.Path.GetDirectoryName(path);
2018-06-20 22:22:21 +01:00
2020-07-22 13:20:25 +01:00
parentFolder ??= "";
2020-02-29 18:03:35 +00:00
if(filename is null ||
filenameNoExt is null)
2021-09-15 13:03:42 +01:00
return Errno.InvalidArgument;
// Prepend data fork name with "R."
2021-09-15 13:03:42 +01:00
string proDosAppleDouble = System.IO.Path.Combine(parentFolder, "R." + filename);
2020-02-29 18:03:35 +00:00
// Prepend data fork name with '%'
2021-09-15 13:03:42 +01:00
string unixAppleDouble = System.IO.Path.Combine(parentFolder, "%" + filename);
2020-02-29 18:03:35 +00:00
// Change file extension to ADF
2021-09-15 13:03:42 +01:00
string dosAppleDouble = System.IO.Path.Combine(parentFolder, filenameNoExt + ".ADF");
2020-02-29 18:03:35 +00:00
// Change file extension to adf
2021-09-15 13:03:42 +01:00
string dosAppleDoubleLower = System.IO.Path.Combine(parentFolder, filenameNoExt + ".adf");
2020-02-29 18:03:35 +00:00
// Store AppleDouble header file in ".AppleDouble" folder with same name
2021-09-15 13:03:42 +01:00
string netatalkAppleDouble = System.IO.Path.Combine(parentFolder, ".AppleDouble", filename);
2020-02-29 18:03:35 +00:00
// Store AppleDouble header file in "resource.frk" folder with same name
2021-09-15 13:03:42 +01:00
string daveAppleDouble = System.IO.Path.Combine(parentFolder, "resource.frk", filename);
2020-02-29 18:03:35 +00:00
// Prepend data fork name with "._"
2021-09-15 13:03:42 +01:00
string osxAppleDouble = System.IO.Path.Combine(parentFolder, "._" + filename);
2020-02-29 18:03:35 +00:00
// Adds ".rsrc" extension
2021-09-15 13:03:42 +01:00
string unArAppleDouble = System.IO.Path.Combine(parentFolder, filename + ".rsrc");
// Check AppleDouble created by A/UX in ProDOS filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(proDosAppleDouble))
{
2020-07-20 21:11:32 +01:00
var prodosStream = new FileStream(proDosAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(prodosStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] prodosB = new byte[26];
prodosStream.Read(prodosB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(prodosB);
prodosStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = proDosAppleDouble;
}
}
// Check AppleDouble created by A/UX in UFS filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(unixAppleDouble))
{
2020-07-20 21:11:32 +01:00
var unixStream = new FileStream(unixAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(unixStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] unixB = new byte[26];
unixStream.Read(unixB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(unixB);
unixStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = unixAppleDouble;
}
}
// Check AppleDouble created by A/UX in FAT filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(dosAppleDouble))
{
2020-07-20 21:11:32 +01:00
var dosStream = new FileStream(dosAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(dosStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] dosB = new byte[26];
dosStream.Read(dosB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(dosB);
dosStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = dosAppleDouble;
}
}
// Check AppleDouble created by A/UX in case preserving FAT filesystem
2020-07-20 21:11:32 +01:00
if(File.Exists(dosAppleDoubleLower))
{
2020-07-20 21:11:32 +01:00
var doslStream = new FileStream(dosAppleDoubleLower, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(doslStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] doslB = new byte[26];
doslStream.Read(doslB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(doslB);
doslStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = dosAppleDoubleLower;
}
}
// Check AppleDouble created by Netatalk
2020-07-20 21:11:32 +01:00
if(File.Exists(netatalkAppleDouble))
{
2020-07-20 21:11:32 +01:00
var netatalkStream = new FileStream(netatalkAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(netatalkStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] netatalkB = new byte[26];
netatalkStream.Read(netatalkB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(netatalkB);
netatalkStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = netatalkAppleDouble;
}
}
// Check AppleDouble created by DAVE
2020-07-20 21:11:32 +01:00
if(File.Exists(daveAppleDouble))
{
2020-07-20 21:11:32 +01:00
var daveStream = new FileStream(daveAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(daveStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] daveB = new byte[26];
daveStream.Read(daveB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(daveB);
daveStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = daveAppleDouble;
}
}
// Check AppleDouble created by Mac OS X
2020-07-20 21:11:32 +01:00
if(File.Exists(osxAppleDouble))
{
2020-07-20 21:11:32 +01:00
var osxStream = new FileStream(osxAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(osxStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] osxB = new byte[26];
osxStream.Read(osxB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(osxB);
osxStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = osxAppleDouble;
}
}
// Check AppleDouble created by UnAr (from The Unarchiver)
2020-07-20 21:11:32 +01:00
if(File.Exists(unArAppleDouble))
{
2020-07-20 21:11:32 +01:00
var unarStream = new FileStream(unArAppleDouble, FileMode.Open, FileAccess.Read);
2020-02-29 18:03:35 +00:00
if(unarStream.Length > 26)
{
2020-07-20 21:11:32 +01:00
byte[] unarB = new byte[26];
unarStream.Read(unarB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(unarB);
unarStream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.magic == MAGIC &&
(_header.version == VERSION || _header.version == VERSION2))
_headerPath = unArAppleDouble;
}
}
2021-09-15 13:03:42 +01:00
// TODO: More appropriate error
if(_headerPath is null)
return Errno.NotSupported;
2020-07-20 21:11:32 +01:00
var fs = new FileStream(_headerPath, FileMode.Open, FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
2020-07-20 21:11:32 +01:00
byte[] hdrB = new byte[26];
fs.Read(hdrB, 0, 26);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
Entry[] entries = new Entry[_header.entries];
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
for(int i = 0; i < _header.entries; i++)
{
byte[] entry = new byte[12];
fs.Read(entry, 0, 12);
entries[i] = Marshal.ByteArrayToStructureBigEndian<Entry>(entry);
}
2021-09-15 13:03:42 +01:00
CreationTime = DateTime.UtcNow;
LastWriteTime = CreationTime;
2020-02-29 18:03:35 +00:00
foreach(Entry entry in entries)
switch((EntryId)entry.id)
{
case EntryId.DataFork:
// AppleDouble have datafork in separated file
break;
case EntryId.FileDates:
fs.Seek(entry.offset, SeekOrigin.Begin);
2020-07-20 21:11:32 +01:00
byte[] datesB = new byte[16];
fs.Read(datesB, 0, 16);
2020-02-29 18:03:35 +00:00
FileDates dates = Marshal.ByteArrayToStructureBigEndian<FileDates>(datesB);
2020-02-29 18:03:35 +00:00
2021-09-15 13:03:42 +01:00
CreationTime = DateHandlers.UnixUnsignedToDateTime(dates.creationDate);
LastWriteTime = DateHandlers.UnixUnsignedToDateTime(dates.modificationDate);
2020-02-29 18:03:35 +00:00
break;
case EntryId.FileInfo:
fs.Seek(entry.offset, SeekOrigin.Begin);
byte[] finfo = new byte[entry.length];
fs.Read(finfo, 0, finfo.Length);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_macintoshHome.SequenceEqual(_header.homeFilesystem))
{
MacFileInfo macinfo = Marshal.ByteArrayToStructureBigEndian<MacFileInfo>(finfo);
2020-02-29 18:03:35 +00:00
2021-09-15 13:03:42 +01:00
CreationTime = DateHandlers.MacToDateTime(macinfo.creationDate);
LastWriteTime = DateHandlers.MacToDateTime(macinfo.modificationDate);
}
2020-07-20 21:11:32 +01:00
else if(_proDosHome.SequenceEqual(_header.homeFilesystem))
{
ProDOSFileInfo prodosinfo = Marshal.ByteArrayToStructureBigEndian<ProDOSFileInfo>(finfo);
2020-02-29 18:03:35 +00:00
2021-09-15 13:03:42 +01:00
CreationTime = DateHandlers.MacToDateTime(prodosinfo.creationDate);
LastWriteTime = DateHandlers.MacToDateTime(prodosinfo.modificationDate);
}
2020-07-20 21:11:32 +01:00
else if(_unixHome.SequenceEqual(_header.homeFilesystem))
{
UnixFileInfo unixinfo = Marshal.ByteArrayToStructureBigEndian<UnixFileInfo>(finfo);
2020-02-29 18:03:35 +00:00
2021-09-15 13:03:42 +01:00
CreationTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.creationDate);
LastWriteTime = DateHandlers.UnixUnsignedToDateTime(unixinfo.modificationDate);
}
2020-07-20 21:11:32 +01:00
else if(_dosHome.SequenceEqual(_header.homeFilesystem))
{
DOSFileInfo dosinfo = Marshal.ByteArrayToStructureBigEndian<DOSFileInfo>(finfo);
2020-02-29 18:03:35 +00:00
2021-09-15 13:03:42 +01:00
LastWriteTime =
2017-12-23 03:59:48 +00:00
DateHandlers.DosToDateTime(dosinfo.modificationDate, dosinfo.modificationTime);
}
2018-06-22 08:08:38 +01:00
break;
case EntryId.ResourceFork:
2020-07-20 21:11:32 +01:00
_rsrcFork = entry;
2020-02-29 18:03:35 +00:00
break;
}
_dataFork = new Entry
2020-02-29 18:03:35 +00:00
{
id = (uint)EntryId.DataFork
2020-02-29 18:03:35 +00:00
};
if(File.Exists(path))
{
2020-02-29 18:03:35 +00:00
var dataFs = new FileStream(path, FileMode.Open, FileAccess.Read);
2020-07-20 21:11:32 +01:00
_dataFork.length = (uint)dataFs.Length;
dataFs.Close();
}
fs.Close();
2021-09-15 13:03:42 +01:00
BasePath = path;
return Errno.NoError;
}
enum EntryId : uint
{
2020-02-29 18:03:35 +00: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,
2020-07-20 21:11:32 +01:00
DOSFileInfo = 12, ShortName = 13, AfpFileInfo = 14,
2020-02-29 18:03:35 +00:00
DirectoryID = 15
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct Header
{
public readonly uint magic;
public readonly uint version;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] homeFilesystem;
public readonly ushort entries;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Entry
{
public uint id;
public readonly uint offset;
public uint length;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct FileDates
{
public readonly uint creationDate;
public readonly uint modificationDate;
public readonly uint backupDate;
public readonly uint accessDate;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct MacFileInfo
{
public readonly uint creationDate;
public readonly uint modificationDate;
public readonly uint backupDate;
public readonly uint accessDate;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct UnixFileInfo
{
public readonly uint creationDate;
public readonly uint accessDate;
public readonly uint modificationDate;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct DOSFileInfo
{
public readonly ushort modificationDate;
public readonly ushort modificationTime;
public readonly ushort attributes;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct ProDOSFileInfo
{
public readonly uint creationDate;
public readonly uint modificationDate;
public readonly uint backupDate;
public readonly ushort access;
public readonly ushort fileType;
public readonly uint auxType;
}
}
2017-12-19 20:33:03 +00:00
}