Пример #1
0
        private SectionHeader *FindSection(uint virtualAddress)
        {
            if (PEHeader->NumberOfSections == 0)
            {
                return(null);
            }
            //1. Find the greatest lower bound of virtualAddress in the section table.
            var first = SectionTable;
            var min   = SectionTable;
            var max   = SectionTable + PEHeader->NumberOfSections - 1;

            while (max >= first && max != min)
            {
                var mid = (((max - min) + 1) / 2) + min;
                if (mid->VirtualAddress == virtualAddress)
                {
                    return(mid);
                }
                if (virtualAddress < mid->VirtualAddress)
                {
                    max = mid - 1;
                }
                else
                {
                    virtualAddress.AssumeGT(mid->VirtualAddress);
                    min = mid;
                }
            }
            if (max < first)
            {
                return(null);
            }

            //2. Verify that the virtual address fits within the found section
            //(if we select the last section as the glb, the virtual address might be outside it).
            try {
                virtualAddress.AssumeGTE(min->VirtualAddress);
                if (virtualAddress > checked (min->VirtualAddress + min->VirtualSize))
                {
                    return(null);
                }
            }
            catch (OverflowException) {
                return(null);
            }
            return(min);
        }