X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FInterop.cs;h=f4c1e2eec7c9b18ddda22ccfed5031b9bdad5339;hb=6c6e3b0c68e764ea520dde48d5e44c757f1dabbb;hp=f21c5c2f02a8df7fef72aee3a3320a007877dd77;hpb=bcdf1637787154a7d20d06fefd235424fda1a1e4;p=NovacoinLibrary.git
diff --git a/Novacoin/Interop.cs b/Novacoin/Interop.cs
index f21c5c2..f4c1e2e 100644
--- a/Novacoin/Interop.cs
+++ b/Novacoin/Interop.cs
@@ -1,273 +1,74 @@
-using System;
+/**
+ * Novacoin classes library
+ * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero 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 Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+
+using System;
using System.Collections.Generic;
-using System.Linq;
using System.Text;
namespace Novacoin
{
- public class InteropException : Exception
- {
- public InteropException()
- {
- }
-
- public InteropException(string message)
- : base(message)
- {
- }
-
- public InteropException(string message, Exception inner)
- : base(message, inner)
- {
- }
- }
-
+ ///
+ /// Miscellaneous functions
+ ///
public class Interop
{
- public static byte[] ReverseIfLE(byte[] source)
+ ///
+ /// Reverse byte array
+ ///
+ /// Source array
+ /// Result array
+ public static byte[] ReverseBytes(byte[] source)
{
- if (BitConverter.IsLittleEndian)
- {
- Array.Reverse(source);
- }
+ var b = new byte[source.Length];
- return source;
- }
-
- public static byte[] LEBytes(uint[] values)
- {
- if (BitConverter.IsLittleEndian)
- {
- byte[] result = new byte[values.Length * sizeof(uint)];
- Buffer.BlockCopy(values, 0, result, 0, result.Length);
+ source.CopyTo(b, 0);
- return result;
- }
- else
- {
- List result = new List();
+ Array.Reverse(b);
- foreach (uint i in values)
- {
- result.AddRange(LEBytes(i));
- }
-
- return result.ToArray();
- }
+ return b;
}
- public static uint[] ToUInt32Array(byte[] bytes)
+ public static byte[] HexToArray(string hex)
{
- if (BitConverter.IsLittleEndian)
- {
- uint[] result = new uint[bytes.Length / sizeof(uint)];
- Buffer.BlockCopy(bytes, 0, result, 0, result.Length);
+ int nChars = hex.Length;
+ var bytes = new byte[nChars / 2];
- return result;
- }
- else
+ for (int i = 0; i < nChars; i += 2)
{
- List result = new List();
-
- for (int i = 0; i < bytes.Length; i += 4)
- {
- result.Add(LEBytesToUInt32(bytes.Skip(i).Take(4).ToArray()));
- }
-
- return result.ToArray();
+ bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
+ return bytes;
}
- public static byte[] LEBytes(ushort n)
+ public static string ToHex(byte[] bytes)
{
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (!BitConverter.IsLittleEndian)
- {
- // Reverse array if we are on big-endian machine
- Array.Reverse(resultBytes);
- }
-
- return resultBytes;
- }
-
- public static byte[] LEBytes(uint n)
- {
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (!BitConverter.IsLittleEndian)
- {
- // Reverse array if we are on big-endian machine
- Array.Reverse(resultBytes);
- }
-
- return resultBytes;
- }
-
- public static byte[] LEBytes(ulong n)
- {
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (!BitConverter.IsLittleEndian)
- {
- // Reverse array if we are on big-endian machine
- Array.Reverse(resultBytes);
- }
-
- return resultBytes;
- }
-
- public static byte[] BEBytes(ushort n)
- {
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (BitConverter.IsLittleEndian)
- {
- // Reverse array if we are on little-endian machine
- Array.Reverse(resultBytes);
- }
-
- return resultBytes;
- }
-
- public static byte[] BEBytes(uint n)
- {
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (BitConverter.IsLittleEndian)
- {
- // Reverse array if we are on little-endian machine
- Array.Reverse(resultBytes);
- }
-
- return resultBytes;
- }
-
- public static byte[] BEBytes(ulong n)
- {
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (BitConverter.IsLittleEndian)
+ var sb = new StringBuilder();
+ foreach (var b in bytes)
{
- // Reverse array if we are on little-endian machine
- Array.Reverse(resultBytes);
- }
-
- return resultBytes;
- }
-
- public static ushort LEBytesToUInt16(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 big-endian machine
- Array.Reverse(bytes);
- }
-
- return BitConverter.ToUInt16(bytes, 0);
- }
-
- public static uint LEBytesToUInt32(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 big-endian machine
- Array.Reverse(bytes);
- }
-
- return BitConverter.ToUInt32(bytes, 0);
- }
-
- public static ulong LEBytesToUInt64(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 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);
+ sb.AppendFormat("{0:x2}", b);
}
-
- return BitConverter.ToUInt64(bytes, 0);
- }
-
- public static IEnumerable ParseHex(string hex)
- {
- return Enumerable.Range(0, hex.Length)
- .Where(x => x % 2 == 0)
- .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
+ return sb.ToString();
}
- public static string ToHex(IEnumerable bytes)
+ public static uint GetTime()
{
- StringBuilder sb = new StringBuilder();
- foreach (byte b in bytes)
- {
- sb.AppendFormat("{0:x2}", b);
- }
- return sb.ToString();
+ return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
}
}
}