| | | 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.Internal; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | |
| | | 9 | | namespace System.Reflection |
| | | 10 | | { |
| | | 11 | | internal static class BlobUtilities |
| | | 12 | | { |
| | | 13 | | public static void WriteBytes(this byte[] buffer, int start, byte value, int byteCount) |
| | 0 | 14 | | { |
| | 0 | 15 | | Debug.Assert(buffer.Length > 0); |
| | 0 | 16 | | new Span<byte>(buffer, start, byteCount).Fill(value); |
| | 0 | 17 | | } |
| | | 18 | | |
| | | 19 | | public static void WriteDouble(this byte[] buffer, int start, double value) => |
| | 0 | 20 | | WriteUInt64(buffer, start, BitConverter.DoubleToUInt64Bits(value)); |
| | | 21 | | |
| | | 22 | | public static void WriteSingle(this byte[] buffer, int start, float value) => |
| | 0 | 23 | | WriteUInt32(buffer, start, BitConverter.SingleToUInt32Bits(value)); |
| | | 24 | | |
| | | 25 | | public static void WriteUInt16(this byte[] buffer, int start, ushort value) => |
| | 0 | 26 | | BinaryPrimitives.WriteUInt16LittleEndian(buffer.AsSpan(start), value); |
| | | 27 | | |
| | | 28 | | public static void WriteUInt16BE(this byte[] buffer, int start, ushort value) => |
| | 0 | 29 | | BinaryPrimitives.WriteUInt16BigEndian(buffer.AsSpan(start), value); |
| | | 30 | | |
| | | 31 | | public static void WriteUInt32BE(this byte[] buffer, int start, uint value) => |
| | 0 | 32 | | BinaryPrimitives.WriteUInt32BigEndian(buffer.AsSpan(start), value); |
| | | 33 | | |
| | | 34 | | public static void WriteUInt32(this byte[] buffer, int start, uint value) => |
| | 0 | 35 | | BinaryPrimitives.WriteUInt32LittleEndian(buffer.AsSpan(start), value); |
| | | 36 | | |
| | | 37 | | public static void WriteUInt64(this byte[] buffer, int start, ulong value) => |
| | 0 | 38 | | BinaryPrimitives.WriteUInt64LittleEndian(buffer.AsSpan(start), value); |
| | | 39 | | |
| | | 40 | | public const int SizeOfSerializedDecimal = sizeof(byte) + 3 * sizeof(uint); |
| | | 41 | | |
| | | 42 | | public static void WriteDecimal(this byte[] buffer, int start, decimal value) |
| | 0 | 43 | | { |
| | | 44 | | bool isNegative; |
| | | 45 | | byte scale; |
| | | 46 | | uint low, mid, high; |
| | 0 | 47 | | value.GetBits(out isNegative, out scale, out low, out mid, out high); |
| | | 48 | | |
| | 0 | 49 | | buffer[start] = (byte)(scale | (isNegative ? 0x80 : 0x00)); |
| | 0 | 50 | | WriteUInt32(buffer, start + 1, low); |
| | 0 | 51 | | WriteUInt32(buffer, start + 5, mid); |
| | 0 | 52 | | WriteUInt32(buffer, start + 9, high); |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public const int SizeOfGuid = 16; |
| | | 56 | | |
| | | 57 | | public static void WriteGuid(this byte[] buffer, int start, Guid value) |
| | 0 | 58 | | { |
| | 0 | 59 | | bool written = value.TryWriteBytes(buffer.AsSpan(start)); |
| | | 60 | | // This function is not public, callers have to ensure that enough space is available. |
| | 0 | 61 | | Debug.Assert(written); |
| | 0 | 62 | | } |
| | | 63 | | |
| | | 64 | | public static unsafe void WriteUTF8(this byte[] buffer, int start, char* charPtr, int charCount, int byteCount, |
| | 0 | 65 | | { |
| | 0 | 66 | | Debug.Assert(byteCount >= charCount); |
| | | 67 | | const char ReplacementCharacter = '\uFFFD'; |
| | | 68 | | |
| | 0 | 69 | | char* strEnd = charPtr + charCount; |
| | 0 | 70 | | fixed (byte* bufferPtr = &buffer[0]) |
| | 0 | 71 | | { |
| | 0 | 72 | | byte* ptr = bufferPtr + start; |
| | | 73 | | |
| | 0 | 74 | | if (byteCount == charCount) |
| | 0 | 75 | | { |
| | 0 | 76 | | while (charPtr < strEnd) |
| | 0 | 77 | | { |
| | 0 | 78 | | Debug.Assert(*charPtr <= 0x7f); |
| | 0 | 79 | | *ptr++ = unchecked((byte)*charPtr++); |
| | 0 | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | else |
| | 0 | 83 | | { |
| | 0 | 84 | | while (charPtr < strEnd) |
| | 0 | 85 | | { |
| | 0 | 86 | | char c = *charPtr++; |
| | | 87 | | |
| | 0 | 88 | | if (c < 0x80) |
| | 0 | 89 | | { |
| | 0 | 90 | | *ptr++ = (byte)c; |
| | 0 | 91 | | continue; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | if (c < 0x800) |
| | 0 | 95 | | { |
| | 0 | 96 | | ptr[0] = (byte)(((c >> 6) & 0x1F) | 0xC0); |
| | 0 | 97 | | ptr[1] = (byte)((c & 0x3F) | 0x80); |
| | 0 | 98 | | ptr += 2; |
| | 0 | 99 | | continue; |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | if (char.IsSurrogate(c)) |
| | 0 | 103 | | { |
| | | 104 | | // surrogate pair |
| | 0 | 105 | | if (char.IsHighSurrogate(c) && charPtr < strEnd && char.IsLowSurrogate(*charPtr)) |
| | 0 | 106 | | { |
| | 0 | 107 | | int highSurrogate = c; |
| | 0 | 108 | | int lowSurrogate = *charPtr++; |
| | 0 | 109 | | int codepoint = (((highSurrogate - 0xd800) << 10) + lowSurrogate - 0xdc00) + 0x10000; |
| | 0 | 110 | | ptr[0] = (byte)(((codepoint >> 18) & 0x7) | 0xF0); |
| | 0 | 111 | | ptr[1] = (byte)(((codepoint >> 12) & 0x3F) | 0x80); |
| | 0 | 112 | | ptr[2] = (byte)(((codepoint >> 6) & 0x3F) | 0x80); |
| | 0 | 113 | | ptr[3] = (byte)((codepoint & 0x3F) | 0x80); |
| | 0 | 114 | | ptr += 4; |
| | 0 | 115 | | continue; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | // unpaired high/low surrogate |
| | 0 | 119 | | if (!allowUnpairedSurrogates) |
| | 0 | 120 | | { |
| | 0 | 121 | | c = ReplacementCharacter; |
| | 0 | 122 | | } |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | ptr[0] = (byte)(((c >> 12) & 0xF) | 0xE0); |
| | 0 | 126 | | ptr[1] = (byte)(((c >> 6) & 0x3F) | 0x80); |
| | 0 | 127 | | ptr[2] = (byte)((c & 0x3F) | 0x80); |
| | 0 | 128 | | ptr += 3; |
| | 0 | 129 | | } |
| | 0 | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | Debug.Assert(ptr == bufferPtr + start + byteCount); |
| | 0 | 133 | | Debug.Assert(charPtr == strEnd); |
| | 0 | 134 | | } |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | internal static unsafe int GetUTF8ByteCount(char* str, int charCount) |
| | 0 | 138 | | { |
| | 0 | 139 | | return GetUTF8ByteCount(str, charCount, int.MaxValue, out _); |
| | 0 | 140 | | } |
| | | 141 | | |
| | | 142 | | internal static unsafe int GetUTF8ByteCount(char* str, int charCount, int byteLimit, out char* remainder) |
| | 0 | 143 | | { |
| | 0 | 144 | | char* end = str + charCount; |
| | | 145 | | |
| | 0 | 146 | | char* ptr = str; |
| | 0 | 147 | | int byteCount = 0; |
| | 0 | 148 | | while (ptr < end) |
| | 0 | 149 | | { |
| | | 150 | | int characterSize; |
| | 0 | 151 | | char c = *ptr++; |
| | 0 | 152 | | if (c < 0x80) |
| | 0 | 153 | | { |
| | 0 | 154 | | characterSize = 1; |
| | 0 | 155 | | } |
| | 0 | 156 | | else if (c < 0x800) |
| | 0 | 157 | | { |
| | 0 | 158 | | characterSize = 2; |
| | 0 | 159 | | } |
| | 0 | 160 | | else if (char.IsHighSurrogate(c) && ptr < end && char.IsLowSurrogate(*ptr)) |
| | 0 | 161 | | { |
| | | 162 | | // surrogate pair: |
| | 0 | 163 | | characterSize = 4; |
| | 0 | 164 | | ptr++; |
| | 0 | 165 | | } |
| | | 166 | | else |
| | 0 | 167 | | { |
| | 0 | 168 | | characterSize = 3; |
| | 0 | 169 | | } |
| | | 170 | | |
| | 0 | 171 | | if (byteCount + characterSize > byteLimit) |
| | 0 | 172 | | { |
| | 0 | 173 | | ptr -= (characterSize < 4) ? 1 : 2; |
| | 0 | 174 | | break; |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | byteCount += characterSize; |
| | 0 | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | remainder = ptr; |
| | 0 | 181 | | return byteCount; |
| | 0 | 182 | | } |
| | | 183 | | |
| | | 184 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 185 | | internal static void ValidateRange(int bufferLength, int start, int byteCount, string byteCountParameterName) |
| | 0 | 186 | | { |
| | 0 | 187 | | if (start < 0 || start > bufferLength) |
| | 0 | 188 | | { |
| | 0 | 189 | | Throw.ArgumentOutOfRange(nameof(start)); |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | if (byteCount < 0 || byteCount > bufferLength - start) |
| | 0 | 193 | | { |
| | 0 | 194 | | Throw.ArgumentOutOfRange(byteCountParameterName); |
| | | 195 | | } |
| | 0 | 196 | | } |
| | | 197 | | |
| | | 198 | | internal static int GetUserStringByteLength(int characterCount) |
| | 0 | 199 | | { |
| | 0 | 200 | | return characterCount * 2 + 1; |
| | 0 | 201 | | } |
| | | 202 | | |
| | | 203 | | internal static byte GetUserStringTrailingByte(string str) |
| | 0 | 204 | | { |
| | | 205 | | // ECMA-335 II.24.2.4: |
| | | 206 | | // This final byte holds the value 1 if and only if any UTF16 character within |
| | | 207 | | // the string has any bit set in its top byte, or its low byte is any of the following: |
| | | 208 | | // 0x01-0x08, 0x0E-0x1F, 0x27, 0x2D, 0x7F. Otherwise, it holds 0. |
| | | 209 | | // The 1 signifies Unicode characters that require handling beyond that normally provided for 8-bit encoding |
| | | 210 | | |
| | 0 | 211 | | foreach (char ch in str) |
| | 0 | 212 | | { |
| | 0 | 213 | | if (ch >= 0x7F) |
| | 0 | 214 | | { |
| | 0 | 215 | | return 1; |
| | | 216 | | } |
| | | 217 | | |
| | 0 | 218 | | switch ((int)ch) |
| | | 219 | | { |
| | | 220 | | case 0x1: |
| | | 221 | | case 0x2: |
| | | 222 | | case 0x3: |
| | | 223 | | case 0x4: |
| | | 224 | | case 0x5: |
| | | 225 | | case 0x6: |
| | | 226 | | case 0x7: |
| | | 227 | | case 0x8: |
| | | 228 | | case 0xE: |
| | | 229 | | case 0xF: |
| | | 230 | | case 0x10: |
| | | 231 | | case 0x11: |
| | | 232 | | case 0x12: |
| | | 233 | | case 0x13: |
| | | 234 | | case 0x14: |
| | | 235 | | case 0x15: |
| | | 236 | | case 0x16: |
| | | 237 | | case 0x17: |
| | | 238 | | case 0x18: |
| | | 239 | | case 0x19: |
| | | 240 | | case 0x1A: |
| | | 241 | | case 0x1B: |
| | | 242 | | case 0x1C: |
| | | 243 | | case 0x1D: |
| | | 244 | | case 0x1E: |
| | | 245 | | case 0x1F: |
| | | 246 | | case 0x27: |
| | | 247 | | case 0x2D: |
| | 0 | 248 | | return 1; |
| | | 249 | | } |
| | 0 | 250 | | } |
| | | 251 | | |
| | 0 | 252 | | return 0; |
| | 0 | 253 | | } |
| | | 254 | | } |
| | | 255 | | } |
| | | 256 | | |