diff --git a/Aaru.Helpers.csproj b/Aaru.Helpers.csproj index 604834a5c..914137bcc 100644 --- a/Aaru.Helpers.csproj +++ b/Aaru.Helpers.csproj @@ -1,73 +1,73 @@  - - 2.0 - {F8BDF57B-1571-4CD0-84B3-B422088D359A} - Library - Aaru.Helpers - Aaru.Helpers - $(Version) - false - true - 6.0.0-alpha8 - Claunia.com - Copyright © 2011-2022 Natalia Portillo - Aaru Data Preservation Suite - Aaru.Helpers - $(Version) - net7.0 - 11 - Contains helpers used by the Aaru Data Preservation Suite. - https://github.com/aaru-dps/ - LGPL-2.1-only - https://github.com/aaru-dps/Aaru.Helpers - true - en-US - true - true - snupkg - Natalia Portillo <claunia@claunia.com> - true - true - - - CS1591;CS1574 - - - - - - - $(Version)+{chash:8} - true - true - - - - - - - - - - - - - - - - - - - - - - - LICENSE.LGPL - - - - - - + + 2.0 + {F8BDF57B-1571-4CD0-84B3-B422088D359A} + Library + Aaru.Helpers + Aaru.Helpers + $(Version) + false + true + 6.0.0-alpha8 + Claunia.com + Copyright © 2011-2022 Natalia Portillo + Aaru Data Preservation Suite + Aaru.Helpers + $(Version) + net7.0 + 11 + Contains helpers used by the Aaru Data Preservation Suite. + https://github.com/aaru-dps/ + LGPL-2.1-only + https://github.com/aaru-dps/Aaru.Helpers + true + en-US + true + true + snupkg + Natalia Portillo <claunia@claunia.com> + true + true + + + CS1591;CS1574 + + + + + + + $(Version)+{chash:8} + true + true + + + + + + + + + + + + + + + + + + + + + + + LICENSE.LGPL + + + + + + \ No newline at end of file diff --git a/ArrayFill.cs b/ArrayFill.cs index f4e440cbc..c3e9e1b0f 100644 --- a/ArrayFill.cs +++ b/ArrayFill.cs @@ -24,11 +24,11 @@ // Copyright(C) 2014 mykohsu // ****************************************************************************/ -namespace Aaru.Helpers; - using System; using System.Text; +namespace Aaru.Helpers; + public static partial class ArrayHelpers { /// Fills an array with the specified value diff --git a/ArrayIsEmpty.cs b/ArrayIsEmpty.cs index 7df593cf6..a8c062ad5 100644 --- a/ArrayIsEmpty.cs +++ b/ArrayIsEmpty.cs @@ -30,10 +30,10 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System.Linq; +namespace Aaru.Helpers; + /// Helper operations to work with arrays public static partial class ArrayHelpers { diff --git a/BigEndianBitConverter.cs b/BigEndianBitConverter.cs index 4c180a5af..f378fed18 100644 --- a/BigEndianBitConverter.cs +++ b/BigEndianBitConverter.cs @@ -30,11 +30,11 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System; using System.Linq; +namespace Aaru.Helpers; + /// /// 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. diff --git a/CHS.cs b/CHS.cs index ad9945014..aa58ff396 100644 --- a/CHS.cs +++ b/CHS.cs @@ -43,6 +43,6 @@ public static class CHS /// Number of sectors per track /// 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; } \ No newline at end of file diff --git a/DateHandlers.cs b/DateHandlers.cs index b01dd0ddd..9aecf7af6 100644 --- a/DateHandlers.cs +++ b/DateHandlers.cs @@ -30,12 +30,12 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System; using System.Text; using Aaru.Console; +namespace Aaru.Helpers; + /// Helper operations for timestamp management (date and time) public static class DateHandlers { @@ -88,7 +88,7 @@ public static class DateHandlers /// .NET DateTime 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 /// .NET DateTime 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 /// .NET DateTime 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; diff --git a/Extensions.cs b/Extensions.cs index c1b8479c0..cc413ea3c 100644 --- a/Extensions.cs +++ b/Extensions.cs @@ -30,10 +30,10 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System.IO; +namespace Aaru.Helpers; + public static class Extensions { /// @@ -58,7 +58,7 @@ public static class Extensions /// public static int EnsureRead(this Stream s, byte[] buffer, int offset, int count) { - var pos = 0; + int pos = 0; int read; do diff --git a/Marshal.cs b/Marshal.cs index 90b09f7e1..6bf697f1a 100644 --- a/Marshal.cs +++ b/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; + /// Provides methods to marshal binary data into C# structs public static class Marshal { @@ -250,15 +250,15 @@ public static class Marshal return ByteArrayToStructureLittleEndian(bytes); return properties.Endian switch - { - BitEndian.Little => properties.HasReferences ? ByteArrayToStructureLittleEndian(bytes) - : SpanToStructureLittleEndian(bytes), - BitEndian.Big => properties.HasReferences ? ByteArrayToStructureBigEndian(bytes) - : SpanToStructureBigEndian(bytes), - BitEndian.Pdp => properties.HasReferences ? ByteArrayToStructurePdpEndian(bytes) - : SpanToStructurePdpEndian(bytes), - _ => throw new ArgumentOutOfRangeException() - }; + { + BitEndian.Little => properties.HasReferences ? ByteArrayToStructureLittleEndian(bytes) + : SpanToStructureLittleEndian(bytes), + BitEndian.Big => properties.HasReferences ? ByteArrayToStructureBigEndian(bytes) + : SpanToStructureBigEndian(bytes), + BitEndian.Pdp => properties.HasReferences ? ByteArrayToStructurePdpEndian(bytes) + : SpanToStructurePdpEndian(bytes), + _ => throw new ArgumentOutOfRangeException() + }; } /// Swaps all members of a structure @@ -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 str) where T : struct { - var buf = new byte[SizeOf()]; - var ptr = GCHandle.Alloc(buf, GCHandleType.Pinned); + byte[] buf = new byte[SizeOf()]; + 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; diff --git a/MarshallingPropertiesAttribute.cs b/MarshallingPropertiesAttribute.cs index ac96c2592..bb9f7c35c 100644 --- a/MarshallingPropertiesAttribute.cs +++ b/MarshallingPropertiesAttribute.cs @@ -36,10 +36,10 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System; +namespace Aaru.Helpers; + /// /// Defines properties to help marshalling structs from binary data [AttributeUsage(AttributeTargets.Struct)] diff --git a/PrintHex.cs b/PrintHex.cs index 0d193e95a..448b3423c 100644 --- a/PrintHex.cs +++ b/PrintHex.cs @@ -30,11 +30,11 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System.Text; using Aaru.Console; +namespace Aaru.Helpers; + /// Helper operations to get hexadecimal representations of byte arrays 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 : '.'); diff --git a/StringHandlers.cs b/StringHandlers.cs index d00e1e089..2238a3e4b 100644 --- a/StringHandlers.cs +++ b/StringHandlers.cs @@ -30,11 +30,11 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System; using System.Text; +namespace Aaru.Helpers; + /// Helper operations to work with strings 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; diff --git a/Swapping.cs b/Swapping.cs index cf713265a..3bc76eefc 100644 --- a/Swapping.cs +++ b/Swapping.cs @@ -30,10 +30,10 @@ // Copyright © 2011-2022 Natalia Portillo // ****************************************************************************/ -namespace Aaru.Helpers; - using System.Runtime.CompilerServices; +namespace Aaru.Helpers; + /// Helper operations to work with swapping endians public static class Swapping {