Ghidra is a software reverse engineering (SRE) framework
Improved documentation of ByteProvider classes
Ryan Kurtzb487ce7ebaa1
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteArrayProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteArrayProvider.javaindex b02f95bee5..a812f64397 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteArrayProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteArrayProvider.java@@ -18,9 +18,9 @@ package ghidra.app.util.bin;import java.io.*;/**- * An implementation of ByteProvider where the underlying- * bytes are supplied by a static byte array.- *+ * An implementation of {@link ByteProvider} where the underlying bytes are supplied by a static+ * byte array.+ * <p>* NOTE: Use of this class is discouraged when the byte array could be large.*/public class ByteArrayProvider implements ByteProvider {@@ -28,18 +28,19 @@ public class ByteArrayProvider implements ByteProvider {private String name;/**- * Constructs a byte array provider using the specified- * byte array- * @param bytes the underlying byte array.+ * Constructs a {@link ByteArrayProvider} using the specified byte array+ *+ * @param bytes the underlying byte array*/public ByteArrayProvider(byte[] bytes) {this.srcBytes = bytes;}/**- * Constructs a byte array provider using the specified- * byte array- * @param bytes the underlying byte array.+ * Constructs a {@link ByteArrayProvider} using the specified byte array+ *+ * @param name the name of the {@link ByteProvider}+ * @param bytes the underlying byte array*/public ByteArrayProvider(String name, byte[] bytes) {this.name = name;@@ -83,7 +84,7 @@ public class ByteArrayProvider implements ByteProvider {}/**- * Throws IOException if index is not a valid position in the buffer.+ * Throws {@link IOException} if index is not a valid position in the buffer** @param index position in buffer to test* @param inclusiveMax allow buffer.length as indexdiff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProvider.javaindex e662f6eeab..c0c0a80ad4 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProvider.java@@ -15,11 +15,11 @@*/package ghidra.app.util.bin;+import java.io.*;+import ghidra.formats.gfilesystem.FSRL;import ghidra.formats.gfilesystem.FileSystemService;-import java.io.*;-/*** An interface for a generic random-access byte provider.*/@@ -28,8 +28,9 @@ public interface ByteProvider extends Closeable {/*** Returns the {@link FSRL} of the underlying file for this byte provider,* or null if this byte provider is not associated with a file.- * <p>- * @return {@link FSRL} of the underlying file, or null if no associated file.+ *+ * @return The {@link FSRL} of the underlying {@link File}, or null if no associated+ * {@link File}.*/default public FSRL getFSRL() {File f = getFile();@@ -37,73 +38,79 @@ public interface ByteProvider extends Closeable {}/**- * Returns the underlying file for this byte- * provider. Or null if this byte provider is- * not associated with a file.+ * Returns the underlying {@link File} for this {@link ByteProvider}, or null if this+ * {@link ByteProvider} is not associated with a {@link File}.+ ** @return the underlying file for this byte provider*/public File getFile();/**- * Returns the name of the byte provider.- * For example, the underlying file name.- * @return the name of the byte provider or null+ * Returns the name of the {@link ByteProvider}. For example, the underlying file name.+ *+ * @return the name of the {@link ByteProvider} or null if there is no name*/public String getName();/**- * Returns the absolute path (similar to, but not a, URI) to the byte provider.+ * Returns the absolute path (similar to, but not a, URI) to the {@link ByteProvider}.* For example, the complete path to the file.- * @return the absolute path to the byte provider or null+ *+ * @return the absolute path to the {@link ByteProvider} or null if not associated with a+ * {@link File}.*/public String getAbsolutePath();/**- * Returns the length of the underlying provider.- * @return the length of the underlying provider+ * Returns the length of the {@link ByteProvider}+ *+ * @return the length of the {@link ByteProvider}* @throws IOException if an I/O error occurs*/public long length() throws IOException;/*** Returns true if the specified index is valid.- * @param index the index in the byte provider- * @return returns true if the specified index is valid+ *+ * @param index the index in the byte provider to check+ * @return true if the specified index is valid*/public boolean isValidIndex(long index);/**- * Releases any resources the provider may have occupied.+ * Releases any resources the {@link ByteProvider} may have occupied+ ** @throws IOException if an I/O error occurs*/@Overridepublic void close() throws IOException;/**- * Reads a byte at the specified index.- * @param index the index to read the byte+ * Reads a byte at the specified index+ *+ * @param index the index of the byte to read* @return the byte read from the specified index* @throws IOException if an I/O error occurs*/public byte readByte(long index) throws IOException;/**- * Reads a byte array at the specified index.- * @param index the index to read the byte array- * @param length the number of elements to read+ * Reads a byte array at the specified index+ *+ * @param index the index of the byte to read+ * @param length the number of bytes to read* @return the byte array read from the specified index* @throws IOException if an I/O error occurs*/public byte[] readBytes(long index, long length) throws IOException;/**- * Returns an input stream to the underlying byte provider starting at- * the specified index+ * Returns an input stream to the underlying byte provider starting at the specified index.* <p>* The caller is responsible for closing the returned {@link InputStream} instance.* <p>- * @param index the index to initialize the input stream- * @return the input stream+ * @param index where in the {@link ByteProvider} to start the {@link InputStream}+ * @return the {@link InputStream}* @throws IOException if an I/O error occurs*/public InputStream getInputStream(long index) throws IOException;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProviderWrapper.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProviderWrapper.javaindex 2c6bb85c0b..3497f0cd02 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProviderWrapper.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/ByteProviderWrapper.java@@ -18,14 +18,21 @@ package ghidra.app.util.bin;import java.io.*;/**- * Creates a byte provider constrained to a sub-section- * of an existing byte provider.+ * Creates a {@link ByteProvider} constrained to a sub-section of an existing {@link ByteProvider}.*/public class ByteProviderWrapper implements ByteProvider {private ByteProvider provider;private long subOffset;private long subLength;+ /**+ * Constructs a {@link ByteProviderWrapper} around the specified {@link ByteProvider}+ *+ * @param provider the {@link ByteProvider} to wrap+ * @param subOffset the offset in the {@link ByteProvider} of where to start the new+ * {@link ByteProviderWrapper}+ * @param subLength the length of the new {@link ByteProviderWrapper}+ */public ByteProviderWrapper(ByteProvider provider, long subOffset, long subLength) {this.provider = provider;this.subOffset = subOffset;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/GhidraRandomAccessFile.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/GhidraRandomAccessFile.javaindex 1fdf4477f9..0b590237c0 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/GhidraRandomAccessFile.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/GhidraRandomAccessFile.java@@ -15,10 +15,10 @@*/package ghidra.app.util.bin;-import ghidra.util.Msg;-import java.io.*;+import ghidra.util.Msg;+/*** Instances of this class support both reading and writing to a* random access file. A random access file behaves like a large@@ -90,7 +90,7 @@ public class GhidraRandomAccessFile {* that name cannot be created, or if some other error occurs* while opening or creating the file*/- public GhidraRandomAccessFile(File file, String mode) throws IOException {+ public GhidraRandomAccessFile(File file, String mode) throws FileNotFoundException {this.file = file;randomAccessFile = new RandomAccessFile(file, mode);this.open = true;@@ -140,8 +140,7 @@ public class GhidraRandomAccessFile {* @param pos the offset position, measured in bytes from the* beginning of the file, at which to set the file* pointer.- * @throws IOException- * @exception IOException if <code>pos</code> is less than+ * @throws IOException if <code>pos</code> is less than* <code>0</code> or if an I/O error occurs.*/public void seek(long pos) throws IOException {@@ -202,9 +201,9 @@ public class GhidraRandomAccessFile {* array of bytes. This method blocks until at least one byte of input* is available.*- * @param b the buffer into which the data is read.- * @param offset the start offset of the data.- * @param length the maximum number of bytes read.+ * @param b the buffer into which the data is read.+ * @param offset the start offset of the data.+ * @param length the maximum number of bytes read.* @return the total number of bytes read into the buffer, or* <code>-1</code> if there is no more data because the end of* the file has been reached.diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/InputStreamByteProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/InputStreamByteProvider.javaindex c30cbee737..ec3c1017f6 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/InputStreamByteProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/InputStreamByteProvider.java@@ -22,7 +22,8 @@ import java.io.*;* data to be read, as long as there are no operations that request data from a previous* offset.* <p>- * In other words, this ByteProvider can only be used to read data at ever increasing offsets.+ * In other words, this {@link ByteProvider} can only be used to read data at ever increasing+ * offsets.* <p>*/public class InputStreamByteProvider implements ByteProvider {@@ -30,6 +31,12 @@ public class InputStreamByteProvider implements ByteProvider {private long length;private long currentIndex;+ /**+ * Constructs a {@link InputStreamByteProvider} from the specified {@link InputStream}+ *+ * @param inputStream the underlying {@link InputStream}+ * @param length the length of the {@link InputStreamByteProvider}+ */public InputStreamByteProvider(InputStream inputStream, long length) {this.inputStream = inputStream;this.length = length;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/MemoryByteProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/MemoryByteProvider.javaindex fc43a0592f..1562395d1b 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/MemoryByteProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/MemoryByteProvider.java@@ -22,24 +22,29 @@ import ghidra.program.model.mem.Memory;import ghidra.program.model.mem.MemoryBlock;/**- * A Byte Provider implementation based on Memory.+ * A {@link ByteProvider} implementation based on {@link Memory}.*/public class MemoryByteProvider implements ByteProvider {+protected Memory memory;protected Address baseAddress;/**- * Constructs a new provider for a specific address space.- * @param memory the memory+ * Constructs a new {@link MemoryByteProvider} for a specific {@link AddressSpace}. Bytes will be+ * provided starting at address 0 in the space.+ *+ * @param memory the {@link Memory}+ * @param space the {@link AddressSpace}*/public MemoryByteProvider(Memory memory, AddressSpace space) {this(memory, space.getAddress(0));}/**- * Constructs a new provider relative to the base address.- * @param memory the memory- * @param baseAddress the relative base address+ * Constructs a new {@link MemoryByteProvider} relative to the specified base address.+ *+ * @param memory the {@link Memory}+ * @param baseAddress the base address*/public MemoryByteProvider(Memory memory, Address baseAddress) {this.memory = memory;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessByteProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessByteProvider.javaindex 2618252cb2..2bace5bf3d 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessByteProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessByteProvider.java@@ -15,10 +15,10 @@*/package ghidra.app.util.bin;-import ghidra.formats.gfilesystem.FSRL;-import java.io.*;+import ghidra.formats.gfilesystem.FSRL;+/*** An implementation of ByteProvider where the underlying* bytes are supplied by a random access file.@@ -33,37 +33,38 @@ public class RandomAccessByteProvider implements ByteProvider {protected File file;protected GhidraRandomAccessFile randomAccessFile;private FSRL fsrl;+ private Long cachedLength;/**- * Constructs a byte provider using the specified file- * @param file the file to open for random access- * @throws FileNotFoundException if the file does not exist+ * Constructs a {@link ByteProvider} using the specified {@link File}.+ *+ * @param file the {@link File} to open for random access+ * @throws FileNotFoundException if the {@link File} does not exist*/- public RandomAccessByteProvider(File file) throws IOException {- this.file = file;- this.randomAccessFile = new GhidraRandomAccessFile(file, "r");+ public RandomAccessByteProvider(File file) throws FileNotFoundException {+ this(file, "r");}/**- * Constructs a byte provider using the specified file and FSRL.+ * Constructs a {@link ByteProvider} using the specified {@link File} and {@link FSRL}*- * @param file the file to open for random access- * @param fsrl the FSRL to use for the file's path- * @throws FileNotFoundException if the file does not exist+ * @param file the {@link File} to open for random access+ * @param fsrl the {@link FSRL} to use for the {@link File}'s path+ * @throws FileNotFoundException if the {@link File} does not exist*/- public RandomAccessByteProvider(File file, FSRL fsrl) throws IOException {- this.file = file;+ public RandomAccessByteProvider(File file, FSRL fsrl) throws FileNotFoundException {+ this(file, "r");this.fsrl = fsrl;- this.randomAccessFile = new GhidraRandomAccessFile(file, "r");}/**- * Constructs a byte provider using the specified file and permissions string- * @param file the file to open for random access+ * Constructs a {@link ByteProvider} using the specified {@link File} and permissions+ *+ * @param file the {@link File} to open for random access* @param permissions indicating permissions used for open- * @throws FileNotFoundException if the file does not exist+ * @throws FileNotFoundException if the {@link File} does not exist*/- public RandomAccessByteProvider(File file, String permissions) throws IOException {+ public RandomAccessByteProvider(File file, String permissions) throws FileNotFoundException {this.file = file;this.randomAccessFile = new GhidraRandomAccessFile(file, permissions);}@@ -74,25 +75,19 @@ public class RandomAccessByteProvider implements ByteProvider {}/**- * Sets the {@link FSRL} of this byte provider.+ * Sets the {@link FSRL} of this {@link ByteProvider}*- * @param fsrl the FSRL to assign to this byte provider+ * @param fsrl the {@link FSRL} to assign to this byte provider*/public void setFSRL(FSRL fsrl) {this.fsrl = fsrl;}- /**- * @see ghidra.app.util.bin.ByteProvider#getFile()- */@Overridepublic File getFile() {return file;}- /**- * @see ghidra.app.util.bin.ByteProvider#getName()- */@Overridepublic String getName() {return fsrl == null ? file.getName() : fsrl.getName();@@ -103,9 +98,6 @@ public class RandomAccessByteProvider implements ByteProvider {return fsrl == null ? file.getAbsolutePath() : fsrl.getPath();}- /**- * @see ghidra.app.util.bin.ByteProvider#getInputStream(long)- */@Overridepublic InputStream getInputStream(long index) throws IOException {FileInputStream is = new FileInputStream(file);@@ -113,45 +105,35 @@ public class RandomAccessByteProvider implements ByteProvider {return is;}- /**- * Closes the underlying random-access file.- * @throws IOException if an I/O error occurs- */@Overridepublic void close() throws IOException {randomAccessFile.close();}- /**- * @see ghidra.app.util.bin.ByteProvider#length()- */@Overridepublic long length() throws IOException {- return randomAccessFile.length();+ if (cachedLength == null) {+ cachedLength = randomAccessFile.length();+ }+ return cachedLength;}@Overridepublic boolean isValidIndex(long index) {try {- return index >= 0 && index < randomAccessFile.length();+ return index >= 0 && index < length();}catch (IOException e) {+ return false;}- return false;}- /**- * @see ghidra.app.util.bin.ByteProvider#readByte(long)- */@Overridepublic byte readByte(long index) throws IOException {randomAccessFile.seek(index);return randomAccessFile.readByte();}- /**- * @see ghidra.app.util.bin.ByteProvider#readBytes(long, long)- */@Overridepublic byte[] readBytes(long index, long length) throws IOException {randomAccessFile.seek(index);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/SynchronizedByteProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/SynchronizedByteProvider.javaindex 505ce87ec9..3d36e431c4 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/SynchronizedByteProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/SynchronizedByteProvider.java@@ -15,13 +15,21 @@*/package ghidra.app.util.bin;-import ghidra.formats.gfilesystem.FSRL;-import java.io.*;+import ghidra.formats.gfilesystem.FSRL;++/**+ * Creates a thread-safe pass-through {@link ByteProvider}.+ */public class SynchronizedByteProvider implements ByteProvider {private final ByteProvider provider;+ /**+ * Constructs a {@link SynchronizedByteProvider} around the specified {@link ByteProvider}+ *+ * @param provider the {@link ByteProvider} to make thread-safe+ */public SynchronizedByteProvider(ByteProvider provider) {this.provider = provider;}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/bplist/ImmutableMemoryRangeByteProvider.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/bplist/ImmutableMemoryRangeByteProvider.javaindex bb1f8c9856..f1981eec18 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/bplist/ImmutableMemoryRangeByteProvider.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/bplist/ImmutableMemoryRangeByteProvider.java@@ -15,13 +15,13 @@*/package ghidra.file.formats.bplist;+import java.io.*;+import ghidra.app.util.bin.ByteProvider;import ghidra.program.model.address.Address;import ghidra.program.model.mem.Memory;import ghidra.program.model.mem.MemoryBlock;-import java.io.*;-class ImmutableMemoryRangeByteProvider implements ByteProvider {private Memory memory;@@ -66,6 +66,7 @@ class ImmutableMemoryRangeByteProvider implements ByteProvider {@Overridepublic void close() throws IOException {+ // nothing to close}@Override