public static void GetEmbeddedModule(ModuleReader reader) { // Read the MZ signature. if (reader.ReadByte() != 'M' || reader.ReadByte() != 'Z') { throw new ModuleException("Invalid PE signature."); } // Read the PE offset. reader.SetPosition(0x3c); uint peOffset = reader.ReadUInt(); // Read the PE\0\0 sinature reader.SetPosition(peOffset); if (reader.ReadByte() != 'P' || reader.ReadByte() != 'E' || reader.ReadByte() != 0 || reader.ReadByte() != 0) { throw new ModuleException("Unsupported MS DOS programs."); } // Read the COFF header. CoffHeader header = new CoffHeader(); header.Read(reader); // Ignore the optional header. reader.Skip(header.optionalHeaderSize); // Read the sections until finding the .cbm section. CoffSectionHeader sectionHeader = new CoffSectionHeader(); for (int i = 0; i < header.numSections; ++i) { // Read the section header. sectionHeader.Read(reader); // If this is the .cbm section, done. if (sectionHeader.name == ".cbm") { reader.SetPosition(sectionHeader.rawDataPointer); return; } } // Couldn't find embedded module. throw new ModuleException("Couldn't find embedded Chela module."); }
public static void GetEmbeddedModule(ModuleReader reader) { // Read the MZ signature. if(reader.ReadByte() != 'M' || reader.ReadByte() != 'Z') throw new ModuleException("Invalid PE signature."); // Read the PE offset. reader.SetPosition(0x3c); uint peOffset = reader.ReadUInt(); // Read the PE\0\0 sinature reader.SetPosition(peOffset); if(reader.ReadByte() != 'P' || reader.ReadByte() != 'E' || reader.ReadByte() != 0 || reader.ReadByte() != 0) throw new ModuleException("Unsupported MS DOS programs."); // Read the COFF header. CoffHeader header = new CoffHeader(); header.Read(reader); // Ignore the optional header. reader.Skip(header.optionalHeaderSize); // Read the sections until finding the .cbm section. CoffSectionHeader sectionHeader = new CoffSectionHeader(); for(int i = 0; i < header.numSections; ++i) { // Read the section header. sectionHeader.Read(reader); // If this is the .cbm section, done. if(sectionHeader.name == ".cbm") { reader.SetPosition(sectionHeader.rawDataPointer); return; } } // Couldn't find embedded module. throw new ModuleException("Couldn't find embedded Chela module."); }