| | | 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 | | |
| | | 7 | | namespace System.Reflection.Metadata |
| | | 8 | | { |
| | | 9 | | internal static class BlobWriterImpl |
| | | 10 | | { |
| | | 11 | | internal const int SingleByteCompressedIntegerMaxValue = 0x7f; |
| | | 12 | | internal const int TwoByteCompressedIntegerMaxValue = 0x3fff; |
| | | 13 | | internal const int MaxCompressedIntegerValue = 0x1fffffff; |
| | | 14 | | internal const int MinSignedCompressedIntegerValue = unchecked((int)0xF0000000); |
| | | 15 | | internal const int MaxSignedCompressedIntegerValue = 0x0FFFFFFF; |
| | | 16 | | internal const int MaxScalarConstantSize = sizeof(ulong); |
| | | 17 | | |
| | | 18 | | internal static int GetCompressedIntegerSize(int value) |
| | 0 | 19 | | { |
| | 0 | 20 | | Debug.Assert(value <= MaxCompressedIntegerValue); |
| | | 21 | | |
| | 0 | 22 | | if (value <= SingleByteCompressedIntegerMaxValue) |
| | 0 | 23 | | { |
| | 0 | 24 | | return 1; |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | if (value <= TwoByteCompressedIntegerMaxValue) |
| | 0 | 28 | | { |
| | 0 | 29 | | return 2; |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | return 4; |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | internal static void WriteCompressedInteger(ref BlobWriter writer, uint value) |
| | 0 | 36 | | { |
| | | 37 | | unchecked |
| | 0 | 38 | | { |
| | 0 | 39 | | if (value <= SingleByteCompressedIntegerMaxValue) |
| | 0 | 40 | | { |
| | 0 | 41 | | writer.WriteByte((byte)value); |
| | 0 | 42 | | } |
| | 0 | 43 | | else if (value <= TwoByteCompressedIntegerMaxValue) |
| | 0 | 44 | | { |
| | 0 | 45 | | writer.WriteUInt16BE((ushort)(0x8000 | value)); |
| | 0 | 46 | | } |
| | 0 | 47 | | else if (value <= MaxCompressedIntegerValue) |
| | 0 | 48 | | { |
| | 0 | 49 | | writer.WriteUInt32BE(0xc0000000 | value); |
| | 0 | 50 | | } |
| | | 51 | | else |
| | 0 | 52 | | { |
| | 0 | 53 | | Throw.ValueArgumentOutOfRange(); |
| | | 54 | | } |
| | 0 | 55 | | } |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | internal static void WriteCompressedInteger(BlobBuilder writer, uint value) |
| | 0 | 59 | | { |
| | | 60 | | unchecked |
| | 0 | 61 | | { |
| | 0 | 62 | | if (value <= SingleByteCompressedIntegerMaxValue) |
| | 0 | 63 | | { |
| | 0 | 64 | | writer.WriteByte((byte)value); |
| | 0 | 65 | | } |
| | 0 | 66 | | else if (value <= TwoByteCompressedIntegerMaxValue) |
| | 0 | 67 | | { |
| | 0 | 68 | | writer.WriteUInt16BE((ushort)(0x8000 | value)); |
| | 0 | 69 | | } |
| | 0 | 70 | | else if (value <= MaxCompressedIntegerValue) |
| | 0 | 71 | | { |
| | 0 | 72 | | writer.WriteUInt32BE(0xc0000000 | value); |
| | 0 | 73 | | } |
| | | 74 | | else |
| | 0 | 75 | | { |
| | 0 | 76 | | Throw.ValueArgumentOutOfRange(); |
| | | 77 | | } |
| | 0 | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | internal static void WriteCompressedSignedInteger(ref BlobWriter writer, int value) |
| | 0 | 82 | | { |
| | | 83 | | unchecked |
| | 0 | 84 | | { |
| | | 85 | | const int b6 = (1 << 6) - 1; |
| | | 86 | | const int b13 = (1 << 13) - 1; |
| | | 87 | | const int b28 = (1 << 28) - 1; |
| | | 88 | | |
| | | 89 | | // 0xffffffff for negative value |
| | | 90 | | // 0x00000000 for non-negative |
| | 0 | 91 | | int signMask = value >> 31; |
| | | 92 | | |
| | 0 | 93 | | if ((value & ~b6) == (signMask & ~b6)) |
| | 0 | 94 | | { |
| | 0 | 95 | | int n = ((value & b6) << 1) | (signMask & 1); |
| | 0 | 96 | | writer.WriteByte((byte)n); |
| | 0 | 97 | | } |
| | 0 | 98 | | else if ((value & ~b13) == (signMask & ~b13)) |
| | 0 | 99 | | { |
| | 0 | 100 | | int n = ((value & b13) << 1) | (signMask & 1); |
| | 0 | 101 | | writer.WriteUInt16BE((ushort)(0x8000 | n)); |
| | 0 | 102 | | } |
| | 0 | 103 | | else if ((value & ~b28) == (signMask & ~b28)) |
| | 0 | 104 | | { |
| | 0 | 105 | | int n = ((value & b28) << 1) | (signMask & 1); |
| | 0 | 106 | | writer.WriteUInt32BE(0xc0000000 | (uint)n); |
| | 0 | 107 | | } |
| | | 108 | | else |
| | 0 | 109 | | { |
| | 0 | 110 | | Throw.ValueArgumentOutOfRange(); |
| | | 111 | | } |
| | 0 | 112 | | } |
| | 0 | 113 | | } |
| | | 114 | | |
| | | 115 | | internal static void WriteCompressedSignedInteger(BlobBuilder writer, int value) |
| | 0 | 116 | | { |
| | | 117 | | unchecked |
| | 0 | 118 | | { |
| | | 119 | | const int b6 = (1 << 6) - 1; |
| | | 120 | | const int b13 = (1 << 13) - 1; |
| | | 121 | | const int b28 = (1 << 28) - 1; |
| | | 122 | | |
| | | 123 | | // 0xffffffff for negative value |
| | | 124 | | // 0x00000000 for non-negative |
| | 0 | 125 | | int signMask = value >> 31; |
| | | 126 | | |
| | 0 | 127 | | if ((value & ~b6) == (signMask & ~b6)) |
| | 0 | 128 | | { |
| | 0 | 129 | | int n = ((value & b6) << 1) | (signMask & 1); |
| | 0 | 130 | | writer.WriteByte((byte)n); |
| | 0 | 131 | | } |
| | 0 | 132 | | else if ((value & ~b13) == (signMask & ~b13)) |
| | 0 | 133 | | { |
| | 0 | 134 | | int n = ((value & b13) << 1) | (signMask & 1); |
| | 0 | 135 | | writer.WriteUInt16BE((ushort)(0x8000 | n)); |
| | 0 | 136 | | } |
| | 0 | 137 | | else if ((value & ~b28) == (signMask & ~b28)) |
| | 0 | 138 | | { |
| | 0 | 139 | | int n = ((value & b28) << 1) | (signMask & 1); |
| | 0 | 140 | | writer.WriteUInt32BE(0xc0000000 | (uint)n); |
| | 0 | 141 | | } |
| | | 142 | | else |
| | 0 | 143 | | { |
| | 0 | 144 | | Throw.ValueArgumentOutOfRange(); |
| | | 145 | | } |
| | 0 | 146 | | } |
| | 0 | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Writes a scalar (non-string) constant to a span. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="bytes">The span where the content will be encoded.</param> |
| | | 153 | | /// <param name="value">The constant value.</param> |
| | | 154 | | /// <returns>The number of bytes that was written.</returns> |
| | | 155 | | internal static int WriteScalarConstant(Span<byte> bytes, object? value) |
| | 0 | 156 | | { |
| | 0 | 157 | | if (value == null) |
| | 0 | 158 | | { |
| | | 159 | | // The encoding of Type for the nullref value for FieldInit is ELEMENT_TYPE_CLASS with a Value of a 4-by |
| | 0 | 160 | | BinaryPrimitives.WriteUInt32LittleEndian(bytes, 0); |
| | 0 | 161 | | return sizeof(uint); |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | var type = value.GetType(); |
| | 0 | 165 | | if (type.IsEnum) |
| | 0 | 166 | | { |
| | 0 | 167 | | type = Enum.GetUnderlyingType(type); |
| | 0 | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | if (type == typeof(bool)) |
| | 0 | 171 | | { |
| | 0 | 172 | | bytes[0] = (byte)((bool)value ? 1 : 0); |
| | 0 | 173 | | return sizeof(bool); |
| | | 174 | | } |
| | 0 | 175 | | else if (type == typeof(int)) |
| | 0 | 176 | | { |
| | 0 | 177 | | BinaryPrimitives.WriteInt32LittleEndian(bytes, (int)value); |
| | 0 | 178 | | return sizeof(int); |
| | | 179 | | } |
| | 0 | 180 | | else if (type == typeof(byte)) |
| | 0 | 181 | | { |
| | 0 | 182 | | bytes[0] = (byte)value; |
| | 0 | 183 | | return sizeof(byte); |
| | | 184 | | } |
| | 0 | 185 | | else if (type == typeof(char)) |
| | 0 | 186 | | { |
| | 0 | 187 | | BinaryPrimitives.WriteUInt16LittleEndian(bytes, (char)value); |
| | 0 | 188 | | return sizeof(char); |
| | | 189 | | } |
| | 0 | 190 | | else if (type == typeof(double)) |
| | 0 | 191 | | { |
| | | 192 | | #if NET |
| | 0 | 193 | | BinaryPrimitives.WriteDoubleLittleEndian(bytes, (double)value); |
| | | 194 | | #else |
| | | 195 | | double v = (double)value; |
| | | 196 | | unsafe |
| | | 197 | | { |
| | | 198 | | BinaryPrimitives.WriteUInt64LittleEndian(bytes, *(ulong*)(&v)); |
| | | 199 | | } |
| | | 200 | | #endif |
| | 0 | 201 | | return sizeof(double); |
| | | 202 | | } |
| | 0 | 203 | | else if (type == typeof(short)) |
| | 0 | 204 | | { |
| | 0 | 205 | | BinaryPrimitives.WriteInt16LittleEndian(bytes, (short)value); |
| | 0 | 206 | | return sizeof(short); |
| | | 207 | | } |
| | 0 | 208 | | else if (type == typeof(long)) |
| | 0 | 209 | | { |
| | 0 | 210 | | BinaryPrimitives.WriteInt64LittleEndian(bytes, (long)value); |
| | 0 | 211 | | return sizeof(long); |
| | | 212 | | } |
| | 0 | 213 | | else if (type == typeof(sbyte)) |
| | 0 | 214 | | { |
| | 0 | 215 | | bytes[0] = (byte)(sbyte)value; |
| | 0 | 216 | | return sizeof(sbyte); |
| | | 217 | | } |
| | 0 | 218 | | else if (type == typeof(float)) |
| | 0 | 219 | | { |
| | | 220 | | #if NET |
| | 0 | 221 | | BinaryPrimitives.WriteSingleLittleEndian(bytes, (float)value); |
| | | 222 | | #else |
| | | 223 | | float v = (float)value; |
| | | 224 | | unsafe |
| | | 225 | | { |
| | | 226 | | BinaryPrimitives.WriteUInt32LittleEndian(bytes, *(uint*)(&v)); |
| | | 227 | | } |
| | | 228 | | #endif |
| | 0 | 229 | | return sizeof(float); |
| | | 230 | | } |
| | 0 | 231 | | else if (type == typeof(ushort)) |
| | 0 | 232 | | { |
| | 0 | 233 | | BinaryPrimitives.WriteUInt16LittleEndian(bytes, (ushort)value); |
| | 0 | 234 | | return sizeof(ushort); |
| | | 235 | | } |
| | 0 | 236 | | else if (type == typeof(uint)) |
| | 0 | 237 | | { |
| | 0 | 238 | | BinaryPrimitives.WriteUInt32LittleEndian(bytes, (uint)value); |
| | 0 | 239 | | return sizeof(uint); |
| | | 240 | | } |
| | 0 | 241 | | else if (type == typeof(ulong)) |
| | 0 | 242 | | { |
| | 0 | 243 | | BinaryPrimitives.WriteUInt64LittleEndian(bytes, (ulong)value); |
| | 0 | 244 | | return sizeof(ulong); |
| | | 245 | | } |
| | | 246 | | else |
| | 0 | 247 | | { |
| | 0 | 248 | | throw new ArgumentException(SR.Format(SR.InvalidConstantValueOfType, type)); |
| | | 249 | | } |
| | 0 | 250 | | } |
| | | 251 | | |
| | | 252 | | internal static void WriteConstant(ref BlobWriter writer, object? value) |
| | 0 | 253 | | { |
| | 0 | 254 | | if (value == null) |
| | 0 | 255 | | { |
| | | 256 | | // The encoding of Type for the nullref value for FieldInit is ELEMENT_TYPE_CLASS with a Value of a 4-by |
| | 0 | 257 | | writer.WriteUInt32(0); |
| | 0 | 258 | | return; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | var type = value.GetType(); |
| | 0 | 262 | | if (type.IsEnum) |
| | 0 | 263 | | { |
| | 0 | 264 | | type = Enum.GetUnderlyingType(type); |
| | 0 | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | if (type == typeof(bool)) |
| | 0 | 268 | | { |
| | 0 | 269 | | writer.WriteBoolean((bool)value); |
| | 0 | 270 | | } |
| | 0 | 271 | | else if (type == typeof(int)) |
| | 0 | 272 | | { |
| | 0 | 273 | | writer.WriteInt32((int)value); |
| | 0 | 274 | | } |
| | 0 | 275 | | else if (type == typeof(string)) |
| | 0 | 276 | | { |
| | 0 | 277 | | writer.WriteUTF16((string)value); |
| | 0 | 278 | | } |
| | 0 | 279 | | else if (type == typeof(byte)) |
| | 0 | 280 | | { |
| | 0 | 281 | | writer.WriteByte((byte)value); |
| | 0 | 282 | | } |
| | 0 | 283 | | else if (type == typeof(char)) |
| | 0 | 284 | | { |
| | 0 | 285 | | writer.WriteUInt16((char)value); |
| | 0 | 286 | | } |
| | 0 | 287 | | else if (type == typeof(double)) |
| | 0 | 288 | | { |
| | 0 | 289 | | writer.WriteDouble((double)value); |
| | 0 | 290 | | } |
| | 0 | 291 | | else if (type == typeof(short)) |
| | 0 | 292 | | { |
| | 0 | 293 | | writer.WriteInt16((short)value); |
| | 0 | 294 | | } |
| | 0 | 295 | | else if (type == typeof(long)) |
| | 0 | 296 | | { |
| | 0 | 297 | | writer.WriteInt64((long)value); |
| | 0 | 298 | | } |
| | 0 | 299 | | else if (type == typeof(sbyte)) |
| | 0 | 300 | | { |
| | 0 | 301 | | writer.WriteSByte((sbyte)value); |
| | 0 | 302 | | } |
| | 0 | 303 | | else if (type == typeof(float)) |
| | 0 | 304 | | { |
| | 0 | 305 | | writer.WriteSingle((float)value); |
| | 0 | 306 | | } |
| | 0 | 307 | | else if (type == typeof(ushort)) |
| | 0 | 308 | | { |
| | 0 | 309 | | writer.WriteUInt16((ushort)value); |
| | 0 | 310 | | } |
| | 0 | 311 | | else if (type == typeof(uint)) |
| | 0 | 312 | | { |
| | 0 | 313 | | writer.WriteUInt32((uint)value); |
| | 0 | 314 | | } |
| | 0 | 315 | | else if (type == typeof(ulong)) |
| | 0 | 316 | | { |
| | 0 | 317 | | writer.WriteUInt64((ulong)value); |
| | 0 | 318 | | } |
| | | 319 | | else |
| | 0 | 320 | | { |
| | 0 | 321 | | throw new ArgumentException(SR.Format(SR.InvalidConstantValueOfType, type)); |
| | | 322 | | } |
| | 0 | 323 | | } |
| | | 324 | | |
| | | 325 | | internal static void WriteConstant(BlobBuilder writer, object? value) |
| | 0 | 326 | | { |
| | 0 | 327 | | if (value == null) |
| | 0 | 328 | | { |
| | | 329 | | // The encoding of Type for the nullref value for FieldInit is ELEMENT_TYPE_CLASS with a Value of a 4-by |
| | 0 | 330 | | writer.WriteUInt32(0); |
| | 0 | 331 | | return; |
| | | 332 | | } |
| | | 333 | | |
| | 0 | 334 | | var type = value.GetType(); |
| | 0 | 335 | | if (type.IsEnum) |
| | 0 | 336 | | { |
| | 0 | 337 | | type = Enum.GetUnderlyingType(type); |
| | 0 | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | if (type == typeof(bool)) |
| | 0 | 341 | | { |
| | 0 | 342 | | writer.WriteBoolean((bool)value); |
| | 0 | 343 | | } |
| | 0 | 344 | | else if (type == typeof(int)) |
| | 0 | 345 | | { |
| | 0 | 346 | | writer.WriteInt32((int)value); |
| | 0 | 347 | | } |
| | 0 | 348 | | else if (type == typeof(string)) |
| | 0 | 349 | | { |
| | 0 | 350 | | writer.WriteUTF16((string)value); |
| | 0 | 351 | | } |
| | 0 | 352 | | else if (type == typeof(byte)) |
| | 0 | 353 | | { |
| | 0 | 354 | | writer.WriteByte((byte)value); |
| | 0 | 355 | | } |
| | 0 | 356 | | else if (type == typeof(char)) |
| | 0 | 357 | | { |
| | 0 | 358 | | writer.WriteUInt16((char)value); |
| | 0 | 359 | | } |
| | 0 | 360 | | else if (type == typeof(double)) |
| | 0 | 361 | | { |
| | 0 | 362 | | writer.WriteDouble((double)value); |
| | 0 | 363 | | } |
| | 0 | 364 | | else if (type == typeof(short)) |
| | 0 | 365 | | { |
| | 0 | 366 | | writer.WriteInt16((short)value); |
| | 0 | 367 | | } |
| | 0 | 368 | | else if (type == typeof(long)) |
| | 0 | 369 | | { |
| | 0 | 370 | | writer.WriteInt64((long)value); |
| | 0 | 371 | | } |
| | 0 | 372 | | else if (type == typeof(sbyte)) |
| | 0 | 373 | | { |
| | 0 | 374 | | writer.WriteSByte((sbyte)value); |
| | 0 | 375 | | } |
| | 0 | 376 | | else if (type == typeof(float)) |
| | 0 | 377 | | { |
| | 0 | 378 | | writer.WriteSingle((float)value); |
| | 0 | 379 | | } |
| | 0 | 380 | | else if (type == typeof(ushort)) |
| | 0 | 381 | | { |
| | 0 | 382 | | writer.WriteUInt16((ushort)value); |
| | 0 | 383 | | } |
| | 0 | 384 | | else if (type == typeof(uint)) |
| | 0 | 385 | | { |
| | 0 | 386 | | writer.WriteUInt32((uint)value); |
| | 0 | 387 | | } |
| | 0 | 388 | | else if (type == typeof(ulong)) |
| | 0 | 389 | | { |
| | 0 | 390 | | writer.WriteUInt64((ulong)value); |
| | 0 | 391 | | } |
| | | 392 | | else |
| | 0 | 393 | | { |
| | 0 | 394 | | throw new ArgumentException(SR.Format(SR.InvalidConstantValueOfType, type)); |
| | | 395 | | } |
| | 0 | 396 | | } |
| | | 397 | | } |
| | | 398 | | } |
| | | 399 | | |