Пример #1
0
        private void UpdateFileHeader()
        {
            var header = Assembly.NtHeaders.FileHeader;

            header.NumberOfSections     = (ushort)SectionTable.GetSections().Count();
            header.SizeOfOptionalHeader = 0xE0;
        }
Пример #2
0
        public void TestReadWrite()
        {
            FileInformationBlock fib = _hWPFDocFixture._fib;

            byte[] mainStream  = _hWPFDocFixture._mainStream;
            byte[] tableStream = _hWPFDocFixture._tableStream;
            int    fcMin       = fib.GetFcMin();

            CPSplitCalculator cps = new CPSplitCalculator(fib);

            ComplexFileTable cft = new ComplexFileTable(mainStream, tableStream, fib.GetFcClx(), fcMin);
            TextPieceTable   tpt = cft.GetTextPieceTable();

            SectionTable sectionTable = new SectionTable(mainStream, tableStream,
                                                         fib.GetFcPlcfsed(),
                                                         fib.GetLcbPlcfsed(),
                                                         fcMin, tpt, cps);
            HWPFFileSystem fileSys = new HWPFFileSystem();

            sectionTable.WriteTo(fileSys, 0);
            MemoryStream tableOut = fileSys.GetStream("1Table");
            MemoryStream mainOut  = fileSys.GetStream("WordDocument");

            byte[] newTableStream = tableOut.ToArray();
            byte[] newMainStream  = mainOut.ToArray();

            SectionTable newSectionTable = new SectionTable(
                newMainStream, newTableStream, 0,
                newTableStream.Length, 0, tpt, cps);

            List <SEPX> oldSections = sectionTable.GetSections();
            List <SEPX> newSections = newSectionTable.GetSections();

            Assert.AreEqual(oldSections.Count, newSections.Count);

            //test for proper char offset conversions
            PlexOfCps oldSedPlex = new PlexOfCps(tableStream, fib.GetFcPlcfsed(),
                                                 fib.GetLcbPlcfsed(), 12);
            PlexOfCps newSedPlex = new PlexOfCps(newTableStream, 0,
                                                 newTableStream.Length, 12);

            Assert.AreEqual(oldSedPlex.Length, newSedPlex.Length);

            for (int x = 0; x < oldSedPlex.Length; x++)
            {
                Assert.AreEqual(oldSedPlex.GetProperty(x).Start, newSedPlex.GetProperty(x).Start);
                Assert.AreEqual(oldSedPlex.GetProperty(x).End, newSedPlex.GetProperty(x).End);
            }

            int size = oldSections.Count;

            for (int x = 0; x < size; x++)
            {
                PropertyNode oldNode = (PropertyNode)oldSections[x];
                PropertyNode newNode = (PropertyNode)newSections[x];
                Assert.AreEqual(oldNode, newNode);
            }
        }
Пример #3
0
        private void UpdateOptionalHeader()
        {
            var header = Assembly.NtHeaders.OptionalHeader;

            header.SizeOfCode            = _textSectionHeader.Section.GetPhysicalLength();
            header.SizeOfInitializedData = (_relocSectionHeader != null ? _relocSectionHeader.Section.GetPhysicalLength() : 0) +
                                           (_rsrcSectionHeader != null ? _rsrcSectionHeader.Section.GetPhysicalLength() : 0);
            header.BaseOfCode = _textSectionHeader.VirtualAddress;
            if (_relocSectionHeader != null)
            {
                header.BaseOfData = _relocSectionHeader.VirtualAddress;
            }

            var lastSection = SectionTable.GetSections().Last().Header;

            header.SizeOfImage = lastSection.VirtualAddress +
                                 Align(lastSection.VirtualSize, Assembly.NtHeaders.OptionalHeader.SectionAlignment);
            header.SizeOfHeaders = 0x200;

            header.AddressOfEntrypoint = (uint)Assembly.FileOffsetToRva(_textContents.Bootstrapper.StartOffset);

            UpdateDataDirectories();
        }