< Summary

Information
Class: System.Reflection.Metadata.BlobWriterImpl
Assembly: System.Reflection.Metadata
File(s): D:\runner\runtime\src\libraries\System.Reflection.Metadata\src\System\Reflection\Metadata\BlobWriterImpl.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 281
Coverable lines: 281
Total lines: 382
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 118
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetCompressedIntegerSize(...)0%440%
WriteCompressedInteger(...)0%660%
WriteCompressedInteger(...)0%660%
WriteCompressedSignedInteger(...)0%660%
WriteCompressedSignedInteger(...)0%660%
WriteScalarConstant(...)0%30300%
WriteConstant(...)0%30300%
WriteConstant(...)0%30300%

File(s)

D:\runner\runtime\src\libraries\System.Reflection.Metadata\src\System\Reflection\Metadata\BlobWriterImpl.cs

#LineLine coverage
 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
 4using System.Buffers.Binary;
 5using System.Diagnostics;
 6
 7namespace 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)
 019        {
 020            Debug.Assert(value <= MaxCompressedIntegerValue);
 21
 022            if (value <= SingleByteCompressedIntegerMaxValue)
 023            {
 024                return 1;
 25            }
 26
 027            if (value <= TwoByteCompressedIntegerMaxValue)
 028            {
 029                return 2;
 30            }
 31
 032            return 4;
 033        }
 34
 35        internal static void WriteCompressedInteger(ref BlobWriter writer, uint value)
 036        {
 37            unchecked
 038            {
 039                if (value <= SingleByteCompressedIntegerMaxValue)
 040                {
 041                    writer.WriteByte((byte)value);
 042                }
 043                else if (value <= TwoByteCompressedIntegerMaxValue)
 044                {
 045                    writer.WriteUInt16BE((ushort)(0x8000 | value));
 046                }
 047                else if (value <= MaxCompressedIntegerValue)
 048                {
 049                    writer.WriteUInt32BE(0xc0000000 | value);
 050                }
 51                else
 052                {
 053                    Throw.ValueArgumentOutOfRange();
 54                }
 055            }
 056        }
 57
 58        internal static void WriteCompressedInteger(BlobBuilder writer, uint value)
 059        {
 60            unchecked
 061            {
 062                if (value <= SingleByteCompressedIntegerMaxValue)
 063                {
 064                    writer.WriteByte((byte)value);
 065                }
 066                else if (value <= TwoByteCompressedIntegerMaxValue)
 067                {
 068                    writer.WriteUInt16BE((ushort)(0x8000 | value));
 069                }
 070                else if (value <= MaxCompressedIntegerValue)
 071                {
 072                    writer.WriteUInt32BE(0xc0000000 | value);
 073                }
 74                else
 075                {
 076                    Throw.ValueArgumentOutOfRange();
 77                }
 078            }
 079        }
 80
 81        internal static void WriteCompressedSignedInteger(ref BlobWriter writer, int value)
 082        {
 83            unchecked
 084            {
 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
 091                int signMask = value >> 31;
 92
 093                if ((value & ~b6) == (signMask & ~b6))
 094                {
 095                    int n = ((value & b6) << 1) | (signMask & 1);
 096                    writer.WriteByte((byte)n);
 097                }
 098                else if ((value & ~b13) == (signMask & ~b13))
 099                {
 0100                    int n = ((value & b13) << 1) | (signMask & 1);
 0101                    writer.WriteUInt16BE((ushort)(0x8000 | n));
 0102                }
 0103                else if ((value & ~b28) == (signMask & ~b28))
 0104                {
 0105                    int n = ((value & b28) << 1) | (signMask & 1);
 0106                    writer.WriteUInt32BE(0xc0000000 | (uint)n);
 0107                }
 108                else
 0109                {
 0110                    Throw.ValueArgumentOutOfRange();
 111                }
 0112            }
 0113        }
 114
 115        internal static void WriteCompressedSignedInteger(BlobBuilder writer, int value)
 0116        {
 117            unchecked
 0118            {
 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
 0125                int signMask = value >> 31;
 126
 0127                if ((value & ~b6) == (signMask & ~b6))
 0128                {
 0129                    int n = ((value & b6) << 1) | (signMask & 1);
 0130                    writer.WriteByte((byte)n);
 0131                }
 0132                else if ((value & ~b13) == (signMask & ~b13))
 0133                {
 0134                    int n = ((value & b13) << 1) | (signMask & 1);
 0135                    writer.WriteUInt16BE((ushort)(0x8000 | n));
 0136                }
 0137                else if ((value & ~b28) == (signMask & ~b28))
 0138                {
 0139                    int n = ((value & b28) << 1) | (signMask & 1);
 0140                    writer.WriteUInt32BE(0xc0000000 | (uint)n);
 0141                }
 142                else
 0143                {
 0144                    Throw.ValueArgumentOutOfRange();
 145                }
 0146            }
 0147        }
 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)
 0156        {
 0157            if (value == null)
 0158            {
 159                // The encoding of Type for the nullref value for FieldInit is ELEMENT_TYPE_CLASS with a Value of a 4-by
 0160                BinaryPrimitives.WriteUInt32LittleEndian(bytes, 0);
 0161                return sizeof(uint);
 162            }
 163
 0164            var type = value.GetType();
 0165            if (type.IsEnum)
 0166            {
 0167                type = Enum.GetUnderlyingType(type);
 0168            }
 169
 0170            if (type == typeof(bool))
 0171            {
 0172                bytes[0] = (byte)((bool)value ? 1 : 0);
 0173                return sizeof(bool);
 174            }
 0175            else if (type == typeof(int))
 0176            {
 0177                BinaryPrimitives.WriteInt32LittleEndian(bytes, (int)value);
 0178                return sizeof(int);
 179            }
 0180            else if (type == typeof(byte))
 0181            {
 0182                bytes[0] = (byte)value;
 0183                return sizeof(byte);
 184            }
 0185            else if (type == typeof(char))
 0186            {
 0187                BinaryPrimitives.WriteUInt16LittleEndian(bytes, (char)value);
 0188                return sizeof(char);
 189            }
 0190            else if (type == typeof(double))
 0191            {
 0192                BinaryPrimitives.WriteDoubleLittleEndian(bytes, (double)value);
 0193                return sizeof(double);
 194            }
 0195            else if (type == typeof(short))
 0196            {
 0197                BinaryPrimitives.WriteInt16LittleEndian(bytes, (short)value);
 0198                return sizeof(short);
 199            }
 0200            else if (type == typeof(long))
 0201            {
 0202                BinaryPrimitives.WriteInt64LittleEndian(bytes, (long)value);
 0203                return sizeof(long);
 204            }
 0205            else if (type == typeof(sbyte))
 0206            {
 0207                bytes[0] = (byte)(sbyte)value;
 0208                return sizeof(sbyte);
 209            }
 0210            else if (type == typeof(float))
 0211            {
 0212                BinaryPrimitives.WriteSingleLittleEndian(bytes, (float)value);
 0213                return sizeof(float);
 214            }
 0215            else if (type == typeof(ushort))
 0216            {
 0217                BinaryPrimitives.WriteUInt16LittleEndian(bytes, (ushort)value);
 0218                return sizeof(ushort);
 219            }
 0220            else if (type == typeof(uint))
 0221            {
 0222                BinaryPrimitives.WriteUInt32LittleEndian(bytes, (uint)value);
 0223                return sizeof(uint);
 224            }
 0225            else if (type == typeof(ulong))
 0226            {
 0227                BinaryPrimitives.WriteUInt64LittleEndian(bytes, (ulong)value);
 0228                return sizeof(ulong);
 229            }
 230            else
 0231            {
 0232                throw new ArgumentException(SR.Format(SR.InvalidConstantValueOfType, type));
 233            }
 0234        }
 235
 236        internal static void WriteConstant(ref BlobWriter writer, object? value)
 0237        {
 0238            if (value == null)
 0239            {
 240                // The encoding of Type for the nullref value for FieldInit is ELEMENT_TYPE_CLASS with a Value of a 4-by
 0241                writer.WriteUInt32(0);
 0242                return;
 243            }
 244
 0245            var type = value.GetType();
 0246            if (type.IsEnum)
 0247            {
 0248                type = Enum.GetUnderlyingType(type);
 0249            }
 250
 0251            if (type == typeof(bool))
 0252            {
 0253                writer.WriteBoolean((bool)value);
 0254            }
 0255            else if (type == typeof(int))
 0256            {
 0257                writer.WriteInt32((int)value);
 0258            }
 0259            else if (type == typeof(string))
 0260            {
 0261                writer.WriteUTF16((string)value);
 0262            }
 0263            else if (type == typeof(byte))
 0264            {
 0265                writer.WriteByte((byte)value);
 0266            }
 0267            else if (type == typeof(char))
 0268            {
 0269                writer.WriteUInt16((char)value);
 0270            }
 0271            else if (type == typeof(double))
 0272            {
 0273                writer.WriteDouble((double)value);
 0274            }
 0275            else if (type == typeof(short))
 0276            {
 0277                writer.WriteInt16((short)value);
 0278            }
 0279            else if (type == typeof(long))
 0280            {
 0281                writer.WriteInt64((long)value);
 0282            }
 0283            else if (type == typeof(sbyte))
 0284            {
 0285                writer.WriteSByte((sbyte)value);
 0286            }
 0287            else if (type == typeof(float))
 0288            {
 0289                writer.WriteSingle((float)value);
 0290            }
 0291            else if (type == typeof(ushort))
 0292            {
 0293                writer.WriteUInt16((ushort)value);
 0294            }
 0295            else if (type == typeof(uint))
 0296            {
 0297                writer.WriteUInt32((uint)value);
 0298            }
 0299            else if (type == typeof(ulong))
 0300            {
 0301                writer.WriteUInt64((ulong)value);
 0302            }
 303            else
 0304            {
 0305                throw new ArgumentException(SR.Format(SR.InvalidConstantValueOfType, type));
 306            }
 0307        }
 308
 309        internal static void WriteConstant(BlobBuilder writer, object? value)
 0310        {
 0311            if (value == null)
 0312            {
 313                // The encoding of Type for the nullref value for FieldInit is ELEMENT_TYPE_CLASS with a Value of a 4-by
 0314                writer.WriteUInt32(0);
 0315                return;
 316            }
 317
 0318            var type = value.GetType();
 0319            if (type.IsEnum)
 0320            {
 0321                type = Enum.GetUnderlyingType(type);
 0322            }
 323
 0324            if (type == typeof(bool))
 0325            {
 0326                writer.WriteBoolean((bool)value);
 0327            }
 0328            else if (type == typeof(int))
 0329            {
 0330                writer.WriteInt32((int)value);
 0331            }
 0332            else if (type == typeof(string))
 0333            {
 0334                writer.WriteUTF16((string)value);
 0335            }
 0336            else if (type == typeof(byte))
 0337            {
 0338                writer.WriteByte((byte)value);
 0339            }
 0340            else if (type == typeof(char))
 0341            {
 0342                writer.WriteUInt16((char)value);
 0343            }
 0344            else if (type == typeof(double))
 0345            {
 0346                writer.WriteDouble((double)value);
 0347            }
 0348            else if (type == typeof(short))
 0349            {
 0350                writer.WriteInt16((short)value);
 0351            }
 0352            else if (type == typeof(long))
 0353            {
 0354                writer.WriteInt64((long)value);
 0355            }
 0356            else if (type == typeof(sbyte))
 0357            {
 0358                writer.WriteSByte((sbyte)value);
 0359            }
 0360            else if (type == typeof(float))
 0361            {
 0362                writer.WriteSingle((float)value);
 0363            }
 0364            else if (type == typeof(ushort))
 0365            {
 0366                writer.WriteUInt16((ushort)value);
 0367            }
 0368            else if (type == typeof(uint))
 0369            {
 0370                writer.WriteUInt32((uint)value);
 0371            }
 0372            else if (type == typeof(ulong))
 0373            {
 0374                writer.WriteUInt64((ulong)value);
 0375            }
 376            else
 0377            {
 0378                throw new ArgumentException(SR.Format(SR.InvalidConstantValueOfType, type));
 379            }
 0380        }
 381    }
 382}