forked from Dawn-of-Light/PacketLogConverter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarshal.cs
328 lines (307 loc) · 10.3 KB
/
Marshal.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
/*
* DAWN OF LIGHT - The first free open source DAoC server emulator
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
using System.Text;
namespace PacketLogConverter
{
/// <summary>
/// Provides basic functionality to convert data types
/// </summary>
public sealed class Marshal
{
/// <summary>
/// Converts a byte c-style string byte-array
/// to a c# string
/// </summary>
/// <param name="cstyle">the bytes</param>
/// <returns>the string</returns>
public static string ConvertToString(byte[] cstyle)
{
if(cstyle==null)
return null;
for (int i = 0; i < cstyle.Length; i++)
{
if (cstyle[i] == 0)
return Encoding.Default.GetString(cstyle, 0, i);
}
return Encoding.Default.GetString(cstyle);
}
/// <summary>
/// Converts 8 bytes to an long value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <returns>the long value</returns>
public static long ConvertToInt64(byte[] val)
{
return ConvertToInt64(val, 0);
}
/// <summary>
/// Converts 8 bytes to an long value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <param name="startIndex">where to read the values from</param>
/// <returns>the long value</returns>
public static long ConvertToInt64(byte[] val, int startIndex)
{
return ConvertToInt64(val[startIndex], val[startIndex+1], val[startIndex+2], val[startIndex+3], val[startIndex+4], val[startIndex+5], val[startIndex+6], val[startIndex+7]);
}
/// <summary>
/// Converts 8 bytes to an long value
/// in high to low order
/// </summary>
/// <param name="v1">the first bytes</param>
/// <param name="v2">the second bytes</param>
/// <param name="v3">the third bytes</param>
/// <param name="v4">the fourth bytes</param>
/// <returns>the long value</returns>
public static long ConvertToInt64(byte v1, byte v2, byte v3, byte v4, byte v5, byte v6, byte v7, byte v8)
{
long result = v1;
result = result << 8 | v2;
result = result << 8 | v3;
result = result << 8 | v4;
result = result << 8 | v5;
result = result << 8 | v6;
result = result << 8 | v7;
result = result << 8 | v8;
return result;
// return (long)((v1 << 56) | (v2 << 48) | (v3 << 40) | (v4 << 32) | (v5 << 24) | (v6 << 16) | (v7 << 8) | v8);
}
/// <summary>
/// Converts 8 bytes to an unsigned long value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <returns>the long value</returns>
public static ulong ConvertToUInt64(byte[] val)
{
return ConvertToUInt64(val, 0);
}
/// <summary>
/// Converts 8 bytes to an unsigned long value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <param name="startIndex">where to read the values from</param>
/// <returns>the long value</returns>
public static ulong ConvertToUInt64(byte[] val, int startIndex)
{
return ConvertToUInt64(val[startIndex], val[startIndex+1], val[startIndex+2], val[startIndex+3], val[startIndex+4], val[startIndex+5], val[startIndex+6], val[startIndex+7]);
}
/// <summary>
/// Converts 8 bytes to an unsigned long value
/// in high to low order
/// </summary>
/// <param name="v1">the first bytes</param>
/// <param name="v2">the second bytes</param>
/// <param name="v3">the third bytes</param>
/// <param name="v4">the fourth bytes</param>
/// <returns>the long value</returns>
public static ulong ConvertToUInt64(byte v1, byte v2, byte v3, byte v4, byte v5, byte v6, byte v7, byte v8)
{
long result = v1;
result = result << 8 | v2;
result = result << 8 | v3;
result = result << 8 | v4;
result = result << 8 | v5;
result = result << 8 | v6;
result = result << 8 | v7;
result = result << 8 | v8;
return (ulong)result;
// return (ulong)((v1 << 56) | (v2 << 48) | (v3 << 40) | (v4 << 32) | (v5 << 24) | (v6 << 16) | (v7 << 8) | v8);
}
/// <summary>
/// Converts 4 bytes to an integer value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <returns>the integer value</returns>
public static int ConvertToInt32(byte[] val)
{
return ConvertToInt32(val,0);
}
/// <summary>
/// Converts 4 bytes to an integer value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <param name="startIndex">where to read the values from</param>
/// <returns>the integer value</returns>
public static int ConvertToInt32(byte[] val, int startIndex)
{
return ConvertToInt32(val[startIndex], val[startIndex+1], val[startIndex+2], val[startIndex+3]);
}
/// <summary>
/// Converts 4 bytes to an integer value
/// in high to low order
/// </summary>
/// <param name="v1">the first bytes</param>
/// <param name="v2">the second bytes</param>
/// <param name="v3">the third bytes</param>
/// <param name="v4">the fourth bytes</param>
/// <returns>the integer value</returns>
public static int ConvertToInt32(byte v1, byte v2, byte v3, byte v4)
{
return (int)((v1 << 24) | (v2 << 16) | (v3 << 8) | v4);
}
/// <summary>
/// Converts 4 bytes to an unsigned integer value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <returns>the integer value</returns>
public static uint ConvertToUInt32(byte[] val)
{
return ConvertToUInt32(val,0);
}
/// <summary>
/// Converts 4 bytes to an unsigned integer value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <param name="startIndex">where to read the values from</param>
/// <returns>the integer value</returns>
public static uint ConvertToUInt32(byte[] val, int startIndex)
{
return ConvertToUInt32(val[startIndex], val[startIndex+1], val[startIndex+2], val[startIndex+3]);
}
/// <summary>
/// Converts 4 bytes to an unsigned integer value
/// in high to low order
/// </summary>
/// <param name="v1">the first bytes</param>
/// <param name="v2">the second bytes</param>
/// <param name="v3">the third bytes</param>
/// <param name="v4">the fourth bytes</param>
/// <returns>the integer value</returns>
public static uint ConvertToUInt32(byte v1, byte v2, byte v3, byte v4)
{
return (uint)((v1 << 24) | (v2 << 16) | (v3 << 8) | v4);
}
/// <summary>
/// Converts 2 bytes to an short value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <returns>the integer value</returns>
public static short ConvertToInt16(byte[] val)
{
return ConvertToInt16(val,0);
}
/// <summary>
/// Converts 2 bytes to an short value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <param name="startIndex">where to read the values from</param>
/// <returns>the integer value</returns>
public static short ConvertToInt16(byte[] val, int startIndex)
{
return ConvertToInt16(val[startIndex], val[startIndex+1]);
}
/// <summary>
/// Converts 2 bytes to an short value
/// in high to low order
/// </summary>
/// <param name="v1">the first bytes</param>
/// <param name="v2">the second bytes</param>
/// <returns>the integer value</returns>
public static short ConvertToInt16(byte v1, byte v2)
{
return (short)((v1 << 24) | (v2 << 16));
}
/// <summary>
/// Converts 2 bytes to an unsigned short value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <returns>the integer value</returns>
public static ushort ConvertToUInt16(byte[] val)
{
return ConvertToUInt16(val,0);
}
/// <summary>
/// Converts 2 bytes to an unsigned short value
/// in high to low order
/// </summary>
/// <param name="val">the bytes</param>
/// <param name="startIndex">where to read the values from</param>
/// <returns>the integer value</returns>
public static ushort ConvertToUInt16(byte[] val, int startIndex)
{
return ConvertToUInt16(val[startIndex], val[startIndex+1]);
}
/// <summary>
/// Converts 2 bytes to an integer value
/// in high to low order
/// </summary>
/// <param name="v1">the first bytes</param>
/// <param name="v2">the second bytes</param>
/// <returns>the integer value</returns>
public static ushort ConvertToUInt16(byte v1, byte v2)
{
return (ushort)((v2 & 0xff) | (v1 & 0xff) << 8);
}
/// <summary>
/// Converts a byte array into a hex dump
/// </summary>
/// <param name="description">Dump description</param>
/// <param name="dump">byte array</param>
/// <returns>the converted hex dump</returns>
public static string ToHexDump(string description, byte[] dump)
{
StringBuilder hexDump = new StringBuilder(description+"\n");
for(int i=0; i<dump.Length; i+=16)
{
StringBuilder text = new StringBuilder();
StringBuilder hex = new StringBuilder();
hex.Append(i.ToString("X4"));
hex.Append(": ");
for(int j=0; j<16; j++)
{
if(j+i < dump.Length)
{
byte val = dump[j+i];
hex.Append(dump[j+i].ToString("X2"));
hex.Append(" ");
if (val>=32 && val<=127)
{
text.Append((char)val);
}
else
{
text.Append(".");
}
}
else
{
hex.Append(" ");
text.Append(" ");
}
}
hex.Append(" ");
hex.Append(text.ToString());
hex.Append('\n');
hexDump.Append(hex.ToString());
}
return hexDump.ToString();
}
}
}