VirtualBox

Changeset 59620 in vbox


Ignore:
Timestamp:
Feb 10, 2016 12:47:33 AM (9 years ago)
Author:
vboxsync
Message:

IPRT: Added 'off' parameter to RTVfsIoStrmSgWrite and RTVfsIoStrmSgRead so it's easier to write passthru layers. Added RTVfsIoStrmReadAll, RTVfsIoStrmReadAllFree, RTVfsIoStrmFromBuffer, RTManifestPtIosIsInstanceOf, RTCrX509Certificate_ReadFromBuffer and RTCrDigestUpdateFromVfsFile. Updated the manifest passthru read code to handle ReadAt requests which skips parts and jumps back to re-read stuff on streams/files which are seekable.

Location:
trunk
Files:
1 added
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/crypto/digest.h

    r58338 r59620  
    174174RTDECL(uint32_t) RTCrDigestRelease(RTCRDIGEST hDigest);
    175175RTDECL(int) RTCrDigestUpdate(RTCRDIGEST hDigest, void const *pvData, size_t cbData);
     176RTDECL(int) RTCrDigestUpdateFromVfsFile(RTCRDIGEST hDigest, RTVFSFILE hVfsFile, bool fRewindFile);
    176177RTDECL(int) RTCrDigestFinal(RTCRDIGEST hDigest, void *pvHash, size_t cbHash);
    177178RTDECL(bool) RTCrDigestMatch(RTCRDIGEST hDigest, void const *pvHash, size_t cbHash);
  • trunk/include/iprt/crypto/pkix.h

    r56291 r59620  
    374374                                                  bool fEncrypt, PCRTASN1BITSTRING pKey,PCRTASN1DYNTYPE pParams);
    375375RTDECL(int) RTCrPkixEncryptionCreateByObjId(PRTCRPKIXENCRYPTION phEncryption, PCRTASN1OBJID pObjId, bool fEncrypt,
    376                                            PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
     376                                            PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
    377377
    378378
    379379RTDECL(int) RTCrPkixEncryptionCreate(PRTCRPKIXENCRYPTION phEncryption, PCRTCRPKIXENCRYPTIONDESC pDesc, void *pvOpaque,
    380                                     bool fEncrypt, PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
     380                                     bool fEncrypt, PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
    381381RTDECL(int) RTCrPkixEncryptionReset(RTCRPKIXENCRYPTION hEncryption);
    382382RTDECL(uint32_t) RTCrPkixEncryptionRetain(RTCRPKIXENCRYPTION hEncryption);
  • trunk/include/iprt/crypto/x509.h

    r58338 r59620  
    961961RTDECL(int) RTCrX509Certificate_ReadFromFile(PRTCRX509CERTIFICATE pCertificate, const char *pszFilename, uint32_t fFlags,
    962962                                             PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo);
     963RTDECL(int) RTCrX509Certificate_ReadFromBuffer(PRTCRX509CERTIFICATE pCertificate, const void *pvBuf, size_t cbBuf,
     964                                               uint32_t fFlags, PCRTASN1ALLOCATORVTABLE pAllocator,
     965                                               PRTERRINFO pErrInfo, const char *pszErrorTag);
     966
    963967
    964968
  • trunk/include/iprt/mangling.h

    r59575 r59620  
    11441144# define RTManifestEqualsEx                             RT_MANGLER(RTManifestEqualsEx)
    11451145# define RTManifestPtIosAddEntryNow                     RT_MANGLER(RTManifestPtIosAddEntryNow)
     1146# define RTManifestPtIosIsInstanceOf                    RT_MANGLER(RTManifestPtIosIsInstanceOf)
    11461147# define RTManifestQueryAllAttrTypes                    RT_MANGLER(RTManifestQueryAllAttrTypes)
    11471148# define RTManifestQueryAttr                            RT_MANGLER(RTManifestQueryAttr)
     
    22342235# define RTVfsIoStreamToPrivate                         RT_MANGLER(RTVfsIoStreamToPrivate)
    22352236# define RTVfsIoStrmFlush                               RT_MANGLER(RTVfsIoStrmFlush)
     2237# define RTVfsIoStrmFromBuffer                          RT_MANGLER(RTVfsIoStrmFromBuffer)
    22362238# define RTVfsIoStrmFromRTFile                          RT_MANGLER(RTVfsIoStrmFromRTFile)
    22372239# define RTVfsIoStrmFromRTPipe                          RT_MANGLER(RTVfsIoStrmFromRTPipe)
     
    22432245# define RTVfsIoStrmRead                                RT_MANGLER(RTVfsIoStrmRead)
    22442246# define RTVfsIoStrmReadAt                              RT_MANGLER(RTVfsIoStrmReadAt)
     2247# define RTVfsIoStrmReadAll                             RT_MANGLER(RTVfsIoStrmReadAll)
     2248# define RTVfsIoStrmReadAllFree                         RT_MANGLER(RTVfsIoStrmReadAllFree)
    22452249# define RTVfsIoStrmRelease                             RT_MANGLER(RTVfsIoStrmRelease)
    22462250# define RTVfsIoStrmRetain                              RT_MANGLER(RTVfsIoStrmRetain)
     
    26972701# define RTCrDigestRetain                               RT_MANGLER(RTCrDigestRetain)
    26982702# define RTCrDigestUpdate                               RT_MANGLER(RTCrDigestUpdate)
     2703# define RTCrDigestUpdateFromVfsFile                    RT_MANGLER(RTCrDigestUpdateFromVfsFile)
    26992704# define RTCrDigestCreateByObjId                        RT_MANGLER(RTCrDigestCreateByObjId)
    27002705# define RTCrDigestCreateByObjIdString                  RT_MANGLER(RTCrDigestCreateByObjIdString)
     
    30593064# define RTCrX509Validity_IsValidAtTimeSpec             RT_MANGLER(RTCrX509Validity_IsValidAtTimeSpec)
    30603065# define RTCrX509Certificate_ReadFromFile               RT_MANGLER(RTCrX509Certificate_ReadFromFile)
     3066# define RTCrX509Certificate_ReadFromBuffer             RT_MANGLER(RTCrX509Certificate_ReadFromBuffer)
    30613067# define RTCrX509AlgorithmIdentifier_Clone              RT_MANGLER(RTCrX509AlgorithmIdentifier_Clone)
    30623068# define RTCrX509AlgorithmIdentifier_Init               RT_MANGLER(RTCrX509AlgorithmIdentifier_Init)
  • trunk/include/iprt/manifest.h

    r59575 r59620  
    320320
    321321/**
     322 * Checks if the give I/O stream is a manifest passthru instance or not.
     323 *
     324 * @returns true if it's a manifest passthru I/O stream, false if not.
     325 * @param   hVfsPtIos   Possible the manifest passthru I/O stream handle.
     326 */
     327RTDECL(bool) RTManifestPtIosIsInstanceOf(RTVFSIOSTREAM hVfsPtIos);
     328
     329/**
    322330 * Adds an entry for a file with the specified set of attributes.
    323331 *
  • trunk/include/iprt/vfs.h

    r59524 r59620  
    440440
    441441/**
     442 * Creates a VFS file from a memory buffer.
     443 *
     444 * @returns IPRT status code.
     445 *
     446 * @param   fFlags          A combination of RTFILE_O_READ and RTFILE_O_WRITE.
     447 * @param   pvBuf           The buffer.  This will be copied and not referenced
     448 *                          after this function returns.
     449 * @param   cbBuf           The buffer size.
     450 * @param   phVfsIos        Where to return the VFS I/O stream handle.
     451 */
     452RTDECL(int)         RTVfsIoStrmFromBuffer(uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSIOSTREAM phVfsIos);
     453
     454/**
    442455 * Creates a VFS I/O stream handle from a standard IPRT file handle (RTFILE).
    443456 *
     
    554567 */
    555568RTDECL(int)         RTVfsIoStrmRead(RTVFSIOSTREAM hVfsIos, void *pvBuf, size_t cbToRead, bool fBlocking, size_t *pcbRead);
    556 RTDECL(int)         RTVfsIoStrmReadAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, void *pvBuf, size_t cbToRead, bool fBlocking, size_t *pcbRead);
    557 
    558 /**
    559  * Write bytes to the I/O stream.
    560  *
    561  * @returns IPRT status code.
    562  * @retval  VERR_ACCESS_DENIED if the stream is not writable.
    563  *
    564  * @param   hVfsIos         The VFS I/O stream handle.
    565  * @param   pvBuf           The bytes to write.
    566  * @param   cbToWrite       The number of bytes to write.
    567  * @param   fBlocking       Whether the call is blocking (@c true) or not.  If
    568  *                          not, the @a pcbWritten parameter must not be NULL.
    569  * @param   pcbWritten      Where to always store the number of bytes actually
    570  *                          written.  This can be NULL if @a fBlocking is true.
    571  * @sa      RTVfsFileWrite, RTFileWrite, RTPipeWrite, RTPipeWriteBlocking,
    572  *          RTSocketWrite
    573  */
    574 RTDECL(int)         RTVfsIoStrmWrite(RTVFSIOSTREAM hVfsIos, const void *pvBuf, size_t cbToWrite, bool fBlocking, size_t *pcbWritten);
    575 RTDECL(int)         RTVfsIoStrmWriteAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, const void *pvBuf, size_t cbToWrite, bool fBlocking, size_t *pcbWritten);
    576 
    577 /**
    578  * Reads bytes from the I/O stream into a scatter buffer.
     569
     570/**
     571 * Read bytes from the I/O stream, optionally with offset.
    579572 *
    580573 * @returns IPRT status code.
     
    594587 *
    595588 * @param   hVfsIos         The VFS I/O stream handle.
     589 * @param   off             Where to read at, -1 for the current position.
     590 * @param   pvBuf           Where to store the read bytes.
     591 * @param   cbToRead        The number of bytes to read.
     592 * @param   fBlocking       Whether the call is blocking (@c true) or not.  If
     593 *                          not, the @a pcbRead parameter must not be NULL.
     594 * @param   pcbRead         Where to always store the number of bytes actually
     595 *                          read.  This can be NULL if @a fBlocking is true.
     596 * @sa      RTVfsFileRead, RTFileRead, RTPipeRead, RTPipeReadBlocking,
     597 *          RTSocketRead
     598 */
     599RTDECL(int)         RTVfsIoStrmReadAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, void *pvBuf, size_t cbToRead, bool fBlocking, size_t *pcbRead);
     600
     601/**
     602 * Reads the remainder of the stream into a memory buffer.
     603 *
     604 * For simplifying string-style processing, the is a zero byte after the
     605 * returned buffer, making sure it can be used as a zero terminated string.
     606 *
     607 * @returns IPRT status code.
     608 * @param   hVfsIos         The VFS I/O stream handle.
     609 * @param   ppvBuf          Where to return the buffer.  Must pass to
     610 *                          RTVfsIoStrmReadAllFree for freeing, not RTMemFree!
     611 * @param   pcbBuf          Where to return the buffer size.
     612 */
     613RTDECL(int)         RTVfsIoStrmReadAll(RTVFSIOSTREAM hVfsIos, void **ppvBuf, size_t *pcbBuf);
     614
     615/**
     616 * Free memory buffer returned by RTVfsIoStrmReadAll.
     617 *
     618 * @param   pvBuf           What RTVfsIoStrmReadAll returned.
     619 * @param   cbBuf           What RTVfsIoStrmReadAll returned.
     620 */
     621RTDECL(void)        RTVfsIoStrmReadAllFree(void *pvBuf, size_t cbBuf);
     622
     623/**
     624 * Write bytes to the I/O stream.
     625 *
     626 * @returns IPRT status code.
     627 * @retval  VERR_ACCESS_DENIED if the stream is not writable.
     628 *
     629 * @param   hVfsIos         The VFS I/O stream handle.
     630 * @param   pvBuf           The bytes to write.
     631 * @param   cbToWrite       The number of bytes to write.
     632 * @param   fBlocking       Whether the call is blocking (@c true) or not.  If
     633 *                          not, the @a pcbWritten parameter must not be NULL.
     634 * @param   pcbWritten      Where to always store the number of bytes actually
     635 *                          written.  This can be NULL if @a fBlocking is true.
     636 * @sa      RTVfsFileWrite, RTFileWrite, RTPipeWrite, RTPipeWriteBlocking,
     637 *          RTSocketWrite
     638 */
     639RTDECL(int)         RTVfsIoStrmWrite(RTVFSIOSTREAM hVfsIos, const void *pvBuf, size_t cbToWrite, bool fBlocking, size_t *pcbWritten);
     640RTDECL(int)         RTVfsIoStrmWriteAt(RTVFSIOSTREAM hVfsIos, RTFOFF off, const void *pvBuf, size_t cbToWrite, bool fBlocking, size_t *pcbWritten);
     641
     642/**
     643 * Reads bytes from the I/O stream into a scatter buffer.
     644 *
     645 * @returns IPRT status code.
     646 * @retval  VINF_SUCCESS and the number of bytes read written to @a pcbRead.
     647 * @retval  VINF_TRY_AGAIN if @a fBlocking is @c false, @a pcbRead is not NULL,
     648 *          and no data was available. @a *pcbRead will be set to 0.
     649 * @retval  VINF_EOF when trying to read __beyond__ the end of the stream and
     650 *          @a pcbRead is not NULL (it will be set to the number of bytes read,
     651 *          or 0 if the end of the stream was reached before this call).
     652 *          When the last byte of the read request is the last byte in the
     653 *          stream, this status code will not be used.  However, VINF_EOF is
     654 *          returned when attempting to read 0 bytes while standing at the end
     655 *          of the stream.
     656 * @retval  VERR_EOF when trying to read __beyond__ the end of the stream and
     657 *          @a pcbRead is NULL.
     658 * @retval  VERR_ACCESS_DENIED if the stream is not readable.
     659 *
     660 * @param   hVfsIos         The VFS I/O stream handle.
     661 * @param   off             Where to read at, -1 for the current position.
    596662 * @param   pSgBuf          Pointer to a scatter buffer descriptor.  The number
    597663 *                          of bytes described by the segments is what will be
     
    603669 * @sa      RTFileSgRead, RTSocketSgRead, RTPipeRead, RTPipeReadBlocking
    604670 */
    605 RTDECL(int)         RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead);
     671RTDECL(int)         RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead);
    606672
    607673/**
     
    612678 *
    613679 * @param   hVfsIos         The VFS I/O stream handle.
     680 * @param   off             Where to write at, -1 for the current position.
    614681 * @param   pSgBuf          Pointer to a gather buffer descriptor.  The number
    615682 *                          of bytes described by the segments is what will be
     
    621688 * @sa      RTFileSgWrite, RTSocketSgWrite
    622689 */
    623 RTDECL(int)         RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten);
     690RTDECL(int)         RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten);
    624691
    625692/**
     
    686753 * Process the rest of the stream, checking if it's all valid UTF-8 encoding.
    687754 *
    688  * @returns VBox status cod.e
     755 * @returns IPRT status code.
    689756 *
    690757 * @param   hVfsIos         The VFS I/O stream handle.
     
    785852 * @returns IPRT status code.
    786853 * @retval  VINF_SUCCESS and the number of bytes read written to @a pcbRead.
    787  * @retval  VINF_TRY_AGAIN if @a fBlocking is @c false, @a pcbRead is not NULL,
    788  *          and no data was available. @a *pcbRead will be set to 0.
    789854 * @retval  VINF_EOF when trying to read __beyond__ the end of the file and
    790855 *          @a pcbRead is not NULL (it will be set to the number of bytes read,
     
    890955 * Memorizes the I/O stream as a file backed by memory.
    891956 *
    892  * @returns VBox status code.
     957 * @returns IPRT status code.
    893958 *
    894959 * @param   hVfsIos         The VFS I/O stream to memorize.  This will be read
     
    905970 * Creates a VFS file from a memory buffer.
    906971 *
    907  * @returns VBox status code.
    908  *
    909  * @param   hVfsIos         The VFS I/O stream to memorize.  This will be read
    910  *                          to the end on success, on failure its position is
    911  *                          undefined.
     972 * @returns IPRT status code.
     973 *
    912974 * @param   fFlags          A combination of RTFILE_O_READ and RTFILE_O_WRITE.
    913975 * @param   pvBuf           The buffer.  This will be copied and not referenced
     
    917979 *                          success.
    918980 */
    919 RTDECL(int) RTVfsFileFromBuffer(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile);
     981RTDECL(int) RTVfsFileFromBuffer(uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile);
    920982
    921983/**
    922984 * Creates a memory backed VFS file object for read and write.
    923985 *
    924  * @returns VBox status code.
     986 * @returns IPRT status code.
    925987 *
    926988 * @param   hVfsIos         The VFS I/O stream to memorize.  This will be read
  • trunk/src/VBox/Runtime/Makefile.kmk

    r59527 r59620  
    336336        common/crypto/digest-core.cpp \
    337337        common/crypto/digest-builtin.cpp \
     338        common/crypto/digest-vfs.cpp \
    338339        common/crypto/iprt-openssl.cpp \
    339340        common/crypto/rsa-asn1-decoder.cpp \
  • trunk/src/VBox/Runtime/common/checksum/manifest2.cpp

    r59575 r59620  
    527527
    528528    /*
    529      * Ignore this entry.
     529     * Ignore this entry?
    530530     */
    531531    char const * const *ppsz = pEquals->papszIgnoreEntries;
  • trunk/src/VBox/Runtime/common/checksum/manifest3.cpp

    r57358 r59620  
    3232#include <iprt/manifest.h>
    3333
     34#include <iprt/alloca.h>
    3435#include <iprt/asm.h>
    3536#include <iprt/assert.h>
     
    9192    /** The hashes.  */
    9293    PRTMANIFESTHASHES   pHashes;
     94    /** The current hash position. */
     95    RTFOFF              offCurPos;
    9396    /** Whether we're reading or writing. */
    9497    bool                fReadOrWrite;
     
    315318{
    316319    PRTMANIFESTPTIOS pThis = (PRTMANIFESTPTIOS)pvThis;
    317     int rc = RTVfsIoStrmSgRead(pThis->hVfsIos, pSgBuf, fBlocking, pcbRead);
     320    int rc;
     321
     322    /*
     323     * To make sure we're continuing where we left off, we must have the exact
     324     * stream position since a previous read using 'off' may change it.
     325     */
     326    RTFOFF offActual = off == -1 ? RTVfsIoStrmTell(pThis->hVfsIos) : off;
     327    if (offActual == pThis->offCurPos)
     328    {
     329        rc = RTVfsIoStrmSgRead(pThis->hVfsIos, off, pSgBuf, fBlocking, pcbRead);
     330        if (RT_SUCCESS(rc))
     331        {
     332            rtManifestPtIos_UpdateHashes(pThis, pSgBuf, pcbRead ? *pcbRead : ~(size_t)0);
     333            if (!pcbRead)
     334                for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
     335                    pThis->offCurPos += pSgBuf->paSegs[iSeg].cbSeg;
     336            else
     337                pThis->offCurPos += *pcbRead;
     338        }
     339        Assert(RTVfsIoStrmTell(pThis->hVfsIos) == pThis->offCurPos);
     340    }
     341    else
     342    {
     343        /*
     344         * If we're skipping over stuff, we need to read the gap and hash it.
     345         */
     346        if (pThis->offCurPos < offActual)
     347        {
     348            size_t cbBuf = _8K;
     349            void  *pvBuf = alloca(cbBuf);
     350            do
     351            {
     352                RTFOFF cbGap = off - pThis->offCurPos;
     353                size_t cbThisRead = cbGap >= (RTFOFF)cbBuf ? cbBuf : (size_t)cbGap;
     354                size_t cbActual;
     355                rc = RTVfsIoStrmReadAt(pThis->hVfsIos, pThis->offCurPos, pvBuf, cbThisRead, fBlocking, &cbActual);
     356                if (RT_FAILURE(rc) || rc == VINF_TRY_AGAIN)
     357                    return rc;
     358
     359                rtManifestHashesUpdate(pThis->pHashes, pvBuf, cbActual);
     360                pThis->offCurPos += cbActual;
     361
     362                if (rc == VINF_EOF)
     363                {
     364                    if (pcbRead)
     365                        *pcbRead = 0;
     366                    else
     367                        rc = VERR_EOF;
     368                    return rc;
     369                }
     370            } while (pThis->offCurPos < offActual);
     371            Assert(RTVfsIoStrmTell(pThis->hVfsIos) == offActual);
     372        }
     373
     374        /*
     375         * At this point we've eliminated any gap and can execute the requested read.
     376         */
     377        rc = RTVfsIoStrmSgRead(pThis->hVfsIos, off, pSgBuf, fBlocking, pcbRead);
     378        if (RT_SUCCESS(rc))
     379        {
     380            /* See if there is anything to update the hash with. */
     381            size_t cbLeft = pcbRead ? *pcbRead : ~(size_t)0;
     382            for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
     383            {
     384                size_t cbThis = pSgBuf->paSegs[iSeg].cbSeg;
     385                if (cbThis > cbLeft)
     386                    cbThis = cbLeft;
     387
     388                if (   offActual >= pThis->offCurPos
     389                    && pThis->offCurPos < offActual + (ssize_t)cbThis)
     390                {
     391                    size_t offSeg = (size_t)(offActual - pThis->offCurPos);
     392                    rtManifestHashesUpdate(pThis->pHashes, (uint8_t *)pSgBuf->paSegs[iSeg].pvSeg + offSeg, cbThis - offSeg);
     393                    pThis->offCurPos += cbThis - offSeg;
     394                }
     395
     396                cbLeft -= cbThis;
     397                if (!cbLeft)
     398                    break;
     399                offActual += cbThis;
     400            }
     401        }
     402    }
     403    return rc;
     404}
     405
     406
     407/**
     408 * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
     409 */
     410static DECLCALLBACK(int) rtManifestPtIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
     411{
     412    PRTMANIFESTPTIOS pThis = (PRTMANIFESTPTIOS)pvThis;
     413    AssertReturn(off == -1 || off == pThis->offCurPos, VERR_WRONG_ORDER);
     414    Assert(RTVfsIoStrmTell(pThis->hVfsIos) == pThis->offCurPos);
     415
     416    int rc = RTVfsIoStrmSgWrite(pThis->hVfsIos, -1 /*off*/, pSgBuf, fBlocking, pcbWritten);
    318417    if (RT_SUCCESS(rc))
    319         rtManifestPtIos_UpdateHashes(pThis, pSgBuf, pcbRead ? *pcbRead : ~(size_t)0);
    320     Assert(off == -1); NOREF(off);
    321     return rc;
    322 }
    323 
    324 
    325 /**
    326  * @interface_method_impl{RTVFSIOSTREAMOPS,pfnWrite}
    327  */
    328 static DECLCALLBACK(int) rtManifestPtIos_Write(void *pvThis, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
    329 {
    330     PRTMANIFESTPTIOS pThis = (PRTMANIFESTPTIOS)pvThis;
    331     int rc = RTVfsIoStrmSgWrite(pThis->hVfsIos, pSgBuf, fBlocking, pcbWritten);
    332     if (RT_SUCCESS(rc))
     418    {
    333419        rtManifestPtIos_UpdateHashes(pThis, pSgBuf, pcbWritten ? *pcbWritten : ~(size_t)0);
    334     Assert(off == -1); NOREF(off);
     420        if (!pcbWritten)
     421            for (uint32_t iSeg = 0; iSeg < pSgBuf->cSegs; iSeg++)
     422                pThis->offCurPos += pSgBuf->paSegs[iSeg].cbSeg;
     423        else
     424            pThis->offCurPos += *pcbWritten;
     425    }
    335426    return rc;
    336427}
     
    425516    AssertPtr(pszEntry);
    426517    AssertPtr(phVfsIosPassthru);
     518
     519    RTFOFF const offCurPos = RTVfsIoStrmTell(hVfsIos);
     520    AssertReturn(offCurPos >= 0, (int)offCurPos);
     521
    427522    uint32_t cRefs = RTManifestRetain(hManifest);
    428523    AssertReturn(cRefs != UINT32_MAX, VERR_INVALID_HANDLE);
     524
    429525    cRefs = RTVfsIoStrmRetain(hVfsIos);
    430526    AssertReturnStmt(cRefs != UINT32_MAX, RTManifestRelease(hManifest), VERR_INVALID_HANDLE);
     
    441537        pThis->hVfsIos          = hVfsIos;
    442538        pThis->pHashes          = rtManifestHashesCreate(fAttrs);
     539        pThis->offCurPos        = offCurPos;
    443540        pThis->hManifest        = hManifest;
    444541        pThis->fReadOrWrite     = fReadOrWrite;
     
    478575    rtManifestHashesFinal(pThis->pHashes);
    479576    return rtManifestHashesSetAttrs(pThis->pHashes, pThis->hManifest, pThis->pszEntry);
     577}
     578
     579
     580/**
     581 * Checks if the give I/O stream is a manifest passthru instance or not.
     582 *
     583 * @returns true if it's a manifest passthru I/O stream, false if not.
     584 * @param   hVfsPtIos   Possible the manifest passthru I/O stream handle.
     585 */
     586RTDECL(bool) RTManifestPtIosIsInstanceOf(RTVFSIOSTREAM hVfsPtIos)
     587{
     588    if (hVfsPtIos != NIL_RTVFSIOSTREAM)
     589    {
     590        PRTMANIFESTPTIOS pThis = (PRTMANIFESTPTIOS)RTVfsIoStreamToPrivate(hVfsPtIos, &g_rtManifestPassthruIosOps);
     591        if (pThis)
     592            return true;
     593    }
     594    return false;
    480595}
    481596
  • trunk/src/VBox/Runtime/common/crypto/x509-file.cpp

    r57358 r59620  
    7979
    8080
     81RTDECL(int) RTCrX509Certificate_ReadFromBuffer(PRTCRX509CERTIFICATE pCertificate, const void *pvBuf, size_t cbBuf,
     82                                               uint32_t fFlags, PCRTASN1ALLOCATORVTABLE pAllocator,
     83                                               PRTERRINFO pErrInfo, const char *pszErrorTag)
     84{
     85    AssertReturn(!fFlags, VERR_INVALID_FLAGS);
     86    PCRTCRPEMSECTION pSectionHead;
     87    int rc = RTCrPemParseContent(pvBuf, cbBuf, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers),
     88                                 &pSectionHead, pErrInfo);
     89    if (RT_SUCCESS(rc))
     90    {
     91        RTCRX509CERTIFICATE TmpCert;
     92        RTASN1CURSORPRIMARY PrimaryCursor;
     93        RTAsn1CursorInitPrimary(&PrimaryCursor, pSectionHead->pbData, (uint32_t)RT_MIN(pSectionHead->cbData, UINT32_MAX),
     94                                pErrInfo, pAllocator, RTASN1CURSOR_FLAGS_DER, pszErrorTag);
     95        rc = RTCrX509Certificate_DecodeAsn1(&PrimaryCursor.Cursor, 0, &TmpCert, "Cert");
     96        if (RT_SUCCESS(rc))
     97        {
     98            rc = RTCrX509Certificate_CheckSanity(&TmpCert, 0, pErrInfo, "Cert");
     99            if (RT_SUCCESS(rc))
     100            {
     101                rc = RTCrX509Certificate_Clone(pCertificate, &TmpCert, &g_RTAsn1DefaultAllocator);
     102                if (RT_SUCCESS(rc))
     103                {
     104                    if (pSectionHead->pNext || PrimaryCursor.Cursor.cbLeft)
     105                        rc = VINF_ASN1_MORE_DATA;
     106                }
     107            }
     108            RTCrX509Certificate_Delete(&TmpCert);
     109        }
     110        RTCrPemFreeSections(pSectionHead);
     111    }
     112    return rc;
     113}
     114
     115
    81116
    82117#if 0
  • trunk/src/VBox/Runtime/common/vfs/vfsbase.cpp

    r57358 r59620  
    21352135
    21362136
    2137 RTDECL(int) RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
     2137RTDECL(int) RTVfsIoStrmSgRead(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbRead)
    21382138{
    21392139    AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
     
    21502150    int rc;
    21512151    if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
    2152         rc = pThis->pOps->pfnRead(pThis->Base.pvThis, -1 /*off*/, pSgBuf, fBlocking, pcbRead);
     2152        rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbRead);
    21532153    else
    21542154    {
     
    21622162
    21632163            size_t cbReadSeg = pcbRead ? 0 : pSgBuf->paSegs[iSeg].cbSeg;
    2164             rc = pThis->pOps->pfnRead(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbRead ? &cbReadSeg : NULL);
     2164            rc = pThis->pOps->pfnRead(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbRead ? &cbReadSeg : NULL);
    21652165            if (RT_FAILURE(rc))
    21662166                break;
     
    21682168            if ((pcbRead && cbReadSeg != SgBuf.paSegs[0].cbSeg) || rc != VINF_SUCCESS)
    21692169                break;
     2170            if (off != -1)
     2171                off += cbReadSeg;
    21702172        }
    21712173
     
    21782180
    21792181
    2180 RTDECL(int) RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
     2182RTDECL(int) RTVfsIoStrmSgWrite(RTVFSIOSTREAM hVfsIos, RTFOFF off, PCRTSGBUF pSgBuf, bool fBlocking, size_t *pcbWritten)
    21812183{
    21822184    AssertPtrNullReturn(pcbWritten, VERR_INVALID_POINTER);
     
    21932195    int rc;
    21942196    if (!(pThis->pOps->fFeatures & RTVFSIOSTREAMOPS_FEAT_NO_SG))
    2195         rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, -1 /*off*/, pSgBuf, fBlocking, pcbWritten);
     2197        rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, pSgBuf, fBlocking, pcbWritten);
    21962198    else
    21972199    {
     
    22052207
    22062208            size_t cbWrittenSeg = 0;
    2207             rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, -1 /*off*/, &SgBuf, fBlocking, pcbWritten ? &cbWrittenSeg : NULL);
     2209            rc = pThis->pOps->pfnWrite(pThis->Base.pvThis, off, &SgBuf, fBlocking, pcbWritten ? &cbWrittenSeg : NULL);
    22082210            if (RT_FAILURE(rc))
    22092211                break;
     
    22132215                if (cbWrittenSeg != SgBuf.paSegs[0].cbSeg)
    22142216                    break;
     2217                if (off != -1)
     2218                    off += cbWrittenSeg;
    22152219            }
     2220            else if (off != -1)
     2221                off += pSgBuf->paSegs[iSeg].cbSeg;
    22162222        }
    22172223
  • trunk/src/VBox/Runtime/common/vfs/vfsiosmisc.cpp

    r57358 r59620  
    3333
    3434#include <iprt/err.h>
     35#include <iprt/mem.h>
    3536#include <iprt/string.h>
    3637
     
    124125}
    125126
     127
     128/** Header size.  */
     129#define READ_ALL_HEADER_SIZE    0x20
     130/** The header magic. It's followed by the size (both size_t). */
     131#define READ_ALL_HEADER_MAGIC   UINT32_C(0x11223355)
     132
     133RTDECL(int) RTVfsIoStrmReadAll(RTVFSIOSTREAM hVfsIos, void **ppvBuf, size_t *pcbBuf)
     134{
     135    /*
     136     * Try query the object information and in case the stream has a known
     137     * size we could use for guidance.
     138     */
     139    RTFSOBJINFO ObjInfo;
     140    int    rc = RTVfsIoStrmQueryInfo(hVfsIos, &ObjInfo, RTFSOBJATTRADD_NOTHING);
     141    size_t cbAllocated = RT_SUCCESS(rc) && ObjInfo.cbObject > 0 && ObjInfo.cbObject < _1G
     142                       ? (size_t)ObjInfo.cbObject + 1 : _16K;
     143    cbAllocated += READ_ALL_HEADER_SIZE;
     144    void *pvBuf = RTMemAlloc(cbAllocated);
     145    if (pvBuf)
     146    {
     147        memset(pvBuf, 0xfe, READ_ALL_HEADER_SIZE);
     148        size_t off = 0;
     149        for (;;)
     150        {
     151            /*
     152             * Handle buffer growing and detecting the end of it all.
     153             */
     154            size_t cbToRead = cbAllocated - off - READ_ALL_HEADER_SIZE - 1;
     155            if (!cbToRead)
     156            {
     157                /* The end? */
     158                uint8_t bIgn;
     159                size_t cbIgn;
     160                rc = RTVfsIoStrmRead(hVfsIos, &bIgn, 0, true /*fBlocking*/, &cbIgn);
     161                if (rc == VINF_EOF)
     162                    break;
     163
     164                /* Grow the buffer. */
     165                cbAllocated -= READ_ALL_HEADER_SIZE - 1;
     166                cbAllocated  = RT_MAX(RT_MIN(cbAllocated, _32M), _1K);
     167                cbAllocated  = RT_ALIGN_Z(cbAllocated, _4K);
     168                cbAllocated += READ_ALL_HEADER_SIZE + 1;
     169
     170                void *pvNew = RTMemRealloc(pvBuf, cbAllocated);
     171                AssertBreakStmt(pvNew, rc = VERR_NO_MEMORY);
     172
     173                cbToRead = cbAllocated - off - READ_ALL_HEADER_SIZE - 1;
     174            }
     175            Assert(cbToRead < cbAllocated);
     176
     177            /*
     178             * Read.
     179             */
     180            size_t cbActual;
     181            rc = RTVfsIoStrmRead(hVfsIos, (uint8_t *)pvBuf + READ_ALL_HEADER_SIZE + off, cbToRead,
     182                                 true /*fBlocking*/, &cbActual);
     183            if (RT_FAILURE(rc))
     184                break;
     185            Assert(cbActual > 0);
     186            Assert(cbActual <= cbToRead);
     187            off += cbActual;
     188            if (rc == VINF_EOF)
     189                break;
     190        }
     191        Assert(rc != VERR_EOF);
     192        if (RT_SUCCESS(rc))
     193        {
     194            ((size_t *)pvBuf)[0] = READ_ALL_HEADER_MAGIC;
     195            ((size_t *)pvBuf)[1] = off;
     196            ((uint8_t *)pvBuf)[READ_ALL_HEADER_SIZE + off] = 0;
     197
     198            *ppvBuf = (uint8_t *)pvBuf + READ_ALL_HEADER_SIZE;
     199            *pcbBuf = off;
     200            return VINF_SUCCESS;
     201        }
     202
     203        RTMemFree(pvBuf);
     204    }
     205    else
     206        rc = VERR_NO_MEMORY;
     207    *ppvBuf = NULL;
     208    *pcbBuf = 0;
     209    return rc;
     210}
     211
     212
     213RTDECL(void) RTVfsIoStrmReadAllFree(void *pvBuf, size_t cbBuf)
     214{
     215    AssertPtrReturnVoid(pvBuf);
     216
     217    /* Spool back to the start of the header. */
     218    pvBuf = (uint8_t *)pvBuf - READ_ALL_HEADER_SIZE;
     219
     220    /* Make sure the caller isn't messing with us. Hardcoded, but works. */
     221    Assert(((size_t *)pvBuf)[0] == READ_ALL_HEADER_MAGIC);
     222    Assert(((size_t *)pvBuf)[1] == cbBuf);
     223
     224    /* Free it. */
     225    RTMemFree(pvBuf);
     226}
     227
  • trunk/src/VBox/Runtime/common/vfs/vfsmemory.cpp

    r59524 r59620  
    805805
    806806
    807 RTDECL(int) RTVfsFileFromBuffer(RTVFSIOSTREAM hVfsIos, uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile)
     807RTDECL(int) RTVfsFileFromBuffer(uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSFILE phVfsFile)
    808808{
    809809    /*
     
    832832            return VINF_SUCCESS;
    833833        }
     834        RTVfsFileRelease(hVfsFile);
     835    }
     836    return rc;
     837}
     838
     839
     840RTDECL(int) RTVfsIoStrmFromBuffer(uint32_t fFlags, void const *pvBuf, size_t cbBuf, PRTVFSIOSTREAM phVfsIos)
     841{
     842    RTVFSFILE hVfsFile;
     843    int rc = RTVfsFileFromBuffer(fFlags, pvBuf, cbBuf, &hVfsFile);
     844    if (RT_SUCCESS(rc))
     845    {
     846        *phVfsIos = RTVfsFileToIoStream(hVfsFile);
    834847        RTVfsFileRelease(hVfsFile);
    835848    }
  • trunk/src/VBox/Runtime/common/vfs/vfsstdfile.cpp

    r57635 r59620  
    502502    int rc = RTVfsFileOpenNormal(pszFilename, fOpen, &hVfsFile);
    503503    if (RT_SUCCESS(rc))
     504    {
    504505        *phVfsIos = RTVfsFileToIoStream(hVfsFile);
    505     return rc;
    506 }
    507 
     506        RTVfsFileRelease(hVfsFile);
     507    }
     508    return rc;
     509}
     510
  • trunk/src/VBox/Runtime/common/zip/gzipvfs.cpp

    r57358 r59620  
    313313        {
    314314            size_t cbReadIn = ~(size_t)0;
    315             rc = RTVfsIoStrmSgRead(pThis->hVfsIos, &pThis->SgBuf, fBlocking, &cbReadIn);
     315            rc = RTVfsIoStrmSgRead(pThis->hVfsIos, -1 /*off*/, &pThis->SgBuf, fBlocking, &cbReadIn);
    316316            if (rc != VINF_SUCCESS)
    317317            {
     
    425425
    426426        cbWrittenOut = ~(size_t)0;
    427         rc = RTVfsIoStrmSgWrite(pThis->hVfsIos, &pThis->SgBuf, fBlocking, &cbWrittenOut);
     427        rc = RTVfsIoStrmSgWrite(pThis->hVfsIos, -1 /*off*/, &pThis->SgBuf, fBlocking, &cbWrittenOut);
    428428        if (rc != VINF_SUCCESS)
    429429        {
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