6#pragma comment(lib, "Ntdll.lib")
9#pragma comment (lib, "shlwapi.lib")
18#define LONG_PATH_PREFIX "\\\\?\\"
19#define GLOBALROOT_NAME "GLOBALROOT"
42 OBJECT_ATTRIBUTES objAttribs = { 0 };
44 UNICODE_STRING unicodeString;
45 RtlInitUnicodeString(&unicodeString, filePath);
47 InitializeObjectAttributes(&objAttribs, &unicodeString, OBJ_CASE_INSENSITIVE, NULL, NULL);
49 const int allocSize = 2048;
50 LARGE_INTEGER largeInteger;
51 largeInteger.QuadPart = allocSize;
53 IO_STATUS_BLOCK ioStatusBlock = { 0 };
59 FILE_ATTRIBUTE_NORMAL,
62 FILE_NON_DIRECTORY_FILE,
66 if (status != STATUS_SUCCESS) {
67 std::wcerr <<
"Cannot open file: " << filePath <<
". Error: " << std::hex << status << std::endl;
75 IO_STATUS_BLOCK status_block = { 0 };
77 struct MY_FILE_NAME_INFORMATION {
82 memset(&name_info, 0,
sizeof(MY_FILE_NAME_INFORMATION));
84 NTSTATUS status = ZwQueryInformationFile(
hFile, &status_block, &name_info,
sizeof(MY_FILE_NAME_INFORMATION), FileNameInformation);
85 if (status != STATUS_SUCCESS) {
88 std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
89 std::string my_string = converter.to_bytes(name_info.FileName);
101 if (path[1] ==
':') {
105 if (path[0] ==
'\\' && path[1] ==
'\\') {
117 if ((path[0] >=
'a' && path[0] <=
'z')
118 || (path[0] >=
'A' && path[0] <=
'Z'))
120 if (path[1] ==
':') {
130 size_t full_path_size = full_path.length();
131 if (full_path_size == 0) {
135 DWORD drives_bitmask = GetLogicalDrives();
138 for (
DWORD i = 0; i < 32; i += 1, drives_bitmask >>= 1) {
139 if ((drives_bitmask & 1) == 1) {
140 char letter[] =
"?:";
141 letter[0] =
'A' + (char)i;
144 if (!QueryDosDeviceA(letter, out_path,
MAX_PATH)) {
150 const char delim[] =
";";
151 char *next_token =
nullptr;
153 char * pch = strtok_s(out_path, delim, &next_token);
154 while (pch !=
nullptr) {
156 std::size_t found = full_path.find(pch);
157 if (found != std::string::npos && found == 0) {
158 size_t dir_len = strlen(pch);
160 std::string str2 = full_path.substr(dir_len, full_path_size);
161 if (str2[0] !=
'/' && str2[0] !=
'\\') {
164 return letter + str2;
166 pch = strtok_s(
nullptr, delim, &next_token);
177 GetCurrentDirectoryA(
MAX_PATH, current_dir);
178 path = std::string(current_dir) +
"\\" + path;
181 PathCanonicalizeA(out_path, path.c_str());
182 return std::string(out_path);
186 for (
size_t i = 0; i < str.length(); ++i) {
209 if (stripped_path.length() < 3) {
214 return stripped_path;
217 const char *szModName = stripped_path.c_str();
218 std::wstring unicode_name(szModName, szModName + strlen(szModName));
220 if (
hFile ==
nullptr) {
230 std::string path = full_path;
234 if (remapped_path == path)
break;
235 path = remapped_path;
242 const std::string device_path =
"\\Device\\";
243 if (path.length() < device_path.length() || path[0] !=
'\\') {
246 if (path.compare(0, device_path.length(), device_path) == 0){
265 if (GetLongPathNameA(abs_path.c_str(), filename,
MAX_PATH) == 0) {
266 size_t len = abs_path.length();
269 memcpy(filename, abs_path.c_str(), len);
std::string replace_char(std::string &str, char ch1, char ch2)
std::string nt_retrieve_file_path(HANDLE hFile)
std::string expand_path(const std::string &path)
char * get_subpath_ptr(char *modulePath, char *searchedPath)
std::string strip_prefix(std::string path, std::string prefix)
std::string remap_to_drive_letter(const std::string &full_path)
NTSTATUS(NTAPI *_RtlCreateProcessReflection)(HANDLE ProcessHandle
std::string convert_to_win32_path(const std::string &path)
std::string device_path_to_win32_path(const std::string &full_path)
bool is_relative(const char *path, size_t path_len)
bool is_disk_relative(const char *path, size_t path_len)
HANDLE nt_create_file(PCWSTR filePath)
std::string get_system_drive()
DWORD(__stdcall *_PssCaptureSnapshot)(HANDLE ProcessHandle
bool convert_to_wow64_path(char *szModName)
std::string relative_to_absolute_path(std::string path)
bool is_device_path(const std::string &path)
char g_Syswow64Path[MAX_PATH]
char g_System32Path[MAX_PATH]