diff --git a/Ghidra/Features/Base/ghidra_scripts/SplitMultiplePefContainersScript.java b/Ghidra/Features/Base/ghidra_scripts/SplitMultiplePefContainersScript.java index da22b575ac..8d6812ec41 100644 --- a/Ghidra/Features/Base/ghidra_scripts/SplitMultiplePefContainersScript.java +++ b/Ghidra/Features/Base/ghidra_scripts/SplitMultiplePefContainersScript.java @@ -1,13 +1,12 @@ /* ### * IP: GHIDRA - * REVIEWED: YES * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,9 +19,13 @@ //name defined in the AppleSingleDouble (.) file. //@category Binary +import java.io.*; +import java.nio.file.AccessMode; +import java.util.List; + import ghidra.app.script.GhidraScript; import ghidra.app.util.bin.ByteProvider; -import ghidra.app.util.bin.RandomAccessByteProvider; +import ghidra.app.util.bin.FileByteProvider; import ghidra.app.util.bin.format.macos.asd.*; import ghidra.app.util.bin.format.macos.cfm.CFragResource; import ghidra.app.util.bin.format.macos.cfm.CFragResourceMember; @@ -31,9 +34,6 @@ import ghidra.framework.OperatingSystem; import ghidra.framework.Platform; import ghidra.util.Msg; -import java.io.*; -import java.util.List; - public class SplitMultiplePefContainersScript extends GhidraScript { private static final int BUFFER = 4096; @@ -50,8 +50,8 @@ public class SplitMultiplePefContainersScript extends GhidraScript { return; } - RandomAccessByteProvider pefProvider = open(pefFile); - RandomAccessByteProvider resourceForkProvider = open(resourceForkFile); + ByteProvider pefProvider = open(pefFile); + ByteProvider resourceForkProvider = open(resourceForkFile); try { ResourceHeader resourceHeader = findResourceFork(resourceForkProvider); @@ -131,8 +131,7 @@ public class SplitMultiplePefContainersScript extends GhidraScript { return null; } - private ResourceHeader findResourceFork(RandomAccessByteProvider resourceForkProvider) - throws Exception { + private ResourceHeader findResourceFork(ByteProvider resourceForkProvider) throws Exception { if (isRunningOnMac()) { return new ResourceHeader(resourceForkProvider); } @@ -148,9 +147,9 @@ public class SplitMultiplePefContainersScript extends GhidraScript { return null; } - private RandomAccessByteProvider open(File file) { + private ByteProvider open(File file) { try { - return new RandomAccessByteProvider(file); + return new FileByteProvider(file, null, AccessMode.READ); } catch (IOException e) { Msg.error(this, "Unexpected Exception: " + e.getMessage(), e); @@ -158,7 +157,7 @@ public class SplitMultiplePefContainersScript extends GhidraScript { return null; } - private void close(RandomAccessByteProvider provider) { + private void close(ByteProvider provider) { try { if (provider != null) { provider.close(); diff --git a/Ghidra/Features/Base/ghidra_scripts/SplitUniversalBinariesScript.java b/Ghidra/Features/Base/ghidra_scripts/SplitUniversalBinariesScript.java index e32b339a78..dcb004edb5 100644 --- a/Ghidra/Features/Base/ghidra_scripts/SplitUniversalBinariesScript.java +++ b/Ghidra/Features/Base/ghidra_scripts/SplitUniversalBinariesScript.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,11 +19,12 @@ //@category Binary import java.io.*; +import java.nio.file.AccessMode; import java.util.List; import ghidra.app.script.GhidraScript; import ghidra.app.util.bin.ByteProvider; -import ghidra.app.util.bin.RandomAccessByteProvider; +import ghidra.app.util.bin.FileByteProvider; import ghidra.app.util.bin.format.macho.CpuTypes; import ghidra.app.util.bin.format.ubi.FatArch; import ghidra.app.util.bin.format.ubi.FatHeader; @@ -37,7 +38,7 @@ public class SplitUniversalBinariesScript extends GhidraScript { File ubiFile = askFile("Select Universal Binary File", "C'mon, Do it! Push da bahtahn!"); File outputDirectory = askDirectory("Select Output Directory", "GO"); - ByteProvider provider = new RandomAccessByteProvider(ubiFile) ; + ByteProvider provider = new FileByteProvider(ubiFile, null, AccessMode.READ); FatHeader header = new FatHeader(provider); List architectures = header.getArchitectures(); 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.java deleted file mode 100644 index 9a13c7948a..0000000000 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessByteProvider.java +++ /dev/null @@ -1,164 +0,0 @@ -/* ### - * IP: GHIDRA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package ghidra.app.util.bin; - -import java.io.*; - -import ghidra.formats.gfilesystem.FSRL; -import ghidra.formats.gfilesystem.FileSystemService; -import ghidra.util.Msg; - -/** - * An implementation of ByteProvider where the underlying - * bytes are supplied by a random access file. - *

- * Note: this implementation is not thread-safe, and using an instance of this - * class from multiple threads will result in reading incorrect data and/or - * {@link ArrayIndexOutOfBoundsException}s. - *

- * See {@link SynchronizedByteProvider} as a solution. - * - * @deprecated See {@link FileByteProvider} as replacement ByteProvider. - */ -@Deprecated(since = "10.1", forRemoval = true) -public class RandomAccessByteProvider implements ByteProvider { - protected File file; - protected GhidraRandomAccessFile randomAccessFile; - private FSRL fsrl; - private long fileLength; - - /** - * Constructs a {@link ByteProvider} using the specified {@link File}. - * - * @param file the {@link File} to open for random access - * @throws IOException if the {@link File} does not exist or other error - */ - public RandomAccessByteProvider(File file) throws IOException { - this(file, "r"); - } - - /** - * Constructs a {@link ByteProvider} using the specified {@link File} and {@link FSRL} - * - * @param file the {@link File} to open for random access - * @param fsrl the {@link FSRL} to use for the {@link File}'s path - * @throws IOException if the {@link File} does not exist or other error - */ - public RandomAccessByteProvider(File file, FSRL fsrl) throws IOException { - this(file, fsrl, "r"); - } - - /** - * 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 IOException if the {@link File} does not exist or other error - */ - public RandomAccessByteProvider(File file, String permissions) throws IOException { - this(file, FileSystemService.getInstance().getLocalFSRL(file), permissions); - } - - private RandomAccessByteProvider(File file, FSRL fsrl, String permissions) throws IOException { - this.file = file; - this.fsrl = fsrl; - this.randomAccessFile = new GhidraRandomAccessFile(file, permissions); - this.fileLength = randomAccessFile.length(); - } - - @Override - public FSRL getFSRL() { - return fsrl; - } - - /** - * Sets the {@link FSRL} of this {@link ByteProvider} - * - * @param fsrl the {@link FSRL} to assign to this byte provider - */ - public void setFSRL(FSRL fsrl) { - this.fsrl = fsrl; - } - - @Override - public File getFile() { - return file; - } - - @Override - public String getName() { - return fsrl == null ? file.getName() : fsrl.getName(); - } - - @Override - public String getAbsolutePath() { - return fsrl == null ? file.getAbsolutePath() : fsrl.getPath(); - } - - @Override - public InputStream getInputStream(long index) throws IOException { - FileInputStream is = new FileInputStream(file); - is.skip(index); - return is; - } - - @Override - public void close() throws IOException { - randomAccessFile.close(); - } - - @Override - public long length() { - return fileLength; - } - - @Override - public boolean isValidIndex(long index) { - return 0 <= index && index < fileLength; - } - - @Override - public byte readByte(long index) throws IOException { - randomAccessFile.seek(index); - return randomAccessFile.readByte(); - } - - @Override - public byte[] readBytes(long index, long length) throws IOException { - byte[] b = new byte[(int) length]; - if (index > fileLength) { - throw new EOFException( - "Invalid file offset " + index + " while reading " + file.getName()); - } - if (index + length > fileLength) { - Msg.trace(this, "Read at EOF, can't return partial buffer, throwing IOException: " + - file.getName()); - throw new EOFException("EOF: unable to read " + length + " bytes at " + index); - } - randomAccessFile.seek(index); - int nRead = randomAccessFile.read(b); - if (nRead != length) { - throw new IOException("Unable to read " + length + " bytes"); - } - return b; - } - - @Override - public String toString() { - return "RandomAccessByteProvider [\n file=" + file + ",\n fsrl=" + fsrl + - ",\n fileLength=" + fileLength + "\n]"; - } -} diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessMutableByteProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessMutableByteProvider.java deleted file mode 100644 index dbda8fc4ca..0000000000 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/RandomAccessMutableByteProvider.java +++ /dev/null @@ -1,56 +0,0 @@ -/* ### - * IP: GHIDRA - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package ghidra.app.util.bin; - -import java.io.*; - -/** - * An implementation of ByteProvider where the underlying - * bytes are supplied by a random access file. - */ -public class RandomAccessMutableByteProvider extends RandomAccessByteProvider implements - MutableByteProvider { - /** - * 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 - */ - public RandomAccessMutableByteProvider(File file) throws IOException { - super(file); - } - - /** - * Constructs a byte provider using the specified file and permissions string - * @param file the file to open for random access - * @param permissions indicating permissions used for open - * @throws FileNotFoundException if the file does not exist - */ - public RandomAccessMutableByteProvider(File file, String permissions) throws IOException { - super(file, permissions); - } - - @Override - public void writeByte(long index, byte value) throws IOException { - randomAccessFile.seek(index); - randomAccessFile.write(value); - } - - @Override - public void writeBytes(long index, byte[] values) throws IOException { - randomAccessFile.seek(index); - randomAccessFile.write(values); - } -} diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java index 6a4456cba4..054379362d 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java @@ -549,7 +549,7 @@ public class FileSystemService { if (provider instanceof RefdByteProvider) { provider = ((RefdByteProvider) provider).getWrappedByteProvider(); } - if (provider instanceof FileByteProvider || provider instanceof RandomAccessByteProvider) { + if (provider instanceof FileByteProvider) { return provider.getFile(); } diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/examiner/ProgramExaminer.java b/Ghidra/Features/Base/src/main/java/ghidra/program/examiner/ProgramExaminer.java index 5ecc6fe97f..7d7d65bf53 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/program/examiner/ProgramExaminer.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/program/examiner/ProgramExaminer.java @@ -17,6 +17,7 @@ package ghidra.program.examiner; import java.io.File; import java.io.IOException; +import java.nio.file.AccessMode; import java.util.ArrayList; import java.util.List; @@ -234,7 +235,7 @@ public class ProgramExaminer { throw new GhidraException("Attempted to process a null file"); } try { - return new RandomAccessByteProvider(file); + return new FileByteProvider(file, null, AccessMode.READ); } catch (IOException e) { throw new GhidraException(e); diff --git a/Ghidra/Features/FileFormats/ghidra_scripts/MachoProcessBindScript.java b/Ghidra/Features/FileFormats/ghidra_scripts/MachoProcessBindScript.java index 0eca920266..06271b1baa 100644 --- a/Ghidra/Features/FileFormats/ghidra_scripts/MachoProcessBindScript.java +++ b/Ghidra/Features/FileFormats/ghidra_scripts/MachoProcessBindScript.java @@ -18,11 +18,12 @@ import java.io.ByteArrayInputStream; import java.io.File; +import java.nio.file.AccessMode; import java.util.List; import ghidra.app.script.GhidraScript; import ghidra.app.util.bin.ByteProvider; -import ghidra.app.util.bin.RandomAccessByteProvider; +import ghidra.app.util.bin.FileByteProvider; import ghidra.app.util.bin.format.macho.MachHeader; import ghidra.app.util.bin.format.macho.Section; import ghidra.app.util.bin.format.macho.commands.*; @@ -48,7 +49,7 @@ public class MachoProcessBindScript extends GhidraScript { popup("Cannot find original binary at \n" + file.getAbsolutePath()); return; } - ByteProvider provider = new RandomAccessByteProvider(file); + ByteProvider provider = new FileByteProvider(file, null, AccessMode.READ); try { MachHeader header = new MachHeader(provider); header.parse(); diff --git a/Ghidra/Features/FileFormats/ghidra_scripts/SplitExtensibleFirmwareInterfaceScript.java b/Ghidra/Features/FileFormats/ghidra_scripts/SplitExtensibleFirmwareInterfaceScript.java index ca843c40c9..4f5288d946 100644 --- a/Ghidra/Features/FileFormats/ghidra_scripts/SplitExtensibleFirmwareInterfaceScript.java +++ b/Ghidra/Features/FileFormats/ghidra_scripts/SplitExtensibleFirmwareInterfaceScript.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -19,6 +19,7 @@ //@category Binary import java.io.*; +import java.nio.file.AccessMode; import ghidra.app.script.GhidraScript; import ghidra.app.util.bin.*; @@ -38,7 +39,7 @@ public class SplitExtensibleFirmwareInterfaceScript extends GhidraScript { File directory = askDirectory("Select Output Directory for Parsed EFI", "OK"); - ByteProvider provider = new RandomAccessByteProvider(efiFile); + ByteProvider provider = new FileByteProvider(efiFile, null, AccessMode.READ); try { BinaryReader reader = new BinaryReader(provider, true); diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dmg/DmgDecryptorStream.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dmg/DmgDecryptorStream.java index 80db50b49e..cd9daee78f 100644 --- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dmg/DmgDecryptorStream.java +++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/ios/dmg/DmgDecryptorStream.java @@ -16,6 +16,7 @@ package ghidra.file.formats.ios.dmg; import java.io.*; +import java.nio.file.AccessMode; import java.util.Arrays; import ghidra.app.util.bin.*; @@ -57,7 +58,7 @@ public class DmgDecryptorStream extends InputStream { */ public DmgDecryptorStream(String containerName, String dmgName, File srcFile) throws IOException { - this(containerName, dmgName, new RandomAccessByteProvider(srcFile)); + this(containerName, dmgName, new FileByteProvider(srcFile, null, AccessMode.READ)); } /** diff --git a/Ghidra/Features/FunctionID/ghidra_scripts/ImportMSLibs.java b/Ghidra/Features/FunctionID/ghidra_scripts/ImportMSLibs.java index 1954efda4e..8b26a8261b 100644 --- a/Ghidra/Features/FunctionID/ghidra_scripts/ImportMSLibs.java +++ b/Ghidra/Features/FunctionID/ghidra_scripts/ImportMSLibs.java @@ -17,6 +17,7 @@ //@category FunctionID import java.io.File; import java.io.IOException; +import java.nio.file.AccessMode; import java.util.ArrayList; import java.util.HashSet; @@ -33,7 +34,8 @@ import ghidra.framework.store.local.LocalFileSystem; import ghidra.program.model.lang.LanguageDescription; import ghidra.program.model.listing.Program; import ghidra.util.InvalidNameException; -import ghidra.util.exception.*; +import ghidra.util.exception.CancelledException; +import ghidra.util.exception.VersionException; import ghidra.util.task.CancelOnlyWrappingTaskMonitor; import ghidra.util.task.TaskMonitor; @@ -79,9 +81,8 @@ public class ImportMSLibs extends GhidraScript { } private void importLibrary(DomainFolder root, File file, boolean isDebug, MessageLog log) - throws CancelledException, DuplicateNameException, InvalidNameException, - VersionException, IOException { - try (RandomAccessByteProvider provider = new RandomAccessByteProvider(file) ) { + throws CancelledException, InvalidNameException, VersionException, IOException { + try (ByteProvider provider = new FileByteProvider(file, null, AccessMode.READ)) { if ( !CoffArchiveHeader.isMatch(provider)) { return; } CoffArchiveHeader coffArchiveHeader = CoffArchiveHeader.read(provider, TaskMonitor.DUMMY); diff --git a/Ghidra/Features/FunctionID/ghidra_scripts/MSLibBatchImportWorker.java b/Ghidra/Features/FunctionID/ghidra_scripts/MSLibBatchImportWorker.java index b6e2dd2ba9..e1d4646876 100644 --- a/Ghidra/Features/FunctionID/ghidra_scripts/MSLibBatchImportWorker.java +++ b/Ghidra/Features/FunctionID/ghidra_scripts/MSLibBatchImportWorker.java @@ -52,6 +52,7 @@ import java.io.File; import java.io.IOException; import java.lang.management.ManagementFactory; +import java.nio.file.AccessMode; import java.util.*; import org.apache.commons.io.FileUtils; @@ -173,7 +174,7 @@ public class MSLibBatchImportWorker extends GhidraScript { private void importLibrary(DomainFolder currentLibraryFolder, File file, MessageLog log) throws CancelledException, InvalidNameException, VersionException, IOException { - try (RandomAccessByteProvider provider = new RandomAccessByteProvider(file)) { + try (ByteProvider provider = new FileByteProvider(file, null, AccessMode.READ)) { if (!CoffArchiveHeader.isMatch(provider)) { return; } diff --git a/Ghidra/Features/FunctionID/ghidra_scripts/RecursiveRecursiveMSLibImport.java b/Ghidra/Features/FunctionID/ghidra_scripts/RecursiveRecursiveMSLibImport.java index e597ddde27..4a6510efad 100644 --- a/Ghidra/Features/FunctionID/ghidra_scripts/RecursiveRecursiveMSLibImport.java +++ b/Ghidra/Features/FunctionID/ghidra_scripts/RecursiveRecursiveMSLibImport.java @@ -15,6 +15,7 @@ */ import java.io.File; import java.io.IOException; +import java.nio.file.AccessMode; import java.util.ArrayList; import java.util.HashSet; @@ -32,7 +33,8 @@ import ghidra.framework.model.DomainFolder; import ghidra.framework.model.DomainObject; import ghidra.framework.store.local.LocalFileSystem; import ghidra.util.InvalidNameException; -import ghidra.util.exception.*; +import ghidra.util.exception.CancelledException; +import ghidra.util.exception.VersionException; import ghidra.util.task.TaskMonitor; public class RecursiveRecursiveMSLibImport extends GhidraScript { @@ -126,10 +128,9 @@ public class RecursiveRecursiveMSLibImport extends GhidraScript { } private void importLibrary(DomainFolder currentLibrary, File file, MessageLog log) - throws CancelledException, DuplicateNameException, InvalidNameException, - VersionException, IOException { + throws CancelledException, InvalidNameException, VersionException, IOException { - try (RandomAccessByteProvider provider = new RandomAccessByteProvider(file)) { + try (ByteProvider provider = new FileByteProvider(file, null, AccessMode.READ)) { if (!CoffArchiveHeader.isMatch(provider)) { return; } diff --git a/Ghidra/Processors/JVM/src/test/java/ghidra/app/util/pcodeInject/TestClassFileCreator.java b/Ghidra/Processors/JVM/src/test/java/ghidra/app/util/pcodeInject/TestClassFileCreator.java index 5bea7849f8..f53bcfdaa5 100644 --- a/Ghidra/Processors/JVM/src/test/java/ghidra/app/util/pcodeInject/TestClassFileCreator.java +++ b/Ghidra/Processors/JVM/src/test/java/ghidra/app/util/pcodeInject/TestClassFileCreator.java @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,6 +17,7 @@ package ghidra.app.util.pcodeInject; import java.io.File; import java.io.IOException; +import java.nio.file.AccessMode; import java.util.ArrayList; import generic.jar.ResourceFile; @@ -57,7 +58,7 @@ public class TestClassFileCreator { ResourceFile moduleRoot = Application.getMyModuleRootDirectory(); File testFile = new File(moduleRoot.getAbsolutePath() + RESOURCE_DIRECTORY + testFileName); - ByteProvider provider = new RandomAccessByteProvider(testFile); + ByteProvider provider = new FileByteProvider(testFile, null, AccessMode.READ); return getConstantPoolFromByteProvider(provider); }