-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLittleEndianReader.cs
194 lines (164 loc) · 5.13 KB
/
LittleEndianReader.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
#region License GNU GPL
// FastLittleEndianReader.cs
//
// Copyright (C) 2012 - BehaviorIsManaged
//
// 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
#endregion
using System;
using System.IO;
using System.Text;
public unsafe class LittleEndianReader
{
private long position = 0;
private readonly byte[] buffer;
public byte[] Buffer
{
get { return buffer; }
}
public LittleEndianReader(byte[] buffer)
{
this.buffer = buffer;
}
public byte ReadByte()
{
fixed (byte* pbyte = &buffer[position++])
{
return *pbyte;
}
}
public sbyte ReadSByte()
{
fixed (byte* pbyte = &buffer[position++])
{
return (sbyte)*pbyte;
}
}
public long Position
{
get { return position; }
}
public long BytesAvailable
{
get { return buffer.Length - position; }
}
public short ReadShort()
{
var position = this.position;
this.position += 2;
fixed (byte* pbyte = &buffer[position])
{
return (short)((*pbyte) | (*(pbyte + 1) << 8)) ;
}
}
public int ReadInt()
{
var position = this.position;
this.position += 4;
fixed (byte* pbyte = &buffer[position])
{
return ( *pbyte ) | ( *( pbyte + 1 ) << 8 ) | ( *( pbyte + 2 ) << 16 ) | ( *( pbyte + 3 ) << 24 );
}
}
public long ReadLong()
{
var position = this.position;
this.position += 8;
fixed (byte* pbyte = &buffer[position])
{
int i1 = ( *pbyte ) | ( *( pbyte + 1 ) << 8 ) | ( *( pbyte + 2 ) << 16 ) | ( *( pbyte + 3 ) << 24 );
int i2 = ( *( pbyte + 4 ) ) | ( *( pbyte + 5 ) << 8 ) | ( *( pbyte + 6 ) << 16 ) | ( *( pbyte + 7 ) << 24 );
return (uint)i1 | ( (long)i2 << 32 );
}
}
public ushort ReadUShort()
{
return (ushort)ReadShort();
}
public uint ReadUInt()
{
return (uint)ReadInt();
}
public ulong ReadULong()
{
return (ulong)ReadLong();
}
public byte[] ReadBytes(int n)
{
var dst = new byte[n];
fixed (byte* pSrc = &buffer[position], pDst = dst)
{
byte* ps = pSrc;
byte* pd = pDst;
// Loop over the count in blocks of 4 bytes, copying an integer (4 bytes) at a time:
for (int i = 0; i < n / 4; i++)
{
*( (int*)pd ) = *( (int*)ps );
pd += 4;
ps += 4;
}
// Complete the copy by moving any bytes that weren't moved in blocks of 4:
for (int i = 0; i < n % 4; i++)
{
*pd = *ps;
pd++;
ps++;
}
}
position += n;
return dst;
}
public bool ReadBoolean()
{
return ReadByte() != 0;
}
public char ReadChar()
{
return (char)ReadShort();
}
public float ReadFloat()
{
int val = ReadInt();
return *(float*)&val;
}
public double ReadDouble()
{
long val = ReadLong();
return *(double*)&val;
}
public string ReadUTF()
{
ushort length = ReadUShort();
byte[] bytes = ReadBytes(length);
return Encoding.UTF8.GetString(bytes);
}
public string ReadUTFBytes(ushort len)
{
byte[] bytes = ReadBytes(len);
return Encoding.UTF8.GetString(bytes);
}
public void Seek(int offset, SeekOrigin seekOrigin)
{
if (seekOrigin == SeekOrigin.Begin)
position = offset;
else if (seekOrigin == SeekOrigin.End)
position = buffer.Length + offset;
else if (seekOrigin == SeekOrigin.Current)
position += offset;
}
public void SkipBytes(int n)
{
position += n;
}
public void Dispose()
{
}
}