X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FInterop.cs;h=54768cfe4a3f68f71df3cb9294d1a029a382ae17;hb=HEAD;hp=149774e0f8a287b226f21e7062458055be2ab3a9;hpb=b31df5639083b6ffdf8b9f74973c8233b6c05818;p=NovacoinLibrary.git
diff --git a/Novacoin/Interop.cs b/Novacoin/Interop.cs
index 149774e..54768cf 100644
--- a/Novacoin/Interop.cs
+++ b/Novacoin/Interop.cs
@@ -1,84 +1,120 @@
-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;
-using System.Threading.Tasks;
namespace Novacoin
{
- class Interop
+ ///
+ /// Miscellaneous functions
+ ///
+ public class Interop
{
- public static byte[] LEBytes(ushort n)
+ ///
+ /// Convert array of unsigned integers to array of bytes.
+ ///
+ /// Array of unsigned integer values.
+ /// Byte array
+ public static byte[] LEBytes(uint[] values)
{
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(resultBytes);
- }
+ var result = new byte[values.Length * sizeof(uint)];
+ Buffer.BlockCopy(values, 0, result, 0, result.Length);
- return resultBytes;
+ return result;
}
- public static byte[] LEBytes(uint n)
+ ///
+ /// Convert byte array to array of unsigned integers.
+ ///
+ /// Byte array.
+ /// Array of integers
+ public static uint[] ToUInt32Array(byte[] bytes)
{
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(resultBytes);
- }
+ var result = new uint[bytes.Length / sizeof(uint)];
+ Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
- return resultBytes;
+ return result;
}
- public static byte[] LEBytes(ulong n)
+ ///
+ /// Reverse byte array
+ ///
+ /// Source array
+ /// Result array
+ public static byte[] ReverseBytes(byte[] source)
{
- byte[] resultBytes = BitConverter.GetBytes(n);
+ var b = new byte[source.Length];
- if (!BitConverter.IsLittleEndian)
- {
- Array.Reverse(resultBytes);
- }
+ source.CopyTo(b, 0);
+
+ Array.Reverse(b);
- return resultBytes;
+ return b;
}
- public static byte[] BEBytes(ushort n)
+ public static byte[] HexToArray(string hex)
{
- byte[] resultBytes = BitConverter.GetBytes(n);
+ int nChars = hex.Length;
+ var bytes = new byte[nChars / 2];
- if (BitConverter.IsLittleEndian)
+ for (int i = 0; i < nChars; i += 2)
{
- Array.Reverse(resultBytes);
+ bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
- return resultBytes;
+ return bytes;
}
- public static byte[] BEBytes(uint n)
+ public static byte[] TrimArray(byte[] bytes)
{
- byte[] resultBytes = BitConverter.GetBytes(n);
-
- if (BitConverter.IsLittleEndian)
+ int trimStart = bytes.Length - 1;
+ while (trimStart >= 0 && bytes[trimStart] == 0)
{
- Array.Reverse(resultBytes);
+ trimStart--;
}
- return resultBytes;
+ return bytes.Take(trimStart + 1).ToArray();
}
- public static byte[] BEBytes(ulong n)
+ public static byte[] AppendWithZeros(byte[] bytes, int nTargetLen=32)
{
- byte[] resultBytes = BitConverter.GetBytes(n);
+ var result = new byte[nTargetLen];
+ bytes.CopyTo(result, 0);
+
+ return result;
+ }
- if (BitConverter.IsLittleEndian)
+ public static string ToHex(byte[] bytes)
+ {
+ var sb = new StringBuilder();
+ foreach (var b in bytes)
{
- Array.Reverse(resultBytes);
+ sb.AppendFormat("{0:x2}", b);
}
-
- return resultBytes;
+ return sb.ToString();
}
+ public static uint GetTime()
+ {
+ return (uint)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
+ }
}
}