Java Code Examples for ghidra.app.util.bin.format.FactoryBundledWithBinaryReader#readNextByte()

The following examples show how to use ghidra.app.util.bin.format.FactoryBundledWithBinaryReader#readNextByte() . 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: NList.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void initNList(FactoryBundledWithBinaryReader reader, boolean is32bit)
		throws IOException {
	this.is32bit = is32bit;

	n_strx = reader.readNextInt();
	n_type = reader.readNextByte();
	n_sect = reader.readNextByte();
	n_desc = reader.readNextShort();
	if (is32bit) {
		n_value = reader.readNextInt() & 0xffffffffL;
	}
	else {
		n_value = reader.readNextLong();
	}
}
 
Example 2
Source File: DebugMisc.java    From ghidra with Apache License 2.0 5 votes vote down vote up
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 3
Source File: InformationBlock.java    From ghidra with Apache License 2.0 4 votes vote down vote up
InformationBlock(FactoryBundledWithBinaryReader reader, short index)
		throws InvalidWindowsHeaderException, IOException {
	long oldIndex = reader.getPointerIndex();
	reader.setPointerIndex(Conv.shortToInt(index));

	ne_magic = reader.readNextShort();

	if (ne_magic != WindowsHeader.IMAGE_NE_SIGNATURE) {
		throw new InvalidWindowsHeaderException();
	}

	ne_ver = reader.readNextByte();
	ne_rev = reader.readNextByte();
	ne_enttab = reader.readNextShort();
	ne_cbenttab = reader.readNextShort();
	ne_crc = reader.readNextInt();
	ne_flags_prog = reader.readNextByte();
	ne_flags_app = reader.readNextByte();
	ne_autodata = reader.readNextShort();
	ne_heap = reader.readNextShort();
	ne_stack = reader.readNextShort();
	ne_csip = reader.readNextInt();
	ne_sssp = reader.readNextInt();
	ne_cseg = reader.readNextShort();
	ne_cmod = reader.readNextShort();
	ne_cbnrestab = reader.readNextShort();
	ne_segtab = reader.readNextShort();
	ne_rsrctab = reader.readNextShort();
	ne_restab = reader.readNextShort();
	ne_modtab = reader.readNextShort();
	ne_imptab = reader.readNextShort();
	ne_nrestab = reader.readNextInt();
	ne_cmovent = reader.readNextShort();
	ne_align = reader.readNextShort();
	ne_cres = reader.readNextShort();
	ne_exetyp = reader.readNextByte();
	ne_flagsothers = reader.readNextByte();
	ne_pretthunks = reader.readNextShort();
	ne_psegrefbytes = reader.readNextShort();
	ne_swaparea = reader.readNextShort();
	ne_expver = reader.readNextShort();

	reader.setPointerIndex(oldIndex);
}
 
Example 4
Source File: ElfSymbol.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void initElfSymbol(FactoryBundledWithBinaryReader reader, int symbolIndex,
		ElfSymbolTable symbolTable, ElfStringTable stringTable, ElfHeader header)
		throws IOException {
	this.header = header;
	this.symbolTable = symbolTable;
	this.symbolTableIndex = symbolIndex;

	if (header.is32Bit()) {
		st_name = reader.readNextInt();
		st_value = reader.readNextInt() & Conv.INT_MASK;
		st_size = reader.readNextInt() & Conv.INT_MASK;
		st_info = reader.readNextByte();
		st_other = reader.readNextByte();
		st_shndx = reader.readNextShort();
	}
	else {
		st_name = reader.readNextInt();
		st_info = reader.readNextByte();
		st_other = reader.readNextByte();
		st_shndx = reader.readNextShort();
		st_value = reader.readNextLong();
		st_size = reader.readNextLong();
	}

	if (st_name == 0) {
		if (getType() == STT_SECTION) {
			ElfSectionHeader[] sections = header.getSections();
			if (st_shndx < 0 || st_shndx >= sections.length) {
				//invalid section reference...
				//this is a bug in objcopy, whereby sections are removed
				//but the corresponding section symbols are left behind.
			}
			else {
				ElfSectionHeader section = sections[st_shndx];
				nameAsString = section.getNameAsString();
			}
		}
	}
	else {
		nameAsString = stringTable.readString(reader, st_name);
	}
}
 
Example 5
Source File: ElfHeader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
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);
	}
}