175 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
112 lines
5.2 KiB
C
112 lines
5.2 KiB
C
#include "wine_bundle_paths.h"
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
static int slash(wchar_t c) { return c == L'\\' || c == L'/'; }
|
|
static wchar_t fold(wchar_t c) {
|
|
if (c >= L'A' && c <= L'Z') return c + (L'a' - L'A');
|
|
return slash(c) ? L'\\' : c;
|
|
}
|
|
static size_t bounded_length(const wchar_t *s, size_t cap) {
|
|
size_t n = 0;
|
|
if (!s) return cap;
|
|
while (n < cap && s[n]) ++n;
|
|
return n;
|
|
}
|
|
static int append(wchar_t *dst, size_t root_len, const wchar_t *suffix) {
|
|
size_t i = 0;
|
|
while (suffix[i] && root_len + i + 1 < GW_BUNDLE_PATH_CAP) {
|
|
dst[root_len + i] = suffix[i];
|
|
++i;
|
|
}
|
|
if (suffix[i]) return 0;
|
|
dst[root_len + i] = 0;
|
|
return 1;
|
|
}
|
|
int gw_bundle_paths_resolve(enum gw_bundle_platform platform,
|
|
const wchar_t *exe, struct gw_bundle_paths *out) {
|
|
static const wchar_t wine_suffix[] = L"\\runtime\\native-client\\_classic_beta_\\WowB.exe";
|
|
static const wchar_t native_suffix[] = L"\\WowB.exe";
|
|
static const wchar_t win_backend[] = L"\\GWNativeBackend.dll";
|
|
static const wchar_t win_codec[] = L"\\forever_codec.dll";
|
|
static const wchar_t wine_backend[] = L"\\build\\libgateway_wine_wire.so";
|
|
static const wchar_t wine_core[] = L"\\build\\libgateway_native.so";
|
|
static const wchar_t wine_codec[] = L"\\build\\forever-schema\\libforever_codec.so";
|
|
static const wchar_t nt_prefix[] = L"\\??\\";
|
|
size_t n, s, root_len, i, component;
|
|
int unc;
|
|
const wchar_t *backend, *codec, *suffix;
|
|
if (!out) return -1;
|
|
out->root[0] = out->backend[0] = out->core[0] = out->codec[0] = out->wine_loader_name[0] = 0;
|
|
if (platform != GW_BUNDLE_NATIVE_WINDOWS && platform != GW_BUNDLE_WINE) return -1;
|
|
n = bounded_length(exe, GW_BUNDLE_PATH_CAP);
|
|
/* The PE-only bundle lives beside the executable on Windows and Wine.
|
|
* Preserve the separate legacy Wine/Unix bundle layout. */
|
|
suffix = platform == GW_BUNDLE_NATIVE_WINDOWS ? native_suffix : wine_suffix;
|
|
s = bounded_length(suffix, GW_BUNDLE_PATH_CAP);
|
|
if (n >= GW_BUNDLE_PATH_CAP || n < s + 2) return -1;
|
|
unc = slash(exe[0]) && slash(exe[1]);
|
|
if (unc) {
|
|
/* Native Windows supports UNC roots. The existing Wine loader
|
|
* contract only establishes drive-qualified NT paths. */
|
|
if (platform != GW_BUNDLE_NATIVE_WINDOWS || slash(exe[2])) return -1;
|
|
component = 2;
|
|
} else {
|
|
if (!((exe[0] >= L'A' && exe[0] <= L'Z') || (exe[0] >= L'a' && exe[0] <= L'z')) ||
|
|
exe[1] != L':' || !slash(exe[2])) return -1;
|
|
component = 3;
|
|
}
|
|
root_len = n - s;
|
|
if (root_len < 2 || slash(exe[root_len - 1])) return -1;
|
|
if (unc) {
|
|
int share_separator = 0;
|
|
for (i = 2; i < root_len; ++i) if (slash(exe[i])) share_separator = 1;
|
|
if (!share_separator) return -1;
|
|
}
|
|
for (i = 0; i < s; ++i) if (fold(exe[root_len + i]) != fold(suffix[i])) return -1;
|
|
/* Reject ambiguous components rather than normalize traversal or device
|
|
* syntax. GetModuleFileNameW returns the normal DOS spelling we support. */
|
|
for (i = component; i <= n; ++i) {
|
|
wchar_t c = exe[i];
|
|
if (c && (c < 32 || c == L':' || c == L'*' || c == L'?' || c == L'"' || c == L'<' || c == L'>' || c == L'|')) return -1;
|
|
if (!c || slash(c)) {
|
|
size_t len = i - component;
|
|
if (!len || (len == 1 && exe[component] == L'.') ||
|
|
(len == 2 && exe[component] == L'.' && exe[component + 1] == L'.')) return -1;
|
|
component = i + 1;
|
|
}
|
|
}
|
|
backend = platform == GW_BUNDLE_WINE ? wine_backend : win_backend;
|
|
codec = platform == GW_BUNDLE_WINE ? wine_codec : win_codec;
|
|
if (root_len + bounded_length(backend, GW_BUNDLE_PATH_CAP) + 1 >= GW_BUNDLE_PATH_CAP ||
|
|
root_len + bounded_length(codec, GW_BUNDLE_PATH_CAP) + 1 >= GW_BUNDLE_PATH_CAP ||
|
|
(platform == GW_BUNDLE_WINE && root_len + bounded_length(wine_core, GW_BUNDLE_PATH_CAP) + 1 >= GW_BUNDLE_PATH_CAP) ||
|
|
(platform == GW_BUNDLE_WINE && 4 + root_len + bounded_length(backend, GW_BUNDLE_PATH_CAP) + 1 >= GW_BUNDLE_PATH_CAP)) return -1;
|
|
for (i = 0; i < root_len; ++i) {
|
|
wchar_t c = slash(exe[i]) ? L'\\' : exe[i];
|
|
out->root[i] = out->backend[i] = out->codec[i] = c;
|
|
if (platform == GW_BUNDLE_WINE) out->core[i] = c;
|
|
}
|
|
if (!unc && root_len == 2) { out->root[root_len] = L'\\'; out->root[root_len + 1] = 0; }
|
|
else out->root[root_len] = 0;
|
|
if (!append(out->backend, root_len, backend) || !append(out->codec, root_len, codec)) return -1;
|
|
if (platform == GW_BUNDLE_WINE) {
|
|
size_t len;
|
|
if (!append(out->core, root_len, wine_core)) return -1;
|
|
for (i = 0; i < 4; ++i) out->wine_loader_name[i] = nt_prefix[i];
|
|
len = bounded_length(out->backend, GW_BUNDLE_PATH_CAP);
|
|
for (i = 0; i <= len; ++i) out->wine_loader_name[i + 4] = out->backend[i];
|
|
}
|
|
return 0;
|
|
}
|
|
#ifdef _WIN32
|
|
int gw_bundle_paths_current(enum gw_bundle_platform platform,
|
|
struct gw_bundle_paths *out) {
|
|
wchar_t exe[GW_BUNDLE_PATH_CAP];
|
|
DWORD n = GetModuleFileNameW(NULL, exe, GW_BUNDLE_PATH_CAP);
|
|
if (!n || n >= GW_BUNDLE_PATH_CAP) return -1;
|
|
return gw_bundle_paths_resolve(platform, exe, out);
|
|
}
|
|
#endif
|