Interop fixes
authorCryptoManiac <balthazar@yandex.ru>
Sun, 16 Aug 2015 22:16:39 +0000 (01:16 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Sun, 16 Aug 2015 22:16:39 +0000 (01:16 +0300)
Novacoin/Interop.cs
Novacoin/ScriptOpcode.cs

index 1008392..0081b46 100644 (file)
@@ -31,6 +31,7 @@ namespace Novacoin
 
             if (!BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on big-endian machine
                 Array.Reverse(resultBytes);
             }
 
@@ -43,6 +44,7 @@ namespace Novacoin
 
             if (!BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on big-endian machine
                 Array.Reverse(resultBytes);
             }
 
@@ -55,6 +57,7 @@ namespace Novacoin
 
             if (!BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on big-endian machine
                 Array.Reverse(resultBytes);
             }
 
@@ -67,6 +70,7 @@ namespace Novacoin
 
             if (BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on little-endian machine
                 Array.Reverse(resultBytes);
             }
 
@@ -79,6 +83,7 @@ namespace Novacoin
 
             if (BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on little-endian machine
                 Array.Reverse(resultBytes);
             }
 
@@ -91,6 +96,7 @@ namespace Novacoin
 
             if (BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on little-endian machine
                 Array.Reverse(resultBytes);
             }
 
@@ -106,6 +112,7 @@ namespace Novacoin
 
             if (!BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on big-endian machine
                 Array.Reverse(bytes);
             }
 
@@ -114,13 +121,14 @@ namespace Novacoin
 
         public static uint LEBytesToUInt32(byte[] bytes)
         {
-            if (bytes.Length != sizeof(ushort))
+            if (bytes.Length != sizeof(uint))
             {
                 throw new InteropException("Array size doesn't match the uint data type.");
             }
 
             if (!BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on big-endian machine
                 Array.Reverse(bytes);
             }
 
@@ -129,19 +137,67 @@ namespace Novacoin
 
         public static ulong LEBytesToUInt64(byte[] bytes)
         {
-            if (bytes.Length != sizeof(ushort))
+            if (bytes.Length != sizeof(ulong))
             {
                 throw new InteropException("Array size doesn't match the ulong data type.");
             }
 
             if (!BitConverter.IsLittleEndian)
             {
+                // Reverse array if we are on big-endian machine
                 Array.Reverse(bytes);
             }
 
             return BitConverter.ToUInt64(bytes, 0);
         }
 
+        public static ushort BEBytesToUInt16(byte[] bytes)
+        {
+            if (bytes.Length != sizeof(ushort))
+            {
+                throw new InteropException("Array size doesn't match the ushort data type.");
+            }
+
+            if (BitConverter.IsLittleEndian)
+            {
+                // Reverse array if we are on little-endian machine
+                Array.Reverse(bytes);
+            }
+
+            return BitConverter.ToUInt16(bytes, 0);
+        }
+
+        public static uint BEBytesToUInt32(byte[] bytes)
+        {
+            if (bytes.Length != sizeof(uint))
+            {
+                throw new InteropException("Array size doesn't match the uint data type.");
+            }
+
+            if (BitConverter.IsLittleEndian)
+            {
+                // Reverse array if we are on little-endian machine
+                Array.Reverse(bytes);
+            }
+
+            return BitConverter.ToUInt32(bytes, 0);
+        }
+
+        public static ulong BEBytesToUInt64(byte[] bytes)
+        {
+            if (bytes.Length != sizeof(ulong))
+            {
+                throw new InteropException("Array size doesn't match the ulong data type.");
+            }
+
+            if (BitConverter.IsLittleEndian)
+            {
+                // Reverse array if we are on little-endian machine
+                Array.Reverse(bytes);
+            }
+
+            return BitConverter.ToUInt64(bytes, 0);
+        }
 
     }
 }
index 3cf1ba1..4631bae 100644 (file)
@@ -480,13 +480,7 @@ namespace Novacoin
                     return false;
                 }
 
-                // Reverse array if we are on little-endian machine
-                if (BitConverter.IsLittleEndian)
-                {
-                    Array.Reverse(szBytes);
-                }
-
-                int nSize = BitConverter.ToInt32(szBytes, 0);
+                int nSize = (int)Interop.BEBytesToUInt32(szBytes);
 
                 if (nSize > 0)
                 {