VirtualBox

Changeset 84210 in vbox for trunk/src


Ignore:
Timestamp:
May 8, 2020 1:05:56 PM (5 years ago)
Author:
vboxsync
Message:

IPRT/Base64: Allow the end-of-line style to be specified via the fFlags parameter when encoding. bugref:9699

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/base64.cpp

    r82968 r84210  
    8383static const char       g_szValToChar[64+1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    8484
     85/** The end-of-line lengths (indexed by style flag value). */
     86static const size_t     g_acchEolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1] =
     87{
     88    /*[RTBASE64_FLAGS_EOL_NATIVE    ]:*/ RTBASE64_EOL_SIZE,
     89    /*[RTBASE64_FLAGS_NO_LINE_BREAKS]:*/ 0,
     90    /*[RTBASE64_FLAGS_EOL_LF        ]:*/ 1,
     91    /*[RTBASE64_FLAGS_EOL_CRLF      ]:*/ 2
     92};
     93
     94/** The end-of-line characters (zero, one or two). */
     95static const char       g_aachEolStyles[RTBASE64_FLAGS_EOL_STYLE_MASK + 1][2] =
     96{
     97    /*[RTBASE64_FLAGS_EOL_NATIVE    ]:*/ { RTBASE64_EOL_SIZE == 1 ? '\n' : '\n', RTBASE64_EOL_SIZE == 1 ? '\n' : '\0', },
     98    /*[RTBASE64_FLAGS_NO_LINE_BREAKS]:*/ { '\0', '\0' },
     99    /*[RTBASE64_FLAGS_EOL_LF        ]:*/ { '\n', '\0' },
     100    /*[RTBASE64_FLAGS_EOL_CRLF      ]:*/ { '\r', '\n' },
     101};
     102
     103
    85104
    86105#ifdef RT_STRICT
     
    421440RTDECL(size_t) RTBase64EncodedLengthEx(size_t cbData, uint32_t fFlags)
    422441{
     442    size_t const cchEol = g_acchEolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK];
     443
    423444    if (cbData * 8 / 8 != cbData)
    424445    {
     
    428449            cch += 8;
    429450        cch /= 6;
    430 
    431         if ((fFlags & RTBASE64_FLAGS_NO_LINE_BREAKS) == 0) /* add EOLs? */
    432             cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
     451        cch += ((cch - 1) / RTBASE64_LINE_LEN) * cchEol;
    433452        return cch;
    434453    }
     
    438457        cch += 8;
    439458    cch /= 6;
    440 
    441     if ((fFlags & RTBASE64_FLAGS_NO_LINE_BREAKS) == 0) /* add EOLs? */
    442         cch += ((cch - 1) / RTBASE64_LINE_LEN) * RTBASE64_EOL_SIZE;
     459    cch += ((cch - 1) / RTBASE64_LINE_LEN) * cchEol;
    443460    return cch;
    444461}
     
    486503                             char *pszBuf, size_t cbBuf, size_t *pcchActual)
    487504{
     505    /* Expand the EOL style flags: */
     506    size_t const    cchEol = g_acchEolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK];
     507    char const      chEol0 = g_aachEolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][0];
     508    char const      chEol1 = g_aachEolStyles[fFlags & RTBASE64_FLAGS_EOL_STYLE_MASK][1];
     509
    488510    /*
    489511     * Process whole "trios" of input data.
     
    515537        pbSrc  += 3;
    516538
    517         if ((fFlags & RTBASE64_FLAGS_NO_LINE_BREAKS) == 0) /* add EOLs? */
     539        if (cchEol > 0)
    518540        {
    519541            /* deal out end-of-line */
    520542            if (cbBuf == cbLineFeed && cbData)
    521543            {
    522                 if (cbBuf < RTBASE64_EOL_SIZE + 1)
     544                if (cbBuf < cchEol + 1)
    523545                    return VERR_BUFFER_OVERFLOW;
    524                 cbBuf -= RTBASE64_EOL_SIZE;
    525                 if (RTBASE64_EOL_SIZE == 2)
    526                     *pchDst++ = '\r';
    527                 *pchDst++ = '\n';
     546                cbBuf -= cchEol;
     547                *pchDst++ = chEol0;
     548                if (chEol1)
     549                    *pchDst++ = chEol1;
    528550                cbLineFeed = cbBuf - RTBASE64_LINE_LEN;
    529551            }
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