Ejemplo n.º 1
0
 public void AddFieldInitData(byte [] data)
 {
     m_fieldDataWriter.Write(data);
     m_fieldDataWriter.QuadAlign();
 }
Ejemplo n.º 2
0
        public override void VisitMetadataRoot(MetadataRoot root)
        {
            WriteMemStream(m_cilWriter);
            WriteMemStream(m_fieldDataWriter);
            m_resStart = (uint)m_binaryWriter.BaseStream.Position;
            WriteMemStream(m_resWriter);
            m_resSize = (uint)(m_binaryWriter.BaseStream.Position - m_resStart);

            // for now, we only reserve the place for the strong name signature
            if ((m_assembly.Name.Flags & AssemblyFlags.PublicKey) > 0)
            {
                m_snsStart = (uint)m_binaryWriter.BaseStream.Position;
                m_snsSize  = GetStrongNameSignatureSize();
                m_binaryWriter.Write(new byte [m_snsSize]);
                m_binaryWriter.QuadAlign();
            }

            // save place for debug header
            if (m_imgWriter.GetImage().DebugHeader != null)
            {
                m_debugHeaderStart = (uint)m_binaryWriter.BaseStream.Position;
                m_binaryWriter.Write(new byte [m_imgWriter.GetImage().DebugHeader.GetSize()]);
                m_binaryWriter.QuadAlign();
            }

            m_mdStart = (uint)m_binaryWriter.BaseStream.Position;

            if (m_stringWriter.BaseStream.Length > 1)
            {
                CreateStream(MetadataStream.Strings);
                SetHeapSize(root.Streams.StringsHeap, m_stringWriter, 0x01);
                m_stringWriter.QuadAlign();
            }

            if (m_guidWriter.BaseStream.Length > 0)
            {
                CreateStream(MetadataStream.GUID);
                SetHeapSize(root.Streams.GuidHeap, m_guidWriter, 0x02);
            }

            if (m_blobWriter.BaseStream.Length > 1)
            {
                CreateStream(MetadataStream.Blob);
                SetHeapSize(root.Streams.BlobHeap, m_blobWriter, 0x04);
                m_blobWriter.QuadAlign();
            }

            if (m_usWriter.BaseStream.Length > 2)
            {
                CreateStream(MetadataStream.UserStrings);
                m_usWriter.QuadAlign();
            }

            m_root.Header.MajorVersion = 1;
            m_root.Header.MinorVersion = 1;

            switch (m_runtime)
            {
            case TargetRuntime.NET_1_0:
                m_root.Header.Version = "v1.0.3705";
                break;

            case TargetRuntime.NET_1_1:
                m_root.Header.Version = "v1.1.4322";
                break;

            case TargetRuntime.NET_2_0:
                m_root.Header.Version = "v2.0.50727";
                break;
            }

            m_root.Streams.TablesHeap.Tables.Accept(m_tableWriter);

            if (m_tWriter.BaseStream.Length == 0)
            {
                m_root.Streams.Remove(m_root.Streams.TablesHeap.GetStream());
            }
        }
 void WriteToken(MetadataToken token)
 {
     if (token.RID == 0)
     {
         m_codeWriter.Write(0);
     }
     else
     {
         m_codeWriter.Write(token.ToUInt());
     }
 }
Ejemplo n.º 4
0
 public override void VisitTableCollection(TableCollection coll)
 {
     if (m_heap.HasTable(ModuleTable.RId))
     {
         m_binaryWriter.Write(m_heap [ModuleTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(TypeRefTable.RId))
     {
         m_binaryWriter.Write(m_heap [TypeRefTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(TypeDefTable.RId))
     {
         m_binaryWriter.Write(m_heap [TypeDefTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(FieldTable.RId))
     {
         m_binaryWriter.Write(m_heap [FieldTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(MethodTable.RId))
     {
         m_binaryWriter.Write(m_heap [MethodTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(ParamTable.RId))
     {
         m_binaryWriter.Write(m_heap [ParamTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(InterfaceImplTable.RId))
     {
         m_binaryWriter.Write(m_heap [InterfaceImplTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(MemberRefTable.RId))
     {
         m_binaryWriter.Write(m_heap [MemberRefTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(ConstantTable.RId))
     {
         m_binaryWriter.Write(m_heap [ConstantTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(CustomAttributeTable.RId))
     {
         m_binaryWriter.Write(m_heap [CustomAttributeTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(FieldMarshalTable.RId))
     {
         m_binaryWriter.Write(m_heap [FieldMarshalTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(DeclSecurityTable.RId))
     {
         m_binaryWriter.Write(m_heap [DeclSecurityTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(ClassLayoutTable.RId))
     {
         m_binaryWriter.Write(m_heap [ClassLayoutTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(FieldLayoutTable.RId))
     {
         m_binaryWriter.Write(m_heap [FieldLayoutTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(StandAloneSigTable.RId))
     {
         m_binaryWriter.Write(m_heap [StandAloneSigTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(EventMapTable.RId))
     {
         m_binaryWriter.Write(m_heap [EventMapTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(EventTable.RId))
     {
         m_binaryWriter.Write(m_heap [EventTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(PropertyMapTable.RId))
     {
         m_binaryWriter.Write(m_heap [PropertyMapTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(PropertyTable.RId))
     {
         m_binaryWriter.Write(m_heap [PropertyTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(MethodSemanticsTable.RId))
     {
         m_binaryWriter.Write(m_heap [MethodSemanticsTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(MethodImplTable.RId))
     {
         m_binaryWriter.Write(m_heap [MethodImplTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(ModuleRefTable.RId))
     {
         m_binaryWriter.Write(m_heap [ModuleRefTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(TypeSpecTable.RId))
     {
         m_binaryWriter.Write(m_heap [TypeSpecTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(ImplMapTable.RId))
     {
         m_binaryWriter.Write(m_heap [ImplMapTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(FieldRVATable.RId))
     {
         m_binaryWriter.Write(m_heap [FieldRVATable.RId].Rows.Count);
     }
     if (m_heap.HasTable(AssemblyTable.RId))
     {
         m_binaryWriter.Write(m_heap [AssemblyTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(AssemblyProcessorTable.RId))
     {
         m_binaryWriter.Write(m_heap [AssemblyProcessorTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(AssemblyOSTable.RId))
     {
         m_binaryWriter.Write(m_heap [AssemblyOSTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(AssemblyRefTable.RId))
     {
         m_binaryWriter.Write(m_heap [AssemblyRefTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(AssemblyRefProcessorTable.RId))
     {
         m_binaryWriter.Write(m_heap [AssemblyRefProcessorTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(AssemblyRefOSTable.RId))
     {
         m_binaryWriter.Write(m_heap [AssemblyRefOSTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(FileTable.RId))
     {
         m_binaryWriter.Write(m_heap [FileTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(ExportedTypeTable.RId))
     {
         m_binaryWriter.Write(m_heap [ExportedTypeTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(ManifestResourceTable.RId))
     {
         m_binaryWriter.Write(m_heap [ManifestResourceTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(NestedClassTable.RId))
     {
         m_binaryWriter.Write(m_heap [NestedClassTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(GenericParamTable.RId))
     {
         m_binaryWriter.Write(m_heap [GenericParamTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(MethodSpecTable.RId))
     {
         m_binaryWriter.Write(m_heap [MethodSpecTable.RId].Rows.Count);
     }
     if (m_heap.HasTable(GenericParamConstraintTable.RId))
     {
         m_binaryWriter.Write(m_heap [GenericParamConstraintTable.RId].Rows.Count);
     }
 }
Ejemplo n.º 5
0
 void WriteByIndexSize(uint value, int size)
 {
     if (size == 4)
     {
         m_binaryWriter.Write(value);
     }
     else if (size == 2)
     {
         m_binaryWriter.Write((ushort)value);
     }
     else
     {
         throw new MetadataFormatException("Non valid size for indexing");
     }
 }
        void Write(CustomAttrib.Elem elem, MemoryBinaryWriter writer)          // TODO
        {
            if (elem.String)
            {
                elem.FieldOrPropType = ElementType.String;
            }
            else if (elem.Type)
            {
                elem.FieldOrPropType = ElementType.Type;
            }
            else if (elem.BoxedValueType)
            {
                Write(elem.FieldOrPropType);
            }

            switch (elem.FieldOrPropType)
            {
            case ElementType.Boolean:
                writer.Write((byte)((bool)elem.Value ? 1 : 0));
                break;

            case ElementType.Char:
                writer.Write((ushort)(char)elem.Value);
                break;

            case ElementType.R4:
                writer.Write((float)elem.Value);
                break;

            case ElementType.R8:
                writer.Write((double)elem.Value);
                break;

            case ElementType.I1:
                writer.Write((sbyte)elem.Value);
                break;

            case ElementType.I2:
                writer.Write((short)elem.Value);
                break;

            case ElementType.I4:
                writer.Write((int)elem.Value);
                break;

            case ElementType.I8:
                writer.Write((long)elem.Value);
                break;

            case ElementType.U1:
                writer.Write((byte)elem.Value);
                break;

            case ElementType.U2:
                writer.Write((ushort)elem.Value);
                break;

            case ElementType.U4:
                writer.Write((uint)elem.Value);
                break;

            case ElementType.U8:
                writer.Write((long)elem.Value);
                break;

            case ElementType.String:
                string s = elem.Value as string;
                if (s == null)
                {
                    writer.Write((byte)0xff);
                }
                else if (s.Length == 0)
                {
                    writer.Write((byte)0x00);
                }
                else
                {
                    Write(s);
                }
                break;

            case ElementType.Type:
                string t = elem.Value as string;
                if (t == null || t.Length == 0)
                {
                    throw new NotSupportedException("Null types not allowed in custom attributes");
                }
                Write(t);
                break;

            case ElementType.Object:
                if (elem.Value != null)
                {
                    throw new NotSupportedException("Unknown state");
                }
                writer.Write((byte)0xff);
                break;

            default:
                throw new NotImplementedException("WriteElem " + elem.FieldOrPropType.ToString());
            }
        }
Ejemplo n.º 7
0
        void Write(CustomAttrib.Elem elem, MemoryBinaryWriter writer)
        {
            if (elem.String)
            {
                elem.FieldOrPropType = ElementType.String;
            }
            else if (elem.Type)
            {
                elem.FieldOrPropType = ElementType.Type;
            }

            if (elem.FieldOrPropType == ElementType.Class)             // an enum in fact
            {
                elem.FieldOrPropType = GetElementTypeFromTypeCode(Type.GetTypeCode(elem.Value.GetType()));
            }

            if (elem.BoxedValueType)
            {
                Write(elem.FieldOrPropType);
            }

            switch (elem.FieldOrPropType)
            {
            case ElementType.Boolean:
                writer.Write((byte)((bool)elem.Value ? 1 : 0));
                break;

            case ElementType.Char:
                writer.Write((ushort)(char)elem.Value);
                break;

            case ElementType.R4:
                writer.Write((float)elem.Value);
                break;

            case ElementType.R8:
                writer.Write((double)elem.Value);
                break;

            case ElementType.I1:
                writer.Write((sbyte)elem.Value);
                break;

            case ElementType.I2:
                writer.Write((short)elem.Value);
                break;

            case ElementType.I4:
                writer.Write((int)elem.Value);
                break;

            case ElementType.I8:
                writer.Write((long)elem.Value);
                break;

            case ElementType.U1:
                writer.Write((byte)elem.Value);
                break;

            case ElementType.U2:
                writer.Write((ushort)elem.Value);
                break;

            case ElementType.U4:
                writer.Write((uint)elem.Value);
                break;

            case ElementType.U8:
                writer.Write((ulong)elem.Value);
                break;

            case ElementType.String:
            case ElementType.Type:
                string s = elem.Value as string;
                if (s == null)
                {
                    writer.Write((byte)0xff);
                }
                else if (s.Length == 0)
                {
                    writer.Write((byte)0x00);
                }
                else
                {
                    Write(s);
                }
                break;

            case ElementType.Object:
                if (elem.Value != null)
                {
                    throw new NotSupportedException("Unknown state");
                }
                writer.Write((byte)0xff);
                break;

            default:
                throw new NotImplementedException("WriteElem " + elem.FieldOrPropType.ToString());
            }
        }