forked from https-github-com-Surachai-kent/runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath.cpp
More file actions
323 lines (282 loc) · 8.11 KB
/
path.cpp
File metadata and controls
323 lines (282 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//
//
// ===========================================================================
// File: path.cpp
//
// Path APIs ported from shlwapi (especially for Fusion)
// ===========================================================================
#include "common.h"
#include "strsafe.h"
#define CH_SLASH W('/')
#define CH_WHACK W('\\')
//
// Inline function to check for a double-backslash at the
// beginning of a string
//
static __inline BOOL DBL_BSLASH(LPCWSTR psz)
{
return (psz[0] == W('\\') && psz[1] == W('\\'));
}
//
// Inline function to check for a path separator character.
//
static __inline BOOL IsPathSeparator(WCHAR ch)
{
return (ch == CH_SLASH || ch == CH_WHACK);
}
__inline BOOL ChrCmpW_inline(WCHAR w1, WCHAR wMatch)
{
return(!(w1 == wMatch));
}
STDAPI_(LPWSTR) StrRChrW(LPCWSTR lpStart, LPCWSTR lpEnd, WCHAR wMatch)
{
LPCWSTR lpFound = NULL;
RIPMSG(lpStart && IS_VALID_STRING_PTRW(lpStart, -1), "StrRChrW: caller passed bad lpStart");
RIPMSG(!lpEnd || lpEnd <= lpStart + wcslen(lpStart), "StrRChrW: caller passed bad lpEnd");
// don't need to check for NULL lpStart
if (!lpEnd)
lpEnd = lpStart + wcslen(lpStart);
for ( ; lpStart < lpEnd; lpStart++)
{
if (!ChrCmpW_inline(*lpStart, wMatch))
lpFound = lpStart;
}
return ((LPWSTR)lpFound);
}
// check if a path is a root
//
// returns:
// TRUE
// "\" "X:\" "\\" "\\foo" "\\foo\bar"
//
// FALSE for others including "\\foo\bar\" (!)
//
STDAPI_(BOOL) PathIsRootW(LPCWSTR pPath)
{
RIPMSG(pPath && IS_VALID_STRING_PTR(pPath, -1), "PathIsRoot: caller passed bad pPath");
if (!pPath || !*pPath)
{
return FALSE;
}
if (!lstrcmpiW(pPath + 1, W(":\\")))
{
return TRUE; // "X:\" case
}
if (IsPathSeparator(*pPath) && (*(pPath + 1) == 0))
{
return TRUE; // "/" or "\" case
}
if (DBL_BSLASH(pPath)) // smells like UNC name
{
LPCWSTR p;
int cBackslashes = 0;
for (p = pPath + 2; *p; p++)
{
if (*p == W('\\'))
{
//
// return FALSE for "\\server\share\dir"
// so just check if there is more than one slash
//
// "\\server\" without a share name causes
// problems for WNet APIs. we should return
// FALSE for this as well
//
if ((++cBackslashes > 1) || !*(p+1))
return FALSE;
}
}
// end of string with only 1 more backslash
// must be a bare UNC, which looks like a root dir
return TRUE;
}
return FALSE;
}
//
// Return a pointer to the end of the next path component in the string.
// ie return a pointer to the next backslash or terminating NULL.
//
LPCWSTR GetPCEnd(LPCWSTR lpszStart)
{
LPCWSTR lpszEnd;
LPCWSTR lpszSlash;
lpszEnd = StrChr(lpszStart, CH_WHACK);
lpszSlash = StrChr(lpszStart, CH_SLASH);
if ((lpszSlash && lpszSlash < lpszEnd) ||
!lpszEnd)
{
lpszEnd = lpszSlash;
}
if (!lpszEnd)
{
lpszEnd = lpszStart + wcslen(lpszStart);
}
return lpszEnd;
}
//
// Given a pointer to the end of a path component, return a pointer to
// its beginning.
// ie return a pointer to the previous backslash (or start of the string).
//
LPCWSTR PCStart(LPCWSTR lpszStart, LPCWSTR lpszEnd)
{
LPCWSTR lpszBegin = StrRChrW(lpszStart, lpszEnd, CH_WHACK);
LPCWSTR lpszSlash = StrRChrW(lpszStart, lpszEnd, CH_SLASH);
if (lpszSlash > lpszBegin)
{
lpszBegin = lpszSlash;
}
if (!lpszBegin)
{
lpszBegin = lpszStart;
}
return lpszBegin;
}
//
// Fix up a few special cases so that things roughly make sense.
//
void NearRootFixups(LPWSTR lpszPath, BOOL fUNC)
{
// Check for empty path.
if (lpszPath[0] == W('\0'))
{
// Fix up.
#ifndef TARGET_UNIX
lpszPath[0] = CH_WHACK;
#else
lpszPath[0] = CH_SLASH;
#endif
lpszPath[1] = W('\0');
}
// Check for missing slash.
if (lpszPath[1] == W(':') && lpszPath[2] == W('\0'))
{
// Fix up.
lpszPath[2] = W('\\');
lpszPath[3] = W('\0');
}
// Check for UNC root.
if (fUNC && lpszPath[0] == W('\\') && lpszPath[1] == W('\0'))
{
// Fix up.
//lpszPath[0] = W('\\'); // already checked in if guard
lpszPath[1] = W('\\');
lpszPath[2] = W('\0');
}
}
/*----------------------------------------------------------
Purpose: Canonicalize a path.
Returns:
Cond: --
*/
STDAPI_(BOOL) PathCanonicalizeW(LPWSTR lpszDst, LPCWSTR lpszSrc)
{
LPCWSTR lpchSrc;
LPCWSTR lpchPCEnd; // Pointer to end of path component.
LPWSTR lpchDst;
BOOL fUNC;
int cchPC;
RIPMSG(lpszDst && IS_VALID_WRITE_BUFFER(lpszDst, WCHAR, MAX_PATH), "PathCanonicalize: caller passed bad lpszDst");
RIPMSG(lpszSrc && IS_VALID_STRING_PTR(lpszSrc, -1), "PathCanonicalize: caller passed bad lpszSrc");
RIPMSG(lpszDst != lpszSrc, "PathCanonicalize: caller passed the same buffer for lpszDst and lpszSrc");
if (!lpszDst || !lpszSrc)
{
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
*lpszDst = W('\0');
fUNC = PathIsUNCW(lpszSrc); // Check for UNCness.
// Init.
lpchSrc = lpszSrc;
lpchDst = lpszDst;
while (*lpchSrc)
{
lpchPCEnd = GetPCEnd(lpchSrc);
cchPC = (int) (lpchPCEnd - lpchSrc)+1;
if (cchPC == 1 && IsPathSeparator(*lpchSrc)) // Check for slashes.
{
// Just copy them.
#ifndef TARGET_UNIX
*lpchDst = CH_WHACK;
#else
*lpchDst = CH_SLASH;
#endif
lpchDst++;
lpchSrc++;
}
else if (cchPC == 2 && *lpchSrc == W('.')) // Check for dots.
{
// Skip it...
// Are we at the end?
if (*(lpchSrc+1) == W('\0'))
{
lpchSrc++;
// remove the last slash we copied (if we've copied one), but don't make a mal-formed root
if ((lpchDst > lpszDst) && !PathIsRootW(lpszDst))
lpchDst--;
}
else
{
lpchSrc += 2;
}
}
else if (cchPC == 3 && *lpchSrc == W('.') && *(lpchSrc + 1) == W('.')) // Check for dot dot.
{
// make sure we aren't already at the root
if (!PathIsRootW(lpszDst))
{
// Go up... Remove the previous path component.
lpchDst = (LPWSTR)PCStart(lpszDst, lpchDst - 1);
}
else
{
// When we can't back up, skip the trailing backslash
// so we don't copy one again. (C:\..\FOO would otherwise
// turn into C:\\FOO).
if (IsPathSeparator(*(lpchSrc + 2)))
{
lpchSrc++;
}
}
// skip ".."
lpchSrc += 2;
}
else // Everything else
{
// Just copy it.
int cchRemainingBuffer = MAX_PATH - (lpszDst - lpchDst);
StringCchCopyNW(lpchDst, cchRemainingBuffer, lpchSrc, cchPC);
lpchDst += cchPC - 1;
lpchSrc += cchPC - 1;
}
// Keep everything nice and tidy.
*lpchDst = W('\0');
}
// Check for weirdo root directory stuff.
NearRootFixups(lpszDst, fUNC);
return TRUE;
}
//---------------------------------------------------------------------------
// Returns TRUE if the given string is a UNC path.
//
// TRUE
// "\\foo\bar"
// "\\foo" <- careful
// "\\"
// FALSE
// "\foo"
// "foo"
// "c:\foo"
//
//
STDAPI_(BOOL) PathIsUNCW(LPCWSTR pszPath)
{
RIPMSG(pszPath && IS_VALID_STRING_PTR(pszPath, -1), "PathIsUNC: caller passed bad pszPath");
if (pszPath)
{
return DBL_BSLASH(pszPath);
}
return FALSE;
}