summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/glue/xpcom/helpers.cpp
blob: bdfa35913bf139fbbf0e74b0ef6448f55f9d6e58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* $Id: helpers.cpp $ */
/** @file
 * COM helper functions for XPCOM
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include "VBox/com/defs.h"
#include <nsMemory.h>
#include <iprt/assertcompile.h>
#include <iprt/utf16.h>


//
// OLE Automation string APIs
//

// Note: on Windows, every BSTR stores its length in the
// byte just before the pointer you get. If we do it like this,
// the caller cannot just use nsMemory::Free() on our strings.
// Therefore we'll try to implement it differently and hope that
// we don't run into problems.

/**
 * Copies a string into a new memory block including the terminating UCS2 NULL.
 *
 * @param   pwsz    Source string to duplicate.
 * @returns New BSTR string buffer.
 */
BSTR SysAllocString(const OLECHAR *pwszSrc)
{
    AssertCompile(sizeof(*pwszSrc) == sizeof(PRUnichar));
    if (pwszSrc)
        return SysAllocStringLen(pwszSrc, RTUtf16Len((PCRTUTF16)pwszSrc));
    return NULL;
}

/**
 * Duplicates an ANSI string into a BSTR / allocates a BSTR with a size given in
 * bytes.
 *
 * No conversion is done.
 *
 * @param   pszSrc      Source string to copy, optional.
 * @param   cbSrcReq    Length of the source string / memory request in bytes.
 * @returns new BSTR string buffer, NULL on failure.
 */
BSTR SysAllocStringByteLen(char const *pszSrc, unsigned int cbSrcReq)
{
    BSTR pBstrNew = (BSTR)nsMemory::Alloc(RT_ALIGN_Z(cbSrcReq + sizeof(OLECHAR), sizeof(OLECHAR)));
    AssertCompile(sizeof(*pBstrNew) == sizeof(OLECHAR));
    if (pBstrNew)
    {
        if (!pszSrc)
            memset(pBstrNew, 0, cbSrcReq + sizeof(OLECHAR));
        else
        {
            // Copy the string and make sure it is terminated.
            memcpy(pBstrNew, pszSrc, cbSrcReq);
            char *pchTerminator = (char *)pBstrNew;
            pchTerminator[cbSrcReq] = '\0';
            pchTerminator[cbSrcReq + 1] = '\0';
        }
    }
    return pBstrNew;
}

/**
 * Duplicates a UTF-16 string into a BSTR / Allocates a BSTR with a size given
 * in UTF-16 characters.
 *
 * @param   pwszSrc     Pointer to the source string, optional.
 * @param   cwcSrcReq   Length of the source string / memory request in UTF-16
 *                      characters.
 * @returns new BSTR string buffer, NULL on failure.
 */
BSTR SysAllocStringLen(const OLECHAR *pwszSrc, unsigned int cwcSrcReq)
{
    size_t const cbReq = (cwcSrcReq + 1) * sizeof(OLECHAR);
    BSTR pBstrNew = (BSTR)nsMemory::Alloc(cbReq);
    AssertCompile(sizeof(*pBstrNew) == sizeof(OLECHAR));
    if (pBstrNew)
    {
        if (!pwszSrc)
            memset(pBstrNew, 0, cbReq);
        else
        {
            // Copy the string and make sure it is terminated.
            memcpy(pBstrNew, pwszSrc, cbReq - sizeof(OLECHAR));
            pBstrNew[cwcSrcReq] = L'\0';
        }
    }
    return pBstrNew;
}

/**
 * Frees the memory associated with the given BSTR.
 *
 * @param   pBstr  The string to free.  NULL is ignored.
 */
void SysFreeString(BSTR pBstr)
{
    if (pBstr)
        nsMemory::Free(pBstr);
}

/**
 * Duplicates @a pwszSrc into an exsting BSTR, adjust its size to make it fit.
 *
 * @param   ppBstr  The existing BSTR buffer pointer.
 * @param   pwszSrc Source string to copy.  If NULL, the existing BSTR is freed.
 * @returns success indicator (TRUE/FALSE)
 */
int SysReAllocString(BSTR *ppBstr, const OLECHAR *pwszSrc)
{
    if (pwszSrc)
        return SysReAllocStringLen(ppBstr, pwszSrc, RTUtf16Len((PCRTUTF16)pwszSrc));
    SysFreeString(*ppBstr);
    *ppBstr = NULL;
    return 1;
}

/**
 * Duplicates @a pwszSrc into an exsting BSTR / resizing an existing BSTR buffer
 * into the given size (@a cwcSrcReq).
 *
 * @param   ppBstr      The existing BSTR buffer pointer.
 * @param   pwszSrc     Source string to copy into the adjusted pbstr, optional.
 * @param   cwcSrcReq   Length of the source string / request in UCS2
 *                      characters, a zero terminator is always added.
 * @returns success indicator (TRUE/FALSE)
 */
int SysReAllocStringLen(BSTR *ppBstr, const OLECHAR *pwszSrc, unsigned int cwcSrcReq)
{
    BSTR pBstrOld = *ppBstr;
    AssertCompile(sizeof(*pBstrOld) == sizeof(OLECHAR));
    if (pBstrOld)
    {
        if ((BSTR)pwszSrc == pBstrOld)
            pwszSrc = NULL;

        size_t cbReq = (cwcSrcReq + 1) * sizeof(OLECHAR);
        BSTR pBstrNew = (BSTR)nsMemory::Realloc(pBstrOld, cbReq);
        if (pBstrNew)
        {
            if (pwszSrc)
                memcpy(pBstrNew, pwszSrc, cbReq - sizeof(OLECHAR));
            pBstrNew[cwcSrcReq] = L'\0';
            *ppBstr = pBstrNew;
            return 1;
        }
    }
    else
    {
        // allocate a new string
        *ppBstr = SysAllocStringLen(pwszSrc, cwcSrcReq);
        if (*ppBstr)
            return 1;
    }
    return 0;
}

/**
 * Returns the string length in bytes without the terminator.
 *
 * @param   pBstr   The BSTR to get the byte length of.
 * @returns String length in bytes.
 */
unsigned int SysStringByteLen(BSTR pBstr)
{
    AssertCompile(sizeof(OLECHAR) == sizeof(*pBstr));
    return RTUtf16Len(pBstr) * sizeof(OLECHAR);
}

/**
 * Returns the string length in OLECHARs without the terminator.
 *
 * @param   pBstr   The BSTR to get the length of.
 * @returns String length in OLECHARs.
 */
unsigned int SysStringLen(BSTR pBstr)
{
    return RTUtf16Len(pBstr);
}