Signed-off-by: Zhiyi Zhang zzhang@codeweavers.com --- .../api-ms-win-core-path-l1-1-0.spec | 2 +- dlls/kernelbase/kernelbase.spec | 2 +- dlls/kernelbase/path.c | 39 ++++++ dlls/kernelbase/tests/path.c | 128 ++++++++++++++++++ include/pathcch.h | 1 + 5 files changed, 170 insertions(+), 2 deletions(-)
diff --git a/dlls/api-ms-win-core-path-l1-1-0/api-ms-win-core-path-l1-1-0.spec b/dlls/api-ms-win-core-path-l1-1-0/api-ms-win-core-path-l1-1-0.spec index e07fe02a51..a09889f415 100644 --- a/dlls/api-ms-win-core-path-l1-1-0/api-ms-win-core-path-l1-1-0.spec +++ b/dlls/api-ms-win-core-path-l1-1-0/api-ms-win-core-path-l1-1-0.spec @@ -18,5 +18,5 @@ @ stdcall PathCchRenameExtension(wstr long wstr) kernelbase.PathCchRenameExtension @ stdcall PathCchSkipRoot(wstr ptr) kernelbase.PathCchSkipRoot @ stdcall PathCchStripPrefix(wstr long) kernelbase.PathCchStripPrefix -@ stub PathCchStripToRoot +@ stdcall PathCchStripToRoot(wstr long) kernelbase.PathCchStripToRoot @ stdcall PathIsUNCEx(wstr ptr) kernelbase.PathIsUNCEx diff --git a/dlls/kernelbase/kernelbase.spec b/dlls/kernelbase/kernelbase.spec index eae67be147..aefc63e54c 100644 --- a/dlls/kernelbase/kernelbase.spec +++ b/dlls/kernelbase/kernelbase.spec @@ -1047,7 +1047,7 @@ @ stdcall PathCchRenameExtension(wstr long wstr) @ stdcall PathCchSkipRoot(wstr ptr) @ stdcall PathCchStripPrefix(wstr long) -# @ stub PathCchStripToRoot +@ stdcall PathCchStripToRoot(wstr long) @ stdcall PathCombineA(ptr str str) shlwapi.PathCombineA @ stdcall PathCombineW(ptr wstr wstr) shlwapi.PathCombineW @ stdcall PathCommonPrefixA(str str ptr) shlwapi.PathCommonPrefixA diff --git a/dlls/kernelbase/path.c b/dlls/kernelbase/path.c index 168edd0610..c59fba8d3e 100644 --- a/dlls/kernelbase/path.c +++ b/dlls/kernelbase/path.c @@ -330,6 +330,45 @@ HRESULT WINAPI PathCchStripPrefix(WCHAR *path, SIZE_T size) return S_FALSE; }
+HRESULT WINAPI PathCchStripToRoot(WCHAR *path, SIZE_T size) +{ + const WCHAR *root_end; + WCHAR *segment_end; + BOOL is_unc; + + TRACE("%s %lu\n", wine_dbgstr_w(path), size); + + if (!path || !*path || !size || size > PATHCCH_MAX_CCH) return E_INVALIDARG; + + /* \\?\UNC\* and \\* have to have at least two extra segments to be striped, + * e.g. \\?\UNC\a\b\c -> \\?\UNC\a\b + * \\a\b\c -> \\a\b */ + if ((is_unc = is_prefixed_unc(path)) || (path[0] == '\' && path[1] == '\' && path[2] != '?')) + { + root_end = is_unc ? path + 8 : path + 3; + if (!get_next_segment(root_end, &root_end)) return S_FALSE; + if (!get_next_segment(root_end, &root_end)) return S_FALSE; + + if (root_end - path >= size) return E_INVALIDARG; + + segment_end = path + (root_end - path) - 1; + *segment_end = 0; + return S_OK; + } + else if (PathCchSkipRoot(path, &root_end) == S_OK) + { + if (root_end - path >= size) return E_INVALIDARG; + + segment_end = path + (root_end - path); + if (!*segment_end) return S_FALSE; + + *segment_end = 0; + return S_OK; + } + else + return E_INVALIDARG; +} + BOOL WINAPI PathIsUNCEx(const WCHAR *path, const WCHAR **server) { const WCHAR *result = NULL; diff --git a/dlls/kernelbase/tests/path.c b/dlls/kernelbase/tests/path.c index f54e77be46..5b47b84831 100644 --- a/dlls/kernelbase/tests/path.c +++ b/dlls/kernelbase/tests/path.c @@ -39,6 +39,7 @@ HRESULT (WINAPI *pPathCchRemoveExtension)(WCHAR *path, SIZE_T size); HRESULT (WINAPI *pPathCchRenameExtension)(WCHAR *path, SIZE_T size, const WCHAR *extension); HRESULT (WINAPI *pPathCchSkipRoot)(const WCHAR *path, const WCHAR **root_end); HRESULT (WINAPI *pPathCchStripPrefix)(WCHAR *path, SIZE_T size); +HRESULT (WINAPI *pPathCchStripToRoot)(WCHAR *path, SIZE_T size); BOOL (WINAPI *pPathIsUNCEx)(const WCHAR *path, const WCHAR **server);
static const struct @@ -835,6 +836,131 @@ static void test_PathCchStripPrefix(void) } }
+struct striptoroot_test +{ + const CHAR *path; + const CHAR *root; + HRESULT hr; + SIZE_T size; +}; + +static const struct striptoroot_test striptoroot_tests[] = +{ + /* Invalid */ + {"", "", E_INVALIDARG}, + {"C", NULL, E_INVALIDARG}, + {"\\?\UNC", NULL, E_INVALIDARG}, + + /* Size */ + {"C:\", NULL, E_INVALIDARG, PATHCCH_MAX_CCH + 1}, + {"C:\", "C:\", S_FALSE, PATHCCH_MAX_CCH}, + /* Size < original path length + 1, read beyond size */ + {"C:\a", "C:\", S_OK, ARRAY_SIZE("C:\a") - 1}, + /* Size < stripped path length + 1 */ + {"C:\a", "C:\", E_INVALIDARG, ARRAY_SIZE("C:\") - 1}, + {"\\a\b\c", NULL, E_INVALIDARG, ARRAY_SIZE("\\a\b") - 1}, + + /* X: */ + {"C:", "C:", S_FALSE}, + {"C:a", "C:", S_OK}, + {"C:a\b", "C:", S_OK}, + {"C:a\b\c", "C:", S_OK}, + + /* X:\ */ + {"C:\", "C:\", S_FALSE}, + {"C:\a", "C:\", S_OK}, + {"C:\a\b", "C:\", S_OK}, + {"C:\a\b\c", "C:\", S_OK}, + + /* \ */ + {"\", "\", S_FALSE}, + {"\a", "\", S_OK}, + {"\a\b", "\", S_OK}, + {"\a\b\c", "\", S_OK}, + + /* \ */ + {"\\", "\\", S_FALSE}, + {"\\a", "\\a", S_FALSE}, + {"\\a\b", "\\a\b", S_FALSE}, + {"\\a\b\c", "\\a\b", S_OK}, + + /* UNC */ + {"\\?\UNC\", "\\?\UNC\", S_FALSE}, + {"\\?\UNC\a", "\\?\UNC\a", S_FALSE}, + {"\\?\UNC\a\b", "\\?\UNC\a\b", S_FALSE}, + {"\\?\UNC\a\b\", "\\?\UNC\a\b", S_OK}, + {"\\?\UNC\a\b\c", "\\?\UNC\a\b", S_OK}, + + /* Prefixed X: */ + {"\\?\C:", "\\?\C:", S_FALSE}, + {"\\?\C:a", "\\?\C:", S_OK}, + {"\\?\C:a\b", "\\?\C:", S_OK}, + {"\\?\C:a\b\c", "\\?\C:", S_OK}, + + /* Prefixed X:\ */ + {"\\?\C:\", "\\?\C:\", S_FALSE}, + {"\\?\C:\a", "\\?\C:\", S_OK}, + {"\\?\C:\a\b", "\\?\C:\", S_OK}, + {"\\?\C:\a\b\c", "\\?\C:\", S_OK}, + + /* UNC Volume */ + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}", S_FALSE}, + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}a", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}", S_OK}, + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}a\b", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}", S_OK}, + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}a\b\c", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}", S_OK}, + + /* UNC Volume with backslash */ + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\", S_FALSE}, + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\a", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\", S_OK}, + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\a\b", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\", S_OK}, + {"\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\a\b\c", + "\\?\Volume{e51a1864-6f2d-4019-b73d-f4e60e600c26}\", S_OK}, +}; + +static void test_PathCchStripToRoot(void) +{ + WCHAR pathW[PATHCCH_MAX_CCH]; + CHAR rootA[PATHCCH_MAX_CCH]; + SIZE_T size; + HRESULT hr; + INT i; + + if (!pPathCchStripToRoot) + { + win_skip("PathCchStripToRoot() is not available.\n"); + return; + } + + /* Null arguments */ + hr = pPathCchStripToRoot(NULL, ARRAY_SIZE(pathW)); + ok(hr == E_INVALIDARG, "Expect result %#x, got %#x\n", E_INVALIDARG, hr); + + MultiByteToWideChar(CP_ACP, 0, "C:\a", -1, pathW, ARRAY_SIZE(pathW)); + hr = pPathCchStripToRoot(pathW, 0); + ok(hr == E_INVALIDARG, "Expect result %#x, got %#x\n", E_INVALIDARG, hr); + + for (i = 0; i < ARRAY_SIZE(striptoroot_tests); i++) + { + const struct striptoroot_test *t = striptoroot_tests + i; + MultiByteToWideChar(CP_ACP, 0, t->path, -1, pathW, ARRAY_SIZE(pathW)); + size = t->size ? t->size : ARRAY_SIZE(pathW); + hr = pPathCchStripToRoot(pathW, size); + ok(hr == t->hr, "path %s expect result %#x, got %#x\n", t->path, t->hr, hr); + if (SUCCEEDED(hr)) + { + WideCharToMultiByte(CP_ACP, 0, pathW, -1, rootA, ARRAY_SIZE(rootA), NULL, NULL); + ok(!lstrcmpA(rootA, t->root), "path %s expect stripped path %s, got %s\n", t->path, t->root, rootA); + } + } +} + struct isuncex_test { const CHAR *path; @@ -917,6 +1043,7 @@ START_TEST(path) pPathCchRenameExtension = (void *)GetProcAddress(hmod, "PathCchRenameExtension"); pPathCchSkipRoot = (void *)GetProcAddress(hmod, "PathCchSkipRoot"); pPathCchStripPrefix = (void *)GetProcAddress(hmod, "PathCchStripPrefix"); + pPathCchStripToRoot = (void *)GetProcAddress(hmod, "PathCchStripToRoot"); pPathIsUNCEx = (void *)GetProcAddress(hmod, "PathIsUNCEx");
test_PathCchCombineEx(); @@ -928,5 +1055,6 @@ START_TEST(path) test_PathCchRenameExtension(); test_PathCchSkipRoot(); test_PathCchStripPrefix(); + test_PathCchStripToRoot(); test_PathIsUNCEx(); } diff --git a/include/pathcch.h b/include/pathcch.h index 42bae4edd0..56920b3444 100644 --- a/include/pathcch.h +++ b/include/pathcch.h @@ -34,4 +34,5 @@ HRESULT WINAPI PathCchRemoveExtension(WCHAR *path, SIZE_T size); HRESULT WINAPI PathCchRenameExtension(WCHAR *path, SIZE_T size, const WCHAR *extension); HRESULT WINAPI PathCchSkipRoot(const WCHAR *path, const WCHAR **root_end); HRESULT WINAPI PathCchStripPrefix(WCHAR *path, SIZE_T size); +HRESULT WINAPI PathCchStripToRoot(WCHAR *path, SIZE_T size); BOOL WINAPI PathIsUNCEx(const WCHAR *path, const WCHAR **server);