Sweden-Number/dlls/kernelbase/path.c

422 lines
11 KiB
C
Raw Normal View History

/*
* Copyright 2018 Nikolay Sivov
* Copyright 2018 Zhiyi Zhang
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#include "windef.h"
#include "winbase.h"
#include "pathcch.h"
#include "strsafe.h"
#include "wine/debug.h"
#include "wine/unicode.h"
WINE_DEFAULT_DEBUG_CHANNEL(path);
static SIZE_T strnlenW(const WCHAR *string, SIZE_T maxlen)
{
SIZE_T i;
for (i = 0; i < maxlen; i++)
if (!string[i]) break;
return i;
}
static BOOL is_prefixed_unc(const WCHAR *string)
{
static const WCHAR prefixed_unc[] = {'\\', '\\', '?', '\\', 'U', 'N', 'C', '\\'};
return !strncmpiW(string, prefixed_unc, ARRAY_SIZE(prefixed_unc));
}
static BOOL is_prefixed_disk(const WCHAR *string)
{
static const WCHAR prefix[] = {'\\', '\\', '?', '\\'};
return !strncmpW(string, prefix, ARRAY_SIZE(prefix)) && isalphaW(string[4]) && string[5] == ':';
}
static BOOL is_prefixed_volume(const WCHAR *string)
{
static const WCHAR prefixed_volume[] = {'\\', '\\', '?', '\\', 'V', 'o', 'l', 'u', 'm', 'e'};
const WCHAR *guid;
INT i = 0;
if (strncmpiW(string, prefixed_volume, ARRAY_SIZE(prefixed_volume))) return FALSE;
guid = string + ARRAY_SIZE(prefixed_volume);
while (i <= 37)
{
switch (i)
{
case 0:
if (guid[i] != '{') return FALSE;
break;
case 9:
case 14:
case 19:
case 24:
if (guid[i] != '-') return FALSE;
break;
case 37:
if (guid[i] != '}') return FALSE;
break;
default:
if (!isalnumW(guid[i])) return FALSE;
break;
}
i++;
}
return TRUE;
}
/* Get the next character beyond end of the segment.
Return TRUE if the last segment ends with a backslash */
static BOOL get_next_segment(const WCHAR *next, const WCHAR **next_segment)
{
while (*next && *next != '\\') next++;
if (*next == '\\')
{
*next_segment = next + 1;
return TRUE;
}
else
{
*next_segment = next;
return FALSE;
}
}
/* Find the last character of the root in a path, if there is one, without any segments */
static const WCHAR *get_root_end(const WCHAR *path)
{
/* Find path root */
if (is_prefixed_volume(path))
return path[48] == '\\' ? path + 48 : path + 47;
else if (is_prefixed_unc(path))
return path + 7;
else if (is_prefixed_disk(path))
return path[6] == '\\' ? path + 6 : path + 5;
/* \\ */
else if (path[0] == '\\' && path[1] == '\\')
return path + 1;
/* \ */
else if (path[0] == '\\')
return path;
/* X:\ */
else if (isalphaW(path[0]) && path[1] == ':')
return path[2] == '\\' ? path + 2 : path + 1;
else
return NULL;
}
HRESULT WINAPI PathCchAddBackslash(WCHAR *path, SIZE_T size)
{
return PathCchAddBackslashEx(path, size, NULL, NULL);
}
HRESULT WINAPI PathCchAddBackslashEx(WCHAR *path, SIZE_T size, WCHAR **endptr, SIZE_T *remaining)
{
BOOL needs_termination;
SIZE_T length;
TRACE("%s, %lu, %p, %p\n", debugstr_w(path), size, endptr, remaining);
length = strlenW(path);
needs_termination = size && length && path[length - 1] != '\\';
if (length >= (needs_termination ? size - 1 : size))
{
if (endptr) *endptr = NULL;
if (remaining) *remaining = 0;
return STRSAFE_E_INSUFFICIENT_BUFFER;
}
if (!needs_termination)
{
if (endptr) *endptr = path + length;
if (remaining) *remaining = size - length;
return S_FALSE;
}
path[length++] = '\\';
path[length] = 0;
if (endptr) *endptr = path + length;
if (remaining) *remaining = size - length;
return S_OK;
}
HRESULT WINAPI PathCchAddExtension(WCHAR *path, SIZE_T size, const WCHAR *extension)
{
const WCHAR *existing_extension, *next;
SIZE_T path_length, extension_length, dot_length;
BOOL has_dot;
HRESULT hr;
TRACE("%s %lu %s\n", wine_dbgstr_w(path), size, wine_dbgstr_w(extension));
if (!path || !size || size > PATHCCH_MAX_CCH || !extension) return E_INVALIDARG;
next = extension;
while (*next)
{
if ((*next == '.' && next > extension) || *next == ' ' || *next == '\\') return E_INVALIDARG;
next++;
}
has_dot = extension[0] == '.' ? TRUE : FALSE;
hr = PathCchFindExtension(path, size, &existing_extension);
if (FAILED(hr)) return hr;
if (*existing_extension) return S_FALSE;
path_length = strnlenW(path, size);
dot_length = has_dot ? 0 : 1;
extension_length = strlenW(extension);
if (path_length + dot_length + extension_length + 1 > size) return STRSAFE_E_INSUFFICIENT_BUFFER;
/* If extension is empty or only dot, return S_OK with path unchanged */
if (!extension[0] || (extension[0] == '.' && !extension[1])) return S_OK;
if (!has_dot)
{
path[path_length] = '.';
path_length++;
}
strcpyW(path + path_length, extension);
return S_OK;
}
HRESULT WINAPI PathCchFindExtension(const WCHAR *path, SIZE_T size, const WCHAR **extension)
{
const WCHAR *lastpoint = NULL;
SIZE_T counter = 0;
TRACE("%s %lu %p\n", wine_dbgstr_w(path), size, extension);
if (!path || !size || size > PATHCCH_MAX_CCH)
{
*extension = NULL;
return E_INVALIDARG;
}
while (*path)
{
if (*path == '\\' || *path == ' ')
lastpoint = NULL;
else if (*path == '.')
lastpoint = path;
path++;
counter++;
if (counter == size || counter == PATHCCH_MAX_CCH)
{
*extension = NULL;
return E_INVALIDARG;
}
}
*extension = lastpoint ? lastpoint : path;
return S_OK;
}
BOOL WINAPI PathCchIsRoot(const WCHAR *path)
{
const WCHAR *root_end;
const WCHAR *next;
BOOL is_unc;
TRACE("%s\n", wine_dbgstr_w(path));
if (!path || !*path) return FALSE;
root_end = get_root_end(path);
if (!root_end) return FALSE;
if ((is_unc = is_prefixed_unc(path)) || (path[0] == '\\' && path[1] == '\\' && path[2] != '?'))
{
next = root_end + 1;
/* No extra segments */
if ((is_unc && !*next) || (!is_unc && !*next)) return TRUE;
/* Has first segment with an ending backslash but no remaining characters */
if (get_next_segment(next, &next) && !*next) return FALSE;
/* Has first segment with no ending backslash */
else if (!*next)
return TRUE;
/* Has first segment with an ending backslash and has remaining characters*/
else
{
next++;
/* Second segment must have no backslash and no remaining characters */
return !get_next_segment(next, &next) && !*next;
}
}
else if (*root_end == '\\' && !root_end[1])
return TRUE;
else
return FALSE;
}
HRESULT WINAPI PathCchRemoveExtension(WCHAR *path, SIZE_T size)
{
const WCHAR *extension;
WCHAR *next;
HRESULT hr;
TRACE("%s %lu\n", wine_dbgstr_w(path), size);
if (!path || !size || size > PATHCCH_MAX_CCH) return E_INVALIDARG;
hr = PathCchFindExtension(path, size, &extension);
if (FAILED(hr)) return hr;
next = path + (extension - path);
while (next - path < size && *next) *next++ = 0;
return next == extension ? S_FALSE : S_OK;
}
HRESULT WINAPI PathCchRenameExtension(WCHAR *path, SIZE_T size, const WCHAR *extension)
{
HRESULT hr;
TRACE("%s %lu %s\n", wine_dbgstr_w(path), size, wine_dbgstr_w(extension));
hr = PathCchRemoveExtension(path, size);
if (FAILED(hr)) return hr;
hr = PathCchAddExtension(path, size, extension);
return FAILED(hr) ? hr : S_OK;
}
HRESULT WINAPI PathCchSkipRoot(const WCHAR *path, const WCHAR **root_end)
{
static const WCHAR unc_prefix[] = {'\\', '\\', '?'};
TRACE("%s %p\n", debugstr_w(path), root_end);
if (!path || !path[0] || !root_end
|| (!memicmpW(unc_prefix, path, ARRAY_SIZE(unc_prefix)) && !is_prefixed_volume(path) && !is_prefixed_unc(path)
&& !is_prefixed_disk(path)))
return E_INVALIDARG;
*root_end = get_root_end(path);
if (*root_end)
{
(*root_end)++;
if (is_prefixed_unc(path))
{
get_next_segment(*root_end, root_end);
get_next_segment(*root_end, root_end);
}
else if (path[0] == '\\' && path[1] == '\\' && path[2] != '?')
{
/* Skip share server */
get_next_segment(*root_end, root_end);
/* If mount point is empty, don't skip over mount point */
if (**root_end != '\\') get_next_segment(*root_end, root_end);
}
}
return *root_end ? S_OK : E_INVALIDARG;
}
HRESULT WINAPI PathCchStripPrefix(WCHAR *path, SIZE_T size)
{
TRACE("%s %lu\n", wine_dbgstr_w(path), size);
if (!path || !size || size > PATHCCH_MAX_CCH) return E_INVALIDARG;
if (is_prefixed_unc(path))
{
/* \\?\UNC\a -> \\a */
if (size < strlenW(path + 8) + 3) return E_INVALIDARG;
strcpyW(path + 2, path + 8);
return S_OK;
}
else if (is_prefixed_disk(path))
{
/* \\?\C:\ -> C:\ */
if (size < strlenW(path + 4) + 1) return E_INVALIDARG;
strcpyW(path, path + 4);
return S_OK;
}
else
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;
TRACE("%s %p\n", wine_dbgstr_w(path), server);
if (is_prefixed_unc(path))
result = path + 8;
else if (path[0] == '\\' && path[1] == '\\' && path[2] != '?')
result = path + 2;
if (server) *server = result;
return result ? TRUE : FALSE;
}