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

The following examples show how to use ghidra.app.util.bin.BinaryReader#peekNextByte() . 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: ISO9660FileSystem.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void readWhileZero(BinaryReader reader, long endIndex) throws IOException {
	while (reader.peekNextByte() == 0) {

		//keep reading if all zeros until non zero is met or
		//end index reached
		if (reader.getPointerIndex() < endIndex) {
			reader.readNextByte();
		}
		else {
			break;
		}
	}
}
 
Example 2
Source File: OmfLibraryRecord.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public static OmfLibraryRecord parse(BinaryReader reader,TaskMonitor monitor) throws IOException {
	OmfLibraryRecord res = null;
	byte type = reader.peekNextByte();
	if (type != (byte)0xF0)
		throw new IOException("Not an OMF Library record");
	res = new OmfLibraryRecord(reader);
	res.members = new ArrayList<MemberHeader>();		
	reader.align(res.pageSize);		// Skip padding to get to next page boundary
	type = reader.peekNextByte();
	while(type != (byte)0xF1) {		// Until we see the official "end of library" record
		MemberHeader curheader = new MemberHeader();
		curheader.payloadOffset = reader.getPointerIndex();
		OmfFileHeader fileheader;
		try {
			fileheader = OmfFileHeader.scan(reader, monitor,false);
		} catch (OmfException e) {
			throw new IOException("Could not parse individual object file within archive");
		}
		curheader.name = fileheader.getLibraryModuleName();		// (preferred) name of the object module
		if (curheader.name == null)
			curheader.name = fileheader.getName();				// As a back-up, this is usually the name of the original source
		curheader.machineName = fileheader.getMachineName();
		curheader.translator = fileheader.getTranslator();
		curheader.size = (int)(reader.getPointerIndex() - curheader.payloadOffset);
		res.members.add(curheader);			
		reader.align(res.pageSize);		// Skip padding to get to next page boundary
		type = reader.peekNextByte();
	}
	return res;
}
 
Example 3
Source File: CliAbstractSig.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliTypeBase(BinaryReader reader, boolean isRetType)
		throws IOException, InvalidInputException {
	this.isVoidAllowed = isRetType;
	while (CliCustomMod.isCustomMod(reader)) {
		customMods.add(new CliCustomMod(reader));
	}
	byte nextByte = reader.peekNextByte();
	if (nextByte == CliElementType.ELEMENT_TYPE_BYREF.id()) {
		byRef = true;
		reader.readNextByte();
	}
	type = readCliType(reader);
}
 
Example 4
Source File: CliBlob.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static int decodeCompressedInt(BinaryReader reader, boolean signed) throws IOException {
	byte firstByte = reader.peekNextByte();
	boolean isLittleEndian = reader.isLittleEndian();
	reader.setLittleEndian(false);
	int numBytes = getNumberBytesInCodedInt(firstByte);
	int decodedSize = 0;
	switch (numBytes) {
		case 1:
			byte codedByte = reader.readNextByte();
			if (signed)
				decodedSize = decodeCompressedSigned(codedByte);
			else
				decodedSize = decodeCompressedUnsigned(codedByte);
			break;
			
		case 2:
			short codedShort = reader.readNextShort();
			if (signed)
				decodedSize = decodeCompressedSigned(codedShort);
			else
				decodedSize = decodeCompressedUnsigned(codedShort);
			break;
			
		case 4:
			int codedInt = reader.readNextInt();
			if (signed)
				decodedSize = decodeCompressedSigned(codedInt);
			else
				decodedSize = decodeCompressedUnsigned(codedInt);
			break;
			
		default:
			break;
	}
	reader.setLittleEndian(isLittleEndian);
	return decodedSize;
}
 
Example 5
Source File: ConstantPoolFactory.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static AbstractConstantPoolInfoJava get(BinaryReader reader) throws IOException {

		switch (reader.peekNextByte()) {

			case ConstantPoolTagsJava.CONSTANT_Class:
				return new ConstantPoolClassInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Double:
				return new ConstantPoolDoubleInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Fieldref:
				return new ConstantPoolFieldReferenceInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Float:
				return new ConstantPoolFloatInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Integer:
				return new ConstantPoolIntegerInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_InterfaceMethodref:
				return new ConstantPoolInterfaceMethodReferenceInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_InvokeDynamic:
				return new ConstantPoolInvokeDynamicInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Long:
				return new ConstantPoolLongInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_MethodHandle:
				return new ConstantPoolMethodHandleInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Methodref:
				return new ConstantPoolMethodReferenceInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_MethodType:
				return new ConstantPoolMethodTypeInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_NameAndType:
				return new ConstantPoolNameAndTypeInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_String:
				return new ConstantPoolStringInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Utf8:
				return new ConstantPoolUtf8Info(reader);

			case ConstantPoolTagsJava.CONSTANT_Dynamic:
				return new ConstantPoolDynamicInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Module:
				return new ConstantPoolModuleInfo(reader);

			case ConstantPoolTagsJava.CONSTANT_Package:
				return new ConstantPoolPackageInfo(reader);

			case 0:
				return null;

			default:
				throw new IllegalArgumentException(
					"Unsupport Constant Pool Entry Type: " + reader.peekNextByte());
		}
	}
 
Example 6
Source File: ISO9660FileSystem.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private ArrayList<ISO9660Directory> createDirectoryList(BinaryReader reader,
		ISO9660Directory parentDir, long blockSize, TaskMonitor monitor) throws IOException {

	ArrayList<ISO9660Directory> directoryList = new ArrayList<>();
	ISO9660Directory childDir = null;
	long dirIndex;
	long endIndex;

	//Get location from parent into child directory
	dirIndex = parentDir.getLocationOfExtentLE() * blockSize;
	endIndex = dirIndex + parentDir.getDataLengthLE();

	//while there is still more data in the current directory level
	while (dirIndex < endIndex) {
		reader.setPointerIndex(dirIndex);

		//If the next byte is not zero then create the directory
		if (reader.peekNextByte() != 0) {
			if (!lookedAtRoot) {
				childDir = new ISO9660Directory(reader);
				addAndStoreDirectory(monitor, directoryList, childDir);

			}

			//Root level has already been looked at
			else {
				if (parentDir.getName() != null) {
					childDir = new ISO9660Directory(reader, parentDir);
					addAndStoreDirectory(monitor, directoryList, childDir);
				}
			}
		}

		//Otherwise there is a gap in the data so keep looking forward
		//while still under the end index and create directory when data is
		//reached
		else {
			readWhileZero(reader, endIndex);

			//Create the data once the reader finds the next position
			//and not reached end index
			if (reader.getPointerIndex() < endIndex) {
				if (!lookedAtRoot) {
					childDir = new ISO9660Directory(reader);
					addAndStoreDirectory(monitor, directoryList, childDir);
					dirIndex = childDir.getVolumeIndex();
				}
				else {
					if (parentDir.getName() != null) {
						childDir = new ISO9660Directory(reader, parentDir);
						addAndStoreDirectory(monitor, directoryList, childDir);
						dirIndex = childDir.getVolumeIndex();
					}
				}
			}

		}
		dirIndex += childDir.getDirectoryRecordLength();
	}

	lookedAtRoot = true;
	return directoryList;
}
 
Example 7
Source File: OmfRecord.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static OmfRecord readRecord(BinaryReader reader) throws IOException, OmfException {
	OmfRecord res = null;
	byte type = reader.peekNextByte();
	type &= 0xfe;	// Mask off the least significant bit
	switch(type) {
	case THEADR:
	case LHEADR:
		res = new OmfFileHeader(reader);
		break;
	case COMENT:
		res = new OmfCommentRecord(reader);
		break;
	case MODEND:
		res = new OmfModuleEnd(reader);
		break;
	case EXTDEF:
		res = new OmfExternalSymbol(reader,false);
		break;
	case PUBDEF:
		res = new OmfSymbolRecord(reader,false);
		break;
	case LINNUM:
		res = new OmfLineNumberRecord(reader);
		break;
	case LNAMES:
		res = new OmfNamesRecord(reader);
		break;
	case SEGDEF:
		res = new OmfSegmentHeader(reader);
		break;
	case GRPDEF:
		res = new OmfGroupRecord(reader);
		break;
	case FIXUPP:
		res = new OmfFixupRecord(reader);
		break;
	case LEDATA:
		res = new OmfEnumeratedData(reader);
		break;
	case LIDATA:
		res = new OmfIteratedData(reader);
		break;
	case COMDEF:
		res = new OmfComdefRecord(reader,false);
		break;
	case LEXTDEF:
		res = new OmfExternalSymbol(reader,true);
		break;
	case LPUBDEF:
		res = new OmfSymbolRecord(reader,true);
		break;
	case LCOMDEF:
		res = new OmfComdefRecord(reader,true);
		break;
	default:
		throw new OmfException("Unrecognized record type");
	}
	return res;
}
 
Example 8
Source File: CliAbstractSig.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static boolean isCustomMod(BinaryReader reader) throws IOException {
	return (reader.peekNextByte() == CliElementType.ELEMENT_TYPE_CMOD_OPT.id() ||
		reader.peekNextByte() == CliElementType.ELEMENT_TYPE_CMOD_REQD.id());
}
 
Example 9
Source File: CliAbstractSig.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public static boolean isConstraint(BinaryReader reader) throws IOException {
	return (reader.peekNextByte() == CliElementType.ELEMENT_TYPE_PINNED.id());
}