Java Code Examples for ghidra.app.util.bin.format.FactoryBundledWithBinaryReader#readNextAsciiString()
The following examples show how to use
ghidra.app.util.bin.format.FactoryBundledWithBinaryReader#readNextAsciiString() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Section.java From ghidra with Apache License 2.0 | 5 votes |
private void initSection(FactoryBundledWithBinaryReader reader, boolean is32bit) throws IOException { this.reader = reader; this.is32bit = is32bit; sectname = reader.readNextAsciiString(MachConstants.NAME_LENGTH); segname = reader.readNextAsciiString(MachConstants.NAME_LENGTH); if (is32bit) { addr = reader.readNextInt() & 0xffffffffL; size = reader.readNextInt() & 0xffffffffL; } else { addr = reader.readNextLong(); size = reader.readNextLong(); } offset = reader.readNextInt(); align = reader.readNextInt(); reloff = reader.readNextInt(); nrelocs = reader.readNextInt(); flags = reader.readNextInt(); reserved1 = reader.readNextInt(); reserved2 = reader.readNextInt(); if (!is32bit) { reserved3 = reader.readNextInt(); } long index = reader.getPointerIndex(); reader.setPointerIndex(reloff); for (int i = 0; i < nrelocs; ++i) { relocations.add(RelocationFactory.readRelocation(reader, is32bit)); } reader.setPointerIndex(index); }
Example 2
Source File: SegmentCommand.java From ghidra with Apache License 2.0 | 5 votes |
private void initSegmentCommand(FactoryBundledWithBinaryReader reader, boolean is32bit) throws IOException { initLoadCommand(reader); this.is32bit = is32bit; segname = reader.readNextAsciiString(MachConstants.NAME_LENGTH); if (is32bit) { vmaddr = reader.readNextInt() & 0xffffffffL; vmsize = reader.readNextInt() & 0xffffffffL; fileoff = reader.readNextInt() & 0xffffffffL; filesize = reader.readNextInt() & 0xffffffffL; } else { vmaddr = reader.readNextLong(); vmsize = reader.readNextLong(); fileoff = reader.readNextLong(); filesize = reader.readNextLong(); } maxprot = reader.readNextInt(); initprot = reader.readNextInt(); nsects = reader.readNextInt(); flags = reader.readNextInt(); for (int i = 0; i < nsects; ++i) { sections.add(Section.createSection(reader, is32bit)); } }
Example 3
Source File: DebugMisc.java From ghidra with Apache License 2.0 | 5 votes |
private void initDebugMisc(FactoryBundledWithBinaryReader reader, DebugDirectory debugDir, OffsetValidator validator) throws IOException { this.debugDir = debugDir; long oldIndex = reader.getPointerIndex(); long index = debugDir.getPointerToRawData() & Conv.INT_MASK; if (!validator.checkPointer(index)) { Msg.error(this, "Invalid file index " + Long.toHexString(index)); return; } reader.setPointerIndex(index); dataType = reader.readNextInt(); length = reader.readNextInt(); unicode = reader.readNextByte() == 1; reserved = reader.readNextByteArray(3); if (length > 0) { actualData = (unicode ? reader.readNextUnicodeString(length) : reader.readNextAsciiString()); } else { Msg.error(this, "Bad string length " + Integer.toHexString(length)); } reader.setPointerIndex(oldIndex); }
Example 4
Source File: ElfRelocationTable.java From ghidra with Apache License 2.0 | 4 votes |
private List<ElfRelocation> parseAndroidRelocations(FactoryBundledWithBinaryReader reader) throws IOException { String identifier = reader.readNextAsciiString(4); if (!"APS2".equals(identifier)) { throw new IOException("Unsupported Android relocation table format"); } List<ElfRelocation> relocations = new ArrayList<>(); try { int relocationIndex = 0; long remainingRelocations = LEB128.decode(reader, true); long offset = LEB128.decode(reader, true); long addend = 0; while (remainingRelocations > 0) { long groupSize = LEB128.decode(reader, true); if (groupSize > remainingRelocations) { Msg.warn(this, "Group relocation count " + groupSize + " exceeded total count " + remainingRelocations); break; } long groupFlags = LEB128.decode(reader, true); boolean groupedByInfo = (groupFlags & AndroidElfRelocationGroup.RELOCATION_GROUPED_BY_INFO_FLAG) != 0; boolean groupedByDelta = (groupFlags & AndroidElfRelocationGroup.RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG) != 0; boolean groupedByAddend = (groupFlags & AndroidElfRelocationGroup.RELOCATION_GROUPED_BY_ADDEND_FLAG) != 0; boolean groupHasAddend = (groupFlags & AndroidElfRelocationGroup.RELOCATION_GROUP_HAS_ADDEND_FLAG) != 0; long groupOffsetDelta = groupedByDelta ? LEB128.decode(reader, true) : 0; long groupRInfo = groupedByInfo ? LEB128.decode(reader, true) : 0; if (groupedByAddend && groupHasAddend) { addend += LEB128.decode(reader, true); } for (int i = 0; i < groupSize; i++) { offset += groupedByDelta ? groupOffsetDelta : LEB128.decode(reader, true); long info = groupedByInfo ? groupRInfo : LEB128.decode(reader, true); long rAddend = 0; if (groupHasAddend) { if (!groupedByAddend) { addend += LEB128.decode(reader, true); } rAddend = addend; } relocations.add(ElfRelocation.createElfRelocation(reader.getFactory(), elfHeader, relocationIndex++, addendTypeReloc, offset, info, rAddend)); } if (!groupHasAddend) { addend = 0; } remainingRelocations -= groupSize; } } catch (IOException e) { Msg.error(this, "Error reading relocations.", e); } return relocations; }
Example 5
Source File: ElfHeader.java From ghidra with Apache License 2.0 | 4 votes |
protected void initElfHeader(GenericFactory factory, ByteProvider provider) throws ElfException { try { determineHeaderEndianess(provider); reader = new FactoryBundledWithBinaryReader(factory, provider, hasLittleEndianHeaders); e_ident_magic_num = reader.readNextByte(); e_ident_magic_str = reader.readNextAsciiString(ElfConstants.MAGIC_STR_LEN); boolean magicMatch = ElfConstants.MAGIC_NUM == e_ident_magic_num && ElfConstants.MAGIC_STR.equalsIgnoreCase(e_ident_magic_str); if (!magicMatch) { throw new ElfException("Not a valid ELF executable."); } e_ident_class = reader.readNextByte(); e_ident_data = reader.readNextByte(); e_ident_version = reader.readNextByte(); e_ident_osabi = reader.readNextByte(); e_ident_abiversion = reader.readNextByte(); e_ident_pad = reader.readNextByteArray(PAD_LENGTH); e_type = reader.readNextShort(); e_machine = reader.readNextShort(); e_version = reader.readNextInt(); if (is32Bit()) { e_entry = reader.readNextInt() & 0xffffffffL; e_phoff = reader.readNextInt() & 0xffffffffL; e_shoff = reader.readNextInt() & 0xffffffffL; } else if (is64Bit()) { e_entry = reader.readNextLong(); e_phoff = reader.readNextLong(); e_shoff = reader.readNextLong(); } else { throw new ElfException("Only 32-bit and 64-bit ELF headers are supported."); } e_flags = reader.readNextInt(); e_ehsize = reader.readNextShort(); e_phentsize = reader.readNextShort(); e_phnum = reader.readNextShort(); if (e_phnum < 0) { e_phnum = 0; // protect against stripped program headers } e_shentsize = reader.readNextShort(); e_shnum = reader.readNextShort(); if (e_shnum < 0) { e_shnum = 0; // protect against stripped section headers (have seen -1) } e_shstrndx = reader.readNextShort(); } catch (IOException e) { throw new ElfException(e); } }