2
Copyright (C) 2009 Jeroen Frijters
4
This software is provided 'as-is', without any express or implied
5
warranty. In no event will the authors be held liable for any damages
6
arising from the use of this software.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
10
freely, subject to the following restrictions:
12
1. The origin of this software must not be misrepresented; you must not
13
claim that you wrote the original software. If you use this software
14
in a product, an acknowledgment in the product documentation would be
15
appreciated but is not required.
16
2. Altered source versions must be plainly marked as such, and must not be
17
misrepresented as being the original software.
18
3. This notice may not be removed or altered from any source distribution.
25
using System.Collections.Generic;
28
namespace IKVM.Reflection.Reader
30
sealed class ByteReader
32
private byte[] buffer;
36
internal ByteReader(byte[] buffer, int offset, int length)
40
this.end = pos + length;
43
internal static ByteReader FromBlob(byte[] blobHeap, int blob)
45
ByteReader br = new ByteReader(blobHeap, blob, 4);
46
int length = br.ReadCompressedUInt();
47
br.end = br.pos + length;
53
get { return end - pos; }
56
internal byte PeekByte()
59
throw new BadImageFormatException();
63
internal byte ReadByte()
66
throw new BadImageFormatException();
70
internal byte[] ReadBytes(int count)
73
throw new BadImageFormatException();
74
if (end - pos < count)
75
throw new BadImageFormatException();
76
byte[] buf = new byte[count];
77
Buffer.BlockCopy(buffer, pos, buf, 0, count);
82
internal int ReadCompressedUInt()
89
else if ((b1 & 0xC0) == 0x80)
92
return ((b1 & 0x3F) << 8) | b2;
99
return ((b1 & 0x3F) << 24) + (b2 << 16) + (b3 << 8) + b4;
103
internal int ReadCompressedInt()
105
byte b1 = PeekByte();
106
int value = ReadCompressedUInt();
107
if ((value & 1) == 0)
117
return (value >> 1) - 0x40;
119
return (value >> 1) - 0x2000;
121
return (value >> 1) - 0x10000000;
126
internal string ReadString()
128
if (PeekByte() == 0xFF)
133
int length = ReadCompressedUInt();
134
string str = Encoding.UTF8.GetString(buffer, pos, length);
139
internal char ReadChar()
141
return (char)ReadInt16();
144
internal sbyte ReadSByte()
146
return (sbyte)ReadByte();
149
internal short ReadInt16()
152
throw new BadImageFormatException();
153
byte b1 = buffer[pos++];
154
byte b2 = buffer[pos++];
155
return (short)(b1 | (b2 << 8));
158
internal ushort ReadUInt16()
160
return (ushort)ReadInt16();
163
internal int ReadInt32()
166
throw new BadImageFormatException();
167
byte b1 = buffer[pos++];
168
byte b2 = buffer[pos++];
169
byte b3 = buffer[pos++];
170
byte b4 = buffer[pos++];
171
return (int)(b1 | (b2 << 8) | (b3 << 16) | (b4 << 24));
174
internal uint ReadUInt32()
176
return (uint)ReadInt32();
179
internal long ReadInt64()
181
ulong lo = ReadUInt32();
182
ulong hi = ReadUInt32();
183
return (long)(lo | (hi << 32));
186
internal ulong ReadUInt64()
188
return (ulong)ReadInt64();
191
internal float ReadSingle()
193
return SingleConverter.Int32BitsToSingle(ReadInt32());
196
internal double ReadDouble()
198
return BitConverter.Int64BitsToDouble(ReadInt64());
201
internal ByteReader Slice(int length)
203
if (end - pos < length)
204
throw new BadImageFormatException();
205
ByteReader br = new ByteReader(buffer, pos, length);
210
// NOTE this method only works if the original offset was aligned and for alignments that are a power of 2
211
internal void Align(int alignment)
214
pos = (pos + alignment) & ~alignment;