mirror of
https://github.com/aaru-dps/Aaru.Helpers.git
synced 2025-12-16 19:24:35 +00:00
General code cleanup and style refactor.
This commit is contained in:
@@ -1,73 +1,73 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{F8BDF57B-1571-4CD0-84B3-B422088D359A}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>Aaru.Helpers</RootNamespace>
|
||||
<AssemblyName>Aaru.Helpers</AssemblyName>
|
||||
<ReleaseVersion>$(Version)</ReleaseVersion>
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<Version>6.0.0-alpha8</Version>
|
||||
<Company>Claunia.com</Company>
|
||||
<Copyright>Copyright © 2011-2022 Natalia Portillo</Copyright>
|
||||
<Product>Aaru Data Preservation Suite</Product>
|
||||
<Title>Aaru.Helpers</Title>
|
||||
<ApplicationVersion>$(Version)</ApplicationVersion>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<LangVersion>11</LangVersion>
|
||||
<Description>Contains helpers used by the Aaru Data Preservation Suite.</Description>
|
||||
<PackageProjectUrl>https://github.com/aaru-dps/</PackageProjectUrl>
|
||||
<PackageLicenseExpression>LGPL-2.1-only</PackageLicenseExpression>
|
||||
<RepositoryUrl>https://github.com/aaru-dps/Aaru.Helpers</RepositoryUrl>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<NeutralLanguage>en-US</NeutralLanguage>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<Authors>Natalia Portillo <claunia@claunia.com></Authors>
|
||||
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
|
||||
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<NoWarn>CS1591;CS1574</NoWarn>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<InternalsVisibleTo Include="Aaru.Tests"/>
|
||||
<InternalsVisibleTo Include="Aaru.Tests.Devices"/>
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<NrtRevisionFormat>$(Version)+{chash:8}</NrtRevisionFormat>
|
||||
<NrtResolveSimpleAttributes>true</NrtResolveSimpleAttributes>
|
||||
<NrtShowRevision>true</NrtShowRevision>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ArrayFill.cs" />
|
||||
<Compile Include="BigEndianBitConverter.cs" />
|
||||
<Compile Include="BitEndian.cs" />
|
||||
<Compile Include="DateHandlers.cs" />
|
||||
<Compile Include="Extensions.cs"/>
|
||||
<Compile Include="Marshal.cs" />
|
||||
<Compile Include="MarshallingPropertiesAttribute.cs" />
|
||||
<Compile Include="PrintHex.cs" />
|
||||
<Compile Include="StringHandlers.cs" />
|
||||
<Compile Include="Swapping.cs" />
|
||||
<Compile Include="ArrayIsEmpty.cs" />
|
||||
<Compile Include="CompareBytes.cs" />
|
||||
<Compile Include="CountBits.cs" />
|
||||
<Compile Include="CHS.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Aaru.Console\Aaru.Console.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="..\LICENSE.LGPL">
|
||||
<Link>LICENSE.LGPL</Link>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Memory" Version="4.5.5" />
|
||||
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{F8BDF57B-1571-4CD0-84B3-B422088D359A}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>Aaru.Helpers</RootNamespace>
|
||||
<AssemblyName>Aaru.Helpers</AssemblyName>
|
||||
<ReleaseVersion>$(Version)</ReleaseVersion>
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
|
||||
<Version>6.0.0-alpha8</Version>
|
||||
<Company>Claunia.com</Company>
|
||||
<Copyright>Copyright © 2011-2022 Natalia Portillo</Copyright>
|
||||
<Product>Aaru Data Preservation Suite</Product>
|
||||
<Title>Aaru.Helpers</Title>
|
||||
<ApplicationVersion>$(Version)</ApplicationVersion>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<LangVersion>11</LangVersion>
|
||||
<Description>Contains helpers used by the Aaru Data Preservation Suite.</Description>
|
||||
<PackageProjectUrl>https://github.com/aaru-dps/</PackageProjectUrl>
|
||||
<PackageLicenseExpression>LGPL-2.1-only</PackageLicenseExpression>
|
||||
<RepositoryUrl>https://github.com/aaru-dps/Aaru.Helpers</RepositoryUrl>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<NeutralLanguage>en-US</NeutralLanguage>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<Authors>Natalia Portillo <claunia@claunia.com></Authors>
|
||||
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
|
||||
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<NoWarn>CS1591;CS1574</NoWarn>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<InternalsVisibleTo Include="Aaru.Tests"/>
|
||||
<InternalsVisibleTo Include="Aaru.Tests.Devices"/>
|
||||
</ItemGroup>
|
||||
<PropertyGroup>
|
||||
<NrtRevisionFormat>$(Version)+{chash:8}</NrtRevisionFormat>
|
||||
<NrtResolveSimpleAttributes>true</NrtResolveSimpleAttributes>
|
||||
<NrtShowRevision>true</NrtShowRevision>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ArrayFill.cs"/>
|
||||
<Compile Include="BigEndianBitConverter.cs"/>
|
||||
<Compile Include="BitEndian.cs"/>
|
||||
<Compile Include="DateHandlers.cs"/>
|
||||
<Compile Include="Extensions.cs"/>
|
||||
<Compile Include="Marshal.cs"/>
|
||||
<Compile Include="MarshallingPropertiesAttribute.cs"/>
|
||||
<Compile Include="PrintHex.cs"/>
|
||||
<Compile Include="StringHandlers.cs"/>
|
||||
<Compile Include="Swapping.cs"/>
|
||||
<Compile Include="ArrayIsEmpty.cs"/>
|
||||
<Compile Include="CompareBytes.cs"/>
|
||||
<Compile Include="CountBits.cs"/>
|
||||
<Compile Include="CHS.cs"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Aaru.Console\Aaru.Console.csproj"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="..\LICENSE.LGPL">
|
||||
<Link>LICENSE.LGPL</Link>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Memory" Version="4.5.5"/>
|
||||
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3" PrivateAssets="all"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -24,11 +24,11 @@
|
||||
// Copyright(C) 2014 mykohsu
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
public static partial class ArrayHelpers
|
||||
{
|
||||
/// <summary>Fills an array with the specified value</summary>
|
||||
|
||||
@@ -30,10 +30,10 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System.Linq;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>Helper operations to work with arrays</summary>
|
||||
public static partial class ArrayHelpers
|
||||
{
|
||||
|
||||
@@ -30,11 +30,11 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// Converts base data types to an array of bytes, and an array of bytes to base data types. All info taken from
|
||||
/// the meta data of System.BitConverter. This implementation allows for Endianness consideration.
|
||||
|
||||
4
CHS.cs
4
CHS.cs
@@ -43,6 +43,6 @@ public static class CHS
|
||||
/// <param name="maxSector">Number of sectors per track</param>
|
||||
/// <returns></returns>
|
||||
public static uint ToLBA(uint cyl, uint head, uint sector, uint maxHead, uint maxSector) =>
|
||||
maxHead == 0 || maxSector == 0 ? (cyl * 16 + head) * 63 + sector - 1
|
||||
: (cyl * maxHead + head) * maxSector + sector - 1;
|
||||
maxHead == 0 || maxSector == 0 ? (((cyl * 16) + head) * 63) + sector - 1
|
||||
: (((cyl * maxHead) + head) * maxSector) + sector - 1;
|
||||
}
|
||||
@@ -30,12 +30,12 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using Aaru.Console;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>Helper operations for timestamp management (date and time)</summary>
|
||||
public static class DateHandlers
|
||||
{
|
||||
@@ -88,7 +88,7 @@ public static class DateHandlers
|
||||
/// <returns>.NET DateTime</returns>
|
||||
public static DateTime HighSierraToDateTime(byte[] vdDateTime)
|
||||
{
|
||||
var isoTime = new byte[17];
|
||||
byte[] isoTime = new byte[17];
|
||||
Array.Copy(vdDateTime, 0, isoTime, 0, 16);
|
||||
|
||||
return Iso9660ToDateTime(isoTime);
|
||||
@@ -100,8 +100,8 @@ public static class DateHandlers
|
||||
/// <returns>.NET DateTime</returns>
|
||||
public static DateTime Iso9660ToDateTime(byte[] vdDateTime)
|
||||
{
|
||||
var twoCharValue = new byte[2];
|
||||
var fourCharValue = new byte[4];
|
||||
byte[] twoCharValue = new byte[2];
|
||||
byte[] fourCharValue = new byte[4];
|
||||
|
||||
fourCharValue[0] = vdDateTime[0];
|
||||
fourCharValue[1] = vdDateTime[1];
|
||||
@@ -172,7 +172,7 @@ public static class DateHandlers
|
||||
"decodedDT = new DateTime({0}, {1}, {2}, {3}, {4}, {5}, {6}, DateTimeKind.Unspecified);",
|
||||
year, month, day, hour, minute, second, hundredths * 10);
|
||||
|
||||
var difference = (sbyte)vdDateTime[16];
|
||||
sbyte difference = (sbyte)vdDateTime[16];
|
||||
|
||||
var decodedDt = new DateTime(year, month, day, hour, minute, second, hundredths * 10, DateTimeKind.Utc);
|
||||
|
||||
@@ -257,9 +257,9 @@ public static class DateHandlers
|
||||
/// <returns>.NET DateTime</returns>
|
||||
public static DateTime CpmToDateTime(byte[] timestamp)
|
||||
{
|
||||
var days = BitConverter.ToUInt16(timestamp, 0);
|
||||
int hours = timestamp[2];
|
||||
int minutes = timestamp[3];
|
||||
ushort days = BitConverter.ToUInt16(timestamp, 0);
|
||||
int hours = timestamp[2];
|
||||
int minutes = timestamp[3];
|
||||
|
||||
DateTime temp = _amigaEpoch.AddDays(days);
|
||||
temp = temp.AddHours(hours);
|
||||
@@ -284,15 +284,15 @@ public static class DateHandlers
|
||||
byte minute, byte second, byte centiseconds, byte hundredsOfMicroseconds,
|
||||
byte microseconds)
|
||||
{
|
||||
var specification = (byte)((typeAndTimeZone & 0xF000) >> 12);
|
||||
byte specification = (byte)((typeAndTimeZone & 0xF000) >> 12);
|
||||
|
||||
long ticks = (long)centiseconds * 100000 + (long)hundredsOfMicroseconds * 1000 + (long)microseconds * 10;
|
||||
long ticks = ((long)centiseconds * 100000) + ((long)hundredsOfMicroseconds * 1000) + ((long)microseconds * 10);
|
||||
|
||||
if(specification == 0)
|
||||
return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc).AddTicks(ticks);
|
||||
|
||||
var preOffset = (ushort)(typeAndTimeZone & 0xFFF);
|
||||
short offset;
|
||||
ushort preOffset = (ushort)(typeAndTimeZone & 0xFFF);
|
||||
short offset;
|
||||
|
||||
if((preOffset & 0x800) == 0x800)
|
||||
offset = (short)(preOffset | 0xF000);
|
||||
@@ -324,7 +324,7 @@ public static class DateHandlers
|
||||
public static DateTime Os9ToDateTime(byte[] date)
|
||||
{
|
||||
if(date == null ||
|
||||
date.Length != 3 && date.Length != 5)
|
||||
(date.Length != 3 && date.Length != 5))
|
||||
return DateTime.MinValue;
|
||||
|
||||
DateTime os9Date;
|
||||
@@ -361,12 +361,12 @@ public static class DateHandlers
|
||||
{
|
||||
try
|
||||
{
|
||||
int iyear = (year >> 4) * 10 + (year & 0xF);
|
||||
int imonth = (month >> 4) * 10 + (month & 0xF);
|
||||
int iday = (day >> 4) * 10 + (day & 0xF);
|
||||
int iminute = (minute >> 4) * 10 + (minute & 0xF);
|
||||
int ihour = (hour >> 4) * 10 + (hour & 0xF);
|
||||
int isecond = (second >> 4) * 10 + (second & 0xF);
|
||||
int iyear = ((year >> 4) * 10) + (year & 0xF);
|
||||
int imonth = ((month >> 4) * 10) + (month & 0xF);
|
||||
int iday = ((day >> 4) * 10) + (day & 0xF);
|
||||
int iminute = ((minute >> 4) * 10) + (minute & 0xF);
|
||||
int ihour = ((hour >> 4) * 10) + (hour & 0xF);
|
||||
int isecond = ((second >> 4) * 10) + (second & 0xF);
|
||||
|
||||
if(iyear >= 70)
|
||||
iyear += 1900;
|
||||
|
||||
@@ -30,10 +30,10 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
public static class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
@@ -58,7 +58,7 @@ public static class Extensions
|
||||
/// </returns>
|
||||
public static int EnsureRead(this Stream s, byte[] buffer, int offset, int count)
|
||||
{
|
||||
var pos = 0;
|
||||
int pos = 0;
|
||||
int read;
|
||||
|
||||
do
|
||||
|
||||
92
Marshal.cs
92
Marshal.cs
@@ -30,14 +30,14 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>Provides methods to marshal binary data into C# structs</summary>
|
||||
public static class Marshal
|
||||
{
|
||||
@@ -250,15 +250,15 @@ public static class Marshal
|
||||
return ByteArrayToStructureLittleEndian<T>(bytes);
|
||||
|
||||
return properties.Endian switch
|
||||
{
|
||||
BitEndian.Little => properties.HasReferences ? ByteArrayToStructureLittleEndian<T>(bytes)
|
||||
: SpanToStructureLittleEndian<T>(bytes),
|
||||
BitEndian.Big => properties.HasReferences ? ByteArrayToStructureBigEndian<T>(bytes)
|
||||
: SpanToStructureBigEndian<T>(bytes),
|
||||
BitEndian.Pdp => properties.HasReferences ? ByteArrayToStructurePdpEndian<T>(bytes)
|
||||
: SpanToStructurePdpEndian<T>(bytes),
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
{
|
||||
BitEndian.Little => properties.HasReferences ? ByteArrayToStructureLittleEndian<T>(bytes)
|
||||
: SpanToStructureLittleEndian<T>(bytes),
|
||||
BitEndian.Big => properties.HasReferences ? ByteArrayToStructureBigEndian<T>(bytes)
|
||||
: SpanToStructureBigEndian<T>(bytes),
|
||||
BitEndian.Pdp => properties.HasReferences ? ByteArrayToStructurePdpEndian<T>(bytes)
|
||||
: SpanToStructurePdpEndian<T>(bytes),
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Swaps all members of a structure</summary>
|
||||
@@ -272,22 +272,22 @@ public static class Marshal
|
||||
|
||||
foreach(FieldInfo fi in fieldInfo)
|
||||
if(fi.FieldType == typeof(short) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(short))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(short)))
|
||||
{
|
||||
var x = (short)(fi.GetValue(str) ?? default(short));
|
||||
short x = (short)(fi.GetValue(str) ?? default(short));
|
||||
fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF)));
|
||||
}
|
||||
else if(fi.FieldType == typeof(int) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int)))
|
||||
{
|
||||
var x = (int)(fi.GetValue(str) ?? default(int));
|
||||
int x = (int)(fi.GetValue(str) ?? default(int));
|
||||
x = (int)(((x << 8) & 0xFF00FF00) | (((uint)x >> 8) & 0xFF00FF));
|
||||
fi.SetValue(str, (int)(((uint)x << 16) | (((uint)x >> 16) & 0xFFFF)));
|
||||
}
|
||||
else if(fi.FieldType == typeof(long) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(long))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(long)))
|
||||
{
|
||||
var x = (long)(fi.GetValue(str) ?? default(long));
|
||||
long x = (long)(fi.GetValue(str) ?? default(long));
|
||||
x = ((x & 0x00000000FFFFFFFF) << 32) | (long)(((ulong)x & 0xFFFFFFFF00000000) >> 32);
|
||||
x = ((x & 0x0000FFFF0000FFFF) << 16) | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16);
|
||||
x = ((x & 0x00FF00FF00FF00FF) << 8) | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8);
|
||||
@@ -295,22 +295,22 @@ public static class Marshal
|
||||
fi.SetValue(str, x);
|
||||
}
|
||||
else if(fi.FieldType == typeof(ushort) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ushort))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ushort)))
|
||||
{
|
||||
var x = (ushort)(fi.GetValue(str) ?? default(ushort));
|
||||
ushort x = (ushort)(fi.GetValue(str) ?? default(ushort));
|
||||
fi.SetValue(str, (ushort)((x << 8) | (x >> 8)));
|
||||
}
|
||||
else if(fi.FieldType == typeof(uint) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint)))
|
||||
{
|
||||
var x = (uint)(fi.GetValue(str) ?? default(uint));
|
||||
uint x = (uint)(fi.GetValue(str) ?? default(uint));
|
||||
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
|
||||
fi.SetValue(str, (x << 16) | (x >> 16));
|
||||
}
|
||||
else if(fi.FieldType == typeof(ulong) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ulong))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(ulong)))
|
||||
{
|
||||
var x = (ulong)(fi.GetValue(str) ?? default(ulong));
|
||||
ulong x = (ulong)(fi.GetValue(str) ?? default(ulong));
|
||||
x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32);
|
||||
x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16);
|
||||
x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8);
|
||||
@@ -318,7 +318,7 @@ public static class Marshal
|
||||
}
|
||||
else if(fi.FieldType == typeof(float))
|
||||
{
|
||||
var flt = (float)(fi.GetValue(str) ?? default(float));
|
||||
float flt = (float)(fi.GetValue(str) ?? default(float));
|
||||
byte[] flt_b = BitConverter.GetBytes(flt);
|
||||
|
||||
fi.SetValue(str, BitConverter.ToSingle(new[]
|
||||
@@ -328,7 +328,7 @@ public static class Marshal
|
||||
}
|
||||
else if(fi.FieldType == typeof(double))
|
||||
{
|
||||
var dbl = (double)(fi.GetValue(str) ?? default(double));
|
||||
double dbl = (double)(fi.GetValue(str) ?? default(double));
|
||||
byte[] dbl_b = BitConverter.GetBytes(dbl);
|
||||
|
||||
fi.SetValue(str, BitConverter.ToDouble(new[]
|
||||
@@ -348,8 +348,7 @@ public static class Marshal
|
||||
|
||||
// TODO: Swap arrays
|
||||
else if(fi.FieldType.IsValueType &&
|
||||
!fi.FieldType.IsEnum &&
|
||||
!fi.FieldType.IsArray)
|
||||
fi.FieldType is { IsEnum: false, IsArray: false })
|
||||
{
|
||||
object obj = fi.GetValue(str);
|
||||
object strc = SwapStructureMembersEndian(obj);
|
||||
@@ -382,22 +381,21 @@ public static class Marshal
|
||||
// Do nothing
|
||||
}
|
||||
else if(fi.FieldType == typeof(int) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(int)))
|
||||
{
|
||||
var x = (int)(fi.GetValue(str) ?? default(int));
|
||||
int x = (int)(fi.GetValue(str) ?? default(int));
|
||||
fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
|
||||
}
|
||||
else if(fi.FieldType == typeof(uint) ||
|
||||
fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint))
|
||||
(fi.FieldType.IsEnum && fi.FieldType.GetEnumUnderlyingType() == typeof(uint)))
|
||||
{
|
||||
var x = (uint)(fi.GetValue(str) ?? default(uint));
|
||||
uint x = (uint)(fi.GetValue(str) ?? default(uint));
|
||||
fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
|
||||
}
|
||||
|
||||
// TODO: Swap arrays
|
||||
else if(fi.FieldType.IsValueType &&
|
||||
!fi.FieldType.IsEnum &&
|
||||
!fi.FieldType.IsArray)
|
||||
fi.FieldType is { IsEnum: false, IsArray: false })
|
||||
{
|
||||
object obj = fi.GetValue(str);
|
||||
object strc = SwapStructureMembersEndianPdp(obj);
|
||||
@@ -414,8 +412,8 @@ public static class Marshal
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static byte[] StructureToByteArrayLittleEndian<T>(T str) where T : struct
|
||||
{
|
||||
var buf = new byte[SizeOf<T>()];
|
||||
var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned);
|
||||
byte[] buf = new byte[SizeOf<T>()];
|
||||
var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned);
|
||||
System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false);
|
||||
ptr.Free();
|
||||
|
||||
@@ -441,14 +439,14 @@ public static class Marshal
|
||||
if(hex is null or "")
|
||||
return -1;
|
||||
|
||||
var off = 0;
|
||||
int off = 0;
|
||||
|
||||
if(hex[0] == '0' &&
|
||||
(hex[1] == 'x' || hex[1] == 'X'))
|
||||
off = 2;
|
||||
|
||||
outBuf = new byte[(hex.Length - off) / 2];
|
||||
var count = 0;
|
||||
int count = 0;
|
||||
|
||||
for(int i = off; i < hex.Length; i += 2)
|
||||
{
|
||||
@@ -458,11 +456,11 @@ public static class Marshal
|
||||
break;
|
||||
|
||||
c -= c switch
|
||||
{
|
||||
>= 'a' and <= 'f' => '\u0057',
|
||||
>= 'A' and <= 'F' => '\u0037',
|
||||
_ => '\u0030'
|
||||
};
|
||||
{
|
||||
>= 'a' and <= 'f' => '\u0057',
|
||||
>= 'A' and <= 'F' => '\u0037',
|
||||
_ => '\u0030'
|
||||
};
|
||||
|
||||
outBuf[(i - off) / 2] = (byte)(c << 4);
|
||||
|
||||
@@ -472,11 +470,11 @@ public static class Marshal
|
||||
break;
|
||||
|
||||
c -= c switch
|
||||
{
|
||||
>= 'a' and <= 'f' => '\u0057',
|
||||
>= 'A' and <= 'F' => '\u0037',
|
||||
_ => '\u0030'
|
||||
};
|
||||
{
|
||||
>= 'a' and <= 'f' => '\u0057',
|
||||
>= 'A' and <= 'F' => '\u0037',
|
||||
_ => '\u0030'
|
||||
};
|
||||
|
||||
outBuf[(i - off) / 2] += (byte)c;
|
||||
|
||||
|
||||
@@ -36,10 +36,10 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <summary>Defines properties to help marshalling structs from binary data</summary>
|
||||
[AttributeUsage(AttributeTargets.Struct)]
|
||||
|
||||
26
PrintHex.cs
26
PrintHex.cs
@@ -30,11 +30,11 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System.Text;
|
||||
using Aaru.Console;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>Helper operations to get hexadecimal representations of byte arrays</summary>
|
||||
public static class PrintHex
|
||||
{
|
||||
@@ -56,11 +56,11 @@ public static class PrintHex
|
||||
|
||||
// TODO: Color list
|
||||
// TODO: Allow to change width
|
||||
var str = "Offset";
|
||||
int rows = array.Length / width;
|
||||
int last = array.Length % width;
|
||||
int offsetLength = $"{array.Length:X}".Length;
|
||||
var sb = new StringBuilder();
|
||||
string str = "Offset";
|
||||
int rows = array.Length / width;
|
||||
int last = array.Length % width;
|
||||
int offsetLength = $"{array.Length:X}".Length;
|
||||
var sb = new StringBuilder();
|
||||
|
||||
switch(last)
|
||||
{
|
||||
@@ -86,7 +86,7 @@ public static class PrintHex
|
||||
sb.Append(str);
|
||||
sb.Append(" ");
|
||||
|
||||
for(var i = 0; i < width; i++)
|
||||
for(int i = 0; i < width; i++)
|
||||
sb.AppendFormat(" {0:X2}", i);
|
||||
|
||||
if(color)
|
||||
@@ -94,11 +94,11 @@ public static class PrintHex
|
||||
|
||||
sb.AppendLine();
|
||||
|
||||
var b = 0;
|
||||
int b = 0;
|
||||
|
||||
string format = $"{{0:X{offsetLength}}}";
|
||||
|
||||
for(var i = 0; i < rows; i++)
|
||||
for(int i = 0; i < rows; i++)
|
||||
{
|
||||
if(color)
|
||||
sb.Append("\u001b[36m");
|
||||
@@ -112,19 +112,19 @@ public static class PrintHex
|
||||
int lastBytes = i == rows - 1 ? last : width;
|
||||
int lastSpaces = width - lastBytes;
|
||||
|
||||
for(var j = 0; j < lastBytes; j++)
|
||||
for(int j = 0; j < lastBytes; j++)
|
||||
{
|
||||
sb.AppendFormat(" {0:X2}", array[b]);
|
||||
b++;
|
||||
}
|
||||
|
||||
for(var j = 0; j < lastSpaces; j++)
|
||||
for(int j = 0; j < lastSpaces; j++)
|
||||
sb.Append(" ");
|
||||
|
||||
b -= lastBytes;
|
||||
sb.Append(" ");
|
||||
|
||||
for(var j = 0; j < lastBytes; j++)
|
||||
for(int j = 0; j < lastBytes; j++)
|
||||
{
|
||||
int v = array[b];
|
||||
sb.Append(v is > 31 and < 127 or > 159 ? (char)v : '.');
|
||||
|
||||
@@ -30,11 +30,11 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>Helper operations to work with strings</summary>
|
||||
public static class StringHandlers
|
||||
{
|
||||
@@ -54,7 +54,7 @@ public static class StringHandlers
|
||||
if(cString == null)
|
||||
return null;
|
||||
|
||||
var len = 0;
|
||||
int len = 0;
|
||||
|
||||
for(int i = start; i < cString.Length; i++)
|
||||
{
|
||||
@@ -78,7 +78,7 @@ public static class StringHandlers
|
||||
if(twoBytes && len % 2 > 0)
|
||||
len--;
|
||||
|
||||
var dest = new byte[len];
|
||||
byte[] dest = new byte[len];
|
||||
Array.Copy(cString, start, dest, 0, len);
|
||||
|
||||
return len == 0 ? "" : encoding.GetString(dest);
|
||||
@@ -100,7 +100,7 @@ public static class StringHandlers
|
||||
return null;
|
||||
|
||||
byte length = pascalString[start];
|
||||
var len = 0;
|
||||
int len = 0;
|
||||
|
||||
for(int i = start + 1; i < length + 1 && i < pascalString.Length; i++)
|
||||
{
|
||||
@@ -110,7 +110,7 @@ public static class StringHandlers
|
||||
len++;
|
||||
}
|
||||
|
||||
var dest = new byte[len];
|
||||
byte[] dest = new byte[len];
|
||||
Array.Copy(pascalString, start + 1, dest, 0, len);
|
||||
|
||||
return len == 0 ? "" : encoding.GetString(dest);
|
||||
@@ -156,13 +156,13 @@ public static class StringHandlers
|
||||
public static string DecompressUnicode(byte[] dstring)
|
||||
{
|
||||
byte compId = dstring[0];
|
||||
var temp = "";
|
||||
string temp = "";
|
||||
|
||||
if(compId != 8 &&
|
||||
compId != 16)
|
||||
return null;
|
||||
|
||||
for(var byteIndex = 1; byteIndex < dstring.Length;)
|
||||
for(int byteIndex = 1; byteIndex < dstring.Length;)
|
||||
{
|
||||
ushort unicode;
|
||||
|
||||
|
||||
@@ -30,10 +30,10 @@
|
||||
// Copyright © 2011-2022 Natalia Portillo
|
||||
// ****************************************************************************/
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Aaru.Helpers;
|
||||
|
||||
/// <summary>Helper operations to work with swapping endians</summary>
|
||||
public static class Swapping
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user