Use temporary array, just to avoid the possibility of some unexpected results
[NovacoinLibrary.git] / Novacoin / Interop.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Novacoin
7 {
8     public class InteropException : Exception
9     {
10         public InteropException()
11         {
12         }
13
14         public InteropException(string message)
15             : base(message)
16         {
17         }
18
19         public InteropException(string message, Exception inner)
20             : base(message, inner)
21         {
22         }
23     }
24
25     public class Interop
26     {
27         public static byte[] ReverseBytes(byte[] source)
28         {
29             byte[] b = new byte[source.Length];
30
31             source.CopyTo(b, 0);
32
33             Array.Reverse(b);
34
35             return b;
36         }
37
38         public static byte[] LEBytes(uint[] values)
39         {
40             byte[] result = new byte[values.Length * sizeof(uint)];
41             Buffer.BlockCopy(values, 0, result, 0, result.Length);
42
43             return result;
44         }
45
46         public static uint[] ToUInt32Array(byte[] bytes)
47         {
48             uint[] result = new uint[bytes.Length / sizeof(uint)];
49             Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
50
51             return result;
52         }
53
54         public static byte[] BEBytes(ushort n)
55         {
56             byte[] resultBytes = BitConverter.GetBytes(n);
57
58             Array.Reverse(resultBytes);
59
60             return resultBytes;
61         }
62
63         public static byte[] BEBytes(uint n)
64         {
65             byte[] resultBytes = BitConverter.GetBytes(n);
66
67             Array.Reverse(resultBytes);
68
69             return resultBytes;
70         }
71
72         public static byte[] BEBytes(ulong n)
73         {
74             byte[] resultBytes = BitConverter.GetBytes(n);
75
76             Array.Reverse(resultBytes);
77
78             return resultBytes;
79         }
80
81
82         public static ushort BEBytesToUInt16(byte[] bytes)
83         {
84             if (bytes.Length != sizeof(ushort))
85             {
86                 throw new InteropException("Array size doesn't match the ushort data type.");
87             }
88
89             Array.Reverse(bytes);
90
91             return BitConverter.ToUInt16(bytes, 0);
92         }
93
94         public static uint BEBytesToUInt32(byte[] bytes)
95         {
96             if (bytes.Length != sizeof(uint))
97             {
98                 throw new InteropException("Array size doesn't match the uint data type.");
99             }
100
101             Array.Reverse(bytes);
102
103             return BitConverter.ToUInt32(bytes, 0);
104         }
105
106         public static ulong BEBytesToUInt64(byte[] bytes)
107         {
108             if (bytes.Length != sizeof(ulong))
109             {
110                 throw new InteropException("Array size doesn't match the ulong data type.");
111             }
112
113             Array.Reverse(bytes);
114
115             return BitConverter.ToUInt64(bytes, 0);
116         }
117
118         public static IEnumerable<byte> ParseHex(string hex)
119         {
120             return Enumerable.Range(0, hex.Length)
121                              .Where(x => x % 2 == 0)
122                              .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
123         }
124
125         public static string ToHex(IEnumerable<byte> bytes)
126         {
127             StringBuilder sb = new StringBuilder();
128             foreach (byte b in bytes)
129             {
130                 sb.AppendFormat("{0:x2}", b);
131             }
132             return sb.ToString();
133         }
134     }
135 }