|
3 | 3 | #include "DynBuf.h"
|
4 | 4 | #include "NativeWinApi.h"
|
5 | 5 |
|
6 |
| -extern "C" __declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSize) |
| 6 | +extern "C" __declspec(dllexport) bool DevicePathToPathW(const wchar_t* szDevicePath, wchar_t* szPath, size_t nSizeInChars) |
7 | 7 | {
|
8 | 8 | DeviceNameResolver deviceNameResolver;
|
9 | 9 | wchar_t targetPath[MAX_PATH] = L"";
|
10 | 10 | if(!deviceNameResolver.resolveDeviceLongNameToShort(szDevicePath, targetPath))
|
11 | 11 | return false;
|
12 |
| - wcscpy_s(szPath, nSize / sizeof(wchar_t), targetPath); |
| 12 | + wcsncpy_s(szPath, nSizeInChars, targetPath, _TRUNCATE); |
13 | 13 | return true;
|
14 | 14 | }
|
15 | 15 |
|
16 |
| -extern "C" __declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSize) |
| 16 | +extern "C" __declspec(dllexport) bool DevicePathToPathA(const char* szDevicePath, char* szPath, size_t nSizeInChars) |
17 | 17 | {
|
18 | 18 | size_t len = strlen(szDevicePath);
|
19 |
| - DynBuf newDevicePathBuf((len + 1)*sizeof(wchar_t)); |
| 19 | + DynBuf newDevicePathBuf((len + 1) * sizeof(wchar_t)); |
20 | 20 | wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
|
21 | 21 | *newDevicePath = L'\0';
|
22 |
| - if(MultiByteToWideChar(CP_ACP, NULL, szDevicePath, -1, newDevicePath, (int)len + 1)) |
| 22 | + if(MultiByteToWideChar(CP_ACP, NULL, szDevicePath, -1, newDevicePath, int(len + 1))) |
23 | 23 | {
|
24 |
| - DynBuf newPathBuf(nSize * sizeof(wchar_t)); |
| 24 | + DynBuf newPathBuf((nSizeInChars + 1) * sizeof(wchar_t)); |
25 | 25 | wchar_t* newPath = (wchar_t*)newPathBuf.GetPtr();
|
26 |
| - if(!DevicePathToPathW(newDevicePath, newPath, nSize * sizeof(wchar_t))) |
| 26 | + if(!DevicePathToPathW(newDevicePath, newPath, nSizeInChars)) |
27 | 27 | return false;
|
28 |
| - if(!WideCharToMultiByte(CP_ACP, NULL, newPath, -1, szPath, (int)wcslen(newPath) + 1, NULL, NULL)) |
| 28 | + if(!WideCharToMultiByte(CP_ACP, NULL, newPath, -1, szPath, int(wcslen(newPath)) + 1, NULL, NULL)) |
29 | 29 | return false;
|
30 | 30 | }
|
31 | 31 | return true;
|
32 | 32 | }
|
33 | 33 |
|
34 |
| -__declspec(dllexport) bool DevicePathFromFileHandleW(HANDLE hFile, wchar_t* szDevicePath, size_t nSize) |
| 34 | +__declspec(dllexport) bool DevicePathFromFileHandleW(HANDLE hFile, wchar_t* szDevicePath, size_t nSizeInChars) |
35 | 35 | {
|
36 | 36 | NativeWinApi::initialize();
|
37 | 37 | ULONG ReturnLength;
|
38 | 38 | bool bRet = false;
|
39 | 39 | if(NativeWinApi::NtQueryObject(hFile, ObjectNameInformation, 0, 0, &ReturnLength) == STATUS_INFO_LENGTH_MISMATCH)
|
40 | 40 | {
|
41 | 41 | ReturnLength += 0x2000; //on Windows XP SP3 ReturnLength will not be set just add some buffer space to fix this
|
42 |
| - POBJECT_NAME_INFORMATION NameInformation = (POBJECT_NAME_INFORMATION)GlobalAlloc(0, ReturnLength); |
| 42 | + POBJECT_NAME_INFORMATION NameInformation = POBJECT_NAME_INFORMATION(GlobalAlloc(0, ReturnLength)); |
43 | 43 | if(NativeWinApi::NtQueryObject(hFile, ObjectNameInformation, NameInformation, ReturnLength, 0) == STATUS_SUCCESS)
|
44 | 44 | {
|
45 | 45 | NameInformation->Name.Buffer[NameInformation->Name.Length / 2] = L'\0'; //null-terminate the UNICODE_STRING
|
46 |
| - if(wcslen(NameInformation->Name.Buffer) < nSize) |
47 |
| - { |
48 |
| - wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), NameInformation->Name.Buffer); |
49 |
| - bRet = true; |
50 |
| - } |
| 46 | + wcsncpy_s(szDevicePath, nSizeInChars, NameInformation->Name.Buffer, _TRUNCATE); |
| 47 | + bRet = true; |
51 | 48 | }
|
52 | 49 | GlobalFree(NameInformation);
|
53 | 50 | }
|
| 51 | + if(!bRet) |
| 52 | + return false; |
54 | 53 | if(_wcsnicmp(szDevicePath, L"\\Device\\LanmanRedirector\\", 25) == 0) // Win XP
|
55 | 54 | {
|
56 |
| - wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), L"\\\\"); |
57 |
| - wcscat_s(szDevicePath, nSize / sizeof(wchar_t), &szDevicePath[25]); |
| 55 | + wcsncpy_s(szDevicePath, nSizeInChars, L"\\\\", _TRUNCATE); |
| 56 | + wcsncat_s(szDevicePath, nSizeInChars, &szDevicePath[25], _TRUNCATE); |
58 | 57 | }
|
59 | 58 | else if(_wcsnicmp(szDevicePath, L"\\Device\\Mup\\", 12) == 0) // Win 7
|
60 | 59 | {
|
61 |
| - wcscpy_s(szDevicePath, nSize / sizeof(wchar_t), L"\\\\"); |
62 |
| - wcscat_s(szDevicePath, nSize / sizeof(wchar_t), &szDevicePath[12]); |
| 60 | + wcsncpy_s(szDevicePath, nSizeInChars, L"\\\\", _TRUNCATE); |
| 61 | + wcsncat_s(szDevicePath, nSizeInChars, &szDevicePath[12], _TRUNCATE); |
63 | 62 | }
|
64 |
| - return bRet; |
| 63 | + return true; |
65 | 64 | }
|
66 | 65 |
|
67 |
| -__declspec(dllexport) bool DevicePathFromFileHandleA(HANDLE hFile, char* szDevicePath, size_t nSize) |
| 66 | +__declspec(dllexport) bool DevicePathFromFileHandleA(HANDLE hFile, char* szDevicePath, size_t nSizeInChars) |
68 | 67 | {
|
69 |
| - DynBuf newDevicePathBuf(nSize * sizeof(wchar_t)); |
| 68 | + DynBuf newDevicePathBuf((nSizeInChars + 1) * sizeof(wchar_t)); |
70 | 69 | wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
|
71 |
| - if(!DevicePathFromFileHandleW(hFile, newDevicePath, nSize * sizeof(wchar_t))) |
| 70 | + if(!DevicePathFromFileHandleW(hFile, newDevicePath, nSizeInChars)) |
72 | 71 | return false;
|
73 |
| - if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szDevicePath, (int)wcslen(newDevicePath) + 1, NULL, NULL)) |
| 72 | + if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szDevicePath, int(wcslen(newDevicePath)) + 1, NULL, NULL)) |
74 | 73 | return false;
|
75 | 74 | return true;
|
76 | 75 | }
|
77 | 76 |
|
78 |
| -__declspec(dllexport) bool PathFromFileHandleW(HANDLE hFile, wchar_t* szPath, size_t nSize) |
| 77 | +__declspec(dllexport) bool PathFromFileHandleW(HANDLE hFile, wchar_t* szPath, size_t nSizeInChars) |
79 | 78 | {
|
80 | 79 | typedef DWORD (WINAPI * GETFINALPATHNAMEBYHANDLEW)(
|
81 |
| - IN HANDLE hFile, |
82 |
| - OUT wchar_t* lpszFilePath, |
83 |
| - IN DWORD cchFilePath, |
84 |
| - IN DWORD dwFlags |
| 80 | + IN HANDLE /*hFile*/, |
| 81 | + OUT wchar_t* /*lpszFilePath*/, |
| 82 | + IN DWORD /*cchFilePath*/, |
| 83 | + IN DWORD /*dwFlags*/ |
85 | 84 | );
|
86 |
| - static GETFINALPATHNAMEBYHANDLEW GetFPNBHW = (GETFINALPATHNAMEBYHANDLEW)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetFinalPathNameByHandleW"); |
87 |
| - if(GetFPNBHW && GetFPNBHW(hFile, szPath, (DWORD)(nSize / sizeof(wchar_t)), 0)) |
| 85 | + static GETFINALPATHNAMEBYHANDLEW GetFPNBHW = GETFINALPATHNAMEBYHANDLEW(GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetFinalPathNameByHandleW")); |
| 86 | + if(GetFPNBHW && GetFPNBHW(hFile, szPath, DWORD(nSizeInChars), 0)) |
88 | 87 | {
|
89 | 88 | if(_wcsnicmp(szPath, L"\\\\?\\UNC\\", 8) == 0) // Server path
|
90 | 89 | {
|
91 |
| - wcscpy_s(szPath, nSize / sizeof(wchar_t), L"\\\\"); |
92 |
| - wcscat_s(szPath, nSize / sizeof(wchar_t), &szPath[8]); |
| 90 | + wcsncpy_s(szPath, nSizeInChars, L"\\\\", _TRUNCATE); |
| 91 | + wcsncat_s(szPath, nSizeInChars, &szPath[8], _TRUNCATE); |
93 | 92 | }
|
94 | 93 | else if(_wcsnicmp(szPath, L"\\\\?\\", 4) == 0 && szPath[5] == L':') // Drive path
|
95 | 94 | {
|
96 |
| - wcscpy_s(szPath, nSize / sizeof(wchar_t), &szPath[4]); |
| 95 | + wcsncpy_s(szPath, nSizeInChars, &szPath[4], _TRUNCATE); |
97 | 96 | }
|
98 | 97 | return true;
|
99 | 98 | }
|
100 |
| - if(!DevicePathFromFileHandleW(hFile, szPath, nSize)) |
| 99 | + if(!DevicePathFromFileHandleW(hFile, szPath, nSizeInChars)) |
101 | 100 | return false;
|
102 | 101 | std::wstring oldPath(szPath);
|
103 |
| - if(!DevicePathToPathW(szPath, szPath, nSize)) |
104 |
| - wcscpy_s(szPath, nSize / sizeof(wchar_t), oldPath.c_str()); |
| 102 | + if(!DevicePathToPathW(szPath, szPath, nSizeInChars)) |
| 103 | + wcsncpy_s(szPath, nSizeInChars, oldPath.c_str(), _TRUNCATE); |
105 | 104 | return true;
|
106 | 105 | }
|
107 | 106 |
|
108 |
| -__declspec(dllexport) bool PathFromFileHandleA(HANDLE hFile, char* szPath, size_t nSize) |
| 107 | +__declspec(dllexport) bool PathFromFileHandleA(HANDLE hFile, char* szPath, size_t nSizeInChars) |
109 | 108 | {
|
110 |
| - DynBuf newDevicePathBuf(nSize * sizeof(wchar_t)); |
| 109 | + DynBuf newDevicePathBuf((nSizeInChars + 1) * sizeof(wchar_t)); |
111 | 110 | wchar_t* newDevicePath = (wchar_t*)newDevicePathBuf.GetPtr();
|
112 |
| - if(!PathFromFileHandleW(hFile, newDevicePath, nSize * sizeof(wchar_t))) |
| 111 | + if(!PathFromFileHandleW(hFile, newDevicePath, nSizeInChars)) |
113 | 112 | return false;
|
114 |
| - if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szPath, (int)wcslen(newDevicePath) + 1, NULL, NULL)) |
| 113 | + if(!WideCharToMultiByte(CP_ACP, NULL, newDevicePath, -1, szPath, int(wcslen(newDevicePath)) + 1, NULL, NULL)) |
115 | 114 | return false;
|
116 | 115 | return true;
|
117 | 116 | }
|
0 commit comments