-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinUtils.cs
177 lines (158 loc) · 6.63 KB
/
BinUtils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LVLTool
{
public static class BinUtils
{
private static bool Find(byte[] hexNumber, long location, byte[] data)
{
int index = 0;
while (index < hexNumber.Length && (int)hexNumber[index] == (int)data[location + (long)index])
++index;
return index == hexNumber.Length;
}
private static bool Find(byte[] hexNumber, long location, List<byte> data)
{
int index = 0;
while (index < hexNumber.Length && (int)hexNumber[index] == (int)data[(int)(location + index)])
++index;
return index == hexNumber.Length;
}
/// <summary>
/// Searched through 'data' for the givenBytes.
/// </summary>
/// <param name="startLocation">Where to start in 'data'</param>
/// <param name="givenBytes">the stuff to look for</param>
/// <param name="data">the big data to look through</param>
/// <returns>location of the givenBytes, -1L if not found.</returns>
public static long GetLocationOfGivenBytes(long startLocation, byte[] givenBytes, byte[] data, long maxDistance)
{
long retVal = -1L;
long num = (long)(data.Length - givenBytes.Length);
for (long location = startLocation; location < num; ++location)
{
if (Find(givenBytes, location, data))
{
retVal = location;
break;
}
else if (maxDistance < location - startLocation)
break;
}
return retVal;
}
public static long GetLocationOfGivenBytes(long startLocation, byte[] givenBytes, List<byte> data, long maxDistance)
{
long retVal = -1L;
long num = (long)(data.Count - givenBytes.Length);
for (long location = startLocation; location < num; ++location)
{
if (Find(givenBytes, location, data))
{
retVal = location;
break;
}
else if (maxDistance < location - startLocation)
break;
}
return retVal;
}
public static string GetByteString(long location, byte[] data, int numBytes)
{
char c = '.';
if (location + numBytes > data.Length)
return "";
StringBuilder b = new StringBuilder(numBytes);
if (location < 0) location = 0;
for (long l = location; l < location + numBytes; l++)
{
if (data[l] != 0)
{
c = (char)data[l];
b.Append(c);
}
}
return b.ToString();
}
// The number/length is stored in reverse least significant --> most significant
// This function gets the number at the specified location and gives it to you good ;
public static UInt16 Get2ByteNumberAtLocation(int loc, List<byte> data)
{
byte b0, b1;
b0 = data[loc];
b1 = data[loc + 1];
UInt16 retVal = (UInt16)(b0 + (b1 << 8));
return retVal;
}
// The number/length is stored in reverse least significant --> most significant
// This function gets the number at the specified location and gives it to you good ;
public static uint GetNumberAtLocation(int loc, List<byte> data)
{
byte b0, b1, b2, b3;
b0 = data[loc];
b1 = data[loc + 1];
b2 = data[loc + 2];
b3 = data[loc + 3];
uint retVal = (uint)(b0 + (b1 << 8) + (b2 << 16) + (b3 << 24));
return retVal;
}
// The number/length is stored in reverse least significant --> most significant
// This function gets the number at the specified location and gives it to you good ;
public static uint GetNumberAtLocation(long loc, byte[] data)
{
byte b0, b1, b2, b3;
b0 = data[loc];
b1 = data[loc + 1];
b2 = data[loc + 2];
b3 = data[loc + 3];
uint retVal = (uint)( b0 + (b1 << 8) + (b2 << 16) + (b3 << 24));
return retVal;
}
// The number/length is stored in reverse least significant --> most significant
// This function sets the number at the specified location
public static void WriteNumberAtLocation(long loc, UInt32 num, byte[] data)
{
byte[] encodedNumber = EncodeNumber( (int) num);
data[loc] = encodedNumber[0];
data[loc + 1] = encodedNumber[1];
data[loc + 2] = encodedNumber[2];
data[loc + 3] = encodedNumber[3];
}
// The number/length is stored in reverse least significant --> most significant
// This function sets the number at the specified location
public static void WriteNumberAtLocation(int loc, UInt32 num, List<byte> data)
{
byte[] encodedNumber = EncodeNumber((int)num);
data[loc] = encodedNumber[0];
data[loc + 1] = encodedNumber[1];
data[loc + 2] = encodedNumber[2];
data[loc + 3] = encodedNumber[3];
}
// The number/length is stored in reverse least significant --> most significant
// This function sets the number at the specified location
public static void Write2ByteNumberAtLocation(int loc, UInt16 num, List<byte> data)
{
byte[] encodedNumber = EncodeNumber((int)num);
data[loc] = encodedNumber[0];
data[loc + 1] = encodedNumber[1];
}
public static byte[] EncodeNumber(int num)
{
byte[] retVal = new byte[4];
retVal[0] = (byte)(num & 0x000000FF); // last byte
retVal[1] = (byte)((num & 0x0000FF00) >> 8);
retVal[2] = (byte)((num & 0x00FF0000) >> 16);
retVal[3] = (byte)((num & 0xFF000000) >> 24); // first byte
return retVal;
}
public static bool BinCompare(byte[] littleData, byte[] bigData, long location)
{
for (long i = 0; i < littleData.Length; i++)
if (littleData[i] != bigData[i + location])
return false;
return true;
}
}
}