FreeRDP
Loading...
Searching...
No Matches
PathCchAppend.h
1
2/*
3#define DEFINE_UNICODE FALSE
4#define CUR_PATH_SEPARATOR_CHR '\\'
5#define CUR_PATH_SEPARATOR_STR "\\"
6#define PATH_CCH_APPEND PathCchAppendA
7*/
8
9#include <string.h>
10
11#include <winpr/wtypes.h>
12#include <winpr/error.h>
13#include <winpr/path.h>
14
15#if defined(DEFINE_UNICODE) && (DEFINE_UNICODE != 0)
16
17HRESULT PATH_CCH_APPEND(PWSTR pszPath, size_t cchPath, PCWSTR pszMore)
18{
19 if (!pszPath)
20 return E_INVALIDARG;
21
22 if (!pszMore)
23 return S_OK;
24
25 if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH))
26 return E_INVALIDARG;
27
28 const size_t pszMoreLength = _wcsnlen(pszMore, cchPath);
29 const size_t pszPathLength = _wcsnlen(pszPath, cchPath);
30
31 BOOL pathBackslash = FALSE;
32 if (pszPathLength > 0)
33 pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
34
35 const BOOL moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
36
37 if (pathBackslash && moreBackslash)
38 {
39 if (pszMoreLength < 1)
40 return E_INVALIDARG;
41
42 if ((pszPathLength + pszMoreLength - 1) < cchPath)
43 {
44 WCHAR* ptr = &pszPath[pszPathLength];
45 *ptr = '\0';
46 _wcsncat(ptr, &pszMore[1], pszMoreLength - 1);
47 return S_OK;
48 }
49 }
50 else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
51 {
52 if ((pszPathLength + pszMoreLength) < cchPath)
53 {
54 WCHAR* ptr = &pszPath[pszPathLength];
55 *ptr = '\0';
56 _wcsncat(ptr, pszMore, pszMoreLength);
57 return S_OK;
58 }
59 }
60 else if (!pathBackslash && !moreBackslash)
61 {
62 if ((pszPathLength + pszMoreLength + 1) < cchPath)
63 {
64 WCHAR* ptr = &pszPath[pszPathLength];
65 *ptr = '\0';
66 _wcsncat(ptr, CUR_PATH_SEPARATOR_STR,
67 _wcsnlen(CUR_PATH_SEPARATOR_STR, ARRAYSIZE(CUR_PATH_SEPARATOR_STR)));
68 _wcsncat(ptr, pszMore, pszMoreLength);
69 return S_OK;
70 }
71 }
72
73 return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
74}
75
76#else
77
78HRESULT PATH_CCH_APPEND(PSTR pszPath, size_t cchPath, PCSTR pszMore)
79{
80 BOOL pathBackslash = FALSE;
81 BOOL moreBackslash = FALSE;
82
83 if (!pszPath)
84 return E_INVALIDARG;
85
86 if (!pszMore)
87 return S_OK;
88
89 if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH))
90 return E_INVALIDARG;
91
92 const size_t pszPathLength = strnlen(pszPath, cchPath);
93 if (pszPathLength > 0)
94 pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
95
96 const size_t pszMoreLength = strnlen(pszMore, cchPath);
97 if (pszMoreLength > 0)
98 moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE;
99
100 if (pathBackslash && moreBackslash)
101 {
102 if ((pszPathLength + pszMoreLength - 1) < cchPath)
103 {
104 if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", &pszMore[1]) < 0)
105 return E_FAIL;
106 return S_OK;
107 }
108 }
109 else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
110 {
111 if ((pszPathLength + pszMoreLength) < cchPath)
112 {
113 if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszMore) < 0)
114 return E_FAIL;
115 return S_OK;
116 }
117 }
118 else if (!pathBackslash && !moreBackslash)
119 {
120 if ((pszPathLength + pszMoreLength + 1) < cchPath)
121 {
122 if (sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s%s",
123 CUR_PATH_SEPARATOR_STR, pszMore) < 0)
124 return E_FAIL;
125 return S_OK;
126 }
127 }
128
129 return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
130}
131
132#endif
133
134/*
135#undef DEFINE_UNICODE
136#undef CUR_PATH_SEPARATOR_CHR
137#undef CUR_PATH_SEPARATOR_STR
138#undef PATH_CCH_APPEND
139*/