Ejemplo n.º 1
0
        /// <summary>
        /// Dispose method
        /// </summary>
        /// <param name="disposing"><c>true</c> if called by <see cref="Dispose()"/></param>
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
            {
                return;
            }
            Dispose(peImage);
            Dispose(stringsStream);
            Dispose(usStream);
            Dispose(blobStream);
            Dispose(guidStream);
            Dispose(tablesStream);
            var as2 = allStreams;

            if (as2 != null)
            {
                foreach (var stream in as2.GetSafeEnumerable())
                {
                    Dispose(stream);
                }
            }
            peImage                   = null;
            cor20Header               = null;
            mdHeader                  = null;
            stringsStream             = null;
            usStream                  = null;
            blobStream                = null;
            guidStream                = null;
            tablesStream              = null;
            allStreams                = null;
            fieldRidToTypeDefRid      = null;
            methodRidToTypeDefRid     = null;
            typeDefRidToNestedClasses = null;
        }
Ejemplo n.º 2
0
 internal MetaData(MetaDataHeader mdHeader, bool isStandalonePortablePdb)
 {
     this.allStreams              = ThreadSafeListCreator.Create <DotNetStream>();
     this.peImage                 = null;
     this.cor20Header             = null;
     this.mdHeader                = mdHeader;
     this.isStandalonePortablePdb = isStandalonePortablePdb;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Dispose method
 /// </summary>
 /// <param name="disposing"><c>true</c> if called by <see cref="Dispose()"/></param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposing)
     {
         return;
     }
     if (peImage != null)
     {
         peImage.Dispose();
     }
     if (stringsStream != null)
     {
         stringsStream.Dispose();
     }
     if (usStream != null)
     {
         usStream.Dispose();
     }
     if (blobStream != null)
     {
         blobStream.Dispose();
     }
     if (guidStream != null)
     {
         guidStream.Dispose();
     }
     if (tablesStream != null)
     {
         tablesStream.Dispose();
     }
     if (allStreams != null)
     {
         foreach (var stream in allStreams)
         {
             if (stream != null)
             {
                 stream.Dispose();
             }
         }
     }
     peImage                   = null;
     cor20Header               = null;
     mdHeader                  = null;
     stringsStream             = null;
     usStream                  = null;
     blobStream                = null;
     guidStream                = null;
     tablesStream              = null;
     allStreams                = null;
     fieldRidToTypeDefRid      = null;
     methodRidToTypeDefRid     = null;
     typeDefRidToNestedClasses = null;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="peImage">The PE image</param>
 /// <param name="cor20Header">The .NET header</param>
 /// <param name="mdHeader">The MD header</param>
 protected MetaData(IPEImage peImage, ImageCor20Header cor20Header, MetaDataHeader mdHeader)
 {
     try {
         this.allStreams  = ThreadSafeListCreator.Create <DotNetStream>();
         this.peImage     = peImage;
         this.cor20Header = cor20Header;
         this.mdHeader    = mdHeader;
     }
     catch {
         if (peImage != null)
         {
             peImage.Dispose();
         }
         throw;
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Create a standalone portable PDB <see cref="MetaData"/> instance
        /// </summary>
        /// <param name="mdStream">Metadata stream</param>
        /// <param name="verify"><c>true</c> if we should verify that it's a .NET PE file</param>
        /// <returns>A new <see cref="MetaData"/> instance</returns>
        internal static MetaData CreateStandalonePortablePDB(IImageStream mdStream, bool verify)
        {
            MetaData md = null;

            try {
                var mdHeader = new MetaDataHeader(mdStream, verify);
                if (verify)
                {
                    foreach (var sh in mdHeader.StreamHeaders)
                    {
                        if (sh.Offset + sh.StreamSize < sh.Offset || sh.Offset + sh.StreamSize > mdStream.Length)
                        {
                            throw new BadImageFormatException("Invalid stream header");
                        }
                    }
                }

                switch (GetMetaDataType(mdHeader.StreamHeaders))
                {
                case MetaDataType.Compressed:
                    md = new CompressedMetaData(mdHeader, true);
                    break;

                case MetaDataType.ENC:
                    md = new ENCMetaData(mdHeader, true);
                    break;

                default:
                    throw new BadImageFormatException("No #~ or #- stream found");
                }
                md.Initialize(mdStream);

                return(md);
            }
            catch {
                if (md != null)
                {
                    md.Dispose();
                }
                throw;
            }
        }
Ejemplo n.º 6
0
 /// <inheritdoc/>
 public CompressedMetaData(IPEImage peImage, ImageCor20Header cor20Header, MetaDataHeader mdHeader)
     : base(peImage, cor20Header, mdHeader)
 {
 }
Ejemplo n.º 7
0
 /// <inheritdoc/>
 public ENCMetaData(IPEImage peImage, ImageCor20Header cor20Header, MetaDataHeader mdHeader)
     : base(peImage, cor20Header, mdHeader)
 {
 }
Ejemplo n.º 8
0
		public StorageHeaderTreeNode(HexDocument doc, MetaDataHeader mdHeader)
			: base((ulong)mdHeader.StorageHeaderOffset, (ulong)mdHeader.StorageHeaderOffset + 4 - 1) {
			this.storageHeaderVM = new StorageHeaderVM(this, doc, StartOffset);
		}
Ejemplo n.º 9
0
        /// <summary>
        /// Create a <see cref="MetaData"/> instance
        /// </summary>
        /// <param name="peImage">The PE image</param>
        /// <param name="verify"><c>true</c> if we should verify that it's a .NET PE file</param>
        /// <returns>A new <see cref="MetaData"/> instance</returns>
        internal static MetaData Create(IPEImage peImage, bool verify)
        {
            IImageStream cor20HeaderStream = null, mdHeaderStream = null;
            MetaData     md = null;

            try {
                var dotNetDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14];
                if (dotNetDir.VirtualAddress == 0)
                {
                    throw new BadImageFormatException(".NET data directory RVA is 0");
                }
                if (dotNetDir.Size < 0x48)
                {
                    throw new BadImageFormatException(".NET data directory size < 0x48");
                }
                var cor20Header = new ImageCor20Header(cor20HeaderStream = peImage.CreateStream(dotNetDir.VirtualAddress, 0x48), verify);
                if (cor20Header.MetaData.VirtualAddress == 0)
                {
                    throw new BadImageFormatException(".NET MetaData RVA is 0");
                }
                if (cor20Header.MetaData.Size < 16)
                {
                    throw new BadImageFormatException(".NET MetaData size is too small");
                }
                var mdSize   = cor20Header.MetaData.Size;
                var mdRva    = cor20Header.MetaData.VirtualAddress;
                var mdHeader = new MetaDataHeader(mdHeaderStream = peImage.CreateStream(mdRva, mdSize), verify);
                if (verify)
                {
                    foreach (var sh in mdHeader.StreamHeaders)
                    {
                        if (sh.Offset + sh.StreamSize < sh.Offset || sh.Offset + sh.StreamSize > mdSize)
                        {
                            throw new BadImageFormatException("Invalid stream header");
                        }
                    }
                }

                switch (GetMetaDataType(mdHeader.StreamHeaders))
                {
                case MetaDataType.Compressed:
                    md = new CompressedMetaData(peImage, cor20Header, mdHeader);
                    break;

                case MetaDataType.ENC:
                    md = new ENCMetaData(peImage, cor20Header, mdHeader);
                    break;

                default:
                    throw new BadImageFormatException("No #~ or #- stream found");
                }
                md.Initialize();

                return(md);
            }
            catch {
                if (md != null)
                {
                    md.Dispose();
                }
                throw;
            }
            finally {
                if (cor20HeaderStream != null)
                {
                    cor20HeaderStream.Dispose();
                }
                if (mdHeaderStream != null)
                {
                    mdHeaderStream.Dispose();
                }
            }
        }
Ejemplo n.º 10
0
		/// <summary>
		/// Dispose method
		/// </summary>
		/// <param name="disposing"><c>true</c> if called by <see cref="Dispose()"/></param>
		protected virtual void Dispose(bool disposing) {
			if (!disposing)
				return;
			if (peImage != null)
				peImage.Dispose();
			if (stringsStream != null)
				stringsStream.Dispose();
			if (usStream != null)
				usStream.Dispose();
			if (blobStream != null)
				blobStream.Dispose();
			if (guidStream != null)
				guidStream.Dispose();
			if (tablesStream != null)
				tablesStream.Dispose();
			if (allStreams != null) {
				foreach (var stream in allStreams) {
					if (stream != null)
						stream.Dispose();
				}
			}
			peImage = null;
			cor20Header = null;
			mdHeader = null;
			stringsStream = null;
			usStream = null;
			blobStream = null;
			guidStream = null;
			tablesStream = null;
			allStreams = null;
			fieldRidToTypeDefRid = null;
			methodRidToTypeDefRid = null;
			typeDefRidToNestedClasses = null;
		}
Ejemplo n.º 11
0
 /// <summary>
 /// Dispose method
 /// </summary>
 /// <param name="disposing"><c>true</c> if called by <see cref="Dispose()"/></param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposing)
         return;
     Dispose(peImage);
     Dispose(stringsStream);
     Dispose(usStream);
     Dispose(blobStream);
     Dispose(guidStream);
     Dispose(tablesStream);
     var as2 = allStreams;
     if (as2 != null) {
         foreach (var stream in as2.GetSafeEnumerable())
             Dispose(stream);
     }
     peImage = null;
     cor20Header = null;
     mdHeader = null;
     stringsStream = null;
     usStream = null;
     blobStream = null;
     guidStream = null;
     tablesStream = null;
     allStreams = null;
     fieldRidToTypeDefRid = null;
     methodRidToTypeDefRid = null;
     typeDefRidToNestedClasses = null;
 }
Ejemplo n.º 12
0
		public StorageHeaderNode(HexBuffer buffer, MetaDataHeader mdHeader)
			: base(HexSpan.FromBounds((ulong)mdHeader.StorageHeaderOffset, (ulong)mdHeader.StorageHeaderOffset + 4)) {
			storageHeaderVM = new StorageHeaderVM(this, buffer, Span.Start);
		}
Ejemplo n.º 13
0
		public StorageSignatureNode(HexBuffer buffer, MetaDataHeader mdHeader)
			: base(HexSpan.FromBounds((ulong)mdHeader.StartOffset, (ulong)mdHeader.StorageHeaderOffset)) {
			storageSignatureVM = new StorageSignatureVM(this, buffer, Span.Start, (int)(Span.Length - 0x10).ToUInt64());
		}
Ejemplo n.º 14
0
		/// <inheritdoc/>
		public ENCMetaData(IPEImage peImage, ImageCor20Header cor20Header, MetaDataHeader mdHeader)
			: base(peImage, cor20Header, mdHeader) {
		}
Ejemplo n.º 15
0
 public StorageSignatureTreeNode(HexDocument doc, MetaDataHeader mdHeader)
     : base((ulong)mdHeader.StartOffset, (ulong)mdHeader.StorageHeaderOffset - 1)
 {
     this.storageSignatureVM = new StorageSignatureVM(doc, StartOffset, (int)(EndOffset - StartOffset + 1 - 0x10));
 }
Ejemplo n.º 16
0
 /// <inheritdoc/>
 internal ENCMetaData(MetaDataHeader mdHeader, bool isStandalonePortablePdb)
     : base(mdHeader, isStandalonePortablePdb)
 {
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="peImage">The PE image</param>
 /// <param name="cor20Header">The .NET header</param>
 /// <param name="mdHeader">The MD header</param>
 protected MetaData(IPEImage peImage, ImageCor20Header cor20Header, MetaDataHeader mdHeader)
 {
     try {
         this.allStreams = ThreadSafeListCreator.Create<DotNetStream>();
         this.peImage = peImage;
         this.cor20Header = cor20Header;
         this.mdHeader = mdHeader;
     }
     catch {
         if (peImage != null)
             peImage.Dispose();
         throw;
     }
 }
Ejemplo n.º 18
0
		/// <inheritdoc/>
		public CompressedMetaData(IPEImage peImage, ImageCor20Header cor20Header, MetaDataHeader mdHeader)
			: base(peImage, cor20Header, mdHeader) {
		}
Ejemplo n.º 19
0
		/// <summary>
		/// Create a <see cref="DotNetFile"/> instance
		/// </summary>
		/// <param name="peImage">The PE image</param>
		/// <param name="verify"><c>true</c> if we should verify that it's a .NET PE file</param>
		/// <returns>A new <see cref="DotNetFile"/> instance</returns>
		public static DotNetFile Load(IPEImage peImage, bool verify) {
			IImageStream cor20HeaderStream = null, mdHeaderStream = null;
			MetaData md = null;
			try {
				var dotNetDir = peImage.ImageNTHeaders.OptionalHeader.DataDirectories[14];
				if (dotNetDir.VirtualAddress == 0)
					throw new BadImageFormatException(".NET data directory RVA is 0");
				if (dotNetDir.Size < 0x48)
					throw new BadImageFormatException(".NET data directory size < 0x48");
				var cor20Header = new ImageCor20Header(cor20HeaderStream = peImage.CreateStream(dotNetDir.VirtualAddress, 0x48), verify);
				if (cor20Header.MetaData.VirtualAddress == 0)
					throw new BadImageFormatException(".NET MetaData RVA is 0");
				if (cor20Header.MetaData.Size < 16)
					throw new BadImageFormatException(".NET MetaData size is too small");
				var mdSize = cor20Header.MetaData.Size;
				var mdRva = cor20Header.MetaData.VirtualAddress;
				var mdHeader = new MetaDataHeader(mdHeaderStream = peImage.CreateStream(mdRva, mdSize), verify);
				if (verify) {
					foreach (var sh in mdHeader.StreamHeaders) {
						if (sh.Offset + sh.StreamSize < sh.Offset || sh.Offset + sh.StreamSize > mdSize)
							throw new BadImageFormatException("Invalid stream header");
					}
				}

				switch (GetMetaDataType(mdHeader.StreamHeaders)) {
				case MetaDataType.Compressed:
					md = new CompressedMetaData(peImage, cor20Header, mdHeader);
					break;

				case MetaDataType.ENC:
					md = new ENCMetaData(peImage, cor20Header, mdHeader);
					break;

				default:
					throw new BadImageFormatException("No #~ or #- stream found");
				}
				md.Initialize();

				return new DotNetFile(md);
			}
			catch {
				if (md != null)
					md.Dispose();
				throw;
			}
			finally {
				if (cor20HeaderStream != null)
					cor20HeaderStream.Dispose();
				if (mdHeaderStream != null)
					mdHeaderStream.Dispose();
			}
		}
Ejemplo n.º 20
0
 /// <inheritdoc/>
 internal CompressedMetaData(MetaDataHeader mdHeader, bool isStandalonePortablePdb)
     : base(mdHeader, isStandalonePortablePdb)
 {
 }