2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
#define DEFINE_UNICODE FALSE
|
|
|
|
#define _PATH_SEPARATOR_CHR '\\'
|
|
|
|
#define _PATH_SEPARATOR_STR "\\"
|
|
|
|
#define PATH_ALLOC_COMBINE PathAllocCombineA
|
|
|
|
*/
|
|
|
|
|
2015-06-08 15:32:19 +03:00
|
|
|
/**
|
|
|
|
* FIXME: These implementations of the PathAllocCombine functions have
|
|
|
|
* several issues:
|
|
|
|
* - pszPathIn or pszMore may be NULL (but not both)
|
|
|
|
* - no check if pszMore is fully qualified (if so, it must be directly
|
|
|
|
* copied to the output buffer without being combined with pszPathIn.
|
|
|
|
* - if pszMore begins with a _single_ backslash it must be combined with
|
|
|
|
* only the root of the path pointed to by pszPathIn and there's no code
|
|
|
|
* to extract the root of pszPathIn.
|
|
|
|
* - the function will crash with some short string lengths of the parameters
|
|
|
|
*/
|
|
|
|
|
2012-10-17 01:18:12 +04:00
|
|
|
#if DEFINE_UNICODE
|
|
|
|
|
|
|
|
HRESULT PATH_ALLOC_COMBINE(PCWSTR pszPathIn, PCWSTR pszMore, unsigned long dwFlags, PWSTR* ppszPathOut)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
PWSTR pszPathOut;
|
|
|
|
BOOL backslashIn;
|
|
|
|
BOOL backslashMore;
|
|
|
|
int pszMoreLength;
|
|
|
|
int pszPathInLength;
|
|
|
|
int pszPathOutLength;
|
|
|
|
|
2015-06-08 15:32:19 +03:00
|
|
|
WLog_WARN(TAG, "%s: has known bugs and needs fixing.", __FUNCTION__);
|
|
|
|
|
|
|
|
if (!ppszPathOut)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
if (!pszPathIn && !pszMore)
|
|
|
|
return E_INVALIDARG;
|
2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
if (!pszMore)
|
2015-06-08 15:32:19 +03:00
|
|
|
return E_FAIL; /* valid but not implemented, see top comment */
|
|
|
|
|
|
|
|
if (!pszPathIn)
|
|
|
|
return E_FAIL; /* valid but not implemented, see top comment */
|
|
|
|
|
|
|
|
pszPathInLength = lstrlenA(pszPathIn);
|
|
|
|
pszMoreLength = lstrlenA(pszMore);
|
2012-10-17 01:18:12 +04:00
|
|
|
|
2015-06-08 15:32:19 +03:00
|
|
|
/* prevent segfaults - the complete implementation below is buggy */
|
|
|
|
if (pszPathInLength < 3)
|
|
|
|
return E_FAIL;
|
2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
backslashIn = (pszPathIn[pszPathInLength - 1] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
|
|
|
|
backslashMore = (pszMore[0] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
if (backslashMore)
|
|
|
|
{
|
|
|
|
if ((pszPathIn[1] == ':') && (pszPathIn[2] == _PATH_SEPARATOR_CHR))
|
|
|
|
{
|
|
|
|
size_t sizeOfBuffer;
|
|
|
|
|
|
|
|
pszPathOutLength = 2 + pszMoreLength;
|
|
|
|
sizeOfBuffer = (pszPathOutLength + 1) * 2;
|
|
|
|
|
|
|
|
pszPathOut = (PWSTR) HeapAlloc(GetProcessHeap(), 0, sizeOfBuffer * 2);
|
2015-06-08 15:32:19 +03:00
|
|
|
if (!pszPathOut)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-10-17 01:18:12 +04:00
|
|
|
swprintf_s(pszPathOut, sizeOfBuffer, L"%c:%s", pszPathIn[0], pszMore);
|
|
|
|
|
|
|
|
*ppszPathOut = pszPathOut;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t sizeOfBuffer;
|
|
|
|
|
|
|
|
pszPathOutLength = pszPathInLength + pszMoreLength;
|
|
|
|
sizeOfBuffer = (pszPathOutLength + 1) * 2;
|
|
|
|
|
|
|
|
pszPathOut = (PWSTR) HeapAlloc(GetProcessHeap(), 0, sizeOfBuffer * 2);
|
2015-06-08 15:32:19 +03:00
|
|
|
if (!pszPathOut)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
if (backslashIn)
|
|
|
|
swprintf_s(pszPathOut, sizeOfBuffer, L"%s%s", pszPathIn, pszMore);
|
|
|
|
else
|
2012-10-17 15:48:24 +04:00
|
|
|
swprintf_s(pszPathOut, sizeOfBuffer, L"%s" _PATH_SEPARATOR_STR L"%s", pszPathIn, pszMore);
|
2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
*ppszPathOut = pszPathOut;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-06-08 15:32:19 +03:00
|
|
|
return E_FAIL;
|
2012-10-17 01:18:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
HRESULT PATH_ALLOC_COMBINE(PCSTR pszPathIn, PCSTR pszMore, unsigned long dwFlags, PSTR* ppszPathOut)
|
|
|
|
{
|
|
|
|
PSTR pszPathOut;
|
|
|
|
BOOL backslashIn;
|
|
|
|
BOOL backslashMore;
|
|
|
|
int pszMoreLength;
|
|
|
|
int pszPathInLength;
|
|
|
|
int pszPathOutLength;
|
|
|
|
|
2015-06-08 15:32:19 +03:00
|
|
|
WLog_WARN(TAG, "%s: has known bugs and needs fixing.", __FUNCTION__);
|
|
|
|
|
|
|
|
if (!ppszPathOut)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
if (!pszPathIn && !pszMore)
|
|
|
|
return E_INVALIDARG;
|
2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
if (!pszMore)
|
2015-06-08 15:32:19 +03:00
|
|
|
return E_FAIL; /* valid but not implemented, see top comment */
|
|
|
|
|
|
|
|
if (!pszPathIn)
|
|
|
|
return E_FAIL; /* valid but not implemented, see top comment */
|
2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
pszPathInLength = lstrlenA(pszPathIn);
|
|
|
|
pszMoreLength = lstrlenA(pszMore);
|
|
|
|
|
2015-06-08 15:32:19 +03:00
|
|
|
/* prevent segfaults - the complete implementation below is buggy */
|
|
|
|
if (pszPathInLength < 3)
|
|
|
|
return E_FAIL;
|
|
|
|
|
2012-10-17 01:18:12 +04:00
|
|
|
backslashIn = (pszPathIn[pszPathInLength - 1] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
|
|
|
|
backslashMore = (pszMore[0] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
|
|
|
|
|
|
|
|
if (backslashMore)
|
|
|
|
{
|
|
|
|
if ((pszPathIn[1] == ':') && (pszPathIn[2] == _PATH_SEPARATOR_CHR))
|
|
|
|
{
|
|
|
|
size_t sizeOfBuffer;
|
|
|
|
|
|
|
|
pszPathOutLength = 2 + pszMoreLength;
|
|
|
|
sizeOfBuffer = (pszPathOutLength + 1) * 2;
|
|
|
|
|
|
|
|
pszPathOut = (PSTR) HeapAlloc(GetProcessHeap(), 0, sizeOfBuffer * 2);
|
2015-06-08 15:32:19 +03:00
|
|
|
if (!pszPathOut)
|
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
|
2012-10-17 01:18:12 +04:00
|
|
|
sprintf_s(pszPathOut, sizeOfBuffer, "%c:%s", pszPathIn[0], pszMore);
|
|
|
|
|
|
|
|
*ppszPathOut = pszPathOut;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t sizeOfBuffer;
|
|
|
|
|
|
|
|
pszPathOutLength = pszPathInLength + pszMoreLength;
|
|
|
|
sizeOfBuffer = (pszPathOutLength + 1) * 2;
|
|
|
|
|
|
|
|
pszPathOut = (PSTR) HeapAlloc(GetProcessHeap(), 0, sizeOfBuffer * 2);
|
2015-06-08 15:32:19 +03:00
|
|
|
if (!pszPathOut)
|
|
|
|
return E_OUTOFMEMORY;
|
2012-10-17 01:18:12 +04:00
|
|
|
|
|
|
|
if (backslashIn)
|
|
|
|
sprintf_s(pszPathOut, sizeOfBuffer, "%s%s", pszPathIn, pszMore);
|
|
|
|
else
|
|
|
|
sprintf_s(pszPathOut, sizeOfBuffer, "%s" _PATH_SEPARATOR_STR "%s", pszPathIn, pszMore);
|
|
|
|
|
|
|
|
*ppszPathOut = pszPathOut;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-08 15:32:19 +03:00
|
|
|
return E_FAIL;
|
2012-10-17 01:18:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
#undef DEFINE_UNICODE
|
|
|
|
#undef _PATH_SEPARATOR_CHR
|
|
|
|
#undef _PATH_SEPARATOR_STR
|
|
|
|
#undef PATH_ALLOC_COMBINE
|
|
|
|
*/
|
|
|
|
|