Files
Aaru/Aaru.Filesystems/ISO9660/PathTable.cs

128 lines
4.4 KiB
C#
Raw Normal View History

2019-07-31 20:10:27 +01:00
// /***************************************************************************
2020-02-27 12:31:25 +00:00
// Aaru Data Preservation Suite
2019-07-31 20:10:27 +01:00
// ----------------------------------------------------------------------------
//
// Filename : PathTable.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : ISO9660 filesystem plugin.
//
// --[ Description ] ----------------------------------------------------------
//
// Decodes path tables.
//
// --[ 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
// In the loving memory of Facunda "Tata" Suárez Domínguez, R.I.P. 2019/07/24
2019-07-31 20:10:27 +01:00
// ****************************************************************************/
2019-07-29 02:45:46 +01:00
using System.Collections.Generic;
2020-02-27 00:33:26 +00:00
using Aaru.Helpers;
2019-07-29 02:45:46 +01:00
namespace Aaru.Filesystems;
2022-03-06 13:29:38 +00:00
public sealed partial class ISO9660
2019-07-29 02:45:46 +01:00
{
2022-03-06 13:29:38 +00:00
PathTableEntryInternal[] DecodePathTable(byte[] data)
2019-07-29 02:45:46 +01:00
{
2022-03-07 07:36:44 +00:00
if(data is null ||
data.Length == 0)
2022-03-06 13:29:38 +00:00
return null;
2019-07-29 02:45:46 +01:00
List<PathTableEntryInternal> table = new List<PathTableEntryInternal>();
2019-07-29 02:45:46 +01:00
int off = 0;
2022-03-06 13:29:38 +00:00
PathTableEntry entry =
Marshal.ByteArrayToStructureBigEndian<PathTableEntry>(data, off, Marshal.SizeOf<PathTableEntry>());
2022-03-06 13:29:38 +00:00
if(entry.name_len != 1 ||
entry.parent_dirno != 1 ||
data.Length <= Marshal.SizeOf<PathTableEntry>() ||
data[Marshal.SizeOf<PathTableEntry>()] != 0x00)
return null;
2019-07-29 02:45:46 +01:00
2022-03-06 13:29:38 +00:00
while(off < data.Length)
{
2022-03-07 07:36:44 +00:00
entry = Marshal.ByteArrayToStructureBigEndian<PathTableEntry>(data, off, Marshal.SizeOf<PathTableEntry>());
2019-07-29 02:45:46 +01:00
2022-03-06 13:29:38 +00:00
if(entry.name_len == 0)
break;
2019-07-29 02:45:46 +01:00
2022-03-06 13:29:38 +00:00
off += Marshal.SizeOf<PathTableEntry>();
2019-07-29 02:45:46 +01:00
2022-03-06 13:29:38 +00:00
string name = Encoding.GetString(data, off, entry.name_len);
2019-07-29 02:45:46 +01:00
2022-03-06 13:29:38 +00:00
table.Add(new PathTableEntryInternal
{
Extent = entry.start_lbn,
Name = name,
Parent = entry.parent_dirno,
XattrLength = entry.xattr_len
});
2019-07-29 02:45:46 +01:00
2022-03-06 13:29:38 +00:00
off += entry.name_len;
2019-07-29 02:45:46 +01:00
2022-03-06 13:29:38 +00:00
if(entry.name_len % 2 != 0)
off++;
2019-07-29 02:45:46 +01:00
}
2022-03-06 13:29:38 +00:00
return table.ToArray();
}
2022-03-06 13:29:38 +00:00
PathTableEntryInternal[] DecodeHighSierraPathTable(byte[] data)
{
if(data is null)
return null;
List<PathTableEntryInternal> table = new List<PathTableEntryInternal>();
2020-02-29 18:03:35 +00:00
int off = 0;
2022-03-06 13:29:38 +00:00
while(off < data.Length)
{
HighSierraPathTableEntry entry =
Marshal.ByteArrayToStructureBigEndian<HighSierraPathTableEntry>(data, off,
2022-03-07 07:36:44 +00:00
Marshal.
SizeOf<
HighSierraPathTableEntry>());
2022-03-06 13:29:38 +00:00
if(entry.name_len == 0)
break;
2022-03-06 13:29:38 +00:00
off += Marshal.SizeOf<HighSierraPathTableEntry>();
2022-03-06 13:29:38 +00:00
string name = Encoding.GetString(data, off, entry.name_len);
2022-03-06 13:29:38 +00:00
table.Add(new PathTableEntryInternal
{
Extent = entry.start_lbn,
Name = name,
Parent = entry.parent_dirno,
XattrLength = entry.xattr_len
});
2022-03-06 13:29:38 +00:00
off += entry.name_len;
2022-03-06 13:29:38 +00:00
if(entry.name_len % 2 != 0)
off++;
}
2022-03-06 13:29:38 +00:00
return table.ToArray();
2019-07-29 02:45:46 +01:00
}
}