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
|
/* $Id: RTPathSplit.cpp $ */
/** @file
* IPRT - RTPathSplit
*/
/*
* Copyright (C) 2013-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.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include "internal/iprt.h"
#include <iprt/path.h>
#include <iprt/assert.h>
#include <iprt/err.h>
#include <iprt/string.h>
RTDECL(int) RTPathSplit(const char *pszPath, PRTPATHSPLIT pSplit, size_t cbSplit, uint32_t fFlags)
{
/*
* Input validation.
*/
AssertReturn(cbSplit >= RT_UOFFSETOF(RTPATHSPLIT, apszComps), VERR_INVALID_PARAMETER);
AssertPtrReturn(pSplit, VERR_INVALID_POINTER);
AssertPtrReturn(pszPath, VERR_INVALID_POINTER);
AssertReturn(*pszPath, VERR_PATH_ZERO_LENGTH);
AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0), VERR_INVALID_FLAGS);
/*
* Use RTPathParse to do the parsing.
* - This makes the ASSUMPTION that the output of this function is greater
* or equal to that of RTPathParsed.
* - We're aliasing the buffer here, so use volatile to avoid issues due to
* compiler optimizations.
*/
RTPATHPARSED volatile *pParsedVolatile = (RTPATHPARSED volatile *)pSplit;
RTPATHSPLIT volatile *pSplitVolatile = (RTPATHSPLIT volatile *)pSplit;
AssertCompile(sizeof(*pParsedVolatile) <= sizeof(*pSplitVolatile));
AssertCompile(sizeof(pParsedVolatile->aComps[0]) <= sizeof(pSplitVolatile->apszComps[0]));
int rc = RTPathParse(pszPath, (PRTPATHPARSED)pParsedVolatile, cbSplit, fFlags);
if (RT_FAILURE(rc) && rc != VERR_BUFFER_OVERFLOW)
return rc;
/*
* Calculate the required buffer space.
*/
uint16_t const cComps = pParsedVolatile->cComps;
uint16_t const fProps = pParsedVolatile->fProps;
uint16_t const cchPath = pParsedVolatile->cchPath;
uint16_t const offSuffix = pParsedVolatile->offSuffix;
uint32_t cbNeeded = RT_UOFFSETOF_DYN(RTPATHSPLIT, apszComps[cComps])
+ cchPath
+ RTPATH_PROP_FIRST_NEEDS_NO_SLASH(fProps) /* zero terminator for root spec. */
- RT_BOOL(fProps & RTPATH_PROP_DIR_SLASH) /* counted by cchPath, not included in the comp str. */
+ 1; /* zero terminator. */
if (cbNeeded > cbSplit)
{
pSplitVolatile->cbNeeded = cbNeeded;
return VERR_BUFFER_OVERFLOW;
}
Assert(RT_SUCCESS(rc));
/*
* Convert the array and copy the strings, both backwards.
*/
char *psz = (char *)pSplit + cbNeeded;
uint32_t idxComp = cComps - 1;
/* the final component first (because of suffix handling). */
uint16_t offComp = pParsedVolatile->aComps[idxComp].off;
uint16_t cchComp = pParsedVolatile->aComps[idxComp].cch;
*--psz = '\0';
psz -= cchComp;
memcpy(psz, &pszPath[offComp], cchComp);
pSplitVolatile->apszComps[idxComp] = psz;
char *pszSuffix;
if (offSuffix >= offComp + cchComp)
pszSuffix = &psz[cchComp];
else
pszSuffix = &psz[offSuffix - offComp];
/* the remainder */
while (idxComp-- > 0)
{
offComp = pParsedVolatile->aComps[idxComp].off;
cchComp = pParsedVolatile->aComps[idxComp].cch;
*--psz = '\0';
psz -= cchComp;
memcpy(psz, &pszPath[offComp], cchComp);
pSplitVolatile->apszComps[idxComp] = psz;
}
/*
* Store / reshuffle the non-array bits. This MUST be done after finishing
* the array processing because there may be members in RTPATHSPLIT
* overlapping the array of RTPATHPARSED.
*/
AssertCompileMembersSameSizeAndOffset(RTPATHPARSED, cComps, RTPATHSPLIT, cComps); Assert(pSplitVolatile->cComps == cComps);
AssertCompileMembersSameSizeAndOffset(RTPATHPARSED, fProps, RTPATHSPLIT, fProps); Assert(pSplitVolatile->fProps == fProps);
AssertCompileMembersSameSizeAndOffset(RTPATHPARSED, cchPath, RTPATHSPLIT, cchPath); Assert(pSplitVolatile->cchPath == cchPath);
pSplitVolatile->u16Reserved = 0;
pSplitVolatile->cbNeeded = cbNeeded;
pSplitVolatile->pszSuffix = pszSuffix;
return rc;
}
|