General code cleanup and style refactor.

This commit is contained in:
2022-11-15 15:58:41 +00:00
parent eaea8b3332
commit cfe81b8905
12 changed files with 171 additions and 173 deletions

View File

@@ -43,23 +43,23 @@
<NrtShowRevision>true</NrtShowRevision> <NrtShowRevision>true</NrtShowRevision>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ArrayFill.cs" /> <Compile Include="ArrayFill.cs"/>
<Compile Include="BigEndianBitConverter.cs" /> <Compile Include="BigEndianBitConverter.cs"/>
<Compile Include="BitEndian.cs" /> <Compile Include="BitEndian.cs"/>
<Compile Include="DateHandlers.cs" /> <Compile Include="DateHandlers.cs"/>
<Compile Include="Extensions.cs"/> <Compile Include="Extensions.cs"/>
<Compile Include="Marshal.cs" /> <Compile Include="Marshal.cs"/>
<Compile Include="MarshallingPropertiesAttribute.cs" /> <Compile Include="MarshallingPropertiesAttribute.cs"/>
<Compile Include="PrintHex.cs" /> <Compile Include="PrintHex.cs"/>
<Compile Include="StringHandlers.cs" /> <Compile Include="StringHandlers.cs"/>
<Compile Include="Swapping.cs" /> <Compile Include="Swapping.cs"/>
<Compile Include="ArrayIsEmpty.cs" /> <Compile Include="ArrayIsEmpty.cs"/>
<Compile Include="CompareBytes.cs" /> <Compile Include="CompareBytes.cs"/>
<Compile Include="CountBits.cs" /> <Compile Include="CountBits.cs"/>
<Compile Include="CHS.cs" /> <Compile Include="CHS.cs"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Aaru.Console\Aaru.Console.csproj" /> <ProjectReference Include="..\Aaru.Console\Aaru.Console.csproj"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<EmbeddedResource Include="..\LICENSE.LGPL"> <EmbeddedResource Include="..\LICENSE.LGPL">
@@ -67,7 +67,7 @@
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.5" /> <PackageReference Include="System.Memory" Version="4.5.5"/>
<PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3" PrivateAssets="all" /> <PackageReference Include="Unclassified.NetRevisionTask" Version="0.4.3" PrivateAssets="all"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -24,11 +24,11 @@
// Copyright(C) 2014 mykohsu // Copyright(C) 2014 mykohsu
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System; using System;
using System.Text; using System.Text;
namespace Aaru.Helpers;
public static partial class ArrayHelpers public static partial class ArrayHelpers
{ {
/// <summary>Fills an array with the specified value</summary> /// <summary>Fills an array with the specified value</summary>

View File

@@ -30,10 +30,10 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System.Linq; using System.Linq;
namespace Aaru.Helpers;
/// <summary>Helper operations to work with arrays</summary> /// <summary>Helper operations to work with arrays</summary>
public static partial class ArrayHelpers public static partial class ArrayHelpers
{ {

View File

@@ -30,11 +30,11 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System; using System;
using System.Linq; using System.Linq;
namespace Aaru.Helpers;
/// <summary> /// <summary>
/// Converts base data types to an array of bytes, and an array of bytes to base data types. All info taken from /// 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. /// the meta data of System.BitConverter. This implementation allows for Endianness consideration.

4
CHS.cs
View File

@@ -43,6 +43,6 @@ public static class CHS
/// <param name="maxSector">Number of sectors per track</param> /// <param name="maxSector">Number of sectors per track</param>
/// <returns></returns> /// <returns></returns>
public static uint ToLBA(uint cyl, uint head, uint sector, uint maxHead, uint maxSector) => public static uint ToLBA(uint cyl, uint head, uint sector, uint maxHead, uint maxSector) =>
maxHead == 0 || maxSector == 0 ? (cyl * 16 + head) * 63 + sector - 1 maxHead == 0 || maxSector == 0 ? (((cyl * 16) + head) * 63) + sector - 1
: (cyl * maxHead + head) * maxSector + sector - 1; : (((cyl * maxHead) + head) * maxSector) + sector - 1;
} }

View File

@@ -30,12 +30,12 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System; using System;
using System.Text; using System.Text;
using Aaru.Console; using Aaru.Console;
namespace Aaru.Helpers;
/// <summary>Helper operations for timestamp management (date and time)</summary> /// <summary>Helper operations for timestamp management (date and time)</summary>
public static class DateHandlers public static class DateHandlers
{ {
@@ -88,7 +88,7 @@ public static class DateHandlers
/// <returns>.NET DateTime</returns> /// <returns>.NET DateTime</returns>
public static DateTime HighSierraToDateTime(byte[] vdDateTime) public static DateTime HighSierraToDateTime(byte[] vdDateTime)
{ {
var isoTime = new byte[17]; byte[] isoTime = new byte[17];
Array.Copy(vdDateTime, 0, isoTime, 0, 16); Array.Copy(vdDateTime, 0, isoTime, 0, 16);
return Iso9660ToDateTime(isoTime); return Iso9660ToDateTime(isoTime);
@@ -100,8 +100,8 @@ public static class DateHandlers
/// <returns>.NET DateTime</returns> /// <returns>.NET DateTime</returns>
public static DateTime Iso9660ToDateTime(byte[] vdDateTime) public static DateTime Iso9660ToDateTime(byte[] vdDateTime)
{ {
var twoCharValue = new byte[2]; byte[] twoCharValue = new byte[2];
var fourCharValue = new byte[4]; byte[] fourCharValue = new byte[4];
fourCharValue[0] = vdDateTime[0]; fourCharValue[0] = vdDateTime[0];
fourCharValue[1] = vdDateTime[1]; fourCharValue[1] = vdDateTime[1];
@@ -172,7 +172,7 @@ public static class DateHandlers
"decodedDT = new DateTime({0}, {1}, {2}, {3}, {4}, {5}, {6}, DateTimeKind.Unspecified);", "decodedDT = new DateTime({0}, {1}, {2}, {3}, {4}, {5}, {6}, DateTimeKind.Unspecified);",
year, month, day, hour, minute, second, hundredths * 10); 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); var decodedDt = new DateTime(year, month, day, hour, minute, second, hundredths * 10, DateTimeKind.Utc);
@@ -257,7 +257,7 @@ public static class DateHandlers
/// <returns>.NET DateTime</returns> /// <returns>.NET DateTime</returns>
public static DateTime CpmToDateTime(byte[] timestamp) public static DateTime CpmToDateTime(byte[] timestamp)
{ {
var days = BitConverter.ToUInt16(timestamp, 0); ushort days = BitConverter.ToUInt16(timestamp, 0);
int hours = timestamp[2]; int hours = timestamp[2];
int minutes = timestamp[3]; int minutes = timestamp[3];
@@ -284,14 +284,14 @@ public static class DateHandlers
byte minute, byte second, byte centiseconds, byte hundredsOfMicroseconds, byte minute, byte second, byte centiseconds, byte hundredsOfMicroseconds,
byte microseconds) 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) if(specification == 0)
return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc).AddTicks(ticks); return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc).AddTicks(ticks);
var preOffset = (ushort)(typeAndTimeZone & 0xFFF); ushort preOffset = (ushort)(typeAndTimeZone & 0xFFF);
short offset; short offset;
if((preOffset & 0x800) == 0x800) if((preOffset & 0x800) == 0x800)
@@ -324,7 +324,7 @@ public static class DateHandlers
public static DateTime Os9ToDateTime(byte[] date) public static DateTime Os9ToDateTime(byte[] date)
{ {
if(date == null || if(date == null ||
date.Length != 3 && date.Length != 5) (date.Length != 3 && date.Length != 5))
return DateTime.MinValue; return DateTime.MinValue;
DateTime os9Date; DateTime os9Date;
@@ -361,12 +361,12 @@ public static class DateHandlers
{ {
try try
{ {
int iyear = (year >> 4) * 10 + (year & 0xF); int iyear = ((year >> 4) * 10) + (year & 0xF);
int imonth = (month >> 4) * 10 + (month & 0xF); int imonth = ((month >> 4) * 10) + (month & 0xF);
int iday = (day >> 4) * 10 + (day & 0xF); int iday = ((day >> 4) * 10) + (day & 0xF);
int iminute = (minute >> 4) * 10 + (minute & 0xF); int iminute = ((minute >> 4) * 10) + (minute & 0xF);
int ihour = (hour >> 4) * 10 + (hour & 0xF); int ihour = ((hour >> 4) * 10) + (hour & 0xF);
int isecond = (second >> 4) * 10 + (second & 0xF); int isecond = ((second >> 4) * 10) + (second & 0xF);
if(iyear >= 70) if(iyear >= 70)
iyear += 1900; iyear += 1900;

View File

@@ -30,10 +30,10 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System.IO; using System.IO;
namespace Aaru.Helpers;
public static class Extensions public static class Extensions
{ {
/// <summary> /// <summary>
@@ -58,7 +58,7 @@ public static class Extensions
/// </returns> /// </returns>
public static int EnsureRead(this Stream s, byte[] buffer, int offset, int count) public static int EnsureRead(this Stream s, byte[] buffer, int offset, int count)
{ {
var pos = 0; int pos = 0;
int read; int read;
do do

View File

@@ -30,14 +30,14 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Aaru.Helpers;
/// <summary>Provides methods to marshal binary data into C# structs</summary> /// <summary>Provides methods to marshal binary data into C# structs</summary>
public static class Marshal public static class Marshal
{ {
@@ -272,22 +272,22 @@ public static class Marshal
foreach(FieldInfo fi in fieldInfo) foreach(FieldInfo fi in fieldInfo)
if(fi.FieldType == typeof(short) || 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))); fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF)));
} }
else if(fi.FieldType == typeof(int) || 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)); x = (int)(((x << 8) & 0xFF00FF00) | (((uint)x >> 8) & 0xFF00FF));
fi.SetValue(str, (int)(((uint)x << 16) | (((uint)x >> 16) & 0xFFFF))); fi.SetValue(str, (int)(((uint)x << 16) | (((uint)x >> 16) & 0xFFFF)));
} }
else if(fi.FieldType == typeof(long) || 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 & 0x00000000FFFFFFFF) << 32) | (long)(((ulong)x & 0xFFFFFFFF00000000) >> 32);
x = ((x & 0x0000FFFF0000FFFF) << 16) | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16); x = ((x & 0x0000FFFF0000FFFF) << 16) | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16);
x = ((x & 0x00FF00FF00FF00FF) << 8) | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8); x = ((x & 0x00FF00FF00FF00FF) << 8) | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8);
@@ -295,22 +295,22 @@ public static class Marshal
fi.SetValue(str, x); fi.SetValue(str, x);
} }
else if(fi.FieldType == typeof(ushort) || 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))); fi.SetValue(str, (ushort)((x << 8) | (x >> 8)));
} }
else if(fi.FieldType == typeof(uint) || 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); x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF);
fi.SetValue(str, (x << 16) | (x >> 16)); fi.SetValue(str, (x << 16) | (x >> 16));
} }
else if(fi.FieldType == typeof(ulong) || 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 & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32);
x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16); x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16);
x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8);
@@ -318,7 +318,7 @@ public static class Marshal
} }
else if(fi.FieldType == typeof(float)) 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); byte[] flt_b = BitConverter.GetBytes(flt);
fi.SetValue(str, BitConverter.ToSingle(new[] fi.SetValue(str, BitConverter.ToSingle(new[]
@@ -328,7 +328,7 @@ public static class Marshal
} }
else if(fi.FieldType == typeof(double)) 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); byte[] dbl_b = BitConverter.GetBytes(dbl);
fi.SetValue(str, BitConverter.ToDouble(new[] fi.SetValue(str, BitConverter.ToDouble(new[]
@@ -348,8 +348,7 @@ public static class Marshal
// TODO: Swap arrays // TODO: Swap arrays
else if(fi.FieldType.IsValueType && else if(fi.FieldType.IsValueType &&
!fi.FieldType.IsEnum && fi.FieldType is { IsEnum: false, IsArray: false })
!fi.FieldType.IsArray)
{ {
object obj = fi.GetValue(str); object obj = fi.GetValue(str);
object strc = SwapStructureMembersEndian(obj); object strc = SwapStructureMembersEndian(obj);
@@ -382,22 +381,21 @@ public static class Marshal
// Do nothing // Do nothing
} }
else if(fi.FieldType == typeof(int) || 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)); fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
} }
else if(fi.FieldType == typeof(uint) || 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)); fi.SetValue(str, ((x & 0xffffu) << 16) | ((x & 0xffff0000u) >> 16));
} }
// TODO: Swap arrays // TODO: Swap arrays
else if(fi.FieldType.IsValueType && else if(fi.FieldType.IsValueType &&
!fi.FieldType.IsEnum && fi.FieldType is { IsEnum: false, IsArray: false })
!fi.FieldType.IsArray)
{ {
object obj = fi.GetValue(str); object obj = fi.GetValue(str);
object strc = SwapStructureMembersEndianPdp(obj); object strc = SwapStructureMembersEndianPdp(obj);
@@ -414,7 +412,7 @@ public static class Marshal
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] StructureToByteArrayLittleEndian<T>(T str) where T : struct public static byte[] StructureToByteArrayLittleEndian<T>(T str) where T : struct
{ {
var buf = new byte[SizeOf<T>()]; byte[] buf = new byte[SizeOf<T>()];
var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned); var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned);
System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false); System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false);
ptr.Free(); ptr.Free();
@@ -441,14 +439,14 @@ public static class Marshal
if(hex is null or "") if(hex is null or "")
return -1; return -1;
var off = 0; int off = 0;
if(hex[0] == '0' && if(hex[0] == '0' &&
(hex[1] == 'x' || hex[1] == 'X')) (hex[1] == 'x' || hex[1] == 'X'))
off = 2; off = 2;
outBuf = new byte[(hex.Length - off) / 2]; outBuf = new byte[(hex.Length - off) / 2];
var count = 0; int count = 0;
for(int i = off; i < hex.Length; i += 2) for(int i = off; i < hex.Length; i += 2)
{ {

View File

@@ -36,10 +36,10 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System; using System;
namespace Aaru.Helpers;
/// <inheritdoc /> /// <inheritdoc />
/// <summary>Defines properties to help marshalling structs from binary data</summary> /// <summary>Defines properties to help marshalling structs from binary data</summary>
[AttributeUsage(AttributeTargets.Struct)] [AttributeUsage(AttributeTargets.Struct)]

View File

@@ -30,11 +30,11 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System.Text; using System.Text;
using Aaru.Console; using Aaru.Console;
namespace Aaru.Helpers;
/// <summary>Helper operations to get hexadecimal representations of byte arrays</summary> /// <summary>Helper operations to get hexadecimal representations of byte arrays</summary>
public static class PrintHex public static class PrintHex
{ {
@@ -56,7 +56,7 @@ public static class PrintHex
// TODO: Color list // TODO: Color list
// TODO: Allow to change width // TODO: Allow to change width
var str = "Offset"; string str = "Offset";
int rows = array.Length / width; int rows = array.Length / width;
int last = array.Length % width; int last = array.Length % width;
int offsetLength = $"{array.Length:X}".Length; int offsetLength = $"{array.Length:X}".Length;
@@ -86,7 +86,7 @@ public static class PrintHex
sb.Append(str); sb.Append(str);
sb.Append(" "); sb.Append(" ");
for(var i = 0; i < width; i++) for(int i = 0; i < width; i++)
sb.AppendFormat(" {0:X2}", i); sb.AppendFormat(" {0:X2}", i);
if(color) if(color)
@@ -94,11 +94,11 @@ public static class PrintHex
sb.AppendLine(); sb.AppendLine();
var b = 0; int b = 0;
string format = $"{{0:X{offsetLength}}}"; string format = $"{{0:X{offsetLength}}}";
for(var i = 0; i < rows; i++) for(int i = 0; i < rows; i++)
{ {
if(color) if(color)
sb.Append("\u001b[36m"); sb.Append("\u001b[36m");
@@ -112,19 +112,19 @@ public static class PrintHex
int lastBytes = i == rows - 1 ? last : width; int lastBytes = i == rows - 1 ? last : width;
int lastSpaces = width - lastBytes; int lastSpaces = width - lastBytes;
for(var j = 0; j < lastBytes; j++) for(int j = 0; j < lastBytes; j++)
{ {
sb.AppendFormat(" {0:X2}", array[b]); sb.AppendFormat(" {0:X2}", array[b]);
b++; b++;
} }
for(var j = 0; j < lastSpaces; j++) for(int j = 0; j < lastSpaces; j++)
sb.Append(" "); sb.Append(" ");
b -= lastBytes; b -= lastBytes;
sb.Append(" "); sb.Append(" ");
for(var j = 0; j < lastBytes; j++) for(int j = 0; j < lastBytes; j++)
{ {
int v = array[b]; int v = array[b];
sb.Append(v is > 31 and < 127 or > 159 ? (char)v : '.'); sb.Append(v is > 31 and < 127 or > 159 ? (char)v : '.');

View File

@@ -30,11 +30,11 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System; using System;
using System.Text; using System.Text;
namespace Aaru.Helpers;
/// <summary>Helper operations to work with strings</summary> /// <summary>Helper operations to work with strings</summary>
public static class StringHandlers public static class StringHandlers
{ {
@@ -54,7 +54,7 @@ public static class StringHandlers
if(cString == null) if(cString == null)
return null; return null;
var len = 0; int len = 0;
for(int i = start; i < cString.Length; i++) for(int i = start; i < cString.Length; i++)
{ {
@@ -78,7 +78,7 @@ public static class StringHandlers
if(twoBytes && len % 2 > 0) if(twoBytes && len % 2 > 0)
len--; len--;
var dest = new byte[len]; byte[] dest = new byte[len];
Array.Copy(cString, start, dest, 0, len); Array.Copy(cString, start, dest, 0, len);
return len == 0 ? "" : encoding.GetString(dest); return len == 0 ? "" : encoding.GetString(dest);
@@ -100,7 +100,7 @@ public static class StringHandlers
return null; return null;
byte length = pascalString[start]; byte length = pascalString[start];
var len = 0; int len = 0;
for(int i = start + 1; i < length + 1 && i < pascalString.Length; i++) for(int i = start + 1; i < length + 1 && i < pascalString.Length; i++)
{ {
@@ -110,7 +110,7 @@ public static class StringHandlers
len++; len++;
} }
var dest = new byte[len]; byte[] dest = new byte[len];
Array.Copy(pascalString, start + 1, dest, 0, len); Array.Copy(pascalString, start + 1, dest, 0, len);
return len == 0 ? "" : encoding.GetString(dest); return len == 0 ? "" : encoding.GetString(dest);
@@ -156,13 +156,13 @@ public static class StringHandlers
public static string DecompressUnicode(byte[] dstring) public static string DecompressUnicode(byte[] dstring)
{ {
byte compId = dstring[0]; byte compId = dstring[0];
var temp = ""; string temp = "";
if(compId != 8 && if(compId != 8 &&
compId != 16) compId != 16)
return null; return null;
for(var byteIndex = 1; byteIndex < dstring.Length;) for(int byteIndex = 1; byteIndex < dstring.Length;)
{ {
ushort unicode; ushort unicode;

View File

@@ -30,10 +30,10 @@
// Copyright © 2011-2022 Natalia Portillo // Copyright © 2011-2022 Natalia Portillo
// ****************************************************************************/ // ****************************************************************************/
namespace Aaru.Helpers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Aaru.Helpers;
/// <summary>Helper operations to work with swapping endians</summary> /// <summary>Helper operations to work with swapping endians</summary>
public static class Swapping public static class Swapping
{ {