VirtualBox

Changeset 73587 in vbox


Ignore:
Timestamp:
Aug 9, 2018 1:37:52 PM (7 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
124226
Message:

iptr/base64: introduce RTBase64EncodedLengthEx() and RTBase64EncodeEx(),
and RTBASE64_FLAGS_NO_LINE_BREAKS flag for them.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/base64.h

    r69105 r73587  
    4343# define RTBASE64_EOL_SIZE      (sizeof("\n")   - 1)
    4444#endif
     45
     46
     47/** @name Flags for RTBase64EncodeEx() and RTBase64EncodedLengthEx().
     48 * @{ */
     49/** Insert line breaks into encoded string.
     50 * The size of the end-of-line marker is that that of the host platform.
     51 */
     52#define RTBASE64_FLAGS_NO_LINE_BREAKS RT_BIT_32(0)
     53/** @} */
     54
    4555
    4656/**
     
    121131                             size_t *pcbActual, char **ppszEnd);
    122132
     133
    123134/**
    124135 * Calculates the length of the Base64 encoding of a given number of bytes of
    125  * data.
    126  *
    127  * This will assume line breaks every 64 chars. A RTBase64EncodedLengthEx
    128  * function can be added if closer control over the output is found to be
    129  * required.
     136 * data produced by RTBase64Encode().
    130137 *
    131138 * @returns The Base64 string length.
     
    135142
    136143/**
     144 * Calculates the length of the Base64 encoding of a given number of bytes of
     145 * data produced by RTBase64EncodeEx() with the same @a fFlags.
     146 *
     147 * @returns The Base64 string length.
     148 * @param   cbData      The number of bytes to encode.
     149 * @param   fFlags      Flags, any combination of the RTBASE64_FLAGS \#defines.
     150 */
     151RTDECL(size_t) RTBase64EncodedLengthEx(size_t cbData, uint32_t fFlags);
     152
     153/**
    137154 * Encodes the specifed data into a Base64 string, the caller supplies the
    138155 * output buffer.
    139156 *
    140  * This will make the same assumptions about line breaks and EOL size as
    141  * RTBase64EncodedLength() does. A RTBase64EncodeEx function can be added if
    142  * more strict control over the output formatting is found necessary.
     157 * This is equivalent to calling RTBase64EncodeEx() with no flags.
    143158 *
    144159 * @returns IRPT status code.
     
    154169RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual);
    155170
     171/**
     172 * Encodes the specifed data into a Base64 string, the caller supplies the
     173 * output buffer.
     174 *
     175 * @returns IRPT status code.
     176 * @retval  VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
     177 *          may contain an invalid Base64 string.
     178 *
     179 * @param   pvData      The data to encode.
     180 * @param   cbData      The number of bytes to encode.
     181 * @param   pszBuf      Where to put the Base64 string.
     182 * @param   cbBuf       The size of the output buffer, including the terminator.
     183 * @param   pcchActual  The actual number of characters returned.
     184 */
     185RTDECL(int) RTBase64EncodeEx(const void *pvData, size_t cbData, uint32_t fFlags,
     186                             char *pszBuf, size_t cbBuf, size_t *pcchActual);
     187
    156188/** @}  */
    157189
  • trunk/include/iprt/mangling.h

    r73494 r73587  
    575575# define RTBase64DecodedSizeEx                          RT_MANGLER(RTBase64DecodedSizeEx)
    576576# define RTBase64Encode                                 RT_MANGLER(RTBase64Encode)
     577# define RTBase64EncodeEx                               RT_MANGLER(RTBase64EncodeEx)
    577578# define RTBase64EncodedLength                          RT_MANGLER(RTBase64EncodedLength)
     579# define RTBase64EncodedLengthEx                        RT_MANGLER(RTBase64EncodedLengthEx)
    578580# define RTBldCfgCompiler                               RT_MANGLER(RTBldCfgCompiler)
    579581# define RTBldCfgRevision                               RT_MANGLER(RTBldCfgRevision)
  • trunk/src/VBox/Runtime/common/string/base64.cpp

    r69111 r73587  
    400400/**
    401401 * Calculates the length of the Base64 encoding of a given number of bytes of
    402  * data.
    403  *
    404  * This will assume line breaks every 64 chars. A RTBase64EncodedLengthEx
    405  * function can be added if closer control over the output is found to be
    406  * required.
     402 * data produced by RTBase64Encode().
    407403 *
    408404 * @returns The Base64 string length.
     
    410406 */
    411407RTDECL(size_t) RTBase64EncodedLength(size_t cbData)
     408{
     409    return RTBase64EncodedLengthEx(cbData, 0);
     410}
     411RT_EXPORT_SYMBOL(RTBase64EncodedLength);
     412
     413
     414/**
     415 * Calculates the length of the Base64 encoding of a given number of bytes of
     416 * data produced by RTBase64EncodeEx() with the same @a fFlags.
     417 *
     418 * @returns The Base64 string length.
     419 * @param   cbData      The number of bytes to encode.
     420 * @param   fFlags      Flags, any combination of the RTBASE64_FLAGS \#defines.
     421 */
     422RTDECL(size_t) RTBase64EncodedLengthEx(size_t cbData, uint32_t fFlags)
    412423{
    413424    if (cbData * 8 / 8 != cbData)
     
    419430        cch /= 6;
    420431
    421         cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
     432        if ((fFlags & RTBASE64_FLAGS_NO_LINE_BREAKS) == 0) /* add EOLs? */
     433            cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
    422434        return cch;
    423435    }
     
    428440    cch /= 6;
    429441
    430     cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
     442    if ((fFlags & RTBASE64_FLAGS_NO_LINE_BREAKS) == 0) /* add EOLs? */
     443        cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
    431444    return cch;
    432445}
    433 RT_EXPORT_SYMBOL(RTBase64EncodedLength);
     446RT_EXPORT_SYMBOL(RTBase64EncodedLengthEx);
    434447
    435448
     
    438451 * output buffer.
    439452 *
    440  * This will make the same assumptions about line breaks and EOL size as
    441  * RTBase64EncodedLength() does. A RTBase64EncodeEx function can be added if
    442  * more strict control over the output formatting is found necessary.
     453 * This is equivalent to calling RTBase64EncodeEx() with no flags.
    443454 *
    444455 * @returns IRPT status code.
     
    453464 */
    454465RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual)
     466{
     467    return RTBase64EncodeEx(pvData, cbData, 0, pszBuf, cbBuf, pcchActual);
     468}
     469RT_EXPORT_SYMBOL(RTBase64Encode);
     470
     471
     472/**
     473 * Encodes the specifed data into a Base64 string, the caller supplies the
     474 * output buffer.
     475 *
     476 * @returns IRPT status code.
     477 * @retval  VERR_BUFFER_OVERFLOW if the output buffer is too small. The buffer
     478 *          may contain an invalid Base64 string.
     479 *
     480 * @param   pvData      The data to encode.
     481 * @param   cbData      The number of bytes to encode.
     482 * @param   pszBuf      Where to put the Base64 string.
     483 * @param   cbBuf       The size of the output buffer, including the terminator.
     484 * @param   pcchActual  The actual number of characters returned.
     485 */
     486RTDECL(int) RTBase64EncodeEx(const void *pvData, size_t cbData, uint32_t fFlags,
     487                             char *pszBuf, size_t cbBuf, size_t *pcchActual)
    455488{
    456489    /*
     
    483516        pbSrc  += 3;
    484517
    485         /* deal out linefeeds */
    486         if (cbBuf == cbLineFeed && cbData)
    487         {
    488             if (cbBuf < RTBASE64_EOL_SIZE + 1)
    489                 return VERR_BUFFER_OVERFLOW;
    490             cbBuf -= RTBASE64_EOL_SIZE;
    491             if (RTBASE64_EOL_SIZE == 2)
    492                 *pchDst++ = '\r';
    493             *pchDst++ = '\n';
    494             cbLineFeed = cbBuf - RTBASE64_LINE_LEN;
     518        if ((fFlags & RTBASE64_FLAGS_NO_LINE_BREAKS) == 0) /* add EOLs? */
     519        {
     520            /* deal out end-of-line */
     521            if (cbBuf == cbLineFeed && cbData)
     522            {
     523                if (cbBuf < RTBASE64_EOL_SIZE + 1)
     524                    return VERR_BUFFER_OVERFLOW;
     525                cbBuf -= RTBASE64_EOL_SIZE;
     526                if (RTBASE64_EOL_SIZE == 2)
     527                    *pchDst++ = '\r';
     528                *pchDst++ = '\n';
     529                cbLineFeed = cbBuf - RTBASE64_LINE_LEN;
     530            }
    495531        }
    496532    }
     
    530566    return VINF_SUCCESS;
    531567}
    532 RT_EXPORT_SYMBOL(RTBase64Encode);
    533 
     568RT_EXPORT_SYMBOL(RTBase64EncodeEx);
Note: See TracChangeset for help on using the changeset viewer.

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