VirtualBox

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

Last change on this file since 93105 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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