r2diaphora 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- r2diaphora/__init__.py +44 -0
- r2diaphora/db_backend.py +374 -0
- r2diaphora/diaphora.py +2371 -0
- r2diaphora/diaphora_heuristics.py +1414 -0
- r2diaphora/diaphora_r2.py +437 -0
- r2diaphora/difflibparser.py +107 -0
- r2diaphora/html_diff.py +403 -0
- r2diaphora/ida_shim/__init__.py +81 -0
- r2diaphora/ida_shim/api.py +1976 -0
- r2diaphora/ida_shim/cache.py +3460 -0
- r2diaphora/ida_shim/decompiler.py +158 -0
- r2diaphora/ida_shim/ida_bytes.py +9 -0
- r2diaphora/ida_shim/ida_funcs.py +9 -0
- r2diaphora/ida_shim/ida_gdl.py +9 -0
- r2diaphora/ida_shim/ida_graph.py +9 -0
- r2diaphora/ida_shim/ida_hexrays.py +107 -0
- r2diaphora/ida_shim/ida_ida.py +9 -0
- r2diaphora/ida_shim/ida_idaapi.py +9 -0
- r2diaphora/ida_shim/ida_idp.py +9 -0
- r2diaphora/ida_shim/ida_kernwin.py +9 -0
- r2diaphora/ida_shim/ida_lines.py +9 -0
- r2diaphora/ida_shim/ida_nalt.py +9 -0
- r2diaphora/ida_shim/ida_name.py +9 -0
- r2diaphora/ida_shim/ida_pro.py +9 -0
- r2diaphora/ida_shim/ida_segment.py +9 -0
- r2diaphora/ida_shim/ida_ua.py +9 -0
- r2diaphora/ida_shim/idaapi.py +9 -0
- r2diaphora/ida_shim/idautils.py +9 -0
- r2diaphora/ida_shim/idc.py +9 -0
- r2diaphora/ida_shim/state.py +22 -0
- r2diaphora/idaapi/__init__.py +0 -0
- r2diaphora/idaapi/idaapi_to_r2.py +693 -0
- r2diaphora/idaapi/instructions.py +8 -0
- r2diaphora/jkutils/__init__.py +0 -0
- r2diaphora/jkutils/factor.py +220 -0
- r2diaphora/jkutils/graph_hashes.py +221 -0
- r2diaphora/jkutils/kfuzzy.py +353 -0
- r2diaphora/others/__init__.py +0 -0
- r2diaphora/others/tarjan_sort.py +114 -0
- r2diaphora/signatures/flirt/arm/uclibc_arm.sig +0 -0
- r2diaphora/signatures/flirt/mips/uclibc_mips.sig +0 -0
- r2diaphora/signatures/flirt/ppc/uclibc_ppc.sig +0 -0
- r2diaphora/signatures/flirt/sh/uclibc_sh4.sig +0 -0
- r2diaphora/signatures/flirt/x64/uclibc_x86-64.sig +0 -0
- r2diaphora/signatures/flirt/x86/uclibc_x86.sig +0 -0
- r2diaphora/upstream/__init__.py +29 -0
- r2diaphora/upstream/codecut/__init__.py +0 -0
- r2diaphora/upstream/codecut/basicutils_7x.py +366 -0
- r2diaphora/upstream/codecut/cc_base.py +164 -0
- r2diaphora/upstream/codecut/cc_main.py +68 -0
- r2diaphora/upstream/codecut/lfa.py +293 -0
- r2diaphora/upstream/codecut/map_read.py +343 -0
- r2diaphora/upstream/codecut/maxcut.py +180 -0
- r2diaphora/upstream/codecut/modnaming.py +309 -0
- r2diaphora/upstream/codecut/module.py +52 -0
- r2diaphora/upstream/codecut/snap_cg.py +67 -0
- r2diaphora/upstream/db_support/__init__.py +19 -0
- r2diaphora/upstream/db_support/schema.py +186 -0
- r2diaphora/upstream/diaphora.py +3868 -0
- r2diaphora/upstream/diaphora_config.py +226 -0
- r2diaphora/upstream/diaphora_heuristics.py +1335 -0
- r2diaphora/upstream/diaphora_ida.py +3995 -0
- r2diaphora/upstream/jkutils/IDAMagicStrings.py +919 -0
- r2diaphora/upstream/jkutils/__init__.py +0 -0
- r2diaphora/upstream/jkutils/factor.py +268 -0
- r2diaphora/upstream/jkutils/graph_hashes.py +210 -0
- r2diaphora/upstream/jkutils/kfuzzy.py +395 -0
- r2diaphora/upstream/jkutils/threads.py +72 -0
- r2diaphora/upstream/ml/__init__.py +0 -0
- r2diaphora/upstream/ml/basic_engine.py +157 -0
- r2diaphora/upstream/ml/diaphora-amalgamation-model.pkl +0 -0
- r2diaphora/upstream/others/__init__.py +0 -0
- r2diaphora/upstream/others/tarjan_sort.py +126 -0
- r2diaphora-0.4.0.data/scripts/r2diaphora-bulk +99 -0
- r2diaphora-0.4.0.data/scripts/r2diaphora-db +113 -0
- r2diaphora-0.4.0.dist-info/METADATA +35 -0
- r2diaphora-0.4.0.dist-info/RECORD +81 -0
- r2diaphora-0.4.0.dist-info/WHEEL +5 -0
- r2diaphora-0.4.0.dist-info/entry_points.txt +2 -0
- r2diaphora-0.4.0.dist-info/licenses/LICENSE +661 -0
- r2diaphora-0.4.0.dist-info/top_level.txt +1 -0
r2diaphora/__init__.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
__author__ = "Fernando Domínguez"
|
|
2
|
+
__credits__ = "Joxean Koret"
|
|
3
|
+
|
|
4
|
+
from .db_backend import (
|
|
5
|
+
configure_db_backend,
|
|
6
|
+
db_exists,
|
|
7
|
+
drop_all,
|
|
8
|
+
drop_db,
|
|
9
|
+
get_active_backend,
|
|
10
|
+
get_db_attrs,
|
|
11
|
+
get_db_attrs_path,
|
|
12
|
+
get_db_dir,
|
|
13
|
+
get_function_details,
|
|
14
|
+
)
|
|
15
|
+
from .diaphora_r2 import compare_dbs, dbname_for_file, generate_db_for_file
|
|
16
|
+
from .html_diff import HtmlResults
|
|
17
|
+
from .idaapi.idaapi_to_r2 import (
|
|
18
|
+
get_all_fns,
|
|
19
|
+
r2_close,
|
|
20
|
+
r2_open,
|
|
21
|
+
string_values,
|
|
22
|
+
strings,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"HtmlResults",
|
|
27
|
+
"compare_dbs",
|
|
28
|
+
"configure_db_backend",
|
|
29
|
+
"db_exists",
|
|
30
|
+
"dbname_for_file",
|
|
31
|
+
"drop_all",
|
|
32
|
+
"drop_db",
|
|
33
|
+
"generate_db_for_file",
|
|
34
|
+
"get_active_backend",
|
|
35
|
+
"get_all_fns",
|
|
36
|
+
"get_db_attrs",
|
|
37
|
+
"get_db_attrs_path",
|
|
38
|
+
"get_db_dir",
|
|
39
|
+
"get_function_details",
|
|
40
|
+
"r2_close",
|
|
41
|
+
"r2_open",
|
|
42
|
+
"string_values",
|
|
43
|
+
"strings",
|
|
44
|
+
]
|
r2diaphora/db_backend.py
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
"""Dual-backend database layer for Diaphora 3.5.
|
|
2
|
+
|
|
3
|
+
SQLite is the default and is used through the stdlib ``sqlite3`` module with
|
|
4
|
+
no translation. MariaDB is selected with ``configure_db_backend("mariadb")``:
|
|
5
|
+
upstream SQL (written for SQLite) is translated on the fly, so the upstream
|
|
6
|
+
sources stay unmodified.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import os
|
|
11
|
+
import re
|
|
12
|
+
import sqlite3
|
|
13
|
+
import threading
|
|
14
|
+
from typing import Any, Dict, List, Optional, Union
|
|
15
|
+
|
|
16
|
+
from r2diaphora.upstream import load_upstream_diaphora
|
|
17
|
+
|
|
18
|
+
BACKENDS = ("sqlite", "mariadb")
|
|
19
|
+
DB_ATTRS_FILE = "db.json"
|
|
20
|
+
DB_SUFFIX = ".sqlite"
|
|
21
|
+
SQLITE_SUFFIXES = ("", "-wal", "-shm", "-crash")
|
|
22
|
+
INDEX_TEXT_PREFIX = 255
|
|
23
|
+
DEFAULT_DB_DIR = os.path.join(os.path.expanduser("~"), ".r2diaphora", "dbs")
|
|
24
|
+
|
|
25
|
+
_LITERAL_RE = re.compile(r"('(?:[^']|'')*'|\"(?:[^\"]|\"\")*\")")
|
|
26
|
+
_TEXT_UNIQUE_RE = re.compile(r"\b(address|rva|start_ea)\s+text\s+unique\b", re.IGNORECASE)
|
|
27
|
+
_INDEX_RE = re.compile(
|
|
28
|
+
r"(create\s+index\s+if\s+not\s+exists\s+\w+\s+on\s+\w+\s*\()([^)]*)\)", re.IGNORECASE
|
|
29
|
+
)
|
|
30
|
+
_ATTACH_RE = re.compile(r'^\s*attach\s+"?([^"\s]+)"?\s+as\s+diff\s*$', re.IGNORECASE)
|
|
31
|
+
_DETACH_RE = re.compile(r'^\s*detach\s+"?(\w+)"?\s*$', re.IGNORECASE)
|
|
32
|
+
|
|
33
|
+
_state: Dict[str, Any] = {"backend": "sqlite", "db_dir": None, "db_attrs": None}
|
|
34
|
+
_registry: List[Any] = []
|
|
35
|
+
_registry_lock = threading.Lock()
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class RowAdapter:
|
|
39
|
+
"""Row that supports both column-name and positional access, like sqlite3.Row."""
|
|
40
|
+
|
|
41
|
+
__slots__ = ("_data", "_values")
|
|
42
|
+
|
|
43
|
+
def __init__(self, dict_row: Dict[str, Any]):
|
|
44
|
+
self._data = dict(dict_row)
|
|
45
|
+
self._values = tuple(self._data.values())
|
|
46
|
+
|
|
47
|
+
def __getitem__(self, key):
|
|
48
|
+
if isinstance(key, (int, slice)):
|
|
49
|
+
return self._values[key]
|
|
50
|
+
return self._data[key]
|
|
51
|
+
|
|
52
|
+
def keys(self):
|
|
53
|
+
return self._data.keys()
|
|
54
|
+
|
|
55
|
+
def __iter__(self):
|
|
56
|
+
return iter(self._values)
|
|
57
|
+
|
|
58
|
+
def __len__(self):
|
|
59
|
+
return len(self._values)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _text_columns() -> set:
|
|
63
|
+
_, _, schema_mod = load_upstream_diaphora()
|
|
64
|
+
found = set()
|
|
65
|
+
for table_sql in schema_mod.TABLES:
|
|
66
|
+
found.update(re.findall(r"\b(\w+)\s+text\b", table_sql, re.IGNORECASE))
|
|
67
|
+
return found
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
_TEXT_COLUMNS: Optional[set] = None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _translate_code(code: str, main_db: str, diff_db: Optional[str]) -> str:
|
|
74
|
+
global _TEXT_COLUMNS
|
|
75
|
+
if _TEXT_COLUMNS is None:
|
|
76
|
+
_TEXT_COLUMNS = _text_columns()
|
|
77
|
+
|
|
78
|
+
code = re.sub(r"(?<![\w.`])main\.", f"`{main_db}`.", code)
|
|
79
|
+
if diff_db:
|
|
80
|
+
code = re.sub(r"(?<![\w.`])diff\.", f"`{diff_db}`.", code)
|
|
81
|
+
code = re.sub(r"insert\s+or\s+ignore\s+into", "insert ignore into", code, flags=re.IGNORECASE)
|
|
82
|
+
code = re.sub(r"cast\(([^()]+?)\s+as\s+real\)", r"cast(\1 as double)", code, flags=re.IGNORECASE)
|
|
83
|
+
code = re.sub(
|
|
84
|
+
r"integer\s+primary\s+key\b(?!\s+auto_increment)",
|
|
85
|
+
"integer primary key auto_increment",
|
|
86
|
+
code,
|
|
87
|
+
flags=re.IGNORECASE,
|
|
88
|
+
)
|
|
89
|
+
code = _TEXT_UNIQUE_RE.sub(lambda m: f"{m.group(1)} varchar(255) unique", code)
|
|
90
|
+
|
|
91
|
+
def _index_cols(m):
|
|
92
|
+
cols = []
|
|
93
|
+
for col in m.group(2).split(","):
|
|
94
|
+
name = col.strip()
|
|
95
|
+
if name.lower() in _TEXT_COLUMNS:
|
|
96
|
+
cols.append(f"{name}({INDEX_TEXT_PREFIX})")
|
|
97
|
+
else:
|
|
98
|
+
cols.append(name)
|
|
99
|
+
return f"{m.group(1)}{', '.join(cols)})"
|
|
100
|
+
|
|
101
|
+
code = _INDEX_RE.sub(_index_cols, code)
|
|
102
|
+
return code.replace("?", "%s")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def translate_sqlite_to_mariadb(sql: str, main_db: str, diff_db: Optional[str] = None) -> str:
|
|
106
|
+
parts = _LITERAL_RE.split(sql)
|
|
107
|
+
out = []
|
|
108
|
+
for i, part in enumerate(parts):
|
|
109
|
+
out.append(part if i % 2 else _translate_code(part, main_db, diff_db))
|
|
110
|
+
return "".join(out)
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def escape_percent_in_literals(sql: str) -> str:
|
|
114
|
+
"""Escape '%' inside SQL literals for DB-API drivers that always interpolate."""
|
|
115
|
+
parts = _LITERAL_RE.split(sql)
|
|
116
|
+
return "".join(p.replace("%", "%%") if i % 2 else p for i, p in enumerate(parts))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def get_active_backend() -> str:
|
|
120
|
+
return _state["backend"]
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def get_db_dir() -> str:
|
|
124
|
+
"""Return the directory where SQLite databases are stored."""
|
|
125
|
+
return _state["db_dir"] or DEFAULT_DB_DIR
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def get_db_attrs_path() -> str:
|
|
129
|
+
return os.path.join(os.path.expanduser("~"), ".r2diaphora", DB_ATTRS_FILE)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def get_db_attrs() -> Dict[str, Any]:
|
|
133
|
+
if _state["db_attrs"] is not None:
|
|
134
|
+
return _state["db_attrs"]
|
|
135
|
+
path = get_db_attrs_path()
|
|
136
|
+
if not os.path.isfile(path):
|
|
137
|
+
return {}
|
|
138
|
+
with open(path, "r", encoding="utf-8") as f:
|
|
139
|
+
return json.load(f)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
class MariaDBCursorAdapter:
|
|
143
|
+
def __init__(self, conn: "MariaDBConnectionAdapter", cur):
|
|
144
|
+
self._conn = conn
|
|
145
|
+
self._cur = cur
|
|
146
|
+
|
|
147
|
+
def execute(self, sql: str, params=None):
|
|
148
|
+
stripped = sql.strip()
|
|
149
|
+
low = stripped.lower()
|
|
150
|
+
if low.startswith("pragma") or low.startswith("analyze") or low in ("begin", "begin transaction"):
|
|
151
|
+
return self
|
|
152
|
+
if low == "commit":
|
|
153
|
+
self._conn.commit()
|
|
154
|
+
return self
|
|
155
|
+
m = _ATTACH_RE.match(stripped)
|
|
156
|
+
if m:
|
|
157
|
+
self._conn.diff_db = m.group(1)
|
|
158
|
+
return self
|
|
159
|
+
m = _DETACH_RE.match(stripped)
|
|
160
|
+
if m:
|
|
161
|
+
self._conn.diff_db = None
|
|
162
|
+
return self
|
|
163
|
+
|
|
164
|
+
translated = translate_sqlite_to_mariadb(stripped, self._conn.main_db, self._conn.diff_db)
|
|
165
|
+
translated = escape_percent_in_literals(translated)
|
|
166
|
+
self._cur.execute(translated, tuple(params) if params is not None else ())
|
|
167
|
+
return self
|
|
168
|
+
|
|
169
|
+
def executemany(self, sql: str, seq):
|
|
170
|
+
for params in seq:
|
|
171
|
+
self.execute(sql, params)
|
|
172
|
+
return self
|
|
173
|
+
|
|
174
|
+
def _wrap(self, row):
|
|
175
|
+
return None if row is None else RowAdapter(row)
|
|
176
|
+
|
|
177
|
+
def fetchone(self):
|
|
178
|
+
return self._wrap(self._cur.fetchone())
|
|
179
|
+
|
|
180
|
+
def fetchmany(self, size: int = 1000):
|
|
181
|
+
return [RowAdapter(r) for r in self._cur.fetchmany(size)]
|
|
182
|
+
|
|
183
|
+
def fetchall(self):
|
|
184
|
+
return [RowAdapter(r) for r in self._cur.fetchall()]
|
|
185
|
+
|
|
186
|
+
def __iter__(self):
|
|
187
|
+
for row in self._cur:
|
|
188
|
+
yield RowAdapter(row)
|
|
189
|
+
|
|
190
|
+
@property
|
|
191
|
+
def lastrowid(self):
|
|
192
|
+
return self._cur.lastrowid
|
|
193
|
+
|
|
194
|
+
@property
|
|
195
|
+
def rowcount(self):
|
|
196
|
+
return self._cur.rowcount
|
|
197
|
+
|
|
198
|
+
def close(self):
|
|
199
|
+
self._cur.close()
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
class MariaDBConnectionAdapter:
|
|
203
|
+
def __init__(self, main_db: str):
|
|
204
|
+
import mysql.connector
|
|
205
|
+
|
|
206
|
+
attrs = get_db_attrs()
|
|
207
|
+
self.main_db = main_db
|
|
208
|
+
self.diff_db: Optional[str] = None
|
|
209
|
+
self._conn = mysql.connector.connect(
|
|
210
|
+
user=attrs.get("user", ""), password=attrs.get("password", ""), host=attrs.get("host", "localhost")
|
|
211
|
+
)
|
|
212
|
+
boot = self._conn.cursor()
|
|
213
|
+
boot.execute(
|
|
214
|
+
f"CREATE DATABASE IF NOT EXISTS `{main_db}` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
|
|
215
|
+
)
|
|
216
|
+
boot.execute(f"USE `{main_db}`")
|
|
217
|
+
boot.close()
|
|
218
|
+
|
|
219
|
+
def cursor(self) -> MariaDBCursorAdapter:
|
|
220
|
+
return MariaDBCursorAdapter(self, self._conn.cursor(dictionary=True, buffered=True))
|
|
221
|
+
|
|
222
|
+
def execute(self, sql: str, params=None) -> MariaDBCursorAdapter:
|
|
223
|
+
cur = self.cursor()
|
|
224
|
+
cur.execute(sql, params)
|
|
225
|
+
return cur
|
|
226
|
+
|
|
227
|
+
def commit(self):
|
|
228
|
+
self._conn.commit()
|
|
229
|
+
|
|
230
|
+
def close(self):
|
|
231
|
+
self._conn.close()
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _sqlite_connect(db_name: str) -> sqlite3.Connection:
|
|
235
|
+
db = sqlite3.connect(db_name, check_same_thread=False)
|
|
236
|
+
db.text_factory = str
|
|
237
|
+
db.row_factory = sqlite3.Row
|
|
238
|
+
with _registry_lock:
|
|
239
|
+
_registry.append(db_name)
|
|
240
|
+
_registry.append(db)
|
|
241
|
+
return db
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _mariadb_connect(db_name: str) -> MariaDBConnectionAdapter:
|
|
245
|
+
db = MariaDBConnectionAdapter(db_name)
|
|
246
|
+
with _registry_lock:
|
|
247
|
+
_registry.append(db_name)
|
|
248
|
+
_registry.append(db)
|
|
249
|
+
return db
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def connect_db(db_name: str) -> Union[sqlite3.Connection, MariaDBConnectionAdapter]:
|
|
253
|
+
if _state["backend"] == "mariadb":
|
|
254
|
+
return _mariadb_connect(db_name)
|
|
255
|
+
return _sqlite_connect(db_name)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def configure_db_backend(
|
|
259
|
+
backend: str = "sqlite", db_dir: Optional[str] = None, db_attrs: Optional[dict] = None
|
|
260
|
+
) -> None:
|
|
261
|
+
if backend not in BACKENDS:
|
|
262
|
+
raise ValueError(f"unknown database backend: {backend}")
|
|
263
|
+
_state["backend"] = backend
|
|
264
|
+
_state["db_dir"] = db_dir
|
|
265
|
+
_state["db_attrs"] = db_attrs
|
|
266
|
+
diaphora_mod, _, _ = load_upstream_diaphora()
|
|
267
|
+
diaphora_mod.sqlite3_connect = connect_db
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def close_all_connections(db_name: Optional[str] = None) -> None:
|
|
271
|
+
with _registry_lock:
|
|
272
|
+
keep = []
|
|
273
|
+
for i in range(0, len(_registry), 2):
|
|
274
|
+
name, conn = _registry[i], _registry[i + 1]
|
|
275
|
+
if db_name is None or name == db_name:
|
|
276
|
+
try:
|
|
277
|
+
conn.close()
|
|
278
|
+
except Exception:
|
|
279
|
+
pass
|
|
280
|
+
else:
|
|
281
|
+
keep.extend([name, conn])
|
|
282
|
+
_registry[:] = keep
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def remove_db_file(db_path: str) -> None:
|
|
286
|
+
close_all_connections(db_path)
|
|
287
|
+
for suffix in SQLITE_SUFFIXES:
|
|
288
|
+
path = f"{db_path}{suffix}"
|
|
289
|
+
if os.path.exists(path):
|
|
290
|
+
os.remove(path)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def db_exists(dbname: str) -> bool:
|
|
294
|
+
if _state["backend"] == "mariadb":
|
|
295
|
+
db = _mariadb_connect(dbname)
|
|
296
|
+
try:
|
|
297
|
+
row = db.execute(
|
|
298
|
+
"select schema_name from information_schema.schemata where schema_name = ?", [dbname]
|
|
299
|
+
).fetchone()
|
|
300
|
+
return row is not None
|
|
301
|
+
finally:
|
|
302
|
+
close_all_connections(dbname)
|
|
303
|
+
return os.path.isfile(dbname)
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
def resolve_sqlite_db_path(name: str) -> str:
|
|
307
|
+
"""Resolve a user-supplied SQLite database name to its file path.
|
|
308
|
+
|
|
309
|
+
Accepts a full path (returned as-is), a bare ``<sha256>`` digest or a
|
|
310
|
+
``<sha256>.sqlite`` file name, the latter two looked up in the configured
|
|
311
|
+
db directory.
|
|
312
|
+
"""
|
|
313
|
+
if os.path.dirname(name):
|
|
314
|
+
return name
|
|
315
|
+
if not name.endswith(DB_SUFFIX):
|
|
316
|
+
name += DB_SUFFIX
|
|
317
|
+
return os.path.join(get_db_dir(), name)
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def drop_db(dbname: str) -> None:
|
|
321
|
+
if _state["backend"] == "mariadb":
|
|
322
|
+
db = _mariadb_connect(dbname)
|
|
323
|
+
try:
|
|
324
|
+
db.execute(f"DROP DATABASE IF EXISTS `{dbname}`")
|
|
325
|
+
finally:
|
|
326
|
+
close_all_connections(dbname)
|
|
327
|
+
return
|
|
328
|
+
remove_db_file(resolve_sqlite_db_path(dbname))
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
def drop_all(db_dir: Optional[str] = None) -> None:
|
|
332
|
+
"""Delete every generated analysis database."""
|
|
333
|
+
close_all_connections(None)
|
|
334
|
+
if _state["backend"] == "mariadb":
|
|
335
|
+
attrs = get_db_attrs()
|
|
336
|
+
if not attrs:
|
|
337
|
+
return
|
|
338
|
+
import mysql.connector
|
|
339
|
+
|
|
340
|
+
conn = mysql.connector.connect(
|
|
341
|
+
user=attrs.get("user", ""),
|
|
342
|
+
password=attrs.get("password", ""),
|
|
343
|
+
host=attrs.get("host", "localhost"),
|
|
344
|
+
)
|
|
345
|
+
try:
|
|
346
|
+
cur = conn.cursor()
|
|
347
|
+
cur.execute("show databases")
|
|
348
|
+
names = [row[0] for row in cur.fetchall()]
|
|
349
|
+
cur.close()
|
|
350
|
+
for name in names:
|
|
351
|
+
if not name.startswith("diaphora_"):
|
|
352
|
+
continue
|
|
353
|
+
cur = conn.cursor()
|
|
354
|
+
cur.execute(f"DROP DATABASE IF EXISTS `{name}`")
|
|
355
|
+
cur.close()
|
|
356
|
+
finally:
|
|
357
|
+
conn.close()
|
|
358
|
+
return
|
|
359
|
+
|
|
360
|
+
directory = db_dir or get_db_dir()
|
|
361
|
+
if not os.path.isdir(directory):
|
|
362
|
+
return
|
|
363
|
+
for entry in os.listdir(directory):
|
|
364
|
+
if not entry.lower().endswith(".sqlite"):
|
|
365
|
+
continue
|
|
366
|
+
remove_db_file(os.path.join(directory, entry))
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+
def get_function_details(dbname: str, fname: str) -> Optional[RowAdapter]:
|
|
370
|
+
db = connect_db(dbname)
|
|
371
|
+
try:
|
|
372
|
+
return db.execute("SELECT * FROM functions WHERE name = ?", [fname]).fetchone()
|
|
373
|
+
finally:
|
|
374
|
+
close_all_connections(dbname)
|