From 9c5a9a3671f7151009133f00be1892508745802a Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Mon, 5 Oct 2015 18:58:13 +0100 Subject: [PATCH] * DiscImageChef/Checksums/MD5Context.cs: * DiscImageChef.Checksums/MD5Context.cs: * DiscImageChef/Checksums/CDChecksums.cs: * DiscImageChef.Checksums/ReedSolomon.cs: * DiscImageChef.Checksums/SHA1Context.cs: * DiscImageChef/Checksums/ReedSolomon.cs: * DiscImageChef/Checksums/SHA1Context.cs: * DiscImageChef.Checksums/CDChecksums.cs: * DiscImageChef/Checksums/CRC64Context.cs: * DiscImageChef/Checksums/CRC32Context.cs: * DiscImageChef/Checksums/CRC16Context.cs: * DiscImageChef.Checksums/CRC64Context.cs: * DiscImageChef.Checksums/CRC32Context.cs: * DiscImageChef.Checksums/CRC16Context.cs: * DiscImageChef/Checksums/SHA256Context.cs: * DiscImageChef.Checksums/SHA512Context.cs: * DiscImageChef.Checksums/SHA384Context.cs: * DiscImageChef/Checksums/SHA384Context.cs: * DiscImageChef/Checksums/SHA512Context.cs: * DiscImageChef.Checksums/SHA256Context.cs: * DiscImageChef.Checksums/Adler32Context.cs: * DiscImageChef/Checksums/SpamSumContext.cs: * DiscImageChef/Checksums/Adler32Context.cs: * DiscImageChef.Checksums/SpamSumContext.cs: * DiscImageChef.Checksums/FletcherContext.cs: * DiscImageChef/Checksums/FletcherContext.cs: * DiscImageChef.Checksums/RIPEMD160Context.cs: * DiscImageChef/Checksums/RIPEMD160Context.cs: * DiscImageChef.Checksums/Properties/AssemblyInfo.cs: * DiscImageChef.Checksums/DiscImageChef.Checksums.csproj: Move checksums to a separate library. * DiscImageChef/Swapping.cs: * DiscImageChef/PrintHex.cs: * DiscImageChef/ArrayFill.cs: * DiscImageChef/DateHandlers.cs: * DiscImageChef/StringHandlers.cs: * DiscImageChef.Helpers/Swapping.cs: * DiscImageChef.Helpers/PrintHex.cs: * DiscImageChef/DiscImageChef.csproj: * DiscImageChef.Helpers/ArrayFill.cs: * DiscImageChef.Helpers/DateHandlers.cs: * DiscImageChef/BigEndianBitConverter.cs: * DiscImageChef.Helpers/StringHandlers.cs: * DiscImageChef/EndianAwareBinaryReader.cs: * DiscImageChef.Helpers/BigEndianBitConverter.cs: * DiscImageChef.Helpers/EndianAwareBinaryReader.cs: * DiscImageChef.Helpers/Properties/AssemblyInfo.cs: * DiscImageChef.Helpers/DiscImageChef.Helpers.csproj: Move helpers to a separate library. * DiscImageChef.sln: Move helpers to a separate library. Move checksums to a separate library. --- ArrayFill.cs | 70 ++++ BigEndianBitConverter.cs | 660 +++++++++++++++++++++++++++++++++++ ChangeLog | 13 + DateHandlers.cs | 158 +++++++++ DiscImageChef.Helpers.csproj | 47 +++ EndianAwareBinaryReader.cs | 162 +++++++++ PrintHex.cs | 93 +++++ Properties/AssemblyInfo.cs | 27 ++ StringHandlers.cs | 113 ++++++ Swapping.cs | 111 ++++++ 10 files changed, 1454 insertions(+) create mode 100644 ArrayFill.cs create mode 100644 BigEndianBitConverter.cs create mode 100644 ChangeLog create mode 100644 DateHandlers.cs create mode 100644 DiscImageChef.Helpers.csproj create mode 100644 EndianAwareBinaryReader.cs create mode 100644 PrintHex.cs create mode 100644 Properties/AssemblyInfo.cs create mode 100644 StringHandlers.cs create mode 100644 Swapping.cs diff --git a/ArrayFill.cs b/ArrayFill.cs new file mode 100644 index 000000000..c3ba54cc6 --- /dev/null +++ b/ArrayFill.cs @@ -0,0 +1,70 @@ +/*************************************************************************** +The Disc Image Chef +---------------------------------------------------------------------------- + +Filename : ArrayFill.cs +Version : 1.0 +Author(s) : https://github.com/mykohsu + +Component : Helpers + +Revision : $Revision$ +Last change by : $Author$ +Date : $Date$ + +--[ Description ] ---------------------------------------------------------- + +Fills an array with a + +--[ License ] -------------------------------------------------------------- + + No license specified by creator. + + Published on https://github.com/mykohsu/Extensions/blob/master/ArrayExtensions.cs + + Assuming open source. + +---------------------------------------------------------------------------- +Copyright (C) 2014 mykohsu +****************************************************************************/ +//$Id$ +using System; + +namespace DiscImageChef +{ + public static class ArrayHelpers + { + public static void ArrayFill(T[] destinationArray, T value) + { + // if called with a single value, wrap the value in an array and call the main function + ArrayFill(destinationArray, new T[] { value }); + } + + public static void ArrayFill(T[] destinationArray, T[] value) + { + if (destinationArray == null) + { + throw new ArgumentNullException("destinationArray"); + } + + if (value.Length > destinationArray.Length) + { + throw new ArgumentException("Length of value array must not be more than length of destination"); + } + + // set the initial array value + Array.Copy(value, destinationArray, value.Length); + + int arrayToFillHalfLength = destinationArray.Length / 2; + int copyLength; + + for(copyLength = value.Length; copyLength < arrayToFillHalfLength; copyLength <<= 1) + { + Array.Copy(destinationArray, 0, destinationArray, copyLength, copyLength); + } + + Array.Copy(destinationArray, 0, destinationArray, copyLength, destinationArray.Length - copyLength); + } + } +} + diff --git a/BigEndianBitConverter.cs b/BigEndianBitConverter.cs new file mode 100644 index 000000000..171848d98 --- /dev/null +++ b/BigEndianBitConverter.cs @@ -0,0 +1,660 @@ +/*************************************************************************** +The Disc Image Chef +---------------------------------------------------------------------------- + +Filename : BigEndianBitConverter.cs +Version : 1.0 +Author(s) : Natalia Portillo + +Component : Program tools + +Revision : $Revision$ +Last change by : $Author$ +Date : $Date$ + +--[ Description ] ---------------------------------------------------------- + +Override of System.BitConverter that knows how to handle big-endian. + +--[ License ] -------------------------------------------------------------- + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +---------------------------------------------------------------------------- +Copyright (C) 2011-2014 Claunia.com +****************************************************************************/ +//$Id$ + +using System; +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. + /// + public static class BigEndianBitConverter + { + /// + /// Indicates the byte order ("endianess") in which data is stored in this computer + /// architecture. + /// + public static bool IsLittleEndian { get; set; } + // should default to false, which is what we want for Empire + /// + /// Converts the specified double-precision floating point number to a 64-bit + /// signed integer. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// A 64-bit signed integer whose value is equivalent to value. + /// + public static long DoubleToInt64Bits(double value) + { + throw new NotImplementedException(); + } + + /// + /// + /// Returns the specified Boolean value as an array of bytes. + /// + /// Parameters: + /// value: + /// A Boolean value. + /// + /// Returns: + /// An array of bytes with length 1. + /// + public static byte[] GetBytes(bool value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified Unicode character value as an array of bytes. + /// + /// Parameters: + /// value: + /// A character to convert. + /// + /// Returns: + /// An array of bytes with length 2. + /// + public static byte[] GetBytes(char value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified double-precision floating point value as an array of + /// bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 8. + /// + public static byte[] GetBytes(double value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified single-precision floating point value as an array of + /// bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 4. + /// + public static byte[] GetBytes(float value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified 32-bit signed integer value as an array of bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 4. + /// + public static byte[] GetBytes(int value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified 64-bit signed integer value as an array of bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 8. + /// + public static byte[] GetBytes(long value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified 16-bit signed integer value as an array of bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 2. + /// + public static byte[] GetBytes(short value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified 32-bit unsigned integer value as an array of bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 4. + /// + //[CLSCompliant(false)] + public static byte[] GetBytes(uint value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified 64-bit unsigned integer value as an array of bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 8. + /// + //[CLSCompliant(false)] + public static byte[] GetBytes(ulong value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Returns the specified 16-bit unsigned integer value as an array of bytes. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// An array of bytes with length 2. + /// + public static byte[] GetBytes(ushort value) + { + return !IsLittleEndian ? BitConverter.GetBytes(value) : BitConverter.GetBytes(value).Reverse().ToArray(); + } + + /// + /// + /// Converts the specified 64-bit signed integer to a double-precision floating + /// point number. + /// + /// Parameters: + /// value: + /// The number to convert. + /// + /// Returns: + /// 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. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// true if the byte at startIndex in value is nonzero; otherwise, false. + /// + /// Exceptions: + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + 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. + /// + /// Parameters: + /// value: + /// An array. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A character formed by two bytes beginning at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex equals the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static char ToChar(byte[] value, int startIndex) + { + throw new NotImplementedException(); + } + + /// + /// + /// Returns a double-precision floating point number converted from eight bytes + /// at a specified position in a byte array. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A double precision floating point number formed by eight bytes beginning + /// at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex is greater than or equal to the length of value minus 7, and is + /// less than or equal to the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + 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. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A 16-bit signed integer formed by two bytes beginning at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex equals the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static short ToInt16(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToInt16(value, startIndex) : BitConverter.ToInt16(value.Reverse().ToArray(), value.Length - sizeof(Int16) - startIndex); + } + + /// + /// + /// Returns a 32-bit signed integer converted from four bytes at a specified + /// position in a byte array. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A 32-bit signed integer formed by four bytes beginning at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex is greater than or equal to the length of value minus 3, and is + /// less than or equal to the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static int ToInt32(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToInt32(value, startIndex) : BitConverter.ToInt32(value.Reverse().ToArray(), value.Length - sizeof(Int32) - startIndex); + } + + /// + /// + /// Returns a 64-bit signed integer converted from eight bytes at a specified + /// position in a byte array. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A 64-bit signed integer formed by eight bytes beginning at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex is greater than or equal to the length of value minus 7, and is + /// less than or equal to the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static long ToInt64(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToInt64(value, startIndex) : BitConverter.ToInt64(value.Reverse().ToArray(), value.Length - sizeof(Int64) - startIndex); + } + + /// + /// + /// Returns a single-precision floating point number converted from four bytes + /// at a specified position in a byte array. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A single-precision floating point number formed by four bytes beginning at + /// startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex is greater than or equal to the length of value minus 3, and is + /// less than or equal to the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static float ToSingle(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToSingle(value, startIndex) : BitConverter.ToSingle(value.Reverse().ToArray(), value.Length - sizeof(Single) - startIndex); + } + + /// + /// + /// Converts the numeric value of each element of a specified array of bytes + /// to its equivalent hexadecimal string representation. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// Returns: + /// A System.String of hexadecimal pairs separated by hyphens, where each pair + /// represents the corresponding element in value; for example, "7F-2C-4A". + /// + /// Exceptions: + /// System.ArgumentNullException: + /// value is null. + /// + public static string ToString(byte[] value) + { + return !IsLittleEndian ? BitConverter.ToString(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. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A System.String of hexadecimal pairs separated by hyphens, where each pair + /// represents the corresponding element in a subarray of value; for example, + /// "7F-2C-4A". + /// + /// Exceptions: + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static string ToString(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToString(value, startIndex) : 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. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// length: + /// The number of array elements in value to convert. + /// + /// Returns: + /// A System.String of hexadecimal pairs separated by hyphens, where each pair + /// represents the corresponding element in a subarray of value; for example, + /// "7F-2C-4A". + /// + /// Exceptions: + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex or length is less than zero. -or- startIndex is greater than + /// zero and is greater than or equal to the length of value. + /// + /// System.ArgumentException: + /// The combination of startIndex and length does not specify a position within + /// value; that is, the startIndex parameter is greater than the length of value + /// minus the length parameter. + /// + public static string ToString(byte[] value, int startIndex, int length) + { + return !IsLittleEndian ? BitConverter.ToString(value, startIndex, 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. + /// + /// Parameters: + /// value: + /// The array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A 16-bit unsigned integer formed by two bytes beginning at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex equals the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static ushort ToUInt16(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToUInt16(value, startIndex) : BitConverter.ToUInt16(value.Reverse().ToArray(), value.Length - sizeof(UInt16) - startIndex); + } + + /// + /// + /// Returns a 32-bit unsigned integer converted from four bytes at a specified + /// position in a byte array. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A 32-bit unsigned integer formed by four bytes beginning at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex is greater than or equal to the length of value minus 3, and is + /// less than or equal to the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static uint ToUInt32(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToUInt32(value, startIndex) : BitConverter.ToUInt32(value.Reverse().ToArray(), value.Length - sizeof(UInt32) - startIndex); + } + + /// + /// + /// Returns a 64-bit unsigned integer converted from eight bytes at a specified + /// position in a byte array. + /// + /// Parameters: + /// value: + /// An array of bytes. + /// + /// startIndex: + /// The starting position within value. + /// + /// Returns: + /// A 64-bit unsigned integer formed by the eight bytes beginning at startIndex. + /// + /// Exceptions: + /// System.ArgumentException: + /// startIndex is greater than or equal to the length of value minus 7, and is + /// less than or equal to the length of value minus 1. + /// + /// System.ArgumentNullException: + /// value is null. + /// + /// System.ArgumentOutOfRangeException: + /// startIndex is less than zero or greater than the length of value minus 1. + /// + public static ulong ToUInt64(byte[] value, int startIndex) + { + return !IsLittleEndian ? BitConverter.ToUInt64(value, startIndex) : BitConverter.ToUInt64(value.Reverse().ToArray(), value.Length - sizeof(UInt64) - startIndex); + } + + public static Guid ToGuid(byte[] value, int startIndex) + { + return new Guid(BigEndianBitConverter.ToUInt32(value, 0 + startIndex), + BigEndianBitConverter.ToUInt16(value, 4 + startIndex), + BigEndianBitConverter.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/ChangeLog b/ChangeLog new file mode 100644 index 000000000..cbde71e5b --- /dev/null +++ b/ChangeLog @@ -0,0 +1,13 @@ +2015-10-05 Natalia Portillo + + * PrintHex.cs: + * Swapping.cs: + * ArrayFill.cs: + * DateHandlers.cs: + * StringHandlers.cs: + * BigEndianBitConverter.cs: + * EndianAwareBinaryReader.cs: + * Properties/AssemblyInfo.cs: + * DiscImageChef.Helpers.csproj: + Move helpers to a separate library. + diff --git a/DateHandlers.cs b/DateHandlers.cs new file mode 100644 index 000000000..2382044c6 --- /dev/null +++ b/DateHandlers.cs @@ -0,0 +1,158 @@ +/*************************************************************************** +The Disc Image Chef +---------------------------------------------------------------------------- + +Filename : DateHandlers.cs +Version : 1.0 +Author(s) : Natalia Portillo + +Component : Program tools + +Revision : $Revision$ +Last change by : $Author$ +Date : $Date$ + +--[ Description ] ---------------------------------------------------------- + +Convert several timestamp formats to C# DateTime. + +--[ License ] -------------------------------------------------------------- + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +---------------------------------------------------------------------------- +Copyright (C) 2011-2014 Claunia.com +****************************************************************************/ +//$Id$ + +using System; + +namespace DiscImageChef +{ + public static class DateHandlers + { + 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 + static readonly DateTime JulianEpoch = new DateTime(1858, 11, 17, 0, 0, 0); + static readonly DateTime AmigaEpoch = new DateTime(1978, 1, 1, 0, 0, 0); + + public static DateTime MacToDateTime(ulong MacTimeStamp) + { + return MacEpoch.AddTicks((long)(MacTimeStamp * 10000000)); + } + + public static DateTime LisaToDateTime(UInt32 LisaTimeStamp) + { + return LisaEpoch.AddSeconds(LisaTimeStamp); + } + + public static DateTime UNIXToDateTime(Int32 UNIXTimeStamp) + { + return UNIXEpoch.AddSeconds(UNIXTimeStamp); + } + + public static DateTime UNIXUnsignedToDateTime(UInt32 UNIXTimeStamp) + { + return UNIXEpoch.AddSeconds(UNIXTimeStamp); + } + + public static DateTime ISO9660ToDateTime(byte[] VDDateTime) + { + int year, month, day, hour, minute, second, hundredths; + byte[] twocharvalue = new byte[2]; + byte[] fourcharvalue = new byte[4]; + + fourcharvalue[0] = VDDateTime[0]; + fourcharvalue[1] = VDDateTime[1]; + fourcharvalue[2] = VDDateTime[2]; + fourcharvalue[3] = VDDateTime[3]; + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (ISO9600ToDateTime handler): year = \"{0}\"", StringHandlers.CToString(fourcharvalue)); + if (!Int32.TryParse(StringHandlers.CToString(fourcharvalue), out year)) + year = 0; +// year = Convert.ToInt32(StringHandlers.CToString(fourcharvalue)); + + twocharvalue[0] = VDDateTime[4]; + twocharvalue[1] = VDDateTime[5]; + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (ISO9600ToDateTime handler): month = \"{0}\"", StringHandlers.CToString(twocharvalue)); + if (!Int32.TryParse(StringHandlers.CToString(twocharvalue), out month)) + month = 0; +// month = Convert.ToInt32(StringHandlers.CToString(twocharvalue)); + + twocharvalue[0] = VDDateTime[6]; + twocharvalue[1] = VDDateTime[7]; + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (ISO9600ToDateTime handler): day = \"{0}\"", StringHandlers.CToString(twocharvalue)); + if (!Int32.TryParse(StringHandlers.CToString(twocharvalue), out day)) + day = 0; +// day = Convert.ToInt32(StringHandlers.CToString(twocharvalue)); + + twocharvalue[0] = VDDateTime[8]; + twocharvalue[1] = VDDateTime[9]; + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (ISO9600ToDateTime handler): hour = \"{0}\"", StringHandlers.CToString(twocharvalue)); + if (!Int32.TryParse(StringHandlers.CToString(twocharvalue), out hour)) + hour = 0; +// hour = Convert.ToInt32(StringHandlers.CToString(twocharvalue)); + + twocharvalue[0] = VDDateTime[10]; + twocharvalue[1] = VDDateTime[11]; + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (ISO9600ToDateTime handler): minute = \"{0}\"", StringHandlers.CToString(twocharvalue)); + if (!Int32.TryParse(StringHandlers.CToString(twocharvalue), out minute)) + minute = 0; +// minute = Convert.ToInt32(StringHandlers.CToString(twocharvalue)); + + twocharvalue[0] = VDDateTime[12]; + twocharvalue[1] = VDDateTime[13]; + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (ISO9600ToDateTime handler): second = \"{0}\"", StringHandlers.CToString(twocharvalue)); + if (!Int32.TryParse(StringHandlers.CToString(twocharvalue), out second)) + second = 0; +// second = Convert.ToInt32(StringHandlers.CToString(twocharvalue)); + + twocharvalue[0] = VDDateTime[14]; + twocharvalue[1] = VDDateTime[15]; + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (ISO9600ToDateTime handler): hundredths = \"{0}\"", StringHandlers.CToString(twocharvalue)); + if (!Int32.TryParse(StringHandlers.CToString(twocharvalue), out hundredths)) + hundredths = 0; +// hundredths = Convert.ToInt32(StringHandlers.CToString(twocharvalue)); + + //if (MainClass.isDebug) + Console.WriteLine("DEBUG (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); + + return decodedDT; + } + + // C# works in UTC, VMS on Julian Date, some displacement may occur on disks created outside UTC + public static DateTime VMSToDateTime(UInt64 vmsDate) + { + double delta = vmsDate * 0.0001; // Tenths of microseconds to milliseconds, will lose some detail + return JulianEpoch.AddMilliseconds(delta); + } + + public static DateTime AmigaToDateTime(UInt32 days, UInt32 minutes, UInt32 ticks) + { + DateTime temp = AmigaEpoch.AddDays(days); + temp = temp.AddMinutes(minutes); + return temp.AddMilliseconds(ticks * 20); + } + } +} + diff --git a/DiscImageChef.Helpers.csproj b/DiscImageChef.Helpers.csproj new file mode 100644 index 000000000..e424bb92a --- /dev/null +++ b/DiscImageChef.Helpers.csproj @@ -0,0 +1,47 @@ + + + + Debug + AnyCPU + 8.0.30703 + 2.0 + {F8BDF57B-1571-4CD0-84B3-B422088D359A} + Library + DiscImageChef.Helpers + DiscImageChef.Helpers + 2.2 + v3.5 + + + true + full + false + bin\Debug + DEBUG; + prompt + 4 + false + + + full + true + bin\Release + prompt + 4 + false + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/EndianAwareBinaryReader.cs b/EndianAwareBinaryReader.cs new file mode 100644 index 000000000..dce8b6be8 --- /dev/null +++ b/EndianAwareBinaryReader.cs @@ -0,0 +1,162 @@ +/*************************************************************************** +The Disc Image Chef +---------------------------------------------------------------------------- + +Filename : EndianAwareBinaryReader.cs +Version : 1.0 +Author(s) : Natalia Portillo + +Component : Program tools + +Revision : $Revision$ +Last change by : $Author$ +Date : $Date$ + +--[ Description ] ---------------------------------------------------------- + +Override for System.IO.Binary.Reader that knows how to handle big-endian. + +--[ License ] -------------------------------------------------------------- + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +---------------------------------------------------------------------------- +Copyright (C) 2011-2014 Claunia.com +****************************************************************************/ +//$Id$ + +using System; +using System.IO; +using System.Linq; +using System.Text; + +namespace DiscImageChef +{ + public class EndianAwareBinaryReader : BinaryReader + { + byte[] buffer = new byte[8]; + + public EndianAwareBinaryReader(Stream input, Encoding encoding, bool isLittleEndian) + : base(input, encoding) + { + IsLittleEndian = isLittleEndian; + } + + public EndianAwareBinaryReader(Stream input, bool isLittleEndian) + : this(input, Encoding.UTF8, isLittleEndian) + { + } + + public bool IsLittleEndian + { + get; + set; + } + + public override double ReadDouble() + { + if (IsLittleEndian) + return base.ReadDouble(); + FillMyBuffer(8); + return BitConverter.ToDouble(buffer.Take(8).Reverse().ToArray(), 0); + } + + public override short ReadInt16() + { + if (IsLittleEndian) + return base.ReadInt16(); + FillMyBuffer(2); + return BitConverter.ToInt16(buffer.Take(2).Reverse().ToArray(), 0); + + } + + public override int ReadInt32() + { + if (IsLittleEndian) + return base.ReadInt32(); + FillMyBuffer(4); + return BitConverter.ToInt32(buffer.Take(4).Reverse().ToArray(), 0); + + } + + public override long ReadInt64() + { + if (IsLittleEndian) + return base.ReadInt64(); + FillMyBuffer(8); + return BitConverter.ToInt64(buffer.Take(8).Reverse().ToArray(), 0); + + } + + public override float ReadSingle() + { + if (IsLittleEndian) + return base.ReadSingle(); + FillMyBuffer(4); + return BitConverter.ToSingle(buffer.Take(4).Reverse().ToArray(), 0); + } + + public override ushort ReadUInt16() + { + if (IsLittleEndian) + return base.ReadUInt16(); + FillMyBuffer(2); + return BitConverter.ToUInt16(buffer.Take(2).Reverse().ToArray(), 0); + } + + public override uint ReadUInt32() + { + if (IsLittleEndian) + return base.ReadUInt32(); + FillMyBuffer(4); + return BitConverter.ToUInt32(buffer.Take(4).Reverse().ToArray(), 0); + } + + public override ulong ReadUInt64() + { + if (IsLittleEndian) + return base.ReadUInt64(); + FillMyBuffer(8); + return BitConverter.ToUInt64(buffer.Take(8).Reverse().ToArray(), 0); + } + + void FillMyBuffer(int numBytes) + { + int offset = 0; + int num2; + if (numBytes == 1) + { + num2 = BaseStream.ReadByte(); + if (num2 == -1) + { + throw new EndOfStreamException("Attempted to read past the end of the stream."); + } + buffer[0] = (byte)num2; + } + else + { + do + { + num2 = BaseStream.Read(buffer, offset, numBytes - offset); + if (num2 == 0) + { + throw new EndOfStreamException("Attempted to read past the end of the stream."); + } + offset += num2; + } + while (offset < numBytes); + } + } + } +} \ No newline at end of file diff --git a/PrintHex.cs b/PrintHex.cs new file mode 100644 index 000000000..a9564e2f4 --- /dev/null +++ b/PrintHex.cs @@ -0,0 +1,93 @@ +/*************************************************************************** +The Disc Image Chef +---------------------------------------------------------------------------- + +Filename : PrintHex.cs +Version : 1.0 +Author(s) : Natalia Portillo + +Component : Helpers + +Revision : $Revision$ +Last change by : $Author$ +Date : $Date$ + +--[ Description ] ---------------------------------------------------------- + +Prints a byte array as hexadecimal in console. + +--[ License ] -------------------------------------------------------------- + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +---------------------------------------------------------------------------- +Copyright (C) 2011-2014 Claunia.com +****************************************************************************/ +//$Id$ +using System; + +namespace DiscImageChef +{ + public static class PrintHex + { + public static void PrintHexArray(byte[] array, int width) + { + Console.WriteLine(ByteArrayToHexArrayString(array, width)); + } + + public static string ByteArrayToHexArrayString(byte[] array, int width) + { + System.Text.StringBuilder sb = new System.Text.StringBuilder(); + + int counter = 0; + int subcounter = 0; + for (long i = 0; i < array.LongLength; i++) + { + if (counter == 0) + { + sb.AppendLine(); + sb.AppendFormat("{0:X16} ", i); + } + else + { + if (subcounter == 3 ) + { + Console.Write(" "); + subcounter = 0; + } + else + { + Console.Write(" "); + subcounter++; + } + } + + sb.AppendFormat("{0:X2}", array[i]); + + if (counter == width - 1) + { + counter = 0; + subcounter = 0; + } + else + counter++; + } + sb.AppendLine(); + sb.AppendLine(); + + return sb.ToString(); + } + } +} + diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..0b0685491 --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("DiscImageChef.Helpers")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Claunia.com")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("© Claunia.com")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/StringHandlers.cs b/StringHandlers.cs new file mode 100644 index 000000000..faba521ae --- /dev/null +++ b/StringHandlers.cs @@ -0,0 +1,113 @@ +/*************************************************************************** +The Disc Image Chef +---------------------------------------------------------------------------- + +Filename : StringHandlers.cs +Version : 1.0 +Author(s) : Natalia Portillo + +Component : Program tools + +Revision : $Revision$ +Last change by : $Author$ +Date : $Date$ + +--[ Description ] ---------------------------------------------------------- + +Convert byte arrays to C# strings. + +--[ License ] -------------------------------------------------------------- + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +---------------------------------------------------------------------------- +Copyright (C) 2011-2014 Claunia.com +****************************************************************************/ +//$Id$ + +using System; +using System.Text; + +namespace DiscImageChef +{ + public static class StringHandlers + { + /// + /// 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) + { + StringBuilder sb = new StringBuilder(); + + for (int i = 0; i < CString.Length; i++) + { + if (CString[i] == 0) + break; + + sb.Append(Encoding.ASCII.GetString(CString, i, 1)); + } + + return sb.ToString(); + } + + /// + /// 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) + { + StringBuilder sb = new StringBuilder(); + + byte length = PascalString[0]; + + for (int i = 1; i < length + 1; i++) + { + sb.Append(Encoding.ASCII.GetString(PascalString, i, 1)); + } + + return sb.ToString(); + } + + /// + /// 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) + { + int length = 0; + + for (int i = SpacePaddedString.Length; i >= 0; i--) + { + if (i == 0) + return ""; + + if (SpacePaddedString[i - 1] != 0x20) + { + length = i; + break; + } + } + + if (length == 0) + return ""; + + return Encoding.ASCII.GetString(SpacePaddedString, 0, length); + } + } +} + diff --git a/Swapping.cs b/Swapping.cs new file mode 100644 index 000000000..177673103 --- /dev/null +++ b/Swapping.cs @@ -0,0 +1,111 @@ +/*************************************************************************** +The Disc Image Chef +---------------------------------------------------------------------------- + +Filename : Swapping.cs +Version : 1.0 +Author(s) : Natalia Portillo + +Component : Program tools + +Revision : $Revision$ +Last change by : $Author$ +Date : $Date$ + +--[ Description ] ---------------------------------------------------------- + +Byte-swapping methods + +--[ License ] -------------------------------------------------------------- + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +---------------------------------------------------------------------------- +Copyright (C) 2011-2014 Claunia.com +****************************************************************************/ +//$Id$ + +using System; + +namespace DiscImageChef +{ + public static class Swapping + { + public static byte[] SwapTenBytes(byte[] source) + { + byte[] destination = new byte[8]; + + destination[0] = source[9]; + destination[1] = source[8]; + destination[2] = source[7]; + destination[3] = source[6]; + destination[4] = source[5]; + destination[5] = source[4]; + destination[6] = source[3]; + destination[7] = source[2]; + destination[8] = source[1]; + destination[9] = source[0]; + + return destination; + } + + public static byte[] SwapEightBytes(byte[] source) + { + byte[] destination = new byte[8]; + + destination[0] = source[7]; + destination[1] = source[6]; + destination[2] = source[5]; + destination[3] = source[4]; + destination[4] = source[3]; + destination[5] = source[2]; + destination[6] = source[1]; + destination[7] = source[0]; + + return destination; + } + + public static byte[] SwapFourBytes(byte[] source) + { + byte[] destination = new byte[4]; + + destination[0] = source[3]; + destination[1] = source[2]; + destination[2] = source[1]; + destination[3] = source[0]; + + return destination; + } + + public static byte[] SwapTwoBytes(byte[] source) + { + byte[] destination = new byte[2]; + + destination[0] = source[1]; + destination[1] = source[0]; + + return destination; + } + + public static UInt32 PDPFromLittleEndian(UInt32 x) + { + return ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16); + } + + public static UInt32 PDPFromBigEndian(UInt32 x) + { + return ((x & 0xff00ff) << 8) | ((x & 0xff00ff00) >> 8); + } + } +}