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

The following examples show how to use ghidra.app.util.bin.BinaryReader#readNextAsciiString() . 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: 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 2
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 3
Source File: CliStreamHeader.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new CLI Stream Header datatype.
 * 
 * @param metadataRoot the metadata root.
 * @param reader A binary reader set to start reading at the start of this header.
 * @throws IOException if there is a problem reading the header.
 */
public CliStreamHeader(CliMetadataRoot metadataRoot, BinaryReader reader)
		throws IOException {
	this.metadataRoot = metadataRoot;
	
	long headerStartIndex = reader.getPointerIndex();

	this.offset = reader.readNextInt();
	this.size = reader.readNextInt();
	
	// name is an ASCII string aligned to the next 4-byte boundary
	long startIndex = reader.getPointerIndex();
	this.name = reader.readNextAsciiString();
	long endIndex = reader.getPointerIndex(); // Gives us the index of the byte after the first null terminator
	long stringBytes = endIndex - startIndex;
	long bytesToRoundUp = 0;
	if ((stringBytes % 4) != 0) {
		bytesToRoundUp += 4 - (stringBytes % 4);
	}
	this.nameLen = (int) (stringBytes + bytesToRoundUp);
	
	int totalLen = 2 * DWordDataType.dataType.getLength() + this.nameLen;

	reader.setPointerIndex(headerStartIndex + totalLen);
}
 
Example 4
Source File: NSO0Header.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
private void readHeader(BinaryReader reader)
{
    try 
    {
        this.magic = reader.readNextAsciiString(4);
        
        if (!this.magic.equals("NSO0"))
            throw new InvalidMagicException("NSO0");
        
        this.version = reader.readNextInt();
        reader.readNextInt(); // Reserved
        this.flags = reader.readNextInt();
        this.textHeader = new NSO0SectionHeader(reader);
        this.moduleOffset = reader.readNextInt();
        this.rodataHeader = new NSO0SectionHeader(reader);
        this.moduleFileSize = reader.readNextInt();
        this.dataHeader = new NSO0SectionHeader(reader);
        this.bssSize = reader.readNextInt();
        this.buildId = reader.readNextByteArray(0x20);
        this.compressedTextSize = reader.readNextInt();
        this.compressedRodataSize = reader.readNextInt();
        this.compressedDataSize = reader.readNextInt();
    } 
    catch (IOException e) 
    {
        Msg.error(this, "Failed to read NSO0 header");
    }
}
 
Example 5
Source File: NRO0Header.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
private void readHeader(BinaryReader reader)
{
    try 
    {
        reader.readNextInt(); // Reserved
        this.mod0Offset = reader.readNextInt();
        reader.readNextLong(); // Padding
        this.magic = reader.readNextAsciiString(4);
        
        if (!this.magic.equals("NRO0"))
            throw new InvalidMagicException("NRO0");
        
        this.version = reader.readNextInt();
        this.size = reader.readNextInt();
        this.flags = reader.readNextInt();
        this.textHeader = new NRO0SectionHeader(reader);
        this.rodataHeader = new NRO0SectionHeader(reader);
        this.dataHeader = new NRO0SectionHeader(reader);
        this.bssSize = reader.readNextInt();
        reader.readNextInt(); // Reserved
        this.buildId = reader.readNextByteArray(0x20);
        reader.readNextInt(); // Reserved
        this.apiInfo = new NRO0SectionHeader(reader);
        this.dynstr = new NRO0SectionHeader(reader);
        this.dynsym = new NRO0SectionHeader(reader);
    } 
    catch (IOException e) 
    {
        Msg.error(this, "Failed to read NRO0 header");
    }
}
 
Example 6
Source File: KIP1Header.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
private void readHeader(BinaryReader reader)
{
    try 
    {
        this.magic = reader.readNextAsciiString(4);
        
        if (!this.magic.equals("KIP1"))
            throw new InvalidMagicException("KIP1");
        
        this.name = reader.readNextAsciiString(12);
        this.tid = reader.readNextLong();
        this.processCategory = reader.readNextInt();
        this.mainThreadPriority = reader.readNextByte();
        this.defaultCpuCore = reader.readNextByte();
        reader.readNextByte(); // Unused
        this.flags = reader.readNextByte();
        
        for (int i = 0; i < this.sectionHeaders.length; i++)
        {
            this.sectionHeaders[i] = new KIP1SectionHeader(reader);
        }
    } 
    catch (IOException e) 
    {
        Msg.error(this, "Failed to read KIP1 header");
    }
}
 
Example 7
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 8
Source File: Ext4DirEntry.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Ext4DirEntry( BinaryReader reader ) throws IOException {
	inode = reader.readNextInt();
	rec_len = reader.readNextShort();
	name_len = reader.readNextShort();
	name = reader.readNextAsciiString(name_len);
	
	int extraSize = rec_len - (8 + name_len);
	if( extraSize > 0 ) {
		extra = reader.readNextByteArray(extraSize);
	}
}
 
Example 9
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 10
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 11
Source File: DWARFLine.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Read in a new file entry and store into this object.
 * @param reader binary reader to read the file entry
 * @throws IOException if an I/O error occurs
 */
public DWARFFile(BinaryReader reader) throws IOException {
	this.name = reader.readNextAsciiString();

	// This entry exists only if the length of the string is more than 0
	if (this.name.length() > 0) {
		this.directory_index = LEB128.decode(reader, false);
		this.modification_time = LEB128.decode(reader, false);
		this.length = LEB128.decode(reader, false);
	}
}
 
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: FileEntry.java    From ghidra with Apache License 2.0 5 votes vote down vote up
FileEntry(BinaryReader reader) throws IOException {
	fileName = reader.readNextAsciiString();
	if (fileName.length() == 0) {
		return;
	}
	directoryIndex = new LEB128(reader, false);
	lastModifiedTime = new LEB128(reader, false);
	fileLengthInBytes = new LEB128(reader, false);
}
 
Example 14
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 15
Source File: DWARFLine.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public DWARFLine(BinaryReader reader) throws IOException, DWARFException {
	this.unit_length = reader.readNextUnsignedInt();
	// Length of 0xffffffff implies 64-bit DWARF format
	if (this.unit_length == 0xffffffffL) {
		this.unit_length = reader.readNextLong();
		this.format = DWARFCompilationUnit.DWARF_64;
	}
	// Length of 0xfffffff0 or greater is reserved for DWARF
	else if (this.unit_length >= 0xfffffff0L) {
		throw new DWARFException("Reserved DWARF length value: " +
			Long.toHexString(this.unit_length) + ". Unknown extension.");
	}
	else {
		this.format = DWARFCompilationUnit.DWARF_32;
	}

	// A version number for this line number information section
	this.version = reader.readNextUnsignedShort();

	// Get the header length based on the current format
	this.header_length = DWARFUtil.readOffsetByDWARFformat(reader, this.format);

	this.minimum_instruction_length = reader.readNextUnsignedByte();

	// Maximum operations per instruction only exists in DWARF version 4 or higher
	if (this.version >= 4) {
		this.maximum_operations_per_instruction = reader.readNextUnsignedByte();
	}
	else {
		this.maximum_operations_per_instruction = 1;
	}
	this.default_is_stmt = reader.readNextUnsignedByte();
	this.line_base = reader.readNextByte();
	this.line_range = reader.readNextUnsignedByte();
	this.opcode_base = reader.readNextUnsignedByte();
	this.standard_opcode_length = new int[this.opcode_base];
	this.standard_opcode_length[0] = 1; /* Should never be used */
	for (int i = 1; i < this.opcode_base; i++) {
		this.standard_opcode_length[i] = reader.readNextUnsignedByte();
	}

	// Read all include directories
	this.include_directories = new ArrayList<>();
	String include = reader.readNextAsciiString();
	while (include.length() != 0) {
		this.include_directories.add(include);
		include = reader.readNextAsciiString();
	}

	// Read all files
	this.file_names = new ArrayList<>();
	DWARFFile file = new DWARFFile(reader);
	while (file.getName().length() != 0) {
		this.file_names.add(file);
		file = new DWARFFile(reader);
	}
}
 
Example 16
Source File: OmfRecord.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Read the OMF string format,  1-byte length, followed by that many ascii characters
 * @param reader
 * @return
 * @throws IOException 
 */
public static String readString(BinaryReader reader) throws IOException {
	int count = reader.readNextByte() & 0xff;
	return reader.readNextAsciiString(count);
}