1 | /* $Id: helpers.cpp 76408 2018-12-23 16:38:11Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * COM helper functions for XPCOM
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 |
|
---|
20 | #include <nsMemory.h>
|
---|
21 |
|
---|
22 | #include <iprt/utf16.h>
|
---|
23 |
|
---|
24 |
|
---|
25 | //
|
---|
26 | // OLE Automation string APIs
|
---|
27 | //
|
---|
28 |
|
---|
29 | // Note: on Windows, every BSTR stores its length in the
|
---|
30 | // byte just before the pointer you get. If we do it like this,
|
---|
31 | // the caller cannot just use nsMemory::Free() on our strings.
|
---|
32 | // Therefore we'll try to implement it differently and hope that
|
---|
33 | // we don't run into problems.
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Copies a string into a new memory block including the terminating UCS2 NULL
|
---|
37 | * @param sz source string to copy
|
---|
38 | * @returns BSTR new string buffer
|
---|
39 | */
|
---|
40 | BSTR SysAllocString(const OLECHAR* sz)
|
---|
41 | {
|
---|
42 | if (!sz)
|
---|
43 | {
|
---|
44 | return NULL;
|
---|
45 | }
|
---|
46 | return SysAllocStringLen(sz, SysStringLen((BSTR)sz));
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Copies len OLECHARs of a string into a new memory block and
|
---|
51 | * adds a terminating UCS2 NULL
|
---|
52 | * @param psz source string to copy
|
---|
53 | * @param len length of the source string in bytes
|
---|
54 | * @returns BSTR new string buffer
|
---|
55 | */
|
---|
56 | BSTR SysAllocStringByteLen(char *psz, unsigned int len)
|
---|
57 | {
|
---|
58 | unsigned int *newBuffer;
|
---|
59 | char *newString;
|
---|
60 |
|
---|
61 | newBuffer = (unsigned int*)nsMemory::Alloc(len + sizeof(OLECHAR));
|
---|
62 | if (!newBuffer)
|
---|
63 | {
|
---|
64 | return NULL;
|
---|
65 | }
|
---|
66 | if (psz)
|
---|
67 | {
|
---|
68 | memcpy(newBuffer, psz, len);
|
---|
69 | }
|
---|
70 | // make sure there is a trailing UCS2 NULL
|
---|
71 | newString = (char*)newBuffer;
|
---|
72 | newString[len] = '\0';
|
---|
73 | newString[len + 1] = '\0';
|
---|
74 | return (BSTR)newString;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Create a BSTR from the OLECHAR string with a given length in UCS2 characters
|
---|
79 | * @param pch pointer to the source string
|
---|
80 | * @param cch length of the source string in UCS2 characters
|
---|
81 | * @returns BSTR new string buffer
|
---|
82 | */
|
---|
83 | BSTR SysAllocStringLen(const OLECHAR *pch, unsigned int cch)
|
---|
84 | {
|
---|
85 | unsigned int bufferSize;
|
---|
86 | unsigned int *newBuffer;
|
---|
87 | OLECHAR *newString;
|
---|
88 |
|
---|
89 | // add the trailing UCS2 NULL
|
---|
90 | bufferSize = cch * sizeof(OLECHAR);
|
---|
91 | newBuffer = (unsigned int*)nsMemory::Alloc(bufferSize + sizeof(OLECHAR));
|
---|
92 | if (!newBuffer)
|
---|
93 | {
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 | // copy the string, a NULL input string is allowed
|
---|
97 | if (pch)
|
---|
98 | {
|
---|
99 | memcpy(newBuffer, pch, bufferSize);
|
---|
100 |
|
---|
101 | } else
|
---|
102 | {
|
---|
103 | memset(newBuffer, 0, bufferSize);
|
---|
104 | }
|
---|
105 | // make sure there is a trailing UCS2 NULL
|
---|
106 | newString = (OLECHAR*)newBuffer;
|
---|
107 | newString[cch] = L'\0';
|
---|
108 |
|
---|
109 | return (BSTR)newString;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Frees the memory associated with the BSTR given
|
---|
114 | * @param bstr source string to free
|
---|
115 | */
|
---|
116 | void SysFreeString(BSTR bstr)
|
---|
117 | {
|
---|
118 | if (bstr)
|
---|
119 | {
|
---|
120 | nsMemory::Free(bstr);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Reallocates a string by freeing the old string and copying
|
---|
126 | * a new string into a new buffer.
|
---|
127 | * @param pbstr old string to free
|
---|
128 | * @param psz source string to copy into the new string
|
---|
129 | * @returns success indicator
|
---|
130 | */
|
---|
131 | int SysReAllocString(BSTR *pbstr, const OLECHAR *psz)
|
---|
132 | {
|
---|
133 | if (!pbstr)
|
---|
134 | {
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 | SysFreeString(*pbstr);
|
---|
138 | *pbstr = SysAllocString(psz);
|
---|
139 | return 1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | #if 0
|
---|
143 | /* Does not work -- we ignore newBuffer! */
|
---|
144 | /**
|
---|
145 | * Changes the length of a previous created BSTR
|
---|
146 | * @param pbstr string to change the length of
|
---|
147 | * @param psz source string to copy into the adjusted pbstr
|
---|
148 | * @param cch length of the source string in UCS2 characters
|
---|
149 | * @returns int success indicator
|
---|
150 | */
|
---|
151 | int SysReAllocStringLen(BSTR *pbstr, const OLECHAR *psz, unsigned int cch)
|
---|
152 | {
|
---|
153 | if (SysStringLen(*pbstr) > 0)
|
---|
154 | {
|
---|
155 | unsigned int newByteLen;
|
---|
156 | unsigned int *newBuffer;
|
---|
157 | newByteLen = cch * sizeof(OLECHAR);
|
---|
158 | newBuffer = (unsigned int*)nsMemory::Realloc((void*)*pbstr,
|
---|
159 | newByteLen + sizeof(OLECHAR));
|
---|
160 | if (psz)
|
---|
161 | {
|
---|
162 | memcpy(*pbstr, psz, newByteLen);
|
---|
163 | *pbstr[cch] = 0;
|
---|
164 | }
|
---|
165 | } else
|
---|
166 | {
|
---|
167 | // allocate a new string
|
---|
168 | *pbstr = SysAllocStringLen(psz, cch);
|
---|
169 | }
|
---|
170 | return 1;
|
---|
171 | }
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Returns the string length in bytes without the terminator
|
---|
176 | * @returns unsigned int length in bytes
|
---|
177 | * @param bstr source string
|
---|
178 | */
|
---|
179 | unsigned int SysStringByteLen(BSTR bstr)
|
---|
180 | {
|
---|
181 | return RTUtf16Len(bstr) * sizeof(OLECHAR);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Returns the string length in OLECHARs without the terminator
|
---|
186 | * @returns unsigned int length in OLECHARs
|
---|
187 | * @param bstr source string
|
---|
188 | */
|
---|
189 | unsigned int SysStringLen(BSTR bstr)
|
---|
190 | {
|
---|
191 | return RTUtf16Len(bstr);
|
---|
192 | }
|
---|