Files
Aaru/Aaru.Filesystems/Cram.cs

161 lines
5.6 KiB
C#
Raw Normal View History

// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
// ----------------------------------------------------------------------------
//
// Filename : Cram.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Cram file system plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Identifies the Cram file system and shows information.
//
// --[ 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/>.
//
// ----------------------------------------------------------------------------
2022-02-18 10:02:53 +00:00
// Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/
2022-03-07 07:36:44 +00:00
// ReSharper disable UnusedMember.Local
namespace Aaru.Filesystems;
using System;
2020-07-20 07:47:12 +01:00
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
2020-02-27 00:33:26 +00:00
using Aaru.CommonTypes;
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;
2017-12-21 14:30:38 +00:00
using Schemas;
2020-02-27 00:33:26 +00:00
using Marshal = Aaru.Helpers.Marshal;
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
/// <summary>Implements detection of the CRAM filesystem</summary>
[SuppressMessage("ReSharper", "UnusedType.Local")]
public sealed class Cram : IFilesystem
{
2022-03-06 13:29:38 +00:00
/// <summary>Identifier for Cram</summary>
const uint CRAM_MAGIC = 0x28CD3D45;
const uint CRAM_CIGAM = 0x453DCD28;
/// <inheritdoc />
public FileSystemType XmlFsType { get; private set; }
/// <inheritdoc />
public Encoding Encoding { get; private set; }
/// <inheritdoc />
public string Name => "Cram filesystem";
/// <inheritdoc />
public Guid Id => new("F8F6E46F-7A2A-48E3-9C0A-46AF4DC29E09");
/// <inheritdoc />
public string Author => "Natalia Portillo";
/// <inheritdoc />
2022-03-06 13:29:38 +00:00
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
2022-03-06 13:29:38 +00:00
if(partition.Start >= partition.End)
return false;
2022-03-06 13:29:38 +00:00
ErrorNumber errno = imagePlugin.ReadSector(partition.Start, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return false;
2022-03-07 07:36:44 +00:00
var magic = BitConverter.ToUInt32(sector, 0x00);
2022-03-16 11:47:00 +00:00
return magic is CRAM_MAGIC or CRAM_CIGAM;
2022-03-06 13:29:38 +00:00
}
2022-03-06 13:29:38 +00:00
/// <inheritdoc />
2022-03-07 07:36:44 +00:00
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information, Encoding encoding)
2022-03-06 13:29:38 +00:00
{
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
information = "";
ErrorNumber errno = imagePlugin.ReadSector(partition.Start, out byte[] sector);
2022-03-06 13:29:38 +00:00
if(errno != ErrorNumber.NoError)
return;
2022-03-07 07:36:44 +00:00
var magic = BitConverter.ToUInt32(sector, 0x00);
2022-03-06 13:29:38 +00:00
2022-03-07 07:36:44 +00:00
var crSb = new SuperBlock();
var littleEndian = true;
2022-03-06 13:29:38 +00:00
switch(magic)
{
2022-03-06 13:29:38 +00:00
case CRAM_MAGIC:
crSb = Marshal.ByteArrayToStructureLittleEndian<SuperBlock>(sector);
break;
case CRAM_CIGAM:
crSb = Marshal.ByteArrayToStructureBigEndian<SuperBlock>(sector);
littleEndian = false;
break;
}
2022-03-06 13:29:38 +00:00
var sbInformation = new StringBuilder();
sbInformation.AppendLine("Cram file system");
sbInformation.AppendLine(littleEndian ? "Little-endian" : "Big-endian");
sbInformation.AppendFormat("Volume edition {0}", crSb.edition).AppendLine();
sbInformation.AppendFormat("Volume name: {0}", StringHandlers.CToString(crSb.name, Encoding)).AppendLine();
sbInformation.AppendFormat("Volume has {0} bytes", crSb.size).AppendLine();
sbInformation.AppendFormat("Volume has {0} blocks", crSb.blocks).AppendLine();
sbInformation.AppendFormat("Volume has {0} files", crSb.files).AppendLine();
information = sbInformation.ToString();
XmlFsType = new FileSystemType
{
2022-03-06 13:29:38 +00:00
VolumeName = StringHandlers.CToString(crSb.name, Encoding),
Type = "Cram file system",
Clusters = crSb.blocks,
Files = crSb.files,
FilesSpecified = true,
FreeClusters = 0,
FreeClustersSpecified = true
};
}
enum CramCompression : ushort
{
2022-03-07 07:36:44 +00:00
Zlib = 1,
Lzma = 2,
Lzo = 3,
Xz = 4,
Lz4 = 5
2022-03-06 13:29:38 +00:00
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct SuperBlock
{
public readonly uint magic;
public readonly uint size;
public readonly uint flags;
public readonly uint future;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] signature;
public readonly uint crc;
public readonly uint edition;
public readonly uint blocks;
public readonly uint files;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public readonly byte[] name;
}
}