Files
Aaru/Aaru.Filesystems/Cram.cs

153 lines
5.8 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/>.
//
// ----------------------------------------------------------------------------
2020-12-31 23:08:23 +00:00
// Copyright © 2011-2021 Natalia Portillo
// ****************************************************************************/
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.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;
2020-07-20 07:47:12 +01:00
// ReSharper disable UnusedMember.Local
2020-02-27 00:33:26 +00:00
namespace Aaru.Filesystems
{
/// <inheritdoc />
2021-08-17 13:56:05 +01:00
/// <summary>
/// Implements detection of the CRAM filesystem
/// </summary>
2020-07-20 07:47:12 +01:00
[SuppressMessage("ReSharper", "UnusedType.Local")]
2020-07-22 13:20:25 +01:00
public sealed class Cram : IFilesystem
{
2020-02-29 18:03:35 +00:00
/// <summary>Identifier for Cram</summary>
const uint CRAM_MAGIC = 0x28CD3D45;
const uint CRAM_CIGAM = 0x453DCD28;
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2017-12-26 08:01:40 +00:00
public FileSystemType XmlFsType { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-06-22 08:08:38 +01:00
public Encoding Encoding { get; private set; }
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-06-22 08:08:38 +01:00
public string Name => "Cram filesystem";
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2018-06-22 08:08:38 +01:00
public Guid Id => new Guid("F8F6E46F-7A2A-48E3-9C0A-46AF4DC29E09");
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 />
public bool Identify(IMediaImage imagePlugin, Partition partition)
{
2020-02-29 18:03:35 +00:00
if(partition.Start >= partition.End)
return false;
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSector(partition.Start);
uint magic = BitConverter.ToUInt32(sector, 0x00);
return magic == CRAM_MAGIC || magic == CRAM_CIGAM;
}
2021-08-17 13:56:05 +01:00
/// <inheritdoc />
2017-12-26 08:01:40 +00:00
public void GetInformation(IMediaImage imagePlugin, Partition partition, out string information,
2020-02-29 18:03:35 +00:00
Encoding encoding)
{
2017-12-26 08:01:40 +00:00
Encoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSector(partition.Start);
2018-06-22 08:08:38 +01:00
uint magic = BitConverter.ToUInt32(sector, 0x00);
2020-07-20 21:11:32 +01:00
var crSb = new SuperBlock();
2020-02-29 18:03:35 +00:00
bool littleEndian = true;
switch(magic)
{
case CRAM_MAGIC:
2020-07-20 21:11:32 +01:00
crSb = Marshal.ByteArrayToStructureLittleEndian<SuperBlock>(sector);
2020-02-29 18:03:35 +00:00
break;
case CRAM_CIGAM:
2020-07-20 21:11:32 +01:00
crSb = Marshal.ByteArrayToStructureBigEndian<SuperBlock>(sector);
littleEndian = false;
2020-02-29 18:03:35 +00:00
break;
}
2020-02-29 18:03:35 +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();
2017-12-26 08:01:40 +00:00
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();
2017-12-26 08:01:40 +00:00
XmlFsType = new FileSystemType
{
2020-07-20 04:34:16 +01: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
{
2020-02-29 18:03:35 +00:00
Zlib = 1, Lzma = 2, Lzo = 3,
Xz = 4, Lz4 = 5
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
readonly struct SuperBlock
{
2020-02-29 18:03:35 +00:00
public readonly uint magic;
public readonly uint size;
public readonly uint flags;
public readonly uint future;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2020-02-29 18:03:35 +00:00
public readonly byte[] signature;
public readonly uint crc;
public readonly uint edition;
public readonly uint blocks;
public readonly uint files;
2018-06-22 08:08:38 +01:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
2020-02-29 18:03:35 +00:00
public readonly byte[] name;
}
}
}