Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-0: Adding a ProgramLoader.Builder.loaders(String) method

Ryan Kurtz60d0883b1087
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/ProgramLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/ProgramLoader.java
index a02fea2712..14822a96ba 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/ProgramLoader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/ProgramLoader.java
@@ -32,8 +32,7 @@ import ghidra.program.model.lang.*;
import ghidra.program.model.listing.Program;
import ghidra.program.util.DefaultLanguageService;
import ghidra.util.Msg;
-import ghidra.util.exception.CancelledException;
-import ghidra.util.exception.VersionException;
+import ghidra.util.exception.*;
import ghidra.util.task.TaskMonitor;
/**
@@ -234,6 +233,25 @@ public class ProgramLoader {
cls != null ? loader -> loader.getClass().equals(cls) : LoaderService.ACCEPT_ALL;
return this;
}
+
+ /**
+ * Sets the acceptable {@link Loader} to use during import.
+ * <p>
+ * By default, all {@link Loader}s are accepted ({@link LoaderService#ACCEPT_ALL}).
+ *
+ * @param clsName The class name of the {@link Loader} to use during import. A {@code null}
+ * value will revert back to the default ({@link LoaderService#ACCEPT_ALL}).
+ * @return This {@link Builder}
+ * @throws InvalidInputException if the given loader class name did not correspond to a
+ * {@link Loader}
+ */
+ public Builder loaders(String clsName) throws InvalidInputException {
+ Class<? extends Loader> cls = LoaderService.getLoaderClassByName(clsName);
+ if (cls == null) {
+ throw new InvalidInputException("Loader '%s' does not exist!".formatted(clsName));
+ }
+ return loaders(cls);
+ }
/**
* Sets the {@link Loader}s to use during import.
@@ -436,8 +454,8 @@ public class ProgramLoader {
LoadSpec loadSpec = getLoadSpec(p);
List<Option> loaderOptions = getLoaderOptions(p, loadSpec);
- String importName = Objects.requireNonNullElse(importNameOverride,
- loadSpec.getLoader().getPreferredFileName(p));
+ String importName = importNameOverride != null ? importNameOverride
+ : loadSpec.getLoader().getPreferredFileName(p);
// Load
Msg.info(ProgramLoader.class, "Using Loader: " + loadSpec.getLoader().getName());
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Loader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Loader.java
index e47a95e951..33b4affd15 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Loader.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/Loader.java
@@ -201,12 +201,13 @@ public interface Loader extends ExtensionPoint, Comparable<Loader> {
* if absolutely necessary.
*
* @param provider The bytes to load.
- * @return The preferred file name to use when loading.
+ * @return The preferred file name to use when loading, or {@code null} if a name could not
+ * be determined from the provider.
*/
public default String getPreferredFileName(ByteProvider provider) {
FSRL fsrl = provider.getFSRL();
String name = (fsrl != null) ? fsrl.getName() : provider.getName();
- return name.replaceAll("[\\\\:|]+", "/");
+ return name != null ? name.replaceAll("[\\\\:|]+", "/") : null;
}
/**
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoaderService.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoaderService.java
index b17077309f..e5a05f449b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoaderService.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/LoaderService.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.
@@ -92,7 +92,7 @@ public class LoaderService {
*
* @param name The name of the {@link Loader} to get the {@link Class} of.
* @return The {@link Loader} {@link Class} that corresponds to the given simple {@link Class}
- * name.
+ * name, or {@code null} if it does not exist.
*/
public static Class<? extends Loader> getLoaderClassByName(String name) {
return getAllLoaders()