/// <inheritdoc/>
 public void Dispose()
 {
     if (Chunk != null)
     {
         Chunk.Data.Dispose();
     }
     Chunk     = null;
     PESection = null;
 }
 public void Dispose()
 {
     if (chunk != null)
     {
         chunk.Data.Dispose();
     }
     chunk     = null;
     peSection = null;
 }
        /// <summary>
        /// Saves any data that is appended to the original PE file
        /// </summary>
        void CreateExtraData()
        {
            if (!Options.KeepExtraPEData)
            {
                return;
            }
            var lastOffs = GetLastFileSectionOffset();

            extraData = new BinaryReaderChunk(peImage.CreateStream((FileOffset)lastOffs));
            if (extraData.Data.Length == 0)
            {
                extraData.Data.Dispose();
                extraData = null;
            }
        }
예제 #4
0
        public static void Erase(NativeModuleWriter writer, ModuleDefMD module)
        {
            if (writer == null || module == null)
                return;

            var sects = (IList)origSects.GetValue(writer);
            var sections = new List<Tuple<uint, uint, byte[]>>();
            var s = new MemoryStream();
            foreach (var origSect in sects) {
                var oldChunk = (BinaryReaderChunk)chunk.GetValue(origSect);
                var sectHdr = (ImageSectionHeader)peSection.GetValue(origSect);

                s.SetLength(0);
                oldChunk.WriteTo(new BinaryWriter(s));
                var buf = s.ToArray();
                var newChunk = new BinaryReaderChunk(MemoryImageStream.Create(buf), oldChunk.GetVirtualSize());
                newChunk.SetOffset(oldChunk.FileOffset, oldChunk.RVA);

                chunk.SetValue(origSect, newChunk);

                sections.Add(Tuple.Create(
                    sectHdr.PointerToRawData,
                    sectHdr.PointerToRawData + sectHdr.SizeOfRawData,
                    buf));
            }

            var md = module.MetaData;

            var row = md.TablesStream.MethodTable.Rows;
            for (uint i = 1; i <= row; i++) {
                var method = md.TablesStream.ReadMethodRow(i);
                var codeType = ((MethodImplAttributes)method.ImplFlags & MethodImplAttributes.CodeTypeMask);
                if (codeType == MethodImplAttributes.IL)
                    Erase(sections, (uint)md.PEImage.ToFileOffset((RVA)method.RVA));
            }

            var res = md.ImageCor20Header.Resources;
            if (res.Size > 0)
                Erase(sections, (uint)res.StartOffset, res.Size);

            Erase(sections, md.ImageCor20Header);
            Erase(sections, md.MetaDataHeader);
            foreach (var stream in md.AllStreams)
                Erase(sections, stream);
        }
        /// <summary>
        /// Creates the PE header "section"
        /// </summary>
        void CreateHeaderSection()
        {
            uint afterLastSectHeader = GetOffsetAfterLastSectionHeader() + (uint)sections.Count * 0x28;
            uint firstRawOffset      = Math.Min(GetFirstRawDataFileOffset(), peImage.ImageNTHeaders.OptionalHeader.SectionAlignment);
            uint headerLen           = afterLastSectHeader;

            if (firstRawOffset > headerLen)
            {
                headerLen = firstRawOffset;
            }
            headerLen = Utils.AlignUp(headerLen, peImage.ImageNTHeaders.OptionalHeader.FileAlignment);
            if (headerLen <= peImage.ImageNTHeaders.OptionalHeader.SectionAlignment)
            {
                headerSection = new BinaryReaderChunk(peImage.CreateStream(0, headerLen));
                return;
            }

            //TODO: Support this too
            throw new ModuleWriterException("Could not create header");
        }
		/// <summary>
		/// Saves any data that is appended to the original PE file
		/// </summary>
		void CreateExtraData() {
			if (!Options.KeepExtraPEData)
				return;
			var lastOffs = GetLastFileSectionOffset();
			extraData = new BinaryReaderChunk(peImage.CreateStream((FileOffset)lastOffs));
			if (extraData.Data.Length == 0) {
				extraData.Data.Dispose();
				extraData = null;
			}
		}
		/// <summary>
		/// Creates the PE header "section"
		/// </summary>
		void CreateHeaderSection() {
			uint afterLastSectHeader = GetOffsetAfterLastSectionHeader() + (uint)sections.Count * 0x28;
			uint firstRawOffset = Math.Min(GetFirstRawDataFileOffset(), peImage.ImageNTHeaders.OptionalHeader.SectionAlignment);
			uint headerLen = afterLastSectHeader;
			if (firstRawOffset > headerLen)
				headerLen = firstRawOffset;
			headerLen = Utils.AlignUp(headerLen, peImage.ImageNTHeaders.OptionalHeader.FileAlignment);
			if (headerLen <= peImage.ImageNTHeaders.OptionalHeader.SectionAlignment) {
				headerSection = new BinaryReaderChunk(peImage.CreateStream(0, headerLen));
				return;
			}

			//TODO: Support this too
			throw new ModuleWriterException("Could not create header");
		}
			public void Dispose() {
				if (chunk != null)
					chunk.Data.Dispose();
				chunk = null;
				peSection = null;
			}
예제 #9
0
 /// <inheritdoc/>
 public void Dispose()
 {
     if (Chunk != null)
         Chunk.Data.Dispose();
     Chunk = null;
     PESection = null;
 }