Files
Aaru/DiscImageChef.Filesystems/Cram.cs

214 lines
7.3 KiB
C#
Raw Normal View History

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using DiscImageChef.CommonTypes;
2017-12-21 14:30:38 +00:00
using DiscImageChef.DiscImages;
using Schemas;
namespace DiscImageChef.Filesystems
{
2017-07-01 03:26:08 +01:00
public class Cram : Filesystem
{
public Cram()
{
Name = "Cram filesystem";
PluginUuid = new Guid("F8F6E46F-7A2A-48E3-9C0A-46AF4DC29E09");
CurrentEncoding = Encoding.GetEncoding("iso-8859-15");
}
public Cram(Encoding encoding)
{
Name = "Cram filesystem";
PluginUuid = new Guid("F8F6E46F-7A2A-48E3-9C0A-46AF4DC29E09");
CurrentEncoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
}
2017-12-21 14:30:38 +00:00
public Cram(ImagePlugin imagePlugin, Partition partition, Encoding encoding)
{
Name = "Cram filesystem";
PluginUuid = new Guid("F8F6E46F-7A2A-48E3-9C0A-46AF4DC29E09");
CurrentEncoding = encoding ?? Encoding.GetEncoding("iso-8859-15");
}
enum CramCompression : ushort
{
Zlib = 1,
Lzma = 2,
Lzo = 3,
Xz = 4,
Lz4 = 5
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct CramSuperBlock
{
public uint magic;
public uint size;
public uint flags;
public uint future;
2017-12-19 20:33:03 +00:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] signature;
public uint crc;
public uint edition;
public uint blocks;
public uint files;
2017-12-19 20:33:03 +00:00
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public byte[] name;
}
/// <summary>
/// Identifier for Cram
/// </summary>
const uint CRAM_MAGIC = 0x28CD3D45;
const uint CRAM_CIGAM = 0x453DCD28;
2017-12-21 14:30:38 +00:00
public override bool Identify(ImagePlugin imagePlugin, Partition partition)
{
2017-12-19 20:33:03 +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;
}
2017-12-21 14:30:38 +00:00
public override void GetInformation(ImagePlugin imagePlugin, Partition partition,
2017-12-19 20:33:03 +00:00
out string information)
{
2017-07-19 16:37:11 +01:00
byte[] sector = imagePlugin.ReadSector(partition.Start);
uint magic = BitConverter.ToUInt32(sector, 0x00);
CramSuperBlock crSb = new CramSuperBlock();
bool littleEndian = true;
switch(magic) {
case CRAM_MAGIC:
IntPtr crSbPtr = Marshal.AllocHGlobal(Marshal.SizeOf(crSb));
Marshal.Copy(sector, 0, crSbPtr, Marshal.SizeOf(crSb));
crSb = (CramSuperBlock)Marshal.PtrToStructure(crSbPtr, typeof(CramSuperBlock));
Marshal.FreeHGlobal(crSbPtr);
break;
case CRAM_CIGAM:
crSb = BigEndianMarshal.ByteArrayToStructureBigEndian<CramSuperBlock>(sector);
littleEndian = false;
break;
}
StringBuilder 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-19 20:33:03 +00:00
sbInformation.AppendFormat("Volume name: {0}", StringHandlers.CToString(crSb.name, CurrentEncoding))
.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
{
VolumeName = StringHandlers.CToString(crSb.name, CurrentEncoding),
Type = "Cram file system",
Clusters = crSb.blocks,
Files = crSb.files,
FilesSpecified = true,
FreeClusters = 0,
FreeClustersSpecified = true
};
}
public override Errno Mount()
{
return Errno.NotImplemented;
}
public override Errno Mount(bool debug)
{
return Errno.NotImplemented;
}
public override Errno Unmount()
{
return Errno.NotImplemented;
}
public override Errno MapBlock(string path, long fileBlock, ref long deviceBlock)
{
return Errno.NotImplemented;
}
public override Errno GetAttributes(string path, ref FileAttributes attributes)
{
return Errno.NotImplemented;
}
public override Errno ListXAttr(string path, ref List<string> xattrs)
{
return Errno.NotImplemented;
}
public override Errno GetXattr(string path, string xattr, ref byte[] buf)
{
return Errno.NotImplemented;
}
public override Errno Read(string path, long offset, long size, ref byte[] buf)
{
return Errno.NotImplemented;
}
public override Errno ReadDir(string path, ref List<string> contents)
{
return Errno.NotImplemented;
}
public override Errno StatFs(ref FileSystemInfo stat)
{
return Errno.NotImplemented;
}
public override Errno Stat(string path, ref FileEntryInfo stat)
{
return Errno.NotImplemented;
}
public override Errno ReadLink(string path, ref string dest)
{
return Errno.NotImplemented;
}
}
}