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
|
/* $Id: helpers.cpp $ */
/** @file
* COM helper functions for XPCOM
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include "VBox/com/defs.h"
#include <nsMemory.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 sz source string to copy
* @returns BSTR new string buffer
*/
BSTR SysAllocString(const OLECHAR* sz)
{
if (!sz)
{
return NULL;
}
return SysAllocStringLen(sz, SysStringLen((BSTR)sz));
}
/**
* Copies len OLECHARs of a string into a new memory block and
* adds a terminating UCS2 NULL
* @param psz source string to copy
* @param len length of the source string in bytes
* @returns BSTR new string buffer
*/
BSTR SysAllocStringByteLen(char *psz, unsigned int len)
{
unsigned int *newBuffer;
char *newString;
newBuffer = (unsigned int*)nsMemory::Alloc(len + sizeof(OLECHAR));
if (!newBuffer)
{
return NULL;
}
if (psz)
{
memcpy(newBuffer, psz, len);
}
// make sure there is a trailing UCS2 NULL
newString = (char*)newBuffer;
newString[len] = '\0';
newString[len + 1] = '\0';
return (BSTR)newString;
}
/**
* Create a BSTR from the OLECHAR string with a given length in UCS2 characters
* @param pch pointer to the source string
* @param cch length of the source string in UCS2 characters
* @returns BSTR new string buffer
*/
BSTR SysAllocStringLen(const OLECHAR *pch, unsigned int cch)
{
unsigned int bufferSize;
unsigned int *newBuffer;
OLECHAR *newString;
// add the trailing UCS2 NULL
bufferSize = cch * sizeof(OLECHAR);
newBuffer = (unsigned int*)nsMemory::Alloc(bufferSize + sizeof(OLECHAR));
if (!newBuffer)
{
return NULL;
}
// copy the string, a NULL input string is allowed
if (pch)
{
memcpy(newBuffer, pch, bufferSize);
} else
{
memset(newBuffer, 0, bufferSize);
}
// make sure there is a trailing UCS2 NULL
newString = (OLECHAR*)newBuffer;
newString[cch] = L'\0';
return (BSTR)newString;
}
/**
* Frees the memory associated with the BSTR given
* @param bstr source string to free
*/
void SysFreeString(BSTR bstr)
{
if (bstr)
{
nsMemory::Free(bstr);
}
}
/**
* Reallocates a string by freeing the old string and copying
* a new string into a new buffer.
* @param pbstr old string to free
* @param psz source string to copy into the new string
* @returns success indicator
*/
int SysReAllocString(BSTR *pbstr, const OLECHAR *psz)
{
if (!pbstr)
{
return 0;
}
SysFreeString(*pbstr);
*pbstr = SysAllocString(psz);
return 1;
}
#if 0
/* Does not work -- we ignore newBuffer! */
/**
* Changes the length of a previous created BSTR
* @param pbstr string to change the length of
* @param psz source string to copy into the adjusted pbstr
* @param cch length of the source string in UCS2 characters
* @returns int success indicator
*/
int SysReAllocStringLen(BSTR *pbstr, const OLECHAR *psz, unsigned int cch)
{
if (SysStringLen(*pbstr) > 0)
{
unsigned int newByteLen;
unsigned int *newBuffer;
newByteLen = cch * sizeof(OLECHAR);
newBuffer = (unsigned int*)nsMemory::Realloc((void*)*pbstr,
newByteLen + sizeof(OLECHAR));
if (psz)
{
memcpy(*pbstr, psz, newByteLen);
*pbstr[cch] = 0;
}
} else
{
// allocate a new string
*pbstr = SysAllocStringLen(psz, cch);
}
return 1;
}
#endif
/**
* Returns the string length in bytes without the terminator
* @returns unsigned int length in bytes
* @param bstr source string
*/
unsigned int SysStringByteLen(BSTR bstr)
{
return RTUtf16Len(bstr) * sizeof(OLECHAR);
}
/**
* Returns the string length in OLECHARs without the terminator
* @returns unsigned int length in OLECHARs
* @param bstr source string
*/
unsigned int SysStringLen(BSTR bstr)
{
return RTUtf16Len(bstr);
}
|