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 E_INVALIDARG;
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 E_INVALIDARG;
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 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", &pszMore[1]);
105 return S_OK;
106 }
107 }
108 else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash))
109 {
110 if ((pszPathLength + pszMoreLength) < cchPath)
111 {
112 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszMore);
113 return S_OK;
114 }
115 }
116 else if (!pathBackslash && !moreBackslash)
117 {
118 if ((pszPathLength + pszMoreLength + 1) < cchPath)
119 {
120 sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s%s",
121 CUR_PATH_SEPARATOR_STR, pszMore);
122 return S_OK;
123 }
124 }
125
126 return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE);
127}
128
129#endif
130
131/*
132#undef DEFINE_UNICODE
133#undef CUR_PATH_SEPARATOR_CHR
134#undef CUR_PATH_SEPARATOR_STR
135#undef PATH_CCH_APPEND
136*/