1 | /* $Id: RTPathSplit.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathSplit
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/path.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | RTDECL(int) RTPathSplit(const char *pszPath, PRTPATHSPLIT pSplit, size_t cbSplit, uint32_t fFlags)
|
---|
51 | {
|
---|
52 | /*
|
---|
53 | * Input validation.
|
---|
54 | */
|
---|
55 | AssertReturn(cbSplit >= RT_UOFFSETOF(RTPATHSPLIT, apszComps), VERR_INVALID_PARAMETER);
|
---|
56 | AssertPtrReturn(pSplit, VERR_INVALID_POINTER);
|
---|
57 | AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
|
---|
58 | AssertReturn(*pszPath, VERR_PATH_ZERO_LENGTH);
|
---|
59 | AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0), VERR_INVALID_FLAGS);
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Use RTPathParse to do the parsing.
|
---|
63 | * - This makes the ASSUMPTION that the output of this function is greater
|
---|
64 | * or equal to that of RTPathParsed.
|
---|
65 | * - We're aliasing the buffer here, so use volatile to avoid issues due to
|
---|
66 | * compiler optimizations.
|
---|
67 | */
|
---|
68 | RTPATHPARSED volatile *pParsedVolatile = (RTPATHPARSED volatile *)pSplit;
|
---|
69 | RTPATHSPLIT volatile *pSplitVolatile = (RTPATHSPLIT volatile *)pSplit;
|
---|
70 |
|
---|
71 | AssertCompile(sizeof(*pParsedVolatile) <= sizeof(*pSplitVolatile));
|
---|
72 | AssertCompile(sizeof(pParsedVolatile->aComps[0]) <= sizeof(pSplitVolatile->apszComps[0]));
|
---|
73 |
|
---|
74 | int rc = RTPathParse(pszPath, (PRTPATHPARSED)pParsedVolatile, cbSplit, fFlags);
|
---|
75 | if (RT_FAILURE(rc) && rc != VERR_BUFFER_OVERFLOW)
|
---|
76 | return rc;
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Calculate the required buffer space.
|
---|
80 | */
|
---|
81 | uint16_t const cComps = pParsedVolatile->cComps;
|
---|
82 | uint16_t const fProps = pParsedVolatile->fProps;
|
---|
83 | uint16_t const cchPath = pParsedVolatile->cchPath;
|
---|
84 | uint16_t const offSuffix = pParsedVolatile->offSuffix;
|
---|
85 | uint32_t cbNeeded = RT_UOFFSETOF_DYN(RTPATHSPLIT, apszComps[cComps])
|
---|
86 | + cchPath
|
---|
87 | + RTPATH_PROP_FIRST_NEEDS_NO_SLASH(fProps) /* zero terminator for root spec. */
|
---|
88 | - RT_BOOL(fProps & RTPATH_PROP_DIR_SLASH) /* counted by cchPath, not included in the comp str. */
|
---|
89 | + 1; /* zero terminator. */
|
---|
90 | if (cbNeeded > cbSplit)
|
---|
91 | {
|
---|
92 | pSplitVolatile->cbNeeded = cbNeeded;
|
---|
93 | return VERR_BUFFER_OVERFLOW;
|
---|
94 | }
|
---|
95 | Assert(RT_SUCCESS(rc));
|
---|
96 |
|
---|
97 | /*
|
---|
98 | * Convert the array and copy the strings, both backwards.
|
---|
99 | */
|
---|
100 | char *psz = (char *)pSplit + cbNeeded;
|
---|
101 | uint32_t idxComp = cComps - 1;
|
---|
102 |
|
---|
103 | /* the final component first (because of suffix handling). */
|
---|
104 | uint16_t offComp = pParsedVolatile->aComps[idxComp].off;
|
---|
105 | uint16_t cchComp = pParsedVolatile->aComps[idxComp].cch;
|
---|
106 |
|
---|
107 | *--psz = '\0';
|
---|
108 | psz -= cchComp;
|
---|
109 | memcpy(psz, &pszPath[offComp], cchComp);
|
---|
110 | pSplitVolatile->apszComps[idxComp] = psz;
|
---|
111 |
|
---|
112 | char *pszSuffix;
|
---|
113 | if (offSuffix >= offComp + cchComp)
|
---|
114 | pszSuffix = &psz[cchComp];
|
---|
115 | else
|
---|
116 | pszSuffix = &psz[offSuffix - offComp];
|
---|
117 |
|
---|
118 | /* the remainder */
|
---|
119 | while (idxComp-- > 0)
|
---|
120 | {
|
---|
121 | offComp = pParsedVolatile->aComps[idxComp].off;
|
---|
122 | cchComp = pParsedVolatile->aComps[idxComp].cch;
|
---|
123 | *--psz = '\0';
|
---|
124 | psz -= cchComp;
|
---|
125 | memcpy(psz, &pszPath[offComp], cchComp);
|
---|
126 | pSplitVolatile->apszComps[idxComp] = psz;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Store / reshuffle the non-array bits. This MUST be done after finishing
|
---|
131 | * the array processing because there may be members in RTPATHSPLIT
|
---|
132 | * overlapping the array of RTPATHPARSED.
|
---|
133 | */
|
---|
134 | AssertCompileMembersSameSizeAndOffset(RTPATHPARSED, cComps, RTPATHSPLIT, cComps); Assert(pSplitVolatile->cComps == cComps);
|
---|
135 | AssertCompileMembersSameSizeAndOffset(RTPATHPARSED, fProps, RTPATHSPLIT, fProps); Assert(pSplitVolatile->fProps == fProps);
|
---|
136 | AssertCompileMembersSameSizeAndOffset(RTPATHPARSED, cchPath, RTPATHSPLIT, cchPath); Assert(pSplitVolatile->cchPath == cchPath);
|
---|
137 | pSplitVolatile->u16Reserved = 0;
|
---|
138 | pSplitVolatile->cbNeeded = cbNeeded;
|
---|
139 | pSplitVolatile->pszSuffix = pszSuffix;
|
---|
140 |
|
---|
141 | return rc;
|
---|
142 | }
|
---|
143 |
|
---|