-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBufferReader.cs
268 lines (204 loc) · 6.25 KB
/
BufferReader.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
using System;
using System.Runtime.InteropServices;
namespace IrbImgFormat
{
class BufferReader
{
private static Logging logging = new Logging("BufferReader");
/// <summary>
/// Union to cast a 4-byte int to a float
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct BinaryConvertIntToFloat
{
[FieldOffset(0)]
public float toFloat;
[FieldOffset(0)]
public int toInt;
}
/// <summary>
/// Union to cast a 8 byte long into a double
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct BinaryConvertLongToDouble
{
[FieldOffset(0)]
public double toDouble;
[FieldOffset(0)]
public long toLong;
}
byte[] data;
int offset = 0;
int dataLength = 0;
public BufferReader(byte[] data)
{
this.data = data;
dataLength = (data != null) ? data.Length : 0;
}
/// <summary>
/// is the buffer Pointer at the end of the buffer
/// </summary>
public bool Eof
{
get
{
return ((offset < 0) || (offset >= dataLength) || (data == null));
}
}
/// <summary>
/// Return the lenght of the buffer
/// </summary>
public int Length
{
get
{
return data.Length;
}
}
/// <summary>
/// Read a string from the the buffer
/// </summary>
public string ReadStr(int length, int offset)
{
if (length < 0) length = 0;
if (offset > -1) this.offset = offset;
string outVal = "";
if (this.offset < dataLength)
{
if ((this.offset + length) > dataLength)
{
length = dataLength - this.offset;
}
outVal = System.Text.Encoding.Default.GetString(data, this.offset, length);
}
else
{
if (length > 0)
{
logging.addWarning("ReadStr:EOF!");
length = 0;
}
}
if (length > 0) this.offset = this.offset + length;
return outVal;
}
/// <summary>
/// read a NULL terminated string from buffer
/// </summary>
public string ReadNullTerminatedString(int offset, int size)
{
string s = this.ReadStr(size, offset);
int pos = s.IndexOf('\0');
if (pos > 0)
{
return s.Substring(0, pos);
}
return s;
}
/// <summary>
/// Read 8 byte big ending long from buffer
/// </summary>
public long ReadLongBE(int offset = -1)
{
if (offset > -1) this.offset = offset;
if (this.offset < dataLength)
{
if ((this.offset + 8) > dataLength)
{
this.offset = dataLength;
return 0;
}
byte[] d = data;
int i = this.offset;
byte[] bytes = { d[i + 0], d[i + 1], d[i + 2], d[i + 3], d[i + 4], d[i + 5], d[i + 6], d[i + 7] };
return BitConverter.ToInt64(bytes, 0);
}
return 0;
}
/// <summary>
/// Read one byte from buffer
/// </summary>
public int ReadByte(int offset = -1)
{
if (offset > -1) this.offset = offset;
if (this.offset >= dataLength)
{
return 0;
}
if ((this.offset + 1) > dataLength)
{
this.offset = dataLength;
return 0;
}
var result = data[this.offset];
this.offset++;
return result;
}
/// <summary>
/// Read big ending word (2 Bytes) from buffer
/// </summary>
/// <param name="offset"></param>
/// <returns></returns>
public int ReadWordBE(int offset = -1)
{
if (offset > -1)
{
this.offset = offset;
}
if (this.offset >= dataLength)
{
return 0;
}
if ((this.offset + 2) > dataLength)
{
this.offset = dataLength;
return 0;
}
var result = (int)data[this.offset] + (data[this.offset + 1] << 8);
this.offset += 2;
return result;
}
/// <summary>
/// Read big-ending int from buffer
/// </summary>
public int ReadIntBE(int offset = -1)
{
if (offset > -1)
{
this.offset = offset;
}
if (this.offset >= dataLength)
{
return 0;
}
if ((this.offset + 4) > dataLength)
{
this.offset = dataLength;
return 0;
}
var result = (int)data[this.offset] + (data[this.offset + 1] << 8) + (data[this.offset + 2] << 16) + (data[this.offset + 3] << 24);
this.offset += 4;
return result;
}
/// <summary>
/// read double from buffer
/// </summary>
public double ReadDoubleBE(int length = 8, int offset = -1)
{
BinaryConvertLongToDouble converterLongDouble;
converterLongDouble.toDouble = 0.0; /// need to be set to avoid compiler errors
converterLongDouble.toLong = ReadLongBE(offset);
return converterLongDouble.toDouble;
}
/// <summary>
/// read float from buffer
/// </summary>
public float ReadSingleBE(int offset = -1)
{
BinaryConvertIntToFloat converterIntFloat;
converterIntFloat.toFloat = 0.0f; /// need to be set to avoid compiler errors
converterIntFloat.toInt = ReadIntBE(offset);
return converterIntFloat.toFloat;
}
}
}