mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 19:24:27 +00:00
* DiscImageChef.Helpers/DiscImageChef.Helpers.csproj: Added code that directly marshals from a big-endian byte array. But untested with nested structures. * DiscImageChef.Partitions/Acorn.cs: Added support for Acorn FileCore partition, closes #4. * DiscImageChef.Partitions/BSD.cs: Moved BSD partitions from inside MBR code to separate code, as they can (and do) appear on other architectures as the only scheme. * DiscImageChef.Partitions/DEC.cs: Added support for DEC disklabels, closes #11. * DiscImageChef.Partitions/DragonFlyBSD.cs: Added support for DragonFly BSD 64-bit disklabels. * DiscImageChef.Partitions/PC98.cs: Added support for NEC PC-9800 partitions. * DiscImageChef.Partitions/RioKarma.cs: Added support for Rio Karma partitions. * DiscImageChef.Partitions/SGI.cs: Added support for SGI DVHs, closes #9. * DiscImageChef.Partitions/UNIX.cs: Moved UNIX partitions from inside MBR code to separate code, as they can (and do) appear on other architectures as the only scheme. * TODO: * README.md: * DiscImageChef.Partitions/DiscImageChef.Partitions.csproj: Added support for Acorn FileCore partition, closes #4. Added support for DEC disklabels, closes #11. Added support for SGI DVHs, closes #9. Moved BSD partitions from inside MBR code to separate code, as they can (and do) appear on other architectures as the only scheme. Added support for DragonFly BSD 64-bit disklabels. Added support for NEC PC-9800 partitions. Added support for Rio Karma partitions. Moved UNIX partitions from inside MBR code to separate code, as they can (and do) appear on other architectures as the only scheme. * DiscImageChef.Partitions/GPT.cs: Added new partition type UUIDs. * DiscImageChef.Partitions/MBR.cs: Moved BSD partitions from inside MBR code to separate code, as they can (and do) appear on other architectures as the only scheme. Moved UNIX partitions from inside MBR code to separate code, as they can (and do) appear on other architectures as the only scheme. * DiscImageChef.Partitions/Sun.cs: Added new partition types. Prepare structures for marshaling.
119 lines
5.2 KiB
C#
119 lines
5.2 KiB
C#
// /***************************************************************************
|
|
// The Disc Image Chef
|
|
// ----------------------------------------------------------------------------
|
|
//
|
|
// Filename : EndianSwapStructure.cs
|
|
// Author(s) : Natalia Portillo <claunia@claunia.com>
|
|
//
|
|
// Component : Component
|
|
//
|
|
// --[ Description ] ----------------------------------------------------------
|
|
//
|
|
// Description
|
|
//
|
|
// --[ 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-2016 Natalia Portillo
|
|
// ****************************************************************************/
|
|
using System;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace DiscImageChef.Helpers
|
|
{
|
|
public static class BigEndianStructure
|
|
{
|
|
// TODO: Check this works
|
|
/// <summary>
|
|
/// Marshals a big-endian byte array to a C# structure. Dunno if it works with nested structures.
|
|
/// </summary>
|
|
/// <returns>The big endian byte array.</returns>
|
|
/// <param name="bytes">Byte array.</param>
|
|
/// <typeparam name="T">C# structure type.</typeparam>
|
|
public static T ByteArrayToStructureBigEndian<T>(byte[] bytes) where T : struct
|
|
{
|
|
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
|
|
T stuff = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
|
|
handle.Free();
|
|
Type t = stuff.GetType();
|
|
FieldInfo[] fieldInfo = t.GetFields();
|
|
foreach(FieldInfo fi in fieldInfo)
|
|
{
|
|
if(fi.FieldType == typeof(short))
|
|
{
|
|
short i16 = (short)fi.GetValue(stuff);
|
|
byte[] b16 = BitConverter.GetBytes(i16);
|
|
byte[] b16r = b16.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToInt16(b16r, 0));
|
|
}
|
|
else if(fi.FieldType == typeof(int))
|
|
{
|
|
int i32 = (int)fi.GetValue(stuff);
|
|
byte[] b32 = BitConverter.GetBytes(i32);
|
|
byte[] b32r = b32.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToInt32(b32r, 0));
|
|
}
|
|
else if(fi.FieldType == typeof(long))
|
|
{
|
|
long i64 = (long)fi.GetValue(stuff);
|
|
byte[] b64 = BitConverter.GetBytes(i64);
|
|
byte[] b64r = b64.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToInt64(b64r, 0));
|
|
}
|
|
else if(fi.FieldType == typeof(ushort))
|
|
{
|
|
ushort i16 = (ushort)fi.GetValue(stuff);
|
|
byte[] b16 = BitConverter.GetBytes(i16);
|
|
byte[] b16r = b16.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToUInt16(b16r, 0));
|
|
}
|
|
else if(fi.FieldType == typeof(uint))
|
|
{
|
|
uint i32 = (uint)fi.GetValue(stuff);
|
|
byte[] b32 = BitConverter.GetBytes(i32);
|
|
byte[] b32r = b32.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToUInt32(b32r, 0));
|
|
}
|
|
else if(fi.FieldType == typeof(ulong))
|
|
{
|
|
ulong i64 = (ulong)fi.GetValue(stuff);
|
|
byte[] b64 = BitConverter.GetBytes(i64);
|
|
byte[] b64r = b64.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToUInt64(b64r, 0));
|
|
}
|
|
else if(fi.FieldType == typeof(float))
|
|
{
|
|
float iflt = (float)fi.GetValue(stuff);
|
|
byte[] bflt = BitConverter.GetBytes(iflt);
|
|
byte[] bfltr = bflt.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToSingle(bfltr, 0));
|
|
}
|
|
else if(fi.FieldType == typeof(double))
|
|
{
|
|
double idbl = (double)fi.GetValue(stuff);
|
|
byte[] bdbl = BitConverter.GetBytes(idbl);
|
|
byte[] bdblr = bdbl.Reverse().ToArray();
|
|
fi.SetValueDirect(__makeref(stuff), BitConverter.ToDouble(bdblr, 0));
|
|
}
|
|
}
|
|
return stuff;
|
|
}
|
|
}
|
|
}
|
|
|