From a7d7c673a1dd992e490d7531d23bea717de1069d Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 25 Nov 2019 00:54:39 +0000 Subject: [PATCH] Code reformat. --- ArrayFill.cs | 28 ++-- ArrayIsEmpty.cs | 19 +-- BigEndianBitConverter.cs | 111 +++++-------- BitEndian.cs | 3 +- CHS.cs | 9 +- CompareBytes.cs | 11 +- CountBits.cs | 7 +- DateHandlers.cs | 186 +++++++++++---------- Marshal.cs | 268 +++++++++++++++--------------- MarshallingPropertiesAttribute.cs | 16 +- PrintHex.cs | 12 +- StringHandlers.cs | 70 ++++---- Swapping.cs | 8 +- 13 files changed, 356 insertions(+), 392 deletions(-) diff --git a/ArrayFill.cs b/ArrayFill.cs index af29d5b..7435ce9 100644 --- a/ArrayFill.cs +++ b/ArrayFill.cs @@ -31,27 +31,23 @@ namespace DiscImageChef { public static partial class ArrayHelpers { - /// - /// Fills an array with the specified value - /// + /// Fills an array with the specified value /// Array /// Value /// Array type - public static void ArrayFill(T[] destinationArray, T value) + public static void ArrayFill(T[] destinationArray, T value) => ArrayFill(destinationArray, new[] { - // if called with a single value, wrap the value in an array and call the main function - ArrayFill(destinationArray, new[] {value}); - } + value + }); - /// - /// Fills an array with the contents of the specified array - /// + /// Fills an array with the contents of the specified array /// Array /// Value /// Array type public static void ArrayFill(T[] destinationArray, T[] value) { - if(destinationArray == null) throw new ArgumentNullException(nameof(destinationArray)); + if(destinationArray == null) + throw new ArgumentNullException(nameof(destinationArray)); if(value.Length > destinationArray.Length) throw new ArgumentException("Length of value array must not be more than length of destination"); @@ -68,16 +64,16 @@ namespace DiscImageChef Array.Copy(destinationArray, 0, destinationArray, copyLength, destinationArray.Length - copyLength); } - /// - /// Converts a byte array to its hexadecimal representation - /// + /// Converts a byte array to its hexadecimal representation /// Byte array /// true to use uppercase /// public static string ByteArrayToHex(byte[] array, bool upper = false) { - StringBuilder sb = new StringBuilder(); - for(long i = 0; i < array.LongLength; i++) sb.AppendFormat("{0:x2}", array[i]); + var sb = new StringBuilder(); + + for(long i = 0; i < array.LongLength; i++) + sb.AppendFormat("{0:x2}", array[i]); return upper ? sb.ToString().ToUpper() : sb.ToString(); } diff --git a/ArrayIsEmpty.cs b/ArrayIsEmpty.cs index 51ffd7a..5d2ed6d 100644 --- a/ArrayIsEmpty.cs +++ b/ArrayIsEmpty.cs @@ -36,24 +36,15 @@ namespace DiscImageChef { public static partial class ArrayHelpers { - /// - /// Checks if an array is null, filled with the NULL byte (0x00) or ASCII whitespace (0x20) - /// + /// Checks if an array is null, filled with the NULL byte (0x00) or ASCII whitespace (0x20) /// Array /// True if null or whitespace - public static bool ArrayIsNullOrWhiteSpace(byte[] array) - { - return array == null || array.All(b => b == 0x00 || b == 0x20); - } + public static bool ArrayIsNullOrWhiteSpace(byte[] array) => + array == null || array.All(b => b == 0x00 || b == 0x20); - /// - /// Checks if an array is null or filled with the NULL byte (0x00) - /// + /// Checks if an array is null or filled with the NULL byte (0x00) /// Array /// True if null - public static bool ArrayIsNullOrEmpty(byte[] array) - { - return array == null || array.All(b => b == 0x00); - } + public static bool ArrayIsNullOrEmpty(byte[] array) => array == null || array.All(b => b == 0x00); } } \ No newline at end of file diff --git a/BigEndianBitConverter.cs b/BigEndianBitConverter.cs index 317da48..c71f188 100644 --- a/BigEndianBitConverter.cs +++ b/BigEndianBitConverter.cs @@ -36,101 +36,73 @@ using System.Linq; namespace DiscImageChef { /// - /// 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. + /// 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. /// public static class BigEndianBitConverter { - /// - /// Converts the specified double-precision floating point number to a 64-bit signed integer. - /// + /// Converts the specified double-precision floating point number to a 64-bit signed integer. /// The number to convert. /// A 64-bit signed integer whose value is equivalent to value. /// It is not currently implemented public static long DoubleToInt64Bits(double value) => throw new NotImplementedException(); - /// - /// Returns the specified Boolean value as an array of bytes. - /// + /// Returns the specified Boolean value as an array of bytes. /// A Boolean value. /// An array of bytes with length 1. public static byte[] GetBytes(bool value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified Unicode character value as an array of bytes. - /// + /// Returns the specified Unicode character value as an array of bytes. /// A character to convert. /// An array of bytes with length 2. public static byte[] GetBytes(char value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified double-precision floating point value as an array of bytes. - /// + /// Returns the specified double-precision floating point value as an array of bytes. /// The number to convert. /// An array of bytes with length 8. public static byte[] GetBytes(double value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified single-precision floating point value as an array of bytes. - /// + /// Returns the specified single-precision floating point value as an array of bytes. /// The number to convert. /// An array of bytes with length 4. public static byte[] GetBytes(float value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified 32-bit signed integer value as an array of bytes. - /// + /// Returns the specified 32-bit signed integer value as an array of bytes. /// The number to convert. /// An array of bytes with length 4. public static byte[] GetBytes(int value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified 64-bit signed integer value as an array of bytes. - /// + /// Returns the specified 64-bit signed integer value as an array of bytes. /// The number to convert. /// An array of bytes with length 8. public static byte[] GetBytes(long value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified 16-bit signed integer value as an array of bytes. - /// + /// Returns the specified 16-bit signed integer value as an array of bytes. /// The number to convert. /// An array of bytes with length 2. public static byte[] GetBytes(short value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified 32-bit unsigned integer value as an array of bytes. - /// + /// Returns the specified 32-bit unsigned integer value as an array of bytes. /// The number to convert. /// An array of bytes with length 4. public static byte[] GetBytes(uint value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified 64-bit unsigned integer value as an array of bytes. - /// + /// Returns the specified 64-bit unsigned integer value as an array of bytes. /// The number to convert. /// An array of bytes with length 8. public static byte[] GetBytes(ulong value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Returns the specified 16-bit unsigned integer value as an array of bytes. - /// + /// Returns the specified 16-bit unsigned integer value as an array of bytes. /// The number to convert. /// An array of bytes with length 2. public static byte[] GetBytes(ushort value) => BitConverter.GetBytes(value).Reverse().ToArray(); - /// - /// Converts the specified 64-bit signed integer to a double-precision floating point number. - /// + /// Converts the specified 64-bit signed integer to a double-precision floating point number. /// The number to convert. /// A double-precision floating point number whose value is equivalent to value. public static double Int64BitsToDouble(long value) => throw new NotImplementedException(); - /// - /// Returns a Boolean value converted from one byte at a specified position in a byte array. - /// + /// Returns a Boolean value converted from one byte at a specified position in a byte array. /// An array of bytes. /// The starting position within value. /// true if the byte at in value is nonzero; otherwise, false. @@ -141,9 +113,7 @@ namespace DiscImageChef /// public static bool ToBoolean(byte[] value, int startIndex) => throw new NotImplementedException(); - /// - /// Returns a Unicode character converted from two bytes at a specified position in a byte array. - /// + /// Returns a Unicode character converted from two bytes at a specified position in a byte array. /// An array. /// The starting position within value. /// A character formed by two bytes beginning at . @@ -173,9 +143,7 @@ namespace DiscImageChef /// public static double ToDouble(byte[] value, int startIndex) => throw new NotImplementedException(); - /// - /// Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array. - /// + /// Returns a 16-bit signed integer converted from two bytes at a specified position in a byte array. /// An array of bytes. /// The starting position within value. /// A 16-bit signed integer formed by two bytes beginning at . @@ -188,9 +156,7 @@ namespace DiscImageChef public static short ToInt16(byte[] value, int startIndex) => BitConverter.ToInt16(value.Reverse().ToArray(), value.Length - sizeof(short) - startIndex); - /// - /// Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array. - /// + /// Returns a 32-bit signed integer converted from four bytes at a specified position in a byte array. /// An array of bytes. /// The starting position within value. /// A 32-bit signed integer formed by four bytes beginning at . @@ -206,9 +172,7 @@ namespace DiscImageChef public static int ToInt32(byte[] value, int startIndex) => BitConverter.ToInt32(value.Reverse().ToArray(), value.Length - sizeof(int) - startIndex); - /// - /// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array. - /// + /// Returns a 64-bit signed integer converted from eight bytes at a specified position in a byte array. /// An array of bytes. /// The starting position within value. /// A 64-bit signed integer formed by eight bytes beginning at . @@ -256,8 +220,8 @@ namespace DiscImageChef public static string ToString(byte[] value) => BitConverter.ToString(value.Reverse().ToArray()); /// - /// Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal string - /// representation. + /// Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal + /// string representation. /// /// An array of bytes. /// The starting position within value. @@ -274,8 +238,8 @@ namespace DiscImageChef BitConverter.ToString(value.Reverse().ToArray(), startIndex); /// - /// Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal string - /// representation. + /// Converts the numeric value of each element of a specified subarray of bytes to its equivalent hexadecimal + /// string representation. /// /// An array of bytes. /// The starting position within value. @@ -296,9 +260,7 @@ namespace DiscImageChef public static string ToString(byte[] value, int startIndex, int length) => BitConverter.ToString(value.Reverse().ToArray(), startIndex, length); - /// - /// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array. - /// + /// Returns a 16-bit unsigned integer converted from two bytes at a specified position in a byte array. /// The array of bytes. /// The starting position within value. /// A 16-bit unsigned integer formed by two bytes beginning at startIndex. @@ -311,9 +273,7 @@ namespace DiscImageChef public static ushort ToUInt16(byte[] value, int startIndex) => BitConverter.ToUInt16(value.Reverse().ToArray(), value.Length - sizeof(ushort) - startIndex); - /// - /// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array. - /// + /// Returns a 32-bit unsigned integer converted from four bytes at a specified position in a byte array. /// An array of bytes. /// The starting position within value. /// A 32-bit unsigned integer formed by four bytes beginning at startIndex. @@ -329,9 +289,7 @@ namespace DiscImageChef public static uint ToUInt32(byte[] value, int startIndex) => BitConverter.ToUInt32(value.Reverse().ToArray(), value.Length - sizeof(uint) - startIndex); - /// - /// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array. - /// + /// Returns a 64-bit unsigned integer converted from eight bytes at a specified position in a byte array. /// An array of bytes. /// The starting position within value. /// A 64-bit unsigned integer formed by the eight bytes beginning at startIndex. @@ -347,11 +305,16 @@ namespace DiscImageChef public static ulong ToUInt64(byte[] value, int startIndex) => BitConverter.ToUInt64(value.Reverse().ToArray(), value.Length - sizeof(ulong) - startIndex); - public static Guid ToGuid(byte[] value, int startIndex) => - new Guid(ToUInt32(value, 0 + startIndex), ToUInt16(value, 4 + startIndex), - ToUInt16(value, 6 + startIndex), - value[8 + startIndex + 0], value[8 + startIndex + 1], value[8 + startIndex + 2], - value[8 + startIndex + 3], value[8 + startIndex + 5], value[8 + startIndex + 5], - value[8 + startIndex + 6], value[8 + startIndex + 7]); + public static Guid ToGuid(byte[] value, int startIndex) => new Guid(ToUInt32(value, 0 + startIndex), + ToUInt16(value, 4 + startIndex), + ToUInt16(value, 6 + startIndex), + value[8 + startIndex + 0], + value[8 + startIndex + 1], + value[8 + startIndex + 2], + value[8 + startIndex + 3], + value[8 + startIndex + 5], + value[8 + startIndex + 5], + value[8 + startIndex + 6], + value[8 + startIndex + 7]); } } \ No newline at end of file diff --git a/BitEndian.cs b/BitEndian.cs index af0324e..c6e7aaf 100644 --- a/BitEndian.cs +++ b/BitEndian.cs @@ -42,8 +42,7 @@ namespace DiscImageChef.Helpers public enum BitEndian { /// Little-endian, or least significant bit - Little, - /// Big-endian, or most significant bit + Little, /// Big-endian, or most significant bit Big, /// PDP-11 endian, little endian except for 32-bit integers where the 16 halves are swapped between them Pdp diff --git a/CHS.cs b/CHS.cs index 7d933bd..ef1e299 100644 --- a/CHS.cs +++ b/CHS.cs @@ -34,9 +34,7 @@ namespace DiscImageChef.Helpers { public static class CHS { - /// - /// Converts a CHS position to a LBA one - /// + /// Converts a CHS position to a LBA one /// Cylinder /// Head /// Sector @@ -44,8 +42,7 @@ namespace DiscImageChef.Helpers /// 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/CompareBytes.cs b/CompareBytes.cs index 8101b2a..c897025 100644 --- a/CompareBytes.cs +++ b/CompareBytes.cs @@ -34,20 +34,19 @@ namespace DiscImageChef { public static partial class ArrayHelpers { - /// - /// Compares two byte arrays - /// + /// Compares two byte arrays /// true if they are different in any way /// true if they have the same size /// Left array /// Right array public static void CompareBytes(out bool different, out bool sameSize, byte[] compareArray1, - byte[] compareArray2) + byte[] compareArray2) { different = false; sameSize = true; long leastBytes; + if(compareArray1.LongLength < compareArray2.LongLength) { sameSize = false; @@ -58,12 +57,14 @@ namespace DiscImageChef sameSize = false; leastBytes = compareArray2.LongLength; } - else leastBytes = compareArray1.LongLength; + else + leastBytes = compareArray1.LongLength; for(long i = 0; i < leastBytes; i++) if(compareArray1[i] != compareArray2[i]) { different = true; + return; } } diff --git a/CountBits.cs b/CountBits.cs index f7fe42e..f85254b 100644 --- a/CountBits.cs +++ b/CountBits.cs @@ -34,16 +34,15 @@ namespace DiscImageChef.Helpers { public static class CountBits { - /// - /// Counts the number of bits set to true in a number - /// + /// Counts the number of bits set to true in a number /// Number /// Bits set to true public static int Count(uint number) { number = 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); } } } \ No newline at end of file diff --git a/DateHandlers.cs b/DateHandlers.cs index 5ee2db9..e0ed690 100644 --- a/DateHandlers.cs +++ b/DateHandlers.cs @@ -41,79 +41,60 @@ namespace DiscImageChef static readonly DateTime LisaEpoch = new DateTime(1901, 1, 1, 0, 0, 0); static readonly DateTime MacEpoch = new DateTime(1904, 1, 1, 0, 0, 0); static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0); - /// - /// Day 0 of Julian Date system - /// + /// Day 0 of Julian Date system static readonly DateTime JulianEpoch = new DateTime(1858, 11, 17, 0, 0, 0); static readonly DateTime AmigaEpoch = new DateTime(1978, 1, 1, 0, 0, 0); - /// - /// Converts a Macintosh timestamp to a .NET DateTime - /// + /// Converts a Macintosh timestamp to a .NET DateTime /// Macintosh timestamp (seconds since 1st Jan. 1904) /// .NET DateTime public static DateTime MacToDateTime(ulong macTimeStamp) => MacEpoch.AddTicks((long)(macTimeStamp * 10000000)); - /// - /// Converts a Lisa timestamp to a .NET DateTime - /// + /// Converts a Lisa timestamp to a .NET DateTime /// Lisa timestamp (seconds since 1st Jan. 1901) /// .NET DateTime public static DateTime LisaToDateTime(uint lisaTimeStamp) => LisaEpoch.AddSeconds(lisaTimeStamp); - /// - /// Converts a UNIX timestamp to a .NET DateTime - /// + /// Converts a UNIX timestamp to a .NET DateTime /// UNIX timestamp (seconds since 1st Jan. 1970) /// .NET DateTime public static DateTime UnixToDateTime(int unixTimeStamp) => UnixEpoch.AddSeconds(unixTimeStamp); - /// - /// Converts a UNIX timestamp to a .NET DateTime - /// + /// Converts a UNIX timestamp to a .NET DateTime /// UNIX timestamp (seconds since 1st Jan. 1970) /// .NET DateTime public static DateTime UnixToDateTime(long unixTimeStamp) => UnixEpoch.AddSeconds(unixTimeStamp); - /// - /// Converts a UNIX timestamp to a .NET DateTime - /// + /// Converts a UNIX timestamp to a .NET DateTime /// UNIX timestamp (seconds since 1st Jan. 1970) /// .NET DateTime public static DateTime UnixUnsignedToDateTime(uint unixTimeStamp) => UnixEpoch.AddSeconds(unixTimeStamp); - /// - /// Converts a UNIX timestamp to a .NET DateTime - /// + /// Converts a UNIX timestamp to a .NET DateTime /// Seconds since 1st Jan. 1970) /// Nanoseconds /// .NET DateTime public static DateTime UnixUnsignedToDateTime(uint seconds, uint nanoseconds) => UnixEpoch.AddSeconds(seconds).AddTicks((long)nanoseconds / 100); - /// - /// Converts a UNIX timestamp to a .NET DateTime - /// + /// Converts a UNIX timestamp to a .NET DateTime /// UNIX timestamp (seconds since 1st Jan. 1970) /// .NET DateTime public static DateTime UnixUnsignedToDateTime(ulong unixTimeStamp) => UnixEpoch.AddSeconds(unixTimeStamp); - /// - /// Converts a High Sierra Format timestamp to a .NET DateTime - /// + /// Converts a High Sierra Format timestamp to a .NET DateTime /// High Sierra Format timestamp /// .NET DateTime public static DateTime HighSierraToDateTime(byte[] vdDateTime) { byte[] isotime = new byte[17]; Array.Copy(vdDateTime, 0, isotime, 0, 16); + return Iso9660ToDateTime(isotime); } // TODO: Timezone - /// - /// Converts an ISO9660 timestamp to a .NET DateTime - /// + /// Converts an ISO9660 timestamp to a .NET DateTime /// ISO9660 timestamp /// .NET DateTime public static DateTime Iso9660ToDateTime(byte[] vdDateTime) @@ -125,71 +106,89 @@ namespace DiscImageChef fourcharvalue[1] = vdDateTime[1]; fourcharvalue[2] = vdDateTime[2]; fourcharvalue[3] = vdDateTime[3]; + DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "year = \"{0}\"", StringHandlers.CToString(fourcharvalue, Encoding.ASCII)); - if(!int.TryParse(StringHandlers.CToString(fourcharvalue, Encoding.ASCII), out int year)) year = 0; + + if(!int.TryParse(StringHandlers.CToString(fourcharvalue, Encoding.ASCII), out int year)) + year = 0; twocharvalue[0] = vdDateTime[4]; twocharvalue[1] = vdDateTime[5]; + DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "month = \"{0}\"", StringHandlers.CToString(twocharvalue, Encoding.ASCII)); - if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int month)) month = 0; + + if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int month)) + month = 0; twocharvalue[0] = vdDateTime[6]; twocharvalue[1] = vdDateTime[7]; + DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "day = \"{0}\"", StringHandlers.CToString(twocharvalue, Encoding.ASCII)); - if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int day)) day = 0; + + if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int day)) + day = 0; twocharvalue[0] = vdDateTime[8]; twocharvalue[1] = vdDateTime[9]; + DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "hour = \"{0}\"", StringHandlers.CToString(twocharvalue, Encoding.ASCII)); - if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int hour)) hour = 0; + + if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int hour)) + hour = 0; twocharvalue[0] = vdDateTime[10]; twocharvalue[1] = vdDateTime[11]; + DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "minute = \"{0}\"", StringHandlers.CToString(twocharvalue, Encoding.ASCII)); - if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int minute)) minute = 0; + + if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int minute)) + minute = 0; twocharvalue[0] = vdDateTime[12]; twocharvalue[1] = vdDateTime[13]; + DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "second = \"{0}\"", StringHandlers.CToString(twocharvalue, Encoding.ASCII)); - if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int second)) second = 0; + + if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int second)) + second = 0; twocharvalue[0] = vdDateTime[14]; twocharvalue[1] = vdDateTime[15]; + DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "hundredths = \"{0}\"", StringHandlers.CToString(twocharvalue, Encoding.ASCII)); + if(!int.TryParse(StringHandlers.CToString(twocharvalue, Encoding.ASCII), out int hundredths)) hundredths = 0; DicConsole.DebugWriteLine("ISO9600ToDateTime handler", "decodedDT = new DateTime({0}, {1}, {2}, {3}, {4}, {5}, {6}, DateTimeKind.Unspecified);", year, month, day, hour, minute, second, hundredths * 10); - DateTime decodedDt = new DateTime(year, month, day, hour, minute, second, hundredths * 10, - DateTimeKind.Unspecified); + + var decodedDt = new DateTime(year, month, day, hour, minute, second, hundredths * 10, + DateTimeKind.Unspecified); return decodedDt; } - /// - /// Converts a VMS timestamp to a .NET DateTime - /// + /// Converts a VMS timestamp to a .NET DateTime /// VMS timestamp (tenths of microseconds since day 0 of the Julian Date) /// .NET DateTime /// C# works in UTC, VMS on Julian Date, some displacement may occur on disks created outside UTC public static DateTime VmsToDateTime(ulong vmsDate) { double delta = vmsDate * 0.0001; // Tenths of microseconds to milliseconds, will lose some detail + return JulianEpoch.AddMilliseconds(delta); } - /// - /// Converts an Amiga timestamp to a .NET DateTime - /// + /// Converts an Amiga timestamp to a .NET DateTime /// Days since the 1st Jan. 1978 /// Minutes since o'clock /// Ticks @@ -198,12 +197,11 @@ namespace DiscImageChef { DateTime temp = AmigaEpoch.AddDays(days); temp = temp.AddMinutes(minutes); + return temp.AddMilliseconds(ticks * 20); } - /// - /// Converts an UCSD Pascal timestamp to a .NET DateTime - /// + /// Converts an UCSD Pascal timestamp to a .NET DateTime /// UCSD Pascal timestamp /// .NET DateTime public static DateTime UcsdPascalToDateTime(short dateRecord) @@ -215,12 +213,11 @@ namespace DiscImageChef DicConsole.DebugWriteLine("UCSDPascalToDateTime handler", "dateRecord = 0x{0:X4}, year = {1}, month = {2}, day = {3}", dateRecord, year, month, day); + return new DateTime(year, month, day); } - /// - /// Converts a DOS timestamp to a .NET DateTime - /// + /// Converts a DOS timestamp to a .NET DateTime /// Date /// Time /// .NET DateTime @@ -235,20 +232,26 @@ namespace DiscImageChef DicConsole.DebugWriteLine("DOSToDateTime handler", "date = 0x{0:X4}, year = {1}, month = {2}, day = {3}", date, year, month, day); + DicConsole.DebugWriteLine("DOSToDateTime handler", "time = 0x{0:X4}, hour = {1}, minute = {2}, second = {3}", time, hour, minute, second); DateTime dosdate; - try { dosdate = new DateTime(year, month, day, hour, minute, second); } - catch(ArgumentOutOfRangeException) { dosdate = new DateTime(1980, 1, 1, 0, 0, 0); } + + try + { + dosdate = new DateTime(year, month, day, hour, minute, second); + } + catch(ArgumentOutOfRangeException) + { + dosdate = new DateTime(1980, 1, 1, 0, 0, 0); + } return dosdate; } - /// - /// Converts a CP/M timestamp to .NET DateTime - /// + /// Converts a CP/M timestamp to .NET DateTime /// CP/M timestamp /// .NET DateTime public static DateTime CpmToDateTime(byte[] timestamp) @@ -264,9 +267,7 @@ namespace DiscImageChef return temp; } - /// - /// Converts an ECMA timestamp to a .NET DateTime - /// + /// Converts an ECMA timestamp to a .NET DateTime /// Timezone /// Year /// Month @@ -278,77 +279,79 @@ namespace DiscImageChef /// Hundreds of microseconds /// Microseconds /// - public static DateTime EcmaToDateTime(ushort typeAndTimeZone, short year, byte month, byte day, - byte hour, - byte minute, byte second, byte centiseconds, - byte hundredsOfMicroseconds, - byte microseconds) + 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 specification = (byte)((typeAndTimeZone & 0xF000) >> 12); + 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; - if((preOffset & 0x800) == 0x800) offset = (short)(preOffset | 0xF000); - else offset = (short)(preOffset & 0x7FF); + if((preOffset & 0x800) == 0x800) + offset = (short)(preOffset | 0xF000); + else + offset = (short)(preOffset & 0x7FF); if(offset == -2047) return new DateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified).AddTicks(ticks); - if(offset < -1440 || offset > 1440) offset = 0; + if(offset < -1440 || + offset > 1440) + offset = 0; - return new DateTimeOffset(year, month, day, hour, minute, second, new TimeSpan(0, offset, 0)) - .AddTicks(ticks).DateTime; + return new DateTimeOffset(year, month, day, hour, minute, second, new TimeSpan(0, offset, 0)). + AddTicks(ticks).DateTime; } - /// - /// Convers a Solaris high resolution timestamp to .NET DateTime - /// + /// Convers a Solaris high resolution timestamp to .NET DateTime /// Solaris high resolution timestamp /// .NET DateTime public static DateTime UnixHrTimeToDateTime(ulong hrTimeStamp) => UnixEpoch.AddTicks((long)(hrTimeStamp / 100)); - /// - /// Converts an OS-9 timestamp to .NET DateTime - /// + /// Converts an OS-9 timestamp to .NET DateTime /// OS-9 timestamp /// .NET DateTime public static DateTime Os9ToDateTime(byte[] date) { - if(date == null || date.Length != 3 && date.Length != 5) return DateTime.MinValue; + if(date == null || + 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) + { + os9Date = new DateTime(1900, 0, 0, 0, 0, 0); } - catch(ArgumentOutOfRangeException) { os9Date = new DateTime(1900, 0, 0, 0, 0, 0); } return os9Date; } - /// - /// Converts a LIF timestamp to .NET DateTime - /// + /// Converts a LIF timestamp to .NET DateTime /// LIF timestamp /// .NET DateTime public static DateTime LifToDateTime(byte[] date) { - if(date == null || date.Length != 6) return new DateTime(1970, 1, 1, 0, 0, 0); + if(date == null || + date.Length != 6) + return new DateTime(1970, 1, 1, 0, 0, 0); return LifToDateTime(date[0], date[1], date[2], date[3], date[4], date[5]); } - /// - /// Converts a LIF timestamp to .NET DateTime - /// + /// Converts a LIF timestamp to .NET DateTime /// Yer /// Month /// Day @@ -367,12 +370,17 @@ namespace DiscImageChef int ihour = (hour >> 4) * 10 + (hour & 0xF); int isecond = (second >> 4) * 10 + (second & 0xF); - if(iyear >= 70) iyear += 1900; - else iyear += 2000; + if(iyear >= 70) + iyear += 1900; + else + iyear += 2000; return new DateTime(iyear, imonth, iday, ihour, iminute, isecond); } - catch(ArgumentOutOfRangeException) { return new DateTime(1970, 1, 1, 0, 0, 0); } + catch(ArgumentOutOfRangeException) + { + return new DateTime(1970, 1, 1, 0, 0, 0); + } } } } \ No newline at end of file diff --git a/Marshal.cs b/Marshal.cs index 2a54e64..0a3a50c 100644 --- a/Marshal.cs +++ b/Marshal.cs @@ -40,36 +40,30 @@ namespace DiscImageChef.Helpers /// Provides methods to marshal binary data into C# structs public static class Marshal { - /// - /// Returns the size of an unmanaged type in bytes. - /// + /// Returns the size of an unmanaged type in bytes. /// The type whose size is to be returned. /// The size, in bytes, of the type that is specified by the generic type parameter. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int SizeOf() - { - return System.Runtime.InteropServices.Marshal.SizeOf(); - } + public static int SizeOf() => System.Runtime.InteropServices.Marshal.SizeOf(); - /// - /// Marshal little-endian binary data to a structure - /// + /// Marshal little-endian binary data to a structure /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructureLittleEndian(byte[] bytes) where T : struct { - var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); + GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); + var str = - (T) System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); + (T)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); + ptr.Free(); + return str; } - /// - /// Marshal little-endian binary data to a structure - /// + /// Marshal little-endian binary data to a structure /// Byte array containing the binary data /// Start on the array where the structure begins /// Length of the structure in bytes @@ -79,28 +73,28 @@ namespace DiscImageChef.Helpers public static T ByteArrayToStructureLittleEndian(byte[] bytes, int start, int length) where T : struct { Span span = bytes; + return ByteArrayToStructureLittleEndian(span.Slice(start, length).ToArray()); } - /// - /// Marshal big-endian binary data to a structure - /// + /// Marshal big-endian binary data to a structure /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T ByteArrayToStructureBigEndian(byte[] bytes) where T : struct { - var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); + GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); + object str = - (T) System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); + (T)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); + ptr.Free(); - return (T) SwapStructureMembersEndian(str); + + return(T)SwapStructureMembersEndian(str); } - /// - /// Marshal big-endian binary data to a structure - /// + /// Marshal big-endian binary data to a structure /// Byte array containing the binary data /// Start on the array where the structure begins /// Length of the structure in bytes @@ -110,12 +104,11 @@ namespace DiscImageChef.Helpers public static T ByteArrayToStructureBigEndian(byte[] bytes, int start, int length) where T : struct { Span span = bytes; + return ByteArrayToStructureBigEndian(span.Slice(start, length).ToArray()); } - /// - /// Marshal PDP-11 binary data to a structure - /// + /// Marshal PDP-11 binary data to a structure /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type @@ -123,17 +116,18 @@ namespace DiscImageChef.Helpers public static T ByteArrayToStructurePdpEndian(byte[] bytes) where T : struct { { - var ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); + GCHandle ptr = GCHandle.Alloc(bytes, GCHandleType.Pinned); + object str = - (T) System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); + (T)System.Runtime.InteropServices.Marshal.PtrToStructure(ptr.AddrOfPinnedObject(), typeof(T)); + ptr.Free(); - return (T) SwapStructureMembersEndianPdp(str); + + return(T)SwapStructureMembersEndianPdp(str); } } - /// - /// Marshal PDP-11 binary data to a structure - /// + /// Marshal PDP-11 binary data to a structure /// Byte array containing the binary data /// Start on the array where the structure begins /// Length of the structure in bytes @@ -143,25 +137,24 @@ namespace DiscImageChef.Helpers public static T ByteArrayToStructurePdpEndian(byte[] bytes, int start, int length) where T : struct { Span span = bytes; + return ByteArrayToStructurePdpEndian(span.Slice(start, length).ToArray()); } /// - /// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this method - /// will crash. + /// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this + /// method will crash. /// /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static T SpanToStructureLittleEndian(ReadOnlySpan bytes) where T : struct - { - return MemoryMarshal.Read(bytes); - } + public static T SpanToStructureLittleEndian(ReadOnlySpan bytes) where T : struct => + MemoryMarshal.Read(bytes); /// - /// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this method - /// will crash. + /// Marshal little-endian binary data to a structure. If the structure type contains any non value type, this + /// method will crash. /// /// Byte span containing the binary data /// Start on the span where the structure begins @@ -170,14 +163,11 @@ namespace DiscImageChef.Helpers /// The binary data marshalled in a structure with the specified type [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T SpanToStructureLittleEndian(ReadOnlySpan bytes, int start, int length) - where T : struct - { - return MemoryMarshal.Read(bytes.Slice(start, length)); - } + where T : struct => MemoryMarshal.Read(bytes.Slice(start, length)); /// - /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method will - /// crash. + /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method + /// will crash. /// /// Byte array containing the binary data /// Type of the structure to marshal @@ -186,12 +176,13 @@ namespace DiscImageChef.Helpers public static T SpanToStructureBigEndian(ReadOnlySpan bytes) where T : struct { var str = SpanToStructureLittleEndian(bytes); - return (T) SwapStructureMembersEndian(str); + + return(T)SwapStructureMembersEndian(str); } /// - /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method will - /// crash. + /// Marshal big-endian binary data to a structure. If the structure type contains any non value type, this method + /// will crash. /// /// Byte span containing the binary data /// Start on the span where the structure begins @@ -202,7 +193,8 @@ namespace DiscImageChef.Helpers public static T SpanToStructureBigEndian(ReadOnlySpan bytes, int start, int length) where T : struct { var str = SpanToStructureLittleEndian(bytes.Slice(start, length)); - return (T) SwapStructureMembersEndian(str); + + return(T)SwapStructureMembersEndian(str); } /// @@ -216,7 +208,8 @@ namespace DiscImageChef.Helpers public static T SpanToStructurePdpEndian(ReadOnlySpan bytes) where T : struct { object str = SpanToStructureLittleEndian(bytes); - return (T) SwapStructureMembersEndianPdp(str); + + return(T)SwapStructureMembersEndianPdp(str); } /// @@ -230,12 +223,13 @@ namespace DiscImageChef.Helpers public static T SpanToStructurePdpEndian(ReadOnlySpan bytes, int start, int length) where T : struct { object str = SpanToStructureLittleEndian(bytes.Slice(start, length)); - return (T) SwapStructureMembersEndianPdp(str); + + return(T)SwapStructureMembersEndianPdp(str); } /// - /// Marshal a structure depending on the decoration of . If the decoration - /// is not present it will marshal as a reference type containing little endian structure. + /// Marshal a structure depending on the decoration of . If the + /// decoration is not present it will marshal as a reference type containing little endian structure. /// /// Byte array containing the binary data /// Type of the structure to marshal @@ -247,111 +241,116 @@ namespace DiscImageChef.Helpers [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T MarshalStructure(byte[] bytes) where T : struct { - if (!(typeof(T).GetCustomAttribute(typeof(MarshallingPropertiesAttribute)) is MarshallingPropertiesAttribute - properties)) return ByteArrayToStructureLittleEndian(bytes); + if(!(typeof(T).GetCustomAttribute(typeof(MarshallingPropertiesAttribute)) is MarshallingPropertiesAttribute + properties)) + return ByteArrayToStructureLittleEndian(bytes); - switch (properties.Endian) + switch(properties.Endian) { case BitEndian.Little: - return properties.HasReferences - ? ByteArrayToStructureLittleEndian(bytes) - : SpanToStructureLittleEndian(bytes); + return properties.HasReferences ? ByteArrayToStructureLittleEndian(bytes) + : SpanToStructureLittleEndian(bytes); break; case BitEndian.Big: - return properties.HasReferences - ? ByteArrayToStructureBigEndian(bytes) - : SpanToStructureBigEndian(bytes); + return properties.HasReferences ? ByteArrayToStructureBigEndian(bytes) + : SpanToStructureBigEndian(bytes); break; case BitEndian.Pdp: - return properties.HasReferences - ? ByteArrayToStructurePdpEndian(bytes) - : SpanToStructurePdpEndian(bytes); + return properties.HasReferences ? ByteArrayToStructurePdpEndian(bytes) + : SpanToStructurePdpEndian(bytes); default: throw new ArgumentOutOfRangeException(); } } - /// - /// Swaps all members of a structure - /// + /// Swaps all members of a structure /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static object SwapStructureMembersEndian(object str) { - var t = str.GetType(); - var fieldInfo = t.GetFields(); - foreach (var fi in fieldInfo) - if (fi.FieldType == typeof(short)) + Type t = str.GetType(); + FieldInfo[] fieldInfo = t.GetFields(); + + foreach(FieldInfo fi in fieldInfo) + if(fi.FieldType == typeof(short)) { - var x = (short) fi.GetValue(str); - fi.SetValue(str, (short) ((x << 8) | ((x >> 8) & 0xFF))); + short x = (short)fi.GetValue(str); + fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF))); } - else if (fi.FieldType == typeof(int)) + else if(fi.FieldType == typeof(int)) { - var x = (int) fi.GetValue(str); - x = (int) (((x << 8) & 0xFF00FF00) | (((uint) x >> 8) & 0xFF00FF)); - fi.SetValue(str, (int) (((uint) x << 16) | (((uint) x >> 16) & 0xFFFF))); + int x = (int)fi.GetValue(str); + 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)) + else if(fi.FieldType == typeof(long)) { - var x = (long) fi.GetValue(str); - 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); + long x = (long)fi.GetValue(str); + 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)) + else if(fi.FieldType == typeof(ushort)) { - var x = (ushort) fi.GetValue(str); - fi.SetValue(str, (ushort) ((x << 8) | (x >> 8))); + ushort x = (ushort)fi.GetValue(str); + fi.SetValue(str, (ushort)((x << 8) | (x >> 8))); } - else if (fi.FieldType == typeof(uint)) + else if(fi.FieldType == typeof(uint)) { - var x = (uint) fi.GetValue(str); - x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); - fi.SetValue(str, (x << 16) | (x >> 16)); + uint x = (uint)fi.GetValue(str); + x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); + fi.SetValue(str, (x << 16) | (x >> 16)); } - else if (fi.FieldType == typeof(ulong)) + else if(fi.FieldType == typeof(ulong)) { - var x = (ulong) fi.GetValue(str); + ulong x = (ulong)fi.GetValue(str); x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32); x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16); - x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); + x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); fi.SetValue(str, x); } - else if (fi.FieldType == typeof(float)) + else if(fi.FieldType == typeof(float)) { - var flt = (float) fi.GetValue(str); - var flt_b = BitConverter.GetBytes(flt); - fi.SetValue(str, BitConverter.ToSingle(new[] {flt_b[3], flt_b[2], flt_b[1], flt_b[0]}, 0)); + float flt = (float)fi.GetValue(str); + 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)); } - else if (fi.FieldType == typeof(double)) + else if(fi.FieldType == typeof(double)) { - var dbl = (double) fi.GetValue(str); - var 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)); + double dbl = (double)fi.GetValue(str); + 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)); } - else if (fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte)) + else if(fi.FieldType == typeof(byte) || + fi.FieldType == typeof(sbyte)) { // Do nothing, can't byteswap them! } - else if (fi.FieldType == typeof(Guid)) + else if(fi.FieldType == typeof(Guid)) { // TODO: Swap GUID } + // TODO: Swap arrays and enums - else if (fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray) + else if(fi.FieldType.IsValueType && + !fi.FieldType.IsEnum && + !fi.FieldType.IsArray) { - var obj = fi.GetValue(str); - var strc = SwapStructureMembersEndian(obj); + object obj = fi.GetValue(str); + object strc = SwapStructureMembersEndian(obj); fi.SetValue(str, strc); } @@ -361,49 +360,58 @@ namespace DiscImageChef.Helpers [MethodImpl(MethodImplOptions.AggressiveInlining)] public static object SwapStructureMembersEndianPdp(object str) { - var t = str.GetType(); - var fieldInfo = t.GetFields(); - foreach (var fi in fieldInfo) - if (fi.FieldType == typeof(short) || fi.FieldType == typeof(long) || fi.FieldType == typeof(ushort) || - fi.FieldType == typeof(ulong) || fi.FieldType == typeof(float) || fi.FieldType == typeof(double) || - fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte) || fi.FieldType == typeof(Guid)) + Type t = str.GetType(); + FieldInfo[] fieldInfo = t.GetFields(); + + foreach(FieldInfo fi in fieldInfo) + if(fi.FieldType == typeof(short) || + fi.FieldType == typeof(long) || + fi.FieldType == typeof(ushort) || + fi.FieldType == typeof(ulong) || + fi.FieldType == typeof(float) || + fi.FieldType == typeof(double) || + fi.FieldType == typeof(byte) || + fi.FieldType == typeof(sbyte) || + fi.FieldType == typeof(Guid)) { // Do nothing } - else if (fi.FieldType == typeof(int)) + else if(fi.FieldType == typeof(int)) { - var x = (int) fi.GetValue(str); + int x = (int)fi.GetValue(str); fi.SetValue(str, ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16)); } - else if (fi.FieldType == typeof(uint)) + else if(fi.FieldType == typeof(uint)) { - var x = (uint) fi.GetValue(str); + uint x = (uint)fi.GetValue(str); fi.SetValue(str, ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16)); } + // TODO: Swap arrays and enums - else if (fi.FieldType.IsValueType && !fi.FieldType.IsEnum && !fi.FieldType.IsArray) + else if(fi.FieldType.IsValueType && + !fi.FieldType.IsEnum && + !fi.FieldType.IsArray) { - var obj = fi.GetValue(str); - var strc = SwapStructureMembersEndianPdp(obj); + object obj = fi.GetValue(str); + object strc = SwapStructureMembersEndianPdp(obj); fi.SetValue(str, strc); } return str; } - /// - /// Marshal a structure to little-endian binary data - /// + /// Marshal a structure to little-endian binary data /// Byte array containing the binary data /// Type of the structure to marshal /// The binary data marshalled in a structure with the specified type [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()]; + GCHandle ptr = GCHandle.Alloc(buf, GCHandleType.Pinned); System.Runtime.InteropServices.Marshal.StructureToPtr(str, ptr.AddrOfPinnedObject(), false); ptr.Free(); + return buf; } } diff --git a/MarshallingPropertiesAttribute.cs b/MarshallingPropertiesAttribute.cs index 110185a..e131e72 100644 --- a/MarshallingPropertiesAttribute.cs +++ b/MarshallingPropertiesAttribute.cs @@ -40,19 +40,10 @@ using System; namespace DiscImageChef.Helpers { - /// - /// Defines properties to help marshalling structs from binary data - /// + /// Defines properties to help marshalling structs from binary data [AttributeUsage(AttributeTargets.Struct)] public class MarshallingPropertiesAttribute : Attribute { - /// c - public BitEndian Endian { get; } - /// - /// Tells if the structure, or any nested structure, has any non-value type (e.g. arrays, strings, etc). - /// - public bool HasReferences { get; set; } - /// Defines properties to help marshalling structs from binary data /// Defines properties to help marshalling structs from binary data public MarshallingPropertiesAttribute(BitEndian endian) @@ -60,5 +51,10 @@ namespace DiscImageChef.Helpers Endian = endian; HasReferences = true; } + + /// c + public BitEndian Endian { get; } + /// Tells if the structure, or any nested structure, has any non-value type (e.g. arrays, strings, etc). + public bool HasReferences { get; set; } } } \ No newline at end of file diff --git a/PrintHex.cs b/PrintHex.cs index 9f79092..8a5e583 100644 --- a/PrintHex.cs +++ b/PrintHex.cs @@ -55,11 +55,11 @@ namespace DiscImageChef // TODO: Color list // TODO: Allow to change width - string str = "Offset"; - int rows = array.Length / 16; - int last = array.Length % 16; - int offsetLength = $"{array.Length:X}".Length; - var sb = new StringBuilder(); + string str = "Offset"; + int rows = array.Length / 16; + int last = array.Length % 16; + int offsetLength = $"{array.Length:X}".Length; + var sb = new StringBuilder(); if(last == 0) last = 16; @@ -101,7 +101,7 @@ namespace DiscImageChef sb.Append("\u001b[0m"); sb.Append(" "); - int lastBytes = i == rows - 1 ? last : 16; + int lastBytes = i == rows - 1 ? last : 16; int lastSpaces = 16 - lastBytes; for(int j = 0; j < lastBytes; j++) diff --git a/StringHandlers.cs b/StringHandlers.cs index 7046e1d..b32d866 100644 --- a/StringHandlers.cs +++ b/StringHandlers.cs @@ -37,16 +37,12 @@ namespace DiscImageChef { public static class StringHandlers { - /// - /// Converts a null-terminated (aka C string) ASCII byte array to a C# string - /// + /// Converts a null-terminated (aka C string) ASCII byte array to a C# string /// The corresponding C# string /// A null-terminated (aka C string) ASCII byte array public static string CToString(byte[] CString) => CToString(CString, Encoding.ASCII); - /// - /// Converts a null-terminated (aka C string) byte array with the specified encoding to a C# string - /// + /// Converts a null-terminated (aka C string) byte array with the specified encoding to a C# string /// The corresponding C# string /// A null-terminated (aka C string) byte array in the specified encoding /// Encoding. @@ -54,7 +50,8 @@ namespace DiscImageChef /// Start decodint at this position public static string CToString(byte[] CString, Encoding encoding, bool twoBytes = false, int start = 0) { - if(CString == null) return null; + if(CString == null) + return null; int len = 0; @@ -63,9 +60,11 @@ namespace DiscImageChef if(CString[i] == 0) if(twoBytes) { - if(i + 1 < CString.Length && CString[i + 1] == 0) + if(i + 1 < CString.Length && + CString[i + 1] == 0) { len++; + break; } } @@ -75,7 +74,8 @@ namespace DiscImageChef len++; } - if(twoBytes && len % 2 > 0) len--; + if(twoBytes && len % 2 > 0) + len--; byte[] dest = new byte[len]; Array.Copy(CString, start, dest, 0, len); @@ -83,30 +83,28 @@ namespace DiscImageChef return len == 0 ? "" : encoding.GetString(dest); } - /// - /// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string - /// + /// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string /// The corresponding C# string /// A length-prefixed (aka Pascal string) ASCII byte array public static string PascalToString(byte[] PascalString) => PascalToString(PascalString, Encoding.ASCII); - /// - /// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string - /// + /// Converts a length-prefixed (aka Pascal string) ASCII byte array to a C# string /// The corresponding C# string /// A length-prefixed (aka Pascal string) ASCII byte array /// Encoding. /// Start decodint at this position public static string PascalToString(byte[] PascalString, Encoding encoding, int start = 0) { - if(PascalString == null) return null; + if(PascalString == null) + return null; byte length = PascalString[start]; int len = 0; for(int i = start + 1; i < length + 1 && i < PascalString.Length; i++) { - if(PascalString[i] == 0) break; + if(PascalString[i] == 0) + break; len++; } @@ -117,43 +115,41 @@ namespace DiscImageChef return len == 0 ? "" : encoding.GetString(dest); } - /// - /// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string - /// + /// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string /// The corresponding C# string /// A space (' ', 0x20, ASCII SPACE) padded ASCII byte array public static string SpacePaddedToString(byte[] SpacePaddedString) => SpacePaddedToString(SpacePaddedString, Encoding.ASCII); - /// - /// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string - /// + /// Converts a space (' ', 0x20, ASCII SPACE) padded ASCII byte array to a C# string /// The corresponding C# string /// A space (' ', 0x20, ASCII SPACE) padded ASCII byte array /// Encoding. /// Start decodint at this position public static string SpacePaddedToString(byte[] SpacePaddedString, Encoding encoding, int start = 0) { - if(SpacePaddedString == null) return null; + if(SpacePaddedString == null) + return null; int len = start; for(int i = SpacePaddedString.Length; i >= start; i--) { - if(i == start) return ""; + if(i == start) + return""; - if(SpacePaddedString[i - 1] == 0x20) continue; + if(SpacePaddedString[i - 1] == 0x20) + continue; len = i; + break; } return len == 0 ? "" : encoding.GetString(SpacePaddedString, start, len); } - /// - /// Converts an OSTA compressed unicode byte array to a C# string - /// + /// Converts an OSTA compressed unicode byte array to a C# string /// The C# string. /// OSTA compressed unicode byte array. public static string DecompressUnicode(byte[] dstring) @@ -162,16 +158,22 @@ namespace DiscImageChef byte compId = dstring[0]; string temp = ""; - if(compId != 8 && compId != 16) return null; + if(compId != 8 && + compId != 16) + return null; for(int byteIndex = 1; byteIndex < dstring.Length;) { - if(compId == 16) unicode = (ushort)(dstring[byteIndex++] << 8); - else unicode = 0; + if(compId == 16) + unicode = (ushort)(dstring[byteIndex++] << 8); + else + unicode = 0; - if(byteIndex < dstring.Length) unicode |= dstring[byteIndex++]; + if(byteIndex < dstring.Length) + unicode |= dstring[byteIndex++]; - if(unicode == 0) break; + if(unicode == 0) + break; temp += Encoding.Unicode.GetString(BitConverter.GetBytes(unicode)); } diff --git a/Swapping.cs b/Swapping.cs index 2b01036..51c8842 100644 --- a/Swapping.cs +++ b/Swapping.cs @@ -52,14 +52,16 @@ namespace DiscImageChef public static uint Swap(uint x) { x = ((x << 8) & 0xFF00FF00) | ((x >> 8) & 0xFF00FF); - return (x << 16) | (x >> 16); + + return(x << 16) | (x >> 16); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Swap(int x) { 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)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -68,6 +70,7 @@ namespace DiscImageChef x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32); x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16); x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); + return x; } @@ -77,6 +80,7 @@ namespace DiscImageChef 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; } }