-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqfiletransport.cpp
More file actions
522 lines (469 loc) · 16.5 KB
/
qfiletransport.cpp
File metadata and controls
522 lines (469 loc) · 16.5 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#include <cstdint>
#include <cstdlib>
#include <cstring>
#if defined(_WIN32)
#include <windows.h>
#endif
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QRegularExpression>
#include <QString>
#include <QUrl>
#include <libaudcore/audstrings.h>
#include <libaudcore/i18n.h>
#include <libaudcore/interface.h>
#include <libaudcore/plugin.h>
#include <libaudcore/runtime.h>
static constexpr const char * const HOOKED_SCHEME = "qfiletransport";
static constexpr const char * const FILE_SCHEME = "file";
static QString toLocalFile(const QByteArray & str)
{
const QUrl url(str);
if (url.isValid() && url.isLocalFile())
return QFileInfo(QUrl(str).toLocalFile()).absoluteFilePath();
QByteArray tmp = str;
#if defined(_WIN32)
/// @note For `file://C:\Users\User/.adplug/adplug.db`
if (tmp.size() > 7 && tmp.startsWith("file://") && tmp[7] != '/')
tmp = tmp.mid(7);
/// @note For `/C:\some\path`
if (tmp.size() > 2 && tmp[0] == '/' && tmp[2] == ':')
tmp = tmp.mid(1);
#endif
return QFileInfo(tmp).absoluteFilePath();
}
static QByteArray fromLocalFile(const QString & str)
{
return QUrl::fromLocalFile(str).toEncoded();
}
static bool pluginEnabled()
{
for (PluginHandle * plugin : aud_plugin_list(PluginType::Transport))
{
if (!aud_plugin_get_enabled(plugin))
continue;
auto tp = reinterpret_cast<const TransportPlugin *>(
aud_plugin_get_header(plugin));
if (!tp)
continue;
for (const char * scheme : tp->schemes)
{
if (scheme && !strcmp(scheme, HOOKED_SCHEME))
return true;
}
}
return false;
}
static StringBuf uri_get_scheme_patched(const char * uri)
{
const char * delim = strstr(uri, "://");
if (!delim || !strcmp_nocase(uri, FILE_SCHEME, delim - uri))
{
/// @note Override scheme only for network files
const QString localFile = toLocalFile(uri);
if (localFile.startsWith("//") || localFile.startsWith("\\\\"))
{
/// @note Avoid earlier pluginEnabled() check due to possible
/// deadlock if function is called from other plugins for some
/// internal files or configs
/// Example: `file://C:\Users\User/.adplug/adplug.db`
/// https://github.com/AlienCowEatCake/audacious-qfiletransport/issues/1
if (pluginEnabled())
{
StringBuf result;
result.insert(0, HOOKED_SCHEME);
return result;
}
}
}
return delim ? str_copy(uri, delim - uri) : StringBuf();
}
struct SearchParams
{
QString filename;
QStringList include, exclude;
};
static bool hasFrontCoverExtension(const QString & name)
{
const QString ext = QFileInfo(name).suffix();
if (ext.isEmpty())
return false;
static const QStringList exts = {"jpg", "jpeg", "png", "webp"};
return exts.contains(ext, Qt::CaseInsensitive);
}
static bool coverNameFilter(const QString & name, const QStringList & keywords,
bool retOnEmpty)
{
if (keywords.isEmpty())
return retOnEmpty;
for (const QString & keyword : keywords)
{
if (name.contains(keyword, Qt::CaseInsensitive))
return true;
}
return false;
}
static bool sameBasename(const QString & a, const QString & b)
{
const QString bnA = QFileInfo(a).completeBaseName();
const QString bnB = QFileInfo(b).completeBaseName();
return bnA.compare(bnB, Qt::CaseInsensitive) == 0;
}
static QString fileinfoRecursiveGetImage(const QString & path,
const SearchParams * params, int depth)
{
const QDir pathDir(path);
if (!pathDir.exists() || !pathDir.isReadable())
return QString();
if (aud_get_bool("use_file_cover") && !depth)
{
// Look for images matching file name
QDirIterator it(path, QDir::Files | QDir::Readable);
while (!it.next().isEmpty())
{
const QFileInfo fileInfo = it.fileInfo();
const QString name = fileInfo.fileName();
if (hasFrontCoverExtension(name) &&
sameBasename(name, params->filename))
{
return fileInfo.absoluteFilePath();
}
}
}
// Search for files using filter
QDirIterator it(path, QDir::Files | QDir::Readable);
while (!it.next().isEmpty())
{
const QFileInfo fileInfo = it.fileInfo();
const QString name = fileInfo.fileName();
if (hasFrontCoverExtension(name) &&
coverNameFilter(name, params->include, true) &&
!coverNameFilter(name, params->exclude, false))
{
return fileInfo.absoluteFilePath();
}
}
if (aud_get_bool("recurse_for_cover") &&
depth < aud_get_int("recurse_for_cover_depth"))
{
// Descend into directories recursively.
QDirIterator it(path,
QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable);
while (!it.next().isEmpty())
{
const QFileInfo fileInfo = it.fileInfo();
const QString newpath = fileInfo.absoluteFilePath();
const QString tmp =
fileinfoRecursiveGetImage(newpath, params, depth + 1);
if (!tmp.isEmpty())
return tmp;
}
}
return QString();
}
static QStringList strListToQStringList(const char * list, const char * delims)
{
const Index<String> index = str_list_to_index(list, delims);
QStringList result;
for (const String & str : index)
result.append(QString::fromLocal8Bit(static_cast<const char *>(str)));
return result;
}
static String art_search_patched(const char * filename)
{
QString local = toLocalFile(filename);
if (local.isEmpty())
return String();
const QString elem = QFileInfo(local).fileName();
if (elem.isEmpty())
return String();
String include = aud_get_str("cover_name_include");
String exclude = aud_get_str("cover_name_exclude");
SearchParams params = {elem, strListToQStringList(include, ", "),
strListToQStringList(exclude, ", ")};
local = QFileInfo(local).absolutePath();
const QString imageLocal = fileinfoRecursiveGetImage(local, ¶ms, 0);
return imageLocal.isEmpty() ? String()
: String(fromLocalFile(imageLocal).constData());
}
static void installUriSchemeHook()
{
#if defined(_WIN32)
#if !defined(AUDCORE_DLL_NAME)
#define AUDCORE_DLL_NAME "audcore"
#endif
HINSTANCE audcoreLib = LoadLibraryA(AUDCORE_DLL_NAME ".dll");
if (audcoreLib)
{
#if defined(_M_IX86)
LPVOID originalAddressUriGetScheme = reinterpret_cast<LPVOID>(
GetProcAddress(audcoreLib, "_Z14uri_get_schemePKc"));
if (originalAddressUriGetScheme)
{
LPVOID patchedAddressUriGetScheme =
reinterpret_cast<LPVOID>(&uri_get_scheme_patched);
constexpr size_t jumpSize = 1 + sizeof(LPVOID);
const size_t jumpOffset =
reinterpret_cast<size_t>(patchedAddressUriGetScheme) -
(reinterpret_cast<size_t>(originalAddressUriGetScheme) +
jumpSize);
char patch[jumpSize];
memcpy(patch, "\xE9", 1);
memcpy(patch + 1, &jumpOffset, sizeof(LPVOID));
WriteProcessMemory(GetCurrentProcess(), originalAddressUriGetScheme,
patch, jumpSize, Q_NULLPTR);
}
LPVOID originalAddressArtSearch = reinterpret_cast<LPVOID>(
GetProcAddress(audcoreLib, "_Z10art_searchPKc"));
if (originalAddressArtSearch)
{
LPVOID patchedAddressArtSearch =
reinterpret_cast<LPVOID>(&art_search_patched);
constexpr size_t jumpSize = 1 + sizeof(LPVOID);
const size_t jumpOffset =
reinterpret_cast<size_t>(patchedAddressArtSearch) -
(reinterpret_cast<size_t>(originalAddressArtSearch) + jumpSize);
char patch[jumpSize];
memcpy(patch, "\xE9", 1);
memcpy(patch + 1, &jumpOffset, sizeof(LPVOID));
WriteProcessMemory(GetCurrentProcess(), originalAddressArtSearch,
patch, jumpSize, Q_NULLPTR);
}
#elif defined(_M_X64)
LPVOID originalAddressUriGetScheme = reinterpret_cast<LPVOID>(
GetProcAddress(audcoreLib, "_Z14uri_get_schemePKc"));
if (originalAddressUriGetScheme)
{
LPVOID patchedAddressUriGetScheme =
reinterpret_cast<LPVOID>(&uri_get_scheme_patched);
constexpr size_t jumpSize = 2 + sizeof(LPVOID) + 3;
char patch[jumpSize];
memcpy(patch, "\x49\xBA", 2);
memcpy(patch + 2, &patchedAddressUriGetScheme, sizeof(LPVOID));
memcpy(patch + 2 + sizeof(LPVOID), "\x41\xFF\xE2", 3);
WriteProcessMemory(GetCurrentProcess(), originalAddressUriGetScheme,
patch, jumpSize, Q_NULLPTR);
}
LPVOID originalAddressArtSearch = reinterpret_cast<LPVOID>(
GetProcAddress(audcoreLib, "_Z10art_searchPKc"));
if (originalAddressArtSearch)
{
LPVOID patchedAddressArtSearch =
reinterpret_cast<LPVOID>(&art_search_patched);
constexpr size_t jumpSize = 2 + sizeof(LPVOID) + 3;
char patch[jumpSize];
memcpy(patch, "\x49\xBA", 2);
memcpy(patch + 2, &patchedAddressArtSearch, sizeof(LPVOID));
memcpy(patch + 2 + sizeof(LPVOID), "\x41\xFF\xE2", 3);
WriteProcessMemory(GetCurrentProcess(), originalAddressArtSearch,
patch, jumpSize, Q_NULLPTR);
}
#endif
FreeLibrary(audcoreLib);
}
#endif
}
class QFileTransport : public TransportPlugin
{
public:
#if !defined(PLUGIN_VERSION)
#define PLUGIN_VERSION ""
#endif
static constexpr PluginInfo info = {
"QFileTransport", "QFileTransport",
"QFileTransport Plugin for Audacious " PLUGIN_VERSION "\n"
"https://github.com/AlienCowEatCake/audacious-qfiletransport\n\n"
"Copyright (C) 2023-2025 Peter S. Zhigalov",
Q_NULLPTR, PluginQtOnly};
static constexpr const char * const schemes[] = {FILE_SCHEME,
HOOKED_SCHEME};
QFileTransport() : TransportPlugin(info, schemes)
{
installUriSchemeHook();
}
void cleanup() Q_DECL_OVERRIDE
{
/// @note Clear timestamp from plugin registry to avoid plugin caching
/// Cached plugins are lazy-loaded, but we need to install hook
StringBuf path =
filename_build({aud_get_path(AudPath::UserDir), "plugin-registry"});
QFile file((QString(path)));
if (!file.open(QFile::ReadWrite | QFile::Text))
return;
QString registryData = QString::fromUtf8(file.readAll());
const QRegularExpression re("(qfiletransport\\.dll[\r\n]+stamp )[0-9]*",
QRegularExpression::MultilineOption);
const QRegularExpressionMatch match = re.match(registryData);
if (match.hasMatch())
registryData.replace(re, match.captured(1) + "0");
file.seek(0);
file.resize(0);
file.write(registryData.toUtf8());
file.close();
}
VFSImpl * fopen(const char * path, const char * mode,
String & error) Q_DECL_OVERRIDE
{
File * file = new File(path, mode);
const QString errorString = file->errorString();
if (!errorString.isEmpty())
{
delete file;
file = Q_NULLPTR;
error = String(errorString.toLocal8Bit().data());
}
return file;
}
VFSFileTest test_file(const char * filename, VFSFileTest test,
String & error) Q_DECL_OVERRIDE
{
int result = 0;
const QFileInfo info(toLocalFile(filename));
if (info.isFile())
result |= VFS_IS_REGULAR;
if (info.isSymLink())
result |= VFS_IS_SYMLINK;
if (info.isDir())
result |= VFS_IS_DIR;
if (info.isExecutable())
result |= VFS_IS_EXECUTABLE;
if (info.exists())
result |= VFS_EXISTS;
if (!info.isReadable())
result |= VFS_NO_ACCESS;
return VFSFileTest(test & result);
}
Index<String> read_folder(const char * filename,
String & error) Q_DECL_OVERRIDE
{
Index<String> result;
const QString path = toLocalFile(filename);
QDirIterator it(path, QDir::Files | QDir::NoDotAndDotDot,
QDirIterator::Subdirectories);
while (it.hasNext())
{
const QString file = it.next();
if (it.fileInfo().isHidden())
continue;
result.append(fromLocalFile(file).constData());
}
return result;
}
private:
class File : public VFSImpl
{
public:
File(const char * path, const char * mode) : m_file(toLocalFile(path))
{
QIODevice::OpenMode openMode = QIODevice::NotOpen;
switch (mode[0])
{
case 'r':
if (strchr(mode, '+'))
openMode |= QIODevice::ReadWrite | QIODevice::ExistingOnly;
else
openMode |= QIODevice::ReadOnly | QIODevice::ExistingOnly;
break;
case 'w':
if (strchr(mode, '+'))
openMode |= QIODevice::ReadWrite | QIODevice::Truncate;
else
openMode |= QIODevice::WriteOnly | QIODevice::Truncate;
break;
case 'a':
if (strchr(mode, '+'))
openMode |= QIODevice::ReadWrite | QIODevice::Append;
else
openMode |= QIODevice::WriteOnly | QIODevice::Append;
break;
default:
m_errorString = "Invalid open mode";
return;
}
if (!m_file.open(openMode))
m_errorString = "Error in QFile::open()";
}
QString errorString() const
{
return m_errorString;
}
protected:
int64_t fread(void * ptr, int64_t size, int64_t nmemb) Q_DECL_OVERRIDE
{
qint64 total = 0;
qint64 remain = size * nmemb;
char * curr = reinterpret_cast<char *>(ptr);
while (remain > 0)
{
qint64 part = m_file.read(curr, remain);
if (part <= 0)
break;
curr += part;
total += part;
remain -= part;
}
return (size > 0) ? total / size : 0;
}
int64_t fwrite(const void * buf, int64_t size,
int64_t nitems) Q_DECL_OVERRIDE
{
qint64 total = 0;
qint64 remain = size * nitems;
const char * curr = reinterpret_cast<const char *>(buf);
while (remain > 0)
{
qint64 part = m_file.write(curr, remain);
if (part <= 0)
break;
curr += part;
total += part;
remain -= part;
}
return (size > 0) ? total / size : 0;
}
int fseek(int64_t offset, VFSSeekType whence) Q_DECL_OVERRIDE
{
qint64 pos = 0;
switch (whence)
{
case VFS_SEEK_SET:
pos = offset;
break;
case VFS_SEEK_CUR:
pos = m_file.pos() + offset;
break;
case VFS_SEEK_END:
pos = m_file.size() + offset;
break;
default:
return -1;
}
return m_file.seek(pos) ? 0 : -1;
}
int64_t ftell() Q_DECL_OVERRIDE
{
return m_file.pos();
}
bool feof() Q_DECL_OVERRIDE
{
return m_file.atEnd();
}
int ftruncate(int64_t length) Q_DECL_OVERRIDE
{
return m_file.resize(length) ? 0 : -1;
}
int64_t fsize() Q_DECL_OVERRIDE
{
return m_file.size();
}
int fflush() Q_DECL_OVERRIDE
{
return m_file.flush() ? 0 : -1;
}
private:
QFile m_file;
QString m_errorString;
};
};
EXPORT QFileTransport aud_plugin_instance;