VirtualBox

Changeset 80790 in vbox for trunk/src/VBox/Main/glue


Ignore:
Timestamp:
Sep 15, 2019 11:12:24 AM (5 years ago)
Author:
vboxsync
Message:

Main/glue/xpcom/helpers.cpp: Fixed and re-enabled the SysReAllocStringLen implementation. Cleaned up the rest.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/glue/xpcom/helpers.cpp

    r76553 r80790  
    1717
    1818#include "VBox/com/defs.h"
    19 
    2019#include <nsMemory.h>
    21 
     20#include <iprt/assertcompile.h>
    2221#include <iprt/utf16.h>
    2322
     
    3433
    3534/**
    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.
    3939 */
    40 BSTR SysAllocString(const OLECHAR* sz)
     40BSTR SysAllocString(const OLECHAR *pwszSrc)
    4141{
    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;
    4746}
    4847
    4948/**
    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.
    5557 */
    56 BSTR SysAllocStringByteLen(char *psz, unsigned int len)
     58BSTR SysAllocStringByteLen(char const *pszSrc, unsigned int cbSrcReq)
    5759{
    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)
    6363    {
    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        }
    6574    }
    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;
    7576}
    7677
    7778/**
    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.
    8286 */
    83 BSTR SysAllocStringLen(const OLECHAR *pch, unsigned int cch)
     87BSTR SysAllocStringLen(const OLECHAR *pwszSrc, unsigned int cwcSrcReq)
    8488{
    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)
    9393    {
    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        }
    95102    }
    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;
    110104}
    111105
    112106/**
    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.
    115110 */
    116 void SysFreeString(BSTR bstr)
     111void SysFreeString(BSTR pBstr)
    117112{
    118     if (bstr)
    119     {
    120         nsMemory::Free(bstr);
    121     }
     113    if (pBstr)
     114        nsMemory::Free(pBstr);
    122115}
    123116
    124117/**
    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
     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)
    130123 */
    131 int SysReAllocString(BSTR *pbstr, const OLECHAR *psz)
     124int SysReAllocString(BSTR *ppBstr, const OLECHAR *pwszSrc)
    132125{
    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;
    139130    return 1;
    140131}
    141132
    142 #if 0
    143 /* Does not work -- we ignore newBuffer! */
    144133/**
    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)
    150142 */
    151 int SysReAllocStringLen(BSTR *pbstr, const OLECHAR *psz, unsigned int cch)
     143int SysReAllocStringLen(BSTR *ppBstr, const OLECHAR *pwszSrc, unsigned int cwcSrcReq)
    152144{
    153     if (SysStringLen(*pbstr) > 0)
     145    BSTR pBstrOld = *ppBstr;
     146    AssertCompile(sizeof(*pBstrOld) == sizeof(OLECHAR));
     147    if (pBstrOld)
    154148    {
    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)
     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)
    161155        {
    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;
    164161        }
    165     } else
     162    }
     163    else
    166164    {
    167165        // allocate a new string
    168         *pbstr = SysAllocStringLen(psz, cch);
     166        *ppBstr = SysAllocStringLen(pwszSrc, cwcSrcReq);
     167        if (*ppBstr)
     168            return 1;
    169169    }
    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;
    182171}
    183172
    184173/**
    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 */
     179unsigned int SysStringByteLen(BSTR pBstr)
    190180{
    191     return RTUtf16Len(bstr);
     181    AssertCompile(sizeof(OLECHAR) == sizeof(*pBstr));
     182    return RTUtf16Len(pBstr) * sizeof(OLECHAR);
    192183}
     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 */
     191unsigned int SysStringLen(BSTR pBstr)
     192{
     193    return RTUtf16Len(pBstr);
     194}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette