[Aaru.Helpers] Reformat and cleanup.

This commit is contained in:
2023-10-03 23:25:24 +01:00
parent ca2f08311f
commit 57ad637c20
16 changed files with 1456 additions and 1413 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -1,2 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xml:space="preserve">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=localization/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -35,10 +35,7 @@ public static partial class ArrayHelpers
/// <param name="destinationArray">Array</param>
/// <param name="value">Value</param>
/// <typeparam name="T">Array type</typeparam>
public static void ArrayFill<T>(T[] destinationArray, T value) => ArrayFill(destinationArray, new[]
{
value
});
public static void ArrayFill<T>(T[] destinationArray, T value) => ArrayFill(destinationArray, new[] { value });
/// <summary>Fills an array with the contents of the specified array</summary>
/// <param name="destinationArray">Array</param>

5
CHS.cs
View File

@@ -43,6 +43,7 @@ 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;
}

View File

@@ -60,11 +60,13 @@ public static partial class ArrayHelpers
leastBytes = compareArray1.LongLength;
for(long i = 0; i < leastBytes; i++)
{
if(compareArray1[i] != compareArray2[i])
{
different = true;
return;
}
}
}
}

View File

@@ -40,9 +40,9 @@ public static class CountBits
/// <returns>Bits set to <c>true</c></returns>
public static int Count(uint number)
{
number -= (number >> 1) & 0x55555555;
number = (number & 0x33333333) + ((number >> 2) & 0x33333333);
number -= number >> 1 & 0x55555555;
number = (number & 0x33333333) + (number >> 2 & 0x33333333);
return (int)((((number + (number >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24);
return (int)((number + (number >> 4) & 0x0F0F0F0F) * 0x01010101 >> 24);
}
}

View File

@@ -91,7 +91,7 @@ public static class DateHandlers
/// <returns>.NET DateTime</returns>
public static DateTime HighSierraToDateTime(byte[] vdDateTime)
{
byte[] isoTime = new byte[17];
var isoTime = new byte[17];
Array.Copy(vdDateTime, 0, isoTime, 0, 16);
return Iso9660ToDateTime(isoTime);
@@ -103,8 +103,8 @@ public static class DateHandlers
/// <returns>.NET DateTime</returns>
public static DateTime Iso9660ToDateTime(byte[] vdDateTime)
{
byte[] twoCharValue = new byte[2];
byte[] fourCharValue = new byte[4];
var twoCharValue = new byte[2];
var fourCharValue = new byte[4];
fourCharValue[0] = vdDateTime[0];
fourCharValue[1] = vdDateTime[1];
@@ -175,7 +175,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);
sbyte difference = (sbyte)vdDateTime[16];
var difference = (sbyte)vdDateTime[16];
var decodedDt = new DateTime(year, month, day, hour, minute, second, hundredths * 10, DateTimeKind.Utc);
@@ -260,9 +260,9 @@ public static class DateHandlers
/// <returns>.NET DateTime</returns>
public static DateTime CpmToDateTime(byte[] timestamp)
{
ushort days = BitConverter.ToUInt16(timestamp, 0);
int hours = timestamp[2];
int minutes = timestamp[3];
var days = BitConverter.ToUInt16(timestamp, 0);
int hours = timestamp[2];
int minutes = timestamp[3];
DateTime temp = _amigaEpoch.AddDays(days);
temp = temp.AddHours(hours);
@@ -284,18 +284,18 @@ public static class DateHandlers
/// <param name="microseconds">Microseconds</param>
/// <returns></returns>
public static DateTime EcmaToDateTime(ushort typeAndTimeZone, short year, byte month, byte day, byte hour,
byte minute, byte second, byte centiseconds, byte hundredsOfMicroseconds,
byte microseconds)
byte minute, byte second, byte centiseconds, byte hundredsOfMicroseconds,
byte microseconds)
{
byte specification = (byte)((typeAndTimeZone & 0xF000) >> 12);
var 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);
ushort preOffset = (ushort)(typeAndTimeZone & 0xFFF);
short offset;
var preOffset = (ushort)(typeAndTimeZone & 0xFFF);
short offset;
if((preOffset & 0x800) == 0x800)
offset = (short)(preOffset | 0xF000);
@@ -327,15 +327,16 @@ 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;
try
{
os9Date = date.Length == 5 ? new DateTime(1900 + date[0], date[1], date[2], date[3], date[4], 0)
: new DateTime(1900 + date[0], date[1], date[2], 0, 0, 0);
os9Date = date.Length == 5
? new DateTime(1900 + date[0], date[1], date[2], date[3], date[4], 0)
: new DateTime(1900 + date[0], date[1], date[2], 0, 0, 0);
}
catch(ArgumentOutOfRangeException)
{
@@ -348,7 +349,8 @@ public static class DateHandlers
/// <summary>Converts a LIF timestamp to .NET DateTime</summary>
/// <param name="date">LIF timestamp</param>
/// <returns>.NET DateTime</returns>
public static DateTime LifToDateTime(byte[] date) => date is not { Length: 6 } ? new DateTime(1970, 1, 1, 0, 0, 0)
public static DateTime LifToDateTime(byte[] date) => date is not { Length: 6 }
? new DateTime(1970, 1, 1, 0, 0, 0)
: LifToDateTime(date[0], date[1], date[2], date[3],
date[4], date[5]);
@@ -364,12 +366,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;

View File

@@ -58,7 +58,7 @@ public static class Extensions
/// </returns>
public static int EnsureRead(this Stream s, byte[] buffer, int offset, int count)
{
int pos = 0;
var pos = 0;
int read;
do

View File

@@ -11,32 +11,46 @@ namespace Aaru.Helpers {
using System;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
/// <summary>
/// A strongly-typed resource class, for looking up localized strings, etc.
/// </summary>
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Localization {
private static System.Resources.ResourceManager resourceMan;
private static global::System.Resources.ResourceManager resourceMan;
private static System.Globalization.CultureInfo resourceCulture;
private static global::System.Globalization.CultureInfo resourceCulture;
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Localization() {
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
internal static System.Resources.ResourceManager ResourceManager {
/// <summary>
/// Returns the cached ResourceManager instance used by this class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.Equals(null, resourceMan)) {
System.Resources.ResourceManager temp = new System.Resources.ResourceManager("Aaru.Helpers.Localization.Localization", typeof(Localization).Assembly);
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Aaru.Helpers.Localization.Localization", typeof(Localization).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
internal static System.Globalization.CultureInfo Culture {
/// <summary>
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
/// </summary>
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture {
get {
return resourceCulture;
}
@@ -45,12 +59,18 @@ namespace Aaru.Helpers {
}
}
/// <summary>
/// Looks up a localized string similar to Length of value array must not be more than length of destination.
/// </summary>
internal static string Length_of_value_array_must_not_be_more_than_length_of_destination {
get {
return ResourceManager.GetString("Length_of_value_array_must_not_be_more_than_length_of_destination", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Offset.
/// </summary>
internal static string Offset {
get {
return ResourceManager.GetString("Offset", resourceCulture);

View File

@@ -1,21 +1,25 @@
<root>
<!-- ReSharper disable MarkupTextTypo -->
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Offset" xml:space="preserve">
<!-- ReSharper disable MarkupTextTypo -->
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="Offset" xml:space="preserve">
<value>Compensación</value>
</data>
<data name="Length_of_value_array_must_not_be_more_than_length_of_destination" xml:space="preserve">
<data name="Length_of_value_array_must_not_be_more_than_length_of_destination" xml:space="preserve">
<value>La longitud de una colección no puede ser mayor que la longitud del destino</value>
</data>
</root>

View File

@@ -1,30 +1,32 @@
<?xml version="1.0" encoding="utf-8" ?>
<root>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="root"
xmlns="">
<xsd:element name="root" msdata:IsDataSet="true"></xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="Length_of_value_array_must_not_be_more_than_length_of_destination" xml:space="preserve">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
id="root"
xmlns="">
<xsd:element name="root" msdata:IsDataSet="true"></xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
</value>
</resheader>
<data name="Length_of_value_array_must_not_be_more_than_length_of_destination" xml:space="preserve">
<value>Length of value array must not be more than length of destination</value>
</data>
<data name="Offset" xml:space="preserve">
<data name="Offset" xml:space="preserve">
<value>Offset</value>
</data>
</root>

View File

@@ -250,91 +250,93 @@ 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>
/// <param name="str"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining), SuppressMessage("ReSharper", "InconsistentNaming")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
[SuppressMessage("ReSharper", "InconsistentNaming")]
public static object SwapStructureMembersEndian(object str)
{
Type t = str.GetType();
FieldInfo[] fieldInfo = t.GetFields();
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))
{
short x = (short)(fi.GetValue(str) ?? default(short));
fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF)));
var 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))
{
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)));
var 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))
{
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);
var 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);
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))
{
ushort x = (ushort)(fi.GetValue(str) ?? default(ushort));
fi.SetValue(str, (ushort)((x << 8) | (x >> 8)));
var 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))
{
uint x = (uint)(fi.GetValue(str) ?? default(uint));
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
fi.SetValue(str, (x << 16) | (x >> 16));
var 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))
{
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);
var 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;
fi.SetValue(str, x);
}
else if(fi.FieldType == typeof(float))
{
float flt = (float)(fi.GetValue(str) ?? default(float));
var flt = (float)(fi.GetValue(str) ?? default(float));
byte[] flt_b = BitConverter.GetBytes(flt);
fi.SetValue(str, BitConverter.ToSingle(new[]
{
flt_b[3], flt_b[2], flt_b[1], flt_b[0]
}, 0));
fi.SetValue(str, BitConverter.ToSingle(new[] { flt_b[3], flt_b[2], flt_b[1], flt_b[0] }, 0));
}
else if(fi.FieldType == typeof(double))
{
double dbl = (double)(fi.GetValue(str) ?? default(double));
var dbl = (double)(fi.GetValue(str) ?? default(double));
byte[] dbl_b = BitConverter.GetBytes(dbl);
fi.SetValue(str, BitConverter.ToDouble(new[]
{
dbl_b[7], dbl_b[6], dbl_b[5], dbl_b[4], dbl_b[3], dbl_b[2], dbl_b[1], dbl_b[0]
}, 0));
fi.SetValue(
str,
BitConverter.ToDouble(
new[] { dbl_b[7], dbl_b[6], dbl_b[5], dbl_b[4], dbl_b[3], dbl_b[2], dbl_b[1], dbl_b[0] }, 0));
}
else if(fi.FieldType == typeof(byte) ||
fi.FieldType == typeof(sbyte))
@@ -354,6 +356,7 @@ public static class Marshal
object strc = SwapStructureMembersEndian(obj);
fi.SetValue(str, strc);
}
}
return str;
}
@@ -368,6 +371,7 @@ public static class Marshal
FieldInfo[] fieldInfo = t.GetFields();
foreach(FieldInfo fi in fieldInfo)
{
if(fi.FieldType == typeof(short) ||
fi.FieldType == typeof(long) ||
fi.FieldType == typeof(ushort) ||
@@ -381,16 +385,16 @@ 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))
{
int x = (int)(fi.GetValue(str) ?? default(int));
fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
var 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))
{
uint x = (uint)(fi.GetValue(str) ?? default(uint));
fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
var x = (uint)(fi.GetValue(str) ?? default(uint));
fi.SetValue(str, (x & 0xffffu) << 16 | (x & 0xffff0000u) >> 16);
}
// TODO: Swap arrays
@@ -401,6 +405,7 @@ public static class Marshal
object strc = SwapStructureMembersEndianPdp(obj);
fi.SetValue(str, strc);
}
}
return str;
}
@@ -412,8 +417,8 @@ public static class Marshal
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] StructureToByteArrayLittleEndian<T>(T str) where T : struct
{
byte[] buf = new byte[SizeOf<T>()];
var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned);
var buf = new byte[SizeOf<T>()];
var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned);
System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false);
ptr.Free();
@@ -439,14 +444,14 @@ public static class Marshal
if(hex is null or "")
return -1;
int off = 0;
var off = 0;
if(hex[0] == '0' &&
(hex[1] == 'x' || hex[1] == 'X'))
off = 2;
outBuf = new byte[(hex.Length - off) / 2];
int count = 0;
var count = 0;
for(int i = off; i < hex.Length; i += 2)
{
@@ -456,11 +461,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);
@@ -470,11 +475,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;

View File

@@ -56,6 +56,7 @@ public sealed class MarshallingPropertiesAttribute : Attribute
/// <summary>c</summary>
public BitEndian Endian { get; }
/// <summary>Tells if the structure, or any nested structure, has any non-value type (e.g. arrays, strings, etc).</summary>
public bool HasReferences { get; set; }
}

View File

@@ -86,7 +86,7 @@ public static class PrintHex
sb.Append(str);
sb.Append(" ");
for(int i = 0; i < width; i++)
for(var i = 0; i < width; i++)
sb.AppendFormat(" {0:X2}", i);
if(color)
@@ -94,11 +94,11 @@ public static class PrintHex
sb.AppendLine();
int b = 0;
var b = 0;
string format = $"{{0:X{offsetLength}}}";
var format = $"{{0:X{offsetLength}}}";
for(int i = 0; i < rows; i++)
for(var 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(int j = 0; j < lastBytes; j++)
for(var j = 0; j < lastBytes; j++)
{
sb.AppendFormat(" {0:X2}", array[b]);
b++;
}
for(int j = 0; j < lastSpaces; j++)
for(var j = 0; j < lastSpaces; j++)
sb.Append(" ");
b -= lastBytes;
sb.Append(" ");
for(int j = 0; j < lastBytes; j++)
for(var j = 0; j < lastBytes; j++)
{
int v = array[b];
sb.Append(v is > 31 and < 127 or > 159 ? (char)v : '.');

View File

@@ -54,11 +54,12 @@ public static class StringHandlers
if(cString == null)
return null;
int len = 0;
var len = 0;
for(int i = start; i < cString.Length; i++)
{
if(cString[i] == 0)
{
if(twoBytes)
{
if(i + 1 < cString.Length &&
@@ -71,6 +72,7 @@ public static class StringHandlers
}
else
break;
}
len++;
}
@@ -78,7 +80,7 @@ public static class StringHandlers
if(twoBytes && len % 2 > 0)
len--;
byte[] dest = new byte[len];
var dest = new byte[len];
Array.Copy(cString, start, dest, 0, len);
return len == 0 ? "" : encoding.GetString(dest);
@@ -100,7 +102,7 @@ public static class StringHandlers
return null;
byte length = pascalString[start];
int len = 0;
var len = 0;
for(int i = start + 1; i < length + 1 && i < pascalString.Length; i++)
{
@@ -110,7 +112,7 @@ public static class StringHandlers
len++;
}
byte[] dest = new byte[len];
var dest = new byte[len];
Array.Copy(pascalString, start + 1, dest, 0, len);
return len == 0 ? "" : encoding.GetString(dest);
@@ -155,14 +157,14 @@ public static class StringHandlers
/// <param name="dstring">OSTA compressed unicode byte array.</param>
public static string DecompressUnicode(byte[] dstring)
{
byte compId = dstring[0];
string temp = "";
byte compId = dstring[0];
var temp = "";
if(compId != 8 &&
compId != 16)
return null;
for(int byteIndex = 1; byteIndex < dstring.Length;)
for(var byteIndex = 1; byteIndex < dstring.Length;)
{
ushort unicode;

View File

@@ -41,25 +41,25 @@ public static class Swapping
/// <param name="x">Little endian unsigned integer</param>
/// <returns>PDP unsigned integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint PDPFromLittleEndian(uint x) => ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16);
public static uint PDPFromLittleEndian(uint x) => (x & 0xffff) << 16 | (x & 0xffff0000) >> 16;
/// <summary>Gets the PDP endian equivalent of the given big endian unsigned integer</summary>
/// <param name="x">Big endian unsigned integer</param>
/// <returns>PDP unsigned integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint PDPFromBigEndian(uint x) => ((x & 0xff00ff) << 8) | ((x & 0xff00ff00) >> 8);
public static uint PDPFromBigEndian(uint x) => (x & 0xff00ff) << 8 | (x & 0xff00ff00) >> 8;
/// <summary>Swaps the endian of the specified unsigned short integer</summary>
/// <param name="x">Unsigned short integer</param>
/// <returns>Swapped unsigned short integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ushort Swap(ushort x) => (ushort)((x << 8) | (x >> 8));
public static ushort Swap(ushort x) => (ushort)(x << 8 | x >> 8);
/// <summary>Swaps the endian of the specified signed short integer</summary>
/// <param name="x">Signed short integer</param>
/// <returns>Swapped signed short integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static short Swap(short x) => (short)((x << 8) | ((x >> 8) & 0xFF));
public static short Swap(short x) => (short)(x << 8 | x >> 8 & 0xFF);
/// <summary>Swaps the endian of the specified unsigned integer</summary>
/// <param name="x">Unsigned integer</param>
@@ -67,9 +67,9 @@ public static class Swapping
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint Swap(uint x)
{
x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
x = x << 8 & 0xFF00FF00 | x >> 8 & 0xFF00FF;
return (x << 16) | (x >> 16);
return x << 16 | x >> 16;
}
/// <summary>Swaps the endian of the specified signed integer</summary>
@@ -78,9 +78,9 @@ public static class Swapping
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Swap(int x)
{
x = (int)(((x << 8) & 0xFF00FF00) | (((uint)x >> 8) & 0xFF00FF));
x = (int)(x << 8 & 0xFF00FF00 | (uint)x >> 8 & 0xFF00FF);
return (int)(((uint)x << 16) | (((uint)x >> 16) & 0xFFFF));
return (int)((uint)x << 16 | (uint)x >> 16 & 0xFFFF);
}
/// <summary>Swaps the endian of the specified unsigned long integer</summary>
@@ -89,9 +89,9 @@ public static class Swapping
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong Swap(ulong x)
{
x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32);
x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16);
x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8);
x = (x & 0x00000000FFFFFFFF) << 32 | (x & 0xFFFFFFFF00000000) >> 32;
x = (x & 0x0000FFFF0000FFFF) << 16 | (x & 0xFFFF0000FFFF0000) >> 16;
x = (x & 0x00FF00FF00FF00FF) << 8 | (x & 0xFF00FF00FF00FF00) >> 8;
return x;
}
@@ -102,9 +102,9 @@ public static class Swapping
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static long Swap(long x)
{
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);
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);
return x;
}