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

The following examples show how to use ghidra.app.util.bin.BinaryReader#readAsciiString() . 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: ElfStringTable.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Read string from table at specified relative table offset
 * @param reader
 * @param stringOffset table relative string offset
 * @return string or null on error
 */
public String readString(BinaryReader reader, long stringOffset) {
	if (fileOffset < 0) {
		return null;
	}
	try {
		if (stringOffset >= length) {
			throw new IOException("String read beyond table bounds");
		}
		return reader.readAsciiString(fileOffset + stringOffset);
	}
	catch (IOException e) {
		Msg.error(this,
			"Failed to read Elf String at offset 0x" + Long.toHexString(stringOffset) +
				" within String Table at offset 0x" + Long.toHexString(fileOffset));
	}
	return null;
}
 
Example 2
Source File: ObjectiveC1_Class.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public ObjectiveC1_Class(ObjectiveC1_State state, BinaryReader reader) throws IOException {
	this._state = state;
	this._index = reader.getPointerIndex();

	isa = new ObjectiveC1_MetaClass(state, reader.clone(reader.readNextInt()));
	super_class = ObjectiveC1_Utilities.dereferenceAsciiString(reader, state.is32bit);
	name = reader.readAsciiString(reader.readNextInt());
	version = reader.readNextInt();
	info = reader.readNextInt();
	instance_size = reader.readNextInt();
	variable_list =
		new ObjectiveC1_InstanceVariableList(state, reader.clone(reader.readNextInt()));
	method_list =
		new ObjectiveC1_MethodList(state, reader.clone(reader.readNextInt()),
			ObjectiveC_MethodType.INSTANCE);
	cache = reader.readNextInt();
	protocols = new ObjectiveC1_ProtocolList(state, reader.clone(reader.readNextInt()));
	unknown0 = reader.readNextInt();
	unknown1 = reader.readNextInt();
}
 
Example 3
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 4
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 5
Source File: DyldCacheImageInfo.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new {@link DyldCacheImageInfo}.
 * 
 * @param reader A {@link BinaryReader} positioned at the start of a DYLD image info
 * @throws IOException if there was an IO-related problem creating the DYLD image info
 */
public DyldCacheImageInfo(BinaryReader reader) throws IOException {
	address = reader.readNextLong();
	modTime = reader.readNextLong();
	inode = reader.readNextLong();
	pathFileOffset = reader.readNextInt();
	pad = reader.readNextInt();

	path = reader.readAsciiString(pathFileOffset);
}
 
Example 6
Source File: ObjectiveC1_Category.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ObjectiveC1_Category(ObjectiveC1_State state, BinaryReader reader) throws IOException {
	this._state = state;
	this._index = reader.getPointerIndex();

	category_name    = reader.readAsciiString( reader.readNextInt() );
	class_name       = reader.readAsciiString( reader.readNextInt() );
	instance_methods = new ObjectiveC1_MethodList(state, reader.clone(reader.readNextInt()), ObjectiveC_MethodType.INSTANCE);
	class_methods    = new ObjectiveC1_MethodList(state, reader.clone(reader.readNextInt()), ObjectiveC_MethodType.CLASS);
	protocols        = new ObjectiveC1_ProtocolList(state, reader.clone( reader.readNextInt() ));

	if (state.isARM) {
		unknown0     = reader.readNextInt();
		unknown1     = reader.readNextInt();
	}
}
 
Example 7
Source File: ImportedSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
ImportedSymbol(BinaryReader reader, LoaderInfoHeader loader) throws IOException {
	int value = reader.readNextInt();

	symbolClass      = ((value & 0xff000000) >> 24) & 0xff;
	symbolNameOffset = ((value & 0x00ffffff));

	long offset = loader.getSection().getContainerOffset()+loader.getLoaderStringsOffset()+symbolNameOffset;
	_name = reader.readAsciiString(offset);
}
 
Example 8
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 9
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 10
Source File: ObjectiveC2_Property.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ObjectiveC2_Property(ObjectiveC2_State state, BinaryReader reader) throws IOException {
	this._state = state;

	long nameIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
	name  = reader.readAsciiString(nameIndex);

	long attributesIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
	attributes  = reader.readAsciiString(attributesIndex);
}
 
Example 11
Source File: ObjectiveC2_Method.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ObjectiveC2_Method(ObjectiveC2_State state, BinaryReader reader, ObjectiveC_MethodType methodType) throws IOException {
	super(state, reader, methodType);

	long nameIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
	name  = reader.readAsciiString(nameIndex);

	long typesIndex = ObjectiveC1_Utilities.readNextIndex(reader, state.is32bit);
	types = reader.readAsciiString(typesIndex);

	imp   = new ObjectiveC2_Implementation(state, reader);
}
 
Example 12
Source File: ResourceDirectoryString.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 resource string begins
 */
public ResourceDirectoryString(BinaryReader reader, int index) throws IOException {
    length = reader.readShort(index);
    nameString = reader.readAsciiString(index+BinaryReader.SIZEOF_SHORT);
    if (nameString.length() != length) {
        //todo:
        throw new IllegalArgumentException("name string length != length");
    }
}
 
Example 13
Source File: SwitchLoader.java    From Ghidra-Switch-Loader with ISC License 4 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);
    String magic_0x0 = reader.readAsciiString(0, 4);
    String magic_0x10 = reader.readAsciiString(0x10, 4);
    
    reader.setPointerIndex(0);

    if (magic_0x0.equals("KIP1")) 
    {
        this.binaryType = BinaryType.KIP1;
    }
    else if (magic_0x0.equals("NSO0"))
    {
        this.binaryType = BinaryType.NSO0;
    }
    else if (magic_0x0.equals("\u00DF\u004F\u0003\u00D5"))
    {
        this.binaryType = BinaryType.KERNEL_800;
    }
    else if (magic_0x10.equals("NRO0"))
    {
        this.binaryType = BinaryType.NRO0;
    }
    else if (magic_0x10.equals("KIP1"))
    {
        // Note: This is kinda a bad way of determining this, but for now it gets the job done
        // and I don't believe there are any clashes.
        this.binaryType = BinaryType.SX_KIP1; 
    }
    else
        return loadSpecs;

    var adapter = this.binaryType.createAdapter(null, provider);
    
    if (adapter.isAarch32())
    {
        loadSpecs.add(new LoadSpec(this, 0, new LanguageCompilerSpecPair(AARCH32_LANGUAGE_ID, new CompilerSpecID("default")), true));
    }
    else
    {
        loadSpecs.add(new LoadSpec(this, 0, new LanguageCompilerSpecPair(AARCH64_LANGUAGE_ID, new CompilerSpecID("default")), true));
    }

    return loadSpecs;
}
 
Example 14
Source File: ObjectiveC2_Protocol.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void readName(BinaryReader reader) throws IOException {
	long index = ObjectiveC1_Utilities.readNextIndex(reader, _state.is32bit);
	if (index != 0 && reader.isValidIndex(index)) {
		name = reader.readAsciiString(index);
	}
}
 
Example 15
Source File: ObjectiveC2_ClassRW.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void readName(BinaryReader reader) throws IOException {
	long nameIndex = ObjectiveC1_Utilities.readNextIndex(reader, _state.is32bit);
	if (nameIndex != 0) {
		name = reader.readAsciiString( nameIndex );
	}
}
 
Example 16
Source File: ObjectiveC2_Category.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void readName(BinaryReader reader) throws IOException {
	long index = ObjectiveC1_Utilities.readNextIndex(reader, _state.is32bit);
	if (index != 0 && reader.isValidIndex(index)) {
		name = reader.readAsciiString(index);
	}
}