VirtualBox

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

Last change on this file since 69500 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

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