1 | /* $Id: RTPathSplitReassemble.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTPathSplitReassemble.
|
---|
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/errcore.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | RTDECL(int) RTPathSplitReassemble(PRTPATHSPLIT pSplit, uint32_t fFlags, char *pszDstPath, size_t cbDstPath)
|
---|
50 | {
|
---|
51 | /*
|
---|
52 | * Input validation.
|
---|
53 | */
|
---|
54 | AssertPtrReturn(pSplit, VERR_INVALID_POINTER);
|
---|
55 | AssertReturn(pSplit->cComps > 0, VERR_INVALID_PARAMETER);
|
---|
56 | AssertReturn(RTPATH_STR_F_IS_VALID(fFlags, 0) && !(fFlags & RTPATH_STR_F_MIDDLE), VERR_INVALID_FLAGS);
|
---|
57 | AssertPtrReturn(pszDstPath, VERR_INVALID_POINTER);
|
---|
58 | AssertReturn(cbDstPath > pSplit->cchPath, VERR_BUFFER_OVERFLOW);
|
---|
59 |
|
---|
60 | /*
|
---|
61 | * Figure which slash to use.
|
---|
62 | */
|
---|
63 | char chSlash;
|
---|
64 | switch (fFlags & RTPATH_STR_F_STYLE_MASK)
|
---|
65 | {
|
---|
66 | case RTPATH_STR_F_STYLE_HOST:
|
---|
67 | chSlash = RTPATH_SLASH;
|
---|
68 | break;
|
---|
69 |
|
---|
70 | case RTPATH_STR_F_STYLE_DOS:
|
---|
71 | chSlash = '\\';
|
---|
72 | break;
|
---|
73 |
|
---|
74 | case RTPATH_STR_F_STYLE_UNIX:
|
---|
75 | chSlash = '/';
|
---|
76 | break;
|
---|
77 |
|
---|
78 | default:
|
---|
79 | AssertFailedReturn(VERR_INVALID_FLAGS); /* impossible */
|
---|
80 | }
|
---|
81 |
|
---|
82 | /*
|
---|
83 | * Do the joining.
|
---|
84 | */
|
---|
85 | uint32_t const cchOrgPath = pSplit->cchPath;
|
---|
86 | size_t cchDstPath = 0;
|
---|
87 | uint32_t const cComps = pSplit->cComps;
|
---|
88 | uint32_t idxComp = 0;
|
---|
89 | char *pszDst = pszDstPath;
|
---|
90 | size_t cchComp;
|
---|
91 |
|
---|
92 | if (RTPATH_PROP_HAS_ROOT_SPEC(pSplit->fProps))
|
---|
93 | {
|
---|
94 | cchComp = strlen(pSplit->apszComps[0]);
|
---|
95 | cchDstPath += cchComp;
|
---|
96 | AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
|
---|
97 | memcpy(pszDst, pSplit->apszComps[0], cchComp);
|
---|
98 |
|
---|
99 | /* fix the slashes */
|
---|
100 | char chOtherSlash = chSlash == '\\' ? '/' : '\\';
|
---|
101 | while (cchComp-- > 0)
|
---|
102 | {
|
---|
103 | if (*pszDst == chOtherSlash)
|
---|
104 | *pszDst = chSlash;
|
---|
105 | pszDst++;
|
---|
106 | }
|
---|
107 | idxComp = 1;
|
---|
108 | }
|
---|
109 |
|
---|
110 | while (idxComp < cComps)
|
---|
111 | {
|
---|
112 | cchComp = strlen(pSplit->apszComps[idxComp]);
|
---|
113 | cchDstPath += cchComp;
|
---|
114 | AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
|
---|
115 | memcpy(pszDst, pSplit->apszComps[idxComp], cchComp);
|
---|
116 | pszDst += cchComp;
|
---|
117 | idxComp++;
|
---|
118 | if (idxComp != cComps || (pSplit->fProps & RTPATH_PROP_DIR_SLASH))
|
---|
119 | {
|
---|
120 | cchDstPath++;
|
---|
121 | AssertReturn(cchDstPath <= cchOrgPath, VERR_INVALID_PARAMETER);
|
---|
122 | *pszDst++ = chSlash;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | *pszDst = '\0';
|
---|
127 |
|
---|
128 | return VINF_SUCCESS;
|
---|
129 | }
|
---|
130 |
|
---|