1 | /* $Id: RTPathParse.cpp.h 46179 2013-05-20 21:38:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathParse - Code Template.
|
---|
4 | *
|
---|
5 | * This file included multiple times with different path style macros.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2006-2013 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | *
|
---|
19 | * The contents of this file may alternatively be used under the terms
|
---|
20 | * of the Common Development and Distribution License Version 1.0
|
---|
21 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
22 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
23 | * CDDL are applicable instead of those of the GPL.
|
---|
24 | *
|
---|
25 | * You may elect to license modified versions of this file under the
|
---|
26 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
27 | */
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * @copydoc RTPathParse
|
---|
33 | */
|
---|
34 | static int RTPATH_STYLE_FN(rtPathParse)(const char *pszPath, PRTPATHPARSED pParsed, size_t cbParsed, uint32_t fFlags)
|
---|
35 | {
|
---|
36 | /*
|
---|
37 | * Parse the root specification if present and initialize the parser state
|
---|
38 | * (keep it on the stack for speed).
|
---|
39 | */
|
---|
40 | uint32_t const cMaxComps = cbParsed < RT_UOFFSETOF(RTPATHPARSED, aComps[0xfff0])
|
---|
41 | ? (uint32_t)((cbParsed - RT_UOFFSETOF(RTPATHPARSED, aComps)) / sizeof(pParsed->aComps[0]))
|
---|
42 | : 0xfff0;
|
---|
43 | uint32_t idxComp = 0;
|
---|
44 | uint32_t cchPath;
|
---|
45 | uint32_t offCur;
|
---|
46 | uint16_t fProps;
|
---|
47 |
|
---|
48 | if (RTPATH_IS_SLASH(pszPath[0]))
|
---|
49 | {
|
---|
50 | if (fFlags & RTPATH_STR_F_NO_START)
|
---|
51 | {
|
---|
52 | offCur = 1;
|
---|
53 | while (RTPATH_IS_SLASH(pszPath[offCur]))
|
---|
54 | offCur++;
|
---|
55 | if (!pszPath[offCur])
|
---|
56 | return VERR_PATH_ZERO_LENGTH;
|
---|
57 | fProps = RTPATH_PROP_RELATIVE | RTPATH_PROP_EXTRA_SLASHES;
|
---|
58 | cchPath = 0;
|
---|
59 | }
|
---|
60 | #if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
|
---|
61 | else if ( RTPATH_IS_SLASH(pszPath[1])
|
---|
62 | && !RTPATH_IS_SLASH(pszPath[2])
|
---|
63 | && pszPath[2])
|
---|
64 | {
|
---|
65 | /* UNC - skip to the end of the potential namespace or computer name. */
|
---|
66 | offCur = 2;
|
---|
67 | while (!RTPATH_IS_SLASH(pszPath[offCur]) && pszPath[offCur])
|
---|
68 | offCur++;
|
---|
69 |
|
---|
70 | /* If there is another slash, we considered it a valid UNC path, if
|
---|
71 | not it's just a root path with an extra slash thrown in. */
|
---|
72 | if (RTPATH_IS_SLASH(pszPath[offCur]))
|
---|
73 | {
|
---|
74 | fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_UNC | RTPATH_PROP_ABSOLUTE;
|
---|
75 | offCur++;
|
---|
76 | cchPath = offCur;
|
---|
77 | }
|
---|
78 | else
|
---|
79 | {
|
---|
80 | fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
|
---|
81 | offCur = 1;
|
---|
82 | cchPath = 1;
|
---|
83 | }
|
---|
84 | }
|
---|
85 | #endif
|
---|
86 | else
|
---|
87 | {
|
---|
88 | #if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
|
---|
89 | fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_RELATIVE;
|
---|
90 | #else
|
---|
91 | fProps = RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
|
---|
92 | #endif
|
---|
93 | offCur = 1;
|
---|
94 | cchPath = 1;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | #if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
|
---|
98 | else if (RT_C_IS_ALPHA(pszPath[0]) && pszPath[1] == ':')
|
---|
99 | {
|
---|
100 | if (!RTPATH_IS_SLASH(pszPath[2]))
|
---|
101 | {
|
---|
102 | fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_RELATIVE;
|
---|
103 | offCur = 2;
|
---|
104 | }
|
---|
105 | else
|
---|
106 | {
|
---|
107 | fProps = RTPATH_PROP_VOLUME | RTPATH_PROP_ROOT_SLASH | RTPATH_PROP_ABSOLUTE;
|
---|
108 | offCur = 3;
|
---|
109 | }
|
---|
110 | cchPath = offCur;
|
---|
111 | }
|
---|
112 | #endif
|
---|
113 | else
|
---|
114 | {
|
---|
115 | fProps = RTPATH_PROP_RELATIVE;
|
---|
116 | offCur = 0;
|
---|
117 | cchPath = 0;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* Add it to the component array . */
|
---|
121 | if (offCur && !(fFlags & RTPATH_STR_F_NO_START))
|
---|
122 | {
|
---|
123 | cchPath = offCur;
|
---|
124 | if (idxComp < cMaxComps)
|
---|
125 | {
|
---|
126 | pParsed->aComps[idxComp].off = 0;
|
---|
127 | pParsed->aComps[idxComp].cch = offCur;
|
---|
128 | }
|
---|
129 | idxComp++;
|
---|
130 |
|
---|
131 | /* Skip unnecessary slashes following the root-spec. */
|
---|
132 | if (RTPATH_IS_SLASH(pszPath[offCur]))
|
---|
133 | {
|
---|
134 | fProps |= RTPATH_PROP_EXTRA_SLASHES;
|
---|
135 | do
|
---|
136 | offCur++;
|
---|
137 | while (RTPATH_IS_SLASH(pszPath[offCur]));
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Parse the rest.
|
---|
143 | */
|
---|
144 | if (pszPath[offCur])
|
---|
145 | {
|
---|
146 | for (;;)
|
---|
147 | {
|
---|
148 | Assert(!RTPATH_IS_SLASH(pszPath[offCur]));
|
---|
149 |
|
---|
150 | /* Find the end of the component. */
|
---|
151 | uint32_t offStart = offCur;
|
---|
152 | char ch;
|
---|
153 | while ((ch = pszPath[offCur]) != '\0' && !RTPATH_IS_SLASH(ch))
|
---|
154 | offCur++;
|
---|
155 | if (offCur >= _64K)
|
---|
156 | return VERR_FILENAME_TOO_LONG;
|
---|
157 |
|
---|
158 | /* Add it. */
|
---|
159 | uint32_t cchComp = offCur - offStart;
|
---|
160 | if (idxComp < cMaxComps)
|
---|
161 | {
|
---|
162 | pParsed->aComps[idxComp].off = offStart;
|
---|
163 | pParsed->aComps[idxComp].cch = cchComp;
|
---|
164 | }
|
---|
165 | idxComp++;
|
---|
166 | cchPath += cchComp;
|
---|
167 |
|
---|
168 | /* Look for '.' and '..' references. */
|
---|
169 | if (cchComp == 1 && pszPath[offCur - 1] == '.')
|
---|
170 | fProps |= RTPATH_PROP_DOT_REFS;
|
---|
171 | else if (cchComp == 2 && pszPath[offCur - 1] == '.' && pszPath[offCur - 2] == '.')
|
---|
172 | {
|
---|
173 | fProps &= ~RTPATH_PROP_ABSOLUTE;
|
---|
174 | fProps |= RTPATH_PROP_DOTDOT_REFS | RTPATH_PROP_RELATIVE;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /* Skip unnecessary slashes. Leave ch unchanged! */
|
---|
178 | char ch2 = ch;
|
---|
179 | if (ch2)
|
---|
180 | {
|
---|
181 | ch2 = pszPath[++offCur];
|
---|
182 | if (RTPATH_IS_SLASH(ch2))
|
---|
183 | {
|
---|
184 | fProps |= RTPATH_PROP_EXTRA_SLASHES;
|
---|
185 | do
|
---|
186 | ch2 = pszPath[++offCur];
|
---|
187 | while (RTPATH_IS_SLASH(ch2));
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* The end? */
|
---|
192 | if (ch2 == '\0')
|
---|
193 | {
|
---|
194 | pParsed->offSuffix = offCur;
|
---|
195 | pParsed->cchSuffix = 0;
|
---|
196 | if (ch)
|
---|
197 | {
|
---|
198 | if (!(fFlags & RTPATH_STR_F_NO_END))
|
---|
199 | {
|
---|
200 | fProps |= RTPATH_PROP_DIR_SLASH; /* (not counted in component, but in cchPath) */
|
---|
201 | cchPath++;
|
---|
202 | }
|
---|
203 | else
|
---|
204 | fProps |= RTPATH_PROP_EXTRA_SLASHES;
|
---|
205 | }
|
---|
206 | else if (!(fFlags & RTPATH_STR_F_NO_END))
|
---|
207 | {
|
---|
208 | fProps |= RTPATH_PROP_FILENAME;
|
---|
209 |
|
---|
210 | /* look for an ? */
|
---|
211 | uint16_t cDots = 0;
|
---|
212 | uint32_t offSuffix = offStart + cchComp;
|
---|
213 | while (offSuffix-- > offStart)
|
---|
214 | if (pszPath[offSuffix] == '.')
|
---|
215 | {
|
---|
216 | uint32_t cchSuffix = offStart + cchComp - offSuffix;
|
---|
217 | if (cchSuffix > 1 && offStart != offSuffix)
|
---|
218 | {
|
---|
219 | pParsed->cchSuffix = cchSuffix;
|
---|
220 | pParsed->offSuffix = offSuffix;
|
---|
221 | fProps |= RTPATH_PROP_SUFFIX;
|
---|
222 | }
|
---|
223 | break;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | break;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /* No, not the end. Account for an separator before we restart the loop. */
|
---|
230 | cchPath += sizeof(RTPATH_SLASH_STR) - 1;
|
---|
231 | }
|
---|
232 | }
|
---|
233 | else
|
---|
234 | {
|
---|
235 | pParsed->offSuffix = offCur;
|
---|
236 | pParsed->cchSuffix = 0;
|
---|
237 | }
|
---|
238 | if (offCur >= _64K)
|
---|
239 | return VERR_FILENAME_TOO_LONG;
|
---|
240 |
|
---|
241 | /*
|
---|
242 | * Store the remainder of the state and we're done.
|
---|
243 | */
|
---|
244 | pParsed->fProps = fProps;
|
---|
245 | pParsed->cchPath = cchPath;
|
---|
246 | pParsed->cComps = idxComp;
|
---|
247 |
|
---|
248 | return idxComp <= cMaxComps ? VINF_SUCCESS : VERR_BUFFER_OVERFLOW;
|
---|
249 | }
|
---|
250 |
|
---|