24#include <unordered_map>
26#ifndef EON_READCON_DB_VERSION
27#define EON_READCON_DB_VERSION ""
31#ifndef WIN32_LEAN_AND_MEAN
32#define WIN32_LEAN_AND_MEAN
53constexpr std::uint64_t kFnvOffset = 14695981039346656037ull;
54constexpr std::uint64_t kFnvPrime = 1099511628211ull;
56std::uint64_t fnv1a(
const std::string &bytes) {
57 std::uint64_t h = kFnvOffset;
58 for (
unsigned char c : bytes) {
59 h ^=
static_cast<std::uint64_t
>(c);
62 return h == 0 ? 1 : h;
65std::optional<std::filesystem::path>
66corpus_dir(
const std::filesystem::path &con_path) {
67 namespace fs = std::filesystem;
69 fs::path cur = fs::weakly_canonical(con_path, ec);
71 cur = fs::absolute(con_path);
73 cur = cur.parent_path();
74 for (
int i = 0; i < 8; ++i) {
75 if (fs::is_regular_file(cur /
"config.ini")) {
76 return cur /
"readcon.db";
78 const fs::path parent = cur.parent_path();
88using OpenFn = int (*)(
const char *, std::size_t *);
89using AppendFn = int (*)(std::size_t, std::uint64_t,
const char *,
const char *,
94 OpenFn open =
nullptr;
95 AppendFn append_str =
nullptr;
97 bool from_process =
false;
102 static int epoch = 0;
106void release_lib(
LibHandle lib,
bool from_process) {
107 if (lib ==
nullptr || from_process) {
117void *symbol(
LibHandle handle,
const char *name) {
119 return reinterpret_cast<void *
>(GetProcAddress(handle, name));
121 return dlsym(handle, name);
127 return LoadLibraryA(name);
129 return dlopen(name, RTLD_LAZY | RTLD_LOCAL);
133const char *open_error() {
135 return "LoadLibrary failed";
137 const char *err = dlerror();
138 return err !=
nullptr ? err :
"dlopen returned null";
142void clear_api(Api &out) {
143 release_lib(out.lib, out.from_process);
147bool resolve_symbols(Api &out,
LibHandle handle) {
148 out.open =
reinterpret_cast<OpenFn
>(symbol(handle,
"rkrdb_open"));
150 reinterpret_cast<AppendFn
>(symbol(handle,
"rkrdb_append_trajectory_str"));
151 return out.open !=
nullptr && out.append_str !=
nullptr;
156 if (out.epoch == mirror_epoch() &&
157 (out.lib !=
nullptr || out.from_process || out.missing)) {
161 out.epoch = mirror_epoch();
162 const char *forced = std::getenv(
"EON_READCON_DB_LIBRARY");
163 if (forced !=
nullptr) {
164 namespace fs = std::filesystem;
166 if (forced[0] ==
'\0' || !fs::is_regular_file(forced, ec) || ec) {
168 forced[0] ==
'\0' ?
"empty path" : forced);
172 out.lib = open_named(forced);
175 if (resolve_symbols(out, RTLD_DEFAULT)) {
176 out.from_process =
true;
180 out.append_str =
nullptr;
181 const char *names[] = {
"libreadcon_db.so",
"libreadcon_db.so.0",
182 "libreadcon_db.dylib"};
187 HMODULE loaded = GetModuleHandleA(
"readcon_db.dll");
188 if (loaded !=
nullptr && resolve_symbols(out, loaded)) {
190 out.from_process =
true;
194 out.append_str =
nullptr;
195 const char *names[] = {
"readcon_db.dll"};
197 for (
const char *name : names) {
198 out.lib = open_named(name);
199 if (out.lib !=
nullptr) {
204 if (out.lib ==
nullptr) {
210 if (!resolve_symbols(out, out.lib)) {
211 release_lib(out.lib,
false);
214 out.append_str =
nullptr;
220std::mutex &handles_mu() {
221 static std::mutex mu;
225std::unordered_map<std::string, std::size_t> &handles() {
226 static std::unordered_map<std::string, std::size_t> map;
230std::size_t corpus_handle(
const std::filesystem::path &dir) {
233 return static_cast<std::size_t
>(-1);
235 const std::string key = dir.string();
236 std::lock_guard<std::mutex> guard(handles_mu());
237 const auto found = handles().find(key);
238 if (found != handles().end()) {
239 return found->second;
242 if (fns.open(key.c_str(), &
id) != 0) {
243 return static_cast<std::size_t
>(-1);
245 handles().emplace(key,
id);
252 namespace fs = std::filesystem;
254 if (!fs::is_regular_file(path, ec) || ec) {
257 std::ifstream in(path);
261 std::ostringstream buf;
263 const std::string text = buf.str();
264 if (text.find_first_not_of(
" \t\r\n") == std::string::npos) {
267 fs::path canonical = fs::weakly_canonical(path, ec);
269 canonical = fs::absolute(path);
271 const std::string resolved = canonical.string();
272 const auto corpus = corpus_dir(canonical);
276 const std::size_t
id = corpus_handle(*corpus);
277 if (
id ==
static_cast<std::size_t
>(-1)) {
280 const std::uint64_t tid = fnv1a(resolved +
'\0' + text);
281 std::uint32_t nframes = 0;
282 api().append_str(
id, tid, text.c_str(), resolved.c_str(), &nframes);
286 const Api &fns = api();
287 return !fns.missing && fns.open !=
nullptr && fns.append_str !=
nullptr;
#define EONC_LOG_WARNING(...)
#define EON_READCON_DB_VERSION
static int(*const kLinkedOpen)(const char *, std::size_t *)
int rkrdb_open(const char *, std::size_t *)
bool readcon_db_mirror_ok()
True when the loaded rkrdb_open belongs to the linked readcon-db.
void readcon_db_mirror_reset()
Drop a cached load so a later call reads EON_READCON_DB_LIBRARY again.
const char * readcon_db_loaded_version()
Version of the loaded library. Empty when the load failed.
void mirror_con_corpus(const std::string &path)
Copy a con file into the run readcon-db corpus when libreadcon_db loads.