VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/path/RTPathCalcRelative.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.3 KB
Line 
1/* $Id: RTPathCalcRelative.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - RTPathCreateRelative.
4 */
5
6/*
7 * Copyright (C) 2013-2023 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#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
48# include <iprt/uni.h>
49#endif
50#include "internal/path.h"
51
52
53#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
54/** Helper for doing case insensitive comparison of a mismatching codepoint. */
55DECLINLINE(bool) rtPathCalcRelativeEqualICaseCodepoint(const char *pszPathFromStart, const char *pszPathFrom,
56 const char *pszPathToStart, const char *pszPathTo)
57{
58 RTUNICP ucFrom = RTStrGetCp(RTStrPrevCp(pszPathFromStart, pszPathFrom));
59 RTUNICP ucTo = RTStrGetCp(RTStrPrevCp(pszPathToStart, pszPathTo));
60 return ucFrom == ucTo
61 || RTUniCpToLower(ucFrom) == RTUniCpToLower(ucTo)
62 || RTUniCpToUpper(ucFrom) == RTUniCpToUpper(ucTo);
63}
64#endif
65
66
67RTDECL(int) RTPathCalcRelative(char *pszPathDst, size_t cbPathDst,
68 const char *pszPathFrom, bool fFromFile,
69 const char *pszPathTo)
70{
71 AssertPtrReturn(pszPathDst, VERR_INVALID_POINTER);
72 AssertReturn(cbPathDst, VERR_INVALID_PARAMETER);
73 AssertPtrReturn(pszPathFrom, VERR_INVALID_POINTER);
74 AssertPtrReturn(pszPathTo, VERR_INVALID_POINTER);
75#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
76 const char * const pszPathFromStart = pszPathFrom;
77 const char * const pszPathToStart = pszPathTo;
78#endif
79
80 /*
81 * Check for different root specifiers (drive letters), creating a relative path doesn't work here.
82 */
83 size_t offRootFrom = rtPathRootSpecLen(pszPathFrom);
84 AssertReturn(offRootFrom > 0, VERR_INVALID_PARAMETER);
85
86 size_t offRootTo = rtPathRootSpecLen(pszPathTo);
87 AssertReturn(offRootTo > 0, VERR_INVALID_PARAMETER);
88
89
90 /** @todo correctly deal with extra root slashes! */
91 if (offRootFrom != offRootTo)
92 return VERR_NOT_SUPPORTED;
93
94#if RTPATH_STYLE != RTPATH_STR_F_STYLE_DOS
95 if (RTStrNCmp(pszPathFrom, pszPathTo, offRootFrom))
96 return VERR_NOT_SUPPORTED;
97#else
98 if (RTStrNICmp(pszPathFrom, pszPathTo, offRootFrom))
99 {
100 const char *pszFromCursor = pszPathFrom;
101 const char *pszToCursor = pszPathTo;
102 while ((size_t)(pszFromCursor - pszPathFrom) < offRootFrom)
103 {
104 RTUNICP ucFrom;
105 int rc = RTStrGetCpEx(&pszFromCursor, &ucFrom);
106 AssertRCReturn(rc, rc);
107
108 RTUNICP ucTo;
109 rc = RTStrGetCpEx(&pszToCursor, &ucTo);
110 AssertRCReturn(rc, rc);
111 if ( ucFrom != ucTo
112 && RTUniCpToLower(ucFrom) != RTUniCpToLower(ucTo)
113 && RTUniCpToUpper(ucFrom) != RTUniCpToUpper(ucTo)
114 && (!RTPATH_IS_SLASH(ucFrom) || !RTPATH_IS_SLASH(ucTo)) )
115 return VERR_NOT_SUPPORTED;
116 }
117 }
118#endif
119
120 pszPathFrom += offRootFrom;
121 pszPathTo += offRootTo;
122
123 /*
124 * Skip out the part of the path which is equal to both.
125 */
126 const char *pszStartOfFromComp = pszPathFrom;
127 for (;;)
128 {
129 char const chFrom = *pszPathFrom;
130 char const chTo = *pszPathTo;
131 if (!RTPATH_IS_SLASH(chFrom))
132 {
133 if (chFrom == chTo)
134 {
135 if (chFrom)
136 { /* likely */ }
137 else
138 {
139 /* Special case: The two paths are equal. */
140 if (fFromFile)
141 {
142 size_t cchComp = pszPathFrom - pszStartOfFromComp;
143 if (cchComp < cbPathDst)
144 {
145 memcpy(pszPathDst, pszStartOfFromComp, cchComp);
146 pszPathDst[cchComp] = '\0';
147 return VINF_SUCCESS;
148 }
149 }
150 else if (sizeof(".") <= cbPathDst)
151 {
152 memcpy(pszPathDst, ".", sizeof("."));
153 return VINF_SUCCESS;
154 }
155 return VERR_BUFFER_OVERFLOW;
156 }
157 }
158#if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
159 else if (rtPathCalcRelativeEqualICaseCodepoint(pszPathFromStart, pszPathFrom + 1, pszPathToStart, pszPathTo + 1))
160 { /* if not likely, then simpler code structure wise. */ }
161#endif
162 else if (chFrom != '\0' || !RTPATH_IS_SLASH(chTo) || fFromFile)
163 break;
164 else
165 {
166 pszStartOfFromComp = pszPathFrom;
167 do
168 pszPathTo++;
169 while (RTPATH_IS_SLASH(*pszPathTo));
170 break;
171 }
172 pszPathFrom++;
173 pszPathTo++;
174 }
175 else if (RTPATH_IS_SLASH(chTo))
176 {
177 /* Both have slashes. Skip any additional ones before taking down
178 the start of the component for rewinding purposes. */
179 do
180 pszPathTo++;
181 while (RTPATH_IS_SLASH(*pszPathTo));
182 do
183 pszPathFrom++;
184 while (RTPATH_IS_SLASH(*pszPathFrom));
185 pszStartOfFromComp = pszPathFrom;
186 }
187 else
188 break;
189 }
190
191 /* Rewind to the start of the current component. */
192 pszPathTo -= pszPathFrom - pszStartOfFromComp;
193 pszPathFrom = pszStartOfFromComp;
194
195 /* Paths point to the first non equal component now. */
196
197 /*
198 * Constructure the relative path.
199 */
200
201 /* Create the part to go up from pszPathFrom. */
202 unsigned offDst = 0;
203
204 if (!fFromFile && *pszPathFrom != '\0')
205 {
206 if (offDst + 3 < cbPathDst)
207 {
208 pszPathDst[offDst++] = '.';
209 pszPathDst[offDst++] = '.';
210 pszPathDst[offDst++] = RTPATH_SLASH;
211 }
212 else
213 return VERR_BUFFER_OVERFLOW;
214 }
215
216 while (*pszPathFrom != '\0')
217 {
218 char ch;
219 while ( (ch = *pszPathFrom) != '\0'
220 && !RTPATH_IS_SLASH(*pszPathFrom))
221 pszPathFrom++;
222 while ( (ch = *pszPathFrom) != '\0'
223 && RTPATH_IS_SLASH(ch))
224 pszPathFrom++;
225 if (!ch)
226 break;
227
228 if (offDst + 3 < cbPathDst)
229 {
230 pszPathDst[offDst++] = '.';
231 pszPathDst[offDst++] = '.';
232 pszPathDst[offDst++] = RTPATH_SLASH;
233 }
234 else
235 return VERR_BUFFER_OVERFLOW;
236 }
237
238 /* Now append the rest of pszPathTo to the final path. */
239 size_t cchTo = strlen(pszPathTo);
240 if (offDst + cchTo <= cbPathDst)
241 {
242 memcpy(&pszPathDst[offDst], pszPathTo, cchTo);
243 pszPathDst[offDst + cchTo] = '\0';
244 return VINF_SUCCESS;
245 }
246 return VERR_BUFFER_OVERFLOW;
247}
248
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use