| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | using System.Reflection.Internal; |
| | | 6 | | using System.Reflection.Metadata.Ecma335; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.Metadata |
| | | 11 | | { |
| | | 12 | | [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] |
| | | 13 | | public unsafe struct BlobReader |
| | | 14 | | { |
| | | 15 | | internal const int InvalidCompressedInteger = int.MaxValue; |
| | | 16 | | |
| | | 17 | | private readonly MemoryBlock _block; |
| | | 18 | | |
| | | 19 | | // Points right behind the last byte of the block. |
| | | 20 | | private readonly byte* _endPointer; |
| | | 21 | | |
| | | 22 | | private byte* _currentPointer; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Creates a reader of the specified memory block. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="buffer">Pointer to the start of the memory block.</param> |
| | | 28 | | /// <param name="length">Length in bytes of the memory block.</param> |
| | | 29 | | /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null and <paramref name="length"/> is g |
| | | 30 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is negative.</exception> |
| | | 31 | | /// <exception cref="PlatformNotSupportedException">The current platform is not little-endian.</exception> |
| | | 32 | | public BlobReader(byte* buffer, int length) |
| | 0 | 33 | | : this(MemoryBlock.CreateChecked(buffer, length)) |
| | 0 | 34 | | { |
| | | 35 | | |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | internal BlobReader(MemoryBlock block) |
| | 0 | 39 | | { |
| | 0 | 40 | | Debug.Assert(block.Length >= 0 && (block.Pointer != null || block.Length == 0)); |
| | 0 | 41 | | _block = block; |
| | 0 | 42 | | _currentPointer = block.Pointer; |
| | 0 | 43 | | _endPointer = block.Pointer + block.Length; |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | internal string GetDebuggerDisplay() |
| | 0 | 47 | | { |
| | 0 | 48 | | if (_block.Pointer == null) |
| | 0 | 49 | | { |
| | 0 | 50 | | return "<null>"; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | int displayedBytes; |
| | 0 | 54 | | string display = _block.GetDebuggerDisplay(out displayedBytes); |
| | 0 | 55 | | if (this.Offset < displayedBytes) |
| | 0 | 56 | | { |
| | 0 | 57 | | display = display.Insert(this.Offset * 3, "*"); |
| | 0 | 58 | | } |
| | 0 | 59 | | else if (displayedBytes == _block.Length) |
| | 0 | 60 | | { |
| | 0 | 61 | | display += "*"; |
| | 0 | 62 | | } |
| | | 63 | | else |
| | 0 | 64 | | { |
| | 0 | 65 | | display += "*..."; |
| | 0 | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return display; |
| | 0 | 69 | | } |
| | | 70 | | |
| | | 71 | | #region Offset, Skipping, Marking, Alignment, Bounds Checking |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Pointer to the byte at the start of the underlying memory block. |
| | | 75 | | /// </summary> |
| | 0 | 76 | | public byte* StartPointer => _block.Pointer; |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Pointer to the byte at the current position of the reader. |
| | | 80 | | /// </summary> |
| | 0 | 81 | | public byte* CurrentPointer => _currentPointer; |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// The total length of the underlying memory block. |
| | | 85 | | /// </summary> |
| | 0 | 86 | | public int Length => _block.Length; |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets or sets the offset from start of the blob to the current position. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <exception cref="BadImageFormatException">Offset is set outside the bounds of underlying reader.</exception> |
| | | 92 | | public int Offset |
| | | 93 | | { |
| | | 94 | | get |
| | 0 | 95 | | { |
| | 0 | 96 | | return (int)(_currentPointer - _block.Pointer); |
| | 0 | 97 | | } |
| | | 98 | | set |
| | 0 | 99 | | { |
| | 0 | 100 | | if (unchecked((uint)value) > (uint)_block.Length) |
| | 0 | 101 | | { |
| | 0 | 102 | | Throw.OutOfBounds(); |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | _currentPointer = _block.Pointer + value; |
| | 0 | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Bytes remaining from current position to end of underlying memory block. |
| | | 111 | | /// </summary> |
| | 0 | 112 | | public int RemainingBytes => (int)(_endPointer - _currentPointer); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Repositions the reader to the start of the underlying memory block. |
| | | 116 | | /// </summary> |
| | | 117 | | public void Reset() |
| | 0 | 118 | | { |
| | 0 | 119 | | _currentPointer = _block.Pointer; |
| | 0 | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary> |
| | | 123 | | /// Repositions the reader forward by the number of bytes required to satisfy the given alignment. |
| | | 124 | | /// </summary> |
| | | 125 | | public void Align(byte alignment) |
| | 0 | 126 | | { |
| | 0 | 127 | | if (!TryAlign(alignment)) |
| | 0 | 128 | | { |
| | 0 | 129 | | Throw.OutOfBounds(); |
| | | 130 | | } |
| | 0 | 131 | | } |
| | | 132 | | |
| | | 133 | | internal bool TryAlign(byte alignment) |
| | 0 | 134 | | { |
| | 0 | 135 | | int remainder = this.Offset & (alignment - 1); |
| | | 136 | | |
| | 0 | 137 | | Debug.Assert((alignment & (alignment - 1)) == 0, "Alignment must be a power of two."); |
| | 0 | 138 | | Debug.Assert(remainder >= 0 && remainder < alignment); |
| | | 139 | | |
| | 0 | 140 | | if (remainder != 0) |
| | 0 | 141 | | { |
| | 0 | 142 | | int bytesToSkip = alignment - remainder; |
| | 0 | 143 | | if (bytesToSkip > RemainingBytes) |
| | 0 | 144 | | { |
| | 0 | 145 | | return false; |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | _currentPointer += bytesToSkip; |
| | 0 | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | return true; |
| | 0 | 152 | | } |
| | | 153 | | |
| | | 154 | | internal MemoryBlock GetMemoryBlockAt(int offset, int length) |
| | 0 | 155 | | { |
| | 0 | 156 | | CheckBounds(offset, length); |
| | 0 | 157 | | return new MemoryBlock(_currentPointer + offset, length); |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | #endregion |
| | | 161 | | |
| | | 162 | | #region Bounds Checking |
| | | 163 | | |
| | | 164 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 165 | | private void CheckBounds(int offset, int byteCount) |
| | 0 | 166 | | { |
| | 0 | 167 | | if (unchecked((ulong)(uint)offset + (uint)byteCount) > (ulong)(_endPointer - _currentPointer)) |
| | 0 | 168 | | { |
| | 0 | 169 | | Throw.OutOfBounds(); |
| | | 170 | | } |
| | 0 | 171 | | } |
| | | 172 | | |
| | | 173 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 174 | | private void CheckBounds(int byteCount) |
| | | 175 | | { |
| | | 176 | | if (unchecked((uint)byteCount) > (_endPointer - _currentPointer)) |
| | | 177 | | { |
| | | 178 | | Throw.OutOfBounds(); |
| | | 179 | | } |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 183 | | private byte* GetCurrentPointerAndAdvance(int length) |
| | 0 | 184 | | { |
| | 0 | 185 | | byte* p = _currentPointer; |
| | | 186 | | |
| | 0 | 187 | | if (unchecked((uint)length) > (uint)(_endPointer - p)) |
| | 0 | 188 | | { |
| | 0 | 189 | | Throw.OutOfBounds(); |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | _currentPointer = p + length; |
| | 0 | 193 | | return p; |
| | 0 | 194 | | } |
| | | 195 | | |
| | | 196 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 197 | | private byte* GetCurrentPointerAndAdvance1() |
| | 0 | 198 | | { |
| | 0 | 199 | | byte* p = _currentPointer; |
| | | 200 | | |
| | 0 | 201 | | if (p == _endPointer) |
| | 0 | 202 | | { |
| | 0 | 203 | | Throw.OutOfBounds(); |
| | | 204 | | } |
| | | 205 | | |
| | 0 | 206 | | _currentPointer = p + 1; |
| | 0 | 207 | | return p; |
| | 0 | 208 | | } |
| | | 209 | | |
| | | 210 | | #endregion |
| | | 211 | | |
| | | 212 | | #region Read Methods |
| | | 213 | | |
| | | 214 | | public bool ReadBoolean() |
| | 0 | 215 | | { |
| | | 216 | | // It's not clear from the ECMA spec what exactly is the encoding of Boolean. |
| | | 217 | | // Some metadata writers encode "true" as 0xff, others as 1. So we treat all non-zero values as "true". |
| | | 218 | | // |
| | | 219 | | // We propose to clarify and relax the current wording in the spec as follows: |
| | | 220 | | // |
| | | 221 | | // Chapter II.16.2 "Field init metadata" |
| | | 222 | | // ... bool '(' true | false ')' Boolean value stored in a single byte, 0 represents false, any non-zero v |
| | | 223 | | // |
| | | 224 | | // Chapter 23.3 "Custom attributes" |
| | | 225 | | // ... A bool is a single byte with value 0 representing false and any non-zero value representing true .. |
| | 0 | 226 | | return ReadByte() != 0; |
| | 0 | 227 | | } |
| | | 228 | | |
| | | 229 | | public sbyte ReadSByte() |
| | 0 | 230 | | { |
| | 0 | 231 | | return *(sbyte*)GetCurrentPointerAndAdvance1(); |
| | 0 | 232 | | } |
| | | 233 | | |
| | | 234 | | public byte ReadByte() |
| | 0 | 235 | | { |
| | 0 | 236 | | return *(byte*)GetCurrentPointerAndAdvance1(); |
| | 0 | 237 | | } |
| | | 238 | | |
| | | 239 | | public char ReadChar() |
| | 0 | 240 | | { |
| | | 241 | | unchecked |
| | 0 | 242 | | { |
| | 0 | 243 | | byte* ptr = GetCurrentPointerAndAdvance(sizeof(char)); |
| | 0 | 244 | | return (char)(ptr[0] + (ptr[1] << 8)); |
| | | 245 | | } |
| | 0 | 246 | | } |
| | | 247 | | |
| | | 248 | | public short ReadInt16() |
| | 0 | 249 | | { |
| | | 250 | | unchecked |
| | 0 | 251 | | { |
| | 0 | 252 | | byte* ptr = GetCurrentPointerAndAdvance(sizeof(short)); |
| | 0 | 253 | | return (short)(ptr[0] + (ptr[1] << 8)); |
| | | 254 | | } |
| | 0 | 255 | | } |
| | | 256 | | |
| | | 257 | | public ushort ReadUInt16() |
| | 0 | 258 | | { |
| | | 259 | | unchecked |
| | 0 | 260 | | { |
| | 0 | 261 | | byte* ptr = GetCurrentPointerAndAdvance(sizeof(ushort)); |
| | 0 | 262 | | return (ushort)(ptr[0] + (ptr[1] << 8)); |
| | | 263 | | } |
| | 0 | 264 | | } |
| | | 265 | | |
| | | 266 | | public int ReadInt32() |
| | 0 | 267 | | { |
| | | 268 | | unchecked |
| | 0 | 269 | | { |
| | 0 | 270 | | byte* ptr = GetCurrentPointerAndAdvance(sizeof(int)); |
| | 0 | 271 | | return (int)(ptr[0] + (ptr[1] << 8) + (ptr[2] << 16) + (ptr[3] << 24)); |
| | | 272 | | } |
| | 0 | 273 | | } |
| | | 274 | | |
| | | 275 | | public uint ReadUInt32() |
| | 0 | 276 | | { |
| | | 277 | | unchecked |
| | 0 | 278 | | { |
| | 0 | 279 | | byte* ptr = GetCurrentPointerAndAdvance(sizeof(uint)); |
| | 0 | 280 | | return (uint)(ptr[0] + (ptr[1] << 8) + (ptr[2] << 16) + (ptr[3] << 24)); |
| | | 281 | | } |
| | 0 | 282 | | } |
| | | 283 | | |
| | | 284 | | public long ReadInt64() |
| | 0 | 285 | | { |
| | | 286 | | unchecked |
| | 0 | 287 | | { |
| | 0 | 288 | | byte* ptr = GetCurrentPointerAndAdvance(sizeof(long)); |
| | 0 | 289 | | uint lo = (uint)(ptr[0] + (ptr[1] << 8) + (ptr[2] << 16) + (ptr[3] << 24)); |
| | 0 | 290 | | uint hi = (uint)(ptr[4] + (ptr[5] << 8) + (ptr[6] << 16) + (ptr[7] << 24)); |
| | 0 | 291 | | return (long)(lo + ((ulong)hi << 32)); |
| | | 292 | | } |
| | 0 | 293 | | } |
| | | 294 | | |
| | | 295 | | public ulong ReadUInt64() |
| | 0 | 296 | | { |
| | 0 | 297 | | return unchecked((ulong)ReadInt64()); |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | public float ReadSingle() |
| | 0 | 301 | | { |
| | 0 | 302 | | int val = ReadInt32(); |
| | 0 | 303 | | return *(float*)&val; |
| | 0 | 304 | | } |
| | | 305 | | |
| | | 306 | | public double ReadDouble() |
| | 0 | 307 | | { |
| | 0 | 308 | | long val = ReadInt64(); |
| | 0 | 309 | | return *(double*)&val; |
| | 0 | 310 | | } |
| | | 311 | | |
| | | 312 | | public Guid ReadGuid() |
| | 0 | 313 | | { |
| | | 314 | | const int size = 16; |
| | 0 | 315 | | byte* ptr = GetCurrentPointerAndAdvance(size); |
| | 0 | 316 | | if (BitConverter.IsLittleEndian) |
| | 0 | 317 | | { |
| | 0 | 318 | | return *(Guid*)ptr; |
| | | 319 | | } |
| | | 320 | | else |
| | 0 | 321 | | { |
| | | 322 | | unchecked |
| | 0 | 323 | | { |
| | 0 | 324 | | return new Guid( |
| | 0 | 325 | | (int)(ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24)), |
| | 0 | 326 | | (short)(ptr[4] | (ptr[5] << 8)), |
| | 0 | 327 | | (short)(ptr[6] | (ptr[7] << 8)), |
| | 0 | 328 | | ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15]); |
| | | 329 | | } |
| | | 330 | | } |
| | 0 | 331 | | } |
| | | 332 | | |
| | | 333 | | /// <summary> |
| | | 334 | | /// Reads <see cref="decimal"/> number. |
| | | 335 | | /// </summary> |
| | | 336 | | /// <remarks> |
| | | 337 | | /// Decimal number is encoded in 13 bytes as follows: |
| | | 338 | | /// - byte 0: highest bit indicates sign (1 for negative, 0 for non-negative); the remaining 7 bits encode scale |
| | | 339 | | /// - bytes 1..12: 96-bit unsigned integer in little endian encoding. |
| | | 340 | | /// </remarks> |
| | | 341 | | /// <exception cref="BadImageFormatException">The data at the current position was not a valid <see cref="decima |
| | | 342 | | public decimal ReadDecimal() |
| | 0 | 343 | | { |
| | 0 | 344 | | byte* ptr = GetCurrentPointerAndAdvance(13); |
| | | 345 | | |
| | 0 | 346 | | byte scale = (byte)(*ptr & 0x7f); |
| | 0 | 347 | | if (scale > 28) |
| | 0 | 348 | | { |
| | 0 | 349 | | throw new BadImageFormatException(SR.ValueTooLarge); |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | unchecked |
| | 0 | 353 | | { |
| | 0 | 354 | | return new decimal( |
| | 0 | 355 | | (int)(ptr[1] | (ptr[2] << 8) | (ptr[3] << 16) | (ptr[4] << 24)), |
| | 0 | 356 | | (int)(ptr[5] | (ptr[6] << 8) | (ptr[7] << 16) | (ptr[8] << 24)), |
| | 0 | 357 | | (int)(ptr[9] | (ptr[10] << 8) | (ptr[11] << 16) | (ptr[12] << 24)), |
| | 0 | 358 | | isNegative: (*ptr & 0x80) != 0, |
| | 0 | 359 | | scale: scale); |
| | | 360 | | } |
| | 0 | 361 | | } |
| | | 362 | | |
| | | 363 | | public DateTime ReadDateTime() |
| | 0 | 364 | | { |
| | 0 | 365 | | return new DateTime(ReadInt64()); |
| | 0 | 366 | | } |
| | | 367 | | |
| | | 368 | | public SignatureHeader ReadSignatureHeader() |
| | 0 | 369 | | { |
| | 0 | 370 | | return new SignatureHeader(ReadByte()); |
| | 0 | 371 | | } |
| | | 372 | | |
| | | 373 | | /// <summary> |
| | | 374 | | /// Finds specified byte in the blob following the current position. |
| | | 375 | | /// </summary> |
| | | 376 | | /// <returns> |
| | | 377 | | /// Index relative to the current position, or -1 if the byte is not found in the blob following the current pos |
| | | 378 | | /// </returns> |
| | | 379 | | /// <remarks> |
| | | 380 | | /// Doesn't change the current position. |
| | | 381 | | /// </remarks> |
| | | 382 | | public int IndexOf(byte value) |
| | 0 | 383 | | { |
| | 0 | 384 | | int start = Offset; |
| | 0 | 385 | | int absoluteIndex = _block.IndexOfUnchecked(value, start); |
| | 0 | 386 | | return (absoluteIndex >= 0) ? absoluteIndex - start : -1; |
| | 0 | 387 | | } |
| | | 388 | | |
| | | 389 | | /// <summary> |
| | | 390 | | /// Reads UTF-8 encoded string starting at the current position. |
| | | 391 | | /// </summary> |
| | | 392 | | /// <param name="byteCount">The number of bytes to read.</param> |
| | | 393 | | /// <returns>The string.</returns> |
| | | 394 | | /// <exception cref="BadImageFormatException"><paramref name="byteCount"/> bytes not available.</exception> |
| | | 395 | | public string ReadUTF8(int byteCount) |
| | 0 | 396 | | { |
| | 0 | 397 | | string s = _block.PeekUtf8(this.Offset, byteCount); |
| | 0 | 398 | | _currentPointer += byteCount; |
| | 0 | 399 | | return s; |
| | 0 | 400 | | } |
| | | 401 | | |
| | | 402 | | /// <summary> |
| | | 403 | | /// Reads UTF-16 (little-endian) encoded string starting at the current position. |
| | | 404 | | /// </summary> |
| | | 405 | | /// <param name="byteCount">The number of bytes to read.</param> |
| | | 406 | | /// <returns>The string.</returns> |
| | | 407 | | /// <exception cref="BadImageFormatException"><paramref name="byteCount"/> bytes not available.</exception> |
| | | 408 | | public string ReadUTF16(int byteCount) |
| | 0 | 409 | | { |
| | 0 | 410 | | string s = _block.PeekUtf16(this.Offset, byteCount); |
| | 0 | 411 | | _currentPointer += byteCount; |
| | 0 | 412 | | return s; |
| | 0 | 413 | | } |
| | | 414 | | |
| | | 415 | | /// <summary> |
| | | 416 | | /// Reads bytes starting at the current position. |
| | | 417 | | /// </summary> |
| | | 418 | | /// <param name="byteCount">The number of bytes to read.</param> |
| | | 419 | | /// <returns>The byte array.</returns> |
| | | 420 | | /// <exception cref="BadImageFormatException"><paramref name="byteCount"/> bytes not available.</exception> |
| | | 421 | | public byte[] ReadBytes(int byteCount) |
| | 0 | 422 | | { |
| | 0 | 423 | | byte[] bytes = _block.PeekBytes(this.Offset, byteCount); |
| | 0 | 424 | | _currentPointer += byteCount; |
| | 0 | 425 | | return bytes; |
| | 0 | 426 | | } |
| | | 427 | | |
| | | 428 | | /// <summary> |
| | | 429 | | /// Reads bytes starting at the current position in to the given buffer at the given offset; |
| | | 430 | | /// </summary> |
| | | 431 | | /// <param name="byteCount">The number of bytes to read.</param> |
| | | 432 | | /// <param name="buffer">The destination buffer the bytes read will be written.</param> |
| | | 433 | | /// <param name="bufferOffset">The offset in the destination buffer where the bytes read will be written.</param |
| | | 434 | | /// <exception cref="BadImageFormatException"><paramref name="byteCount"/> bytes not available.</exception> |
| | | 435 | | public void ReadBytes(int byteCount, byte[] buffer, int bufferOffset) |
| | 0 | 436 | | { |
| | 0 | 437 | | Marshal.Copy((IntPtr)GetCurrentPointerAndAdvance(byteCount), buffer, bufferOffset, byteCount); |
| | 0 | 438 | | } |
| | | 439 | | |
| | | 440 | | internal string ReadUtf8NullTerminated() |
| | 0 | 441 | | { |
| | | 442 | | int bytesRead; |
| | 0 | 443 | | string value = _block.PeekUtf8NullTerminated(this.Offset, null, MetadataStringDecoder.DefaultUTF8, out bytes |
| | 0 | 444 | | _currentPointer += bytesRead; |
| | 0 | 445 | | return value; |
| | 0 | 446 | | } |
| | | 447 | | |
| | | 448 | | private int ReadCompressedIntegerOrInvalid() |
| | 0 | 449 | | { |
| | | 450 | | int bytesRead; |
| | 0 | 451 | | int value = _block.PeekCompressedInteger(this.Offset, out bytesRead); |
| | 0 | 452 | | _currentPointer += bytesRead; |
| | 0 | 453 | | return value; |
| | 0 | 454 | | } |
| | | 455 | | |
| | | 456 | | /// <summary> |
| | | 457 | | /// Reads an unsigned compressed integer value. |
| | | 458 | | /// See Metadata Specification section II.23.2: Blobs and signatures. |
| | | 459 | | /// </summary> |
| | | 460 | | /// <param name="value">The value of the compressed integer that was read.</param> |
| | | 461 | | /// <returns>true if the value was read successfully. false if the data at the current position was not a valid |
| | | 462 | | public bool TryReadCompressedInteger(out int value) |
| | 0 | 463 | | { |
| | 0 | 464 | | value = ReadCompressedIntegerOrInvalid(); |
| | 0 | 465 | | return value != InvalidCompressedInteger; |
| | 0 | 466 | | } |
| | | 467 | | |
| | | 468 | | /// <summary> |
| | | 469 | | /// Reads an unsigned compressed integer value. |
| | | 470 | | /// See Metadata Specification section II.23.2: Blobs and signatures. |
| | | 471 | | /// </summary> |
| | | 472 | | /// <returns>The value of the compressed integer that was read.</returns> |
| | | 473 | | /// <exception cref="BadImageFormatException">The data at the current position was not a valid compressed intege |
| | | 474 | | public int ReadCompressedInteger() |
| | 0 | 475 | | { |
| | | 476 | | int value; |
| | 0 | 477 | | if (!TryReadCompressedInteger(out value)) |
| | 0 | 478 | | { |
| | 0 | 479 | | Throw.InvalidCompressedInteger(); |
| | | 480 | | } |
| | 0 | 481 | | return value; |
| | 0 | 482 | | } |
| | | 483 | | |
| | | 484 | | /// <summary> |
| | | 485 | | /// Reads a signed compressed integer value. |
| | | 486 | | /// See Metadata Specification section II.23.2: Blobs and signatures. |
| | | 487 | | /// </summary> |
| | | 488 | | /// <param name="value">The value of the compressed integer that was read.</param> |
| | | 489 | | /// <returns>true if the value was read successfully. false if the data at the current position was not a valid |
| | | 490 | | public bool TryReadCompressedSignedInteger(out int value) |
| | 0 | 491 | | { |
| | | 492 | | int bytesRead; |
| | 0 | 493 | | value = _block.PeekCompressedInteger(this.Offset, out bytesRead); |
| | | 494 | | |
| | 0 | 495 | | if (value == InvalidCompressedInteger) |
| | 0 | 496 | | { |
| | 0 | 497 | | return false; |
| | | 498 | | } |
| | | 499 | | |
| | 0 | 500 | | bool signExtend = (value & 0x1) != 0; |
| | 0 | 501 | | value >>= 1; |
| | | 502 | | |
| | 0 | 503 | | if (signExtend) |
| | 0 | 504 | | { |
| | 0 | 505 | | switch (bytesRead) |
| | | 506 | | { |
| | | 507 | | case 1: |
| | 0 | 508 | | value |= unchecked((int)0xffffffc0); |
| | 0 | 509 | | break; |
| | | 510 | | case 2: |
| | 0 | 511 | | value |= unchecked((int)0xffffe000); |
| | 0 | 512 | | break; |
| | | 513 | | default: |
| | 0 | 514 | | Debug.Assert(bytesRead == 4); |
| | 0 | 515 | | value |= unchecked((int)0xf0000000); |
| | 0 | 516 | | break; |
| | | 517 | | } |
| | 0 | 518 | | } |
| | | 519 | | |
| | 0 | 520 | | _currentPointer += bytesRead; |
| | 0 | 521 | | return true; |
| | 0 | 522 | | } |
| | | 523 | | |
| | | 524 | | /// <summary> |
| | | 525 | | /// Reads a signed compressed integer value. |
| | | 526 | | /// See Metadata Specification section II.23.2: Blobs and signatures. |
| | | 527 | | /// </summary> |
| | | 528 | | /// <returns>The value of the compressed integer that was read.</returns> |
| | | 529 | | /// <exception cref="BadImageFormatException">The data at the current position was not a valid compressed intege |
| | | 530 | | public int ReadCompressedSignedInteger() |
| | 0 | 531 | | { |
| | | 532 | | int value; |
| | 0 | 533 | | if (!TryReadCompressedSignedInteger(out value)) |
| | 0 | 534 | | { |
| | 0 | 535 | | Throw.InvalidCompressedInteger(); |
| | | 536 | | } |
| | 0 | 537 | | return value; |
| | 0 | 538 | | } |
| | | 539 | | |
| | | 540 | | /// <summary> |
| | | 541 | | /// Reads type code encoded in a serialized custom attribute value. |
| | | 542 | | /// </summary> |
| | | 543 | | /// <returns><see cref="SerializationTypeCode.Invalid"/> if the encoding is invalid.</returns> |
| | | 544 | | public SerializationTypeCode ReadSerializationTypeCode() |
| | 0 | 545 | | { |
| | 0 | 546 | | int value = ReadCompressedIntegerOrInvalid(); |
| | 0 | 547 | | if (value > byte.MaxValue) |
| | 0 | 548 | | { |
| | 0 | 549 | | return SerializationTypeCode.Invalid; |
| | | 550 | | } |
| | | 551 | | |
| | 0 | 552 | | return unchecked((SerializationTypeCode)value); |
| | 0 | 553 | | } |
| | | 554 | | |
| | | 555 | | /// <summary> |
| | | 556 | | /// Reads type code encoded in a signature. |
| | | 557 | | /// </summary> |
| | | 558 | | /// <returns><see cref="SignatureTypeCode.Invalid"/> if the encoding is invalid.</returns> |
| | | 559 | | public SignatureTypeCode ReadSignatureTypeCode() |
| | 0 | 560 | | { |
| | 0 | 561 | | int value = ReadCompressedIntegerOrInvalid(); |
| | | 562 | | |
| | 0 | 563 | | switch (value) |
| | | 564 | | { |
| | | 565 | | case (int)CorElementType.ELEMENT_TYPE_CLASS: |
| | | 566 | | case (int)CorElementType.ELEMENT_TYPE_VALUETYPE: |
| | 0 | 567 | | return SignatureTypeCode.TypeHandle; |
| | | 568 | | |
| | | 569 | | default: |
| | 0 | 570 | | if (value > byte.MaxValue) |
| | 0 | 571 | | { |
| | 0 | 572 | | return SignatureTypeCode.Invalid; |
| | | 573 | | } |
| | | 574 | | |
| | 0 | 575 | | return unchecked((SignatureTypeCode)value); |
| | | 576 | | } |
| | 0 | 577 | | } |
| | | 578 | | |
| | | 579 | | /// <summary> |
| | | 580 | | /// Reads a string encoded as a compressed integer containing its length followed by |
| | | 581 | | /// its contents in UTF-8. Null strings are encoded as a single 0xFF byte. |
| | | 582 | | /// </summary> |
| | | 583 | | /// <remarks>Defined as a 'SerString' in the ECMA CLI specification.</remarks> |
| | | 584 | | /// <returns>String value or null.</returns> |
| | | 585 | | /// <exception cref="BadImageFormatException">If the encoding is invalid.</exception> |
| | | 586 | | public string? ReadSerializedString() |
| | 0 | 587 | | { |
| | | 588 | | int length; |
| | 0 | 589 | | if (TryReadCompressedInteger(out length)) |
| | 0 | 590 | | { |
| | 0 | 591 | | return ReadUTF8(length); |
| | | 592 | | } |
| | | 593 | | |
| | 0 | 594 | | if (ReadByte() != 0xFF) |
| | 0 | 595 | | { |
| | 0 | 596 | | Throw.InvalidSerializedString(); |
| | | 597 | | } |
| | | 598 | | |
| | 0 | 599 | | return null; |
| | 0 | 600 | | } |
| | | 601 | | |
| | | 602 | | /// <summary> |
| | | 603 | | /// Reads a type handle encoded in a signature as TypeDefOrRefOrSpecEncoded (see ECMA-335 II.23.2.8). |
| | | 604 | | /// </summary> |
| | | 605 | | /// <returns>The handle or nil if the encoding is invalid.</returns> |
| | | 606 | | public EntityHandle ReadTypeHandle() |
| | 0 | 607 | | { |
| | 0 | 608 | | uint value = (uint)ReadCompressedIntegerOrInvalid(); |
| | 0 | 609 | | uint tokenType = CorEncodeTokenArray[(int)(value & 0x3)]; |
| | | 610 | | |
| | 0 | 611 | | if (value == InvalidCompressedInteger || tokenType == 0) |
| | 0 | 612 | | { |
| | 0 | 613 | | return default(EntityHandle); |
| | | 614 | | } |
| | | 615 | | |
| | 0 | 616 | | return new EntityHandle(tokenType | (value >> 2)); |
| | 0 | 617 | | } |
| | | 618 | | |
| | 0 | 619 | | private static ReadOnlySpan<uint> CorEncodeTokenArray => [TokenTypeIds.TypeDef, TokenTypeIds.TypeRef, TokenTypeI |
| | | 620 | | |
| | | 621 | | /// <summary> |
| | | 622 | | /// Reads a #Blob heap handle encoded as a compressed integer. |
| | | 623 | | /// </summary> |
| | | 624 | | /// <remarks> |
| | | 625 | | /// Blobs that contain references to other blobs are used in Portable PDB format, for example <see cref="Documen |
| | | 626 | | /// </remarks> |
| | | 627 | | public BlobHandle ReadBlobHandle() |
| | 0 | 628 | | { |
| | 0 | 629 | | return BlobHandle.FromOffset(ReadCompressedInteger()); |
| | 0 | 630 | | } |
| | | 631 | | |
| | | 632 | | /// <summary> |
| | | 633 | | /// Reads a constant value (see ECMA-335 Partition II section 22.9) from the current position. |
| | | 634 | | /// </summary> |
| | | 635 | | /// <exception cref="BadImageFormatException">Error while reading from the blob.</exception> |
| | | 636 | | /// <exception cref="ArgumentOutOfRangeException"><paramref name="typeCode"/> is not a valid <see cref="Constant |
| | | 637 | | /// <returns> |
| | | 638 | | /// Boxed constant value. To avoid allocating the object use Read* methods directly. |
| | | 639 | | /// Constants of type <see cref="ConstantTypeCode.String"/> are encoded as UTF16 strings, use <see cref="ReadUTF |
| | | 640 | | /// </returns> |
| | | 641 | | public object? ReadConstant(ConstantTypeCode typeCode) |
| | 0 | 642 | | { |
| | | 643 | | // Partition II section 22.9: |
| | | 644 | | // |
| | | 645 | | // Type shall be exactly one of: ELEMENT_TYPE_BOOLEAN, ELEMENT_TYPE_CHAR, ELEMENT_TYPE_I1, |
| | | 646 | | // ELEMENT_TYPE_U1, ELEMENT_TYPE_I2, ELEMENT_TYPE_U2, ELEMENT_TYPE_I4, ELEMENT_TYPE_U4, |
| | | 647 | | // ELEMENT_TYPE_I8, ELEMENT_TYPE_U8, ELEMENT_TYPE_R4, ELEMENT_TYPE_R8, or ELEMENT_TYPE_STRING; |
| | | 648 | | // or ELEMENT_TYPE_CLASS with a Value of zero (23.1.16) |
| | | 649 | | |
| | 0 | 650 | | switch (typeCode) |
| | | 651 | | { |
| | | 652 | | case ConstantTypeCode.Boolean: |
| | 0 | 653 | | return ReadBoolean(); |
| | | 654 | | |
| | | 655 | | case ConstantTypeCode.Char: |
| | 0 | 656 | | return ReadChar(); |
| | | 657 | | |
| | | 658 | | case ConstantTypeCode.SByte: |
| | 0 | 659 | | return ReadSByte(); |
| | | 660 | | |
| | | 661 | | case ConstantTypeCode.Int16: |
| | 0 | 662 | | return ReadInt16(); |
| | | 663 | | |
| | | 664 | | case ConstantTypeCode.Int32: |
| | 0 | 665 | | return ReadInt32(); |
| | | 666 | | |
| | | 667 | | case ConstantTypeCode.Int64: |
| | 0 | 668 | | return ReadInt64(); |
| | | 669 | | |
| | | 670 | | case ConstantTypeCode.Byte: |
| | 0 | 671 | | return ReadByte(); |
| | | 672 | | |
| | | 673 | | case ConstantTypeCode.UInt16: |
| | 0 | 674 | | return ReadUInt16(); |
| | | 675 | | |
| | | 676 | | case ConstantTypeCode.UInt32: |
| | 0 | 677 | | return ReadUInt32(); |
| | | 678 | | |
| | | 679 | | case ConstantTypeCode.UInt64: |
| | 0 | 680 | | return ReadUInt64(); |
| | | 681 | | |
| | | 682 | | case ConstantTypeCode.Single: |
| | 0 | 683 | | return ReadSingle(); |
| | | 684 | | |
| | | 685 | | case ConstantTypeCode.Double: |
| | 0 | 686 | | return ReadDouble(); |
| | | 687 | | |
| | | 688 | | case ConstantTypeCode.String: |
| | 0 | 689 | | return ReadUTF16(RemainingBytes); |
| | | 690 | | |
| | | 691 | | case ConstantTypeCode.NullReference: |
| | | 692 | | // Partition II section 22.9: |
| | | 693 | | // The encoding of Type for the nullref value is ELEMENT_TYPE_CLASS with a Value of a 4-byte zero. |
| | | 694 | | // Unlike uses of ELEMENT_TYPE_CLASS in signatures, this one is not followed by a type token. |
| | 0 | 695 | | if (ReadUInt32() != 0) |
| | 0 | 696 | | { |
| | 0 | 697 | | throw new BadImageFormatException(SR.InvalidConstantValue); |
| | | 698 | | } |
| | | 699 | | |
| | 0 | 700 | | return null; |
| | | 701 | | |
| | | 702 | | default: |
| | 0 | 703 | | throw new ArgumentOutOfRangeException(nameof(typeCode)); |
| | | 704 | | } |
| | 0 | 705 | | } |
| | | 706 | | |
| | | 707 | | #endregion |
| | | 708 | | } |
| | | 709 | | } |
| | | 710 | | |