#include "Python.h"
#include "pycore_fileutils.h"
#include "pycore_pystate.h"
#ifdef HAVE_DIRECT_H
#include <direct.h>
#endif
#include <ctype.h>
#include "importdl.h"
#include "patchlevel.h"
#include <windows.h>
#ifdef _DEBUG
#define PYD_DEBUG_SUFFIX "_d"
#else
#define PYD_DEBUG_SUFFIX ""
#endif
#ifdef PYD_PLATFORM_TAG
#define PYD_TAGGED_SUFFIX PYD_DEBUG_SUFFIX ".cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) "-" PYD_PLATFORM_TAG ".pyd"
#else
#define PYD_TAGGED_SUFFIX PYD_DEBUG_SUFFIX ".cp" Py_STRINGIFY(PY_MAJOR_VERSION) Py_STRINGIFY(PY_MINOR_VERSION) ".pyd"
#endif
#define PYD_UNTAGGED_SUFFIX PYD_DEBUG_SUFFIX ".pyd"
const char *_PyImport_DynLoadFiletab[] = {
PYD_TAGGED_SUFFIX,
PYD_UNTAGGED_SUFFIX,
NULL
};
#define DWORD_AT(mem) (*(DWORD *)(mem))
#define WORD_AT(mem) (*(WORD *)(mem))
static char *GetPythonImport (HINSTANCE hModule)
{
unsigned char *dllbase, *import_data, *import_name;
DWORD pe_offset, opt_offset;
WORD opt_magic;
int num_dict_off, import_off;
if (hModule == NULL) {
return NULL;
}
dllbase = (unsigned char *)hModule;
pe_offset = DWORD_AT(dllbase + 0x3C);
if (memcmp(dllbase+pe_offset,"PE\0\0",4)) {
return NULL;
}
opt_offset = pe_offset + 4 + 20;
opt_magic = WORD_AT(dllbase+opt_offset);
if (opt_magic == 0x10B) {
num_dict_off = 92;
import_off = 104;
} else if (opt_magic == 0x20B) {
num_dict_off = 108;
import_off = 120;
} else {
return NULL;
}
if (DWORD_AT(dllbase + opt_offset + num_dict_off) >= 2) {
if (0 == DWORD_AT(dllbase + opt_offset + import_off + sizeof(DWORD)))
return NULL;
import_data = dllbase + DWORD_AT(dllbase +
opt_offset +
import_off);
while (DWORD_AT(import_data)) {
import_name = dllbase + DWORD_AT(import_data+12);
if (strlen(import_name) >= 6 &&
!strncmp(import_name,"python",6)) {
char *pch;
#ifdef _DEBUG
if (strcmp(import_name, "python3_d.dll") == 0) {
#else
if (strcmp(import_name, "python3.dll") == 0) {
#endif
import_data += 20;
continue;
}
pch = import_name + 6;
#ifdef _DEBUG
while (*pch && pch[0] != '_' && pch[1] != 'd' && pch[2] != '.') {
#else
while (*pch && *pch != '.') {
#endif
if (*pch >= '0' && *pch <= '9') {
pch++;
} else {
pch = NULL;
break;
}
}
if (pch) {
return import_name;
}
}
import_data += 20;
}
}
return NULL;
}
#ifdef Py_ENABLE_SHARED
extern HMODULE PyWin_DLLhModule;
static int
_Py_CheckPython3(void)
{
static int python3_checked = 0;
static HANDLE hPython3;
#define MAXPATHLEN 512
wchar_t py3path[MAXPATHLEN+1];
if (python3_checked) {
return hPython3 != NULL;
}
python3_checked = 1;
if (PyWin_DLLhModule && GetModuleFileNameW(PyWin_DLLhModule, py3path, MAXPATHLEN)) {
wchar_t *p = wcsrchr(py3path, L'\\');
if (p) {
wcscpy(p + 1, PY3_DLLNAME);
hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
if (hPython3 != NULL) {
return 1;
}
}
}
hPython3 = LoadLibraryExW(PY3_DLLNAME, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR);
if (hPython3 != NULL) {
return 1;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
PyConfig *config = (PyConfig*)_PyInterpreterState_GetConfig(interp);
assert(config->prefix);
if (config->prefix) {
wcscpy_s(py3path, MAXPATHLEN, config->prefix);
if (py3path[0] && _Py_add_relfile(py3path, L"DLLs\\" PY3_DLLNAME, MAXPATHLEN) >= 0) {
hPython3 = LoadLibraryExW(py3path, NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
}
}
return hPython3 != NULL;
#undef MAXPATHLEN
}
#endif
dl_funcptr _PyImport_FindSharedFuncptrWindows(const char *prefix,
const char *shortname,
PyObject *pathname, FILE *fp)
{
dl_funcptr p;
char funcname[258], *import_python;
#ifdef Py_ENABLE_SHARED
_Py_CheckPython3();
#endif
wchar_t *wpathname = PyUnicode_AsWideCharString(pathname, NULL);
if (wpathname == NULL)
return NULL;
PyOS_snprintf(funcname, sizeof(funcname), "%.20s_%.200s", prefix, shortname);
{
HINSTANCE hDLL = NULL;
#ifdef MS_WINDOWS_DESKTOP
unsigned int old_mode;
old_mode = SetErrorMode(SEM_FAILCRITICALERRORS);
#endif
Py_BEGIN_ALLOW_THREADS
hDLL = LoadLibraryExW(wpathname, NULL,
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS |
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR);
Py_END_ALLOW_THREADS
PyMem_Free(wpathname);
#ifdef MS_WINDOWS_DESKTOP
SetErrorMode(old_mode);
#endif
if (hDLL==NULL){
PyObject *message;
unsigned int errorCode;
wchar_t theInfo[256];
int theLength;
errorCode = GetLastError();
theLength = FormatMessageW(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
errorCode,
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),
theInfo,
sizeof(theInfo) / sizeof(wchar_t),
NULL);
if (theLength == 0) {
message = PyUnicode_FromFormat(
"DLL load failed with error code %u while importing %s",
errorCode, shortname);
} else {
if (theLength >= 2 &&
theInfo[theLength-2] == '\r' &&
theInfo[theLength-1] == '\n') {
theLength -= 2;
theInfo[theLength] = '\0';
}
message = PyUnicode_FromFormat(
"DLL load failed while importing %s: ", shortname);
PyUnicode_AppendAndDel(&message,
PyUnicode_FromWideChar(
theInfo,
theLength));
}
if (message != NULL) {
PyObject *shortname_obj = PyUnicode_FromString(shortname);
PyErr_SetImportError(message, shortname_obj, pathname);
Py_XDECREF(shortname_obj);
Py_DECREF(message);
}
return NULL;
} else {
char buffer[256];
PyOS_snprintf(buffer, sizeof(buffer),
#ifdef _DEBUG
"python%d%d_d.dll",
#else
"python%d%d.dll",
#endif
PY_MAJOR_VERSION,PY_MINOR_VERSION);
import_python = GetPythonImport(hDLL);
if (import_python &&
_stricmp(buffer,import_python)) {
PyErr_Format(PyExc_ImportError,
"Module use of %.150s conflicts "
"with this version of Python.",
import_python);
Py_BEGIN_ALLOW_THREADS
FreeLibrary(hDLL);
Py_END_ALLOW_THREADS
return NULL;
}
}
Py_BEGIN_ALLOW_THREADS
p = GetProcAddress(hDLL, funcname);
Py_END_ALLOW_THREADS
}
return p;
}