This repository has been archived by the owner on Feb 8, 2018. It is now read-only.
forked from gluck/ikvm
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BigEndianBinaryReader.cs
227 lines (206 loc) · 5.26 KB
/
BigEndianBinaryReader.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
/*
Copyright (C) 2002, 2004 Jeroen Frijters
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Jeroen Frijters
jeroen@frijters.net
*/
using System;
using System.IO;
sealed class BigEndianBinaryReader
{
private byte[] buf;
private int pos;
private int end;
internal BigEndianBinaryReader(byte[] buf, int offset, int length)
{
this.buf = buf;
this.pos = offset;
this.end = checked(offset + length);
if(offset < 0 || length < 0 || buf.Length - offset < length)
{
throw new ClassFormatError("Truncated class file");
}
}
internal BigEndianBinaryReader Section(uint length)
{
BigEndianBinaryReader br = new BigEndianBinaryReader(buf, pos, checked((int)length));
Skip(length);
return br;
}
internal bool IsAtEnd
{
get
{
return pos == end;
}
}
internal int Position
{
get
{
return pos;
}
}
internal void Skip(uint count)
{
if(end - pos < count)
{
throw new ClassFormatError("Truncated class file");
}
checked
{
pos += (int)count;
}
}
internal byte ReadByte()
{
if(pos == end)
{
throw new ClassFormatError("Truncated class file");
}
return buf[pos++];
}
internal sbyte ReadSByte()
{
if(pos == end)
{
throw new ClassFormatError("Truncated class file");
}
return (sbyte)buf[pos++];
}
internal double ReadDouble()
{
return BitConverter.Int64BitsToDouble(ReadInt64());
}
internal short ReadInt16()
{
if(end - pos < 2)
{
throw new ClassFormatError("Truncated class file");
}
short s = (short)((buf[pos] << 8) + buf[pos + 1]);
pos += 2;
return s;
}
internal int ReadInt32()
{
if(end - pos < 4)
{
throw new ClassFormatError("Truncated class file");
}
int i = (int)((buf[pos] << 24) + (buf[pos + 1] << 16) + (buf[pos + 2] << 8) + buf[pos + 3]);
pos += 4;
return i;
}
internal long ReadInt64()
{
if(end - pos < 8)
{
throw new ClassFormatError("Truncated class file");
}
uint i1 = (uint)((buf[pos] << 24) + (buf[pos + 1] << 16) + (buf[pos + 2] << 8) + buf[pos + 3]);
uint i2 = (uint)((buf[pos + 4] << 24) + (buf[pos + 5] << 16) + (buf[pos + 6] << 8) + buf[pos + 7]);
long l = (((long)i1) << 32) + i2;
pos += 8;
return l;
}
internal float ReadSingle()
{
return BitConverter.ToSingle(BitConverter.GetBytes(ReadInt32()), 0);
}
internal string ReadString(string classFile)
{
int len = ReadUInt16();
if(end - pos < len)
{
throw new ClassFormatError("{0} (Truncated class file)", classFile);
}
// special code path for ASCII strings (which occur *very* frequently)
for(int j = 0; j < len; j++)
{
if(buf[pos + j] == 0 || buf[pos + j] >= 128)
{
// NOTE we *cannot* use System.Text.UTF8Encoding, because this is *not* compatible
// (esp. for embedded nulls)
char[] ch = new char[len];
int l = 0;
for(int i = 0; i < len; i++)
{
int c = buf[pos + i];
int char2, char3;
switch (c >> 4)
{
case 0:
if(c == 0)
{
throw new ClassFormatError("{0} (Illegal UTF8 string in constant pool)", classFile);
}
break;
case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = buf[pos + ++i];
if((char2 & 0xc0) != 0x80)
{
throw new ClassFormatError("{0} (Illegal UTF8 string in constant pool)", classFile);
}
c = (((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = buf[pos + ++i];
char3 = buf[pos + ++i];
if((char2 & 0xc0) != 0x80 || (char3 & 0xc0) != 0x80)
{
throw new ClassFormatError("{0} (Illegal UTF8 string in constant pool)", classFile);
}
c = (((c & 0x0F) << 12) | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));
break;
default:
throw new ClassFormatError("{0} (Illegal UTF8 string in constant pool)", classFile);
}
ch[l++] = (char)c;
}
pos += len;
return new String(ch, 0, l);
}
}
string s = System.Text.ASCIIEncoding.ASCII.GetString(buf, pos, len);
pos += len;
return s;
}
internal ushort ReadUInt16()
{
if(end - pos < 2)
{
throw new ClassFormatError("Truncated class file");
}
ushort s = (ushort)((buf[pos] << 8) + buf[pos + 1]);
pos += 2;
return s;
}
internal uint ReadUInt32()
{
if(end - pos < 4)
{
throw new ClassFormatError("Truncated class file");
}
uint i = (uint)((buf[pos] << 24) + (buf[pos + 1] << 16) + (buf[pos + 2] << 8) + buf[pos + 3]);
pos += 4;
return i;
}
}