Use byte[] instead of IEnumerable<byte> if possible
[NovacoinLibrary.git] / Novacoin / Interop.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Collections.Generic;
21 using System.Linq;
22 using System.Text;
23
24 namespace Novacoin
25 {
26     public class InteropException : Exception
27     {
28         public InteropException()
29         {
30         }
31
32         public InteropException(string message)
33             : base(message)
34         {
35         }
36
37         public InteropException(string message, Exception inner)
38             : base(message, inner)
39         {
40         }
41     }
42
43     public class Interop
44     {
45         public static byte[] ReverseBytes(byte[] source)
46         {
47             var b = new byte[source.Length];
48
49             source.CopyTo(b, 0);
50
51             Array.Reverse(b);
52
53             return b;
54         }
55
56         public static byte[] LEBytes(uint[] values)
57         {
58             var result = new byte[values.Length * sizeof(uint)];
59             Buffer.BlockCopy(values, 0, result, 0, result.Length);
60
61             return result;
62         }
63
64         public static uint[] ToUInt32Array(byte[] bytes)
65         {
66             var result = new uint[bytes.Length / sizeof(uint)];
67             Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
68
69             return result;
70         }
71
72         public static byte[] BEBytes(ushort n)
73         {
74             var resultBytes = BitConverter.GetBytes(n);
75
76             Array.Reverse(resultBytes);
77
78             return resultBytes;
79         }
80
81         public static byte[] BEBytes(uint n)
82         {
83             var resultBytes = BitConverter.GetBytes(n);
84
85             Array.Reverse(resultBytes);
86
87             return resultBytes;
88         }
89
90         public static byte[] BEBytes(ulong n)
91         {
92             var resultBytes = BitConverter.GetBytes(n);
93
94             Array.Reverse(resultBytes);
95
96             return resultBytes;
97         }
98
99
100         public static ushort BEBytesToUInt16(byte[] bytes)
101         {
102             if (bytes.Length != sizeof(ushort))
103             {
104                 throw new InteropException("Array size doesn't match the ushort data type.");
105             }
106
107             Array.Reverse(bytes);
108
109             return BitConverter.ToUInt16(bytes, 0);
110         }
111
112         public static uint BEBytesToUInt32(byte[] bytes)
113         {
114             if (bytes.Length != sizeof(uint))
115             {
116                 throw new InteropException("Array size doesn't match the uint data type.");
117             }
118
119             Array.Reverse(bytes);
120
121             return BitConverter.ToUInt32(bytes, 0);
122         }
123
124         public static ulong BEBytesToUInt64(byte[] bytes)
125         {
126             if (bytes.Length != sizeof(ulong))
127             {
128                 throw new InteropException("Array size doesn't match the ulong data type.");
129             }
130
131             Array.Reverse(bytes);
132
133             return BitConverter.ToUInt64(bytes, 0);
134         }
135
136         public static IEnumerable<byte> HexToEnumerable(string hex)
137         {
138             return Enumerable.Range(0, hex.Length)
139                              .Where(x => x % 2 == 0)
140                              .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
141         }
142
143         public static IList<byte> HexToList(string hex)
144         {
145             return HexToEnumerable(hex).ToList();
146         }
147
148         public static byte[] HexToArray(string hex)
149         {
150             return HexToEnumerable(hex).ToArray();
151         }
152
153         public static string ToHex(IEnumerable<byte> bytes)
154         {
155             var sb = new StringBuilder();
156             foreach (var b in bytes)
157             {
158                 sb.AppendFormat("{0:x2}", b);
159             }
160             return sb.ToString();
161         }
162     }
163 }