Remove unnecessary usings, rewrite pubkey serialization code
[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[] LEBytes(ushort n)
28         {
29             byte[] resultBytes = BitConverter.GetBytes(n);
30
31             if (!BitConverter.IsLittleEndian)
32             {
33                 // Reverse array if we are on big-endian machine
34                 Array.Reverse(resultBytes);
35             }
36
37             return resultBytes;
38         }
39
40         public static byte[] LEBytes(uint n)
41         {
42             byte[] resultBytes = BitConverter.GetBytes(n);
43
44             if (!BitConverter.IsLittleEndian)
45             {
46                 // Reverse array if we are on big-endian machine
47                 Array.Reverse(resultBytes);
48             }
49
50             return resultBytes;
51         }
52
53         public static byte[] LEBytes(ulong n)
54         {
55             byte[] resultBytes = BitConverter.GetBytes(n);
56
57             if (!BitConverter.IsLittleEndian)
58             {
59                 // Reverse array if we are on big-endian machine
60                 Array.Reverse(resultBytes);
61             }
62
63             return resultBytes;
64         }
65
66         public static byte[] BEBytes(ushort n)
67         {
68             byte[] resultBytes = BitConverter.GetBytes(n);
69
70             if (BitConverter.IsLittleEndian)
71             {
72                 // Reverse array if we are on little-endian machine
73                 Array.Reverse(resultBytes);
74             }
75
76             return resultBytes;
77         }
78
79         public static byte[] BEBytes(uint n)
80         {
81             byte[] resultBytes = BitConverter.GetBytes(n);
82
83             if (BitConverter.IsLittleEndian)
84             {
85                 // Reverse array if we are on little-endian machine
86                 Array.Reverse(resultBytes);
87             }
88
89             return resultBytes;
90         }
91
92         public static byte[] BEBytes(ulong n)
93         {
94             byte[] resultBytes = BitConverter.GetBytes(n);
95
96             if (BitConverter.IsLittleEndian)
97             {
98                 // Reverse array if we are on little-endian machine
99                 Array.Reverse(resultBytes);
100             }
101
102             return resultBytes;
103         }
104
105         public static ushort LEBytesToUInt16(byte[] bytes)
106         {
107             if (bytes.Length != sizeof(ushort))
108             {
109                 throw new InteropException("Array size doesn't match the ushort data type.");
110             }
111
112             if (!BitConverter.IsLittleEndian)
113             {
114                 // Reverse array if we are on big-endian machine
115                 Array.Reverse(bytes);
116             }
117
118             return BitConverter.ToUInt16(bytes, 0);
119         }
120
121         public static uint LEBytesToUInt32(byte[] bytes)
122         {
123             if (bytes.Length != sizeof(uint))
124             {
125                 throw new InteropException("Array size doesn't match the uint data type.");
126             }
127
128             if (!BitConverter.IsLittleEndian)
129             {
130                 // Reverse array if we are on big-endian machine
131                 Array.Reverse(bytes);
132             }
133
134             return BitConverter.ToUInt32(bytes, 0);
135         }
136
137         public static ulong LEBytesToUInt64(byte[] bytes)
138         {
139             if (bytes.Length != sizeof(ulong))
140             {
141                 throw new InteropException("Array size doesn't match the ulong data type.");
142             }
143
144             if (!BitConverter.IsLittleEndian)
145             {
146                 // Reverse array if we are on big-endian machine
147                 Array.Reverse(bytes);
148             }
149
150             return BitConverter.ToUInt64(bytes, 0);
151         }
152
153         public static ushort BEBytesToUInt16(byte[] bytes)
154         {
155             if (bytes.Length != sizeof(ushort))
156             {
157                 throw new InteropException("Array size doesn't match the ushort data type.");
158             }
159
160             if (BitConverter.IsLittleEndian)
161             {
162                 // Reverse array if we are on little-endian machine
163                 Array.Reverse(bytes);
164             }
165
166             return BitConverter.ToUInt16(bytes, 0);
167         }
168
169         public static uint BEBytesToUInt32(byte[] bytes)
170         {
171             if (bytes.Length != sizeof(uint))
172             {
173                 throw new InteropException("Array size doesn't match the uint data type.");
174             }
175
176             if (BitConverter.IsLittleEndian)
177             {
178                 // Reverse array if we are on little-endian machine
179                 Array.Reverse(bytes);
180             }
181
182             return BitConverter.ToUInt32(bytes, 0);
183         }
184
185         public static ulong BEBytesToUInt64(byte[] bytes)
186         {
187             if (bytes.Length != sizeof(ulong))
188             {
189                 throw new InteropException("Array size doesn't match the ulong data type.");
190             }
191
192             if (BitConverter.IsLittleEndian)
193             {
194                 // Reverse array if we are on little-endian machine
195                 Array.Reverse(bytes);
196             }
197
198             return BitConverter.ToUInt64(bytes, 0);
199         }
200
201         public static IEnumerable<byte> ParseHex(string hex)
202         {
203             return Enumerable.Range(0, hex.Length)
204                              .Where(x => x % 2 == 0)
205                              .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
206         }
207
208         public static string ToHex(IEnumerable<byte> bytes)
209         {
210             StringBuilder sb = new StringBuilder();
211             foreach (byte b in bytes)
212             {
213                 sb.AppendFormat("{0:x2}", b);
214             }
215             return sb.ToString();
216         }
217     }
218 }