/
Converters.cs
332 lines (289 loc) · 12 KB
/
Converters.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
///////////////////////////////////////////////////////////////////////
/// SharpHash Library
/// Copyright(c) 2019 - 2020 Mbadiwe Nnaemeka Ronald
/// Github Repository <https://github.com/ron4fun/SharpHash>
///
/// The contents of this file are subject to the
/// Mozilla Public License Version 2.0 (the "License");
/// you may not use this file except in
/// compliance with the License. You may obtain a copy of the License
/// at https://www.mozilla.org/en-US/MPL/2.0/
///
/// Software distributed under the License is distributed on an "AS IS"
/// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
/// the License for the specific language governing rights and
/// limitations under the License.
///
/// Acknowledgements:
///
/// Thanks to Ugochukwu Mmaduekwe (https://github.com/Xor-el) for his creative
/// development of this library in Pascal/Delphi (https://github.com/Xor-el/HashLib4Pascal).
///
/// Also, I will like to thank Udezue Chukwunwike (https://github.com/IzarchTech) for
/// his contributions to the growth and development of this library.
///
////////////////////////////////////////////////////////////////////////
using System;
using System.Globalization;
using System.Text;
namespace SharpHash.Utils
{
public static class Converters
{
public static unsafe void swap_copy_str_to_u32(IntPtr src, Int32 src_index,
IntPtr dest, Int32 dest_index, Int32 length)
{
UInt32* lsrc, ldest, lend;
byte* lbsrc;
Int32 lLength;
// if all pointers and length are 32-bits aligned
if ((((int)((byte*)(dest) - (byte*)(0)) | (int)((byte*)(src) - (byte*)(0)) | src_index |
dest_index | length) & 3) == 0)
{
// copy memory as 32-bit words
lsrc = (UInt32*)((byte*)(src) + src_index);
lend = (UInt32*)(((byte*)(src) + src_index) + length);
ldest = (UInt32*)((byte*)(dest) + dest_index);
while (lsrc < lend)
{
*ldest = Bits.ReverseBytesUInt32(*lsrc);
ldest += 1;
lsrc += 1;
} // end while
} // end if
else
{
lbsrc = ((byte*)(src) + src_index);
lLength = length + dest_index;
while (dest_index < lLength)
{
((byte*)dest)[dest_index ^ 3] = *lbsrc;
lbsrc += 1;
dest_index += 1;
} // end while
} // end else
} // end function swap_copy_str_to_u32
public static unsafe void swap_copy_str_to_u64(IntPtr src, Int32 src_index,
IntPtr dest, Int32 dest_index, Int32 length)
{
UInt64* lsrc, ldest, lend;
byte* lbsrc;
Int32 lLength;
// if all pointers and length are 64-bits aligned
if ((((Int32)((byte*)dest - (byte*)0) | (Int32)((byte*)src - (byte*)0) | src_index |
dest_index | length) & 7) == 0)
{
// copy aligned memory block as 64-bit integers
lsrc = (UInt64*)((byte*)src + src_index);
lend = (UInt64*)(((byte*)src + src_index) + length);
ldest = (UInt64*)((byte*)dest + dest_index);
while (lsrc < lend)
{
*ldest = Bits.ReverseBytesUInt64(*lsrc);
ldest += 1;
lsrc += 1;
} // end while
} // end if
else
{
lbsrc = ((byte*)src + src_index);
lLength = length + dest_index;
while (dest_index < lLength)
{
((byte*)dest)[dest_index ^ 7] = *lbsrc;
lbsrc += 1;
dest_index += 1;
} // end while
} // end else
} // end function swap_copy_str_to_u64
public static UInt32 be2me_32(UInt32 x)
{
if (BitConverter.IsLittleEndian)
{
return Bits.ReverseBytesUInt32(x);
} // end if
return x;
} // end function be2me_32
public static UInt64 be2me_64(UInt64 x)
{
if (BitConverter.IsLittleEndian)
{
return Bits.ReverseBytesUInt64(x);
} // end if
return x;
} // end function be2me_64
public static unsafe void be32_copy(IntPtr src, Int32 src_index,
IntPtr dest, Int32 dest_index, Int32 length)
{
if (BitConverter.IsLittleEndian)
{
swap_copy_str_to_u32(src, src_index, dest, dest_index, length);
} // end if
else
{
Utils.Memmove((IntPtr)((byte*)dest + dest_index), (IntPtr)((byte*)src + src_index), length);
} // end else
} // end function be32_copy
public static unsafe void be64_copy(IntPtr src, Int32 src_index,
IntPtr dest, Int32 dest_index, Int32 length)
{
if (BitConverter.IsLittleEndian)
{
swap_copy_str_to_u64(src, src_index, dest, dest_index, length);
} // end if
else
{
Utils.Memmove((IntPtr)((byte*)dest + dest_index), (IntPtr)((byte*)src + src_index), length);
} // end else
} // end function be64_copy
public static UInt32 le2me_32(Int32 x)
{
if (!BitConverter.IsLittleEndian)
{
return Bits.ReverseBytesUInt32((UInt32)(x));
} // end if
return (UInt32)x;
} // end function le2me_32
public static UInt64 le2me_64(UInt64 x)
{
if (!BitConverter.IsLittleEndian)
{
return Bits.ReverseBytesUInt64(x);
} // end if
return x;
} // end function le2me_64
public static unsafe void le32_copy(IntPtr src, Int32 src_index,
IntPtr dest, Int32 dest_index, Int32 length)
{
if (BitConverter.IsLittleEndian)
{
Utils.Memmove((IntPtr)((byte*)dest + dest_index), (IntPtr)((byte*)src + src_index), length);
} // end if
else
{
swap_copy_str_to_u32(src, src_index, dest, dest_index, length);
} // end else
} // end function le32_copy
public static unsafe void le64_copy(IntPtr src, Int32 src_index,
IntPtr dest, Int32 dest_index, Int32 length)
{
if (BitConverter.IsLittleEndian)
{
Utils.Memmove((IntPtr)((byte*)dest + dest_index), (IntPtr)((byte*)src + src_index), length);
} // end if
else
{
swap_copy_str_to_u64(src, src_index, dest, dest_index, length);
} // end else
} // end function le64_copy
public static unsafe UInt32 ReadBytesAsUInt32LE(IntPtr a_in, Int32 a_index)
{
UInt32 result = *(UInt32*)((byte*)a_in + a_index);
return Converters.le2me_32((Int32)result);
} // end function ReadBytesAsUInt32LE
public static unsafe UInt64 ReadBytesAsUInt64LE(IntPtr a_in, Int32 a_index)
{
UInt64 result = *(UInt64*)((byte*)a_in + a_index);
return Converters.le2me_64(result);
} // end function ReadBytesAsUInt64LE
public static byte[] ReadUInt32AsBytesLE(UInt32 a_in)
{
byte[] arr = new byte[4];
arr[0] = (byte)a_in;
arr[1] = (byte)(a_in >> 8);
arr[2] = (byte)(a_in >> 16);
arr[3] = (byte)(a_in >> 24);
return arr;
} // end function ReadUInt32AsBytesLE
public static void ReadUInt32AsBytesLE(UInt32 a_Input, ref byte[] a_Output, Int32 a_Index)
{
a_Output[a_Index] = (byte)(a_Input);
a_Output[a_Index + 1] = (byte)(a_Input >> 8);
a_Output[a_Index + 2] = (byte)(a_Input >> 16);
a_Output[a_Index + 3] = (byte)(a_Input >> 24);
} // end function ReadUInt32AsBytesLE
public static void ReadUInt32AsBytesBE(UInt32 a_Input, ref byte[] a_Output, Int32 a_Index)
{
a_Output[a_Index] = (byte)(a_Input >> 24);
a_Output[a_Index + 1] = (byte)(a_Input >> 16);
a_Output[a_Index + 2] = (byte)(a_Input >> 8);
a_Output[a_Index + 3] = (byte)(a_Input);
} // end function ReadUInt32AsBytesBE
public static byte[] ReadUInt64AsBytesLE(UInt64 a_in)
{
byte[] arr = new byte[8];
arr[0] = (byte)a_in;
arr[1] = (byte)(a_in >> 8);
arr[2] = (byte)(a_in >> 16);
arr[3] = (byte)(a_in >> 24);
arr[4] = (byte)(a_in >> 32);
arr[5] = (byte)(a_in >> 40);
arr[6] = (byte)(a_in >> 48);
arr[7] = (byte)(a_in >> 56);
return arr;
} // end function ReadUInt64AsBytesLE
public static void ReadUInt64AsBytesLE(UInt64 a_in, ref byte[] a_out, Int32 a_index)
{
a_out[a_index] = (byte)a_in;
a_out[a_index + 1] = (byte)(a_in >> 8);
a_out[a_index + 2] = (byte)(a_in >> 16);
a_out[a_index + 3] = (byte)(a_in >> 24);
a_out[a_index + 4] = (byte)(a_in >> 32);
a_out[a_index + 5] = (byte)(a_in >> 40);
a_out[a_index + 6] = (byte)(a_in >> 48);
a_out[a_index + 7] = (byte)(a_in >> 56);
} // end function ReadUInt64AsBytesLE
public static void ReadUInt64AsBytesBE(UInt64 a_in, ref byte[] a_out, Int32 a_index)
{
a_out[a_index] = (byte)(a_in >> 56);
a_out[a_index + 1] = (byte)(a_in >> 48);
a_out[a_index + 2] = (byte)(a_in >> 40);
a_out[a_index + 3] = (byte)(a_in >> 32);
a_out[a_index + 4] = (byte)(a_in >> 24);
a_out[a_index + 5] = (byte)(a_in >> 16);
a_out[a_index + 6] = (byte)(a_in >> 8);
a_out[a_index + 7] = (byte)a_in;
} // end function ReadUInt64AsBytesBE
public static unsafe string ConvertBytesToHexString(byte[] a_in, bool a_group, char delimeter = '-')
{
if (a_in == null || a_in.Length == 0) return "";
fixed (byte* bPtr = a_in)
{
return ConvertBytesToHexString((IntPtr)bPtr, (UInt32)a_in.Length, a_group, delimeter);
}
} // end function ConvertBytesToHexString
public static string ConvertBytesToHexString(IntPtr a_in, UInt32 size, bool a_group, char delimeter = '-')
{
string hex = ExtendedBitConverter.ToString(a_in, 0, (Int32)size, delimeter);
hex = hex.ToUpper();
if (size == 1)
{
return hex;
} // end if
if (size == 2)
{
return hex.Replace(delimeter.ToString(), "", true, CultureInfo.CurrentCulture);
} // end if
if (a_group) return hex;
return hex.Replace(delimeter.ToString(), "",
true, CultureInfo.CurrentCulture);
} // end function ConvertBytesToHexString
public static byte[] ConvertHexStringToBytes(string a_in, char delimeter = '-')
{
string l_in = a_in;
l_in = l_in.Replace(delimeter.ToString(), "");
byte[] result = new byte[l_in.Length >> 1];
for (Int32 i = 0, j = 0; i < l_in.Length; i += 2, j += 1)
{
string byteStr = l_in.Substring(i, 2);
result[j] = (byte)Convert.ToChar(Convert.ToUInt32(byteStr, 16));
} // end for
return result;
} // end function ConvertHexStringToBytes
public static byte[] ConvertStringToBytes(string a_in, Encoding encoding)
{
if (String.IsNullOrEmpty(a_in)) return new byte[0];
return encoding.GetBytes(a_in);
} // end function ConvertStringToBytes
}
}