-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStreamExtensions.cs
325 lines (303 loc) · 8.6 KB
/
StreamExtensions.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
/*
* I hereby release this file, StreamExtensions.cs, to the public domain.
* Use it if you wish.
*/
using System;
using System.Text;
using System.IO;
static class StreamExtensions
{
/// <summary>
/// Read a signed 8-bit integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static sbyte ReadInt8(this Stream s) => unchecked((sbyte)s.ReadUInt8());
/// <summary>
/// Read an unsigned 8-bit integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static byte ReadUInt8(this Stream s)
{
byte ret;
byte[] tmp = new byte[1];
s.Read(tmp, 0, 1);
ret = tmp[0];
return ret;
}
/// <summary>
/// Read an unsigned 16-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static ushort ReadUInt16LE(this Stream s) => unchecked((ushort)s.ReadInt16LE());
/// <summary>
/// Read a signed 16-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static short ReadInt16LE(this Stream s)
{
int ret;
byte[] tmp = new byte[2];
s.Read(tmp, 0, 2);
ret = tmp[0] & 0x00FF;
ret |= (tmp[1] << 8) & 0xFF00;
return (short)ret;
}
/// <summary>
/// Read an unsigned 16-bit Big-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static ushort ReadUInt16BE(this Stream s) => unchecked((ushort)s.ReadInt16BE());
/// <summary>
/// Read a signed 16-bit Big-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static short ReadInt16BE(this Stream s)
{
int ret;
byte[] tmp = new byte[2];
s.Read(tmp, 0, 2);
ret = (tmp[0] << 8) & 0xFF00;
ret |= tmp[1] & 0x00FF;
return (short)ret;
}
/// <summary>
/// Read an unsigned 24-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int ReadUInt24LE(this Stream s)
{
int ret;
byte[] tmp = new byte[3];
s.Read(tmp, 0, 3);
ret = tmp[0] & 0x0000FF;
ret |= (tmp[1] << 8) & 0x00FF00;
ret |= (tmp[2] << 16) & 0xFF0000;
return ret;
}
/// <summary>
/// Read a signed 24-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int ReadInt24LE(this Stream s)
{
int ret;
byte[] tmp = new byte[3];
s.Read(tmp, 0, 3);
ret = tmp[0] & 0x0000FF;
ret |= (tmp[1] << 8) & 0x00FF00;
ret |= (tmp[2] << 16) & 0xFF0000;
if ((tmp[2] & 0x80) == 0x80)
{
ret |= 0xFF << 24;
}
return ret;
}
/// <summary>
/// Read an unsigned 24-bit Big-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int ReadUInt24BE(this Stream s)
{
int ret;
byte[] tmp = new byte[3];
s.Read(tmp, 0, 3);
ret = tmp[2] & 0x0000FF;
ret |= (tmp[1] << 8) & 0x00FF00;
ret |= (tmp[0] << 16) & 0xFF0000;
return ret;
}
/// <summary>
/// Read a signed 24-bit Big-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int ReadInt24BE(this Stream s)
{
int ret;
byte[] tmp = new byte[3];
s.Read(tmp, 0, 3);
ret = tmp[2] & 0x0000FF;
ret |= (tmp[1] << 8) & 0x00FF00;
ret |= (tmp[0] << 16) & 0xFF0000;
if ((tmp[0] & 0x80) == 0x80)
{
ret |= 0xFF << 24; // sign-extend
}
return ret;
}
/// <summary>
/// Read an unsigned 32-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static uint ReadUInt32LE(this Stream s) => unchecked((uint)s.ReadInt32LE());
/// <summary>
/// Read a signed 32-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int ReadInt32LE(this Stream s)
{
int ret;
byte[] tmp = new byte[4];
s.Read(tmp, 0, 4);
ret = tmp[0] & 0x000000FF;
ret |= (tmp[1] << 8) & 0x0000FF00;
ret |= (tmp[2] << 16) & 0x00FF0000;
ret |= (tmp[3] << 24);
return ret;
}
/// <summary>
/// Read an unsigned 32-bit Big-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static uint ReadUInt32BE(this Stream s) => unchecked((uint)s.ReadInt32BE());
/// <summary>
/// Read a signed 32-bit Big-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int ReadInt32BE(this Stream s)
{
int ret;
byte[] tmp = new byte[4];
s.Read(tmp, 0, 4);
ret = (tmp[0] << 24);
ret |= (tmp[1] << 16) & 0x00FF0000;
ret |= (tmp[2] << 8) & 0x0000FF00;
ret |= tmp[3] & 0x000000FF;
return ret;
}
/// <summary>
/// Read an unsigned 64-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static ulong ReadUInt64LE(this Stream s) => unchecked((ulong)s.ReadInt64LE());
/// <summary>
/// Read a signed 64-bit little-endian integer from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static long ReadInt64LE(this Stream s)
{
long ret;
byte[] tmp = new byte[8];
s.Read(tmp, 0, 8);
ret = tmp[4] & 0x000000FFL;
ret |= (tmp[5] << 8) & 0x0000FF00L;
ret |= (tmp[6] << 16) & 0x00FF0000L;
ret |= (tmp[7] << 24) & 0xFF000000L;
ret <<= 32;
ret |= tmp[0] & 0x000000FFL;
ret |= (tmp[1] << 8) & 0x0000FF00L;
ret |= (tmp[2] << 16) & 0x00FF0000L;
ret |= (tmp[3] << 24) & 0xFF000000L;
return ret;
}
/// <summary>
/// Read a single-precision (4-byte) floating-point value from the stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static float ReadFloat(this Stream s)
{
byte[] tmp = new byte[4];
s.Read(tmp, 0, 4);
return BitConverter.ToSingle(tmp, 0);
}
/// <summary>
/// Read a null-terminated ASCII string from the given stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ReadASCIINullTerminated(this Stream s)
{
StringBuilder sb = new StringBuilder(255);
char cur;
while ((cur = (char)s.ReadByte()) != 0)
{
sb.Append(cur);
}
return sb.ToString();
}
/// <summary>
/// Read a length-prefixed string of the specified encoding type from the file.
/// The length is a 32-bit little endian integer.
/// </summary>
/// <param name="s"></param>
/// <param name="e">The encoding to use to decode the string.</param>
/// <returns></returns>
public static string ReadLengthPrefixedString(this Stream s, Encoding e)
{
int length = s.ReadInt32LE();
byte[] chars = new byte[length];
s.Read(chars, 0, length);
return e.GetString(chars);
}
/// <summary>
/// Read a length-prefixed UTF-8 string from the given stream.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ReadLengthUTF8(this Stream s)
{
return s.ReadLengthPrefixedString(Encoding.UTF8);
}
/// <summary>
/// Read a given number of bytes from a stream into a new byte array.
/// </summary>
/// <param name="s"></param>
/// <param name="count">Number of bytes to read (maximum)</param>
/// <returns>New byte array of size <=count.</returns>
public static byte[] ReadBytes(this Stream s, int count)
{
// Size of returned array at most count, at least difference between position and length.
int realCount = (int)((s.Position + count > s.Length) ? (s.Length - s.Position) : count);
byte[] ret = new byte[realCount];
s.Read(ret, 0, realCount);
return ret;
}
/// <summary>
/// Read a variable-length integral value as found in MIDI messages.
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static int ReadMidiMultiByte(this Stream s)
{
int ret = 0;
byte b = (byte)(s.ReadByte());
ret += b & 0x7f;
if (0x80 == (b & 0x80))
{
ret <<= 7;
b = (byte)(s.ReadByte());
ret += b & 0x7f;
if (0x80 == (b & 0x80))
{
ret <<= 7;
b = (byte)(s.ReadByte());
ret += b & 0x7f;
if (0x80 == (b & 0x80))
{
ret <<= 7;
b = (byte)(s.ReadByte());
ret += b & 0x7f;
if (0x80 == (b & 0x80))
throw new InvalidDataException("Variable-length MIDI number > 4 bytes");
}
}
}
return ret;
}
}