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

The following examples show how to use ghidra.app.util.bin.BinaryReader#getPointerIndex() . 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: OmfExternalSymbol.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public OmfExternalSymbol(BinaryReader reader,boolean isStatic) throws IOException {
	this.isStatic = 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 type = OmfRecord.readIndex(reader);
		OmfSymbol subrec = new OmfSymbol(name,type,0,0,0);
		symbollist.add(subrec);
	}
	
	readCheckSumByte(reader);
	symbol = new OmfSymbol[symbollist.size()];
	symbollist.toArray(symbol);
}
 
Example 2
Source File: ObjectiveC2_Class.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void readData(BinaryReader reader) throws IOException {
	long index = 0;
	try {
		index = ObjectiveC1_Utilities.readNextIndex(reader, _state.is32bit);
	}
	catch (IOException ioe) {
		//Trying to read uninitialized memory
		return;
	}
	if (index != 0 && reader.isValidIndex(index)) {
		long originalIndex = reader.getPointerIndex();
		reader.setPointerIndex(index);
		data = new ObjectiveC2_ClassRW(_state, reader);
		reader.setPointerIndex(originalIndex);
	}
}
 
Example 3
Source File: StringDataItem.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public StringDataItem( StringIDItem stringItem, BinaryReader reader ) throws IOException {
	long oldIndex = reader.getPointerIndex( );
	try {
		reader.setPointerIndex( stringItem.getStringDataOffset( ) );
		stringLength = Leb128.readUnsignedLeb128( reader.readByteArray( stringItem.getStringDataOffset( ), 5 ) );

		lebLength = Leb128.unsignedLeb128Size( stringLength );

		reader.readNextByteArray( lebLength );// consume leb...

		actualLength = computeActualLength( reader );

		byte [] stringBytes = reader.readNextByteArray( actualLength );

		ByteArrayInputStream in = new ByteArrayInputStream( stringBytes );

		char [] out = new char[ stringLength ];

		string = ModifiedUTF8.decode( in, out );
	}
	finally {
		reader.setPointerIndex( oldIndex );
	}
}
 
Example 4
Source File: CliBlobMarshalSpec.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public CliBlobMarshalSpec(CliBlob blob) throws IOException {
	super(blob);

	BinaryReader reader = blob.getContentsReader();
	nativeIntrinsic = CliNativeType.fromInt(reader.readNextByte());
	if (nativeIntrinsic == CliNativeType.NATIVE_TYPE_ARRAY ||
		nativeIntrinsic == CliNativeType.NATIVE_TYPE_FIXEDARRAY) {
		arrayElemType = CliNativeType.fromInt(reader.readNextByte());
		// There is no sentinel other than blob size that indicates whether 0, 1, or 2 compressed unsigned ints follow
		if (contentsSize > 2) {
			long origIndex = reader.getPointerIndex();
			paramNum = decodeCompressedUnsignedInt(reader);
			paramNumBytes = (int) (reader.getPointerIndex() - origIndex);
			if (contentsSize > (2 + paramNumBytes)) {
				origIndex = reader.getPointerIndex();
				numElem = decodeCompressedUnsignedInt(reader);
				numElemBytes = (int) (reader.getPointerIndex() - origIndex);
			}
		}
	}
}
 
Example 5
Source File: DyldCacheLocalSymbolsInfo.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new {@link DyldCacheLocalSymbolsInfo}.
 * 
 * @param reader A {@link BinaryReader} positioned at the start of a DYLD local symbols info
 * @param architecture The {@link DyldArchitecture}
 * @throws IOException if there was an IO-related problem creating the DYLD local symbols info
 */
public DyldCacheLocalSymbolsInfo(BinaryReader reader, DyldArchitecture architecture)
		throws IOException {
	this.reader = reader;
	this.startIndex = reader.getPointerIndex();

	nlistOffset = reader.readNextInt();
	nlistCount = reader.readNextInt();
	stringsOffset = reader.readNextInt();
	stringsSize = reader.readNextInt();
	entriesOffset = reader.readNextInt();
	entriesCount = reader.readNextInt();

	nlistList = new ArrayList<>(nlistCount);
	localSymbolsEntryList = new ArrayList<>(entriesCount);

	is32bit = !(architecture.getCpuType() == CpuTypes.CPU_TYPE_ARM_64 ||
		architecture.getCpuType() == CpuTypes.CPU_TYPE_X86_64);
}
 
Example 6
Source File: ObjectiveC2_Class.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public ObjectiveC2_Class(ObjectiveC2_State state, BinaryReader reader) {
	this._state = state;
	this._index = reader.getPointerIndex();

	state.classIndexMap.put(_index, this);

	try {
		readISA(reader);
		readSuperClass(reader);
		readCache(reader);
		readVTable(reader);
		readData(reader);
	}
	catch (IOException ioe) {
		// Couldn't read something, usually a metaclass pointing to an uninitialized section since
		// runtime 2.0 got rid of the metaclass type.
	}
}
 
Example 7
Source File: OmfSymbolRecord.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public OmfSymbolRecord(BinaryReader reader,boolean isStatic) throws IOException {
	this.isStatic = isStatic;
	readRecordHeader(reader);
	long max = reader.getPointerIndex() + getRecordLength() - 1;
	boolean hasBigFields = hasBigFields();
	baseGroupIndex = OmfRecord.readIndex(reader);
	baseSegmentIndex = OmfRecord.readIndex(reader);
	if (baseSegmentIndex == 0)
		baseFrame = reader.readNextShort() & 0xffff;
	
	ArrayList<OmfSymbol> symbollist = new ArrayList<OmfSymbol>();
	while(reader.getPointerIndex() < max) {
		String name = OmfRecord.readString(reader);
		long offset = OmfRecord.readInt2Or4(reader, hasBigFields) & 0xffffffffL;
		int type = OmfRecord.readIndex(reader);
		OmfSymbol subrec = new OmfSymbol(name,type,offset,0,0);
		symbollist.add(subrec);
	}
	readCheckSumByte(reader);
	symbol = new OmfSymbol[symbollist.size()];
	symbollist.toArray(symbol);
}
 
Example 8
Source File: ObjectiveC2_Category.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void readInstanceMethods(BinaryReader reader) throws IOException {
	long index = ObjectiveC1_Utilities.readNextIndex(reader, _state.is32bit);
	if (index != 0 && reader.isValidIndex(index)) {
		long originalIndex = reader.getPointerIndex();
		reader.setPointerIndex(index);
		instanceMethods = new ObjectiveC2_MethodList(_state, reader, ObjectiveC_MethodType.INSTANCE);
		reader.setPointerIndex(originalIndex);
	}
}
 
Example 9
Source File: ObjectiveC2_Implementation.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ObjectiveC2_Implementation(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._is32bit = state.is32bit;
	this._index = reader.getPointerIndex();

	if (state.is32bit) {
		imp = reader.readNextInt() & Conv.INT_MASK;
	}
	else {
		imp = reader.readNextLong();
	}
}
 
Example 10
Source File: CliAbstractSig.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliCustomMod(BinaryReader reader) throws IOException {
	cmod = CliElementType.fromInt(reader.readNextByte());
	long origIndex = reader.getPointerIndex();
	typeEncoded = decodeCompressedUnsignedInt(reader);
	long endIndex = reader.getPointerIndex();
	sizeOfCount = (int) (endIndex - origIndex);
}
 
Example 11
Source File: ObjectiveC2_InstanceVariableList.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ObjectiveC2_InstanceVariableList(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._state = state;
	this._index = reader.getPointerIndex();

	entsize = reader.readNextInt();
	count   = reader.readNextInt();

	for (int i = 0 ; i < count ; ++i) {
		ivars.add( new ObjectiveC2_InstanceVariable(state, reader) );
	}
}
 
Example 12
Source File: PdbInfo.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public PdbInfo(BinaryReader reader, int ptr) throws IOException {
	long origIndex = reader.getPointerIndex();
	reader.setPointerIndex(ptr);
	try {
		magic     = reader.readNextByteArray(4);
		offset    = reader.readNextInt();
		sig       = reader.readNextInt();
		age       = reader.readNextInt();
		pdbName   = reader.readNextAsciiString();
	}
	finally {
		reader.setPointerIndex(origIndex);
	}
}
 
Example 13
Source File: PdbInfoDotNet.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public PdbInfoDotNet(BinaryReader reader, int ptr) throws IOException {
	long origIndex = reader.getPointerIndex();
	reader.setPointerIndex(ptr);
	try {
		magic = reader.readNextByteArray(4);
		guid = new GUID(reader);
		age = reader.readNextInt();
		pdbName = reader.readNextAsciiString();
	}
	finally {
		reader.setPointerIndex(origIndex);
	}
}
 
Example 14
Source File: ObjectiveC2_Protocol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void readProtocols(BinaryReader reader) throws IOException {
	long index = ObjectiveC1_Utilities.readNextIndex(reader, _state.is32bit);
	if (index != 0 && reader.isValidIndex(index)) {
		long originalIndex = reader.getPointerIndex();
		reader.setPointerIndex(index);
		protocols = new ObjectiveC2_ProtocolList(_state, reader);
		reader.setPointerIndex(originalIndex);
	}
}
 
Example 15
Source File: NRO0Header.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
public NRO0Header(BinaryReader reader, int readerOffset)
{
    long prevPointerIndex = reader.getPointerIndex();
    
    reader.setPointerIndex(readerOffset);
    this.readHeader(reader);
    
    // Restore the previous pointer index
    reader.setPointerIndex(prevPointerIndex);
}
 
Example 16
Source File: ISO9660BaseVolume.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public ISO9660BaseVolume(BinaryReader reader) throws IOException {
	volumeIndex = reader.getPointerIndex();
	typeCode = reader.readNextByte();
	identifier = reader.readNextByteArray(ISO9660Constants.MAGIC_BYTES.length);
	version = reader.readNextByte();
}
 
Example 17
Source File: ObjectiveC_MethodList.java    From ghidra with Apache License 2.0 4 votes vote down vote up
protected ObjectiveC_MethodList(ObjectiveC1_State state, BinaryReader reader, String className) {
	this._state = state;
	this._index = reader.getPointerIndex();
	this._className = className;
}
 
Example 18
Source File: CliAbstractSig.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public CliTypeClass(BinaryReader reader, CliElementType typeCode) throws IOException {
	super(typeCode);
	long origIndex = reader.getPointerIndex();
	encodedType = decodeCompressedUnsignedInt(reader);
	typeBytes = (int) (reader.getPointerIndex() - origIndex);
}
 
Example 19
Source File: DIEAggregate.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Return a list of DWARF locations read from the debug_loc section.
 * @param offset offset into the debug_loc section
 * @return list of DWARF locations (address range and location expression)
 * @throws IOException if an I/O error occurs
 */
private List<DWARFLocation> readDebugLocList(long offset) throws IOException {
	BinaryReader debug_loc = getCompilationUnit().getProgram().getDebugLocation();

	List<DWARFLocation> ranges = new ArrayList<>();
	if (debug_loc == null) {
		return ranges;
	}

	debug_loc.setPointerIndex(offset);
	byte pointerSize = getCompilationUnit().getPointerSize();

	Number baseAddress = getCompilationUnit().getCompileUnit().getLowPC();
	long baseAddressOffset = (baseAddress != null) ? baseAddress.longValue() : 0;

	Number cuLowPC = getCompilationUnit().getCompileUnit().getLowPC();
	long cuBase = (cuLowPC != null) ? cuLowPC.longValue() : Long.MAX_VALUE;

	// Loop through the debug_loc entry
	while (debug_loc.getPointerIndex() < debug_loc.length()) {
		Number beginning = DWARFUtil.readAddress(debug_loc, pointerSize);
		Number ending = DWARFUtil.readAddress(debug_loc, pointerSize);

		// List end
		if (beginning.longValue() == 0 && ending.longValue() == 0) {
			break;
		}

		// Check to see if this is a base address entry
		if (NumberUtil.equalsMaxUnsignedValue(beginning)) {
			baseAddressOffset = ending.longValue();
			continue;
		}

		// Size is 2 bytes
		int size = debug_loc.readNextShort() & NumberUtil.UNSIGNED_SHORT_MASK;

		// Read the location description
		byte[] location = debug_loc.readNextByteArray(size);

		// Test to see if the 'offset' read from the debug_loc data is already
		// greater-than the compunit's lowpc.  This indicates the 'offset' isn't
		// an offset, but already an absolute value.  This occurs in some
		// gcc dwarf compilation flag combinations.
		boolean isBadOffset = (beginning.longValue() > cuBase);

		long absStart = beginning.longValue();
		long absEnd = ending.longValue();
		if (!isBadOffset) {
			absStart += baseAddressOffset;
			absEnd += baseAddressOffset;
		}

		// TODO: verify end addr calc with DWARFstd.pdf, inclusive vs exclusive
		ranges.add(new DWARFLocation(new DWARFRange(absStart, absEnd + 1), location));
	}
	return ranges;
}
 
Example 20
Source File: ISO9660SetTerminator.java    From ghidra with Apache License 2.0 2 votes vote down vote up
public ISO9660SetTerminator(BinaryReader reader) throws IOException {

		super(reader);
		endVolumeIndex = reader.getPointerIndex();

	}