VirtualBox

source: vbox/trunk/src/VBox/Main/glue/xpcom/helpers.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: helpers.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * COM helper functions for XPCOM
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 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#include "VBox/com/defs.h"
29#include <nsMemory.h>
30#include <iprt/assertcompile.h>
31#include <iprt/utf16.h>
32
33
34//
35// OLE Automation string APIs
36//
37
38// Note: on Windows, every BSTR stores its length in the
39// byte just before the pointer you get. If we do it like this,
40// the caller cannot just use nsMemory::Free() on our strings.
41// Therefore we'll try to implement it differently and hope that
42// we don't run into problems.
43
44/**
45 * Copies a string into a new memory block including the terminating UCS2 NULL.
46 *
47 * @param pwsz Source string to duplicate.
48 * @returns New BSTR string buffer.
49 */
50BSTR SysAllocString(const OLECHAR *pwszSrc)
51{
52 AssertCompile(sizeof(*pwszSrc) == sizeof(PRUnichar));
53 if (pwszSrc)
54 return SysAllocStringLen(pwszSrc, RTUtf16Len((PCRTUTF16)pwszSrc));
55 return NULL;
56}
57
58/**
59 * Duplicates an ANSI string into a BSTR / allocates a BSTR with a size given in
60 * bytes.
61 *
62 * No conversion is done.
63 *
64 * @param pszSrc Source string to copy, optional.
65 * @param cbSrcReq Length of the source string / memory request in bytes.
66 * @returns new BSTR string buffer, NULL on failure.
67 */
68BSTR SysAllocStringByteLen(char const *pszSrc, unsigned int cbSrcReq)
69{
70 BSTR pBstrNew = (BSTR)nsMemory::Alloc(RT_ALIGN_Z(cbSrcReq + sizeof(OLECHAR), sizeof(OLECHAR)));
71 AssertCompile(sizeof(*pBstrNew) == sizeof(OLECHAR));
72 if (pBstrNew)
73 {
74 if (!pszSrc)
75 memset(pBstrNew, 0, cbSrcReq + sizeof(OLECHAR));
76 else
77 {
78 // Copy the string and make sure it is terminated.
79 memcpy(pBstrNew, pszSrc, cbSrcReq);
80 char *pchTerminator = (char *)pBstrNew;
81 pchTerminator[cbSrcReq] = '\0';
82 pchTerminator[cbSrcReq + 1] = '\0';
83 }
84 }
85 return pBstrNew;
86}
87
88/**
89 * Duplicates a UTF-16 string into a BSTR / Allocates a BSTR with a size given
90 * in UTF-16 characters.
91 *
92 * @param pwszSrc Pointer to the source string, optional.
93 * @param cwcSrcReq Length of the source string / memory request in UTF-16
94 * characters.
95 * @returns new BSTR string buffer, NULL on failure.
96 */
97BSTR SysAllocStringLen(const OLECHAR *pwszSrc, unsigned int cwcSrcReq)
98{
99 size_t const cbReq = (cwcSrcReq + 1) * sizeof(OLECHAR);
100 BSTR pBstrNew = (BSTR)nsMemory::Alloc(cbReq);
101 AssertCompile(sizeof(*pBstrNew) == sizeof(OLECHAR));
102 if (pBstrNew)
103 {
104 if (!pwszSrc)
105 memset(pBstrNew, 0, cbReq);
106 else
107 {
108 // Copy the string and make sure it is terminated.
109 memcpy(pBstrNew, pwszSrc, cbReq - sizeof(OLECHAR));
110 pBstrNew[cwcSrcReq] = L'\0';
111 }
112 }
113 return pBstrNew;
114}
115
116/**
117 * Frees the memory associated with the given BSTR.
118 *
119 * @param pBstr The string to free. NULL is ignored.
120 */
121void SysFreeString(BSTR pBstr)
122{
123 if (pBstr)
124 nsMemory::Free(pBstr);
125}
126
127/**
128 * Duplicates @a pwszSrc into an exsting BSTR, adjust its size to make it fit.
129 *
130 * @param ppBstr The existing BSTR buffer pointer.
131 * @param pwszSrc Source string to copy. If NULL, the existing BSTR is freed.
132 * @returns success indicator (TRUE/FALSE)
133 */
134int SysReAllocString(BSTR *ppBstr, const OLECHAR *pwszSrc)
135{
136 if (pwszSrc)
137 return SysReAllocStringLen(ppBstr, pwszSrc, RTUtf16Len((PCRTUTF16)pwszSrc));
138 SysFreeString(*ppBstr);
139 *ppBstr = NULL;
140 return 1;
141}
142
143/**
144 * Duplicates @a pwszSrc into an exsting BSTR / resizing an existing BSTR buffer
145 * into the given size (@a cwcSrcReq).
146 *
147 * @param ppBstr The existing BSTR buffer pointer.
148 * @param pwszSrc Source string to copy into the adjusted pbstr, optional.
149 * @param cwcSrcReq Length of the source string / request in UCS2
150 * characters, a zero terminator is always added.
151 * @returns success indicator (TRUE/FALSE)
152 */
153int SysReAllocStringLen(BSTR *ppBstr, const OLECHAR *pwszSrc, unsigned int cwcSrcReq)
154{
155 BSTR pBstrOld = *ppBstr;
156 AssertCompile(sizeof(*pBstrOld) == sizeof(OLECHAR));
157 if (pBstrOld)
158 {
159 if ((BSTR)pwszSrc == pBstrOld)
160 pwszSrc = NULL;
161
162 size_t cbReq = (cwcSrcReq + 1) * sizeof(OLECHAR);
163 BSTR pBstrNew = (BSTR)nsMemory::Realloc(pBstrOld, cbReq);
164 if (pBstrNew)
165 {
166 if (pwszSrc)
167 memcpy(pBstrNew, pwszSrc, cbReq - sizeof(OLECHAR));
168 pBstrNew[cwcSrcReq] = L'\0';
169 *ppBstr = pBstrNew;
170 return 1;
171 }
172 }
173 else
174 {
175 // allocate a new string
176 *ppBstr = SysAllocStringLen(pwszSrc, cwcSrcReq);
177 if (*ppBstr)
178 return 1;
179 }
180 return 0;
181}
182
183/**
184 * Returns the string length in bytes without the terminator.
185 *
186 * @param pBstr The BSTR to get the byte length of.
187 * @returns String length in bytes.
188 */
189unsigned int SysStringByteLen(BSTR pBstr)
190{
191 AssertCompile(sizeof(OLECHAR) == sizeof(*pBstr));
192 return RTUtf16Len(pBstr) * sizeof(OLECHAR);
193}
194
195/**
196 * Returns the string length in OLECHARs without the terminator.
197 *
198 * @param pBstr The BSTR to get the length of.
199 * @returns String length in OLECHARs.
200 */
201unsigned int SysStringLen(BSTR pBstr)
202{
203 return RTUtf16Len(pBstr);
204}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette