Changeset 80790 in vbox for trunk/src/VBox/Main/glue
- Timestamp:
- Sep 15, 2019 11:12:24 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/glue/xpcom/helpers.cpp
r76553 r80790 17 17 18 18 #include "VBox/com/defs.h" 19 20 19 #include <nsMemory.h> 21 20 #include <iprt/assertcompile.h> 22 21 #include <iprt/utf16.h> 23 22 … … 34 33 35 34 /** 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 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 39 */ 40 BSTR SysAllocString(const OLECHAR * sz)40 BSTR SysAllocString(const OLECHAR *pwszSrc) 41 41 { 42 if (!sz) 43 { 44 return NULL; 45 } 46 return SysAllocStringLen(sz, SysStringLen((BSTR)sz)); 42 AssertCompile(sizeof(*pwszSrc) == sizeof(PRUnichar)); 43 if (pwszSrc) 44 return SysAllocStringLen(pwszSrc, RTUtf16Len((PCRTUTF16)pwszSrc)); 45 return NULL; 47 46 } 48 47 49 48 /** 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 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. 55 57 */ 56 BSTR SysAllocStringByteLen(char *psz, unsigned int len)58 BSTR SysAllocStringByteLen(char const *pszSrc, unsigned int cbSrcReq) 57 59 { 58 unsigned int *newBuffer; 59 char *newString; 60 61 newBuffer = (unsigned int*)nsMemory::Alloc(len + sizeof(OLECHAR)); 62 if (!newBuffer) 60 BSTR pBstrNew = (BSTR)nsMemory::Alloc(RT_ALIGN_Z(cbSrcReq + sizeof(OLECHAR), sizeof(OLECHAR))); 61 AssertCompile(sizeof(*pBstrNew) == sizeof(OLECHAR)); 62 if (pBstrNew) 63 63 { 64 return NULL; 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 } 65 74 } 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 return pBstrNew; 75 76 } 76 77 77 78 /** 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 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. 82 86 */ 83 BSTR SysAllocStringLen(const OLECHAR *p ch, unsigned int cch)87 BSTR SysAllocStringLen(const OLECHAR *pwszSrc, unsigned int cwcSrcReq) 84 88 { 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) 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 93 { 94 return NULL; 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 } 95 102 } 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; 103 return pBstrNew; 110 104 } 111 105 112 106 /** 113 * Frees the memory associated with the BSTR given 114 * @param bstr source string to free 107 * Frees the memory associated with the given BSTR. 108 * 109 * @param pBstr The string to free. NULL is ignored. 115 110 */ 116 void SysFreeString(BSTR bstr)111 void SysFreeString(BSTR pBstr) 117 112 { 118 if (bstr) 119 { 120 nsMemory::Free(bstr); 121 } 113 if (pBstr) 114 nsMemory::Free(pBstr); 122 115 } 123 116 124 117 /** 125 * Reallocates a string by freeing the old string and copying126 * a new string into a new buffer.127 * @param pbstr old string to free128 * @param psz source string to copy into the new string129 * @returns success indicator 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) 130 123 */ 131 int SysReAllocString(BSTR *p bstr, const OLECHAR *psz)124 int SysReAllocString(BSTR *ppBstr, const OLECHAR *pwszSrc) 132 125 { 133 if (!pbstr) 134 { 135 return 0; 136 } 137 SysFreeString(*pbstr); 138 *pbstr = SysAllocString(psz); 126 if (pwszSrc) 127 return SysReAllocStringLen(ppBstr, pwszSrc, RTUtf16Len((PCRTUTF16)pwszSrc)); 128 SysFreeString(*ppBstr); 129 *ppBstr = NULL; 139 130 return 1; 140 131 } 141 132 142 #if 0143 /* Does not work -- we ignore newBuffer! */144 133 /** 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 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) 150 142 */ 151 int SysReAllocStringLen(BSTR *p bstr, const OLECHAR *psz, unsigned int cch)143 int SysReAllocStringLen(BSTR *ppBstr, const OLECHAR *pwszSrc, unsigned int cwcSrcReq) 152 144 { 153 if (SysStringLen(*pbstr) > 0) 145 BSTR pBstrOld = *ppBstr; 146 AssertCompile(sizeof(*pBstrOld) == sizeof(OLECHAR)); 147 if (pBstrOld) 154 148 { 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 (p sz)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) 161 155 { 162 memcpy(*pbstr, psz, newByteLen); 163 *pbstr[cch] = 0; 156 if (pwszSrc) 157 memcpy(pBstrNew, pwszSrc, cbReq - sizeof(OLECHAR)); 158 pBstrNew[cwcSrcReq] = L'\0'; 159 *ppBstr = pBstrNew; 160 return 1; 164 161 } 165 } else 162 } 163 else 166 164 { 167 165 // allocate a new string 168 *pbstr = SysAllocStringLen(psz, cch); 166 *ppBstr = SysAllocStringLen(pwszSrc, cwcSrcReq); 167 if (*ppBstr) 168 return 1; 169 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); 170 return 0; 182 171 } 183 172 184 173 /** 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) 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 */ 179 unsigned int SysStringByteLen(BSTR pBstr) 190 180 { 191 return RTUtf16Len(bstr); 181 AssertCompile(sizeof(OLECHAR) == sizeof(*pBstr)); 182 return RTUtf16Len(pBstr) * sizeof(OLECHAR); 192 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 */ 191 unsigned int SysStringLen(BSTR pBstr) 192 { 193 return RTUtf16Len(pBstr); 194 }
Note:
See TracChangeset
for help on using the changeset viewer.