public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            writer.WriteByte((byte)NativeType);
            writer.WriteByte((byte)ElementType);

            base.Write(buffer, writer);
        }
Exemple #2
0
        public void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            if (IsFat)
            {
                writer.WriteUInt32((uint)HandlerType);
                writer.WriteUInt32((uint)TryStart.Offset);
                writer.WriteUInt32((uint)(TryEnd.Offset - TryStart.Offset));
                writer.WriteUInt32((uint)HandlerStart.Offset);
                writer.WriteUInt32((uint)(HandlerEnd.Offset - HandlerStart.Offset));
            }
            else
            {
                writer.WriteUInt16((ushort)HandlerType);
                writer.WriteUInt16((ushort)TryStart.Offset);
                writer.WriteByte((byte)(TryEnd.Offset - TryStart.Offset));
                writer.WriteUInt16((ushort)HandlerStart.Offset);
                writer.WriteByte((byte)(HandlerEnd.Offset - HandlerStart.Offset));
            }

            switch (HandlerType)
            {
            case ExceptionHandlerType.Exception:
                writer.WriteUInt32(buffer.TableStreamBuffer.GetTypeToken(CatchType).ToUInt32());
                break;

            case ExceptionHandlerType.Filter:
                writer.WriteUInt32((uint)FilterStart.Offset);
                break;

            default:
                writer.WriteUInt32(0);
                break;
            }
        }
Exemple #3
0
 /// <inheritdoc />
 public override void Write(IBinaryStreamWriter writer)
 {
     writer.WriteByte((byte)Type);
     writer.WriteByte(Version);
     writer.WriteUInt16(0);
     writer.WriteUInt32((uint)SignatureAlgorithm);
 }
Exemple #4
0
        private void WriteExceptionHandler(MethodBodySerializationContext context, IBinaryStreamWriter writer, CilExceptionHandler handler, bool useFatFormat)
        {
            if (handler.IsFat && !useFatFormat)
            {
                throw new InvalidOperationException("Can only serialize fat exception handlers in fat format.");
            }

            // Write handler type and boundaries.
            if (useFatFormat)
            {
                writer.WriteUInt32((uint)handler.HandlerType);
                writer.WriteUInt32((uint)handler.TryStart.Offset);
                writer.WriteUInt32((uint)(handler.TryEnd.Offset - handler.TryStart.Offset));
                writer.WriteUInt32((uint)handler.HandlerStart.Offset);
                writer.WriteUInt32((uint)(handler.HandlerEnd.Offset - handler.HandlerStart.Offset));
            }
            else
            {
                writer.WriteUInt16((ushort)handler.HandlerType);
                writer.WriteUInt16((ushort)handler.TryStart.Offset);
                writer.WriteByte((byte)(handler.TryEnd.Offset - handler.TryStart.Offset));
                writer.WriteUInt16((ushort)handler.HandlerStart.Offset);
                writer.WriteByte((byte)(handler.HandlerEnd.Offset - handler.HandlerStart.Offset));
            }

            // Write handler type or filter start.
            switch (handler.HandlerType)
            {
            case CilExceptionHandlerType.Exception:
            {
                var provider = context.TokenProvider;

                var token = handler.ExceptionType switch
                {
                    TypeReference typeReference => provider.GetTypeReferenceToken(typeReference),
                    TypeDefinition typeDefinition => provider.GetTypeDefinitionToken(typeDefinition),
                    TypeSpecification typeSpecification => provider.GetTypeSpecificationToken(typeSpecification),
                    _ => context.DiagnosticBag.RegisterExceptionAndReturnDefault <MetadataToken>(
                        new ArgumentOutOfRangeException(
                            $"Invalid or unsupported exception type ({handler.ExceptionType.SafeToString()})"))
                };
                writer.WriteUInt32(token.ToUInt32());
                break;
            }

            case CilExceptionHandlerType.Filter:
                writer.WriteUInt32((uint)handler.FilterStart.Offset);
                break;

            case CilExceptionHandlerType.Finally:
            case CilExceptionHandlerType.Fault:
                writer.WriteUInt32(0);
                break;

            default:
                context.DiagnosticBag.RegisterException(new ArgumentOutOfRangeException(
                                                            $"Invalid or unsupported handler type ({handler.HandlerType.SafeToString()}"));
                break;
            }
        }
Exemple #5
0
 /// <summary>
 /// Writes the operation code to the output stream.
 /// </summary>
 /// <param name="opCode">The opcode to write.</param>
 private void WriteOpCode(CilOpCode opCode)
 {
     if (opCode.Size == 2)
     {
         _writer.WriteByte(opCode.Op1);
     }
     _writer.WriteByte(opCode.Op2);
 }
Exemple #6
0
        /// <inheritdoc />
        public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            writer.WriteByte((byte)NativeType);
            writer.WriteCompressedUInt32((uint)NumberOfElements);
            writer.WriteByte((byte)ElementType);

            base.Write(buffer, writer);
        }
Exemple #7
0
 private void WriteOpCode(CilOpCode opCode)
 {
     _writer.WriteByte(opCode.Byte1);
     if (opCode.IsLarge)
     {
         _writer.WriteByte(opCode.Byte2);
     }
 }
 /// <inheritdoc />
 protected override void WriteContents(IBinaryStreamWriter writer, ITypeCodedIndexProvider provider)
 {
     writer.WriteByte((byte)ElementType);
     writer.WriteByte((byte)(IsValueType ? ElementType.ValueType : ElementType.Class));
     TypeSignature.WriteTypeDefOrRef(writer, provider, GenericType);
     writer.WriteCompressedUInt32((uint)TypeArguments.Count);
     for (int i = 0; i < TypeArguments.Count; i++)
     {
         TypeArguments[i].Write(writer, provider);
     }
 }
Exemple #9
0
 /// <inheritdoc />
 public override void Write(IBinaryStreamWriter writer)
 {
     writer.WriteByte(Width);
     writer.WriteByte((Height));
     writer.WriteByte(ColorCount);
     writer.WriteByte(Reserved);
     writer.WriteUInt16(ColorPlanes);
     writer.WriteUInt16(PixelBitCount);
     writer.WriteUInt32(BytesInRes);
     writer.WriteUInt16(Id);
 }
        private void WriteExceptionHandler(IMetadataTokenProvider provider, IBinaryStreamWriter writer, CilExceptionHandler handler, bool useFatFormat)
        {
            if (handler.IsFat && !useFatFormat)
            {
                throw new InvalidOperationException("Can only serialize fat exception handlers in fat format.");
            }

            // Write handler type and boundaries.
            if (useFatFormat)
            {
                writer.WriteUInt32((uint)handler.HandlerType);
                writer.WriteUInt32((uint)handler.TryStart.Offset);
                writer.WriteUInt32((uint)(handler.TryEnd.Offset - handler.TryStart.Offset));
                writer.WriteUInt32((uint)handler.HandlerStart.Offset);
                writer.WriteUInt32((uint)(handler.HandlerEnd.Offset - handler.HandlerStart.Offset));
            }
            else
            {
                writer.WriteUInt16((ushort)handler.HandlerType);
                writer.WriteUInt16((ushort)handler.TryStart.Offset);
                writer.WriteByte((byte)(handler.TryEnd.Offset - handler.TryStart.Offset));
                writer.WriteUInt16((ushort)handler.HandlerStart.Offset);
                writer.WriteByte((byte)(handler.HandlerEnd.Offset - handler.HandlerStart.Offset));
            }

            // Write handler type or filter start.
            switch (handler.HandlerType)
            {
            case CilExceptionHandlerType.Exception:
            {
                var token = handler.ExceptionType switch
                {
                    TypeReference typeReference => provider.GetTypeReferenceToken(typeReference),
                    TypeDefinition typeDefinition => provider.GetTypeDefinitionToken(typeDefinition),
                    TypeSpecification typeSpecification => provider.GetTypeSpecificationToken(typeSpecification),
                    _ => throw new ArgumentOutOfRangeException()
                };
                writer.WriteUInt32(token.ToUInt32());
                break;
            }

            case CilExceptionHandlerType.Filter:
                writer.WriteUInt32((uint)handler.FilterStart.Offset);
                break;

            case CilExceptionHandlerType.Finally:
            case CilExceptionHandlerType.Fault:
                writer.WriteUInt32(0);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #11
0
        internal static void WriteFieldOrPropType(IBinaryStreamWriter writer, TypeSignature type)
        {
            switch (type.ElementType)
            {
            case ElementType.Boolean:
            case ElementType.Char:
            case ElementType.I1:
            case ElementType.U1:
            case ElementType.I2:
            case ElementType.U2:
            case ElementType.I4:
            case ElementType.U4:
            case ElementType.I8:
            case ElementType.U8:
            case ElementType.R4:
            case ElementType.R8:
            case ElementType.I:
            case ElementType.U:
            case ElementType.String:
                writer.WriteByte((byte)type.ElementType);
                break;

            case ElementType.Object:
                writer.WriteByte((byte)ElementType.Boxed);

                break;

            case ElementType.SzArray:
                writer.WriteByte((byte)ElementType.SzArray);

                var arrayType = (SzArrayTypeSignature)type;
                WriteFieldOrPropType(writer, arrayType.BaseType);
                break;

            default:
                if (type.IsTypeOf("System", "Type"))
                {
                    writer.WriteByte((byte)ElementType.Type);
                    return;
                }

                var typeDef = type.Resolve();
                if (typeDef != null && typeDef.IsEnum)
                {
                    writer.WriteByte((byte)ElementType.Enum);
                    writer.WriteSerString(TypeNameBuilder.GetAssemblyQualifiedName(type));
                    return;
                }

                throw new ArgumentOutOfRangeException();
            }
        }
Exemple #12
0
        public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            writer.WriteByte((byte)ElementType);
            writer.WriteByte((byte)(IsValueType ? ElementType.ValueType : ElementType.Class));

            WriteTypeDefOrRef(buffer, writer, GenericType);

            writer.WriteCompressedUInt32((uint)GenericArguments.Count);
            foreach (var argument in GenericArguments)
            {
                argument.Write(buffer, writer);
            }
        }
Exemple #13
0
 public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
 {
     writer.WriteByte((byte)NativeType);
     writer.WriteByte((byte)ElementType);
     if (ParameterIndex.HasValue)
     {
         writer.WriteCompressedUInt32((uint)ParameterIndex.Value);
         if (NumberOfElements.HasValue)
         {
             writer.WriteCompressedUInt32((uint)NumberOfElements.Value);
         }
     }
 }
Exemple #14
0
        public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            if (!Validate())
            {
                throw new InvalidOperationException();
            }

            writer.WriteByte((byte)ElementType);
            BaseType.Write(buffer, writer);
            writer.WriteCompressedUInt32((uint)Dimensions.Count);

            var sizedDimensions = Dimensions.Where(x => x.Size.HasValue).ToArray();

            writer.WriteCompressedUInt32((uint)sizedDimensions.Length);
            foreach (var sizedDimension in sizedDimensions)
            {
                writer.WriteCompressedUInt32((uint)sizedDimension.Size.Value);
            }

            var boundedDimensions = Dimensions.Where(x => x.LowerBound.HasValue).ToArray();

            writer.WriteCompressedUInt32((uint)boundedDimensions.Length);
            foreach (var boundedDimension in boundedDimensions)
            {
                writer.WriteCompressedUInt32((uint)boundedDimension.LowerBound.Value);
            }

            base.Write(buffer, writer);
        }
 private static void WriteHintName(IBinaryStreamWriter writer, ushort hint, string name)
 {
     writer.WriteUInt16(hint);
     writer.WriteAsciiString(name);
     writer.WriteByte(0);
     writer.Align(2);
 }
 /// <summary>
 /// Writes the named argument to the provided output stream.
 /// </summary>
 /// <param name="writer">The output stream.</param>
 /// <param name="provider">The object to use for obtaining metadata tokens for members in the tables stream.</param>
 public void Write(IBinaryStreamWriter writer, ITypeCodedIndexProvider provider)
 {
     writer.WriteByte((byte)MemberType);
     TypeSignature.WriteFieldOrPropType(writer, ArgumentType);
     writer.WriteSerString(MemberName);
     Argument.Write(writer, provider);
 }
 public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
 {
     writer.WriteByte((byte)ArgumentMemberType);
     ArgumentType.Write(buffer, writer); // TODO: write FieldOrPropType instead.
     writer.WriteSerString(MemberName);
     Argument.Write(buffer, writer);
 }
Exemple #18
0
        /// <inheritdoc />
        public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            writer.WriteByte(0x06);
            FieldType.Write(buffer, writer);

            base.Write(buffer, writer);
        }
Exemple #19
0
        /// <inheritdoc />
        public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            writer.WriteByte((byte)ElementType.FnPtr);
            Signature.Write(buffer, writer);

            base.Write(buffer, writer);
        }
        /// <inheritdoc />
        protected override void WriteContents(IBinaryStreamWriter writer, ITypeCodedIndexProvider provider)
        {
            if (!Validate())
            {
                throw new InvalidOperationException();
            }

            writer.WriteByte((byte)ElementType);
            BaseType.Write(writer, provider);
            writer.WriteCompressedUInt32((uint)Dimensions.Count);

            // Sized dimensions.
            var sizedDimensions = Dimensions
                                  .Where(x => x.Size.HasValue)
                                  .ToArray();

            writer.WriteCompressedUInt32((uint)sizedDimensions.Length);
            foreach (var sizedDimension in sizedDimensions)
            {
                writer.WriteCompressedUInt32((uint)sizedDimension.Size.Value);
            }

            // Bounded dimensions.
            var boundedDimensions = Dimensions
                                    .Where(x => x.LowerBound.HasValue)
                                    .ToArray();

            writer.WriteCompressedUInt32((uint)boundedDimensions.Length);
            foreach (var boundedDimension in boundedDimensions)
            {
                writer.WriteCompressedUInt32((uint)boundedDimension.LowerBound.Value);
            }
        }
        /// <summary>
        /// Writes a specified amount of zero bytes to the stream.
        /// </summary>
        /// <param name="writer">The writer to use.</param>
        /// <param name="count">The amount of zeroes to write.</param>
        public static void WriteZeroes(this IBinaryStreamWriter writer, int count)
        {
            while (count >= sizeof(ulong))
            {
                writer.WriteUInt64(0);
                count -= sizeof(ulong);
            }

            while (count >= sizeof(uint))
            {
                writer.WriteUInt32(0);
                count -= sizeof(uint);
            }

            while (count >= sizeof(ushort))
            {
                writer.WriteUInt16(0);
                count -= sizeof(ushort);
            }

            while (count >= sizeof(byte))
            {
                writer.WriteByte(0);
                count -= sizeof(byte);
            }
        }
Exemple #22
0
 public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
 {
     writer.WriteByte((byte)NativeType);
     writer.WriteSerString(Guid.ToString("B"));
     writer.WriteSerString(UnmanagedType);
     writer.WriteSerString(ManagedType);
     writer.WriteSerString(Cookie);
 }
Exemple #23
0
 /// <inheritdoc />
 public override void Write(IBinaryStreamWriter writer)
 {
     writer.WriteUInt32((uint)Signature);
     writer.WriteBytes(Guid.ToByteArray());
     writer.WriteUInt32(Age);
     writer.WriteBytes(Encoding.UTF8.GetBytes(Path));
     writer.WriteByte(0x00);
 }
        /// <inheritdoc />
        public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
        {
            writer.WriteByte((byte)ElementType);
            WriteTypeDefOrRef(buffer, writer, ModifierType);
            BaseType.Write(buffer, writer);

            base.Write(buffer, writer);
        }
Exemple #25
0
 /// <inheritdoc />
 public override void Write(IBinaryStreamWriter writer)
 {
     foreach (string value in _entries)
     {
         writer.WriteAsciiString(value);
         writer.WriteByte(0);
     }
 }
 /// <inheritdoc />
 protected override void WriteContents(IBinaryStreamWriter writer, ITypeCodedIndexProvider provider)
 {
     writer.WriteByte((byte)NativeType);
     writer.WriteSerString(Guid ?? string.Empty);
     writer.WriteSerString(NativeTypeName ?? string.Empty);
     writer.WriteSerString(MarshalType is null ? string.Empty : TypeNameBuilder.GetAssemblyQualifiedName(MarshalType));
     writer.WriteSerString(Cookie ?? string.Empty);
 }
 public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
 {
     writer.WriteByte(0x0A);
     writer.WriteCompressedUInt32((uint)GenericArguments.Count);
     foreach (var argument in GenericArguments)
     {
         argument.Write(buffer, writer);
     }
 }
 public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
 {
     writer.WriteByte((byte)'.');
     writer.WriteCompressedUInt32((uint)Attributes.Count);
     foreach (var attribute in Attributes)
     {
         attribute.Write(buffer, writer);
     }
 }
Exemple #29
0
 public override void Write(MetadataBuffer buffer, IBinaryStreamWriter writer)
 {
     writer.WriteByte(0x07);
     writer.WriteCompressedUInt32((uint)Variables.Count);
     foreach (var variable in Variables)
     {
         variable.Write(buffer, writer);
     }
 }
 /// <inheritdoc />
 protected override void WriteContents(IBinaryStreamWriter writer, ITypeCodedIndexProvider provider)
 {
     writer.WriteByte((byte)Attributes);
     writer.WriteCompressedUInt32((uint)TypeArguments.Count);
     for (int i = 0; i < TypeArguments.Count; i++)
     {
         TypeArguments[i].Write(writer, provider);
     }
 }