/src/FreeRDP/winpr/libwinpr/path/include/PathCchAppend.h
Line | Count | Source |
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 | | |
17 | | HRESULT PATH_CCH_APPEND(PWSTR pszPath, size_t cchPath, PCWSTR pszMore) |
18 | 0 | { |
19 | 0 | if (!pszPath) |
20 | 0 | return E_INVALIDARG; |
21 | | |
22 | 0 | if (!pszMore) |
23 | 0 | return E_INVALIDARG; |
24 | | |
25 | 0 | if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH)) |
26 | 0 | return E_INVALIDARG; |
27 | | |
28 | 0 | const size_t pszMoreLength = _wcsnlen(pszMore, cchPath); |
29 | 0 | const size_t pszPathLength = _wcsnlen(pszPath, cchPath); |
30 | |
|
31 | 0 | BOOL pathBackslash = FALSE; |
32 | 0 | if (pszPathLength > 0) |
33 | 0 | pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE; |
34 | |
|
35 | 0 | const BOOL moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE; |
36 | |
|
37 | 0 | if (pathBackslash && moreBackslash) |
38 | 0 | { |
39 | 0 | if (pszMoreLength < 1) |
40 | 0 | return E_INVALIDARG; |
41 | | |
42 | 0 | if ((pszPathLength + pszMoreLength - 1) < cchPath) |
43 | 0 | { |
44 | 0 | WCHAR* ptr = &pszPath[pszPathLength]; |
45 | 0 | *ptr = '\0'; |
46 | 0 | _wcsncat(ptr, &pszMore[1], pszMoreLength - 1); |
47 | 0 | return S_OK; |
48 | 0 | } |
49 | 0 | } |
50 | 0 | else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash)) |
51 | 0 | { |
52 | 0 | if ((pszPathLength + pszMoreLength) < cchPath) |
53 | 0 | { |
54 | 0 | WCHAR* ptr = &pszPath[pszPathLength]; |
55 | 0 | *ptr = '\0'; |
56 | 0 | _wcsncat(ptr, pszMore, pszMoreLength); |
57 | 0 | return S_OK; |
58 | 0 | } |
59 | 0 | } |
60 | 0 | else if (!pathBackslash && !moreBackslash) |
61 | 0 | { |
62 | 0 | if ((pszPathLength + pszMoreLength + 1) < cchPath) |
63 | 0 | { |
64 | 0 | WCHAR* ptr = &pszPath[pszPathLength]; |
65 | 0 | *ptr = '\0'; |
66 | 0 | _wcsncat(ptr, CUR_PATH_SEPARATOR_STR, |
67 | 0 | _wcsnlen(CUR_PATH_SEPARATOR_STR, ARRAYSIZE(CUR_PATH_SEPARATOR_STR))); |
68 | 0 | _wcsncat(ptr, pszMore, pszMoreLength); |
69 | 0 | return S_OK; |
70 | 0 | } |
71 | 0 | } |
72 | | |
73 | 0 | return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE); |
74 | 0 | } Unexecuted instantiation: PathCchAppendW Unexecuted instantiation: UnixPathCchAppendW Unexecuted instantiation: NativePathCchAppendW |
75 | | |
76 | | #else |
77 | | |
78 | | HRESULT PATH_CCH_APPEND(PSTR pszPath, size_t cchPath, PCSTR pszMore) |
79 | 0 | { |
80 | 0 | BOOL pathBackslash = FALSE; |
81 | 0 | BOOL moreBackslash = FALSE; |
82 | |
|
83 | 0 | if (!pszPath) |
84 | 0 | return E_INVALIDARG; |
85 | | |
86 | 0 | if (!pszMore) |
87 | 0 | return E_INVALIDARG; |
88 | | |
89 | 0 | if ((cchPath == 0) || (cchPath > PATHCCH_MAX_CCH)) |
90 | 0 | return E_INVALIDARG; |
91 | | |
92 | 0 | const size_t pszPathLength = strnlen(pszPath, cchPath); |
93 | 0 | if (pszPathLength > 0) |
94 | 0 | pathBackslash = (pszPath[pszPathLength - 1] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE; |
95 | |
|
96 | 0 | const size_t pszMoreLength = strnlen(pszMore, cchPath); |
97 | 0 | if (pszMoreLength > 0) |
98 | 0 | moreBackslash = (pszMore[0] == CUR_PATH_SEPARATOR_CHR) ? TRUE : FALSE; |
99 | |
|
100 | 0 | if (pathBackslash && moreBackslash) |
101 | 0 | { |
102 | 0 | if ((pszPathLength + pszMoreLength - 1) < cchPath) |
103 | 0 | { |
104 | 0 | sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", &pszMore[1]); |
105 | 0 | return S_OK; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | else if ((pathBackslash && !moreBackslash) || (!pathBackslash && moreBackslash)) |
109 | 0 | { |
110 | 0 | if ((pszPathLength + pszMoreLength) < cchPath) |
111 | 0 | { |
112 | 0 | sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s", pszMore); |
113 | 0 | return S_OK; |
114 | 0 | } |
115 | 0 | } |
116 | 0 | else if (!pathBackslash && !moreBackslash) |
117 | 0 | { |
118 | 0 | if ((pszPathLength + pszMoreLength + 1) < cchPath) |
119 | 0 | { |
120 | 0 | sprintf_s(&pszPath[pszPathLength], cchPath - pszPathLength, "%s%s", |
121 | 0 | CUR_PATH_SEPARATOR_STR, pszMore); |
122 | 0 | return S_OK; |
123 | 0 | } |
124 | 0 | } |
125 | | |
126 | 0 | return HRESULT_FROM_WIN32(ERROR_FILENAME_EXCED_RANGE); |
127 | 0 | } Unexecuted instantiation: PathCchAppendA Unexecuted instantiation: UnixPathCchAppendA Unexecuted instantiation: NativePathCchAppendA |
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 | | */ |