FreeRDP/winpr/libwinpr/path/include/PathCchAppend.c
Norbert Federa 178afd8dd5 winpr: fix PathCchAppend
Fix incorrect usage of S_FALSE which was used to indicate errors
although it is a HRESULT success code.
Make this function behave like the Windows 8 implementation and
the according MSDN specification.

- return E_INVALIDARG instead of S_FALSE if pszPath is NULL
- return E_INVALIDARG instead of S_FALSE if pszMore is NULL
- return E_INVALIDARG if cchPath is zero
- return E_INVALIDARG if cchPath is greater than PATHCCH_MAX_CCH
- return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE) if the combined
  path size exceeds cchPath (this is the same HRESULT which the Windows
  implementation returns in this case and which is referred to as
  PATHCCH_E_FILENAME_TOO_LONG on msdn)

Also extended/fixed the TestPathCchAppend ctest
2015-06-03 16:06:02 +02:00

124 lines
2.8 KiB
C

/*
#define DEFINE_UNICODE FALSE
#define _PATH_SEPARATOR_CHR '\\'
#define _PATH_SEPARATOR_STR "\\"
#define PATH_CCH_APPEND PathCchAppendA
*/
#if DEFINE_UNICODE
HRESULT PATH_CCH_APPEND(PWSTR pszPath, size_t cchPath, PCWSTR pszMore)
{
#ifdef _WIN32
BOOL pathBackslash;
BOOL moreBackslash;
size_t pszMoreLength;
size_t pszPathLength;
if (!pszPath)
return E_INVALIDARG;
if (!pszMore)
return E_INVALIDARG;
if (cchPath == 0 || cchPath > PATHCCH_MAX_CCH)
return E_INVALIDARG;
pszMoreLength = lstrlenW(pszMore);
pszPathLength = lstrlenW(pszPath);
pathBackslash = (pszPath[pszPathLength - 1] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
moreBackslash = (pszMore[0] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
if (pathBackslash && moreBackslash)
{
if ((pszPathLength + pszMoreLength - 1) < cchPath)
{
swprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, L"%s", &pszMore[1]);
return S_OK;
}
}
else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
{
if ((pszPathLength + pszMoreLength) < cchPath)
{
swprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, L"%s", pszMore);
return S_OK;
}
}
else if (!pathBackslash && !moreBackslash)
{
if ((pszPathLength + pszMoreLength + 1) < cchPath)
{
swprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, _PATH_SEPARATOR_STR L"%s", pszMore);
return S_OK;
}
}
#endif
return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
}
#else
HRESULT PATH_CCH_APPEND(PSTR pszPath, size_t cchPath, PCSTR pszMore)
{
BOOL pathBackslash;
BOOL moreBackslash;
size_t pszMoreLength;
size_t pszPathLength;
if (!pszPath)
return E_INVALIDARG;
if (!pszMore)
return E_INVALIDARG;
if (cchPath == 0 || cchPath > PATHCCH_MAX_CCH)
return E_INVALIDARG;
pszMoreLength = lstrlenA(pszMore);
pszPathLength = lstrlenA(pszPath);
pathBackslash = (pszPath[pszPathLength - 1] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
moreBackslash = (pszMore[0] == _PATH_SEPARATOR_CHR) ? TRUE : FALSE;
if (pathBackslash && moreBackslash)
{
if ((pszPathLength + pszMoreLength - 1) < cchPath)
{
sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", &pszMore[1]);
return S_OK;
}
}
else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
{
if ((pszPathLength + pszMoreLength) < cchPath)
{
sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszMore);
return S_OK;
}
}
else if (!pathBackslash && !moreBackslash)
{
if ((pszPathLength + pszMoreLength + 1) < cchPath)
{
sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, _PATH_SEPARATOR_STR "%s", pszMore);
return S_OK;
}
}
return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
}
#endif
/*
#undef DEFINE_UNICODE
#undef _PATH_SEPARATOR_CHR
#undef _PATH_SEPARATOR_STR
#undef PATH_CCH_APPEND
*/