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

The following examples show how to use ghidra.app.util.bin.BinaryReader#readNextByteArray() . 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: Ext4JournalSuperBlockS.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public Ext4JournalSuperBlockS(BinaryReader reader) throws IOException {
	// Journal is big-endian... opposite of the rest of the file.
	reader.setLittleEndian(false);
	
	s_header = new Ext4JournalHeaderS(reader);
	s_blocksize = reader.readNextInt();
	s_maxlen = reader.readNextInt();
	s_first = reader.readNextInt();
	s_sequence = reader.readNextInt();
	s_start = reader.readNextInt();
	s_errno = reader.readNextInt();
	s_feature_compat = reader.readNextInt();
	s_feature_incompat = reader.readNextInt();
	s_feature_ro_compat = reader.readNextInt();
	s_uuid = reader.readNextByteArray(16); //16 bytes long.
	s_nr_users = reader.readNextInt();
	s_dynsuper = reader.readNextInt();
	s_max_transaction = reader.readNextInt();
	s_max_trans_data = reader.readNextInt();
	s_checksum_type = reader.readNextByte();
	s_padding2 = reader.readNextByteArray(3); //3 bytes long.
	s_padding = reader.readNextIntArray(42); //42 ints long.
	s_checksum = reader.readNextInt();
	s_users = reader.readNextByteArray(768); //768 (16*48) bytes long.
}
 
Example 2
Source File: Apple8900Decryptor.java    From ghidra with Apache License 2.0 6 votes vote down vote up
public DecryptedPacket decrypt( String firmwareName, String firmwarePath, ByteProvider provider, TaskMonitor monitor ) throws IOException, CryptoException {
	BinaryReader reader = new BinaryReader( provider, true );

	Apple8900Header header = new Apple8900Header( reader );

	if (!header.getMagic( ).equals( Apple8900Constants.MAGIC )) {
		throw new IOException( "The 8900 file is not valid!" );
	}

	byte [] encryptedBlock = reader.readNextByteArray( header.getSizeOfData( ) );

	if (header.isEncrypted( )) {
		iOS_AesCrypto crypto = new iOS_AesCrypto( Apple8900Constants.AES_KEY_BYTES, Apple8900Constants.AES_IV_ZERO_BYTES );

		byte [] decryptedBlock = crypto.decrypt( encryptedBlock );

		return new DecryptedPacket( new ByteArrayInputStream( decryptedBlock ), decryptedBlock.length );
	}
	return new DecryptedPacket(new ByteArrayInputStream(encryptedBlock), encryptedBlock.length);
}
 
Example 3
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 4
Source File: PNGChunk.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Reads in the bytes of a PNG chunk from a given
 * BinaryReader
 * @param reader
 * @throws IOException
 */
public PNGChunk(BinaryReader reader) throws IOException {
	length = reader.readNextInt();
	chunkID = reader.readNextInt();
	data = reader.readNextByteArray(length);
	crc32 = reader.readNextInt();

	totalLength += (CrushedPNGConstants.GENERIC_CHUNK_SIZE + data.length);
}
 
Example 5
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 6
Source File: CoffSymbolAuxTagName.java    From ghidra with Apache License 2.0 5 votes vote down vote up
CoffSymbolAuxTagName(BinaryReader reader) throws IOException {
	unused1         = reader.readNextByteArray(6);
	size            = reader.readNextShort();
	unused2         = reader.readNextByteArray(4);
	nextEntryIndex  = reader.readNextInt();
	unused3         = reader.readNextByteArray(2);
}
 
Example 7
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 8
Source File: DebugInfoItem.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public DebugInfoItem( BinaryReader reader ) throws IOException {
	lineStart = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );
	lineStartLength = Leb128.unsignedLeb128Size( lineStart );
	reader.readNextByteArray( lineStartLength );// consume leb...

	parametersSize = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );
	parametersSizeLength = Leb128.unsignedLeb128Size( parametersSize );
	reader.readNextByteArray( parametersSizeLength );// consume leb...

	parameterNames = new int[ parametersSize ];
	parameterNamesLengths = new int[ parametersSize ];

	for ( int i = 0 ; i < parametersSize ; ++i ) {
		int value = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );
		int valueLength = Leb128.unsignedLeb128Size( value );
		reader.readNextByteArray( valueLength );// consume leb...

		parameterNames[ i ] = value - 1;// uleb128p1

		parameterNamesLengths[ i ] = valueLength;
	}

	long startIndex = reader.getPointerIndex( );
	int count = DebugInfoStateMachineReader.computeLength( reader );
	reader.setPointerIndex( startIndex );
	stateMachineOpcodes = reader.readNextByteArray( count );
}
 
Example 9
Source File: Ext4Inode.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Ext4Inode( BinaryReader reader ) throws IOException {
	i_mode = reader.readNextShort();
	i_uid = reader.readNextShort();
	i_size_lo = reader.readNextInt();
	i_atime = reader.readNextInt();
	i_ctime = reader.readNextInt();
	i_mtime = reader.readNextInt();
	i_dtime = reader.readNextInt();
	i_gid = reader.readNextShort();
	i_links_count = reader.readNextShort();
	i_blocks_lo = reader.readNextInt();
	i_flags = reader.readNextInt();
	i_osd1 = reader.readNextInt();
	i_block = new Ext4IBlock(reader, (i_flags & 0x80000) != 0 );
	i_generation = reader.readNextInt();
	i_file_acl_lo = reader.readNextInt();
	i_size_high = reader.readNextInt();
	i_obso_faddr = reader.readNextInt();
	i_osd2 = reader.readNextByteArray(12); //12 bytes long
	i_extra_isize = reader.readNextShort();
	i_checksum_hi = reader.readNextShort();
	i_ctime_extra = reader.readNextInt();
	i_mtime_extra = reader.readNextInt();
	i_atime_extra = reader.readNextInt();
	i_crtime = reader.readNextInt();
	i_crtime_extra = reader.readNextInt();
	i_version_hi = reader.readNextInt();
	i_projid = reader.readNextInt();
}
 
Example 10
Source File: EncodedTypeAddressPair.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public EncodedTypeAddressPair( BinaryReader reader ) throws IOException {
	typeIndex = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );
	typeIndexLength = Leb128.unsignedLeb128Size( typeIndex );
	reader.readNextByteArray( typeIndexLength );// consume leb...

	address = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );
	addressLength = Leb128.unsignedLeb128Size( address );
	reader.readNextByteArray( addressLength );// consume leb...
}
 
Example 11
Source File: CliMetadataRoot.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new CLI Metadata Root datatype. Matches ISO 23271 II.24.2.
 * 
 * @param reader A binary reader set to start reading at the start of this header.
 * @param rva The RVA of this header.
 * @throws IOException if there is a problem reading the header.
 */
public CliMetadataRoot(BinaryReader reader, int rva)
		throws IOException {
	this.reader = reader;
	this.fileOffset = reader.getPointerIndex();
	this.rva = rva;
	
	this.signature = reader.readNextInt();
	this.majorVersion = reader.readNextShort();
	this.minorVersion = reader.readNextShort();
	this.reserved = reader.readNextInt();
	this.versionLength = reader.readNextInt();
	if (versionLength > 0 && versionLength < NTHeader.MAX_SANE_COUNT) {
		this.version =
			new String(reader.readNextByteArray(this.versionLength), Charset.forName("UTF-8"));
	}
	this.flags = reader.readNextShort();
	this.streamsCount = reader.readNextShort();

	this.streamHeaderMap = new LinkedHashMap<>();
	for (short i = 0; i < this.streamsCount; i++) {
		CliStreamHeader streamHeader = new CliStreamHeader(this, reader);
		streamHeaderMap.put(streamHeader.getName(), streamHeader);
		if (streamHeader.getName().equals(CliStreamMetadata.getName())) {
			metadataHeader = streamHeader;
		}
	}
}
 
Example 12
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 13
Source File: XCoffSymbol.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public XCoffSymbol(BinaryReader reader, XCoffOptionalHeader optionalHeader) throws IOException {
	_optionalHeader = optionalHeader;

	n_name  = reader.readNextByteArray(SYMNMLEN);
	n_value  = reader.readNextInt();
	n_scnum  = reader.readNextShort();
	n_type   = reader.readNextShort();
	n_sclass = reader.readNextByte();
	n_numaux = reader.readNextByte();

	aux = new byte[n_numaux * SYMSZ];

	// 11th byte in the last auxiliary entry (csect)
	x_smclas = (n_numaux > 0) ? aux[aux.length - 7] : 0;	
}
 
Example 14
Source File: Ext4IBlock.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Ext4IBlock(BinaryReader reader, boolean isExtentTree) throws IOException {
	this.isExtentTree = isExtentTree;
	int count = 0;
	if( isExtentTree ) {
		header = new Ext4ExtentHeader(reader);
		count++;
		short numEntries = header.getEh_entries();
		if( header.getEh_depth() > 0 ) {
			indexEntries = new ArrayList<Ext4ExtentIdx>();
			for( int i = 0; i < numEntries; i++ ) {
				indexEntries.add( new Ext4ExtentIdx(reader) );
				count++;
			}
		}
		else {
			extentEntries = new ArrayList<Ext4Extent>();
			for( int i = 0; i < numEntries; i++ ) {
				extentEntries.add( new Ext4Extent(reader) );
				count++;
			}
		}
	}
	
	int extraBytes = 60 - (count * 12);
	if ( extraBytes > 0 ) {
		extra = reader.readNextByteArray(extraBytes);
	}
	else {
		extra = new byte[ 0 ];
	}
}
 
Example 15
Source File: DmgHeaderV2_old.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public DmgHeaderV2_old(BinaryReader reader) throws IOException {
	sig                      = reader.readNextByteArray( 8 );
	version                  = reader.readNextInt();
	enc_iv_size              = reader.readNextInt();
	unk1                     = reader.readNextInt();
	unk2                     = reader.readNextInt();
	unk3                     = reader.readNextInt();
	unk4                     = reader.readNextInt();
	unk5                     = reader.readNextInt();
	uuid                     = reader.readNextByteArray( 16 );
	blocksize                = reader.readNextInt();
	datasize                 = reader.readNextLong();
	dataoffset               = reader.readNextLong();
	filler1                  = reader.readNextByteArray( 0x260 );
	kdf_algorithm            = reader.readNextInt();
	kdf_prng_algorithm       = reader.readNextInt();
	kdf_iteration_count      = reader.readNextInt();
	kdf_salt_len             = reader.readNextInt();
	kdf_salt                 = reader.readNextByteArray( 32 );
	blob_enc_iv_size         = reader.readNextInt();
	blob_enc_iv              = reader.readNextByteArray( 32 );
	blob_enc_key_bits        = reader.readNextInt();
	blob_enc_algorithm       = reader.readNextInt();
	blob_enc_padding         = reader.readNextInt();
	blob_enc_mode            = reader.readNextInt();
	encrypted_keyblob_size   = reader.readNextInt();
	encrypted_keyblob        = reader.readNextByteArray( 0x30 );
}
 
Example 16
Source File: EncodedValue.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public EncodedValue( BinaryReader reader ) throws IOException {
	value = reader.readNextByte( );
	valueType = ( byte ) ( value & 0x1f );
	valueArgs = ( byte ) ( ( value & 0xe0 ) >> 5 );

	// length of value[] is based on TYPE....

	switch ( valueType ) {
		case ValueFormats.VALUE_BYTE:
		case ValueFormats.VALUE_SHORT:
		case ValueFormats.VALUE_CHAR:
		case ValueFormats.VALUE_INT:
		case ValueFormats.VALUE_LONG:
		case ValueFormats.VALUE_FLOAT:
		case ValueFormats.VALUE_DOUBLE:
		case ValueFormats.VALUE_STRING:
		case ValueFormats.VALUE_TYPE:
		case ValueFormats.VALUE_FIELD:
		case ValueFormats.VALUE_METHOD:
		case ValueFormats.VALUE_ENUM: {
			valueBytes = reader.readNextByteArray( valueArgs + 1 );
			break;
		}
		case ValueFormats.VALUE_ARRAY: {
			array = new EncodedArray( reader );
			break;
		}
		case ValueFormats.VALUE_ANNOTATION: {
			annotation = new EncodedAnnotation( reader );
			break;
		}
		case ValueFormats.VALUE_NULL: {
			break;// do nothing...
		}
		case ValueFormats.VALUE_BOOLEAN: {
			break;// do nothing...
		}
		default : {
			//TODO throw new RuntimeException( "unsupported encoded value: 0x" + Integer.toHexString( valueType & 0xff ) );
		}
	}
}
 
Example 17
Source File: SourceDebugExtensionAttribute.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public SourceDebugExtensionAttribute( BinaryReader reader ) throws IOException {
	super( reader );

	debugExtension = reader.readNextByteArray( getAttributeLength() );
}
 
Example 18
Source File: DexHeader.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public DexHeader(BinaryReader reader) throws IOException {
	magic = reader.readNextByteArray( DexConstants.DEX_MAGIC_BASE.length( ) );
	version = reader.readNextByteArray( DexConstants.DEX_VERSION_LENGTH );

	if (!DexConstants.DEX_MAGIC_BASE.equals(new String(magic))) {
		throw new IOException("not a dex file.");
	}

	checksum = reader.readNextInt();
	signature = reader.readNextByteArray(20);
	fileSize = reader.readNextInt();
	headerSize = reader.readNextInt();
	endianTag = reader.readNextInt();
	linkSize = reader.readNextInt();
	linkOffset = reader.readNextInt();
	mapOffset = reader.readNextInt();
	stringIdsSize = reader.readNextInt();
	stringIdsOffset = reader.readNextInt();
	typeIdsSize = reader.readNextInt();
	typeIdsOffset = reader.readNextInt();
	protoIdsSize = reader.readNextInt();
	protoIdsOffset = reader.readNextInt();
	fieldIdsSize = reader.readNextInt();
	fieldIdsOffset = reader.readNextInt();
	methodIdsSize = reader.readNextInt();
	methodIdsOffset = reader.readNextInt();
	classDefsIdsSize = reader.readNextInt();
	classDefsIdsOffset = reader.readNextInt();
	dataSize = reader.readNextInt();
	dataOffset = reader.readNextInt();

	reader.setPointerIndex(mapOffset);
	if (mapOffset > 0) {
		mapList = new MapList(reader);
	}

	reader.setPointerIndex(stringIdsOffset);
	for (int i = 0; i < stringIdsSize; ++i) {
		strings.add(new StringIDItem(reader));
	}

	reader.setPointerIndex(typeIdsOffset);
	for (int i = 0; i < typeIdsSize; ++i) {
		types.add(new TypeIDItem(reader));
	}

	reader.setPointerIndex(protoIdsOffset);
	for (int i = 0; i < protoIdsSize; ++i) {
		prototypes.add(new PrototypesIDItem(reader));
	}

	reader.setPointerIndex(fieldIdsOffset);
	for (int i = 0; i < fieldIdsSize; ++i) {
		fields.add(new FieldIDItem(reader));
	}

	reader.setPointerIndex(methodIdsOffset);
	for (int i = 0; i < methodIdsSize; ++i) {
		methods.add(new MethodIDItem(reader));
	}

	reader.setPointerIndex(classDefsIdsOffset);
	for (int i = 0; i < classDefsIdsSize; ++i) {
		classDefs.add(new ClassDefItem(reader));
	}
}
 
Example 19
Source File: SingleResourceData.java    From ghidra with Apache License 2.0 4 votes vote down vote up
public SingleResourceData(BinaryReader reader) throws IOException {
	length = reader.readNextInt();
	data = reader.readNextByteArray(length);
}
 
Example 20
Source File: OmfEnumeratedData.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public byte[] getByteArray(BinaryReader reader) throws IOException {
	reader.setPointerIndex(streamOffset);
	byte[] buffer = reader.readNextByteArray(streamLength);
	return buffer;
}