ghidra.app.util.bin.ByteArrayProvider Java Examples

The following examples show how to use ghidra.app.util.bin.ByteArrayProvider. 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: ElfDataType.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
protected void populateDynamicStructure(MemBuffer buf, Structure struct) {
	try {
        Memory memory = buf.getMemory();
        MemoryBlock block = memory.getBlock(buf.getAddress());
        byte [] bytes = new byte[(int)block.getSize()];
        block.getBytes(block.getStart(), bytes);

        ByteArrayProvider bap = new ByteArrayProvider(bytes);

        ElfHeader elf = ElfHeader.createElfHeader(RethrowContinuesFactory.INSTANCE, bap);
        elf.parse();

        struct.add(elf.toDataType());
	}
	catch (Exception e) {
		
	}
}
 
Example #2
Source File: MemoryBlockUtilTest.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Test
public void testDuplicateExceptionHandling() throws Exception {
	ByteProvider byteProvider = new ByteArrayProvider(new byte[1000]);
	FileBytes fileBytes =
		MemoryBlockUtils.createFileBytes(prog, byteProvider, TaskMonitor.DUMMY);

	MemoryBlockUtils.createInitializedBlock(prog, true, "test", addr(0), fileBytes, 0, 10, "",
		"", true, true, true, new MessageLog());
	MemoryBlockUtils.createInitializedBlock(prog, true, "test", addr(0), fileBytes, 0, 10, "",
		"", true, true, true, new MessageLog());
	MemoryBlockUtils.createInitializedBlock(prog, true, "test", addr(0), fileBytes, 0, 10, "",
		"", true, true, true, new MessageLog());

	MemoryBlock[] blocks = prog.getMemory().getBlocks();
	assertEquals(3, blocks.length);

	assertEquals("test", blocks[0].getName());
	assertEquals("test_1", blocks[1].getName());
	assertEquals("test_2", blocks[2].getName());

}
 
Example #3
Source File: NRO0Adapter.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
private void read() throws IOException
{
    this.nro0 = new NRO0Header(this.fileReader, 0x0);
    
    NRO0SectionHeader textHeader = this.nro0.getSectionHeader(NXOSectionType.TEXT);
    NRO0SectionHeader rodataHeader = this.nro0.getSectionHeader(NXOSectionType.RODATA);
    NRO0SectionHeader dataHeader = this.nro0.getSectionHeader(NXOSectionType.DATA);

    int textOffset = textHeader.getFileOffset();
    int rodataOffset = rodataHeader.getFileOffset();
    int dataOffset = dataHeader.getFileOffset();
    int textSize = textHeader.getSize();
    int rodataSize = rodataHeader.getSize();
    int dataSize = dataHeader.getSize();

    // The data section is last, so we use its offset + decompressed size
    byte[] full = new byte[dataOffset + dataSize];

    byte[] text = this.fileProvider.readBytes(textHeader.getFileOffset(), textSize);
    System.arraycopy(text, 0, full, textOffset, textSize);

    byte[] rodata = this.fileProvider.readBytes(rodataHeader.getFileOffset(), rodataSize);
    System.arraycopy(rodata, 0, full, rodataOffset, rodataSize);

    byte[] data = this.fileProvider.readBytes(dataHeader.getFileOffset(), dataSize);
    System.arraycopy(data, 0, full, dataOffset, dataSize);
    this.memoryProvider = new ByteArrayProvider(full);
    
    this.sections = new NXOSection[3];
    this.sections[NXOSectionType.TEXT.ordinal()] = new NXOSection(NXOSectionType.TEXT, textOffset, textSize);
    this.sections[NXOSectionType.RODATA.ordinal()] = new NXOSection(NXOSectionType.RODATA, rodataOffset, rodataSize);
    this.sections[NXOSectionType.DATA.ordinal()] = new NXOSection(NXOSectionType.DATA, dataOffset, dataSize);
}
 
Example #4
Source File: CompressedSectionProvider.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public ByteProvider getSectionAsByteProvider(String sectionName) throws IOException {
	ByteProvider bp = sp.getSectionAsByteProvider(sectionName);
	if (bp != null) {
		return bp;
	}

	bp = sectionNameToDecompressedSectionDataMap.get(sectionName);
	if (bp != null) {
		return bp;
	}

	bp = sp.getSectionAsByteProvider("z" + sectionName);
	if (bp != null) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		byte[] tempArray = new byte[1024];

		Inflater decompressor = new Inflater();
		decompressor.setInput(bp.readBytes(0, bp.length()));

		while (!decompressor.finished()) {
			try {
				int result = decompressor.inflate(tempArray);
				if (result == 0 && !decompressor.finished()) {
					throw new IOException("Zlib decompressor returned 0 bytes to inflate");
				}
				stream.write(tempArray, 0, result);
			}
			catch (DataFormatException e) {
				throw new IOException(e);
			}
		}

		ByteProvider decompressedBP = new ByteArrayProvider(stream.toByteArray());
		sectionNameToDecompressedSectionDataMap.put(sectionName, decompressedBP);
		return decompressedBP;
	}

	return null;
}
 
Example #5
Source File: NSO0Adapter.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
private void read() throws IOException
{
    this.nso0 = new NSO0Header(this.fileReader, 0x0);
    
    LZ4Factory factory = LZ4Factory.fastestInstance();
    LZ4FastDecompressor decompressor = factory.fastDecompressor();
    
    NSO0SectionHeader textHeader = this.nso0.getSectionHeader(NXOSectionType.TEXT);
    NSO0SectionHeader rodataHeader = this.nso0.getSectionHeader(NXOSectionType.RODATA);
    NSO0SectionHeader dataHeader = this.nso0.getSectionHeader(NXOSectionType.DATA);
    
    int textOffset = textHeader.getMemoryOffset();
    int rodataOffset = rodataHeader.getMemoryOffset();
    int dataOffset = dataHeader.getMemoryOffset();
    int textSize = textHeader.getDecompressedSize();
    int rodataSize = rodataHeader.getDecompressedSize();
    int dataSize = dataHeader.getDecompressedSize();
    
    // The data section is last, so we use its offset + decompressed size
    byte[] full = new byte[dataOffset + dataSize];
    byte[] decompressedText;
    byte[] decompressedRodata;
    byte[] decompressedData;
    
    if (this.nso0.isSectionCompressed(NXOSectionType.TEXT))
    {
        byte[] compressedText = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.TEXT), this.nso0.getCompressedSectionSize(NXOSectionType.TEXT));
        decompressedText = new byte[textSize];
        decompressor.decompress(compressedText, decompressedText);
    }
    else
    {
        decompressedText = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.TEXT), textSize);
    }
    
    System.arraycopy(decompressedText, 0, full, textOffset, textSize);
    
    if (this.nso0.isSectionCompressed(NXOSectionType.RODATA))
    {
        byte[] compressedRodata = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.RODATA), this.nso0.getCompressedSectionSize(NXOSectionType.RODATA));
        decompressedRodata = new byte[rodataSize];
        decompressor.decompress(compressedRodata, decompressedRodata);
    }
    else
    {
        decompressedRodata = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.RODATA), rodataSize);
    }
    
    System.arraycopy(decompressedRodata, 0, full, rodataOffset, rodataSize);
    
    if (this.nso0.isSectionCompressed(NXOSectionType.DATA))
    {
        byte[] compressedData = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.DATA), this.nso0.getCompressedSectionSize(NXOSectionType.DATA));
        decompressedData = new byte[dataSize];
        decompressor.decompress(compressedData, decompressedData);
    }
    else
    {
        decompressedData = this.fileProvider.readBytes(this.nso0.getSectionFileOffset(NXOSectionType.DATA), dataSize);
    }
    
    System.arraycopy(decompressedData, 0, full, dataOffset, dataSize);
    this.memoryProvider = new ByteArrayProvider(full);
    
    this.sections = new NXOSection[3];
    this.sections[NXOSectionType.TEXT.ordinal()] = new NXOSection(NXOSectionType.TEXT, textOffset, textSize);
    this.sections[NXOSectionType.RODATA.ordinal()] = new NXOSection(NXOSectionType.RODATA, rodataOffset, rodataSize);
    this.sections[NXOSectionType.DATA.ordinal()] = new NXOSection(NXOSectionType.DATA, dataOffset, dataSize);
}
 
Example #6
Source File: KNXAdapter.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
private void read() throws IOException
{
    Msg.info(this, "Reading...");
    
    this.fileReader.setPointerIndex(0);
    
    while (this.fileReader.getPointerIndex() < 0x2000)
    {
        long candidate = this.fileReader.readNextInt();
        
        if (candidate == 0xD51C403E)
        {
            break;
        }
    }
    
    if (this.fileReader.getPointerIndex() >= 0x2000)
        throw new RuntimeException("Failed to find map offset");
    
    long mapOffset = this.fileReader.getPointerIndex() - 0x34;
    this.map = new KNXMapHeader(this.fileReader, (int)mapOffset);
    
    int textOffset = this.map.getTextFileOffset();
    int rodataOffset = this.map.getRodataFileOffset();
    int dataOffset = this.map.getDataFileOffset();
    int textSize = this.map.getTextSize();
    int rodataSize = this.map.getRodataSize();
    int dataSize = this.map.getDataSize();

    Msg.info(this, String.format("Text size: 0x%X", textSize));
    
    // The data section is last, so we use its offset + decompressed size
    byte[] full = new byte[dataOffset + dataSize];

    byte[] text = this.fileProvider.readBytes(textOffset, textSize);
    System.arraycopy(text, 0, full, textOffset, textSize);

    byte[] rodata = this.fileProvider.readBytes(rodataOffset, rodataSize);
    System.arraycopy(rodata, 0, full, rodataOffset, rodataSize);

    byte[] data = this.fileProvider.readBytes(dataOffset, dataSize);
    System.arraycopy(data, 0, full, dataOffset, dataSize);
    this.memoryProvider = new ByteArrayProvider(full);
    
    this.sections = new NXOSection[3];
    this.sections[NXOSectionType.TEXT.ordinal()] = new NXOSection(NXOSectionType.TEXT, textOffset, textSize);
    this.sections[NXOSectionType.RODATA.ordinal()] = new NXOSection(NXOSectionType.RODATA, rodataOffset, rodataSize);
    this.sections[NXOSectionType.DATA.ordinal()] = new NXOSection(NXOSectionType.DATA, dataOffset, dataSize);
}
 
Example #7
Source File: KIP1Adapter.java    From Ghidra-Switch-Loader with ISC License 4 votes vote down vote up
private void read() throws IOException
{
    this.kip1 = new KIP1Header(this.fileReader, 0x0);
    
    KIP1SectionHeader textHeader = this.kip1.getSectionHeader(NXOSectionType.TEXT);
    KIP1SectionHeader rodataHeader = this.kip1.getSectionHeader(NXOSectionType.RODATA);
    KIP1SectionHeader dataHeader = this.kip1.getSectionHeader(NXOSectionType.DATA);
    
    int textOffset = textHeader.getOutOffset();
    int rodataOffset = rodataHeader.getOutOffset();
    int dataOffset = dataHeader.getOutOffset();
    int textSize = textHeader.getDecompressedSize();
    int rodataSize = rodataHeader.getDecompressedSize();
    int dataSize = dataHeader.getDecompressedSize();
    
    // The data section is last, so we use its offset + decompressed size
    byte[] full = new byte[dataOffset + dataSize];
    byte[] decompressedText;
    byte[] decompressedRodata;
    byte[] decompressedData;
    
    if (this.kip1.isSectionCompressed(NXOSectionType.TEXT))
    {
        byte[] compressedText = this.fileProvider.readBytes(this.kip1.getSectionFileOffset(NXOSectionType.TEXT), this.kip1.getCompressedSectionSize(NXOSectionType.TEXT));
        decompressedText = ByteUtil.kip1BlzDecompress(compressedText, textSize);
    }
    else
    {
        decompressedText = this.fileProvider.readBytes(this.kip1.getSectionFileOffset(NXOSectionType.TEXT), textSize);
    }
    
    System.arraycopy(decompressedText, 0, full, textOffset, textSize);
    
    if (this.kip1.isSectionCompressed(NXOSectionType.RODATA))
    {
        byte[] compressedRodata = this.fileProvider.readBytes(this.kip1.getSectionFileOffset(NXOSectionType.RODATA), this.kip1.getCompressedSectionSize(NXOSectionType.RODATA));
        decompressedRodata = ByteUtil.kip1BlzDecompress(compressedRodata, rodataSize);
    }
    else
    {
        decompressedRodata = this.fileProvider.readBytes(this.kip1.getSectionFileOffset(NXOSectionType.RODATA), rodataSize);
    }
    
    System.arraycopy(decompressedRodata, 0, full, rodataOffset, rodataSize);
    
    if (this.kip1.isSectionCompressed(NXOSectionType.DATA))
    {
        byte[] compressedData = this.fileProvider.readBytes(this.kip1.getSectionFileOffset(NXOSectionType.DATA), this.kip1.getCompressedSectionSize(NXOSectionType.DATA));
        decompressedData = ByteUtil.kip1BlzDecompress(compressedData, dataSize);
    }
    else
    {
        decompressedData = this.fileProvider.readBytes(this.kip1.getSectionFileOffset(NXOSectionType.DATA), dataSize);
    }
    
    System.arraycopy(decompressedData, 0, full, dataOffset, dataSize);
    this.memoryProvider = new ByteArrayProvider(full);
    
    this.sections = new NXOSection[3];
    this.sections[NXOSectionType.TEXT.ordinal()] = new NXOSection(NXOSectionType.TEXT, textOffset, textSize);
    this.sections[NXOSectionType.RODATA.ordinal()] = new NXOSection(NXOSectionType.RODATA, rodataOffset, rodataSize);
    this.sections[NXOSectionType.DATA.ordinal()] = new NXOSection(NXOSectionType.DATA, dataOffset, dataSize);
}
 
Example #8
Source File: NullSectionProvider.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public ByteProvider getSectionAsByteProvider(String sectionName) throws IOException {
	return new ByteArrayProvider(new byte[] {});
}
 
Example #9
Source File: DWARFAttributeFactoryTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private BinaryReader br(byte... bytes) {
	return new BinaryReader(new ByteArrayProvider(bytes), prog.isLittleEndian());
}
 
Example #10
Source File: LEB128Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private BinaryReader br(byte... bytes) {
	return new BinaryReader(new ByteArrayProvider(bytes), BR_IS_LITTLE_ENDIAN);
}
 
Example #11
Source File: StringTableTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private BinaryReader br(byte... bytes) {
	return new BinaryReader(new ByteArrayProvider(bytes), true);
}
 
Example #12
Source File: Yaz0.java    From Ghidra-GameCube-Loader with Apache License 2.0 4 votes vote down vote up
public ByteProvider Decompress(ByteProvider provider) throws IOException {
	var reader = new BinaryReader(provider, false);
	int decompressedSize = reader.readInt(4);
	byte[] decompressBuffer = new byte[decompressedSize];
	
	int readPosition = 0x10;
	int sourceBitfield = 0;
	int writePosition = 0;
	int sourceByte = 0;
	
	do {
		int localReadPosition = readPosition;
		
		if (sourceBitfield == 0) {
			sourceByte = reader.readUnsignedByte(readPosition);
			sourceBitfield = 0x80;
			localReadPosition = readPosition + 1;
		}
		
		if ((sourceByte & sourceBitfield) == 0) {
			readPosition = localReadPosition + 2;
			
			int bitInfo = reader.readUnsignedShort(localReadPosition);
			int bitAdjustReadOffset = writePosition - (bitInfo & 0x0FFF);
			int writeSize;
			
			if ((bitInfo >> 12) == 0) {
				writeSize = reader.readUnsignedByte(readPosition) + 0x12;
				readPosition = localReadPosition + 3;
			}
			else {
				writeSize = ((bitInfo >> 12) & 0xF) + 2;
			}
			
			while (writeSize != 0) {
				decompressBuffer[writePosition] = decompressBuffer[bitAdjustReadOffset - 1];
				writePosition++;
				bitAdjustReadOffset++;
				writeSize--;
			}
		}
		else {
			readPosition = localReadPosition + 1;
			decompressBuffer[writePosition] = (byte)reader.readUnsignedByte(localReadPosition);
			writePosition++;
		}
		
		sourceBitfield >>= 1;
	} while (writePosition < decompressedSize);
	
	return new ByteArrayProvider(decompressBuffer);
}