Files
Aaru/Aaru.Filters/MacBinary.cs

370 lines
14 KiB
C#
Raw Normal View History

2017-05-19 20:28:49 +01:00
// /***************************************************************************
2016-09-07 22:32:44 +01:00
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// 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-01-03 17:51:30 +00:00
// Copyright © 2011-2020 Natalia Portillo
2016-09-07 22:32:44 +01:00
// ****************************************************************************/
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using DiscImageChef.CommonTypes.Interfaces;
2019-02-27 08:49:42 +00:00
using Marshal = DiscImageChef.Helpers.Marshal;
2016-09-07 22:32:44 +01:00
namespace DiscImageChef.Filters
{
// TODO: Interpret fdScript
/// <summary>
/// Decodes MacBinary files
/// </summary>
public class MacBinary : IFilter
2016-09-07 22:32:44 +01:00
{
const uint MACBINARY_MAGIC = 0x6D42494E;
2018-06-22 08:08:38 +01:00
string basePath;
byte[] bytes;
DateTime creationTime;
2016-09-07 22:32:44 +01:00
2018-06-22 08:08:38 +01:00
long dataForkOff;
string filename;
MacBinaryHeader header;
2018-06-22 08:08:38 +01:00
bool isBytes, isStream, isPath, opened;
DateTime lastWriteTime;
long rsrcForkOff;
Stream stream;
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public string Name => "MacBinary";
public Guid Id => new Guid("D7C321D3-E51F-45DF-A150-F6BFDF0D7704");
public string Author => "Natalia Portillo";
2016-09-07 22:32:44 +01:00
public void Close()
2016-09-07 22:32:44 +01:00
{
bytes = null;
stream?.Close();
2018-06-22 08:08:38 +01:00
isBytes = false;
2016-09-07 22:32:44 +01:00
isStream = false;
2018-06-22 08:08:38 +01:00
isPath = false;
opened = false;
2016-09-07 22:32:44 +01:00
}
2018-08-29 22:15:43 +01:00
public string GetBasePath() => basePath;
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public DateTime GetCreationTime() => creationTime;
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public long GetDataForkLength() => header.dataLength;
2016-09-07 22:32:44 +01:00
public Stream GetDataForkStream()
2016-09-07 22:32:44 +01:00
{
2017-12-19 20:33:03 +00:00
if(header.dataLength == 0) return null;
2016-09-07 22:32:44 +01:00
2018-06-22 08:08:38 +01:00
if(isBytes) return new OffsetStream(bytes, dataForkOff, dataForkOff + header.dataLength - 1);
2017-12-19 20:33:03 +00:00
if(isStream) return new OffsetStream(stream, dataForkOff, dataForkOff + header.dataLength - 1);
2016-09-07 22:32:44 +01:00
if(isPath)
2017-12-19 20:33:03 +00:00
return new OffsetStream(basePath, FileMode.Open, FileAccess.Read, dataForkOff,
dataForkOff + header.dataLength - 1);
2016-09-07 22:32:44 +01:00
return null;
}
2018-08-29 22:15:43 +01:00
public string GetFilename() => filename;
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public DateTime GetLastWriteTime() => lastWriteTime;
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public long GetLength() => header.dataLength + header.resourceLength;
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public string GetParentFolder() => Path.GetDirectoryName(basePath);
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public string GetPath() => basePath;
2016-09-07 22:32:44 +01:00
2018-08-29 22:15:43 +01:00
public long GetResourceForkLength() => header.resourceLength;
2016-09-07 22:32:44 +01:00
public Stream GetResourceForkStream()
2016-09-07 22:32:44 +01:00
{
2017-12-19 20:33:03 +00:00
if(header.resourceLength == 0) return null;
2016-09-07 22:32:44 +01:00
2018-06-22 08:08:38 +01:00
if(isBytes) return new OffsetStream(bytes, rsrcForkOff, rsrcForkOff + header.resourceLength - 1);
2017-12-19 20:33:03 +00:00
if(isStream) return new OffsetStream(stream, rsrcForkOff, rsrcForkOff + header.resourceLength - 1);
2016-09-07 22:32:44 +01:00
if(isPath)
2017-12-19 20:33:03 +00:00
return new OffsetStream(basePath, FileMode.Open, FileAccess.Read, rsrcForkOff,
rsrcForkOff + header.resourceLength - 1);
2016-09-07 22:32:44 +01:00
return null;
}
2018-08-29 22:15:43 +01:00
public bool HasResourceFork() => header.resourceLength > 0;
2016-09-07 22:32:44 +01:00
public bool Identify(byte[] buffer)
2016-09-07 22:32:44 +01:00
{
2017-12-19 20:33:03 +00:00
if(buffer == null || buffer.Length < 128) return false;
2016-09-07 22:32:44 +01:00
byte[] hdr_b = new byte[128];
Array.Copy(buffer, 0, hdr_b, 0, 128);
2019-02-27 08:49:42 +00:00
header = Marshal.ByteArrayToStructureBigEndian<MacBinaryHeader>(hdr_b);
2016-09-07 22:32:44 +01:00
2018-06-22 08:08:38 +01:00
return header.magic == MACBINARY_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
}
public bool Identify(Stream stream)
2016-09-07 22:32:44 +01:00
{
2017-12-19 20:33:03 +00:00
if(stream == null || stream.Length < 128) return false;
2016-09-07 22:32:44 +01:00
byte[] hdr_b = new byte[128];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(hdr_b, 0, 128);
2019-02-27 08:49:42 +00:00
header = Marshal.ByteArrayToStructureBigEndian<MacBinaryHeader>(hdr_b);
2016-09-07 22:32:44 +01:00
2018-06-22 08:08:38 +01:00
return header.magic == MACBINARY_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
}
public bool Identify(string path)
2016-09-07 22:32:44 +01:00
{
FileStream fstream = new FileStream(path, FileMode.Open, FileAccess.Read);
if(fstream.Length < 128) return false;
2016-09-07 22:32:44 +01:00
byte[] hdr_b = new byte[128];
fstream.Read(hdr_b, 0, 128);
2019-02-27 08:49:42 +00:00
header = Marshal.ByteArrayToStructureBigEndian<MacBinaryHeader>(hdr_b);
2016-09-07 22:32:44 +01:00
fstream.Close();
2018-06-22 08:08:38 +01:00
return header.magic == MACBINARY_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
}
2018-08-29 22:15:43 +01:00
public bool IsOpened() => opened;
2016-09-07 22:32:44 +01:00
public void Open(byte[] buffer)
2016-09-07 22:32:44 +01:00
{
MemoryStream ms = new MemoryStream(buffer);
ms.Seek(0, SeekOrigin.Begin);
byte[] hdr_b = new byte[128];
ms.Read(hdr_b, 0, 128);
2019-02-27 08:49:42 +00:00
header = Marshal.ByteArrayToStructureBigEndian<MacBinaryHeader>(hdr_b);
2016-09-07 22:32:44 +01:00
uint blocks = 1;
blocks += (uint)(header.secondaryHeaderLength / 128);
2018-06-22 08:08:38 +01:00
if(header.secondaryHeaderLength % 128 > 0) blocks++;
dataForkOff = blocks * 128;
blocks += header.dataLength / 128;
if(header.dataLength % 128 > 0) blocks++;
rsrcForkOff = blocks * 128;
filename = StringHandlers.PascalToString(header.filename, Encoding.GetEncoding("macintosh"));
creationTime = DateHandlers.MacToDateTime(header.creationTime);
2016-09-07 22:32:44 +01:00
lastWriteTime = DateHandlers.MacToDateTime(header.modificationTime);
ms.Close();
2018-06-22 08:08:38 +01:00
opened = true;
2016-09-07 22:32:44 +01:00
isBytes = true;
2018-06-22 08:08:38 +01:00
bytes = buffer;
2016-09-07 22:32:44 +01:00
}
public void Open(Stream stream)
2016-09-07 22:32:44 +01:00
{
stream.Seek(0, SeekOrigin.Begin);
byte[] hdr_b = new byte[128];
stream.Read(hdr_b, 0, 128);
2019-02-27 08:49:42 +00:00
header = Marshal.ByteArrayToStructureBigEndian<MacBinaryHeader>(hdr_b);
2016-09-07 22:32:44 +01:00
uint blocks = 1;
blocks += (uint)(header.secondaryHeaderLength / 128);
2018-06-22 08:08:38 +01:00
if(header.secondaryHeaderLength % 128 > 0) blocks++;
dataForkOff = blocks * 128;
blocks += header.dataLength / 128;
if(header.dataLength % 128 > 0) blocks++;
rsrcForkOff = blocks * 128;
filename = StringHandlers.PascalToString(header.filename, Encoding.GetEncoding("macintosh"));
creationTime = DateHandlers.MacToDateTime(header.creationTime);
2016-09-07 22:32:44 +01:00
lastWriteTime = DateHandlers.MacToDateTime(header.modificationTime);
stream.Seek(0, SeekOrigin.Begin);
2018-06-22 08:08:38 +01:00
opened = true;
isStream = true;
2016-09-07 22:32:44 +01:00
this.stream = stream;
}
public void Open(string path)
2016-09-07 22:32:44 +01:00
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
fs.Seek(0, SeekOrigin.Begin);
byte[] hdr_b = new byte[128];
fs.Read(hdr_b, 0, 128);
2019-02-27 08:49:42 +00:00
header = Marshal.ByteArrayToStructureBigEndian<MacBinaryHeader>(hdr_b);
2016-09-07 22:32:44 +01:00
uint blocks = 1;
blocks += (uint)(header.secondaryHeaderLength / 128);
2018-06-22 08:08:38 +01:00
if(header.secondaryHeaderLength % 128 > 0) blocks++;
dataForkOff = blocks * 128;
blocks += header.dataLength / 128;
if(header.dataLength % 128 > 0) blocks++;
rsrcForkOff = blocks * 128;
filename = StringHandlers.PascalToString(header.filename, Encoding.GetEncoding("macintosh"));
creationTime = DateHandlers.MacToDateTime(header.creationTime);
2016-09-07 22:32:44 +01:00
lastWriteTime = DateHandlers.MacToDateTime(header.modificationTime);
fs.Close();
2018-06-22 08:08:38 +01:00
opened = true;
isPath = true;
2016-09-07 22:32:44 +01:00
basePath = path;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct MacBinaryHeader
{
/// <summary>
/// 0x00, MacBinary version, 0
/// </summary>
public byte version;
/// <summary>
/// 0x01, Str63 Pascal filename
/// </summary>
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public byte[] filename;
/// <summary>
/// 0x41, File type
/// </summary>
public uint type;
/// <summary>
/// 0x45, File creator
/// </summary>
public uint creator;
/// <summary>
/// 0x49, High byte of Finder flags
/// </summary>
public byte finderFlags;
/// <summary>
/// 0x4A, Must be 0
/// </summary>
public byte zero1;
/// <summary>
/// 0x4B, File's icon vertical position within its window
/// </summary>
public ushort verticalPosition;
/// <summary>
/// 0x4D, File's icon horizontal position within its window
/// </summary>
public ushort horizontalPosition;
/// <summary>
/// 0x4F, File's window or folder ID
/// </summary>
public short windowID;
/// <summary>
/// 0x51, Protected flag
/// </summary>
public byte protect;
/// <summary>
/// 0x52, Must be 0
/// </summary>
public byte zero2;
/// <summary>
/// 0x53, Size of data fork
/// </summary>
public uint dataLength;
/// <summary>
/// 0x57, Size of resource fork
/// </summary>
public uint resourceLength;
/// <summary>
/// 0x5B, File's creation time
/// </summary>
public uint creationTime;
/// <summary>
/// 0x5F, File's last modified time
/// </summary>
public uint modificationTime;
/// <summary>
/// 0x63, Length of Get Info comment
/// </summary>
public ushort commentLength;
/// <summary>
/// 0x65, Low byte of Finder flags
/// </summary>
public byte finderFlags2;
#region MacBinary III
/// <summary>
/// 0x66, magic identifier, "mBIN"
/// </summary>
public uint magic;
/// <summary>
/// 0x6A, fdScript from fxInfo, identifies codepage of filename
/// </summary>
public byte fdScript;
/// <summary>
/// 0x6B, fdXFlags from fxInfo, extended Mac OS 8 finder flags
/// </summary>
public byte fdXFlags;
#endregion MacBinary III
/// <summary>
/// 0x6C, unused
/// </summary>
public ulong reserved;
/// <summary>
/// 0x74, Total unpacked files
/// </summary>
public uint totalPackedFiles;
#region MacBinary II
/// <summary>
/// 0x78, Length of secondary header
/// </summary>
public ushort secondaryHeaderLength;
/// <summary>
/// 0x7A, version number of MacBinary that wrote this file, starts at 129
/// </summary>
public byte version2;
/// <summary>
/// 0x7B, version number of MacBinary required to open this file, starts at 129
/// </summary>
public byte minVersion;
/// <summary>
/// 0x7C, CRC of previous bytes
/// </summary>
public short crc;
#endregion MacBinary II
/// <summary>
/// 0x7E, Reserved for computer type and OS ID
/// </summary>
public short computerID;
}
2016-09-07 22:32:44 +01:00
}
2017-12-19 20:33:03 +00:00
}