-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHelpers.cs
More file actions
104 lines (88 loc) · 3.02 KB
/
Copy pathHelpers.cs
File metadata and controls
104 lines (88 loc) · 3.02 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public static class Helpers
{
// Note this MODIFIES THE GIVEN ARRAY then returns a reference to the modified array.
public static byte[] Reverse(this byte[] b)
{
Array.Reverse(b);
return b;
}
public static UInt16 ReadUInt16BE(this BinaryReader binRdr)
{
return BitConverter.ToUInt16(binRdr.ReadBytesRequired(sizeof(UInt16)).Reverse(), 0);
}
public static Int16 ReadInt16BE(this BinaryReader binRdr)
{
return BitConverter.ToInt16(binRdr.ReadBytesRequired(sizeof(Int16)).Reverse(), 0);
}
public static UInt32 ReadUInt32BE(this BinaryReader binRdr)
{
return BitConverter.ToUInt32(binRdr.ReadBytesRequired(sizeof(UInt32)).Reverse(), 0);
}
public static Int32 ReadInt32BE(this BinaryReader binRdr)
{
return BitConverter.ToInt32(binRdr.ReadBytesRequired(sizeof(Int32)).Reverse(), 0);
}
public static UInt64 ReadUInt64BE(this BinaryReader binRdr)
{
return BitConverter.ToUInt64(binRdr.ReadBytesRequired(sizeof(UInt64)).Reverse(), 0);
}
public static Int64 ReadInt64BE(this BinaryReader binRdr)
{
return BitConverter.ToInt64(binRdr.ReadBytesRequired(sizeof(Int64)).Reverse(), 0);
}
public static byte[] ReadBytesRequired(this BinaryReader binRdr, int byteCount)
{
var result = binRdr.ReadBytes(byteCount);
if (result.Length != byteCount)
throw new EndOfStreamException(string.Format("{0} bytes required from stream, but only {1} returned.", byteCount, result.Length));
return result;
}
public static byte? PeekDataByte(this BinaryReader binRdr)
{
byte read = binRdr.ReadByte();
binRdr.BaseStream.Seek(-1, SeekOrigin.Current);
if ((read & 0x80) == 0x80)
{
return null;
}
return read;
}
public static byte ReadDataByte(this BinaryReader binRdr)
{
byte read = binRdr.ReadByte();
if ((read & 0x80) == 0x80)
throw new InvalidDataException("Invalid variable-length integer format: first byte should not have the continuation bit set.");
return read;
}
public static int ReadVariableInt32(this BinaryReader reader)
{
int value = 0;
for(int i = 0; i < 3; i++)
{
byte read = reader.ReadDataByte();
value += read << (i * 6);
if ((read & 0x40) != 0x40)
return value;
}
throw new EndOfStreamException($"Invalid byte sequence at offset {reader.BaseStream.Position}");
}
public static int ReadVariableInt32Ex(this BinaryReader reader)
{
int value = 0;
while (true)
{
byte? b = reader.PeekDataByte();
if (b == null)
break;
if (b == 0)
return value; //EOT
value = value + reader.ReadVariableInt32();
}
return value;
}
}