Files
Aaru/Aaru.Filters/MacBinary.cs

375 lines
14 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-07 22:32:44 +01:00
// ----------------------------------------------------------------------------
//
// Filename : MacBinary.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Filters.
2016-09-07 22:32:44 +01:00
//
// --[ Description ] ----------------------------------------------------------
//
// Provides a filter to open MacBinary 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
2016-09-07 22:32:44 +01:00
// ****************************************************************************/
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes.Interfaces;
2020-07-20 15:43:52 +01:00
using Aaru.Helpers;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2016-09-07 22:32:44 +01:00
2020-02-27 00:33:26 +00:00
namespace Aaru.Filters
2016-09-07 22:32:44 +01:00
{
// TODO: Interpret fdScript
2020-02-29 18:03:35 +00:00
/// <summary>Decodes MacBinary files</summary>
2020-07-22 13:20:25 +01:00
public sealed class MacBinary : IFilter
2016-09-07 22:32:44 +01:00
{
const uint MAGIC = 0x6D42494E;
string _basePath;
byte[] _bytes;
DateTime _creationTime;
long _dataForkOff;
string _filename;
Header _header;
bool _isBytes, _isStream, _isPath, _opened;
DateTime _lastWriteTime;
long _rsrcForkOff;
Stream _stream;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public string Name => "MacBinary";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public Guid Id => new Guid("D7C321D3-E51F-45DF-A150-F6BFDF0D7704");
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-08-29 22:15:43 +01:00
public string Author => "Natalia Portillo";
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Close()
2016-09-07 22:32:44 +01:00
{
2020-07-20 21:11:32 +01:00
_bytes = null;
_stream?.Close();
_isBytes = false;
_isStream = false;
_isPath = false;
_opened = false;
2016-09-07 22:32:44 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public string GetBasePath() => _basePath;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public DateTime GetCreationTime() => _creationTime;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public long GetDataForkLength() => _header.dataLength;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public Stream GetDataForkStream()
2016-09-07 22:32:44 +01:00
{
2020-07-20 21:11:32 +01:00
if(_header.dataLength == 0)
2020-02-29 18:03:35 +00:00
return null;
2020-07-20 21:11:32 +01:00
if(_isBytes)
return new OffsetStream(_bytes, _dataForkOff, (_dataForkOff + _header.dataLength) - 1);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_isStream)
return new OffsetStream(_stream, _dataForkOff, (_dataForkOff + _header.dataLength) - 1);
2016-09-07 22:32:44 +01:00
2020-07-20 21:11:32 +01:00
if(_isPath)
return new OffsetStream(_basePath, FileMode.Open, FileAccess.Read, _dataForkOff,
(_dataForkOff + _header.dataLength) - 1);
2016-09-07 22:32:44 +01:00
return null;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public string GetFilename() => _filename;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public DateTime GetLastWriteTime() => _lastWriteTime;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public long GetLength() => _header.dataLength + _header.resourceLength;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public string GetParentFolder() => Path.GetDirectoryName(_basePath);
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public string GetPath() => _basePath;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public long GetResourceForkLength() => _header.resourceLength;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public Stream GetResourceForkStream()
2016-09-07 22:32:44 +01:00
{
2020-07-20 21:11:32 +01:00
if(_header.resourceLength == 0)
2020-02-29 18:03:35 +00:00
return null;
2020-07-20 21:11:32 +01:00
if(_isBytes)
return new OffsetStream(_bytes, _rsrcForkOff, (_rsrcForkOff + _header.resourceLength) - 1);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_isStream)
return new OffsetStream(_stream, _rsrcForkOff, (_rsrcForkOff + _header.resourceLength) - 1);
2016-09-07 22:32:44 +01:00
2020-07-20 21:11:32 +01:00
if(_isPath)
return new OffsetStream(_basePath, FileMode.Open, FileAccess.Read, _rsrcForkOff,
(_rsrcForkOff + _header.resourceLength) - 1);
2016-09-07 22:32:44 +01:00
return null;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public bool HasResourceFork() => _header.resourceLength > 0;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(byte[] buffer)
2016-09-07 22:32:44 +01:00
{
2020-02-29 18:03:35 +00:00
if(buffer == null ||
buffer.Length < 128)
return false;
2016-09-07 22:32:44 +01:00
2020-07-20 21:11:32 +01:00
byte[] hdrB = new byte[128];
Array.Copy(buffer, 0, hdrB, 0, 128);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
2016-09-07 22:32:44 +01:00
2020-07-20 21:11:32 +01:00
return _header.magic == MAGIC || (_header.version == 0 && _header.filename[0] > 0 &&
_header.filename[0] < 64 && _header.zero1 == 0 && _header.zero2 == 0 &&
_header.reserved == 0 &&
(_header.dataLength > 0 || _header.resourceLength > 0));
2016-09-07 22:32:44 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(Stream stream)
2016-09-07 22:32:44 +01:00
{
2020-02-29 18:03:35 +00:00
if(stream == null ||
stream.Length < 128)
return false;
2016-09-07 22:32:44 +01:00
2020-07-20 21:11:32 +01:00
byte[] hdrB = new byte[128];
2016-09-07 22:32:44 +01:00
stream.Seek(0, SeekOrigin.Begin);
2020-07-20 21:11:32 +01:00
stream.Read(hdrB, 0, 128);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
2016-09-07 22:32:44 +01:00
2020-07-20 21:11:32 +01:00
return _header.magic == MAGIC || (_header.version == 0 && _header.filename[0] > 0 &&
_header.filename[0] < 64 && _header.zero1 == 0 && _header.zero2 == 0 &&
_header.reserved == 0 &&
(_header.dataLength > 0 || _header.resourceLength > 0));
2016-09-07 22:32:44 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public bool Identify(string path)
2016-09-07 22:32:44 +01:00
{
if(!File.Exists(path))
return false;
2020-02-29 18:03:35 +00:00
var fstream = new FileStream(path, FileMode.Open, FileAccess.Read);
if(fstream.Length < 128)
return false;
2016-09-07 22:32:44 +01:00
2020-07-20 21:11:32 +01:00
byte[] hdrB = new byte[128];
fstream.Read(hdrB, 0, 128);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
2016-09-07 22:32:44 +01:00
fstream.Close();
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
return _header.magic == MAGIC || (_header.version == 0 && _header.filename[0] > 0 &&
_header.filename[0] < 64 && _header.zero1 == 0 && _header.zero2 == 0 &&
_header.reserved == 0 &&
(_header.dataLength > 0 || _header.resourceLength > 0));
2016-09-07 22:32:44 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2020-07-20 21:11:32 +01:00
public bool IsOpened() => _opened;
2016-09-07 22:32:44 +01:00
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(byte[] buffer)
2016-09-07 22:32:44 +01:00
{
2020-02-29 18:03:35 +00:00
var ms = new MemoryStream(buffer);
2016-09-07 22:32:44 +01:00
ms.Seek(0, SeekOrigin.Begin);
2020-07-20 21:11:32 +01:00
byte[] hdrB = new byte[128];
ms.Read(hdrB, 0, 128);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
2016-09-07 22:32:44 +01:00
uint blocks = 1;
2020-07-20 21:11:32 +01:00
blocks += (uint)(_header.secondaryHeaderLength / 128);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.secondaryHeaderLength % 128 > 0)
2020-02-29 18:03:35 +00:00
blocks++;
2020-07-20 21:11:32 +01:00
_dataForkOff = blocks * 128;
blocks += _header.dataLength / 128;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.dataLength % 128 > 0)
2020-02-29 18:03:35 +00:00
blocks++;
2020-07-20 21:11:32 +01:00
_rsrcForkOff = blocks * 128;
2018-06-22 08:08:38 +01:00
2020-07-20 21:11:32 +01:00
_filename = StringHandlers.PascalToString(_header.filename, Encoding.GetEncoding("macintosh"));
_creationTime = DateHandlers.MacToDateTime(_header.creationTime);
_lastWriteTime = DateHandlers.MacToDateTime(_header.modificationTime);
2016-09-07 22:32:44 +01:00
ms.Close();
2020-07-20 21:11:32 +01:00
_opened = true;
_isBytes = true;
_bytes = buffer;
2016-09-07 22:32:44 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(Stream stream)
2016-09-07 22:32:44 +01:00
{
stream.Seek(0, SeekOrigin.Begin);
2020-07-20 21:11:32 +01:00
byte[] hdrB = new byte[128];
stream.Read(hdrB, 0, 128);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
2016-09-07 22:32:44 +01:00
uint blocks = 1;
2020-07-20 21:11:32 +01:00
blocks += (uint)(_header.secondaryHeaderLength / 128);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.secondaryHeaderLength % 128 > 0)
2020-02-29 18:03:35 +00:00
blocks++;
2020-07-20 21:11:32 +01:00
_dataForkOff = blocks * 128;
blocks += _header.dataLength / 128;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.dataLength % 128 > 0)
2020-02-29 18:03:35 +00:00
blocks++;
2020-07-20 21:11:32 +01:00
_rsrcForkOff = blocks * 128;
2018-06-22 08:08:38 +01:00
2020-07-20 21:11:32 +01:00
_filename = StringHandlers.PascalToString(_header.filename, Encoding.GetEncoding("macintosh"));
_creationTime = DateHandlers.MacToDateTime(_header.creationTime);
_lastWriteTime = DateHandlers.MacToDateTime(_header.modificationTime);
2016-09-07 22:32:44 +01:00
stream.Seek(0, SeekOrigin.Begin);
2020-07-20 21:11:32 +01:00
_opened = true;
_isStream = true;
_stream = stream;
2016-09-07 22:32:44 +01:00
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
public void Open(string path)
2016-09-07 22:32:44 +01:00
{
2020-02-29 18:03:35 +00:00
var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
2016-09-07 22:32:44 +01:00
fs.Seek(0, SeekOrigin.Begin);
2020-07-20 21:11:32 +01:00
byte[] hdrB = new byte[128];
fs.Read(hdrB, 0, 128);
_header = Marshal.ByteArrayToStructureBigEndian<Header>(hdrB);
2016-09-07 22:32:44 +01:00
uint blocks = 1;
2020-07-20 21:11:32 +01:00
blocks += (uint)(_header.secondaryHeaderLength / 128);
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.secondaryHeaderLength % 128 > 0)
2020-02-29 18:03:35 +00:00
blocks++;
2020-07-20 21:11:32 +01:00
_dataForkOff = blocks * 128;
blocks += _header.dataLength / 128;
2020-02-29 18:03:35 +00:00
2020-07-20 21:11:32 +01:00
if(_header.dataLength % 128 > 0)
2020-02-29 18:03:35 +00:00
blocks++;
2020-07-20 21:11:32 +01:00
_rsrcForkOff = blocks * 128;
2018-06-22 08:08:38 +01:00
2020-07-20 21:11:32 +01:00
_filename = StringHandlers.PascalToString(_header.filename, Encoding.GetEncoding("macintosh"));
_creationTime = DateHandlers.MacToDateTime(_header.creationTime);
_lastWriteTime = DateHandlers.MacToDateTime(_header.modificationTime);
2016-09-07 22:32:44 +01:00
fs.Close();
2020-07-20 21:11:32 +01:00
_opened = true;
_isPath = true;
_basePath = path;
2016-09-07 22:32:44 +01:00
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Header
{
2020-02-29 18:03:35 +00:00
/// <summary>0x00, MacBinary version, 0</summary>
public readonly byte version;
/// <summary>0x01, Str63 Pascal filename</summary>
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
2020-02-29 18:03:35 +00:00
public readonly byte[] filename;
/// <summary>0x41, File type</summary>
public readonly uint type;
/// <summary>0x45, File creator</summary>
public readonly uint creator;
/// <summary>0x49, High byte of Finder flags</summary>
public readonly byte finderFlags;
/// <summary>0x4A, Must be 0</summary>
public readonly byte zero1;
/// <summary>0x4B, File's icon vertical position within its window</summary>
public readonly ushort verticalPosition;
/// <summary>0x4D, File's icon horizontal position within its window</summary>
public readonly ushort horizontalPosition;
/// <summary>0x4F, File's window or folder ID</summary>
public readonly short windowID;
/// <summary>0x51, Protected flag</summary>
public readonly byte protect;
/// <summary>0x52, Must be 0</summary>
public readonly byte zero2;
/// <summary>0x53, Size of data fork</summary>
public readonly uint dataLength;
/// <summary>0x57, Size of resource fork</summary>
public readonly uint resourceLength;
/// <summary>0x5B, File's creation time</summary>
public readonly uint creationTime;
/// <summary>0x5F, File's last modified time</summary>
public readonly uint modificationTime;
/// <summary>0x63, Length of Get Info comment</summary>
public readonly ushort commentLength;
/// <summary>0x65, Low byte of Finder flags</summary>
public readonly byte finderFlags2;
#region MacBinary III
2020-02-29 18:03:35 +00:00
/// <summary>0x66, magic identifier, "mBIN"</summary>
public readonly uint magic;
/// <summary>0x6A, fdScript from fxInfo, identifies codepage of filename</summary>
public readonly byte fdScript;
/// <summary>0x6B, fdXFlags from fxInfo, extended Mac OS 8 finder flags</summary>
public readonly byte fdXFlags;
#endregion MacBinary III
2020-02-29 18:03:35 +00:00
/// <summary>0x6C, unused</summary>
public readonly ulong reserved;
/// <summary>0x74, Total unpacked files</summary>
public readonly uint totalPackedFiles;
#region MacBinary II
2020-02-29 18:03:35 +00:00
/// <summary>0x78, Length of secondary header</summary>
public readonly ushort secondaryHeaderLength;
/// <summary>0x7A, version number of MacBinary that wrote this file, starts at 129</summary>
public readonly byte version2;
/// <summary>0x7B, version number of MacBinary required to open this file, starts at 129</summary>
public readonly byte minVersion;
/// <summary>0x7C, CRC of previous bytes</summary>
public readonly short crc;
#endregion MacBinary II
2020-02-29 18:03:35 +00:00
/// <summary>0x7E, Reserved for computer type and OS ID</summary>
public readonly short computerID;
}
2016-09-07 22:32:44 +01:00
}
2017-12-19 20:33:03 +00:00
}