Interop fixes
[NovacoinLibrary.git] / Novacoin / Interop.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Novacoin
8 {
9     public class InteropException : Exception
10     {
11         public InteropException()
12         {
13         }
14
15         public InteropException(string message)
16             : base(message)
17         {
18         }
19
20         public InteropException(string message, Exception inner)
21             : base(message, inner)
22         {
23         }
24     }
25
26     class Interop
27     {
28         public static byte[] LEBytes(ushort n)
29         {
30             byte[] resultBytes = BitConverter.GetBytes(n);
31
32             if (!BitConverter.IsLittleEndian)
33             {
34                 // Reverse array if we are on big-endian machine
35                 Array.Reverse(resultBytes);
36             }
37
38             return resultBytes;
39         }
40
41         public static byte[] LEBytes(uint n)
42         {
43             byte[] resultBytes = BitConverter.GetBytes(n);
44
45             if (!BitConverter.IsLittleEndian)
46             {
47                 // Reverse array if we are on big-endian machine
48                 Array.Reverse(resultBytes);
49             }
50
51             return resultBytes;
52         }
53
54         public static byte[] LEBytes(ulong n)
55         {
56             byte[] resultBytes = BitConverter.GetBytes(n);
57
58             if (!BitConverter.IsLittleEndian)
59             {
60                 // Reverse array if we are on big-endian machine
61                 Array.Reverse(resultBytes);
62             }
63
64             return resultBytes;
65         }
66
67         public static byte[] BEBytes(ushort n)
68         {
69             byte[] resultBytes = BitConverter.GetBytes(n);
70
71             if (BitConverter.IsLittleEndian)
72             {
73                 // Reverse array if we are on little-endian machine
74                 Array.Reverse(resultBytes);
75             }
76
77             return resultBytes;
78         }
79
80         public static byte[] BEBytes(uint n)
81         {
82             byte[] resultBytes = BitConverter.GetBytes(n);
83
84             if (BitConverter.IsLittleEndian)
85             {
86                 // Reverse array if we are on little-endian machine
87                 Array.Reverse(resultBytes);
88             }
89
90             return resultBytes;
91         }
92
93         public static byte[] BEBytes(ulong n)
94         {
95             byte[] resultBytes = BitConverter.GetBytes(n);
96
97             if (BitConverter.IsLittleEndian)
98             {
99                 // Reverse array if we are on little-endian machine
100                 Array.Reverse(resultBytes);
101             }
102
103             return resultBytes;
104         }
105
106         public static ushort LEBytesToUInt16(byte[] bytes)
107         {
108             if (bytes.Length != sizeof(ushort))
109             {
110                 throw new InteropException("Array size doesn't match the ushort data type.");
111             }
112
113             if (!BitConverter.IsLittleEndian)
114             {
115                 // Reverse array if we are on big-endian machine
116                 Array.Reverse(bytes);
117             }
118
119             return BitConverter.ToUInt16(bytes, 0);
120         }
121
122         public static uint LEBytesToUInt32(byte[] bytes)
123         {
124             if (bytes.Length != sizeof(uint))
125             {
126                 throw new InteropException("Array size doesn't match the uint data type.");
127             }
128
129             if (!BitConverter.IsLittleEndian)
130             {
131                 // Reverse array if we are on big-endian machine
132                 Array.Reverse(bytes);
133             }
134
135             return BitConverter.ToUInt32(bytes, 0);
136         }
137
138         public static ulong LEBytesToUInt64(byte[] bytes)
139         {
140             if (bytes.Length != sizeof(ulong))
141             {
142                 throw new InteropException("Array size doesn't match the ulong data type.");
143             }
144
145             if (!BitConverter.IsLittleEndian)
146             {
147                 // Reverse array if we are on big-endian machine
148                 Array.Reverse(bytes);
149             }
150
151             return BitConverter.ToUInt64(bytes, 0);
152         }
153
154         public static ushort BEBytesToUInt16(byte[] bytes)
155         {
156             if (bytes.Length != sizeof(ushort))
157             {
158                 throw new InteropException("Array size doesn't match the ushort data type.");
159             }
160
161             if (BitConverter.IsLittleEndian)
162             {
163                 // Reverse array if we are on little-endian machine
164                 Array.Reverse(bytes);
165             }
166
167             return BitConverter.ToUInt16(bytes, 0);
168         }
169
170         public static uint BEBytesToUInt32(byte[] bytes)
171         {
172             if (bytes.Length != sizeof(uint))
173             {
174                 throw new InteropException("Array size doesn't match the uint data type.");
175             }
176
177             if (BitConverter.IsLittleEndian)
178             {
179                 // Reverse array if we are on little-endian machine
180                 Array.Reverse(bytes);
181             }
182
183             return BitConverter.ToUInt32(bytes, 0);
184         }
185
186         public static ulong BEBytesToUInt64(byte[] bytes)
187         {
188             if (bytes.Length != sizeof(ulong))
189             {
190                 throw new InteropException("Array size doesn't match the ulong data type.");
191             }
192
193             if (BitConverter.IsLittleEndian)
194             {
195                 // Reverse array if we are on little-endian machine
196                 Array.Reverse(bytes);
197             }
198
199             return BitConverter.ToUInt64(bytes, 0);
200         }
201
202     }
203 }