ghidra/Ghidra/Features/Base/ghidra_scripts/SynchronizeGDTCategoryPaths.java
emteere 832dea6b6d GP-1551_emteere updated gdt archive files with new cparser with fixes
for sizeof() evaluation and better expression simplification for
calculated values in enums and array sizes.
2021-12-09 11:10:06 -05:00

98 lines
3.0 KiB
Java

/* ###
* 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.
*/
//
// Synchronize the category path name case from the first archive into the second
// archive.
//
//@category Data Types
import java.io.File;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.data.Category;
import ghidra.program.model.data.FileDataTypeManager;
import ghidra.util.InvalidNameException;
import ghidra.util.exception.DuplicateNameException;
public class SynchronizeGDTCategoryPaths extends GhidraScript {
private File firstFile;
private File secondFile;
private FileDataTypeManager firstArchive;
private FileDataTypeManager secondArchive;
@Override
protected void run() throws Exception {
firstFile = askFile("Select First GDT File", "Select 1st");
secondFile = askFile("Select Second GDT File", "Select 2nd");
try {
firstArchive = FileDataTypeManager.openFileArchive(firstFile, false);
secondArchive = FileDataTypeManager.openFileArchive(secondFile, true);
int transactionID = secondArchive.startTransaction("Synchronize Category Path Names");
Category firstCategory = firstArchive.getRootCategory();
Category secondCategory = secondArchive.getRootCategory();
synchronizeCategory(firstCategory, secondCategory);
secondArchive.endTransaction(transactionID, true);
}
finally {
if (firstArchive != null) {
firstArchive.close();
}
secondArchive.save();
if (secondArchive != null) {
secondArchive.close();
}
}
}
private void synchronizeCategory(Category firstCategory, Category secondCategory) {
Category[] firstCategories = firstCategory.getCategories();
for (Category categoryA : firstCategories) {
// loop through categories looking for a case agnostic path match
Category[] secondCategories = secondCategory.getCategories();
boolean foundIt = false;
for (Category categoryB : secondCategories) {
if (categoryA.getName().equalsIgnoreCase(categoryB.getName())) {
// if not the exact same name, rename it
if (!categoryA.getName().equals(categoryB.getName())) {
try {
println(
"Renamed " + categoryB.getName() + " to " + categoryA.getName());
categoryB.setName(categoryA.getName());
foundIt = true;
}
catch (DuplicateNameException | InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
foundIt = true;
}
synchronizeCategory(categoryA, categoryB);
}
}
if (!foundIt) {
println("Couldn't find matching category for " + categoryA.getName());
}
}
}
}