diff --git a/BigEndianMarshal.cs b/BigEndianMarshal.cs index 15cdb208e..a4c5569c7 100644 --- a/BigEndianMarshal.cs +++ b/BigEndianMarshal.cs @@ -31,7 +31,6 @@ // ****************************************************************************/ using System; -using System.Linq; using System.Reflection; using System.Runtime.InteropServices; @@ -67,59 +66,57 @@ namespace DiscImageChef foreach(FieldInfo fi in fieldInfo) if(fi.FieldType == typeof(short)) { - short int16 = (short)fi.GetValue(str); - byte[] int16_b = BitConverter.GetBytes(int16); - byte[] int16_r = int16_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToInt16(int16_r, 0)); + short x = (short)fi.GetValue(str); + fi.SetValue(str, (short)((x << 8) | ((x >> 8) & 0xFF))); } else if(fi.FieldType == typeof(int)) { - int int32 = (int)fi.GetValue(str); - byte[] int32_b = BitConverter.GetBytes(int32); - byte[] int32_r = int32_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToInt32(int32_r, 0)); + 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)) { - long int64 = (long)fi.GetValue(str); - byte[] int64_b = BitConverter.GetBytes(int64); - byte[] int64_r = int64_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToInt64(int64_r, 0)); + 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)) { - ushort uint16 = (ushort)fi.GetValue(str); - byte[] uint16_b = BitConverter.GetBytes(uint16); - byte[] uint16_r = uint16_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToUInt16(uint16_r, 0)); + ushort x = (ushort)fi.GetValue(str); + fi.SetValue(str, (ushort)((x << 8) | (x >> 8))); } else if(fi.FieldType == typeof(uint)) { - uint uint32 = (uint)fi.GetValue(str); - byte[] uint32_b = BitConverter.GetBytes(uint32); - byte[] uint32_r = uint32_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToUInt32(uint32_r, 0)); + 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)) { - ulong uint64 = (ulong)fi.GetValue(str); - byte[] uint64_b = BitConverter.GetBytes(uint64); - byte[] uint64_r = uint64_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToUInt64(uint64_r, 0)); + 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); + fi.SetValue(str, x); } else if(fi.FieldType == typeof(float)) { float flt = (float)fi.GetValue(str); byte[] flt_b = BitConverter.GetBytes(flt); - byte[] flt_r = flt_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToSingle(flt_r, 0)); + fi.SetValue(str, BitConverter.ToSingle(new[] {flt_b[3], flt_b[2], flt_b[1], flt_b[0]}, 0)); } else if(fi.FieldType == typeof(double)) { double dbl = (double)fi.GetValue(str); byte[] dbl_b = BitConverter.GetBytes(dbl); - byte[] dbl_r = dbl_b.Reverse().ToArray(); - fi.SetValue(str, BitConverter.ToDouble(dbl_r, 0)); + fi.SetValue(str, + BitConverter + .ToDouble(new[] {dbl_b[7], dbl_b[6], dbl_b[5], dbl_b[4], dbl_b[3], dbl_b[2], dbl_b[1], dbl_b[0]}, + 0)); } else if(fi.FieldType == typeof(byte) || fi.FieldType == typeof(sbyte)) { diff --git a/Swapping.cs b/Swapping.cs index b8defbbe8..962305042 100644 --- a/Swapping.cs +++ b/Swapping.cs @@ -30,83 +30,14 @@ // Copyright © 2011-2019 Natalia Portillo // ****************************************************************************/ -using System; -using System.Linq; - 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 uint PDPFromLittleEndian(uint x) => ((x & 0xffff) << 16) | ((x & 0xffff0000) >> 16); public static uint PDPFromBigEndian(uint x) => ((x & 0xff00ff) << 8) | ((x & 0xff00ff00) >> 8); - public static ulong Swap(ulong x) - { - x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32); - x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16); - x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); - return x; - } - - public static long Swap(long x) => BitConverter.ToInt64(BitConverter.GetBytes(x).Reverse().ToArray(), 0); - public static ushort Swap(ushort x) => (ushort)((x << 8) | (x >> 8)); public static short Swap(short x) => (short)((x << 8) | ((x >> 8) & 0xFF)); @@ -122,5 +53,21 @@ namespace DiscImageChef x = (int)(((x << 8) & 0xFF00FF00) | (((uint)x >> 8) & 0xFF00FF)); return (int)(((uint)x << 16) | (((uint)x >> 16) & 0xFFFF)); } + + public static ulong Swap(ulong x) + { + x = ((x & 0x00000000FFFFFFFF) << 32) | ((x & 0xFFFFFFFF00000000) >> 32); + x = ((x & 0x0000FFFF0000FFFF) << 16) | ((x & 0xFFFF0000FFFF0000) >> 16); + x = ((x & 0x00FF00FF00FF00FF) << 8) | ((x & 0xFF00FF00FF00FF00) >> 8); + return x; + } + + public static long Swap(long x) + { + x = ((x & 0x00000000FFFFFFFF) << 32) | (long)(((ulong)x & 0xFFFFFFFF00000000) >> 32); + x = ((x & 0x0000FFFF0000FFFF) << 16) | (long)(((ulong)x & 0xFFFF0000FFFF0000) >> 16); + x = ((x & 0x00FF00FF00FF00FF) << 8) | (long)(((ulong)x & 0xFF00FF00FF00FF00) >> 8); + return x; + } } } \ No newline at end of file