Exemple #1
0
        public void Write(WriterContext ctxt)
        {
            var writer = ctxt.GetTablesWriter();

            writer.WriteUInt32(reserved0);
            writer.WriteByte(majorVersion);
            writer.WriteByte(minorVersion);
            var heapSizes = default(byte);

            if (IsStringStreamBig)
            {
                heapSizes |= 0x01;
            }
            if (IsGuidStreamBig)
            {
                heapSizes |= 0x02;
            }
            if (IsBlobStreamBig)
            {
                heapSizes |= 0x04;
            }
            writer.WriteByte(heapSizes);
            writer.WriteByte(Reserved1);

            var valid = new IntSet64();

            valid[(int)TableTag.Assembly]             = AssemblyTable.Count > 0;
            valid[(int)TableTag.AssemblyOS]           = AssemblyOSTable.Count > 0;
            valid[(int)TableTag.AssemblyProcessor]    = AssemblyProcessorTable.Count > 0;
            valid[(int)TableTag.AssemblyRef]          = AssemblyRefTable.Count > 0;
            valid[(int)TableTag.AssemblyRefOS]        = AssemblyRefOSTable.Count > 0;
            valid[(int)TableTag.AssemblyRefProcessor] = AssemblyRefProcessorTable.Count > 0;
            valid[(int)TableTag.ClassLayout]          = ClassLayoutTable.Count > 0;
            valid[(int)TableTag.Constant]             = ConstantTable.Count > 0;
            valid[(int)TableTag.CustomAttribute]      = CustomAttributeTable.Count > 0;
            valid[(int)TableTag.DeclSecurity]         = DeclSecurityTable.Count > 0;
            valid[(int)TableTag.EventMap]             = EventMapTable.Count > 0;
            valid[(int)TableTag.Event]                  = EventTable.Count > 0;
            valid[(int)TableTag.ExportedType]           = ExportedTypeTable.Count > 0;
            valid[(int)TableTag.Field]                  = FieldTable.Count > 0;
            valid[(int)TableTag.FieldLayout]            = FieldLayoutTable.Count > 0;
            valid[(int)TableTag.FieldMarshal]           = FieldMarshalTable.Count > 0;
            valid[(int)TableTag.FieldRVA]               = FieldRVATable.Count > 0;
            valid[(int)TableTag.File]                   = FileTable.Count > 0;
            valid[(int)TableTag.GenericParam]           = GenericParamTable.Count > 0;
            valid[(int)TableTag.GenericParamConstraint] = GenericParamConstraintTable.Count > 0;
            valid[(int)TableTag.ImplMap]                = ImplMapTable.Count > 0;
            valid[(int)TableTag.InterfaceImpl]          = InterfaceImplTable.Count > 0;
            valid[(int)TableTag.ManifestResource]       = ManifestResourceTable.Count > 0;
            valid[(int)TableTag.MemberRef]              = MemberRefTable.Count > 0;
            valid[(int)TableTag.MethodDef]              = MethodDefTable.Count > 0;
            valid[(int)TableTag.MethodImpl]             = MethodImplTable.Count > 0;
            valid[(int)TableTag.MethodSemantics]        = MethodSemanticsTable.Count > 0;
            valid[(int)TableTag.MethodSpec]             = MethodSpecTable.Count > 0;
            valid[(int)TableTag.Module]                 = ModuleTable.Count > 0;
            valid[(int)TableTag.ModuleRef]              = ModuleRefTable.Count > 0;
            valid[(int)TableTag.NestedClass]            = NestedClassTable.Count > 0;
            valid[(int)TableTag.Param]                  = ParamTable.Count > 0;
            valid[(int)TableTag.Property]               = PropertyTable.Count > 0;
            valid[(int)TableTag.PropertyMap]            = PropertyMapTable.Count > 0;
            valid[(int)TableTag.StandAloneSig]          = StandAloneSigTable.Count > 0;
            valid[(int)TableTag.TypeDef]                = TypeDefTable.Count > 0;
            valid[(int)TableTag.TypeRef]                = TypeRefTable.Count > 0;
            valid[(int)TableTag.TypeSpec]               = TypeSpecTable.Count > 0;
            writer.WriteUInt64(valid.ToUInt64());
            writer.WriteUInt64(valid.ToUInt64());

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    var numRows = default(int);
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        numRows = ModuleTable.NumRows;
                        break;

                    case TableTag.Assembly:
                        numRows = AssemblyTable.NumRows;
                        break;

                    case TableTag.AssemblyOS:
                        numRows = AssemblyOSTable.NumRows;
                        break;

                    case TableTag.AssemblyProcessor:
                        numRows = AssemblyProcessorTable.NumRows;
                        break;

                    case TableTag.AssemblyRef:
                        numRows = AssemblyRefTable.NumRows;
                        break;

                    case TableTag.AssemblyRefOS:
                        numRows = AssemblyRefOSTable.NumRows;
                        break;

                    case TableTag.AssemblyRefProcessor:
                        numRows = AssemblyRefProcessorTable.NumRows;
                        break;

                    case TableTag.ClassLayout:
                        numRows = ClassLayoutTable.NumRows;
                        break;

                    case TableTag.Constant:
                        numRows = ConstantTable.NumRows;
                        break;

                    case TableTag.CustomAttribute:
                        numRows = CustomAttributeTable.NumRows;
                        break;

                    case TableTag.DeclSecurity:
                        numRows = DeclSecurityTable.NumRows;
                        break;

                    case TableTag.EventMap:
                        numRows = EventMapTable.NumRows;
                        break;

                    case TableTag.Event:
                        numRows = EventTable.NumRows;
                        break;

                    case TableTag.ExportedType:
                        numRows = ExportedTypeTable.NumRows;
                        break;

                    case TableTag.Field:
                        numRows = FieldTable.NumRows;
                        break;

                    case TableTag.FieldLayout:
                        numRows = FieldLayoutTable.NumRows;
                        break;

                    case TableTag.FieldMarshal:
                        numRows = FieldMarshalTable.NumRows;
                        break;

                    case TableTag.FieldRVA:
                        numRows = FieldRVATable.NumRows;
                        break;

                    case TableTag.File:
                        numRows = FileTable.NumRows;
                        break;

                    case TableTag.GenericParam:
                        numRows = GenericParamTable.NumRows;
                        break;

                    case TableTag.GenericParamConstraint:
                        numRows = GenericParamConstraintTable.NumRows;
                        break;

                    case TableTag.ImplMap:
                        numRows = ImplMapTable.NumRows;
                        break;

                    case TableTag.InterfaceImpl:
                        numRows = InterfaceImplTable.NumRows;
                        break;

                    case TableTag.ManifestResource:
                        numRows = ManifestResourceTable.NumRows;
                        break;

                    case TableTag.MemberRef:
                        numRows = MemberRefTable.NumRows;
                        break;

                    case TableTag.MethodDef:
                        numRows = MethodDefTable.NumRows;
                        break;

                    case TableTag.MethodImpl:
                        numRows = MethodImplTable.NumRows;
                        break;

                    case TableTag.MethodSemantics:
                        numRows = MethodSemanticsTable.NumRows;
                        break;

                    case TableTag.MethodSpec:
                        numRows = MethodSpecTable.NumRows;
                        break;

                    case TableTag.ModuleRef:
                        numRows = ModuleRefTable.NumRows;
                        break;

                    case TableTag.NestedClass:
                        numRows = NestedClassTable.NumRows;
                        break;

                    case TableTag.Param:
                        numRows = ParamTable.NumRows;
                        break;

                    case TableTag.Property:
                        numRows = PropertyTable.NumRows;
                        break;

                    case TableTag.PropertyMap:
                        numRows = PropertyMapTable.NumRows;
                        break;

                    case TableTag.StandAloneSig:
                        numRows = StandAloneSigTable.NumRows;
                        break;

                    case TableTag.TypeDef:
                        numRows = TypeDefTable.NumRows;
                        break;

                    case TableTag.TypeRef:
                        numRows = TypeRefTable.NumRows;
                        break;

                    case TableTag.TypeSpec:
                        numRows = TypeSpecTable.NumRows;
                        break;

                    default:
                        throw new PEException("invalid TableTag");
                    }
                    writer.WriteUInt32((uint)numRows);
                }
            }

            for (var i = 0; i < 64; i++)
            {
                if (valid[i])
                {
                    switch ((TableTag)i)
                    {
                    case TableTag.Module:
                        ModuleTable.Write(ctxt, writer);
                        break;

                    case TableTag.Assembly:
                        AssemblyTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyOS:
                        AssemblyOSTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyProcessor:
                        AssemblyProcessorTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyRef:
                        AssemblyRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyRefOS:
                        AssemblyRefOSTable.Write(ctxt, writer);
                        break;

                    case TableTag.AssemblyRefProcessor:
                        AssemblyRefProcessorTable.Write(ctxt, writer);
                        break;

                    case TableTag.ClassLayout:
                        ClassLayoutTable.Write(ctxt, writer);
                        break;

                    case TableTag.Constant:
                        ConstantTable.Write(ctxt, writer);
                        break;

                    case TableTag.CustomAttribute:
                        CustomAttributeTable.Write(ctxt, writer);
                        break;

                    case TableTag.DeclSecurity:
                        DeclSecurityTable.Write(ctxt, writer);
                        break;

                    case TableTag.EventMap:
                        EventMapTable.Write(ctxt, writer);
                        break;

                    case TableTag.Event:
                        EventTable.Write(ctxt, writer);
                        break;

                    case TableTag.ExportedType:
                        ExportedTypeTable.Write(ctxt, writer);
                        break;

                    case TableTag.Field:
                        FieldTable.Write(ctxt, writer);
                        break;

                    case TableTag.FieldLayout:
                        FieldLayoutTable.Write(ctxt, writer);
                        break;

                    case TableTag.FieldMarshal:
                        FieldMarshalTable.Write(ctxt, writer);
                        break;

                    case TableTag.FieldRVA:
                        FieldRVATable.Write(ctxt, writer);
                        break;

                    case TableTag.File:
                        FileTable.Write(ctxt, writer);
                        break;

                    case TableTag.GenericParam:
                        GenericParamTable.Write(ctxt, writer);
                        break;

                    case TableTag.GenericParamConstraint:
                        GenericParamConstraintTable.Write(ctxt, writer);
                        break;

                    case TableTag.ImplMap:
                        ImplMapTable.Write(ctxt, writer);
                        break;

                    case TableTag.InterfaceImpl:
                        InterfaceImplTable.Write(ctxt, writer);
                        break;

                    case TableTag.ManifestResource:
                        ManifestResourceTable.Write(ctxt, writer);
                        break;

                    case TableTag.MemberRef:
                        MemberRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodDef:
                        MethodDefTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodImpl:
                        MethodImplTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodSemantics:
                        MethodSemanticsTable.Write(ctxt, writer);
                        break;

                    case TableTag.MethodSpec:
                        MethodSpecTable.Write(ctxt, writer);
                        break;

                    case TableTag.ModuleRef:
                        ModuleRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.NestedClass:
                        NestedClassTable.Write(ctxt, writer);
                        break;

                    case TableTag.Param:
                        ParamTable.Write(ctxt, writer);
                        break;

                    case TableTag.Property:
                        PropertyTable.Write(ctxt, writer);
                        break;

                    case TableTag.PropertyMap:
                        PropertyMapTable.Write(ctxt, writer);
                        break;

                    case TableTag.StandAloneSig:
                        StandAloneSigTable.Write(ctxt, writer);
                        break;

                    case TableTag.TypeDef:
                        TypeDefTable.Write(ctxt, writer);
                        break;

                    case TableTag.TypeRef:
                        TypeRefTable.Write(ctxt, writer);
                        break;

                    case TableTag.TypeSpec:
                        TypeSpecTable.Write(ctxt, writer);
                        break;

                    default:
                        throw new PEException("invalid TableTag");
                    }
                }
            }
        }