Beispiel #1
0
        public override void VisitMetadataStreamCollection(MetadataStreamCollection streams)
        {
            foreach (MetadataStream stream in streams)
            {
                MetadataStream.MetadataStreamHeader header = stream.Header;

                header.Offset = (uint)(m_binaryWriter.BaseStream.Position);
                m_binaryWriter.Write(header.Offset);
                MemoryBinaryWriter container;
                string             name = header.Name;
                uint size = 0;
                switch (header.Name)
                {
                case MetadataStream.Tables:
                    container = m_tWriter;
                    size     += 24;                 // header
                    break;

                case MetadataStream.Strings:
                    name     += "\0\0\0\0";
                    container = m_stringWriter;
                    break;

                case MetadataStream.GUID:
                    container = m_guidWriter;
                    break;

                case MetadataStream.Blob:
                    container = m_blobWriter;
                    break;

                case MetadataStream.UserStrings:
                    container = m_usWriter;
                    break;

                default:
                    throw new MetadataFormatException("Unknown stream kind");
                }

                size += (uint)(container.BaseStream.Length + 3 & (~3));
                m_binaryWriter.Write(size);
                m_binaryWriter.Write(Encoding.ASCII.GetBytes(name));
                m_binaryWriter.QuadAlign();
            }
        }
Beispiel #2
0
        public override void VisitMetadataStreamHeader(MetadataStream.MetadataStreamHeader header)
        {
            header.Offset = m_binaryReader.ReadUInt32();
            header.Size   = m_binaryReader.ReadUInt32();

            StringBuilder buffer = new StringBuilder();

            while (true)
            {
                char cur = (char)m_binaryReader.ReadSByte();
                if (cur == '\0')
                {
                    break;
                }
                buffer.Append(cur);
            }
            header.Name = buffer.ToString();
            if (header.Name.Length == 0)
            {
                throw new MetadataFormatException("Invalid stream name");
            }

            long rootpos = m_root.GetImage().ResolveVirtualAddress(
                m_root.GetImage().CLIHeader.Metadata.VirtualAddress);

            long curpos = m_binaryReader.BaseStream.Position;

            if (header.Size != 0)
            {
                curpos -= rootpos;
            }

            curpos += 3;
            curpos &= ~3;

            if (header.Size != 0)
            {
                curpos += rootpos;
            }

            m_binaryReader.BaseStream.Position = curpos;

            header.Stream.Heap = MetadataHeap.HeapFactory(header.Stream);
        }