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
|
/* $Id: RTPathParse.cpp.h $ */
/** @file
* IPRT - RTPathParse - Code Template.
*
* This file included multiple times with different path style macros.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/**
* @copydoc RTPathParse
*/
static int RTPATH_STYLE_FN(rtPathParse)(const char *pszPath, PRTPATHPARSED pParsed, size_t cbParsed, uint32_t fFlags)
{
/*
* Parse the root specification if present and initialize the parser state
* (keep it on the stack for speed).
*/
uint32_t const cMaxComps = cbParsed < RT_UOFFSETOF(RTPATHPARSED, aComps[0xfff0])
? (uint32_t)((cbParsed - RT_UOFFSETOF(RTPATHPARSED, aComps)) / sizeof(pParsed->aComps[0]))
: 0xfff0;
uint32_t idxComp = 0;
uint32_t cchPath;
uint32_t offCur;
uint16_t fProps;
if (RTPATH_IS_SLASH(pszPath[0]))
{
if (fFlags & RTPATH_STR_F_NO_START)
{
offCur = 1;
while (RTPATH_IS_SLASH(pszPath[offCur]))
offCur++;
if (!pszPath[offCur])
return VERR_PATH_ZERO_LENGTH;
fProps = RTPATH_PROP_RELATIVE | RTPATH_PROP_EXTRA_SLASHES;
cchPath = 0;
}
#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
else if ( RTPATH_IS_SLASH(pszPath[1])
&& !RTPATH_IS_SLASH(pszPath[2])
&& pszPath[2])
{
/* UNC - skip to the end of the potential namespace or computer name. */
offCur = 2;
while (!RTPATH_IS_SLASH(pszPath[offCur]) && pszPath[offCur])
offCur++;
/* If there is another slash, we considered it a valid UNC path, if
not it's just a root path with an extra slash thrown in. */
if (RTPATH_IS_SLASH(pszPath[offCur]))
{
fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_UNC | RTPATH_PROP_ABSOLUTE;
offCur++;
cchPath = offCur;
}
else
{
fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
offCur = 1;
cchPath = 1;
}
}
#endif
else
{
#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
#else
fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
#endif
offCur = 1;
cchPath = 1;
}
}
#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
else if (RT_C_IS_ALPHA(pszPath[0]) && pszPath[1] == ':')
{
if (!RTPATH_IS_SLASH(pszPath[2]))
{
fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_RELATIVE;
offCur = 2;
}
else
{
fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
offCur = 3;
}
cchPath = offCur;
}
#endif
else
{
fProps = RTPATH_PROP_RELATIVE;
offCur = 0;
cchPath = 0;
}
/* Add it to the component array . */
if (offCur && !(fFlags & RTPATH_STR_F_NO_START))
{
cchPath = offCur;
if (idxComp < cMaxComps)
{
pParsed->aComps[idxComp].off = 0;
pParsed->aComps[idxComp].cch = offCur;
}
idxComp++;
/* Skip unnecessary slashes following the root-spec. */
if (RTPATH_IS_SLASH(pszPath[offCur]))
{
fProps |= RTPATH_PROP_EXTRA_SLASHES;
do
offCur++;
while (RTPATH_IS_SLASH(pszPath[offCur]));
}
}
/*
* Parse the rest.
*/
if (pszPath[offCur])
{
for (;;)
{
Assert(!RTPATH_IS_SLASH(pszPath[offCur]));
/* Find the end of the component. */
uint32_t offStart = offCur;
char ch;
while ((ch = pszPath[offCur]) != '\0' && !RTPATH_IS_SLASH(ch))
offCur++;
if (offCur >= _64K)
return VERR_FILENAME_TOO_LONG;
/* Add it. */
uint32_t cchComp = offCur - offStart;
if (idxComp < cMaxComps)
{
pParsed->aComps[idxComp].off = offStart;
pParsed->aComps[idxComp].cch = cchComp;
}
idxComp++;
cchPath += cchComp;
/* Look for '.' and '..' references. */
if (cchComp == 1 && pszPath[offCur - 1] == '.')
fProps |= RTPATH_PROP_DOT_REFS;
else if (cchComp == 2 && pszPath[offCur - 1] == '.' && pszPath[offCur - 2] == '.')
{
fProps &= ~RTPATH_PROP_ABSOLUTE;
fProps |= RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_RELATIVE;
}
/* Skip unnecessary slashes. Leave ch unchanged! */
char ch2 = ch;
if (ch2)
{
ch2 = pszPath[++offCur];
if (RTPATH_IS_SLASH(ch2))
{
fProps |= RTPATH_PROP_EXTRA_SLASHES;
do
ch2 = pszPath[++offCur];
while (RTPATH_IS_SLASH(ch2));
}
}
/* The end? */
if (ch2 == '\0')
{
pParsed->offSuffix = offCur;
pParsed->cchSuffix = 0;
if (ch)
{
if (!(fFlags & RTPATH_STR_F_NO_END))
{
fProps |= RTPATH_PROP_DIR_SLASH; /* (not counted in component, but in cchPath) */
cchPath++;
}
else
fProps |= RTPATH_PROP_EXTRA_SLASHES;
}
else if (!(fFlags & RTPATH_STR_F_NO_END))
{
fProps |= RTPATH_PROP_FILENAME;
/* look for an ? */
uint32_t offSuffix = offStart + cchComp;
while (offSuffix-- > offStart)
if (pszPath[offSuffix] == '.')
{
uint32_t cchSuffix = offStart + cchComp - offSuffix;
if (cchSuffix > 1 && offStart != offSuffix)
{
pParsed->cchSuffix = cchSuffix;
pParsed->offSuffix = offSuffix;
fProps |= RTPATH_PROP_SUFFIX;
}
break;
}
}
break;
}
/* No, not the end. Account for an separator before we restart the loop. */
cchPath += sizeof(RTPATH_SLASH_STR) - 1;
}
}
else
{
pParsed->offSuffix = offCur;
pParsed->cchSuffix = 0;
}
if (offCur >= _64K)
return VERR_FILENAME_TOO_LONG;
/*
* Store the remainder of the state and we're done.
*/
pParsed->fProps = fProps;
pParsed->cchPath = cchPath;
pParsed->cComps = idxComp;
return idxComp <= cMaxComps ? VINF_SUCCESS : VERR_BUFFER_OVERFLOW;
}
|