VirtualBox

Ignore:
Timestamp:
May 6, 2020 3:31:33 PM (5 years ago)
Author:
vboxsync
Message:

IPRT: PEM writer functions. bugref:9699

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/vfs/vfsprintf.cpp

    r84152 r84163  
    3535
    3636
    37 /*********************************************************************************************************************************
    38 *   Structures and Typedefs                                                                                                      *
    39 *********************************************************************************************************************************/
    40 typedef struct PRINTFBUF
    41 {
    42     RTVFSIOSTREAM   hVfsIos;
    43     int             rc;
    44     size_t          offBuf;
    45     char            szBuf[256];
    46 } PRINTFBUF;
    47 
    48 
    4937/** Writes the buffer to the VFS file. */
    50 static void FlushPrintfBuffer(PRINTFBUF *pBuf)
     38static void FlushPrintfBuffer(PVFSIOSTRMOUTBUF pBuf)
    5139{
    5240    if (pBuf->offBuf)
     
    6149
    6250
    63 /** @callback_method_impl{FNRTSTROUTPUT} */
    64 static DECLCALLBACK(size_t) MyPrintfOutputter(void *pvArg, const char *pachChars, size_t cbChars)
     51/**
     52 * @callback_method_impl{FNRTSTROUTPUT,
     53 *      For use with VFSIOSTRMOUTBUF.}
     54 */
     55RTDECL(size_t) RTVfsIoStrmStrOutputCallback(void *pvArg, const char *pachChars, size_t cbChars)
    6556{
    66     PRINTFBUF *pBuf = (PRINTFBUF *)pvArg;
     57    PVFSIOSTRMOUTBUF pBuf = (PVFSIOSTRMOUTBUF)pvArg;
     58    AssertReturn(pBuf->cbSelf == sizeof(*pBuf), 0);
     59
    6760    if (cbChars != 0)
    6861    {
    69         size_t offSrc = 0;
    70         while  (offSrc < cbChars)
     62        if (cbChars <= sizeof(pBuf->szBuf) * 3 / 2)
    7163        {
    72             size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
    73             if (cbLeft > 0)
     64            /*
     65             * Small piece of output: Buffer it.
     66             */
     67            size_t offSrc = 0;
     68            while  (offSrc < cbChars)
    7469            {
    75                 size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
    76                 memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
    77                 pBuf->offBuf += cbToCopy;
    78                 pBuf->szBuf[pBuf->offBuf] = '\0';
    79                 if (cbLeft > cbToCopy)
    80                     break;
    81                 offSrc += cbToCopy;
     70                size_t cbLeft = sizeof(pBuf->szBuf) - pBuf->offBuf - 1;
     71                if (cbLeft > 0)
     72                {
     73                    size_t cbToCopy = RT_MIN(cbChars - offSrc, cbLeft);
     74                    memcpy(&pBuf->szBuf[pBuf->offBuf], &pachChars[offSrc], cbToCopy);
     75                    pBuf->offBuf += cbToCopy;
     76                    pBuf->szBuf[pBuf->offBuf] = '\0';
     77                    if (cbLeft > cbToCopy)
     78                        break;
     79                    offSrc += cbToCopy;
     80                }
     81                FlushPrintfBuffer(pBuf);
    8282            }
     83        }
     84        else
     85        {
     86            /*
     87             * Large chunk of output: Output it directly.
     88            */
    8389            FlushPrintfBuffer(pBuf);
     90
     91            int rc = RTVfsIoStrmWrite(pBuf->hVfsIos, pachChars, cbChars, true /*fBlocking*/, NULL);
     92            if (RT_FAILURE(rc))
     93                pBuf->rc = rc;
    8494        }
    8595    }
     
    90100
    91101
    92 
    93102RTDECL(ssize_t) RTVfsIoStrmPrintfV(RTVFSIOSTREAM hVfsIos, const char *pszFormat, va_list va)
    94103{
    95     PRINTFBUF Buf;
    96     Buf.hVfsIos  = hVfsIos;
    97     Buf.rc       = VINF_SUCCESS;
    98     Buf.offBuf   = 0;
    99     Buf.szBuf[0] = '\0';
     104    VFSIOSTRMOUTBUF Buf;
     105    VFSIOSTRMOUTBUF_INIT(&Buf, hVfsIos);
    100106
    101     size_t cchRet = RTStrFormatV(MyPrintfOutputter, &Buf, NULL, NULL, pszFormat, va);
     107    size_t cchRet = RTStrFormatV(RTVfsIoStrmStrOutputCallback, &Buf, NULL, NULL, pszFormat, va);
    102108    if (RT_SUCCESS(Buf.rc))
    103109        return cchRet;
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