This repository has been archived by the owner on Apr 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Core.cpp
182 lines (157 loc) · 5.62 KB
/
Core.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* The MIT License (MIT)
*
* Copyright (c) 2014 athre0z
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Core.hpp"
#include "Config.hpp"
#include "Settings.hpp"
#include "Ui.hpp"
#include "ImportExport.hpp"
#include "RETypedef.hpp"
#include <QDir>
#include <QApplication>
#include <QMessageBox>
#include <idp.hpp>
#include <diskio.hpp>
#include <loader.hpp>
// =============================================================================================== //
// [Core] //
// =============================================================================================== //
Core::Core()
: m_originalMangler(nullptr)
{
#if IDA_SDK_VERSION >= 670
action_desc_t action =
{
sizeof(action),
"retypedef_open_name_subst_editor",
"Edit name substitutions...",
&m_optionsMenuItemClickedAction,
&PLUGIN
};
register_action(action);
attach_action_to_menu("Options/", "retypedef_open_name_subst_editor", 0);
#else
add_menu_item("Options/", "Edit name substitutions...", nullptr, 0,
&Core::onOptionsMenuItemClicked, this);
#endif
// First start? Initialize with default rules.
Settings settings;
if (settings.value(Settings::kFirstStart, true).toBool())
{
QSettings defaultRules(":/Misc/default_rules.ini", QSettings::IniFormat);
SettingsImporterExporter importer(&m_substitutionManager, &defaultRules);
importer.importRules();
saveToSettings();
settings.setValue(Settings::kFirstStart, false);
}
// Load rules from settings and subscribe to changes in the manager
try
{
SettingsImporterExporter importer(&m_substitutionManager, &settings);
importer.importRules();
}
catch (const SettingsImporterExporter::Error& e)
{
msg("[" PLUGIN_NAME "] Cannot load settings: %s\n", e.what());
}
connect(&m_substitutionManager, SIGNAL(entryAdded()), SLOT(saveToSettings()));
connect(&m_substitutionManager, SIGNAL(entryDeleted()), SLOT(saveToSettings()));
// Place demangler detour
HMODULE hIdaWll = GetModuleHandleA("IDA.WLL");
if (!hIdaWll)
throw std::runtime_error("cannot find IDA.WLL");
auto demangle = reinterpret_cast<demangler_t*>(GetProcAddress(hIdaWll, "demangle"));
if (!demangle)
throw std::runtime_error("cannot find exported function \"demangle\" in IDA.WLL");
m_demanglerDetour.reset(new DemanglerDetour(demangle, &Core::demanglerHookCallback));
m_demanglerDetour->attach(m_originalMangler);
}
Core::~Core()
{
// Remove demangler detour
try
{
m_demanglerDetour->detach();
}
catch (const DemanglerDetour::Error& /*e*/)
{
QMessageBox::critical(nullptr, PLUGIN_NAME,
"Critical: cannot detach internal hooks. Will terminate now.");
std::terminate();
}
#if IDA_SDK_VERSION >= 670
detach_action_from_menu("Options/", "retypedef_open_name_subst_editor");
unregister_action("retypedef_open_name_subst_editor");
#else
del_menu_item("Options/Edit name substitutions...");
#endif
}
#if IDA_SDK_VERSION >= 670
int Core::OptionsMenuItemClickedAction::activate(action_activation_ctx_t * /*ctx*/)
{
onOptionsMenuItemClicked(&Core::instance());
return 1;
}
action_state_t Core::OptionsMenuItemClickedAction::update(action_update_ctx_t * /*ctx*/)
{
return AST_ENABLE_ALWAYS;
}
#endif
void Core::runPlugin()
{
AboutDialog().exec();
}
int32 Core::demanglerHookCallback(char* answer, uint answerLength,
const char* str, uint32 disableMask)
{
auto &thiz = instance();
auto ret = thiz.m_originalMangler(answer, answerLength, str, disableMask);
//msg("str: %s; ret: 0x%08X\n", str, ret);
if (answer && answerLength != 0)
thiz.m_substitutionManager.applyToString(answer, answerLength);
return ret;
}
bool Core::onOptionsMenuItemClicked(void* userData)
{
auto thiz = reinterpret_cast<Core*>(userData);
SubstitutionModel model(&thiz->m_substitutionManager);
SubstitutionEditor editor(qApp->activeWindow());
editor.setModel(&model);
editor.exec();
return 0;
}
void Core::saveToSettings()
{
try
{
Settings settings;
SettingsImporterExporter exporter(&m_substitutionManager, &settings);
exporter.exportRules();
request_refresh(IWID_NAMES | IWID_DISASMS);
}
catch (const SettingsImporterExporter::Error &e)
{
msg("[" PLUGIN_NAME "] Cannot save to settings: %s\n", e.what());
}
}
// ============================================================================================== //