| | | 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.Buffers.Binary; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Reflection.Metadata; |
| | | 7 | | using System.Reflection.Metadata.Ecma335; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Text; |
| | | 10 | | |
| | | 11 | | namespace System.Reflection.Internal |
| | | 12 | | { |
| | | 13 | | [DebuggerDisplay("{GetDebuggerDisplay(),nq}")] |
| | | 14 | | internal readonly unsafe struct MemoryBlock |
| | | 15 | | { |
| | | 16 | | internal readonly byte* Pointer; |
| | | 17 | | internal readonly int Length; |
| | | 18 | | |
| | | 19 | | internal MemoryBlock(byte* buffer, int length) |
| | 0 | 20 | | { |
| | 0 | 21 | | Debug.Assert(length >= 0 && (buffer != null || length == 0)); |
| | 0 | 22 | | this.Pointer = buffer; |
| | 0 | 23 | | this.Length = length; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | internal static MemoryBlock CreateChecked(byte* buffer, int length) |
| | 0 | 27 | | { |
| | 0 | 28 | | if (length < 0) |
| | 0 | 29 | | { |
| | 0 | 30 | | throw new ArgumentOutOfRangeException(nameof(length)); |
| | | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | if (buffer == null && length != 0) |
| | 0 | 34 | | { |
| | 0 | 35 | | Throw.ArgumentNull(nameof(buffer)); |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | return new MemoryBlock(buffer, length); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 42 | | private void CheckBounds(int offset, int byteCount) |
| | 0 | 43 | | { |
| | 0 | 44 | | if (unchecked((ulong)(uint)offset + (uint)byteCount) > (ulong)Length) |
| | 0 | 45 | | { |
| | 0 | 46 | | Throw.OutOfBounds(); |
| | | 47 | | } |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | internal byte[]? ToArray() |
| | 0 | 51 | | { |
| | 0 | 52 | | return Pointer == null ? null : PeekBytes(0, Length); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | private string GetDebuggerDisplay() |
| | 0 | 56 | | { |
| | 0 | 57 | | if (Pointer == null) |
| | 0 | 58 | | { |
| | 0 | 59 | | return "<null>"; |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | return GetDebuggerDisplay(out _); |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | internal string GetDebuggerDisplay(out int displayedBytes) |
| | 0 | 66 | | { |
| | 0 | 67 | | displayedBytes = Math.Min(Length, 64); |
| | 0 | 68 | | string result = BitConverter.ToString(PeekBytes(0, displayedBytes)); |
| | 0 | 69 | | if (displayedBytes < Length) |
| | 0 | 70 | | { |
| | 0 | 71 | | result += "-..."; |
| | 0 | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | return result; |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | internal string GetDebuggerDisplay(int offset) |
| | 0 | 78 | | { |
| | 0 | 79 | | if (Pointer == null) |
| | 0 | 80 | | { |
| | 0 | 81 | | return "<null>"; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | int displayedBytes; |
| | 0 | 85 | | string display = GetDebuggerDisplay(out displayedBytes); |
| | 0 | 86 | | if (offset < displayedBytes) |
| | 0 | 87 | | { |
| | 0 | 88 | | display = display.Insert(offset * 3, "*"); |
| | 0 | 89 | | } |
| | 0 | 90 | | else if (displayedBytes == Length) |
| | 0 | 91 | | { |
| | 0 | 92 | | display += "*"; |
| | 0 | 93 | | } |
| | | 94 | | else |
| | 0 | 95 | | { |
| | 0 | 96 | | display += "*..."; |
| | 0 | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | return display; |
| | 0 | 100 | | } |
| | | 101 | | |
| | | 102 | | internal MemoryBlock GetMemoryBlockAt(int offset, int length) |
| | 0 | 103 | | { |
| | 0 | 104 | | CheckBounds(offset, length); |
| | 0 | 105 | | return new MemoryBlock(Pointer + offset, length); |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | internal byte PeekByte(int offset) |
| | 0 | 109 | | { |
| | 0 | 110 | | CheckBounds(offset, sizeof(byte)); |
| | 0 | 111 | | return Pointer[offset]; |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | internal int PeekInt32(int offset) |
| | 0 | 115 | | { |
| | 0 | 116 | | uint result = PeekUInt32(offset); |
| | 0 | 117 | | if (unchecked((int)result != result)) |
| | 0 | 118 | | { |
| | 0 | 119 | | Throw.ValueOverflow(); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | return (int)result; |
| | 0 | 123 | | } |
| | | 124 | | |
| | | 125 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 126 | | internal uint PeekUInt32(int offset) |
| | 0 | 127 | | { |
| | 0 | 128 | | CheckBounds(offset, sizeof(uint)); |
| | | 129 | | |
| | 0 | 130 | | uint result = Unsafe.ReadUnaligned<uint>(Pointer + offset); |
| | 0 | 131 | | return BitConverter.IsLittleEndian ? result : BinaryPrimitives.ReverseEndianness(result); |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Decodes a compressed integer value starting at offset. |
| | | 136 | | /// See Metadata Specification section II.23.2: Blobs and signatures. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <param name="offset">Offset to the start of the compressed data.</param> |
| | | 139 | | /// <param name="numberOfBytesRead">Bytes actually read.</param> |
| | | 140 | | /// <returns> |
| | | 141 | | /// Value between 0 and 0x1fffffff, or <see cref="BlobReader.InvalidCompressedInteger"/> if the value encoding i |
| | | 142 | | /// </returns> |
| | | 143 | | internal int PeekCompressedInteger(int offset, out int numberOfBytesRead) |
| | 0 | 144 | | { |
| | 0 | 145 | | CheckBounds(offset, 0); |
| | | 146 | | |
| | 0 | 147 | | byte* ptr = Pointer + offset; |
| | 0 | 148 | | long limit = Length - offset; |
| | | 149 | | |
| | 0 | 150 | | if (limit == 0) |
| | 0 | 151 | | { |
| | 0 | 152 | | numberOfBytesRead = 0; |
| | 0 | 153 | | return BlobReader.InvalidCompressedInteger; |
| | | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | byte headerByte = ptr[0]; |
| | 0 | 157 | | if ((headerByte & 0x80) == 0) |
| | 0 | 158 | | { |
| | 0 | 159 | | numberOfBytesRead = 1; |
| | 0 | 160 | | return headerByte; |
| | | 161 | | } |
| | 0 | 162 | | else if ((headerByte & 0x40) == 0) |
| | 0 | 163 | | { |
| | 0 | 164 | | if (limit >= 2) |
| | 0 | 165 | | { |
| | 0 | 166 | | numberOfBytesRead = 2; |
| | 0 | 167 | | return ((headerByte & 0x3f) << 8) | ptr[1]; |
| | | 168 | | } |
| | 0 | 169 | | } |
| | 0 | 170 | | else if ((headerByte & 0x20) == 0) |
| | 0 | 171 | | { |
| | 0 | 172 | | if (limit >= 4) |
| | 0 | 173 | | { |
| | 0 | 174 | | numberOfBytesRead = 4; |
| | 0 | 175 | | return ((headerByte & 0x1f) << 24) | (ptr[1] << 16) | (ptr[2] << 8) | ptr[3]; |
| | | 176 | | } |
| | 0 | 177 | | } |
| | | 178 | | |
| | 0 | 179 | | numberOfBytesRead = 0; |
| | 0 | 180 | | return BlobReader.InvalidCompressedInteger; |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 184 | | internal ushort PeekUInt16(int offset) |
| | 0 | 185 | | { |
| | 0 | 186 | | CheckBounds(offset, sizeof(ushort)); |
| | | 187 | | |
| | 0 | 188 | | ushort result = Unsafe.ReadUnaligned<ushort>(Pointer + offset); |
| | 0 | 189 | | return BitConverter.IsLittleEndian ? result : BinaryPrimitives.ReverseEndianness(result); |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | // When reference has tag bits. |
| | | 193 | | internal uint PeekTaggedReference(int offset, bool smallRefSize) |
| | 0 | 194 | | { |
| | 0 | 195 | | return PeekReferenceUnchecked(offset, smallRefSize); |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | // Use when searching for a tagged or non-tagged reference. |
| | | 199 | | // The result may be an invalid reference and shall only be used to compare with a valid reference. |
| | | 200 | | internal uint PeekReferenceUnchecked(int offset, bool smallRefSize) |
| | 0 | 201 | | { |
| | 0 | 202 | | return smallRefSize ? PeekUInt16(offset) : PeekUInt32(offset); |
| | 0 | 203 | | } |
| | | 204 | | |
| | | 205 | | // When reference has at most 24 bits. |
| | | 206 | | internal int PeekReference(int offset, bool smallRefSize) |
| | 0 | 207 | | { |
| | 0 | 208 | | if (smallRefSize) |
| | 0 | 209 | | { |
| | 0 | 210 | | return PeekUInt16(offset); |
| | | 211 | | } |
| | | 212 | | |
| | 0 | 213 | | uint value = PeekUInt32(offset); |
| | | 214 | | |
| | 0 | 215 | | if (!TokenTypeIds.IsValidRowId(value)) |
| | 0 | 216 | | { |
| | 0 | 217 | | Throw.ReferenceOverflow(); |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | return (int)value; |
| | 0 | 221 | | } |
| | | 222 | | |
| | | 223 | | // #String, #Blob heaps |
| | | 224 | | internal int PeekHeapReference(int offset, bool smallRefSize) |
| | 0 | 225 | | { |
| | 0 | 226 | | if (smallRefSize) |
| | 0 | 227 | | { |
| | 0 | 228 | | return PeekUInt16(offset); |
| | | 229 | | } |
| | | 230 | | |
| | 0 | 231 | | uint value = PeekUInt32(offset); |
| | | 232 | | |
| | 0 | 233 | | if (!HeapHandleType.IsValidHeapOffset(value)) |
| | 0 | 234 | | { |
| | 0 | 235 | | Throw.ReferenceOverflow(); |
| | | 236 | | } |
| | | 237 | | |
| | 0 | 238 | | return (int)value; |
| | 0 | 239 | | } |
| | | 240 | | |
| | | 241 | | internal Guid PeekGuid(int offset) |
| | 0 | 242 | | { |
| | 0 | 243 | | CheckBounds(offset, sizeof(Guid)); |
| | | 244 | | |
| | 0 | 245 | | byte* ptr = Pointer + offset; |
| | 0 | 246 | | if (BitConverter.IsLittleEndian) |
| | 0 | 247 | | { |
| | 0 | 248 | | return Unsafe.ReadUnaligned<Guid>(ptr); |
| | | 249 | | } |
| | | 250 | | else |
| | 0 | 251 | | { |
| | | 252 | | unchecked |
| | 0 | 253 | | { |
| | 0 | 254 | | return new Guid( |
| | 0 | 255 | | (int)(ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24)), |
| | 0 | 256 | | (short)(ptr[4] | (ptr[5] << 8)), |
| | 0 | 257 | | (short)(ptr[6] | (ptr[7] << 8)), |
| | 0 | 258 | | ptr[8], ptr[9], ptr[10], ptr[11], ptr[12], ptr[13], ptr[14], ptr[15]); |
| | | 259 | | } |
| | | 260 | | } |
| | 0 | 261 | | } |
| | | 262 | | |
| | | 263 | | internal string PeekUtf16(int offset, int byteCount) |
| | 0 | 264 | | { |
| | 0 | 265 | | CheckBounds(offset, byteCount); |
| | | 266 | | |
| | 0 | 267 | | byte* ptr = Pointer + offset; |
| | 0 | 268 | | if (BitConverter.IsLittleEndian) |
| | 0 | 269 | | { |
| | | 270 | | // doesn't allocate a new string if byteCount == 0 |
| | 0 | 271 | | return new string((char*)ptr, 0, byteCount / sizeof(char)); |
| | | 272 | | } |
| | | 273 | | else |
| | 0 | 274 | | { |
| | 0 | 275 | | return Encoding.Unicode.GetString(ptr, byteCount); |
| | | 276 | | } |
| | 0 | 277 | | } |
| | | 278 | | |
| | | 279 | | internal string PeekUtf8(int offset, int byteCount) |
| | 0 | 280 | | { |
| | 0 | 281 | | CheckBounds(offset, byteCount); |
| | 0 | 282 | | return Encoding.UTF8.GetString(Pointer + offset, byteCount); |
| | 0 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Read UTF-8 at the given offset up to the given terminator, null terminator, or end-of-block. |
| | | 287 | | /// </summary> |
| | | 288 | | /// <param name="offset">Offset in to the block where the UTF-8 bytes start.</param> |
| | | 289 | | /// <param name="prefix">UTF-8 encoded prefix to prepend to the bytes at the offset before decoding.</param> |
| | | 290 | | /// <param name="utf8Decoder">The UTF-8 decoder to use that allows user to adjust fallback and/or reuse existing |
| | | 291 | | /// <param name="numberOfBytesRead">The number of bytes read, which includes the terminator if we did not hit th |
| | | 292 | | /// <param name="terminator">A character in the ASCII range that marks the end of the string. |
| | | 293 | | /// If a value other than '\0' is passed we still stop at the null terminator if encountered first.</param> |
| | | 294 | | /// <returns>The decoded string.</returns> |
| | | 295 | | internal string PeekUtf8NullTerminated(int offset, byte[]? prefix, MetadataStringDecoder utf8Decoder, out int nu |
| | 0 | 296 | | { |
| | 0 | 297 | | Debug.Assert(terminator <= 0x7F); |
| | 0 | 298 | | CheckBounds(offset, 0); |
| | 0 | 299 | | int length = GetUtf8NullTerminatedLength(offset, out numberOfBytesRead, terminator); |
| | 0 | 300 | | return EncodingHelper.DecodeUtf8(Pointer + offset, length, prefix, utf8Decoder); |
| | 0 | 301 | | } |
| | | 302 | | |
| | | 303 | | /// <summary> |
| | | 304 | | /// Get number of bytes from offset to given terminator, null terminator, or end-of-block (whichever comes first |
| | | 305 | | /// Returned length does not include the terminator, but numberOfBytesRead out parameter does. |
| | | 306 | | /// </summary> |
| | | 307 | | /// <param name="offset">Offset in to the block where the UTF-8 bytes start.</param> |
| | | 308 | | /// <param name="terminator">A character in the ASCII range that marks the end of the string. |
| | | 309 | | /// If a value other than '\0' is passed we still stop at the null terminator if encountered first.</param> |
| | | 310 | | /// <param name="numberOfBytesRead">The number of bytes read, which includes the terminator if we did not hit th |
| | | 311 | | /// <returns>Length (byte count) not including terminator.</returns> |
| | | 312 | | internal int GetUtf8NullTerminatedLength(int offset, out int numberOfBytesRead, char terminator) |
| | 0 | 313 | | { |
| | 0 | 314 | | CheckBounds(offset, 0); |
| | | 315 | | |
| | 0 | 316 | | Debug.Assert(terminator <= 0x7f); |
| | | 317 | | |
| | 0 | 318 | | ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(Pointer + offset, Length - offset); |
| | 0 | 319 | | int length = terminator != '\0' ? |
| | 0 | 320 | | span.IndexOfAny((byte)0, (byte)terminator) : |
| | 0 | 321 | | span.IndexOf((byte)0); |
| | 0 | 322 | | if (length >= 0) |
| | 0 | 323 | | { |
| | 0 | 324 | | numberOfBytesRead = length + 1; // we also read the terminator |
| | 0 | 325 | | } |
| | | 326 | | else |
| | 0 | 327 | | { |
| | 0 | 328 | | numberOfBytesRead = length = span.Length; |
| | 0 | 329 | | } |
| | | 330 | | |
| | 0 | 331 | | return length; |
| | 0 | 332 | | } |
| | | 333 | | |
| | | 334 | | internal int Utf8NullTerminatedOffsetOfAsciiChar(int startOffset, char asciiChar) |
| | 0 | 335 | | { |
| | 0 | 336 | | CheckBounds(startOffset, 0); |
| | | 337 | | |
| | 0 | 338 | | Debug.Assert(asciiChar != 0 && asciiChar <= 0x7f); |
| | | 339 | | |
| | 0 | 340 | | ReadOnlySpan<byte> span = new ReadOnlySpan<byte>(Pointer + startOffset, Length - startOffset); |
| | 0 | 341 | | int i = span.IndexOfAny((byte)asciiChar, (byte)0); |
| | 0 | 342 | | return i >= 0 && span[i] == asciiChar ? |
| | 0 | 343 | | startOffset + i : |
| | 0 | 344 | | -1; |
| | 0 | 345 | | } |
| | | 346 | | |
| | | 347 | | // comparison stops at null terminator, terminator parameter, or end-of-block -- whichever comes first. |
| | | 348 | | internal bool Utf8NullTerminatedEquals(int offset, string text, MetadataStringDecoder utf8Decoder, char terminat |
| | 0 | 349 | | { |
| | 0 | 350 | | FastComparisonResult result = Utf8NullTerminatedFastCompare(offset, text, 0, out _, terminator, ignoreCase); |
| | | 351 | | |
| | 0 | 352 | | if (result == FastComparisonResult.Inconclusive) |
| | 0 | 353 | | { |
| | 0 | 354 | | string decoded = PeekUtf8NullTerminated(offset, null, utf8Decoder, out _, terminator); |
| | 0 | 355 | | return decoded.Equals(text, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal); |
| | | 356 | | } |
| | | 357 | | |
| | 0 | 358 | | return result == FastComparisonResult.Equal; |
| | 0 | 359 | | } |
| | | 360 | | |
| | | 361 | | // comparison stops at null terminator, terminator parameter, or end-of-block -- whichever comes first. |
| | | 362 | | internal bool Utf8NullTerminatedStartsWith(int offset, string text, MetadataStringDecoder utf8Decoder, char term |
| | 0 | 363 | | { |
| | 0 | 364 | | FastComparisonResult result = Utf8NullTerminatedFastCompare(offset, text, 0, out _, terminator, ignoreCase); |
| | | 365 | | |
| | 0 | 366 | | switch (result) |
| | | 367 | | { |
| | | 368 | | case FastComparisonResult.Equal: |
| | | 369 | | case FastComparisonResult.BytesStartWithText: |
| | 0 | 370 | | return true; |
| | | 371 | | |
| | | 372 | | case FastComparisonResult.Unequal: |
| | | 373 | | case FastComparisonResult.TextStartsWithBytes: |
| | 0 | 374 | | return false; |
| | | 375 | | |
| | | 376 | | default: |
| | 0 | 377 | | Debug.Assert(result == FastComparisonResult.Inconclusive); |
| | 0 | 378 | | string decoded = PeekUtf8NullTerminated(offset, null, utf8Decoder, out _, terminator); |
| | 0 | 379 | | return decoded.StartsWith(text, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.O |
| | | 380 | | } |
| | 0 | 381 | | } |
| | | 382 | | |
| | | 383 | | internal enum FastComparisonResult |
| | | 384 | | { |
| | | 385 | | Equal, |
| | | 386 | | BytesStartWithText, |
| | | 387 | | TextStartsWithBytes, |
| | | 388 | | Unequal, |
| | | 389 | | Inconclusive |
| | | 390 | | } |
| | | 391 | | |
| | | 392 | | // comparison stops at null terminator, terminator parameter, or end-of-block -- whichever comes first. |
| | | 393 | | internal FastComparisonResult Utf8NullTerminatedFastCompare(int offset, string text, int textStart, out int firs |
| | 0 | 394 | | { |
| | 0 | 395 | | CheckBounds(offset, 0); |
| | | 396 | | |
| | 0 | 397 | | Debug.Assert(terminator <= 0x7F); |
| | | 398 | | |
| | 0 | 399 | | byte* startPointer = Pointer + offset; |
| | 0 | 400 | | byte* endPointer = Pointer + Length; |
| | 0 | 401 | | byte* currentPointer = startPointer; |
| | | 402 | | |
| | 0 | 403 | | int ignoreCaseMask = StringUtils.IgnoreCaseMask(ignoreCase); |
| | 0 | 404 | | int currentIndex = textStart; |
| | 0 | 405 | | while (currentIndex < text.Length && currentPointer != endPointer) |
| | 0 | 406 | | { |
| | 0 | 407 | | byte currentByte = *currentPointer; |
| | | 408 | | |
| | | 409 | | // note that terminator is not compared case-insensitively even if ignore case is true |
| | 0 | 410 | | if (currentByte == 0 || currentByte == terminator) |
| | 0 | 411 | | { |
| | 0 | 412 | | break; |
| | | 413 | | } |
| | | 414 | | |
| | 0 | 415 | | char currentChar = text[currentIndex]; |
| | 0 | 416 | | if ((currentByte & 0x80) == 0 && StringUtils.IsEqualAscii(currentChar, currentByte, ignoreCaseMask)) |
| | 0 | 417 | | { |
| | 0 | 418 | | currentIndex++; |
| | 0 | 419 | | currentPointer++; |
| | 0 | 420 | | } |
| | | 421 | | else |
| | 0 | 422 | | { |
| | 0 | 423 | | firstDifferenceIndex = currentIndex; |
| | | 424 | | |
| | | 425 | | // uncommon non-ascii case --> fall back to slow allocating comparison. |
| | 0 | 426 | | return (currentChar > 0x7F) ? FastComparisonResult.Inconclusive : FastComparisonResult.Unequal; |
| | | 427 | | } |
| | 0 | 428 | | } |
| | | 429 | | |
| | 0 | 430 | | firstDifferenceIndex = currentIndex; |
| | | 431 | | |
| | 0 | 432 | | bool textTerminated = currentIndex == text.Length; |
| | 0 | 433 | | bool bytesTerminated = currentPointer == endPointer || *currentPointer == 0 || *currentPointer == terminator |
| | | 434 | | |
| | 0 | 435 | | if (textTerminated && bytesTerminated) |
| | 0 | 436 | | { |
| | 0 | 437 | | return FastComparisonResult.Equal; |
| | | 438 | | } |
| | | 439 | | |
| | 0 | 440 | | return textTerminated ? FastComparisonResult.BytesStartWithText : FastComparisonResult.TextStartsWithBytes; |
| | 0 | 441 | | } |
| | | 442 | | |
| | | 443 | | // comparison stops at null terminator, terminator parameter, or end-of-block -- whichever comes first. |
| | | 444 | | internal bool Utf8NullTerminatedStringStartsWithAsciiPrefix(int offset, string asciiPrefix) |
| | 0 | 445 | | { |
| | | 446 | | // Assumes stringAscii only contains ASCII characters and no nil characters. |
| | | 447 | | |
| | 0 | 448 | | CheckBounds(offset, 0); |
| | | 449 | | |
| | | 450 | | // Make sure that we won't read beyond the block even if the block doesn't end with 0 byte. |
| | 0 | 451 | | if (asciiPrefix.Length > Length - offset) |
| | 0 | 452 | | { |
| | 0 | 453 | | return false; |
| | | 454 | | } |
| | | 455 | | |
| | 0 | 456 | | byte* p = Pointer + offset; |
| | | 457 | | |
| | 0 | 458 | | for (int i = 0; i < asciiPrefix.Length; i++) |
| | 0 | 459 | | { |
| | 0 | 460 | | Debug.Assert(asciiPrefix[i] > 0 && asciiPrefix[i] <= 0x7f); |
| | | 461 | | |
| | 0 | 462 | | if (asciiPrefix[i] != *p) |
| | 0 | 463 | | { |
| | 0 | 464 | | return false; |
| | | 465 | | } |
| | | 466 | | |
| | 0 | 467 | | p++; |
| | 0 | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | return true; |
| | 0 | 471 | | } |
| | | 472 | | |
| | | 473 | | internal int CompareUtf8NullTerminatedStringWithAsciiString(int offset, string asciiString) |
| | 0 | 474 | | { |
| | | 475 | | // Assumes stringAscii only contains ASCII characters and no nil characters. |
| | | 476 | | |
| | 0 | 477 | | CheckBounds(offset, 0); |
| | | 478 | | |
| | 0 | 479 | | byte* p = Pointer + offset; |
| | 0 | 480 | | int limit = Length - offset; |
| | | 481 | | |
| | 0 | 482 | | for (int i = 0; i < asciiString.Length; i++) |
| | 0 | 483 | | { |
| | 0 | 484 | | Debug.Assert(asciiString[i] > 0 && asciiString[i] <= 0x7f); |
| | | 485 | | |
| | 0 | 486 | | if (i > limit) |
| | 0 | 487 | | { |
| | | 488 | | // Heap value is shorter. |
| | 0 | 489 | | return -1; |
| | | 490 | | } |
| | | 491 | | |
| | 0 | 492 | | if (*p != asciiString[i]) |
| | 0 | 493 | | { |
| | | 494 | | // If we hit the end of the heap value (*p == 0) |
| | | 495 | | // the heap value is shorter than the string, so we return negative value. |
| | 0 | 496 | | return *p - asciiString[i]; |
| | | 497 | | } |
| | | 498 | | |
| | 0 | 499 | | p++; |
| | 0 | 500 | | } |
| | | 501 | | |
| | | 502 | | // Either the heap value name matches exactly the given string or |
| | | 503 | | // it is longer so it is considered "greater". |
| | 0 | 504 | | return (*p == 0) ? 0 : +1; |
| | 0 | 505 | | } |
| | | 506 | | |
| | | 507 | | internal byte[] PeekBytes(int offset, int byteCount) |
| | 0 | 508 | | { |
| | 0 | 509 | | CheckBounds(offset, byteCount); |
| | 0 | 510 | | return new ReadOnlySpan<byte>(Pointer + offset, byteCount).ToArray(); |
| | 0 | 511 | | } |
| | | 512 | | |
| | | 513 | | internal int IndexOf(byte b, int start) |
| | 0 | 514 | | { |
| | 0 | 515 | | CheckBounds(start, 0); |
| | 0 | 516 | | return IndexOfUnchecked(b, start); |
| | 0 | 517 | | } |
| | | 518 | | |
| | | 519 | | internal int IndexOfUnchecked(byte b, int start) |
| | 0 | 520 | | { |
| | 0 | 521 | | int i = new ReadOnlySpan<byte>(Pointer + start, Length - start).IndexOf(b); |
| | 0 | 522 | | return i >= 0 ? |
| | 0 | 523 | | i + start : |
| | 0 | 524 | | -1; |
| | 0 | 525 | | } |
| | | 526 | | |
| | | 527 | | // same as Array.BinarySearch, but without using IComparer |
| | | 528 | | internal int BinarySearch(string[] asciiKeys, int offset) |
| | 0 | 529 | | { |
| | 0 | 530 | | var low = 0; |
| | 0 | 531 | | var high = asciiKeys.Length - 1; |
| | | 532 | | |
| | 0 | 533 | | while (low <= high) |
| | 0 | 534 | | { |
| | 0 | 535 | | var middle = low + ((high - low) >> 1); |
| | 0 | 536 | | var midValue = asciiKeys[middle]; |
| | | 537 | | |
| | 0 | 538 | | int comparison = CompareUtf8NullTerminatedStringWithAsciiString(offset, midValue); |
| | 0 | 539 | | if (comparison == 0) |
| | 0 | 540 | | { |
| | 0 | 541 | | return middle; |
| | | 542 | | } |
| | | 543 | | |
| | 0 | 544 | | if (comparison < 0) |
| | 0 | 545 | | { |
| | 0 | 546 | | high = middle - 1; |
| | 0 | 547 | | } |
| | | 548 | | else |
| | 0 | 549 | | { |
| | 0 | 550 | | low = middle + 1; |
| | 0 | 551 | | } |
| | 0 | 552 | | } |
| | | 553 | | |
| | 0 | 554 | | return ~low; |
| | 0 | 555 | | } |
| | | 556 | | |
| | | 557 | | /// <summary> |
| | | 558 | | /// In a table that specifies children via a list field (e.g. TypeDef.FieldList, TypeDef.MethodList), |
| | | 559 | | /// searches for the parent given a reference to a child. |
| | | 560 | | /// </summary> |
| | | 561 | | /// <returns>Returns row number [0..RowCount).</returns> |
| | | 562 | | internal int BinarySearchForSlot( |
| | | 563 | | int rowCount, |
| | | 564 | | int rowSize, |
| | | 565 | | int referenceListOffset, |
| | | 566 | | uint referenceValue, |
| | | 567 | | bool isReferenceSmall) |
| | 0 | 568 | | { |
| | 0 | 569 | | int startRowNumber = 0; |
| | 0 | 570 | | int endRowNumber = rowCount - 1; |
| | 0 | 571 | | uint startValue = PeekReferenceUnchecked(startRowNumber * rowSize + referenceListOffset, isReferenceSmall); |
| | 0 | 572 | | uint endValue = PeekReferenceUnchecked(endRowNumber * rowSize + referenceListOffset, isReferenceSmall); |
| | 0 | 573 | | if (endRowNumber == 1) |
| | 0 | 574 | | { |
| | 0 | 575 | | if (referenceValue >= endValue) |
| | 0 | 576 | | { |
| | 0 | 577 | | return endRowNumber; |
| | | 578 | | } |
| | | 579 | | |
| | 0 | 580 | | return startRowNumber; |
| | | 581 | | } |
| | | 582 | | |
| | 0 | 583 | | while (endRowNumber - startRowNumber > 1) |
| | 0 | 584 | | { |
| | 0 | 585 | | if (referenceValue <= startValue) |
| | 0 | 586 | | { |
| | 0 | 587 | | return referenceValue == startValue ? startRowNumber : startRowNumber - 1; |
| | | 588 | | } |
| | | 589 | | |
| | 0 | 590 | | if (referenceValue >= endValue) |
| | 0 | 591 | | { |
| | 0 | 592 | | return referenceValue == endValue ? endRowNumber : endRowNumber + 1; |
| | | 593 | | } |
| | | 594 | | |
| | 0 | 595 | | int midRowNumber = (startRowNumber + endRowNumber) / 2; |
| | 0 | 596 | | uint midReferenceValue = PeekReferenceUnchecked(midRowNumber * rowSize + referenceListOffset, isReferenc |
| | 0 | 597 | | if (referenceValue > midReferenceValue) |
| | 0 | 598 | | { |
| | 0 | 599 | | startRowNumber = midRowNumber; |
| | 0 | 600 | | startValue = midReferenceValue; |
| | 0 | 601 | | } |
| | 0 | 602 | | else if (referenceValue < midReferenceValue) |
| | 0 | 603 | | { |
| | 0 | 604 | | endRowNumber = midRowNumber; |
| | 0 | 605 | | endValue = midReferenceValue; |
| | 0 | 606 | | } |
| | | 607 | | else |
| | 0 | 608 | | { |
| | 0 | 609 | | return midRowNumber; |
| | | 610 | | } |
| | 0 | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | return startRowNumber; |
| | 0 | 614 | | } |
| | | 615 | | |
| | | 616 | | /// <summary> |
| | | 617 | | /// In a table ordered by a column containing entity references searches for a row with the specified reference. |
| | | 618 | | /// </summary> |
| | | 619 | | /// <returns>Returns row number [0..RowCount) or -1 if not found.</returns> |
| | | 620 | | internal int BinarySearchReference( |
| | | 621 | | int rowCount, |
| | | 622 | | int rowSize, |
| | | 623 | | int referenceOffset, |
| | | 624 | | uint referenceValue, |
| | | 625 | | bool isReferenceSmall) |
| | 0 | 626 | | { |
| | 0 | 627 | | int startRowNumber = 0; |
| | 0 | 628 | | int endRowNumber = rowCount - 1; |
| | 0 | 629 | | while (startRowNumber <= endRowNumber) |
| | 0 | 630 | | { |
| | 0 | 631 | | int midRowNumber = (startRowNumber + endRowNumber) / 2; |
| | 0 | 632 | | uint midReferenceValue = PeekReferenceUnchecked(midRowNumber * rowSize + referenceOffset, isReferenceSma |
| | 0 | 633 | | if (referenceValue > midReferenceValue) |
| | 0 | 634 | | { |
| | 0 | 635 | | startRowNumber = midRowNumber + 1; |
| | 0 | 636 | | } |
| | 0 | 637 | | else if (referenceValue < midReferenceValue) |
| | 0 | 638 | | { |
| | 0 | 639 | | endRowNumber = midRowNumber - 1; |
| | 0 | 640 | | } |
| | | 641 | | else |
| | 0 | 642 | | { |
| | 0 | 643 | | return midRowNumber; |
| | | 644 | | } |
| | 0 | 645 | | } |
| | | 646 | | |
| | 0 | 647 | | return -1; |
| | 0 | 648 | | } |
| | | 649 | | |
| | | 650 | | // Row number [0, ptrTable.Length) or -1 if not found. |
| | | 651 | | internal int BinarySearchReference( |
| | | 652 | | int[] ptrTable, |
| | | 653 | | int rowSize, |
| | | 654 | | int referenceOffset, |
| | | 655 | | uint referenceValue, |
| | | 656 | | bool isReferenceSmall) |
| | 0 | 657 | | { |
| | 0 | 658 | | int startRowNumber = 0; |
| | 0 | 659 | | int endRowNumber = ptrTable.Length - 1; |
| | 0 | 660 | | while (startRowNumber <= endRowNumber) |
| | 0 | 661 | | { |
| | 0 | 662 | | int midRowNumber = (startRowNumber + endRowNumber) / 2; |
| | 0 | 663 | | uint midReferenceValue = PeekReferenceUnchecked((ptrTable[midRowNumber] - 1) * rowSize + referenceOffset |
| | 0 | 664 | | if (referenceValue > midReferenceValue) |
| | 0 | 665 | | { |
| | 0 | 666 | | startRowNumber = midRowNumber + 1; |
| | 0 | 667 | | } |
| | 0 | 668 | | else if (referenceValue < midReferenceValue) |
| | 0 | 669 | | { |
| | 0 | 670 | | endRowNumber = midRowNumber - 1; |
| | 0 | 671 | | } |
| | | 672 | | else |
| | 0 | 673 | | { |
| | 0 | 674 | | return midRowNumber; |
| | | 675 | | } |
| | 0 | 676 | | } |
| | | 677 | | |
| | 0 | 678 | | return -1; |
| | 0 | 679 | | } |
| | | 680 | | |
| | | 681 | | /// <summary> |
| | | 682 | | /// Calculates a range of rows that have specified value in the specified column in a table that is sorted by th |
| | | 683 | | /// </summary> |
| | | 684 | | internal void BinarySearchReferenceRange( |
| | | 685 | | int rowCount, |
| | | 686 | | int rowSize, |
| | | 687 | | int referenceOffset, |
| | | 688 | | uint referenceValue, |
| | | 689 | | bool isReferenceSmall, |
| | | 690 | | out int startRowNumber, // [0, rowCount) or -1 |
| | | 691 | | out int endRowNumber) // [0, rowCount) or -1 |
| | 0 | 692 | | { |
| | 0 | 693 | | int foundRowNumber = BinarySearchReference( |
| | 0 | 694 | | rowCount, |
| | 0 | 695 | | rowSize, |
| | 0 | 696 | | referenceOffset, |
| | 0 | 697 | | referenceValue, |
| | 0 | 698 | | isReferenceSmall |
| | 0 | 699 | | ); |
| | | 700 | | |
| | 0 | 701 | | if (foundRowNumber == -1) |
| | 0 | 702 | | { |
| | 0 | 703 | | startRowNumber = -1; |
| | 0 | 704 | | endRowNumber = -1; |
| | 0 | 705 | | return; |
| | | 706 | | } |
| | | 707 | | |
| | 0 | 708 | | startRowNumber = foundRowNumber; |
| | 0 | 709 | | while (startRowNumber > 0 && |
| | 0 | 710 | | PeekReferenceUnchecked((startRowNumber - 1) * rowSize + referenceOffset, isReferenceSmall) == referen |
| | 0 | 711 | | { |
| | 0 | 712 | | startRowNumber--; |
| | 0 | 713 | | } |
| | | 714 | | |
| | 0 | 715 | | endRowNumber = foundRowNumber; |
| | 0 | 716 | | while (endRowNumber + 1 < rowCount && |
| | 0 | 717 | | PeekReferenceUnchecked((endRowNumber + 1) * rowSize + referenceOffset, isReferenceSmall) == reference |
| | 0 | 718 | | { |
| | 0 | 719 | | endRowNumber++; |
| | 0 | 720 | | } |
| | 0 | 721 | | } |
| | | 722 | | |
| | | 723 | | /// <summary> |
| | | 724 | | /// Calculates a range of rows that have specified value in the specified column in a table that is sorted by th |
| | | 725 | | /// </summary> |
| | | 726 | | internal void BinarySearchReferenceRange( |
| | | 727 | | int[] ptrTable, |
| | | 728 | | int rowSize, |
| | | 729 | | int referenceOffset, |
| | | 730 | | uint referenceValue, |
| | | 731 | | bool isReferenceSmall, |
| | | 732 | | out int startRowNumber, // [0, ptrTable.Length) or -1 |
| | | 733 | | out int endRowNumber) // [0, ptrTable.Length) or -1 |
| | 0 | 734 | | { |
| | 0 | 735 | | int foundRowNumber = BinarySearchReference( |
| | 0 | 736 | | ptrTable, |
| | 0 | 737 | | rowSize, |
| | 0 | 738 | | referenceOffset, |
| | 0 | 739 | | referenceValue, |
| | 0 | 740 | | isReferenceSmall |
| | 0 | 741 | | ); |
| | | 742 | | |
| | 0 | 743 | | if (foundRowNumber == -1) |
| | 0 | 744 | | { |
| | 0 | 745 | | startRowNumber = -1; |
| | 0 | 746 | | endRowNumber = -1; |
| | 0 | 747 | | return; |
| | | 748 | | } |
| | | 749 | | |
| | 0 | 750 | | startRowNumber = foundRowNumber; |
| | 0 | 751 | | while (startRowNumber > 0 && |
| | 0 | 752 | | PeekReferenceUnchecked((ptrTable[startRowNumber - 1] - 1) * rowSize + referenceOffset, isReferenceSma |
| | 0 | 753 | | { |
| | 0 | 754 | | startRowNumber--; |
| | 0 | 755 | | } |
| | | 756 | | |
| | 0 | 757 | | endRowNumber = foundRowNumber; |
| | 0 | 758 | | while (endRowNumber + 1 < ptrTable.Length && |
| | 0 | 759 | | PeekReferenceUnchecked((ptrTable[endRowNumber + 1] - 1) * rowSize + referenceOffset, isReferenceSmall |
| | 0 | 760 | | { |
| | 0 | 761 | | endRowNumber++; |
| | 0 | 762 | | } |
| | 0 | 763 | | } |
| | | 764 | | |
| | | 765 | | // Always RowNumber.... |
| | | 766 | | internal int LinearSearchReference( |
| | | 767 | | int rowSize, |
| | | 768 | | int referenceOffset, |
| | | 769 | | uint referenceValue, |
| | | 770 | | bool isReferenceSmall) |
| | 0 | 771 | | { |
| | 0 | 772 | | int currOffset = referenceOffset; |
| | 0 | 773 | | int totalSize = this.Length; |
| | 0 | 774 | | while (currOffset < totalSize) |
| | 0 | 775 | | { |
| | 0 | 776 | | uint currReference = PeekReferenceUnchecked(currOffset, isReferenceSmall); |
| | 0 | 777 | | if (currReference == referenceValue) |
| | 0 | 778 | | { |
| | 0 | 779 | | return currOffset / rowSize; |
| | | 780 | | } |
| | | 781 | | |
| | 0 | 782 | | currOffset += rowSize; |
| | 0 | 783 | | } |
| | | 784 | | |
| | 0 | 785 | | return -1; |
| | 0 | 786 | | } |
| | | 787 | | |
| | | 788 | | internal bool IsOrderedByReferenceAscending( |
| | | 789 | | int rowSize, |
| | | 790 | | int referenceOffset, |
| | | 791 | | bool isReferenceSmall) |
| | 0 | 792 | | { |
| | 0 | 793 | | int offset = referenceOffset; |
| | 0 | 794 | | int totalSize = this.Length; |
| | | 795 | | |
| | 0 | 796 | | uint previous = 0; |
| | 0 | 797 | | while (offset < totalSize) |
| | 0 | 798 | | { |
| | 0 | 799 | | uint current = PeekReferenceUnchecked(offset, isReferenceSmall); |
| | 0 | 800 | | if (current < previous) |
| | 0 | 801 | | { |
| | 0 | 802 | | return false; |
| | | 803 | | } |
| | | 804 | | |
| | 0 | 805 | | previous = current; |
| | 0 | 806 | | offset += rowSize; |
| | 0 | 807 | | } |
| | | 808 | | |
| | 0 | 809 | | return true; |
| | 0 | 810 | | } |
| | | 811 | | |
| | | 812 | | internal int[] BuildPtrTable( |
| | | 813 | | int numberOfRows, |
| | | 814 | | int rowSize, |
| | | 815 | | int referenceOffset, |
| | | 816 | | bool isReferenceSmall) |
| | 0 | 817 | | { |
| | 0 | 818 | | int[] ptrTable = new int[numberOfRows]; |
| | 0 | 819 | | uint[] unsortedReferences = new uint[numberOfRows]; |
| | | 820 | | |
| | 0 | 821 | | for (int i = 0; i < ptrTable.Length; i++) |
| | 0 | 822 | | { |
| | 0 | 823 | | ptrTable[i] = i + 1; |
| | 0 | 824 | | } |
| | | 825 | | |
| | 0 | 826 | | ReadColumn(unsortedReferences, rowSize, referenceOffset, isReferenceSmall); |
| | 0 | 827 | | Array.Sort(ptrTable, (int a, int b) => { return unsortedReferences[a - 1].CompareTo(unsortedReferences[b - 1 |
| | 0 | 828 | | return ptrTable; |
| | 0 | 829 | | } |
| | | 830 | | |
| | | 831 | | private void ReadColumn( |
| | | 832 | | uint[] result, |
| | | 833 | | int rowSize, |
| | | 834 | | int referenceOffset, |
| | | 835 | | bool isReferenceSmall) |
| | 0 | 836 | | { |
| | 0 | 837 | | int offset = referenceOffset; |
| | 0 | 838 | | int totalSize = this.Length; |
| | | 839 | | |
| | 0 | 840 | | int i = 0; |
| | 0 | 841 | | while (offset < totalSize) |
| | 0 | 842 | | { |
| | 0 | 843 | | result[i] = PeekReferenceUnchecked(offset, isReferenceSmall); |
| | 0 | 844 | | offset += rowSize; |
| | 0 | 845 | | i++; |
| | 0 | 846 | | } |
| | | 847 | | |
| | 0 | 848 | | Debug.Assert(i == result.Length); |
| | 0 | 849 | | } |
| | | 850 | | |
| | | 851 | | internal bool PeekHeapValueOffsetAndSize(int index, out int offset, out int size) |
| | 0 | 852 | | { |
| | | 853 | | int bytesRead; |
| | 0 | 854 | | int numberOfBytes = PeekCompressedInteger(index, out bytesRead); |
| | 0 | 855 | | if (numberOfBytes == BlobReader.InvalidCompressedInteger) |
| | 0 | 856 | | { |
| | 0 | 857 | | offset = 0; |
| | 0 | 858 | | size = 0; |
| | 0 | 859 | | return false; |
| | | 860 | | } |
| | | 861 | | |
| | 0 | 862 | | offset = index + bytesRead; |
| | 0 | 863 | | size = numberOfBytes; |
| | 0 | 864 | | return true; |
| | 0 | 865 | | } |
| | | 866 | | } |
| | | 867 | | } |
| | | 868 | | |