void () { ImageOptionalHeader imageOptionalHeader = myReader.getPeHeader().getImageOptionalHeader(); final int FileAlignment = (int)imageOptionalHeader.getFileAlignment().getValue(); final int SectionAlignment = (int)imageOptionalHeader.getSectionAlignment().getValue(); List<String> errors = new ArrayList<>(); Bin.ArrayOfBins<ImageSectionHeader> sections = myReader.getSectionHeaders(); for (ImageSectionHeader header : sections) { String name = header.getSectionName(); long sizeOfRawData = header.getSizeOfRawData().getValue(); long pointerToRawData = header.getPointerToRawData().getValue(); long virtualAddress = header.getVirtualAddress().getValue(); long virtualSize = header.getVirtualSize().getValue(); if (pointerToRawData == 0) { errors.add(format("Section '%s' may not have zero PointerToRawData", name)); } if (virtualAddress == 0) { errors.add(format("Section '%s' may not have zero VirtualAddress", name)); } if (sizeOfRawData % FileAlignment != 0) { errors.add( format("SizeOfRawData of section '%s' isn't dividable by 'FileAlignment' (%#x): %#x", name, FileAlignment, sizeOfRawData)); } if (pointerToRawData % FileAlignment != 0) { errors.add( format("PointerToRawData of section '%s' isn't dividable by 'FileAlignment' (%#x): %#x", name, FileAlignment, pointerToRawData)); } if (virtualAddress % SectionAlignment != 0) { errors.add( format("VirtualAddress of section '%s' isn't dividable by 'SectionAlignment' (%#x): %#x", name, SectionAlignment, virtualAddress)); } if (name.equals(Section.RESOURCES_SECTION_NAME) && virtualSize < sizeOfRawData) { errors.add( format("VirtualSize of section '%s' is smaller than SizeOfRawData (%#x): %#x", name, sizeOfRawData, virtualSize)); } } Bin.ArrayOfBins<ImageDataDirectory> imageDataDirs = imageOptionalHeader.getImageDataDirectories(); check(errors, "resources", imageDataDirs.get(ImageDataDirectory.IMAGE_DIRECTORY_ENTRY_RESOURCE), getSection(sections, Section.RESOURCES_SECTION_NAME)); check(errors, "relocations", imageDataDirs.get(ImageDataDirectory.IMAGE_DIRECTORY_ENTRY_BASERELOC), getSection(sections, Section.RELOCATIONS_SECTION_NAME)); //SizeOfImage //The size of the image, in bytes, including all headers. Must be a multiple of SectionAlignment. long sizeOfImage = imageOptionalHeader.getSizeOfImage().getValue(); if (sizeOfImage % SectionAlignment != 0) { errors.add(format("SizeOfImage isn't dividable by 'SectionAlignment' (%#x): %#x", SectionAlignment, sizeOfImage)); } if (!errors.isEmpty()) { StringBuilder msg = new StringBuilder(); msg.append("Output verification failed with ").append(errors.size()).append(" error"); if (errors.size() > 1) msg.append("s"); msg.append(":\n"); for (String error : errors) { msg.append('\t').append(error).append('\n'); } throw new RuntimeException(msg.toString()); } }