| 1 |
/* |
| 2 |
Copyright (C) 2009 Jeroen Frijters |
| 3 |
|
| 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. |
| 7 |
|
| 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: |
| 11 |
|
| 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. |
| 19 |
|
| 20 |
Jeroen Frijters |
| 21 |
[email protected] |
| 22 |
|
| 23 |
*/ |
| 24 |
using System; |
| 25 |
using System.Collections.Generic; |
| 26 |
using System.Text; |
| 27 |
|
| 28 |
namespace IKVM.Reflection.Reader |
| 29 |
{ |
| 30 |
sealed class ByteReader |
| 31 |
{ |
| 32 |
private byte[] buffer; |
| 33 |
private int pos; |
| 34 |
private int end; |
| 35 |
|
| 36 |
internal ByteReader(byte[] buffer, int offset, int length) |
| 37 |
{ |
| 38 |
this.buffer = buffer; |
| 39 |
this.pos = offset; |
| 40 |
this.end = pos + length; |
| 41 |
} |
| 42 |
|
| 43 |
internal static ByteReader FromBlob(byte[] blobHeap, int blob) |
| 44 |
{ |
| 45 |
ByteReader br = new ByteReader(blobHeap, blob, 4); |
| 46 |
int length = br.ReadCompressedUInt(); |
| 47 |
br.end = br.pos + length; |
| 48 |
return br; |
| 49 |
} |
| 50 |
|
| 51 |
internal int Length |
| 52 |
{ |
| 53 |
get { return end - pos; } |
| 54 |
} |
| 55 |
|
| 56 |
internal byte PeekByte() |
| 57 |
{ |
| 58 |
if (pos == end) |
| 59 |
throw new BadImageFormatException(); |
| 60 |
return buffer[pos]; |
| 61 |
} |
| 62 |
|
| 63 |
internal byte ReadByte() |
| 64 |
{ |
| 65 |
if (pos == end) |
| 66 |
throw new BadImageFormatException(); |
| 67 |
return buffer[pos++]; |
| 68 |
} |
| 69 |
|
| 70 |
internal byte[] ReadBytes(int count) |
| 71 |
{ |
| 72 |
if (count < 0) |
| 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); |
| 78 |
pos += count; |
| 79 |
return buf; |
| 80 |
} |
| 81 |
|
| 82 |
internal int ReadCompressedUInt() |
| 83 |
{ |
| 84 |
byte b1 = ReadByte(); |
| 85 |
if (b1 <= 0x7F) |
| 86 |
{ |
| 87 |
return b1; |
| 88 |
} |
| 89 |
else if ((b1 & 0xC0) == 0x80) |
| 90 |
{ |
| 91 |
byte b2 = ReadByte(); |
| 92 |
return ((b1 & 0x3F) << 8) | b2; |
| 93 |
} |
| 94 |
else |
| 95 |
{ |
| 96 |
byte b2 = ReadByte(); |
| 97 |
byte b3 = ReadByte(); |
| 98 |
byte b4 = ReadByte(); |
| 99 |
return ((b1 & 0x3F) << 24) + (b2 << 16) + (b3 << 8) + b4; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
internal int ReadCompressedInt() |
| 104 |
{ |
| 105 |
byte b1 = PeekByte(); |
| 106 |
int value = ReadCompressedUInt(); |
| 107 |
if ((value & 1) == 0) |
| 108 |
{ |
| 109 |
return value >> 1; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
switch (b1 & 0xC0) |
| 114 |
{ |
| 115 |
case 0: |
| 116 |
case 0x40: |
| 117 |
return (value >> 1) - 0x40; |
| 118 |
case 0x80: |
| 119 |
return (value >> 1) - 0x2000; |
| 120 |
default: |
| 121 |
return (value >> 1) - 0x10000000; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
internal string ReadString() |
| 127 |
{ |
| 128 |
if (PeekByte() == 0xFF) |
| 129 |
{ |
| 130 |
pos++; |
| 131 |
return null; |
| 132 |
} |
| 133 |
int length = ReadCompressedUInt(); |
| 134 |
string str = Encoding.UTF8.GetString(buffer, pos, length); |
| 135 |
pos += length; |
| 136 |
return str; |
| 137 |
} |
| 138 |
|
| 139 |
internal char ReadChar() |
| 140 |
{ |
| 141 |
return (char)ReadInt16(); |
| 142 |
} |
| 143 |
|
| 144 |
internal sbyte ReadSByte() |
| 145 |
{ |
| 146 |
return (sbyte)ReadByte(); |
| 147 |
} |
| 148 |
|
| 149 |
internal short ReadInt16() |
| 150 |
{ |
| 151 |
if (end - pos < 2) |
| 152 |
throw new BadImageFormatException(); |
| 153 |
byte b1 = buffer[pos++]; |
| 154 |
byte b2 = buffer[pos++]; |
| 155 |
return (short)(b1 | (b2 << 8)); |
| 156 |
} |
| 157 |
|
| 158 |
internal ushort ReadUInt16() |
| 159 |
{ |
| 160 |
return (ushort)ReadInt16(); |
| 161 |
} |
| 162 |
|
| 163 |
internal int ReadInt32() |
| 164 |
{ |
| 165 |
if (end - pos < 4) |
| 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)); |
| 172 |
} |
| 173 |
|
| 174 |
internal uint ReadUInt32() |
| 175 |
{ |
| 176 |
return (uint)ReadInt32(); |
| 177 |
} |
| 178 |
|
| 179 |
internal long ReadInt64() |
| 180 |
{ |
| 181 |
ulong lo = ReadUInt32(); |
| 182 |
ulong hi = ReadUInt32(); |
| 183 |
return (long)(lo | (hi << 32)); |
| 184 |
} |
| 185 |
|
| 186 |
internal ulong ReadUInt64() |
| 187 |
{ |
| 188 |
return (ulong)ReadInt64(); |
| 189 |
} |
| 190 |
|
| 191 |
internal float ReadSingle() |
| 192 |
{ |
| 193 |
return SingleConverter.Int32BitsToSingle(ReadInt32()); |
| 194 |
} |
| 195 |
|
| 196 |
internal double ReadDouble() |
| 197 |
{ |
| 198 |
return BitConverter.Int64BitsToDouble(ReadInt64()); |
| 199 |
} |
| 200 |
|
| 201 |
internal ByteReader Slice(int length) |
| 202 |
{ |
| 203 |
if (end - pos < length) |
| 204 |
throw new BadImageFormatException(); |
| 205 |
ByteReader br = new ByteReader(buffer, pos, length); |
| 206 |
pos += length; |
| 207 |
return br; |
| 208 |
} |
| 209 |
|
| 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) |
| 212 |
{ |
| 213 |
alignment--; |
| 214 |
pos = (pos + alignment) & ~alignment; |
| 215 |
} |
| 216 |
} |
| 217 |
} |