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

The following examples show how to use ghidra.app.util.bin.BinaryReader#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: Ext4DxRoot.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public Ext4DxRoot(BinaryReader reader) throws IOException {
	dot_inode = reader.readNextInt();
	dot_rec_len = reader.readNextShort();
	dot_name_len = reader.readNextByte();
	dot_file_type = reader.readNextByte();
	dot_name = reader.readNextByteArray(4);
	dotdot_inode = reader.readNextInt();
	dotdot_rec_len = reader.readNextShort();
	dotdot_name_len = reader.readNextByte();
	dotdot_file_type = reader.readNextByte();
	dotdot_name = reader.readNextByteArray(4);
	dx_root_info_reserved_zero = reader.readNextInt();
	dx_root_info_hash_version = reader.readNextByte();
	dx_root_info_info_length = reader.readNextByte();
	dx_root_info_indirect_levels = reader.readNextByte();
	dx_root_info_unused_flags = reader.readNextByte(); 	
	limit = reader.readNextShort();
	count = reader.readNextShort();
	block = reader.readNextInt();
	entries = new Ext4DxEntry[count];
	for( int i = 0; i < count; i++ ) {
		entries[i] = new Ext4DxEntry(reader);
	}
}
 
Example 2
Source File: OmfComdefRecord.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static int readCommunalLength(BinaryReader reader) throws OmfException, IOException {
	int val = reader.readNextByte() & 0xff;
	if (val <= 128)
		return val;
	if (val == 0x81) {
		val = reader.readNextShort() & 0xffff;
	}
	else if (val == 0x84) {
		val = reader.readNextShort() & 0xffff;
		int hithird = reader.readNextByte() & 0xff;
		val += (hithird << 16);
	}
	else if (val == 0x88) {
		val = reader.readNextInt();
	}
	else
		throw new OmfException("Illegal communal length encoding");
	return val;
}
 
Example 3
Source File: CliSigMethodSpec.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public CliSigMethodSpec(CliBlob blob) throws IOException {
	super(blob);

	BinaryReader reader = getContentsReader();
	firstByte = reader.readNextByte();
	if (firstByte != identifier)
		return; // TODO: Do something worse?
	long origIndex = reader.getPointerIndex();
	genArgCount = decodeCompressedUnsignedInt(reader);
	genArgCountBytes = (int) (reader.getPointerIndex() - origIndex);
	types = new CliSigType[genArgCount];
	for (int i = 0; i < genArgCount; i++) {
		try {
			types[i] = readCliType(reader);
		}
		catch (InvalidInputException e) {
			types[i] = null;
		}
	}
}
 
Example 4
Source File: ISO9660PathTable.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public ISO9660PathTable(BinaryReader reader, boolean littleEndian) throws IOException {
	reader.setLittleEndian(littleEndian);
	this.littleEndian = littleEndian;
	volumeIndex = reader.getPointerIndex();
	dirIdentifierLength = reader.readNextByte();
	extendedAttributeRecordLength = reader.readNextByte();
	locationOfExtent = reader.readNextInt();
	directoryNumberPathIndex = reader.readNextShort();
	directoryIdentifier = reader.readNextByteArray(dirIdentifierLength);

	//The padding field is only present if the directoryIdentifierLength
	//is odd, otherwise it is not used.
	if (dirIdentifierLength % 2 != 0) {
		paddingField = reader.readNextByte();
		paddingFieldPresent = true;
	}
	else {
		paddingFieldPresent = false;
	}
}
 
Example 5
Source File: BinaryPropertyListHeader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public BinaryPropertyListHeader( BinaryReader reader ) throws IOException {

		if ( reader.isLittleEndian( ) ) {
			throw new IOException( "BinaryReader must be BIG endian!" );
		}

		magic = reader.readNextAsciiString( BinaryPropertyListConstants.BINARY_PLIST_MAGIC.length( ) );

		if ( !BinaryPropertyListConstants.BINARY_PLIST_MAGIC.equals( magic ) ) {
			throw new IOException( "Not a valid binary PLIST" );
		}

		majorVersion = reader.readNextByte( );
		minorVersion = reader.readNextByte( );

		if ( majorVersion != BinaryPropertyListConstants.MAJOR_VERSION_0 ) {
			throw new IOException( "Unsupported binary PLIST version: " + majorVersion + "." + minorVersion );
		}

		trailer = new BinaryPropertyListTrailer( reader );
	}
 
Example 6
Source File: NSObjectParser.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private static int parseLength( BinaryReader reader, int objectInfo ) throws IOException {
	int length = objectInfo;
	if ( objectInfo == 0xf ) {// longer than 0xf bytes...
		int offset = reader.readNextByte( ) & 0xff;
		if ( offset == 0x10 ) {
			length = reader.readNextByte( ) & 0xff;
		}
		else if ( offset == 0x11 ) {
			length = reader.readNextShort( ) & 0xffff;
		}
		else {
			throw new RuntimeException( );
		}
	}
	return length;
}
 
Example 7
Source File: AnnotationElementValue.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public AnnotationElementValue(BinaryReader reader) throws IOException {
	tag = reader.readNextByte();

	if (tag == DescriptorDecoder.BASE_TYPE_BYTE || tag == DescriptorDecoder.BASE_TYPE_CHAR ||
		tag == DescriptorDecoder.BASE_TYPE_INT || tag == DescriptorDecoder.BASE_TYPE_SHORT ||
		tag == DescriptorDecoder.BASE_TYPE_LONG || tag == DescriptorDecoder.BASE_TYPE_FLOAT ||
		tag == DescriptorDecoder.BASE_TYPE_DOUBLE ||
		tag == DescriptorDecoder.BASE_TYPE_BOOLEAN ||
		tag == DescriptorDecoder.BASE_TYPE_STRING) {

		constantValueIndex = reader.readNextShort();
	}
	else if (tag == DescriptorDecoder.BASE_TYPE_ENUM) {
		typeNameIndex = reader.readNextShort();
		constantNameIndex = reader.readNextShort();
	}
	else if (tag == DescriptorDecoder.BASE_TYPE_CLASS) {
		classInfoIndex = reader.readNextShort();
	}
	else if (tag == DescriptorDecoder.BASE_TYPE_ANNOTATION) {
		annotation = new AnnotationJava(reader);
	}
	else if (tag == DescriptorDecoder.BASE_TYPE_ARRAY) {
		numberOfValues = reader.readNextShort();
		values = new AnnotationElementValue[numberOfValues & 0xffff];
		for (int i = 0; i < (numberOfValues & 0xffff); ++i) {
			values[i] = new AnnotationElementValue(reader);
		}
	}
}
 
Example 8
Source File: Ext4DirEntryTail.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Ext4DirEntryTail(BinaryReader reader) throws IOException {
	det_reserved_zero1 = reader.readNextInt();
	det_rec_len = reader.readNextShort();
	det_reserved_zero2 = reader.readNextByte();
	det_reserved_ft = reader.readNextByte();
	det_checksum = reader.readNextInt();
}
 
Example 9
Source File: Ext4DirEntry2.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Ext4DirEntry2( BinaryReader reader ) throws IOException {
	inode = reader.readNextInt( );
	rec_len = reader.readNextShort( );
	name_len = reader.readNextByte( );
	file_type = reader.readNextByte( );
	name = reader.readNextAsciiString( name_len & 0xff );
	
	int extraSize = ( rec_len & 0xffff ) - ( 8 + ( name_len & 0xff ) );
	if ( extraSize > 0 ) {
		extra = reader.readNextByteArray( extraSize );
	}
}
 
Example 10
Source File: Ext4DxNode.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Ext4DxNode(BinaryReader reader) throws IOException {
	fake_inode = reader.readNextInt();
	fake_rec_len = reader.readNextShort();
	name_len = reader.readNextByte();
	file_type = reader.readNextByte();
	limit = reader.readNextShort();
	count = reader.readNextShort();
	block = reader.readNextInt();
	entries = new Ext4DxEntry[count];
	for( int i = 0; i < count; i++ ) {
		entries[i] = new Ext4DxEntry(reader);
	}
}
 
Example 11
Source File: OmfLibraryRecord.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public OmfLibraryRecord(BinaryReader reader) throws IOException {
	readRecordHeader(reader);
	pageSize = recordLength + 3;
	dictionaryOffset = reader.readNextInt() & 0xffffffff;
	dictionarySize = reader.readNextShort() & 0xffff;
	flags = reader.readNextByte();
	// No checksum byte  (just padding)
}
 
Example 12
Source File: StatementProgramPrologue.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public StatementProgramPrologue(BinaryReader reader) throws IOException {
	totalLength                = reader.readNextInt();
	version                    = reader.readNextShort();

	if (version != 2) {
		throw new IllegalStateException("Only DWARF v2 is supported.");
	}

	prologueLength             = reader.readNextInt();
	minimumInstructionLength   = reader.readNextByte();
	defaultIsStatement         = reader.readNextByte() != 0;
	lineBase                   = reader.readNextByte();
	lineRange                  = reader.readNextByte();
	opcodeBase                 = reader.readNextByte();
	standardOpcodeLengths      = reader.readNextByteArray(opcodeBase - 1);

	while (true) {
		String dir = reader.readNextAsciiString();
		if (dir.length() == 0) {
			break;
		}
		includeDirectories.add(dir);
	}

	while (true) {
		FileEntry entry = new FileEntry(reader);
		if (entry.getFileName().length() == 0) {
			break;
		}
		fileNames.add(entry);
	}
}
 
Example 13
Source File: OmfFixupRecord.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static ThreadSubrecord readThreadSubrecord(BinaryReader reader,boolean hasBigFields) throws IOException {
	ThreadSubrecord thread = new ThreadSubrecord();
	thread.type = reader.readNextByte();
	int method = thread.getMethod();
	if (method < 4)
		thread.index = OmfRecord.readInt1Or2(reader, hasBigFields);
	else
		thread.index = -1;
	return thread;
}
 
Example 14
Source File: LEB128.java    From ghidra with Apache License 2.0 5 votes vote down vote up
LEB128(BinaryReader reader, boolean isSigned) throws IOException {
	if (isSigned) {
		throw new UnsupportedOperationException();
	}
	int shift = 0;
	while (true) {
		int nextByte = reader.readNextByte() & 0xff;
		value |= ((nextByte & 0x7f) << shift);
		if ((nextByte & 0x80) == 0) {
			break;
		}
		shift += 7;
	}
}
 
Example 15
Source File: OmfComdefRecord.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public OmfComdefRecord(BinaryReader reader,boolean isStatic) throws IOException, OmfException {
	super(isStatic);
	readRecordHeader(reader);
	long max = reader.getPointerIndex() + getRecordLength() - 1;

	ArrayList<OmfSymbol> symbollist = new ArrayList<OmfSymbol>();
	while(reader.getPointerIndex() < max) {
		String name = OmfRecord.readString(reader);
		int typeIndex = OmfRecord.readIndex(reader);
		byte dataType = reader.readNextByte();
		int byteLength=0;
		if (dataType == 0x61) {		// FAR data, reads numElements and elSize
			int numElements = readCommunalLength(reader);
			int elSize = readCommunalLength(reader);
			byteLength = numElements * elSize;
		}
		else {
			// Values 1 thru 5f plus 61, read the byte length
			byteLength = readCommunalLength(reader);
		}
		OmfSymbol sym = new OmfSymbol(name,typeIndex,0,dataType,byteLength);
		symbollist.add(sym);
	}
	readCheckSumByte(reader);
	symbol = new OmfSymbol[symbollist.size()];
	symbollist.toArray(symbol);
}
 
Example 16
Source File: OmfRecord.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public void readRecordHeader(BinaryReader reader) throws IOException {
	recordType = reader.readNextByte();
	recordLength = reader.readNextShort() & 0xffff;
}
 
Example 17
Source File: ISO9660VolumeDescriptor.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public ISO9660VolumeDescriptor(BinaryReader reader) throws IOException {

		super(reader);
		unused = reader.readNextByte();
		systemIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_32);
		volumeIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_32);
		unused2 = reader.readNextLong();
		volumeSpaceSizeLE = reader.readNextInt();
		volumeSpaceSizeBE = readIntBigEndian(reader);
		unused3 = reader.readNextByteArray(ISO9660Constants.UNUSED_SPACER_LEN_32);
		volumeSetSizeLE = reader.readNextShort();
		volumeSetSizeBE = readShortBigEndian(reader);
		volumeSeqNumberLE = reader.readNextShort();
		volumeSeqNumberBE = readShortBigEndian(reader);
		logicalBlockSizeLE = reader.readNextShort();
		logicalBlockSizeBE = readShortBigEndian(reader);
		pathTableSizeLE = reader.readNextInt();
		pathTableSizeBE = readIntBigEndian(reader);
		typeLPathTableLocation = reader.readNextInt();
		optionalTypeLPathTableLocation = reader.readNextInt();
		typeMPathTableLocation = readIntBigEndian(reader);
		optionalTypeMPathTableLocation = readIntBigEndian(reader);
		directoryEntry = new ISO9660Directory(reader);
		volumeSetIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_128);
		publisherIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_128);
		dataPreparerIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_128);
		applicationIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_128);
		copyrightFileIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_38);
		abstractFileIdentifier = reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_36);
		bibliographicFileIdentifier =
			reader.readNextByteArray(ISO9660Constants.IDENTIFIER_LENGTH_37);
		volumeCreationDateTime = reader.readNextByteArray(ISO9660Constants.DATE_TIME_LENGTH_17);
		volumeModifyDateTime = reader.readNextByteArray(ISO9660Constants.DATE_TIME_LENGTH_17);
		volumeExpirationDateTime = reader.readNextByteArray(ISO9660Constants.DATE_TIME_LENGTH_17);
		volumeEffectiveDateTime = reader.readNextByteArray(ISO9660Constants.DATE_TIME_LENGTH_17);
		fileStructureVersion = reader.readNextByte();
		unused4 = reader.readNextByte();
		applicationUsed = reader.readNextByteArray(ISO9660Constants.UNUSED_SPACER_LEN_512);
		reserved = reader.readNextByteArray(ISO9660Constants.RESERVED_SIZE);

	}
 
Example 18
Source File: OmfRecord.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static int readInt1Or2(BinaryReader reader,boolean isBig) throws IOException {
	if (isBig)
		return (reader.readNextShort()  & 0xffff);
	return (reader.readNextByte() & 0xff);
}
 
Example 19
Source File: AbstractConstantPoolInfoJava.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected AbstractConstantPoolInfoJava(BinaryReader reader) throws IOException {
	_offset = reader.getPointerIndex();

	tag = reader.readNextByte();
}
 
Example 20
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;
	}
}