2017-07-19 16:31:08 +01:00
|
|
|
|
// /***************************************************************************
|
2020-02-27 12:31:25 +00:00
|
|
|
|
// Aaru Data Preservation Suite
|
2016-09-14 14:25:08 +01:00
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Filename : Squash.cs
|
|
|
|
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
|
|
|
|
//
|
|
|
|
|
|
// Component : Squash file system plugin.
|
|
|
|
|
|
//
|
|
|
|
|
|
// --[ Description ] ----------------------------------------------------------
|
|
|
|
|
|
//
|
|
|
|
|
|
// Identifies the Squash 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-12-03 16:07:10 +00:00
|
|
|
|
// Copyright © 2011-2023 Natalia Portillo
|
2016-09-14 14:25:08 +01:00
|
|
|
|
// ****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
using System.Text;
|
2020-02-27 00:33:26 +00:00
|
|
|
|
using Aaru.CommonTypes;
|
2021-09-19 21:16:47 +01:00
|
|
|
|
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;
|
2016-09-14 14:25:08 +01:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
namespace Aaru.Filesystems;
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <summary>Implements detection of the squash filesystem</summary>
|
|
|
|
|
|
public sealed class Squash : IFilesystem
|
2016-09-14 14:25:08 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <summary>Identifier for Squash</summary>
|
|
|
|
|
|
const uint SQUASH_MAGIC = 0x73717368;
|
|
|
|
|
|
const uint SQUASH_CIGAM = 0x68737173;
|
|
|
|
|
|
|
2022-11-28 02:59:53 +00:00
|
|
|
|
const string FS_TYPE = "squashfs";
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public FileSystemType XmlFsType { get; private set; }
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public Encoding Encoding { get; private set; }
|
|
|
|
|
|
/// <inheritdoc />
|
2022-11-28 02:59:53 +00:00
|
|
|
|
public string Name => Localization.Squash_Name;
|
2021-08-17 14:25:12 +01:00
|
|
|
|
/// <inheritdoc />
|
2022-03-06 13:29:38 +00:00
|
|
|
|
public Guid Id => new("F8F6E46F-7A2A-48E3-9C0A-46AF4DC29E09");
|
|
|
|
|
|
/// <inheritdoc />
|
2022-11-28 02:59:53 +00:00
|
|
|
|
public string Author => Authors.NataliaPortillo;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool Identify(IMediaImage imagePlugin, Partition partition)
|
2016-09-14 14:25:08 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(partition.Start >= partition.End)
|
|
|
|
|
|
return false;
|
2016-09-14 14:25:08 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
ErrorNumber errno = imagePlugin.ReadSector(partition.Start, out byte[] sector);
|
2021-09-19 21:16:47 +01:00
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return false;
|
2016-09-14 14:25:08 +01:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
uint magic = BitConverter.ToUInt32(sector, 0x00);
|
2016-09-14 14:25:08 +01:00
|
|
|
|
|
2022-03-16 11:47:00 +00:00
|
|
|
|
return magic is SQUASH_MAGIC or SQUASH_CIGAM;
|
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.UTF8;
|
|
|
|
|
|
information = "";
|
|
|
|
|
|
ErrorNumber errno = imagePlugin.ReadSector(partition.Start, out byte[] sector);
|
|
|
|
|
|
|
|
|
|
|
|
if(errno != ErrorNumber.NoError)
|
|
|
|
|
|
return;
|
2016-09-14 14:25:08 +01:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
uint magic = BitConverter.ToUInt32(sector, 0x00);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2022-11-15 15:58:43 +00:00
|
|
|
|
var sqSb = new SuperBlock();
|
|
|
|
|
|
bool littleEndian = true;
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
switch(magic)
|
2016-09-14 14:25:08 +01:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
case SQUASH_MAGIC:
|
|
|
|
|
|
sqSb = Marshal.ByteArrayToStructureLittleEndian<SuperBlock>(sector);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case SQUASH_CIGAM:
|
|
|
|
|
|
sqSb = Marshal.ByteArrayToStructureBigEndian<SuperBlock>(sector);
|
|
|
|
|
|
littleEndian = false;
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2016-09-14 14:25:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
var sbInformation = new StringBuilder();
|
|
|
|
|
|
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendLine(Localization.Squash_file_system);
|
|
|
|
|
|
sbInformation.AppendLine(littleEndian ? Localization.Little_endian : Localization.Big_endian);
|
|
|
|
|
|
sbInformation.AppendFormat(Localization.Volume_version_0_1, sqSb.s_major, sqSb.s_minor).AppendLine();
|
|
|
|
|
|
sbInformation.AppendFormat(Localization.Volume_has_0_bytes, sqSb.bytes_used).AppendLine();
|
|
|
|
|
|
sbInformation.AppendFormat(Localization.Volume_has_0_bytes_per_block, sqSb.block_size).AppendLine();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.
|
|
|
|
|
|
AppendFormat(Localization.Volume_created_on_0, DateHandlers.UnixUnsignedToDateTime(sqSb.mkfs_time)).
|
|
|
|
|
|
AppendLine();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendFormat(Localization.Volume_has_0_inodes, sqSb.inodes).AppendLine();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
switch(sqSb.compression)
|
2017-12-24 02:37:41 +00:00
|
|
|
|
{
|
2022-03-06 13:29:38 +00:00
|
|
|
|
case (ushort)SquashCompression.Lz4:
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendLine(Localization.Volume_is_compressed_using_LZ4);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case (ushort)SquashCompression.Lzo:
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendLine(Localization.Volume_is_compressed_using_LZO);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case (ushort)SquashCompression.Lzma:
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendLine(Localization.Volume_is_compressed_using_LZMA);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case (ushort)SquashCompression.Xz:
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendLine(Localization.Volume_is_compressed_using_XZ);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case (ushort)SquashCompression.Zlib:
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendLine(Localization.Volume_is_compressed_using_GZIP);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
case (ushort)SquashCompression.Zstd:
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.AppendLine(Localization.Volume_is_compressed_using_Zstandard);
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2022-11-28 02:59:53 +00:00
|
|
|
|
sbInformation.
|
|
|
|
|
|
AppendFormat(Localization.Volume_is_compressed_using_unknown_algorithm_0, sqSb.compression).
|
|
|
|
|
|
AppendLine();
|
2022-03-06 13:29:38 +00:00
|
|
|
|
|
|
|
|
|
|
break;
|
2017-12-24 02:37:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 13:29:38 +00:00
|
|
|
|
information = sbInformation.ToString();
|
|
|
|
|
|
|
|
|
|
|
|
XmlFsType = new FileSystemType
|
2017-12-24 02:37:41 +00:00
|
|
|
|
{
|
2022-11-28 02:59:53 +00:00
|
|
|
|
Type = FS_TYPE,
|
2022-03-06 13:29:38 +00:00
|
|
|
|
CreationDate = DateHandlers.UnixUnsignedToDateTime(sqSb.mkfs_time),
|
|
|
|
|
|
CreationDateSpecified = true,
|
|
|
|
|
|
Clusters = (partition.End - partition.Start + 1) * imagePlugin.Info.SectorSize / sqSb.block_size,
|
|
|
|
|
|
ClusterSize = sqSb.block_size,
|
|
|
|
|
|
Files = sqSb.inodes,
|
|
|
|
|
|
FilesSpecified = true,
|
|
|
|
|
|
FreeClusters = 0,
|
|
|
|
|
|
FreeClustersSpecified = true
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
enum SquashCompression : ushort
|
|
|
|
|
|
{
|
2022-11-15 15:58:43 +00:00
|
|
|
|
Zlib = 1, Lzma = 2, Lzo = 3,
|
|
|
|
|
|
Xz = 4, Lz4 = 5, Zstd = 6
|
2022-03-06 13:29:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
|
readonly struct SuperBlock
|
|
|
|
|
|
{
|
|
|
|
|
|
public readonly uint magic;
|
|
|
|
|
|
public readonly uint inodes;
|
|
|
|
|
|
public readonly uint mkfs_time;
|
|
|
|
|
|
public readonly uint block_size;
|
|
|
|
|
|
public readonly uint fragments;
|
|
|
|
|
|
public readonly ushort compression;
|
|
|
|
|
|
public readonly ushort block_log;
|
|
|
|
|
|
public readonly ushort flags;
|
|
|
|
|
|
public readonly ushort no_ids;
|
|
|
|
|
|
public readonly ushort s_major;
|
|
|
|
|
|
public readonly ushort s_minor;
|
|
|
|
|
|
public readonly ulong root_inode;
|
|
|
|
|
|
public readonly ulong bytes_used;
|
|
|
|
|
|
public readonly ulong id_table_start;
|
|
|
|
|
|
public readonly ulong xattr_id_table_start;
|
|
|
|
|
|
public readonly ulong inode_table_start;
|
|
|
|
|
|
public readonly ulong directory_table_start;
|
|
|
|
|
|
public readonly ulong fragment_table_start;
|
|
|
|
|
|
public readonly ulong lookup_table_start;
|
2016-09-14 14:25:08 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|