Ghidra is a software reverse engineering (SRE) framework
fix comments and names per review suggestions
Jason P. Leasuredab1a33e5b3d
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.javaindex 936ac030c9..ac33aec14a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java@@ -195,19 +195,9 @@ public class BundleHost {return bundle;}- /**- * Create new GhidraBundles and add to the list of managed bundles. All GhidraBundles created- * with the same {@code enabled} and {@code systemBundle} values.- *- * @param bundleFileList a list of bundle files- * @param enabled if the new bundle should be enabled- * @param systemBundle if the new bundle is a system bundle- * @return the new bundle objects- */- public Collection<GhidraBundle> add(List<ResourceFile> bundleFileList, boolean enabled,- boolean systemBundle) {- Set<ResourceFile> bundleFiles = new HashSet<>(bundleFileList);- Iterator<ResourceFile> bundleFileIterator = bundleFiles.iterator();+ private Set<ResourceFile> dedupeBundleFiles(List<ResourceFile> bundleFiles) {+ Set<ResourceFile> dedupedBundleFiles = new HashSet<>(bundleFiles);+ Iterator<ResourceFile> bundleFileIterator = dedupedBundleFiles.iterator();while (bundleFileIterator.hasNext()) {ResourceFile bundleFile = bundleFileIterator.next();if (fileToBundleMap.containsKey(bundleFile)) {@@ -215,7 +205,22 @@ public class BundleHost {Msg.warn(this, "adding an already managed bundle: " + bundleFile.getAbsolutePath());}}- Map<ResourceFile, GhidraBundle> newBundleMap = bundleFiles.stream()+ return dedupedBundleFiles;+ }++ /**+ * Create new GhidraBundles and add to the list of managed bundles. All GhidraBundles created+ * with the same {@code enabled} and {@code systemBundle} values.+ *+ * @param bundleFiles a list of new bundle files to add+ * @param enabled if the new bundles should be enabled+ * @param systemBundle if the new bundles are system bundles+ * @return the new bundle objects+ */+ public Collection<GhidraBundle> add(List<ResourceFile> bundleFiles, boolean enabled,+ boolean systemBundle) {+ Set<ResourceFile> dedupedBundleFiles = dedupeBundleFiles(bundleFiles);+ Map<ResourceFile, GhidraBundle> newBundleMap = dedupedBundleFiles.stream().collect(Collectors.toUnmodifiableMap(Function.identity(),bundleFile -> createGhidraBundle(BundleHost.this, bundleFile, enabled,systemBundle)));@@ -957,10 +962,10 @@ public class BundleHost {newFront.entrySet().iterator();while (newFrontIter.hasNext() && !monitor.isCancelled()) {Entry<GhidraBundle, Set<GhidraBundle>> entry = newFrontIter.next();- GhidraBundle src = entry.getKey();- if (containsVertex(src)) {- for (GhidraBundle dst : entry.getValue()) {- addEdge(src, dst, new Dependency());+ GhidraBundle source = entry.getKey();+ if (containsVertex(source)) {+ for (GhidraBundle destination : entry.getValue()) {+ addEdge(source, destination, new Dependency());}newFrontIter.remove();}@@ -969,44 +974,44 @@ public class BundleHost {void addFront(Map<GhidraBundle, Set<GhidraBundle>> front) {for (Entry<GhidraBundle, Set<GhidraBundle>> e : front.entrySet()) {- GhidraBundle src = e.getKey();- availableBundles.add(src);- addVertex(src);- Set<GhidraBundle> dsts = e.getValue();- if (dsts != null) {- for (GhidraBundle dst : dsts) {- addEdge(src, dst, new Dependency());+ GhidraBundle source = e.getKey();+ availableBundles.add(source);+ addVertex(source);+ Set<GhidraBundle> destinations = e.getValue();+ if (destinations != null) {+ for (GhidraBundle destination : destinations) {+ addEdge(source, destination, new Dependency());}}}}void resolve(GhidraBundle bundle, Map<GhidraBundle, Set<GhidraBundle>> newFront) {- List<BundleRequirement> reqs;+ List<BundleRequirement> requirements;try {- reqs = new ArrayList<>(bundle.getAllRequirements());+ requirements = new ArrayList<>(bundle.getAllRequirements());}catch (GhidraBundleException e) {throw new RuntimeException(e);}- if (reqs.isEmpty()) {+ if (requirements.isEmpty()) {return;}for (GhidraBundle depBundle : availableBundles) {- for (BundleCapability cap : getCapabilities(depBundle)) {+ for (BundleCapability capability : getCapabilities(depBundle)) {if (monitor.isCancelled()) {return;}- Iterator<BundleRequirement> reqIter = reqs.iterator();+ Iterator<BundleRequirement> reqIter = requirements.iterator();while (reqIter.hasNext()) {BundleRequirement req = reqIter.next();- if (req.matches(cap)) {+ if (req.matches(capability)) {newFront.computeIfAbsent(depBundle, b -> new HashSet<>()).add(bundle);reqIter.remove();}}- if (reqs.isEmpty()) {+ if (requirements.isEmpty()) {return;}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.javaindex c357091d3d..9c2fd5d4cd 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/OSGiUtils.java@@ -42,24 +42,34 @@ public class OSGiUtils {*/private static Pattern JAR_FILENAME_EXTRACTOR = Pattern.compile("^.*:(.*)!.*$");+ /*+ * Match group 1 contains the name of the Java package from an OSGi resolution+ * error message. If present, match group 2 will be the version constraint.+ *+ * e.g. for the requirement+ * (&(osgi.wiring.package=x.y.z)(version>=1.2.3))+ *+ * PACKAGE_NAME_EXTRACTOR will match with group 1 "x.y.z" and group 2 "(version>=1.2.3)".+ *+ */+ private static Pattern PACKAGE_NAME_EXTRACTOR =+ Pattern.compile("\\(osgi\\.wiring\\.package=([^)]*)\\)(\\(version[^)]*\\))?");+/*** The syntax of the error generated when OSGi requirements cannot be resolved is- * difficult to parse, so we try to extract package names.+ * difficult to parse, so we try to extract package name and versions.** @param osgiExceptionMessage the exception message- * @return a list of package names+ * @return a list of package names, possibly including versions*/static List<String> extractPackageNamesFromFailedResolution(String osgiExceptionMessage) {try (Scanner s = new Scanner(osgiExceptionMessage)) {- return s.findAll(- Pattern.compile("\\(osgi\\.wiring\\.package=([^)]*)\\)(\\(version[^)]*\\))?"))- .map(m -> {- if (m.group(2) != null) {- return m.group(1) + " " + m.group(2);- }- return m.group(1);- })- .collect(Collectors.toList());+ return s.findAll(PACKAGE_NAME_EXTRACTOR).map(m -> {+ if (m.group(2) != null) {+ return m.group(1) + " " + m.group(2);+ }+ return m.group(1);+ }).collect(Collectors.toList());}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.javaindex b0b1fd3f9d..45e5244cc7 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptActionManager.java@@ -394,14 +394,14 @@ class GhidraScriptActionManager {}File versionedExtractDir = new File(extractDir, version);- try (ZipFile zf = new ZipFile(zipFile)) {+ try (ZipFile zipFileObject = new ZipFile(zipFile)) {// Check to see if zip file has been extracted alreadyif (versionedExtractDir.exists()) {// Open Javadoc if all the files are presenttry (Stream<Path> walk = Files.walk(versionedExtractDir.toPath())) {- if (zf.size() + 1 == walk.count()) {+ if (zipFileObject.size() + 1 == walk.count()) {launchJavadoc();return;}@@ -412,9 +412,9 @@ class GhidraScriptActionManager {}monitor.setMessage("Preparing to extract Ghidra API javadoc...");- monitor.initialize(zf.size());+ monitor.initialize(zipFileObject.size());- Enumeration<? extends ZipEntry> entries = zf.entries();+ Enumeration<? extends ZipEntry> entries = zipFileObject.entries();while (entries.hasMoreElements()) {if (monitor.isCancelled()) {cleanup(monitor, versionedExtractDir);@@ -423,7 +423,7 @@ class GhidraScriptActionManager {ZipEntry entry = entries.nextElement();monitor.setMessage("Extracting " + entry.getName() + "...");- writeZipEntry(versionedExtractDir, entry, zf.getInputStream(entry));+ writeZipEntry(versionedExtractDir, entry, zipFileObject.getInputStream(entry));monitor.incrementProgress(1);}