1 | /* $Id: stringalloc.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Manipulation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 <iprt/string.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #ifndef IN_RING0
|
---|
45 | # include <iprt/alloca.h>
|
---|
46 | #endif
|
---|
47 | #include <iprt/assert.h>
|
---|
48 | #include <iprt/mem.h>
|
---|
49 | #include <iprt/err.h>
|
---|
50 | #include "internal/string.h"
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | RTDECL(char *) RTStrAllocTag(size_t cb, const char *pszTag)
|
---|
55 | {
|
---|
56 | char *psz = (char *)RTMemAllocTag(RT_MAX(cb, 1), pszTag);
|
---|
57 | if (psz)
|
---|
58 | *psz = '\0';
|
---|
59 | return psz;
|
---|
60 | }
|
---|
61 | RT_EXPORT_SYMBOL(RTStrAllocTag);
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTStrAllocExTag(char **ppsz, size_t cb, const char *pszTag)
|
---|
65 | {
|
---|
66 | char *psz = *ppsz = (char *)RTMemAllocTag(RT_MAX(cb, 1), pszTag);
|
---|
67 | if (psz)
|
---|
68 | {
|
---|
69 | *psz = '\0';
|
---|
70 | return VINF_SUCCESS;
|
---|
71 | }
|
---|
72 | return VERR_NO_STR_MEMORY;
|
---|
73 | }
|
---|
74 | RT_EXPORT_SYMBOL(RTStrAllocExTag);
|
---|
75 |
|
---|
76 |
|
---|
77 | RTDECL(int) RTStrReallocTag(char **ppsz, size_t cbNew, const char *pszTag)
|
---|
78 | {
|
---|
79 | char *pszOld = *ppsz;
|
---|
80 | if (!cbNew)
|
---|
81 | {
|
---|
82 | RTMemFree(pszOld);
|
---|
83 | *ppsz = NULL;
|
---|
84 | }
|
---|
85 | else if (pszOld)
|
---|
86 | {
|
---|
87 | char *pszNew = (char *)RTMemReallocTag(pszOld, cbNew, pszTag);
|
---|
88 | if (!pszNew)
|
---|
89 | return VERR_NO_STR_MEMORY;
|
---|
90 | pszNew[cbNew - 1] = '\0';
|
---|
91 | *ppsz = pszNew;
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | char *pszNew = (char *)RTMemAllocTag(cbNew, pszTag);
|
---|
96 | if (!pszNew)
|
---|
97 | return VERR_NO_STR_MEMORY;
|
---|
98 | pszNew[0] = '\0';
|
---|
99 | pszNew[cbNew - 1] = '\0';
|
---|
100 | *ppsz = pszNew;
|
---|
101 | }
|
---|
102 | return VINF_SUCCESS;
|
---|
103 | }
|
---|
104 | RT_EXPORT_SYMBOL(RTStrReallocTag);
|
---|
105 |
|
---|
106 | RTDECL(void) RTStrFree(char *pszString)
|
---|
107 | {
|
---|
108 | if (pszString)
|
---|
109 | RTMemTmpFree(pszString);
|
---|
110 | }
|
---|
111 | RT_EXPORT_SYMBOL(RTStrFree);
|
---|
112 |
|
---|
113 |
|
---|
114 | RTDECL(char *) RTStrDupTag(const char *pszString, const char *pszTag)
|
---|
115 | {
|
---|
116 | #if defined(__cplusplus)
|
---|
117 | AssertPtr(pszString);
|
---|
118 | #endif
|
---|
119 | size_t cch = strlen(pszString) + 1;
|
---|
120 | char *psz = (char *)RTMemAllocTag(cch, pszTag);
|
---|
121 | if (psz)
|
---|
122 | memcpy(psz, pszString, cch);
|
---|
123 | return psz;
|
---|
124 | }
|
---|
125 | RT_EXPORT_SYMBOL(RTStrDupTag);
|
---|
126 |
|
---|
127 |
|
---|
128 | RTDECL(int) RTStrDupExTag(char **ppszCopy, const char *pszString, const char *pszTag)
|
---|
129 | {
|
---|
130 | #if defined(__cplusplus)
|
---|
131 | AssertPtr(ppszCopy);
|
---|
132 | AssertPtr(pszString);
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | size_t cch = strlen(pszString);
|
---|
136 | char *pszDst = (char *)RTMemAllocTag(cch + 1, pszTag);
|
---|
137 | if (pszDst)
|
---|
138 | {
|
---|
139 | memcpy(pszDst, pszString, cch);
|
---|
140 | pszDst[cch] = '\0';
|
---|
141 | *ppszCopy = pszDst;
|
---|
142 | return VINF_SUCCESS;
|
---|
143 | }
|
---|
144 | *ppszCopy = NULL;
|
---|
145 | return VERR_NO_STR_MEMORY;
|
---|
146 | }
|
---|
147 | RT_EXPORT_SYMBOL(RTStrDupExTag);
|
---|
148 |
|
---|
149 |
|
---|
150 | RTDECL(char *) RTStrDupNTag(const char *pszString, size_t cchMax, const char *pszTag)
|
---|
151 | {
|
---|
152 | #if defined(__cplusplus)
|
---|
153 | AssertPtr(pszString);
|
---|
154 | #endif
|
---|
155 | char const *pszEnd = RTStrEnd(pszString, cchMax);
|
---|
156 | size_t cch = pszEnd ? (uintptr_t)pszEnd - (uintptr_t)pszString : cchMax;
|
---|
157 | char *pszDst = (char *)RTMemAllocTag(cch + 1, pszTag);
|
---|
158 | if (pszDst)
|
---|
159 | {
|
---|
160 | memcpy(pszDst, pszString, cch);
|
---|
161 | pszDst[cch] = '\0';
|
---|
162 | }
|
---|
163 | return pszDst;
|
---|
164 | }
|
---|
165 | RT_EXPORT_SYMBOL(RTStrDupNTag);
|
---|
166 |
|
---|
167 |
|
---|
168 | RTDECL(int) RTStrDupNExTag(char **ppszCopy, const char *pszString, size_t cchMax, const char *pszTag)
|
---|
169 | {
|
---|
170 | #if defined(__cplusplus)
|
---|
171 | AssertPtr(pszString);
|
---|
172 | #endif
|
---|
173 | char const *pszEnd = RTStrEnd(pszString, cchMax);
|
---|
174 | size_t cch = pszEnd ? (uintptr_t)pszEnd - (uintptr_t)pszString : cchMax;
|
---|
175 | char *pszDst = (char *)RTMemAllocTag(cch + 1, pszTag);
|
---|
176 | if (pszDst)
|
---|
177 | {
|
---|
178 | memcpy(pszDst, pszString, cch);
|
---|
179 | pszDst[cch] = '\0';
|
---|
180 | *ppszCopy = pszDst;
|
---|
181 | return VINF_SUCCESS;
|
---|
182 | }
|
---|
183 | *ppszCopy = NULL;
|
---|
184 | return VERR_NO_STR_MEMORY;
|
---|
185 | }
|
---|
186 | RT_EXPORT_SYMBOL(RTStrDupNExTag);
|
---|
187 |
|
---|
188 |
|
---|
189 | RTDECL(int) RTStrAAppendTag(char **ppsz, const char *pszAppend, const char *pszTag)
|
---|
190 | {
|
---|
191 | if (!pszAppend)
|
---|
192 | return VINF_SUCCESS;
|
---|
193 | return RTStrAAppendNTag(ppsz, pszAppend, RTSTR_MAX, pszTag);
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | RTDECL(int) RTStrAAppendNTag(char **ppsz, const char *pszAppend, size_t cchAppend, const char *pszTag)
|
---|
198 | {
|
---|
199 | size_t cchOrg;
|
---|
200 | char *pszNew;
|
---|
201 |
|
---|
202 | if (!cchAppend)
|
---|
203 | return VINF_SUCCESS;
|
---|
204 | if (cchAppend == RTSTR_MAX)
|
---|
205 | cchAppend = strlen(pszAppend);
|
---|
206 | else
|
---|
207 | Assert(cchAppend == RTStrNLen(pszAppend, cchAppend));
|
---|
208 |
|
---|
209 | cchOrg = *ppsz ? strlen(*ppsz) : 0;
|
---|
210 | pszNew = (char *)RTMemReallocTag(*ppsz, cchOrg + cchAppend + 1, pszTag);
|
---|
211 | if (!pszNew)
|
---|
212 | return VERR_NO_STR_MEMORY;
|
---|
213 |
|
---|
214 | memcpy(&pszNew[cchOrg], pszAppend, cchAppend);
|
---|
215 | pszNew[cchOrg + cchAppend] = '\0';
|
---|
216 |
|
---|
217 | *ppsz = pszNew;
|
---|
218 | return VINF_SUCCESS;
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | #if !defined(IN_RING0) && !defined(IPRT_NO_ALLOCA_TROUBLE)
|
---|
223 |
|
---|
224 | /* XXX Currently not needed anywhere. alloca() induces some linker problems for ring 0 code
|
---|
225 | * with newer versions of VCC */
|
---|
226 |
|
---|
227 | RTDECL(int) RTStrAAppendExNVTag(char **ppsz, size_t cPairs, va_list va, const char *pszTag)
|
---|
228 | {
|
---|
229 | AssertPtr(ppsz);
|
---|
230 | if (!cPairs)
|
---|
231 | return VINF_SUCCESS;
|
---|
232 |
|
---|
233 | /*
|
---|
234 | * Determine the length of each string and calc the new total.
|
---|
235 | */
|
---|
236 | struct RTStrAAppendExNVStruct
|
---|
237 | {
|
---|
238 | const char *psz;
|
---|
239 | size_t cch;
|
---|
240 | } *paPairs = (struct RTStrAAppendExNVStruct *)alloca(cPairs * sizeof(*paPairs));
|
---|
241 | AssertReturn(paPairs, VERR_NO_STR_MEMORY);
|
---|
242 |
|
---|
243 | size_t cchOrg = *ppsz ? strlen(*ppsz) : 0;
|
---|
244 | size_t cchNewTotal = cchOrg;
|
---|
245 | for (size_t i = 0; i < cPairs; i++)
|
---|
246 | {
|
---|
247 | const char *psz = va_arg(va, const char *);
|
---|
248 | size_t cch = va_arg(va, size_t);
|
---|
249 | AssertPtrNull(psz);
|
---|
250 | Assert(cch == RTSTR_MAX || cch == RTStrNLen(psz, cch));
|
---|
251 |
|
---|
252 | if (cch == RTSTR_MAX)
|
---|
253 | cch = psz ? strlen(psz) : 0;
|
---|
254 | cchNewTotal += cch;
|
---|
255 |
|
---|
256 | paPairs[i].cch = cch;
|
---|
257 | paPairs[i].psz = psz;
|
---|
258 | }
|
---|
259 | cchNewTotal++; /* '\0' */
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * Try reallocate the string.
|
---|
263 | */
|
---|
264 | char *pszNew = (char *)RTMemReallocTag(*ppsz, cchNewTotal, pszTag);
|
---|
265 | if (!pszNew)
|
---|
266 | return VERR_NO_STR_MEMORY;
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Do the appending.
|
---|
270 | */
|
---|
271 | size_t off = cchOrg;
|
---|
272 | for (size_t i = 0; i < cPairs; i++)
|
---|
273 | {
|
---|
274 | memcpy(&pszNew[off], paPairs[i].psz, paPairs[i].cch);
|
---|
275 | off += paPairs[i].cch;
|
---|
276 | }
|
---|
277 | Assert(off + 1 == cchNewTotal);
|
---|
278 | pszNew[off] = '\0';
|
---|
279 |
|
---|
280 | /* done */
|
---|
281 | *ppsz = pszNew;
|
---|
282 | return VINF_SUCCESS;
|
---|
283 | }
|
---|
284 | RT_EXPORT_SYMBOL(RTStrAAppendExNVTag);
|
---|
285 |
|
---|
286 | #endif
|
---|
287 |
|
---|
288 |
|
---|
289 | RTDECL(int) RTStrATruncateTag(char **ppsz, size_t cchNew, const char *pszTag)
|
---|
290 | {
|
---|
291 | char *pszNew;
|
---|
292 | char *pszOld = *ppsz;
|
---|
293 | if (!cchNew)
|
---|
294 | {
|
---|
295 | if (pszOld && *pszOld)
|
---|
296 | {
|
---|
297 | *pszOld = '\0';
|
---|
298 | pszNew = (char *)RTMemReallocTag(pszOld, 1, pszTag);
|
---|
299 | if (pszNew)
|
---|
300 | *ppsz = pszNew;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | else
|
---|
304 | {
|
---|
305 | char *pszZero;
|
---|
306 | AssertPtrReturn(pszOld, VERR_OUT_OF_RANGE);
|
---|
307 | AssertReturn(cchNew < ~(size_t)64, VERR_OUT_OF_RANGE);
|
---|
308 | pszZero = RTStrEnd(pszOld, cchNew + 63);
|
---|
309 | AssertReturn(!pszZero || (size_t)(pszZero - pszOld) >= cchNew, VERR_OUT_OF_RANGE);
|
---|
310 | pszOld[cchNew] = '\0';
|
---|
311 | if (!pszZero)
|
---|
312 | {
|
---|
313 | pszNew = (char *)RTMemReallocTag(pszOld, cchNew + 1, pszTag);
|
---|
314 | if (pszNew)
|
---|
315 | *ppsz = pszNew;
|
---|
316 | }
|
---|
317 | }
|
---|
318 | return VINF_SUCCESS;
|
---|
319 | }
|
---|
320 | RT_EXPORT_SYMBOL(RTStrATruncateTag);
|
---|
321 |
|
---|