VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/RTPathParse.cpp.h@ 71916

Last change on this file since 71916 was 69111, checked in by vboxsync, 7 years ago

(C) year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.9 KB
Line 
1/* $Id: RTPathParse.cpp.h 69111 2017-10-17 14:26:02Z 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-2017 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 */
34static 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 uint32_t offSuffix = offStart + cchComp;
212 while (offSuffix-- > offStart)
213 if (pszPath[offSuffix] == '.')
214 {
215 uint32_t cchSuffix = offStart + cchComp - offSuffix;
216 if (cchSuffix > 1 && offStart != offSuffix)
217 {
218 pParsed->cchSuffix = cchSuffix;
219 pParsed->offSuffix = offSuffix;
220 fProps |= RTPATH_PROP_SUFFIX;
221 }
222 break;
223 }
224 }
225 break;
226 }
227
228 /* No, not the end. Account for an separator before we restart the loop. */
229 cchPath += sizeof(RTPATH_SLASH_STR) - 1;
230 }
231 }
232 else
233 {
234 pParsed->offSuffix = offCur;
235 pParsed->cchSuffix = 0;
236 }
237 if (offCur >= _64K)
238 return VERR_FILENAME_TOO_LONG;
239
240 /*
241 * Store the remainder of the state and we're done.
242 */
243 pParsed->fProps = fProps;
244 pParsed->cchPath = cchPath;
245 pParsed->cComps = idxComp;
246
247 return idxComp <= cMaxComps ? VINF_SUCCESS : VERR_BUFFER_OVERFLOW;
248}
249
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette