Java Code Examples for ghidra.program.model.listing.Program#getDomainFile()

The following examples show how to use ghidra.program.model.listing.Program#getDomainFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExporterPlugin.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void createToolAction() {
	DockingAction action = new NavigatableContextAction("Export Program", getName()) {

		@Override
		protected void actionPerformed(NavigatableActionContext context) {
			Program program = context.getProgram();
			DomainFile domainFile = program.getDomainFile();
			ExporterDialog dialog =
				new ExporterDialog(tool, domainFile, program, context.getSelection());
			tool.showDialog(dialog);
		}
	};
	MenuData menuData =
		new MenuData(new String[] { "&File", "Export Program..." }, "Import Export");
	menuData.setMenuSubGroup("z"); // last in the "Save" group
	action.setMenuBarData(menuData);
	action.setKeyBindingData(new KeyBindingData(KeyEvent.VK_O, 0));
	action.setHelpLocation(new HelpLocation("ExporterPlugin", "Export"));
	action.setDescription(getPluginDescription().getDescription());
	tool.addAction(action);
}
 
Example 2
Source File: ProgramManagerPlugin.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void writeProgramInfo(Program program, SaveState saveState, int index) {
	if (locked) {
		return; // do not save state when locked.
	}
	String projectLocation = null;
	String projectName = null;
	String path = null;
	DomainFile df = program.getDomainFile();
	ProjectLocator projectLocator = df.getProjectLocator();
	if (projectLocator != null && !projectLocator.isTransient()) {
		projectLocation = projectLocator.getLocation();
		projectName = projectLocator.getName();
		path = df.getPathname();
	}
	int version = DomainFile.DEFAULT_VERSION;
	if (!df.isLatestVersion()) {
		version = df.getVersion();
	}

	saveState.putString("LOCATION_" + index, projectLocation);
	saveState.putString("PROJECT_NAME_" + index, projectName);
	saveState.putInt("VERSION_" + index, version);
	saveState.putString("PATHNAME_" + index, path);
}
 
Example 3
Source File: ProgramSaveManager.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void saveAs(Program currentProgram, DomainFolder folder, String name) {
	DomainFile existingFile = folder.getFile(name);
	if (existingFile == currentProgram.getDomainFile()) {
		save(currentProgram);
		return;
	}
	if (existingFile != null) {
		String msg = "Program " + name + " already exists.\n" + "Do you want to overwrite it?";
		if (OptionDialog.showOptionDialog(tool.getToolFrame(), "Duplicate Name", msg,
			"Overwrite", OptionDialog.QUESTION_MESSAGE) == OptionDialog.CANCEL_OPTION) {
			return;
		}
	}
	tool.prepareToSave(currentProgram);
	SaveAsTask task = new SaveAsTask(currentProgram, folder, name, existingFile != null);
	new TaskLauncher(task, tool.getToolFrame());
}
 
Example 4
Source File: SaveVersionTrackingSessionAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionContext context) {
	VTSession session = controller.getSession();
	if (session == null) {
		return;
	}

	if (session instanceof VTSessionDB) {
		VTSessionDB sessionDB = (VTSessionDB) session;
		DomainFile vtDomainFile = sessionDB.getDomainFile();

		// Save version tracking session changes.
		SaveTask saveVersionTrackingTask = new SaveTask(vtDomainFile);
		TaskLauncher.launch(saveVersionTrackingTask);
		Program program = controller.getDestinationProgram();
		DomainFile destinationProgramFile = program.getDomainFile();
		if (destinationProgramFile.isChanged()) {
			SaveTask saveDestinationTask = new SaveTask(destinationProgramFile);
			TaskLauncher.launch(saveDestinationTask);
		}

		controller.refresh();
	}
}
 
Example 5
Source File: AddToProgramDialog.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getFolderName(Program program) {
	DomainFile domainFile = program.getDomainFile();
	DomainFolder parent = domainFile.getParent();
	if (parent == null) {
		return "";
	}
	return parent.toString();
}
 
Example 6
Source File: MyProgramChangesDisplayPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void scheduleUpdatesFromServer(Program p) {
	// ensure we never have more than one pending job
	worker.clearPendingJobs();

	DomainFile file = p.getDomainFile();
	worker.schedule(new UpdateChangeSetJob(file));
}
 
Example 7
Source File: MultiProgramManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public Program getOpenProgram(DomainFile domainFile, int version) {
	for (Program program : programMap.keySet()) {
		DomainFile programDomainFile = program.getDomainFile();
		if (filesMatch(domainFile, version, programDomainFile)) {
			return program;
		}
	}
	return null;
}
 
Example 8
Source File: MultiTabPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
String getStringUsedInList(Program program) {
	DomainFile df = program.getDomainFile();
	String changeIndicator = program.isChanged() ? "*" : "";
	if (!df.isInWritableProject()) {
		return df.toString() + " [Read-Only]" + changeIndicator;
	}
	return df.toString() + changeIndicator;
}
 
Example 9
Source File: MultiTabPlugin.java    From ghidra with Apache License 2.0 5 votes vote down vote up
String getName(Program program) {
	DomainFile df = program.getDomainFile();
	String tabName = df.getName();
	if (df.isReadOnly()) {
		int version = df.getVersion();
		if (!df.canSave() && version != DomainFile.DEFAULT_VERSION) {
			tabName += "@" + version;
		}
		tabName = tabName + " [Read-Only]";
	}
	return tabName;
}
 
Example 10
Source File: ProgramSaveManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Prompt user for programs to be saved.
 * Caller must already have lock on all programs contained within the list.
 * @return true if it is safe to close tool
 */
private boolean saveChangedPrograms(List<Program> openProgramList) {
	SaveDataDialog saveDataDialog = new SaveDataDialog(tool);

	// don't modify the original list, as it is used by the caller to perform cleanup
	List<Program> saveProgramsList = new ArrayList<>(openProgramList);

	// make sure we have some files to save
	List<DomainFile> domainFilesToSaveList = new ArrayList<>();
	Iterator<Program> iter = saveProgramsList.iterator();
	while (iter.hasNext()) {
		Program program = iter.next();
		DomainFile domainFile = program.getDomainFile();
		if (!domainFile.isChanged()) {
			iter.remove();
		}
		else {
			domainFilesToSaveList.add(domainFile);
		}
	}

	if (saveProgramsList.size() == 0) {
		return true;
	}
	// calling can close here ensures that we use the same dialog for single files
	else if (saveProgramsList.size() == 1) {
		return canClose(saveProgramsList.get(0));
	}

	return saveDataDialog.showDialog(domainFilesToSaveList);
}
 
Example 11
Source File: ProgramSaveManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void save(Program program) {

		tool.prepareToSave(program);
		if (acquireSaveLock(program, "Save")) {

			try {
				SaveFileTask task = new SaveFileTask(program.getDomainFile());
				new TaskLauncher(task, tool.getToolFrame());
			}
			finally {
				program.unlock();
			}
		}
	}
 
Example 12
Source File: ProgramSaveManager.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private boolean handleChangedProgram(Program currentProgram) {
	if (!currentProgram.isChanged()) {
		return true;
	}
	DomainFile df = currentProgram.getDomainFile();

	String filename = df.getName();

	if (!df.isInWritableProject()) {
		return OptionDialog.showOptionDialog(tool.getToolFrame(), "Program Changed",
			HTMLUtilities.lineWrapWithHTMLLineBreaks(
				"<html>Viewed file '" + HTMLUtilities.escapeHTML(filename) +
					"' has been changed.  \n" + "If you continue, your changes will be lost!"),
			"Continue", OptionDialog.QUESTION_MESSAGE) != OptionDialog.CANCEL_OPTION;
	}

	if (df.isReadOnly()) {
		return OptionDialog.showOptionDialog(tool.getToolFrame(), "Program Changed",
			HTMLUtilities.lineWrapWithHTMLLineBreaks(
				"<html>Read-only file '" + HTMLUtilities.escapeHTML(filename) +
					"' has been changed.  \n" + "If you continue, your changes will be lost!"),
			"Continue", OptionDialog.QUESTION_MESSAGE) != OptionDialog.CANCEL_OPTION;

	}

	int result = OptionDialog.showOptionDialog(tool.getToolFrame(), "Save Program?",
		HTMLUtilities.lineWrapWithHTMLLineBreaks("<html>" + HTMLUtilities.escapeHTML(filename) +
			" has changed.\nDo you want to save it?"),
		"&Save", "Do&n't Save", OptionDialog.QUESTION_MESSAGE);

	if (result == OptionDialog.CANCEL_OPTION) {
		return false;
	}
	if (result == OptionDialog.OPTION_ONE) {
		SaveFileTask task = new SaveFileTask(currentProgram.getDomainFile());
		new TaskLauncher(task, tool.getToolFrame());
	}
	return true;
}
 
Example 13
Source File: VTSessionDB.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("hiding")
// this is from our constructor
private void initializePrograms(Program sourceProgram, Program destinationProgram) {
	this.sourceProgram = sourceProgram;
	this.destinationProgram = destinationProgram;
	sourceProgram.addConsumer(this);
	destinationProgram.addConsumer(this);
	Options properties = getOptions(PROGRAM_ID_PROPERTYLIST_NAME);
	DomainFile sourceDomainFile = sourceProgram.getDomainFile();
	properties.setString(SOURCE_PROGRAM_ID_PROPERTY_KEY, sourceDomainFile.getFileID());
	DomainFile destinationDomainFile = destinationProgram.getDomainFile();
	properties.setString(DESTINATION_PROGRAM_ID_PROPERTY_KEY,
		destinationDomainFile.getFileID());

}
 
Example 14
Source File: MSLibBatchImportWorker.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private void importLibrary(DomainFolder currentLibraryFolder, File file, MessageLog log)
		throws CancelledException, DuplicateNameException, InvalidNameException,
		VersionException, IOException {
	try (RandomAccessByteProvider provider = new RandomAccessByteProvider(file)) {
		if (!CoffArchiveHeader.isMatch(provider)) {
			return;
		}
		CoffArchiveHeader coffArchiveHeader =
			CoffArchiveHeader.read(provider, TaskMonitor.DUMMY);
		HashSet<Long> offsetsSeen = new HashSet<Long>();
		for (CoffArchiveMemberHeader archiveMemberHeader : coffArchiveHeader.getArchiveMemberHeaders()) {
			if (offsetsSeen.contains(archiveMemberHeader.getPayloadOffset())) {
				continue;
			}
			offsetsSeen.add(archiveMemberHeader.getPayloadOffset());
			if (archiveMemberHeader.isCOFF()) {
				try (ByteProvider coffProvider = new ByteProviderWrapper(provider,
					archiveMemberHeader.getPayloadOffset(), archiveMemberHeader.getSize())) {
					CoffFileHeader header = new CoffFileHeader(coffProvider);
					if (CoffMachineType.isMachineTypeDefined(header.getMagic())) {
						String preferredName = archiveMemberHeader.getName();

						Pair<DomainFolder, String> pair =
							getFolderAndUniqueFile(currentLibraryFolder, preferredName);

						List<Program> programs = AutoImporter.importFresh(coffProvider,
							pair.first, this, log, monitor, LOADER_FILTER, LOADSPEC_CHOOSER,
							pair.second, OptionChooser.DEFAULT_OPTIONS,
							MultipleProgramsStrategy.ONE_PROGRAM_OR_EXCEPTION);

						if (programs != null) {
							for (Program program : programs) {
								println("Imported " + program.getDomainFile().getPathname());
								DomainFile progFile = program.getDomainFile();

								program.release(this);

								if (!progFile.isVersioned()) {
									progFile.addToVersionControl(initalCheckInComment, false,
										monitor);
								}

							}
						}
					}
				}
			}
		}
	}
	catch (CoffException e) {
		//TODO
	}
}
 
Example 15
Source File: CreateDomainObjectTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private static void renameProgram(Program p, String newName) throws Exception {
	DomainFile dfile = p.getDomainFile();
	dfile.setName(newName);
}