summaryrefslogtreecommitdiff
path: root/view/sharedcache/core/SharedCacheView.h
blob: 7ad2ba9a1fc790815630b97159380062eaf6975a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by kat on 5/23/23.
//

#ifndef SHAREDCACHE_DSCVIEW_H
#define SHAREDCACHE_DSCVIEW_H

#include <binaryninjaapi.h>

class SharedCacheView : public BinaryNinja::BinaryView
{
	bool m_parseOnly;
	BinaryNinja::Ref<BinaryNinja::Logger> m_logger;

	// Restored primary file name from metadata, or the file name on first open.
	std::string m_primaryFileName;
	// Project-relative path when the database is in a project, otherwise relative to the database directory.
	std::string m_primaryFilePath;

	// Restored associated file names from metadata, this is all the associated cache entries.
	// NOTE: Currently this is just used to alert the user to supposed missing files.
	std::set<std::string> m_secondaryFileNames;

	std::string StorePrimaryProjectFile(BinaryNinja::ProjectFile* projectFile);
	void StorePrimaryFilePath(const std::string& path, BinaryNinja::Project* project, const std::string& databaseDir);
	std::optional<std::string> ResolveProjectFilePath(BinaryNinja::Project* project, const std::string& projectPath);
	std::optional<std::string> ResolveUniqueProjectFileName(BinaryNinja::Project* project);
	std::optional<std::string> ResolveMetadataPrimaryFilePath(BinaryNinja::Project* project, const std::string& databaseDir);
	std::optional<std::string> PromptForPrimaryFile();

public:
	SharedCacheView(const std::string& typeName, BinaryView* data, bool parseOnly = false);

	~SharedCacheView() override = default;

	bool Init() override;
	void OnAfterSnapshotDataApplied() override;

	// Initialized the shared cache controller for this view. This is what allows us to load images and regions.
	bool InitController();

	void SetPrimaryFileName(std::string primaryFileName);
	void SetPrimaryFileLocation(std::string primaryFilePath, std::string primaryFileName);

	// Logs the secondary file name to `m_secondaryFileNames`, see the note on the field about usage.
	void LogSecondaryFileName(std::string associatedFileName);

	// Get the path to the primary file.
	std::optional<std::string> GetPrimaryFilePath();

	// Get the metadata for saving the state of the shared cache.
	BinaryNinja::Ref<BinaryNinja::Metadata> GetMetadata() const;

	void LoadMetadata(const BinaryNinja::Metadata& metadata);

	virtual bool PerformIsExecutable() const override { return true; }
};


class SharedCacheViewType : public BinaryNinja::BinaryViewType
{
public:
	SharedCacheViewType();

	static void Register();

	BinaryNinja::Ref<BinaryNinja::BinaryView> Create(BinaryNinja::BinaryView* data) override;

	BinaryNinja::Ref<BinaryNinja::BinaryView> Parse(BinaryNinja::BinaryView* data) override;

	bool IsTypeValidForData(BinaryNinja::BinaryView* data) override;

	bool IsDeprecated() override { return false; }

	bool HasNoInitialContent() override { return true; }

	BinaryNinja::Ref<BinaryNinja::Settings> GetLoadSettingsForData(BinaryNinja::BinaryView* data) override;
};


#endif  // SHAREDCACHE_DSCVIEW_H