Exemplo n.º 1
0
        internal MetadataStream(IMetadata metadata, StreamHeader header)
        {
            if (metadata is null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            if (header is null)
            {
                throw new ArgumentNullException(nameof(header));
            }

            _peImage = metadata.PEImage;
            _offset  = (uint)metadata.StorageSignature.FOA + header.Offset;
            _rawData = (byte *)_peImage.RawData + _offset;
            _length  = header.Size;
        }
Exemplo n.º 2
0
        public Metadata(IPEImage peImage)
        {
            if (peImage is null)
            {
                throw new ArgumentNullException(nameof(peImage));
            }
            if (!peImage.IsDotNetImage)
            {
                throw new InvalidOperationException();
            }

            bool?isCompressed;

            _peImage          = peImage;
            _cor20Header      = new Cor20Header(this);
            _storageSignature = new StorageSignature(this);
            _storageHeader    = new StorageHeader(this);
            _streamHeaders    = new StreamHeader[_storageHeader.StreamCount];
            for (int i = 0; i < _streamHeaders.Length; i++)
            {
                _streamHeaders[i] = new StreamHeader(this, (uint)i);
            }
            isCompressed = null;
            foreach (StreamHeader header in _streamHeaders)
            {
                string name;

                name = header.DisplayName;
                if (isCompressed is null)
                {
                    if (name == "#~")
                    {
                        isCompressed = true;
                    }
                    else if (name == "#-")
                    {
                        isCompressed = false;
                    }
                }
                if (name == "#Schema")
                {
                    isCompressed = false;
                }
            }
            if (isCompressed is null)
            {
                throw new BadImageFormatException("Metadata table (#~ / #-) not found");
            }
            if (isCompressed.Value)
            {
                for (int i = _streamHeaders.Length - 1; i >= 0; i--)
                {
                    switch (_streamHeaders[i].DisplayName)
                    {
                    case "#~":
                        if (_tableStream is null)
                        {
                            _tableStream = new TableStream(this, i, true);
                        }
                        break;

                    case "#Strings":
                        if (_stringHeap is null)
                        {
                            _stringHeap = new StringHeap(this, i);
                        }
                        break;

                    case "#US":
                        if (_userStringHeap is null)
                        {
                            _userStringHeap = new UserStringHeap(this, i);
                        }
                        break;

                    case "#GUID":
                        if (_guidHeap is null)
                        {
                            _guidHeap = new GuidHeap(this, i);
                        }
                        break;

                    case "#Blob":
                        if (_blobHeap is null)
                        {
                            _blobHeap = new BlobHeap(this, i);
                        }
                        break;
                    }
                }
            }
            else
            {
                for (int i = 0; i < _streamHeaders.Length; i++)
                {
                    switch (_streamHeaders[i].DisplayName.ToUpperInvariant())
                    {
                    case "#~":
                    case "#-":
                        if (_tableStream is null)
                        {
                            _tableStream = new TableStream(this, i, false);
                        }
                        break;

                    case "#STRINGS":
                        if (_stringHeap is null)
                        {
                            _stringHeap = new StringHeap(this, i);
                        }
                        break;

                    case "#US":
                        if (_userStringHeap is null)
                        {
                            _userStringHeap = new UserStringHeap(this, i);
                        }
                        break;

                    case "#GUID":
                        if (_guidHeap is null)
                        {
                            _guidHeap = new GuidHeap(this, i);
                        }
                        break;

                    case "#BLOB":
                        if (_blobHeap is null)
                        {
                            _blobHeap = new BlobHeap(this, i);
                        }
                        break;
                    }
                }
            }
        }