Java Code Examples for ghidra.app.util.bin.BinaryReader#readNextUnsignedByte()

The following examples show how to use ghidra.app.util.bin.BinaryReader#readNextUnsignedByte() . 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: DWARFUtil.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Read a variable-sized unsigned integer and return it as a java signed int.
 * <p>
 * Unsigned 32 bit int values larger than java's signed Integer.MAX_VALUE are not
 * supported and will throw an IOException.
 *
 * @param reader {@link BinaryReader} to read the data from
 * @param size number of bytes the integer value is stored in, must be 1, 2 or 4.
 * @return unsigned integer value.
 * @throws IOException if error
 */
public static int readVarSizedUInt(BinaryReader reader, int size) throws IOException {
	switch (size) {
		case 1:
			return reader.readNextUnsignedByte();
		case 2:
			return reader.readNextUnsignedShort();
		case 4:
			long l = reader.readNextUnsignedInt();
			if (l < 0 || l > Integer.MAX_VALUE) {
				throw new IOException("Unsigned int value too large: " + l);
			}
			return (int) l;
	}
	throw new IOException("Unsupported variable-sized int: " + size);
}
 
Example 2
Source File: LEB128.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes a LEB128 number using a binary reader and stores it in a long.
 * <p>
 * Large unsigned integers that use 64 bits will be returned in java's native
 * 'long' type, which is signed.  It is up to the caller to treat the value as unsigned.
 * <p>
 * Large integers that use more than 64 bits will cause an IOException to be thrown.
 * <p>
 * @param reader the binary reader
 * @param isSigned true if the value is signed
 * @throws IOException if an I/O error occurs
 */
public static long decode(BinaryReader reader, boolean isSigned) throws IOException {
	int nextByte = 0;
	int shift = 0;
	long value = 0;
	boolean overflow = false;
	while (true) {
		nextByte = reader.readNextUnsignedByte();
		if (shift == 70 || (isSigned == false && shift == 63 && nextByte > 1)) {
			// if the value being read is more than 64 bits long mark it as overflow.
			// keep reading the rest of the number so the caller is not left in the
			// middle of the LEB128 number's guts.
			overflow = true;
		}

		// must cast to long before shifting otherwise shift values greater than 32 cause problems
		value |= ((long) (nextByte & 0x7F)) << shift;
		shift += 7;

		if ((nextByte & 0x80) == 0) {
			break;
		}
	}
	if (overflow) {
		throw new IOException(
			"Unsupported LEB128 value, too large to fit in 64bit java long variable");
	}
	if ((isSigned) && (shift < Long.SIZE) && ((nextByte & 0x40) != 0)) {
		value |= -(1 << shift);
	}

	return value;
}
 
Example 3
Source File: DWARFUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Read a variable-sized unsigned integer and return it as a java signed long.
 * <p>
 * @param reader {@link BinaryReader} to read the data from
 * @param pointerSize number of bytes the value is stored in, must be 1, 2, 4, or 8.
 * @return unsigned long integer value.
 * @throws IOException if error
 */
public static long readVarSizedULong(BinaryReader reader, int pointerSize) throws IOException {
	switch (pointerSize) {
		case 1:
			return reader.readNextUnsignedByte();
		case 2:
			return reader.readNextUnsignedShort();
		case 4:
			return reader.readNextUnsignedInt();
		case 8:
			return reader.readNextLong() /* no unsigned long mask possible */;
	}
	throw new IOException("Unsupported variable-sized int: " + pointerSize);
}
 
Example 4
Source File: DWARFUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a variable-sized unsigned 'address' value from a {@link BinaryReader} and
 * returns it as a 64 bit java long.
 * <p>
 * The valid pointerSizes are 1, 2, 4, and 8.
 * <p>
 * @param reader {@link BinaryReader} to read the data from
 * @param pointerSize number of bytes the value is stored in, must be 1, 2, 4, or 8.
 * @return unsigned long value.
 * @throws IOException if error
 */
public static long readAddressAsLong(BinaryReader reader, byte pointerSize) throws IOException {
	switch (pointerSize) {
		case 1:
			return reader.readNextUnsignedByte();
		case 2:
			return reader.readNextUnsignedShort();
		case 4:
			return reader.readNextUnsignedInt();
		case 8:
			return reader.readNextLong();
	}
	throw new IllegalArgumentException(
		"Unknown pointer size: 0x" + Integer.toHexString(pointerSize));
}
 
Example 5
Source File: DWARFLine.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public DWARFLine(BinaryReader reader) throws IOException, DWARFException {
	this.unit_length = reader.readNextUnsignedInt();
	// Length of 0xffffffff implies 64-bit DWARF format
	if (this.unit_length == 0xffffffffL) {
		this.unit_length = reader.readNextLong();
		this.format = DWARFCompilationUnit.DWARF_64;
	}
	// Length of 0xfffffff0 or greater is reserved for DWARF
	else if (this.unit_length >= 0xfffffff0L) {
		throw new DWARFException("Reserved DWARF length value: " +
			Long.toHexString(this.unit_length) + ". Unknown extension.");
	}
	else {
		this.format = DWARFCompilationUnit.DWARF_32;
	}

	// A version number for this line number information section
	this.version = reader.readNextUnsignedShort();

	// Get the header length based on the current format
	this.header_length = DWARFUtil.readOffsetByDWARFformat(reader, this.format);

	this.minimum_instruction_length = reader.readNextUnsignedByte();

	// Maximum operations per instruction only exists in DWARF version 4 or higher
	if (this.version >= 4) {
		this.maximum_operations_per_instruction = reader.readNextUnsignedByte();
	}
	else {
		this.maximum_operations_per_instruction = 1;
	}
	this.default_is_stmt = reader.readNextUnsignedByte();
	this.line_base = reader.readNextByte();
	this.line_range = reader.readNextUnsignedByte();
	this.opcode_base = reader.readNextUnsignedByte();
	this.standard_opcode_length = new int[this.opcode_base];
	this.standard_opcode_length[0] = 1; /* Should never be used */
	for (int i = 1; i < this.opcode_base; i++) {
		this.standard_opcode_length[i] = reader.readNextUnsignedByte();
	}

	// Read all include directories
	this.include_directories = new ArrayList<>();
	String include = reader.readNextAsciiString();
	while (include.length() != 0) {
		this.include_directories.add(include);
		include = reader.readNextAsciiString();
	}

	// Read all files
	this.file_names = new ArrayList<>();
	DWARFFile file = new DWARFFile(reader);
	while (file.getName().length() != 0) {
		this.file_names.add(file);
		file = new DWARFFile(reader);
	}
}
 
Example 6
Source File: RELHeader.java    From Ghidra-GameCube-Loader with Apache License 2.0 4 votes vote down vote up
private void readHeader(BinaryReader reader) {
	try {
		reader.setPointerIndex(0);
		
		this.moduleId = reader.readNextUnsignedInt();
		this.previousModuleAddress = reader.readNextUnsignedInt();
		this.nextModuleAddress = reader.readNextUnsignedInt();
		this.sectionCount = reader.readNextUnsignedInt();
		this.sectionTableOffset = reader.readNextUnsignedInt();
		this.moduleNameOffset = reader.readNextUnsignedInt();
		this.moduleNameLength = reader.readNextUnsignedInt();
		this.moduleVersion = reader.readNextUnsignedInt();
		this.bssSize = reader.readNextUnsignedInt();
		this.relocationTableOffset = reader.readNextUnsignedInt();
		this.importTableOffset = reader.readNextUnsignedInt();
		this.importTableSize = reader.readNextUnsignedInt();
		this.prologSectionId = reader.readNextUnsignedByte();
		this.epilogSectionId = reader.readNextUnsignedByte();
		this.unresolvedSectionId = reader.readNextUnsignedByte();
		this.bssSectionId = reader.readNextUnsignedByte();
		this.prologSectionOffset = reader.readNextUnsignedInt();
		this.epilogSectionOffset = reader.readNextUnsignedInt();
		this.unresolvedSectionOffset = reader.readNextUnsignedInt();
		
		// Version specific settings
		if (this.moduleVersion > 1) {
			this.sectionAlignment = reader.readNextUnsignedInt();
			this.bssSectionAlignment = reader.readNextUnsignedInt();
		}
		else {
		    // Version 1's default values for alignment
		    this.sectionAlignment = 32;
		    this.bssSectionAlignment = 32;
		}
		
		if (this.moduleVersion > 2) {
			this.fixSize = reader.readNextUnsignedInt();
		}
		
		// Only read the sections if the header is valid.
		if (IsValid(reader)) {
			// Read sections info.
			reader.setPointerIndex(this.sectionTableOffset);
			this.sections = new SectionInfo[(int) this.sectionCount];
			for (var i = 0; i < this.sectionCount; i++) {
				this.sections[i] = new SectionInfo(reader.readNextUnsignedInt(), reader.readNextUnsignedInt());
			}
		}
	}
	catch (IOException e) {
		Msg.error(this,  "Failed to read REL header!");
	}
}