Package ghidra.app.util.bin
Class BinaryReader
- java.lang.Object
-
- ghidra.app.util.bin.BinaryReader
-
- Direct Known Subclasses:
FactoryBundledWithBinaryReader
public class BinaryReader extends java.lang.ObjectA class for reading data from a generic byte provider in either big-endian or little-endian.
-
-
Field Summary
Fields Modifier and Type Field Description static intSIZEOF_BYTEThe size of a BYTE in Java.static intSIZEOF_INTThe size of an INTEGER in Java.static intSIZEOF_LONGThe size of a LONG in Java.static intSIZEOF_SHORTThe size of a SHORT in Java.
-
Constructor Summary
Constructors Constructor Description BinaryReader(ByteProvider provider, boolean isLittleEndian)Constructs a reader using the given ByteProvider and endian-order.BinaryReader(ByteProvider provider, DataConverter converter, long initialIndex)Creates a BinaryReader instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intalign(int alignValue)Aligns the current index on the specified alignment value.BinaryReaderasBigEndian()Returns a BinaryReader that is in BigEndian mode.BinaryReaderasLittleEndian()Returns a BinaryReader that is in LittleEndian mode.BinaryReaderclone()Returns an independent clone of this reader positioned at the same index.BinaryReaderclone(long newIndex)Returns a clone of this reader, with its own independent current position, positioned at the new index.ByteProvidergetByteProvider()Returns the underlying byte provider.longgetPointerIndex()Returns the current index value.booleanisBigEndian()Returns true if this reader will extract values in big endian.booleanisLittleEndian()Returns true if this reader will extract values in little endian, otherwise in big endian.booleanisValidIndex(int index)Returns true if the specified index into the underlying byte provider is valid.booleanisValidIndex(long index)Returns true if the specified index into the underlying byte provider is valid.longlength()Returns the length of the underlying file.bytepeekNextByte()Peeks at the next byte without incrementing the current index.intpeekNextInt()Peeks at the next integer without incrementing the current index.longpeekNextLong()Peeks at the next long without incrementing the current index.shortpeekNextShort()Peeks at the next short without incrementing the current index.java.lang.StringreadAsciiString(long index)Reads an Ascii string starting atindex, ending at the next character outside the range [32..126] or when reaching the end of the underlying ByteProvider.java.lang.StringreadAsciiString(long index, int length)Returns an Ascii string oflengthbytes starting atindex.java.lang.String[]readAsciiStringArray(long index, int nElements)Returns the Ascii string array ofnElementsstarting atindexbytereadByte(long index)Returns the signed BYTE atindex.byte[]readByteArray(long index, int nElements)Returns the BYTE array ofnElementsstarting atindex.java.lang.StringreadFixedLenAsciiString(long index, int len)Reads an fixed length Ascii string starting atindex.intreadInt(long index)Returns the signed INTEGER atindex.int[]readIntArray(long index, int nElements)Returns the INTEGER array ofnElementsstarting atindex.longreadLong(long index)Returns the signed LONG atindex.long[]readLongArray(long index, int nElements)Returns the LONG array ofnElementsstarting atindex.java.lang.StringreadNextAsciiString()Reads the Ascii string at the current index and then increments the current index by the length of the Ascii string that was found.java.lang.StringreadNextAsciiString(int length)Reads an Ascii string oflengthcharacters starting at the current index and then increments the current index bylength.bytereadNextByte()Reads the byte at the current index and then increments the current index bySIZEOF_BYTE.byte[]readNextByteArray(int nElements)Reads a byte array ofnElementsstarting at the current index and then increments the current index bySIZEOF_BYTE * nElements.intreadNextInt()Reads the integer at the current index and then increments the current index bySIZEOF_INT.int[]readNextIntArray(int nElements)Reads an integer array ofnElementsstarting at the current index and then increments the current index bySIZEOF_INT * nElements.longreadNextLong()Reads the long at the current index and then increments the current index bySIZEOF_LONG.long[]readNextLongArray(int nElements)Reads a long array ofnElementsstarting at the current index and then increments the current index bySIZEOF_LONG * nElements.java.lang.StringreadNextNullTerminatedAsciiString()Reads a null terminated Ascii string starting at the current index, ending at the first null character or when reaching the end of the underlying ByteProvider.shortreadNextShort()Reads the short at the current index and then increments the current index bySIZEOF_SHORT.short[]readNextShortArray(int nElements)Reads a short array ofnElementsstarting at the current index and then increments the current index bySIZEOF_SHORT * nElements.java.lang.StringreadNextUnicodeString()Reads the Unicode string at the current index and then increments the current index by the length of the Unicode string that was found.java.lang.StringreadNextUnicodeString(int length)Reads fixed length UTF-16 Unicode string the current index and then increments the currentpointer indexbylengthelements (length*2 bytes).intreadNextUnsignedByte()Reads the unsigned byte at the current index and then increments the current index bySIZEOF_BYTE.longreadNextUnsignedInt()Reads the unsigned integer at the current index and then increments the current index bySIZEOF_INT.intreadNextUnsignedShort()Reads the unsigned short at the current index and then increments the current index bySIZEOF_SHORT.shortreadShort(long index)Returns the signed SHORT atindex.short[]readShortArray(long index, int nElements)Returns the SHORT array ofnElementsstarting atindex.java.lang.StringreadTerminatedString(long index, char termChar)Reads an Ascii string starting atindex, ending at the nexttermCharcharacter byte or when reaching the end of the underlying ByteProvider.java.lang.StringreadTerminatedString(long index, java.lang.String termChars)Reads an Ascii string starting atindex, ending at the next character that is one of the specifiedtermCharsor when reaching the end of the underlying ByteProvider.java.lang.StringreadUnicodeString(long index)Reads a null-terminated UTF-16 Unicode string starting atindexusing the pre-specifiedendianness.java.lang.StringreadUnicodeString(long index, int length)Reads a fixed length UTF-16 Unicode string oflengthcharacters starting atindex, using the pre-specifiedendianness.intreadUnsignedByte(long index)Returns the unsigned BYTE atindex.longreadUnsignedInt(long index)Returns the unsigned INTEGER atindex.intreadUnsignedShort(long index)Returns the unsigned SHORT atindex.longreadUnsignedValue(long index, int len)Returns the unsigned value of the integer (of the specified length) at the specified offset.longreadValue(long index, int len)Returns the signed value of the integer (of the specified length) at the specified offset.voidsetLittleEndian(boolean isLittleEndian)Sets the endian of this binary reader.voidsetPointerIndex(int index)A convenience method for setting the index using a 32 bit integer.voidsetPointerIndex(long index)Sets the current index to the specified value.
-
-
-
Field Detail
-
SIZEOF_BYTE
public static final int SIZEOF_BYTE
The size of a BYTE in Java.- See Also:
- Constant Field Values
-
SIZEOF_SHORT
public static final int SIZEOF_SHORT
The size of a SHORT in Java.- See Also:
- Constant Field Values
-
SIZEOF_INT
public static final int SIZEOF_INT
The size of an INTEGER in Java.- See Also:
- Constant Field Values
-
SIZEOF_LONG
public static final int SIZEOF_LONG
The size of a LONG in Java.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BinaryReader
public BinaryReader(ByteProvider provider, boolean isLittleEndian)
Constructs a reader using the given ByteProvider and endian-order. If isLittleEndian is true, then all values read from the file will be done so assuming little-endian order. Otherwise, if isLittleEndian is false, then all values will be read assuming big-endian order.- Parameters:
provider- the byte providerisLittleEndian- the endian-order
-
BinaryReader
public BinaryReader(ByteProvider provider, DataConverter converter, long initialIndex)
Creates a BinaryReader instance.- Parameters:
provider- the ByteProvider to useconverter- theDataConverterto useinitialIndex- the initial offset
-
-
Method Detail
-
clone
public BinaryReader clone(long newIndex)
Returns a clone of this reader, with its own independent current position, positioned at the new index.- Parameters:
newIndex- the new index- Returns:
- an independent clone of this reader positioned at the new index
-
clone
public BinaryReader clone()
Returns an independent clone of this reader positioned at the same index.- Overrides:
clonein classjava.lang.Object- Returns:
- a independent clone of this reader positioned at the same index
-
asBigEndian
public BinaryReader asBigEndian()
Returns a BinaryReader that is in BigEndian mode.- Returns:
- a new independent BinaryReader, at the same position, in BigEndian mode
-
asLittleEndian
public BinaryReader asLittleEndian()
Returns a BinaryReader that is in LittleEndian mode.- Returns:
- a new independent instance, at the same position, in LittleEndian mode
-
isLittleEndian
public boolean isLittleEndian()
Returns true if this reader will extract values in little endian, otherwise in big endian.- Returns:
- true is little endian, false is big endian
-
isBigEndian
public boolean isBigEndian()
Returns true if this reader will extract values in big endian.- Returns:
- true is big endian, false is little endian
-
setLittleEndian
public void setLittleEndian(boolean isLittleEndian)
Sets the endian of this binary reader.- Parameters:
isLittleEndian- true for little-endian and false for big-endian
-
length
public long length() throws java.io.IOExceptionReturns the length of the underlying file.- Returns:
- returns the length of the underlying file
- Throws:
java.io.IOException- if an I/O error occurs
-
isValidIndex
public boolean isValidIndex(int index) throws java.io.IOExceptionReturns true if the specified index into the underlying byte provider is valid.- Parameters:
index- the index in the byte provider- Returns:
- returns true if the specified index is valid
- Throws:
java.io.IOException- if an I/O error occurs
-
isValidIndex
public boolean isValidIndex(long index) throws java.io.IOExceptionReturns true if the specified index into the underlying byte provider is valid.- Parameters:
index- the index in the byte provider- Returns:
- returns true if the specified index is valid
- Throws:
java.io.IOException- if an I/O error occurs
-
align
public int align(int alignValue)
Aligns the current index on the specified alignment value. For example, if current index was 123 and align value was 16, then current index would become 128.- Parameters:
alignValue-- Returns:
- the number of bytes required to align
-
setPointerIndex
public void setPointerIndex(int index)
A convenience method for setting the index using a 32 bit integer.- Parameters:
index- new index, treated as a 32 bit unsigned integer
-
setPointerIndex
public void setPointerIndex(long index)
Sets the current index to the specified value. The pointer index will allow the reader to operate as a psuedo-iterator.- Parameters:
index- the byte provider index value
-
getPointerIndex
public long getPointerIndex()
Returns the current index value.- Returns:
- the current index value
-
peekNextByte
public byte peekNextByte() throws java.io.IOExceptionPeeks at the next byte without incrementing the current index.- Returns:
- the next byte
- Throws:
java.io.IOException- if an I/O error occurs
-
peekNextShort
public short peekNextShort() throws java.io.IOExceptionPeeks at the next short without incrementing the current index.- Returns:
- the next short
- Throws:
java.io.IOException- if an I/O error occurs
-
peekNextInt
public int peekNextInt() throws java.io.IOExceptionPeeks at the next integer without incrementing the current index.- Returns:
- the next int
- Throws:
java.io.IOException- if an I/O error occurs
-
peekNextLong
public long peekNextLong() throws java.io.IOExceptionPeeks at the next long without incrementing the current index.- Returns:
- the next long
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextByte
public byte readNextByte() throws java.io.IOExceptionReads the byte at the current index and then increments the current index bySIZEOF_BYTE.- Returns:
- the byte at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextUnsignedByte
public int readNextUnsignedByte() throws java.io.IOExceptionReads the unsigned byte at the current index and then increments the current index bySIZEOF_BYTE.- Returns:
- the unsigned byte at the current index, as an int
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextShort
public short readNextShort() throws java.io.IOExceptionReads the short at the current index and then increments the current index bySIZEOF_SHORT.- Returns:
- the short at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextUnsignedShort
public int readNextUnsignedShort() throws java.io.IOExceptionReads the unsigned short at the current index and then increments the current index bySIZEOF_SHORT.- Returns:
- the unsigned short at the current index, as an int
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextInt
public int readNextInt() throws java.io.IOExceptionReads the integer at the current index and then increments the current index bySIZEOF_INT.- Returns:
- the integer at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextUnsignedInt
public long readNextUnsignedInt() throws java.io.IOExceptionReads the unsigned integer at the current index and then increments the current index bySIZEOF_INT.- Returns:
- the unsigned integer at the current index, as a long
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextLong
public long readNextLong() throws java.io.IOExceptionReads the long at the current index and then increments the current index bySIZEOF_LONG.- Returns:
- the long at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextAsciiString
public java.lang.String readNextAsciiString() throws java.io.IOExceptionReads the Ascii string at the current index and then increments the current index by the length of the Ascii string that was found. This method expects the string to be null-terminated.- Returns:
- the null-terminated Ascii string at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextNullTerminatedAsciiString
public java.lang.String readNextNullTerminatedAsciiString() throws java.io.IOExceptionReads a null terminated Ascii string starting at the current index, ending at the first null character or when reaching the end of the underlying ByteProvider.The current index is advanced to the next byte after the null terminator.
- Returns:
- the null-terminated Ascii string at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextAsciiString
public java.lang.String readNextAsciiString(int length) throws java.io.IOExceptionReads an Ascii string oflengthcharacters starting at the current index and then increments the current index bylength.- Returns:
- the Ascii string at the current index
- Throws:
java.io.IOException
-
readNextUnicodeString
public java.lang.String readNextUnicodeString() throws java.io.IOExceptionReads the Unicode string at the current index and then increments the current index by the length of the Unicode string that was found. This method expects the string to be double null-terminated ('\0\0').- Returns:
- the null-terminated Ascii string at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextUnicodeString
public java.lang.String readNextUnicodeString(int length) throws java.io.IOExceptionReads fixed length UTF-16 Unicode string the current index and then increments the currentpointer indexbylengthelements (length*2 bytes).- Returns:
- the UTF-16 Unicode string at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextByteArray
public byte[] readNextByteArray(int nElements) throws java.io.IOExceptionReads a byte array ofnElementsstarting at the current index and then increments the current index bySIZEOF_BYTE * nElements.- Returns:
- the byte array starting at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextShortArray
public short[] readNextShortArray(int nElements) throws java.io.IOExceptionReads a short array ofnElementsstarting at the current index and then increments the current index bySIZEOF_SHORT * nElements.- Returns:
- the short array starting at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextIntArray
public int[] readNextIntArray(int nElements) throws java.io.IOExceptionReads an integer array ofnElementsstarting at the current index and then increments the current index bySIZEOF_INT * nElements.- Returns:
- the integer array starting at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readNextLongArray
public long[] readNextLongArray(int nElements) throws java.io.IOExceptionReads a long array ofnElementsstarting at the current index and then increments the current index bySIZEOF_LONG * nElements.- Returns:
- the long array starting at the current index
- Throws:
java.io.IOException- if an I/O error occurs
-
readAsciiString
public java.lang.String readAsciiString(long index) throws java.io.IOExceptionReads an Ascii string starting atindex, ending at the next character outside the range [32..126] or when reaching the end of the underlying ByteProvider.Leading and trailing spaces will be trimmed before the string is returned.
- Parameters:
index- the index where the Ascii string begins- Returns:
- the trimmed Ascii string
- Throws:
java.io.IOException- if an I/O error occurs
-
readAsciiString
public java.lang.String readAsciiString(long index, int length) throws java.io.IOExceptionReturns an Ascii string oflengthbytes starting atindex. This method does not care about null-terminators. Leading and trailing spaces will be trimmed before the string is returned.- Parameters:
index- the index where the Ascii string beginslength- the length of the Ascii string- Returns:
- the trimmed Ascii string
- Throws:
java.io.IOException- if an I/O error occurs
-
readTerminatedString
public java.lang.String readTerminatedString(long index, char termChar) throws java.io.IOExceptionReads an Ascii string starting atindex, ending at the nexttermCharcharacter byte or when reaching the end of the underlying ByteProvider.Does NOT trim the string.
- Parameters:
index- the index where the Ascii string begins- Returns:
- the Ascii string (excluding the terminating character)
- Throws:
java.io.IOException- if an I/O error occurs
-
readTerminatedString
public java.lang.String readTerminatedString(long index, java.lang.String termChars) throws java.io.IOExceptionReads an Ascii string starting atindex, ending at the next character that is one of the specifiedtermCharsor when reaching the end of the underlying ByteProvider.Does NOT trim the string.
- Parameters:
index- the index where the Ascii string begins- Returns:
- the Ascii string (excluding the terminating character)
- Throws:
java.io.IOException- if an I/O error occurs
-
readFixedLenAsciiString
public java.lang.String readFixedLenAsciiString(long index, int len) throws java.io.IOExceptionReads an fixed length Ascii string starting atindex.Does NOT trim the string.
- Parameters:
index- the index where the Ascii string beginslen- number of bytes to read- Returns:
- the Ascii string
- Throws:
java.io.IOException- if an I/O error occurs
-
readUnicodeString
public java.lang.String readUnicodeString(long index) throws java.io.IOExceptionReads a null-terminated UTF-16 Unicode string starting atindexusing the pre-specifiedendianness.The end of the string is denoted by a two-byte (ie. short)
nullcharacter.Leading and trailing spaces will be trimmed before the string is returned.
- Parameters:
index- the index where the UTF-16 Unicode string begins- Returns:
- the trimmed UTF-16 Unicode string
- Throws:
java.io.IOException- if an I/O error occurs
-
readUnicodeString
public java.lang.String readUnicodeString(long index, int length) throws java.io.IOExceptionReads a fixed length UTF-16 Unicode string oflengthcharacters starting atindex, using the pre-specifiedendianness.This method does not care about null-terminators.
Leading and trailing spaces will be trimmed before the string is returned.
- Parameters:
index- the index where the UTF-16 Unicode string beginslength- the number of UTF-16 character elements to read.- Returns:
- the trimmed UTF-16 Unicode string
- Throws:
java.io.IOException- if an I/O error occurs
-
readByte
public byte readByte(long index) throws java.io.IOExceptionReturns the signed BYTE atindex.- Parameters:
index- the index where the BYTE begins- Returns:
- the signed BYTE
- Throws:
java.io.IOException- if an I/O error occurs
-
readUnsignedByte
public int readUnsignedByte(long index) throws java.io.IOExceptionReturns the unsigned BYTE atindex.- Parameters:
index- the index where the BYTE begins- Returns:
- the unsigned BYTE as an int
- Throws:
java.io.IOException- if an I/O error occurs
-
readShort
public short readShort(long index) throws java.io.IOExceptionReturns the signed SHORT atindex.- Parameters:
index- the index where the SHORT begins- Returns:
- the signed SHORT
- Throws:
java.io.IOException- if an I/O error occurs
-
readUnsignedShort
public int readUnsignedShort(long index) throws java.io.IOExceptionReturns the unsigned SHORT atindex.- Parameters:
index- the index where the SHORT begins- Returns:
- the unsigned SHORT as an int
- Throws:
java.io.IOException- if an I/O error occurs
-
readInt
public int readInt(long index) throws java.io.IOExceptionReturns the signed INTEGER atindex.- Parameters:
index- the index where the INTEGER begins- Returns:
- the signed INTEGER
- Throws:
java.io.IOException- if an I/O error occurs
-
readUnsignedInt
public long readUnsignedInt(long index) throws java.io.IOExceptionReturns the unsigned INTEGER atindex.- Parameters:
index- the index where the INTEGER begins- Returns:
- the unsigned INTEGER as a long
- Throws:
java.io.IOException- if an I/O error occurs
-
readLong
public long readLong(long index) throws java.io.IOExceptionReturns the signed LONG atindex.- Parameters:
index- the index where the LONG begins- Returns:
- the LONG
- Throws:
java.io.IOException- if an I/O error occurs
-
readValue
public long readValue(long index, int len) throws java.io.IOExceptionReturns the signed value of the integer (of the specified length) at the specified offset.- Parameters:
index- offset the offset from the membuffers origin (the address that it is set at)len- the number of bytes that the integer occupies. Valid values are 1 (byte), 2 (short), 4 (int), 8 (long)- Returns:
- value of requested length, with sign bit extended, in a long
- Throws:
java.io.IOException
-
readUnsignedValue
public long readUnsignedValue(long index, int len) throws java.io.IOExceptionReturns the unsigned value of the integer (of the specified length) at the specified offset.- Parameters:
index- offset the offset from the membuffers origin (the address that it is set at)len- the number of bytes that the integer occupies. Valid values are 1 (byte), 2 (short), 4 (int), 8 (long)- Returns:
- unsigned value of requested length, in a long
- Throws:
java.io.IOException
-
readByteArray
public byte[] readByteArray(long index, int nElements) throws java.io.IOExceptionReturns the BYTE array ofnElementsstarting atindex.- Parameters:
index- the index where the BYTE beginsnElements- the number of array elements- Returns:
- the BYTE array
- Throws:
java.io.IOException- if an I/O error occurs
-
readShortArray
public short[] readShortArray(long index, int nElements) throws java.io.IOExceptionReturns the SHORT array ofnElementsstarting atindex.- Parameters:
index- the index where the SHORT beginsnElements- the number of array elements- Returns:
- the SHORT array
- Throws:
java.io.IOException- if an I/O error occurs
-
readIntArray
public int[] readIntArray(long index, int nElements) throws java.io.IOExceptionReturns the INTEGER array ofnElementsstarting atindex.- Parameters:
index- the index where the INTEGER beginsnElements- the number of array elements- Returns:
- the INTEGER array
- Throws:
java.io.IOException- if an I/O error occurs
-
readLongArray
public long[] readLongArray(long index, int nElements) throws java.io.IOExceptionReturns the LONG array ofnElementsstarting atindex.- Parameters:
index- the index where the LONG beginsnElements- the number of array elements- Returns:
- the LONG array
- Throws:
java.io.IOException- if an I/O error occurs
-
readAsciiStringArray
public java.lang.String[] readAsciiStringArray(long index, int nElements) throws java.io.IOExceptionReturns the Ascii string array ofnElementsstarting atindex- Parameters:
index- the index where the Ascii Strings beginnElements- the number of array elements- Returns:
- the Ascii String array
- Throws:
java.io.IOException- if an I/O error occurs
-
getByteProvider
public ByteProvider getByteProvider()
Returns the underlying byte provider.- Returns:
- the underlying byte provider
-
-