Salsa20 + wrapper stub
[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[] ReverseIfLE(byte[] source)
28         {
29             if (BitConverter.IsLittleEndian)
30             {
31                 Array.Reverse(source);
32             }
33
34             return source;
35         }
36
37         public static byte[] LEBytes(uint[] values)
38         {
39             if (BitConverter.IsLittleEndian)
40             {
41                 byte[] result = new byte[values.Length * sizeof(uint)];
42                 Buffer.BlockCopy(values, 0, result, 0, result.Length);
43
44                 return result;
45             }
46             else
47             {
48                 List<byte> result = new List<byte>();
49
50                 foreach (uint i in values)
51                 {
52                     result.AddRange(LEBytes(i));
53                 }
54
55                 return result.ToArray();
56             }
57         }
58
59         public static uint[] ToUInt32Array(byte[] bytes)
60         {
61             if (BitConverter.IsLittleEndian)
62             {
63                 uint[] result = new uint[bytes.Length / sizeof(uint)];
64                 Buffer.BlockCopy(bytes, 0, result, 0, result.Length);
65
66                 return result;
67             }
68             else
69             {
70                 List<uint> result = new List<uint>();
71
72                 for (int i = 0; i < bytes.Length; i += 4)
73                 {
74                     result.Add(LEBytesToUInt32(bytes.Skip(i).Take(4).ToArray()));
75                 }
76
77                 return result.ToArray();
78             }
79
80         }
81
82         public static byte[] LEBytes(ushort n)
83         {
84             byte[] resultBytes = BitConverter.GetBytes(n);
85
86             if (!BitConverter.IsLittleEndian)
87             {
88                 // Reverse array if we are on big-endian machine
89                 Array.Reverse(resultBytes);
90             }
91
92             return resultBytes;
93         }
94
95         public static byte[] LEBytes(uint n)
96         {
97             byte[] resultBytes = BitConverter.GetBytes(n);
98
99             if (!BitConverter.IsLittleEndian)
100             {
101                 // Reverse array if we are on big-endian machine
102                 Array.Reverse(resultBytes);
103             }
104
105             return resultBytes;
106         }
107
108         public static byte[] LEBytes(ulong n)
109         {
110             byte[] resultBytes = BitConverter.GetBytes(n);
111
112             if (!BitConverter.IsLittleEndian)
113             {
114                 // Reverse array if we are on big-endian machine
115                 Array.Reverse(resultBytes);
116             }
117
118             return resultBytes;
119         }
120
121         public static byte[] BEBytes(ushort n)
122         {
123             byte[] resultBytes = BitConverter.GetBytes(n);
124
125             if (BitConverter.IsLittleEndian)
126             {
127                 // Reverse array if we are on little-endian machine
128                 Array.Reverse(resultBytes);
129             }
130
131             return resultBytes;
132         }
133
134         public static byte[] BEBytes(uint n)
135         {
136             byte[] resultBytes = BitConverter.GetBytes(n);
137
138             if (BitConverter.IsLittleEndian)
139             {
140                 // Reverse array if we are on little-endian machine
141                 Array.Reverse(resultBytes);
142             }
143
144             return resultBytes;
145         }
146
147         public static byte[] BEBytes(ulong n)
148         {
149             byte[] resultBytes = BitConverter.GetBytes(n);
150
151             if (BitConverter.IsLittleEndian)
152             {
153                 // Reverse array if we are on little-endian machine
154                 Array.Reverse(resultBytes);
155             }
156
157             return resultBytes;
158         }
159
160         public static ushort LEBytesToUInt16(byte[] bytes)
161         {
162             if (bytes.Length != sizeof(ushort))
163             {
164                 throw new InteropException("Array size doesn't match the ushort data type.");
165             }
166
167             if (!BitConverter.IsLittleEndian)
168             {
169                 // Reverse array if we are on big-endian machine
170                 Array.Reverse(bytes);
171             }
172
173             return BitConverter.ToUInt16(bytes, 0);
174         }
175
176         public static uint LEBytesToUInt32(byte[] bytes)
177         {
178             if (bytes.Length != sizeof(uint))
179             {
180                 throw new InteropException("Array size doesn't match the uint data type.");
181             }
182
183             if (!BitConverter.IsLittleEndian)
184             {
185                 // Reverse array if we are on big-endian machine
186                 Array.Reverse(bytes);
187             }
188
189             return BitConverter.ToUInt32(bytes, 0);
190         }
191
192         public static ulong LEBytesToUInt64(byte[] bytes)
193         {
194             if (bytes.Length != sizeof(ulong))
195             {
196                 throw new InteropException("Array size doesn't match the ulong data type.");
197             }
198
199             if (!BitConverter.IsLittleEndian)
200             {
201                 // Reverse array if we are on big-endian machine
202                 Array.Reverse(bytes);
203             }
204
205             return BitConverter.ToUInt64(bytes, 0);
206         }
207
208         public static ushort BEBytesToUInt16(byte[] bytes)
209         {
210             if (bytes.Length != sizeof(ushort))
211             {
212                 throw new InteropException("Array size doesn't match the ushort data type.");
213             }
214
215             if (BitConverter.IsLittleEndian)
216             {
217                 // Reverse array if we are on little-endian machine
218                 Array.Reverse(bytes);
219             }
220
221             return BitConverter.ToUInt16(bytes, 0);
222         }
223
224         public static uint BEBytesToUInt32(byte[] bytes)
225         {
226             if (bytes.Length != sizeof(uint))
227             {
228                 throw new InteropException("Array size doesn't match the uint data type.");
229             }
230
231             if (BitConverter.IsLittleEndian)
232             {
233                 // Reverse array if we are on little-endian machine
234                 Array.Reverse(bytes);
235             }
236
237             return BitConverter.ToUInt32(bytes, 0);
238         }
239
240         public static ulong BEBytesToUInt64(byte[] bytes)
241         {
242             if (bytes.Length != sizeof(ulong))
243             {
244                 throw new InteropException("Array size doesn't match the ulong data type.");
245             }
246
247             if (BitConverter.IsLittleEndian)
248             {
249                 // Reverse array if we are on little-endian machine
250                 Array.Reverse(bytes);
251             }
252
253             return BitConverter.ToUInt64(bytes, 0);
254         }
255
256         public static IEnumerable<byte> ParseHex(string hex)
257         {
258             return Enumerable.Range(0, hex.Length)
259                              .Where(x => x % 2 == 0)
260                              .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
261         }
262
263         public static string ToHex(IEnumerable<byte> bytes)
264         {
265             StringBuilder sb = new StringBuilder();
266             foreach (byte b in bytes)
267             {
268                 sb.AppendFormat("{0:x2}", b);
269             }
270             return sb.ToString();
271         }
272     }
273 }