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

The following examples show how to use ghidra.app.util.bin.BinaryReader#readNextLong() . 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: ObjectiveC2_ClassRW.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public ObjectiveC2_ClassRW(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._state = state;
	this._index = reader.getPointerIndex();

	if (state.is32bit) {
		flags         = reader.readNextInt() & Conv.INT_MASK;
		instanceStart = reader.readNextInt() & Conv.INT_MASK;
		instanceSize  = reader.readNextInt() & Conv.INT_MASK;
		reserved      = reader.readNextInt() & Conv.INT_MASK;
	}
	else {
		flags         = reader.readNextLong();
		instanceStart = reader.readNextLong();
		instanceSize  = reader.readNextLong();
	}

	readName(reader);
	readBaseMethods(reader);
	readBaseProtocols(reader);
	readInstanceVariables(reader);

	weakIvarLayout = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);

	readBaseProperties(reader);
}
 
Example 2
Source File: DmgHeaderV2.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public DmgHeaderV2(BinaryReader reader) throws IOException {
	if (reader.isLittleEndian()) {
		throw new IOException("binary reader must be BIG endian");
	}
	signature   =  reader.readNextByteArray( 0x8 );
	version     =  reader.readNextInt();
	ivSize      =  reader.readNextInt();
	unknown0    =  reader.readNextInt();
	unknown1    =  reader.readNextInt();
	unknown2    =  reader.readNextInt();
	unknown3    =  reader.readNextInt();
	unknown4    =  reader.readNextInt();
	uuid        =  reader.readNextByteArray( 0x10 );
	blockSize   =  reader.readNextInt();
	dataSize    =  reader.readNextLong();
	dataOffset  =  reader.readNextLong();
}
 
Example 3
Source File: ObjectiveC2_Protocol.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public ObjectiveC2_Protocol(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._state = state;
	this._index = reader.getPointerIndex();

	isa = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);//TODO
	readName(reader);
	readProtocols(reader);
	readInstanceMethods(reader);
	readClassMethods(reader);
	readOptionalInstanceMethods(reader);
	readOptionalClassMethods(reader);
	readInstanceProperties(reader);

	if (state.is32bit) {
		unknown0 = reader.readNextInt() & Conv.INT_MASK;
		unknown1 = reader.readNextInt() & Conv.INT_MASK;
	}
	else {
		unknown0 = reader.readNextLong();
		unknown1 = reader.readNextLong();
	}
}
 
Example 4
Source File: ObjectiveC2_InstanceVariable.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public ObjectiveC2_InstanceVariable(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._state = state;

	if (state.is32bit) {
		offset = reader.readNextInt() & Conv.INT_MASK;
	}
	else {
		offset = reader.readNextLong();
	}

	long nameIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
	if (nameIndex > 0 && reader.isValidIndex(nameIndex)) {
		name      = reader.readAsciiString( nameIndex );
	}

	long typeIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
	if (typeIndex > 0 && reader.isValidIndex(typeIndex)) {
		type      = reader.readAsciiString( typeIndex );
	}

	alignment  = reader.readNextInt();
	size       = reader.readNextInt();
}
 
Example 5
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 6
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 7
Source File: DWARFUtil.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Read an offset value who's size depends on the DWARF format: 32 vs 64.
 * <p>
 * @param reader BinaryReader pointing to the value to read
 * @param dwarfFormat - See {@link DWARFCompilationUnit#DWARF_32} and {@link DWARFCompilationUnit#DWARF_64}.
 * @return the offset value
 * @throws IOException if an I/O error occurs or bad dwarfFormat value
 */
public static long readOffsetByDWARFformat(BinaryReader reader, int dwarfFormat)
		throws IOException {
	switch (dwarfFormat) {
		case DWARFCompilationUnit.DWARF_32:
			return reader.readNextUnsignedInt();
		case DWARFCompilationUnit.DWARF_64:
			return reader.readNextLong();
	}
	throw new IOException("Unknown DWARF Format Value: " + dwarfFormat);
}
 
Example 8
Source File: ObjectiveC2_ProtocolList.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ObjectiveC2_ProtocolList(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._state = state;
	this._index = reader.getPointerIndex();

	long count = state.is32bit ? reader.readNextInt() & Conv.INT_MASK : reader.readNextLong();

	for (long i = 0 ; i < count ; ++i) {
		long protocolIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
		long originalIndex = reader.getPointerIndex();
		reader.setPointerIndex(protocolIndex);
		protocols.add( new ObjectiveC2_Protocol(state, reader) );
		reader.setPointerIndex(originalIndex);
	}
}
 
Example 9
Source File: DyldCacheSlideInfo3.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link DyldCacheSlideInfo3}.
 * 
 * @param reader A {@link BinaryReader} positioned at the start of a DYLD slide info 3
 * @throws IOException if there was an IO-related problem creating the DYLD slide info 3
 */
public DyldCacheSlideInfo3(BinaryReader reader) throws IOException {
	super(reader);
	page_size = reader.readNextInt();
	page_starts_count = reader.readNextInt();
	auth_value_add = reader.readNextLong();
	page_starts = reader.readNextShortArray(page_starts_count);
}
 
Example 10
Source File: DyldCacheImageTextInfo.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link DyldCacheImageTextInfo}.
 * 
 * @param reader A {@link BinaryReader} positioned at the start of a DYLD image text info
 * @throws IOException if there was an IO-related problem creating the DYLD image text info
 */
public DyldCacheImageTextInfo(BinaryReader reader) throws IOException {
	uuid = reader.readNextByteArray(16);
	loadAddress = reader.readNextLong();
	textSegmentSize = reader.readNextInt();
	pathOffset = reader.readNextInt();

	path = reader.readAsciiString(pathOffset);
}
 
Example 11
Source File: ObjectiveC2_MessageReference.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ObjectiveC2_MessageReference(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._state = state;

	if (state.is32bit) {
		implementation = reader.readNextInt() & Conv.INT_MASK;
	}
	else {
		implementation = reader.readNextLong();
	}

	long selectorIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
	if (selectorIndex != 0) {
		selector = reader.readAsciiString(selectorIndex);
	}
}
 
Example 12
Source File: DyldCacheSlideInfo2.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link DyldCacheSlideInfo2}.
 * 
 * @param reader A {@link BinaryReader} positioned at the start of a DYLD slide info 2
 * @throws IOException if there was an IO-related problem creating the DYLD slide info 2
 */
public DyldCacheSlideInfo2(BinaryReader reader) throws IOException {
	super(reader);
	page_size = reader.readNextInt();
	page_starts_offset = reader.readNextInt();
	page_starts_count = reader.readNextInt();
	page_extras_offset = reader.readNextInt();
	page_extras_count = reader.readNextInt();
	delta_mask = reader.readNextLong();
	value_add = reader.readNextLong();
	page_starts_entries = reader.readNextShortArray(page_starts_count);
	page_extras_entries = reader.readNextShortArray(page_extras_count);
}
 
Example 13
Source File: Ext4Mmp.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Ext4Mmp(BinaryReader reader) throws IOException {
	mmp_magic = reader.readNextInt();
	mmp_seq = reader.readNextInt();
	mmp_time = reader.readNextLong();
	mmp_nodename = reader.readNextByteArray(64);
	mmp_bdevname = reader.readNextByteArray(32);
	mmp_check_interval = reader.readNextShort();
	mmp_pad1 = reader.readNextShort();
	mmp_pad2 = reader.readNextIntArray(226);
	mmp_checksum = reader.readNextInt();
}
 
Example 14
Source File: ObjectiveC1_Utilities.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Reads the next index value. If is32bit is true, then 4 bytes
 * will be read to form index. Otherwise, 8 bytes will be read to form index.
 */
public static long readNextIndex(BinaryReader reader, boolean is32bit) throws IOException {
	if (is32bit) {
		return reader.readNextInt() & Conv.INT_MASK;
	}
	return reader.readNextLong();
}
 
Example 15
Source File: KIP1Header.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
private void readHeader(BinaryReader reader)
{
    try 
    {
        this.magic = reader.readNextAsciiString(4);
        
        if (!this.magic.equals("KIP1"))
            throw new InvalidMagicException("KIP1");
        
        this.name = reader.readNextAsciiString(12);
        this.tid = reader.readNextLong();
        this.processCategory = reader.readNextInt();
        this.mainThreadPriority = reader.readNextByte();
        this.defaultCpuCore = reader.readNextByte();
        reader.readNextByte(); // Unused
        this.flags = reader.readNextByte();
        
        for (int i = 0; i < this.sectionHeaders.length; i++)
        {
            this.sectionHeaders[i] = new KIP1SectionHeader(reader);
        }
    } 
    catch (IOException e) 
    {
        Msg.error(this, "Failed to read KIP1 header");
    }
}
 
Example 16
Source File: DWARFCompilationUnit.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new {@link DWARFCompilationUnit} by reading a compilationUnit's header data
 * from the debug_info section and the debug_abbr section and its compileUnit DIE (ie.
 * the first DIE right after the header).
 * <p>
 * Returns NULL if there was an ignorable error while reading the compilation unit (and
 * leaves the input stream at the next compilation unit to read), otherwise throws
 * an IOException if there was an unrecoverable error.
 *
 * @param dwarfProgram the dwarf program.
 * @param debugInfoBR the debug info binary reader.
 * @param debugAbbrBR the debug abbreviation binary reader
 * @param cuNumber the compilation unit number
 * @param monitor the current task monitor
 * @return the read compilation unit.
 * @throws DWARFException if an invalid or unsupported DWARF version is read.
 * @throws IOException if the length of the compilation unit is invalid.
 * @throws CancelledException if the task has been canceled.
 */
public static DWARFCompilationUnit readCompilationUnit(DWARFProgram dwarfProgram,
		BinaryReader debugInfoBR, BinaryReader debugAbbrBR, int cuNumber, TaskMonitor monitor)
		throws DWARFException, IOException, CancelledException {

	long startOffset = debugInfoBR.getPointerIndex();
	long length = debugInfoBR.readNextUnsignedInt();
	int format;

	if (length == 0xffffffffL) {
		// Length of 0xffffffff implies 64-bit DWARF format
		// Mostly untested as there is no easy way to force the compiler
		// to generate this
		length = debugInfoBR.readNextLong();
		format = DWARF_64;
	}
	else if (length >= 0xfffffff0L) {
		// Length of 0xfffffff0 or greater is reserved for DWARF
		throw new DWARFException("Reserved DWARF length value: " + Long.toHexString(length) +
			". Unknown extension.");
	}
	else if (length == 0) {
		throw new DWARFException("Invalid length 0 for DWARF Compilation Unit at 0x" +
			Long.toHexString(startOffset));
	}
	else {
		format = DWARF_32;
	}

	long endOffset = (debugInfoBR.getPointerIndex() + length);
	short version = debugInfoBR.readNextShort();
	long abbreviationOffset = DWARFUtil.readOffsetByDWARFformat(debugInfoBR, format);
	byte pointerSize = debugInfoBR.readNextByte();
	long firstDIEOffset = debugInfoBR.getPointerIndex();

	if (version < 2 || version > 4) {
		throw new DWARFException(
			"Only DWARF version 2, 3, or 4 information is currently supported.");
	}
	if (firstDIEOffset > endOffset) {
		throw new IOException("Invalid length " + (endOffset - startOffset) +
			" for DWARF Compilation Unit at 0x" + Long.toHexString(startOffset));
	}
	else if (firstDIEOffset == endOffset) {
		// silently skip this empty compunit
		return null;
	}

	debugAbbrBR.setPointerIndex(abbreviationOffset);
	Map<Integer, DWARFAbbreviation> abbrMap =
		DWARFAbbreviation.readAbbreviations(debugAbbrBR, dwarfProgram, monitor);

	DWARFCompilationUnit cu =
		new DWARFCompilationUnit(dwarfProgram, startOffset, endOffset, length, format, version,
			abbreviationOffset, pointerSize, cuNumber, firstDIEOffset, abbrMap);

	try {
		DebugInfoEntry compileUnitDIE =
			DebugInfoEntry.read(debugInfoBR, cu, dwarfProgram.getAttributeFactory());

		DWARFCompileUnit compUnit = DWARFCompileUnit.read(
			DIEAggregate.createSingle(compileUnitDIE), dwarfProgram.getDebugLine());
		cu.setCompileUnit(compUnit);
		return cu;
	}
	catch (IOException ioe) {
		Msg.error(null,
			"Failed to parse the DW_TAG_compile_unit DIE at the start of compilation unit " +
				cuNumber + " at offset " + startOffset + " (0x" +
				Long.toHexString(startOffset) + "), skipping entire compilation unit",
			ioe);
		debugInfoBR.setPointerIndex(cu.getEndOffset());
		return null;
	}
}
 
Example 17
Source File: LoadConfigDirectory.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private long readPointer(BinaryReader reader) throws IOException {
	if (is64bit) {
		return reader.readNextLong();
	}
	return reader.readNextInt() & Conv.INT_MASK;
}
 
Example 18
Source File: ExceptionStateX86_64.java    From ghidra with Apache License 2.0 4 votes vote down vote up
ExceptionStateX86_64(BinaryReader reader) throws IOException {
	trapno = reader.readNextInt();
	err = reader.readNextInt();
	faultvaddr = reader.readNextLong();
}
 
Example 19
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 20
Source File: DyldCacheRangeEntry.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new {@link DyldCacheRangeEntry}.
 * 
 * @param reader A {@link BinaryReader} positioned at the start of a DYLD range entry
 * @throws IOException if there was an IO-related problem creating the DYLD range entry
 */
public DyldCacheRangeEntry(BinaryReader reader) throws IOException {
	startAddress = reader.readNextLong();
	size = reader.readNextInt();
	imageIndex = reader.readNextInt();
}