ghidra.app.util.bin.BinaryReader Java Examples

The following examples show how to use ghidra.app.util.bin.BinaryReader. 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: CliTableMethodDef.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public CliTableMethodDef(BinaryReader reader, CliStreamMetadata stream, CliTypeTable tableId)
		throws IOException {
	super(reader, stream, tableId);
	CliMethodDefRow lastRow = null;
	for (int i = 0; i < this.numRows; i++) {
		CliMethodDefRow row =
			new CliMethodDefRow(reader.readNextInt(), reader.readNextShort(),
				reader.readNextShort(), readStringIndex(reader), readBlobIndex(reader),
				readTableIndex(reader, CliTypeTable.Param));
		rows.add(row);
		strings.add(row.nameIndex);

		if (lastRow != null) {
			lastRow.nextRowParamIndex = row.paramIndex;
		}
		lastRow = row;
	}
	reader.setPointerIndex(this.readerOffset); // TODO: why do this, also elsewhere
}
 
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: DexLoader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<LoadSpec> findSupportedLoadSpecs(ByteProvider provider) throws IOException {
	List<LoadSpec> loadSpecs = new ArrayList<>();

	BinaryReader reader = new BinaryReader(provider, true);
	try {
		DexHeader header = new DexHeader(reader);
		if (DexConstants.DEX_MAGIC_BASE.equals(new String(header.getMagic()))) {
			List<QueryResult> queries =
				QueryOpinionService.query(getName(), DexConstants.MACHINE, null);
			for (QueryResult result : queries) {
				loadSpecs.add(new LoadSpec(this, 0, result));
			}
			if (loadSpecs.isEmpty()) {
				loadSpecs.add(new LoadSpec(this, 0, true));
			}
		}
	}
	catch (Exception e) {
		//ignore
	}

	return loadSpecs;
}
 
Example #5
Source File: RuntimeParameterAnnotationsAttribute.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public RuntimeParameterAnnotationsAttribute(BinaryReader reader, boolean isVisible)
		throws IOException {
	super(reader);

	_isVisible = isVisible;

	numberOfParameters = reader.readNextByte();

	for (int i = 0; i < getNumberOfParameters(); ++i) {
		short numberOfAnnotations = reader.readNextShort();
		AnnotationJava[] annotations = new AnnotationJava[(numberOfAnnotations & 0xffff)];
		for (int a = 0; a < (numberOfAnnotations & 0xffff); ++a) {
			annotations[a] = new AnnotationJava(reader);
		}
		parameterAnnotations.put(i, annotations);
	}
}
 
Example #6
Source File: ObjectiveC1_ClassAnalyzer.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private List<ObjectiveC1_Module> parseModuleList(ObjectiveC1_State state, BinaryReader reader) {
	List<ObjectiveC1_Module> modules = new ArrayList<ObjectiveC1_Module>();
	state.monitor.setMessage("Parsing Objective-C information...");
	try {
		MemoryBlock moduleInfoBlock =
			state.program.getMemory().getBlock(ObjectiveC1_Constants.OBJC_SECTION_MODULE_INFO);
		long moduleInfoStartIndex = moduleInfoBlock.getStart().getOffset();
		long moduleInfoEndIndex = moduleInfoBlock.getEnd().getOffset();

		state.monitor.initialize((int) (moduleInfoEndIndex - moduleInfoStartIndex));

		reader.setPointerIndex(moduleInfoStartIndex);
		while (reader.getPointerIndex() < moduleInfoEndIndex) {
			if (state.monitor.isCancelled()) {
				break;
			}
			modules.add(new ObjectiveC1_Module(state, reader));
			state.monitor.setProgress((int) (reader.getPointerIndex() - moduleInfoStartIndex));
		}
	}
	catch (IOException e) {
		e.printStackTrace();
	}
	return modules;
}
 
Example #7
Source File: DWARFAttributeFactoryTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testIndirect() throws IOException {
	// @formatter:off
	BinaryReader br = br(
		(byte)DWARFForm.DW_FORM_data1.getValue(),
		(byte) 55,
		(byte)DWARFForm.DW_FORM_ref4.getValue(),
		(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xaa
		);
	// @formatter:on

	DWARFAttributeValue result = attribFactory.read(br, cu, DWARFForm.DW_FORM_indirect);
	DWARFIndirectAttribute dia = (DWARFIndirectAttribute) result;
	DWARFNumericAttribute nestedAttr = (DWARFNumericAttribute) dia.getValue();
	assertEquals("should be 55", 55, nestedAttr.getValue());

	result = attribFactory.read(br, cu, DWARFForm.DW_FORM_indirect);
	dia = (DWARFIndirectAttribute) result;
	nestedAttr = (DWARFNumericAttribute) dia.getValue();
	assertEquals("should be aa + cuOffset", 0xaa + cu.getStartOffset(), nestedAttr.getValue());
}
 
Example #8
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 #9
Source File: MOD0Header.java    From Ghidra-Switch-Loader with ISC License 6 votes vote down vote up
private void readHeader(BinaryReader reader, int mod0StartOffset) throws InvalidMagicException, IOException
{
    this.magic = reader.readNextAsciiString(4);
    
    if (!this.magic.equals("MOD0"))
        throw new InvalidMagicException("MOD0");
    
    this.dynamicOffset = mod0StartOffset + reader.readNextInt();
    this.bssStartOffset = mod0StartOffset + reader.readNextInt();
    this.bssEndOffset = mod0StartOffset + reader.readNextInt();
    this.ehFrameHdrStartOffset = mod0StartOffset + reader.readNextInt();
    this.ehFrameHdrEndOffset = mod0StartOffset + reader.readNextInt();
    this.runtimeModuleOffset = mod0StartOffset + reader.readNextInt();
    
    this.lnxMagic = reader.readNextAsciiString(4);
    
    if (this.lnxMagic.equals("LNY0"))
    {
        Msg.info(this, "Detected Libnx MOD0 extension");
        this.lnxGotStart = mod0StartOffset + reader.readNextInt();
        this.lnxGotEnd = mod0StartOffset + reader.readNextInt();
    }
}
 
Example #10
Source File: CrushedPNGFileSystem.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void open(TaskMonitor monitor) throws IOException, CryptoException, CancelledException {
	BinaryReader reader = new BinaryReader(provider, false);

	monitor.setMessage("Opening iOS Crushed PNG...");
	this.png = new ProcessedPNG(reader, monitor);

	String uncrushedPngFilename = getName();

	//Remove the .png extension and then replace with .uncrushed.png extension
	if ("png".equalsIgnoreCase(FilenameUtils.getExtension(uncrushedPngFilename))) {
		uncrushedPngFilename =
			FilenameUtils.removeExtension(uncrushedPngFilename) + ".uncrushed.png";
	}

	pngGFile = GFileImpl.fromFilename(this, root, uncrushedPngFilename, false, 1, null);
}
 
Example #11
Source File: CliSigLocalVar.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public CliSigLocalVar(CliBlob blob) throws IOException {
	super(blob);

	// Now read our special data from the blob!
	BinaryReader reader = getContentsReader();
	byte id = reader.readNextByte();
	if (id != identifier)
		return; // Freak out? or just return...
	long origIndex = reader.getPointerIndex();
	int typesCount = decodeCompressedUnsignedInt(reader);
	sizeOfCount = (int) (reader.getPointerIndex() - origIndex);
	types = new CliParam[typesCount];
	// TODO: Does CliParam parse constraints?
	for (byte i = 0; i < typesCount; i++) {
		try {
			types[i] = new CliParam(reader);
		}
		catch (InvalidInputException e) {
			types[i] = null;
			e.printStackTrace();
		}
	}
}
 
Example #12
Source File: RelocSmRepeat.java    From ghidra with Apache License 2.0 5 votes vote down vote up
RelocSmRepeat(BinaryReader reader) throws IOException {
	int value   = reader.readNextShort() & 0xffff;

	opcode      = (value & 0xf000) >> 12;
	chunks      = (value & 0x0f00) >>  8;
	repeatCount = (value & 0x00ff);
}
 
Example #13
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 #14
Source File: ExportedSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
ExportedSymbol(BinaryReader reader, LoaderInfoHeader loader, ExportedSymbolKey key) throws IOException {
	classAndName = reader.readNextInt();
	symbolValue  = reader.readNextInt();
	sectionIndex = reader.readNextShort();

	long offset = loader.getSection().getContainerOffset()+loader.getLoaderStringsOffset()+getNameOffset();
	_name = reader.readAsciiString(offset, key.getNameLength()); 
}
 
Example #15
Source File: ObjectiveC1_MethodList.java    From ghidra with Apache License 2.0 5 votes vote down vote up
ObjectiveC1_MethodList(ObjectiveC1_State state, BinaryReader reader, ObjectiveC_MethodType methodType) throws IOException {
	super(state, reader, NAME);

	if (_index == 0) {
		return;
	}

	obsolete = new ObjectiveC1_MethodList(state, reader.clone(reader.readNextInt()), methodType );

	method_count = reader.readNextInt();

	for (int i = 0 ; i < method_count ; ++i) {
		methods.add(new ObjectiveC1_Method(state, reader, methodType));
	}
}
 
Example #16
Source File: CliTableMethodSemantics.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliTableMethodSemantics(BinaryReader reader, CliStreamMetadata stream, CliTypeTable tableId) throws IOException {
	super(reader, stream, tableId);
	for (int i = 0; i < this.numRows; i++) {
		rows.add(new CliMethodSemanticsRow(reader.readNextShort(), readTableIndex(reader, CliTypeTable.MethodDef), CliIndexHasSemantics.readCodedIndex(reader, stream)));
	}
	reader.setPointerIndex(this.readerOffset);
}
 
Example #17
Source File: CliMethodExtraSections.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliMethodExtraSections(BinaryReader reader) throws IOException {
	while (true) {
		ExtraSection section = new ExtraSection(reader);
		extraSections.add(section);
		if (!section.hasMoreSections)
			break;
	}
}
 
Example #18
Source File: ObjectiveC2_ClassRW.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void readInstanceVariables(BinaryReader reader)
		throws IOException {
	long instanceVariablesIndex = ObjectiveC1_Utilities.readNextIndex(reader, _state.is32bit);
	if (instanceVariablesIndex != 0) {
		long originalIndex = reader.getPointerIndex();
		reader.setPointerIndex(instanceVariablesIndex);
		ivars = new ObjectiveC2_InstanceVariableList( _state, reader );
		reader.setPointerIndex(originalIndex);
	}
}
 
Example #19
Source File: DWARFAttributeFactoryTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrp_32() throws IOException {
	prog.getDebugStrings().add(1, "string1 at 1");
	prog.getDebugStrings().add(100, "string2 at 100");

	// @formatter:off
	BinaryReader br = br(
		/* ref to str2 */ (byte) 0, (byte) 0, (byte) 0, (byte) 100,
		/* ref to str1 */ (byte) 0, (byte) 0, (byte) 0, (byte) 1,
		/* ref to str2 ofcut */ (byte) 0, (byte) 0, (byte) 0, (byte) 101,
		/* guard byte for test */ (byte) 0xff);
	// @formatter:on

	DWARFAttributeValue result = attribFactory.read(br, cu, DWARFForm.DW_FORM_strp);
	assertTrue("Should be string", result instanceof DWARFStringAttribute);
	assertEquals("string2 at 100", ((DWARFStringAttribute) result).getValue(stringTable));

	result = attribFactory.read(br, cu, DWARFForm.DW_FORM_strp);
	assertTrue("Should be string", result instanceof DWARFStringAttribute);
	assertEquals("string1 at 1", ((DWARFStringAttribute) result).getValue(stringTable));

	// test string ref to substring of string2
	result = attribFactory.read(br, cu, DWARFForm.DW_FORM_strp);
	assertTrue("Should be string", result instanceof DWARFStringAttribute);
	assertEquals("tring2 at 100", ((DWARFStringAttribute) result).getValue(stringTable));

	assertEquals("guard byte", (byte) 0xff, br.readNextByte());
}
 
Example #20
Source File: CoffSymbolAuxArray.java    From ghidra with Apache License 2.0 5 votes vote down vote up
CoffSymbolAuxArray(BinaryReader reader) throws IOException {
	tagIndex        = reader.readNextInt();
	lineNumber      = reader.readNextShort();
	arraySize       = reader.readNextShort();
	firstDimension  = reader.readNextShort();
	secondDimension = reader.readNextShort();
	thirdDimension  = reader.readNextShort();
	fourthDimension = reader.readNextShort();
	unused          = reader.readNextByteArray(2);
}
 
Example #21
Source File: RelocValueGroup.java    From ghidra with Apache License 2.0 5 votes vote down vote up
RelocValueGroup(BinaryReader reader) throws IOException {
	int value = reader.readNextShort() & 0xffff;

	opcode    = (value & 0xe000) >> 13;
	subopcode = (value & 0x1e00) >> 9;
	runLength = (value & 0x01ff);
}
 
Example #22
Source File: ResourceDataEntry.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
* Constructor.
* @param reader the binary reader
* @param index the index where this entry begins
*/
  public ResourceDataEntry(FactoryBundledWithBinaryReader reader, int index) throws IOException {
      offsetToData = reader.readInt(index);
      size         = reader.readInt(index += BinaryReader.SIZEOF_INT);
      codePage     = reader.readInt(index += BinaryReader.SIZEOF_INT);
      reserved     = reader.readInt(index += BinaryReader.SIZEOF_INT);
  }
 
Example #23
Source File: BinaryPropertyListTrailer.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public BinaryPropertyListTrailer( BinaryReader reader ) throws IOException {
	trailerIndex = reader.length( ) - BinaryPropertyListConstants.TRAILER_SIZE;

	offsetSize = reader.readByte( trailerIndex + 6 ) & 0xff;
	objectRefSize = reader.readByte( trailerIndex + 7 ) & 0xff;
	objectCount = reader.readInt( trailerIndex + 12 ) & 0xffffffff;
	topObject = reader.readInt( trailerIndex + 20 ) & 0xffffffff;
	offsetTableOffset = reader.readInt( trailerIndex + 28 ) & 0xffffffff;

	// if ( offsetSize != 4 ) {
	// throw new IOException( "Unsupported binary PLIST offset size: " +
	// offsetSize );
	// }

	offsetTable = new int [ objectCount ];

	for ( int i = 0 ; i < objectCount ; ++i ) {
		if ( offsetSize == 4 ) {
			offsetTable[ i ] = reader.readInt( offsetTableOffset + i * offsetSize );
		}
		else if ( offsetSize == 2 ) {
			offsetTable[ i ] = reader.readShort( offsetTableOffset + i * offsetSize ) & 0xffff;
		}
		else if ( offsetSize == 1 ) {
			offsetTable[ i ] = reader.readByte( offsetTableOffset + i * offsetSize ) & 0xff;
		}
		else {
			throw new RuntimeException( "Invalid offset size in binary PList" );
		}
	}
}
 
Example #24
Source File: ApploaderHeader.java    From Ghidra-GameCube-Loader with Apache License 2.0 5 votes vote down vote up
private void readHeader(BinaryReader reader) {
	try {
		fileSize = reader.length();
		reader.setPointerIndex(0);
		
		revision = reader.readNextAsciiString(16);
		entryPoint = reader.readNextUnsignedInt();
		size = reader.readNextInt();
		trailerSize = reader.readNextInt();
	}
	catch(IOException e) {
		Msg.error(this, "Failed to read Apploader header!");
	}
}
 
Example #25
Source File: CliTableFile.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliTableFile(BinaryReader reader, CliStreamMetadata stream, CliTypeTable tableId) throws IOException {
	super(reader, stream, tableId);
	for (int i = 0; i < this.numRows; i++) {
		CliFileRow row = new CliFileRow(reader.readNextInt(), readStringIndex(reader), readBlobIndex(reader));
		rows.add(row);
		strings.add(row.nameIndex);
		blobs.add(row.hashIndex);
	}
	reader.setPointerIndex(this.readerOffset);

}
 
Example #26
Source File: CliTableParam.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliTableParam(BinaryReader reader, CliStreamMetadata stream, CliTypeTable tableId) throws IOException {
	super(reader, stream, tableId);
	for (int i = 0; i < this.numRows; i++) {
		CliParamRow row = new CliParamRow(reader.readNextShort(), reader.readNextShort(), readStringIndex(reader));
		rows.add(row);
		strings.add(row.nameIndex);
	}
	reader.setPointerIndex(this.readerOffset);
}
 
Example #27
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 #28
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 #29
Source File: CliTableExportedType.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliTableExportedType(BinaryReader reader, CliStreamMetadata stream, CliTypeTable tableId) throws IOException {
	super(reader, stream, tableId);
	for (int i = 0; i < this.numRows; i++) {
		CliExportedTypeRow row = new CliExportedTypeRow(reader.readNextInt(), reader.readNextInt(), readStringIndex(reader), readStringIndex(reader),
			CliIndexImplementation.readCodedIndex(reader, stream));
		rows.add(row);
		strings.add(row.typeNameIndex);
		strings.add(row.typeNamespaceIndex);
	}
	reader.setPointerIndex(this.readerOffset);
}
 
Example #30
Source File: CliTablePropertyMap.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public CliTablePropertyMap(BinaryReader reader, CliStreamMetadata stream, CliTypeTable tableId) throws IOException {
	super(reader, stream, tableId);
	for (int i = 0; i < this.numRows; i++) {
		rows.add(new CliPropertyMapRow(readTableIndex(reader, CliTypeTable.TypeDef), readTableIndex(reader, CliTypeTable.Property)));
	}
	reader.setPointerIndex(this.readerOffset);
}