VirtualBox

Changeset 21871 in vbox


Ignore:
Timestamp:
Jul 30, 2009 9:30:27 AM (15 years ago)
Author:
vboxsync
Message:

SSM: partly working buffering.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/SSM-new.cpp

    r21866 r21871  
    295295    uint64_t                offStream;
    296296    /** The amount of buffered data. */
    297     size_t                  cb;
     297    uint32_t                cb;
    298298    /** End of stream indicator (for read streams only). */
    299299    bool                    fEndOfStream;
     
    317317    /** Termination indicator. */
    318318    bool volatile           fTerminating;
     319    /** Indicates whether it is necessary to seek before the next buffer is
     320     *  read from the stream.  This is used to avoid a seek in ssmR3StrmPeekAt. */
     321    bool                    fNeedSeek;
    319322    /** Stream error status. */
    320323    int32_t volatile        rc;
     324    /** The handle of the I/O thread. This is set to nil when not active. */
     325    RTTHREAD                hIoThread;
    321326
    322327    /** The head of the consumer queue.
     
    13611366     */
    13621367    pStrm->fTerminating = false;
     1368    pStrm->fNeedSeek    = false;
    13631369    pStrm->rc           = VINF_SUCCESS;
     1370    pStrm->hIoThread    = NIL_RTTHREAD;
    13641371
    13651372    pStrm->pHead        = NULL;
     
    15421549    for (;;)
    15431550    {
    1544         PSSMSTRMBUF pMine = (PSSMSTRMBUF)ASMAtomicUoReadPtr((void * volatile *)pStrm->pFree);
     1551        PSSMSTRMBUF pMine = (PSSMSTRMBUF)ASMAtomicUoReadPtr((void * volatile *)&pStrm->pFree);
    15451552        if (!pMine)
    15461553        {
     
    16321639        }
    16331640
    1634         pMine = (PSSMSTRMBUF)ASMAtomicXchgPtr((void * volatile *)pStrm->pHead, NULL);
     1641        pMine = (PSSMSTRMBUF)ASMAtomicXchgPtr((void * volatile *)&pStrm->pHead, NULL);
    16351642        if (pMine)
    16361643            pStrm->pPending = ssmR3StrmReverseList(pMine);
     
    16591666    if (pStrm->pCur)
    16601667    {
    1661         PSSMSTRMBUF pCur = pStrm->pCur;
     1668        PSSMSTRMBUF pBuf = pStrm->pCur;
    16621669        pStrm->pCur = NULL;
    16631670
     
    16651672        {
    16661673            uint32_t cb     = pStrm->off;
    1667             pCur->cb        = cb;
    1668             pCur->offStream = pStrm->offCurStream;
     1674            pBuf->cb        = cb;
     1675            pBuf->offStream = pStrm->offCurStream;
    16691676            if (    pStrm->fChecksummed
    16701677                &&  pStrm->offStreamCRC < cb)
    16711678                pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC,
    1672                                                      &pCur->abData[pStrm->offStreamCRC],
     1679                                                     &pBuf->abData[pStrm->offStreamCRC],
    16731680                                                     cb - pStrm->offStreamCRC);
    16741681            pStrm->offCurStream += cb;
     
    16761683            pStrm->offStreamCRC = 0;
    16771684
    1678             ssmR3StrmPutBuf(pStrm, pCur);
     1685            ssmR3StrmPutBuf(pStrm, pBuf);
    16791686        }
    16801687        else
    16811688        {
    1682             uint32_t cb = pCur->cb;
     1689            uint32_t cb = pBuf->cb;
    16831690            if (    pStrm->fChecksummed
    16841691                &&  pStrm->offStreamCRC < cb)
    16851692                pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC,
    1686                                                      &pCur->abData[pStrm->offStreamCRC],
     1693                                                     &pBuf->abData[pStrm->offStreamCRC],
    16871694                                                     cb - pStrm->offStreamCRC);
    16881695            pStrm->offCurStream += cb;
     
    16901697            pStrm->offStreamCRC = 0;
    16911698
    1692             ssmR3StrmPutFreeBuf(pStrm, pCur);
     1699            ssmR3StrmPutFreeBuf(pStrm, pBuf);
    16931700        }
    16941701    }
     
    17951802    if (RT_LIKELY(pBuf))
    17961803    {
    1797         uint32_t cbLeft = pStrm->off - RT_SIZEOFMEMB(SSMSTRMBUF, abData);
     1804        uint32_t cbLeft = RT_SIZEOFMEMB(SSMSTRMBUF, abData) - pStrm->off;
    17981805        if (RT_LIKELY(cbLeft >= cbToWrite))
    17991806        {
    18001807            memcpy(&pBuf->abData[pStrm->off], pvBuf, cbToWrite);
    1801             pStrm->off += cbToWrite;
     1808            pStrm->off += (uint32_t)cbToWrite;
    18021809            return VINF_SUCCESS;
    18031810        }
     
    18101817            pvBuf       = (uint8_t const *)pvBuf + cbLeft;
    18111818        }
     1819        Assert(pStrm->off == RT_SIZEOFMEMB(SSMSTRMBUF, abData));
    18121820    }
    18131821
     
    18321840         * Copy data to the buffer.
    18331841         */
    1834         uint32_t cbCopy = pStrm->off - RT_SIZEOFMEMB(SSMSTRMBUF, abData);
     1842        uint32_t cbCopy = RT_SIZEOFMEMB(SSMSTRMBUF, abData);
    18351843        if (cbCopy > cbToWrite)
    1836             cbCopy = cbToWrite;
    1837         memcpy(&pBuf->abData[pStrm->off], pvBuf, cbCopy);
    1838         pStrm->off += cbCopy;
     1844            cbCopy = (uint32_t)cbToWrite;
     1845        memcpy(&pBuf->abData[0], pvBuf, cbCopy);
     1846        pStrm->off  = cbCopy;
    18391847        cbToWrite  -= cbCopy;
    18401848        pvBuf       = (uint8_t const *)pvBuf + cbCopy;
     
    18561864static int ssmR3StrmReadMore(PSSMSTRM pStrm)
    18571865{
     1866    int rc;
     1867
     1868    /*
     1869     * Undo seek done by ssmR3StrmPeekAt.
     1870     */
     1871    if (pStrm->fNeedSeek)
     1872    {
     1873        rc = RTFileSeek(pStrm->hFile, pStrm->offCurStream + pStrm->off, RTFILE_SEEK_BEGIN, NULL);
     1874        if (RT_FAILURE(rc))
     1875        {
     1876            if (ssmR3StrmSetError(pStrm, rc))
     1877                LogRel(("ssmR3StrmReadMore: RTFileSeek(,%#llx,) failed with rc=%Rrc\n", pStrm->offCurStream + pStrm->off, rc));
     1878            return rc;
     1879        }
     1880
     1881    }
     1882
     1883    /*
     1884     * Get a free buffer and try fill it up.
     1885     */
    18581886    PSSMSTRMBUF pBuf = ssmR3StrmGetFreeBuf(pStrm);
    18591887    if (!pBuf)
    18601888        return pStrm->rc;
    18611889
    1862     pBuf->offStream = RTFileTell(pStrm->hFile);
     1890    pBuf->offStream = pStrm->offCurStream + pStrm->off;
     1891    Assert(pBuf->offStream == RTFileTell(pStrm->hFile));
    18631892    size_t cbRead   = sizeof(pBuf->abData);
    1864     int rc = RTFileRead(pStrm->hFile, &pBuf->abData[0], cbRead, &cbRead);
     1893    rc = RTFileRead(pStrm->hFile, &pBuf->abData[0], cbRead, &cbRead);
    18651894    if (RT_SUCCESS(rc))
    18661895    {
    1867         pBuf->cb           = cbRead;
    1868         pBuf->fEndOfStream = cbRead == 0;
     1896        pBuf->cb           = (uint32_t)cbRead;
     1897        pBuf->fEndOfStream = cbRead == 0; /** @todo Check out this EOF mess... */
    18691898        ssmR3StrmPutBuf(pStrm, pBuf);
    18701899    }
     
    19061935    {
    19071936        Assert(pStrm->off <= pBuf->cb);
    1908         uint32_t cbLeft = pStrm->off - pBuf->cb;
     1937        uint32_t cbLeft = pBuf->cb - pStrm->off;
    19091938        if (cbLeft >= cbToRead)
    19101939        {
    19111940            memcpy(pvBuf, &pBuf->abData[pStrm->off], cbToRead);
    1912             pStrm->off += cbToRead;
     1941            pStrm->off += (uint32_t)cbToRead;
     1942            Assert(pStrm->off <= pBuf->cb);
    19131943            return VINF_SUCCESS;
    19141944        }
     
    19221952        else if (pBuf->fEndOfStream)
    19231953            return VERR_EOF;
     1954        Assert(pStrm->off == pBuf->cb);
    19241955    }
    19251956
     
    19621993         * Read data from the buffer.
    19631994         */
    1964         uint32_t cbCopy = pStrm->off - pBuf->cb;
     1995        uint32_t cbCopy = pBuf->cb;
    19651996        if (cbCopy > cbToRead)
    1966             cbCopy = cbToRead;
    1967         memcpy(pvBuf, &pBuf->abData[pStrm->off], cbCopy);
    1968         pStrm->off += cbCopy;
     1997            cbCopy = (uint32_t)cbToRead;
     1998        memcpy(pvBuf, &pBuf->abData[0], cbCopy);
     1999        pStrm->off  = cbCopy;
    19692000        cbToRead   -= cbCopy;
    19702001        pvBuf       = (uint8_t *)pvBuf + cbCopy;
     2002        Assert(!pStrm->pCur || pStrm->off <= pStrm->pCur->cb);
    19712003    } while (cbToRead > 0);
    19722004
     
    20002032    {
    20012033        PSSMSTRMBUF pBuf = pStrm->pCur; Assert(pBuf);
    2002         pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC, &pBuf->abData[pStrm->off], pStrm->off - pStrm->offStreamCRC);
     2034        pStrm->u32StreamCRC = RTCrc32Process(pStrm->u32StreamCRC, &pBuf->abData[pStrm->offStreamCRC], pStrm->off - pStrm->offStreamCRC);
     2035//if (pStrm->offStreamCRC == 0 && pStrm->off == 0x40)
     2036//    LogAlways(("ssmR3StrmCurCRC: %08x\n%.64Rhxd\n", pStrm->u32StreamCRC, pBuf->abData));
     2037        pStrm->offStreamCRC = pStrm->off;
    20032038    }
    20042039    else
     
    20302065{
    20312066    pStrm->fChecksummed = false;
     2067}
     2068
     2069
     2070/**
     2071 *
     2072 * @returns VBox stutus code.
     2073 * @param   pStrm       The strem handle.
     2074 * @param   off         The seek offset.
     2075 * @param   uMethod     The seek method.
     2076 * @param   poff        Where to optionally store the new file offset.
     2077 */
     2078static int ssmR3StrmSeek(PSSMSTRM pStrm, int64_t off, uint32_t uMethod, uint64_t *poff)
     2079{
     2080    AssertReturn(!pStrm->fWrite, VERR_NOT_SUPPORTED);
     2081    AssertReturn(pStrm->hIoThread == NIL_RTTHREAD, VERR_WRONG_ORDER);
     2082
     2083    uint64_t offStream;
     2084    int rc = RTFileSeek(pStrm->hFile, off, uMethod, &offStream);
     2085    if (RT_SUCCESS(rc))
     2086    {
     2087        pStrm->offCurStream = offStream;
     2088        pStrm->off = 0;
     2089        pStrm->offStreamCRC = 0;
     2090        pStrm->u32StreamCRC = 0;
     2091        if (pStrm->pCur)
     2092        {
     2093            ssmR3StrmPutFreeBuf(pStrm, pStrm->pCur);
     2094            pStrm->pCur = NULL;
     2095        }
     2096        if (poff)
     2097            *poff = offStream;
     2098    }
     2099    return rc;
    20322100}
    20332101
     
    20792147
    20802148
     2149/***
     2150 * Tests if the stream is a file stream or not.
     2151 *
     2152 * @returns true / false.
     2153 * @param   pStrm       The stream handle.
     2154 */
     2155static bool ssmR3StrmIsFile(PSSMSTRM pStrm)
     2156{
     2157    return pStrm->hFile != NIL_RTFILE;
     2158}
     2159
     2160
     2161/**
     2162 * Peeks at data in a file stream without buffering anything (or upsetting
     2163 * the buffering for that matter).
     2164 *
     2165 * @returns VBox status code.
     2166 * @param   pStrm       The stream handle
     2167 * @param   off         The offset to start peeking at. Use a negative offset to
     2168 *                      peek at something relative to the end of the file.
     2169 * @param   pvBuf       Output buffer.
     2170 * @param   cbToRead    How much to read.
     2171 * @param   poff        Where to optionally store the position.  Useful when
     2172 *                      using a negative off.
     2173 *
     2174 * @remarks Failures occuring while peeking will not be raised on the stream.
     2175 */
     2176static int ssmR3StrmPeekAt(PSSMSTRM pStrm, RTFOFF off, void *pvBuf, size_t cbToRead, uint64_t *poff)
     2177{
     2178    AssertReturn(!pStrm->fWrite && pStrm->hFile != NIL_RTFILE, VERR_NOT_SUPPORTED);
     2179    AssertReturn(pStrm->hIoThread == NIL_RTTHREAD, VERR_WRONG_ORDER);
     2180
     2181    pStrm->fNeedSeek = true;
     2182    int rc = RTFileSeek(pStrm->hFile, off, off >= 0 ? RTFILE_SEEK_BEGIN : RTFILE_SEEK_END, poff);
     2183    if (RT_SUCCESS(rc))
     2184        rc = RTFileRead(pStrm->hFile, pvBuf, cbToRead, NULL);
     2185
     2186    return rc;
     2187}
     2188
     2189
    20812190
    20822191/**
     
    20842193 *
    20852194 * @returns VBox status.
    2086  * @param   File        Handle to the file. Positioned where to start
    2087  *                     checksumming.
    2088  * @param   cbFile      The portion of the file to checksum.
     2195 * @param   pStrm       The stream handle
     2196 * @param   off         Where to start checksumming.
     2197 * @param   cb          How much to checksum.
    20892198 * @param   pu32CRC     Where to store the calculated checksum.
    20902199 */
    2091 static int ssmR3CalcChecksum(RTFILE File, uint64_t cbFile, uint32_t *pu32CRC)
     2200static int ssmR3CalcChecksum(PSSMSTRM pStrm, uint64_t off, uint64_t cb, uint32_t *pu32CRC)
    20922201{
    20932202    /*
    20942203     * Allocate a buffer.
    20952204     */
    2096     void *pvBuf = RTMemTmpAlloc(32*1024);
     2205    const size_t cbBuf = _32K;
     2206    void *pvBuf = RTMemTmpAlloc(cbBuf);
    20972207    if (!pvBuf)
    20982208        return VERR_NO_TMP_MEMORY;
     
    21012211     * Loop reading and calculating CRC32.
    21022212     */
    2103     int         rc = VINF_SUCCESS;
     2213    int         rc     = VINF_SUCCESS;
    21042214    uint32_t    u32CRC = RTCrc32Start();
    2105     while (cbFile)
     2215    while (cb > 0)
    21062216    {
    21072217        /* read chunk */
    2108         register unsigned cbToRead = 32*1024;
    2109         if (cbFile < 32*1024)
    2110             cbToRead = (unsigned)cbFile;
    2111         rc = RTFileRead(File, pvBuf, cbToRead, NULL);
     2218        size_t cbToRead = cbBuf;
     2219        if (cb < cbBuf)
     2220            cbToRead = cb;
     2221        rc = ssmR3StrmPeekAt(pStrm, off, pvBuf, cbToRead, NULL);
    21122222        if (RT_FAILURE(rc))
    21132223        {
     
    21182228
    21192229        /* update total */
    2120         cbFile -= cbToRead;
     2230        cb -= cbToRead;
    21212231
    21222232        /* calc crc32. */
    2123         u32CRC = RTCrc32Process(u32CRC, pvBuf,  cbToRead);
     2233        u32CRC = RTCrc32Process(u32CRC, pvBuf, cbToRead);
    21242234    }
    21252235    RTMemTmpFree(pvBuf);
     
    27472857
    27482858        /*
    2749          * Read and validate the footer.
     2859         * Read and validate the footer if it's a file.
    27502860         */
    2751 /** @todo this needs to be fixed when starting to buffer input using a separate thread. */
    2752         uint64_t    offRestore = RTFileTell(pSSM->Strm.hFile);
    2753         SSMFILEFTR  Footer;
    2754         uint64_t    offFooter;
    2755         rc = RTFileSeek(pSSM->Strm.hFile, -(RTFOFF)sizeof(Footer), RTFILE_SEEK_END, &offFooter);
    2756         AssertLogRelRCReturn(rc, rc);
    2757         rc = RTFileRead(pSSM->Strm.hFile, &Footer, sizeof(Footer), NULL);
    2758         AssertLogRelRCReturn(rc, rc);
    2759         if (memcmp(Footer.szMagic, SSMFILEFTR_MAGIC, sizeof(Footer.szMagic)))
    2760         {
    2761             LogRel(("SSM: Bad footer magic: %.*Rhxs\n", sizeof(Footer.szMagic), &Footer.szMagic[0]));
    2762             return VERR_SSM_INTEGRITY;
    2763         }
    2764         SSM_CHECK_CRC32_RET(&Footer, sizeof(Footer), ("Footer CRC mismatch: %08x, correct is %08x\n", u32CRC, u32ActualCRC));
    2765         if (Footer.offStream != offFooter)
    2766         {
    2767             LogRel(("SSM: SSMFILEFTR::offStream is wrong: %llx, expected %llx\n", Footer.offStream, offFooter));
    2768             return VERR_SSM_INTEGRITY;
    2769         }
    2770         if (Footer.u32Reserved)
    2771         {
    2772             LogRel(("SSM: Reserved footer field isn't zero: %08x\n", Footer.u32Reserved));
    2773             return VERR_SSM_INTEGRITY;
    2774         }
    2775         if (    !fChecksummed
    2776             &&  Footer.u32StreamCRC)
    2777         {
    2778             LogRel(("SSM: u32StreamCRC field isn't zero, but header says stream checksumming is disabled.\n"));
    2779             return VERR_SSM_INTEGRITY;
    2780         }
    2781 
    2782         pSSM->u.Read.cbLoadFile = offFooter + sizeof(Footer);
    2783         pSSM->u.Read.u32LoadCRC = Footer.u32StreamCRC;
     2861        if (ssmR3StrmIsFile(&pSSM->Strm))
     2862        {
     2863            SSMFILEFTR  Footer;
     2864            uint64_t    offFooter;
     2865            rc = ssmR3StrmPeekAt(&pSSM->Strm, -(RTFOFF)sizeof(SSMFILEFTR), &Footer, sizeof(Footer), &offFooter);
     2866            AssertLogRelRCReturn(rc, rc);
     2867            if (memcmp(Footer.szMagic, SSMFILEFTR_MAGIC, sizeof(Footer.szMagic)))
     2868            {
     2869                LogRel(("SSM: Bad footer magic: %.*Rhxs\n", sizeof(Footer.szMagic), &Footer.szMagic[0]));
     2870                return VERR_SSM_INTEGRITY;
     2871            }
     2872            SSM_CHECK_CRC32_RET(&Footer, sizeof(Footer), ("Footer CRC mismatch: %08x, correct is %08x\n", u32CRC, u32ActualCRC));
     2873            if (Footer.offStream != offFooter)
     2874            {
     2875                LogRel(("SSM: SSMFILEFTR::offStream is wrong: %llx, expected %llx\n", Footer.offStream, offFooter));
     2876                return VERR_SSM_INTEGRITY;
     2877            }
     2878            if (Footer.u32Reserved)
     2879            {
     2880                LogRel(("SSM: Reserved footer field isn't zero: %08x\n", Footer.u32Reserved));
     2881                return VERR_SSM_INTEGRITY;
     2882            }
     2883            if (    !fChecksummed
     2884                &&  Footer.u32StreamCRC)
     2885            {
     2886                LogRel(("SSM: u32StreamCRC field isn't zero, but header says stream checksumming is disabled.\n"));
     2887                return VERR_SSM_INTEGRITY;
     2888            }
     2889
     2890            pSSM->u.Read.cbLoadFile = offFooter + sizeof(Footer);
     2891            pSSM->u.Read.u32LoadCRC = Footer.u32StreamCRC;
     2892        }
     2893        else
     2894        {
     2895            pSSM->u.Read.cbLoadFile = UINT64_MAX;
     2896            pSSM->u.Read.u32LoadCRC = 0;
     2897        }
    27842898
    27852899        /*
     
    27952909        if (    fChecksummed
    27962910            &&  fChecksumIt
    2797             &&  !fChecksumOnRead)
    2798         {
    2799             rc = RTFileSeek(pSSM->Strm.hFile, 0, RTFILE_SEEK_BEGIN, NULL);
    2800             AssertLogRelRCReturn(rc, rc);
     2911            &&  !fChecksumOnRead
     2912            &&  ssmR3StrmIsFile(&pSSM->Strm))
     2913        {
    28012914            uint32_t u32CRC;
    2802             rc = ssmR3CalcChecksum(pSSM->Strm.hFile, offFooter, &u32CRC);
     2915            rc = ssmR3CalcChecksum(&pSSM->Strm, 0, pSSM->u.Read.cbLoadFile - sizeof(SSMFILEFTR), &u32CRC);
    28032916            if (RT_FAILURE(rc))
    28042917                return rc;
     
    28092922            }
    28102923        }
    2811 
    2812         rc = RTFileSeek(pSSM->Strm.hFile, RTFILE_SEEK_BEGIN, offRestore, NULL);
    2813         AssertRCReturn(rc, rc);
    28142924    }
    28152925    else
     
    28943004            ||  fChecksumOnRead)
    28953005        {
    2896 /** @todo this needs to be fixed when starting to buffer input using a separate thread. */
    2897             uint64_t offRestore = RTFileTell(pSSM->Strm.hFile);
    2898             rc = RTFileSeek(pSSM->Strm.hFile, RT_OFFSETOF(SSMFILEHDRV11, u32CRC) + sizeof(uHdr.v1_1.u32CRC), RTFILE_SEEK_BEGIN, NULL);
    2899             AssertLogRelRCReturn(rc, rc);
    29003006            uint32_t u32CRC;
    2901             rc = ssmR3CalcChecksum(pSSM->Strm.hFile, cbFile - pSSM->u.Read.cbFileHdr, &u32CRC);
     3007            rc = ssmR3CalcChecksum(&pSSM->Strm,
     3008                                   RT_OFFSETOF(SSMFILEHDRV11, u32CRC) + sizeof(uHdr.v1_1.u32CRC),
     3009                                   cbFile - pSSM->u.Read.cbFileHdr,
     3010                                   &u32CRC);
    29023011            if (RT_FAILURE(rc))
    29033012                return rc;
     
    29073016                return VERR_SSM_INTEGRITY_CRC;
    29083017            }
    2909             rc = RTFileSeek(pSSM->Strm.hFile, RTFILE_SEEK_BEGIN, offRestore, NULL);
    2910             AssertRCReturn(rc, rc);
    29113018        }
    29123019    }
     
    32033310         */
    32043311        uint64_t        offUnit         = ssmR3StrmTell(&pSSM->Strm);
    3205         uint32_t        u32CurStreamCRC = pSSM->u32StreamCRC;
     3312        uint32_t        u32CurStreamCRC = ssmR3StrmCurCRC(&pSSM->Strm);
    32063313        SSMFILEUNITHDR  UnitHdr;
    32073314        int rc = ssmR3StrmRead(&pSSM->Strm, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName));
     
    32403347            return VERR_SSM_INTEGRITY;
    32413348        }
    3242         AssertLogRelMsgReturn(UnitHdr.u32CurStreamCRC == u32CurStreamCRC || !pSSM->fChecksummed,
     3349        AssertLogRelMsgReturn(UnitHdr.u32CurStreamCRC == u32CurStreamCRC || !pSSM->Strm.fChecksummed,
    32433350                              ("Unit at %#llx (%lld): Stream CRC mismatch: %08x, correct is %08x\n", offUnit, offUnit, UnitHdr.u32CurStreamCRC, u32CurStreamCRC), VERR_SSM_INTEGRITY);
    32443351        AssertLogRelMsgReturn(!UnitHdr.fFlags, ("Unit at %#llx (%lld): fFlags=%08x\n", offUnit, offUnit, UnitHdr.fFlags), VERR_SSM_INTEGRITY);
     
    34763583        if (pfnProgress)
    34773584            pfnProgress(pVM, 99, pvUser);
     3585
     3586        ssmR3StrmClose(&Handle.Strm);
    34783587    }
    34793588
     
    34813590     * Done
    34823591     */
    3483     ssmR3StrmClose(&Handle.Strm);
    34843592    if (RT_SUCCESS(rc))
    34853593    {
     
    35953703     * Close the stream and free the handle.
    35963704     */
    3597     ssmR3StrmClose(&Handle.Strm);
     3705    int rc = ssmR3StrmClose(&pSSM->Strm);
    35983706    if (pSSM->u.Read.pZipDecompV1)
    35993707    {
     
    36293737         * Read the unit header and verify it.
    36303738         */
    3631         int rc = RTFileReadAt(pSSM->hFile, off, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName), NULL);
     3739/** @todo this doesn't work when we get an I/O thread... */
     3740        int rc = RTFileReadAt(pSSM->Strm.hFile, off, &UnitHdr, RT_OFFSETOF(SSMFILEUNITHDR, szName), NULL);
    36323741        AssertRCReturn(rc, rc);
    36333742        if (!memcmp(&UnitHdr.achMagic[0], SSMFILEUNITHDR_MAGIC, sizeof(SSMFILEUNITHDR_MAGIC)))
     
    36393748                &&  UnitHdr.cchName     == cbUnit)
    36403749            {
    3641                 rc = RTFileRead(pSSM->hFile, szName, cbUnit, NULL);
     3750                rc = RTFileRead(pSSM->Strm.hFile, szName, cbUnit, NULL);
    36423751                AssertRCReturn(rc, rc);
    36433752                AssertLogRelMsgReturn(!szName[UnitHdr.cchName - 1],
     
    36853794     */
    36863795    uint64_t const offDir = pSSM->u.Read.cbLoadFile - sizeof(SSMFILEFTR) - cbDir;
    3687     int rc = RTFileReadAt(pSSM->hFile, offDir, pDir, cbDir, NULL);
     3796/** @todo this doesn't work when we get an I/O thread... */
     3797    int rc = RTFileReadAt(pSSM->Strm.hFile, offDir, pDir, cbDir, NULL);
    36883798    AssertLogRelRCReturn(rc, rc);
    36893799    AssertLogRelReturn(!memcmp(pDir->szMagic, SSMFILEDIR_MAGIC, sizeof(pDir->szMagic)), VERR_SSM_INTEGRITY);
     
    37173827                RT_ZERO(UnitHdr);
    37183828            }
    3719             rc = RTFileReadAt(pSSM->hFile, pDir->aEntries[i].off, &UnitHdr, cbToRead, NULL);
     3829/** @todo this doesn't work when we get an I/O thread... */
     3830            rc = RTFileReadAt(pSSM->Strm.hFile, pDir->aEntries[i].off, &UnitHdr, cbToRead, NULL);
    37203831            AssertLogRelRCReturn(rc, rc);
    37213832
     
    37483859                if (piVersion)
    37493860                    *piVersion = UnitHdr.u32Version;
    3750                 rc = RTFileSeek(pSSM->hFile, pDir->aEntries[i].off + cbUnitHdr, RTFILE_SEEK_BEGIN, NULL);
     3861/** @todo this doesn't work when we get an I/O thread... */
     3862                rc = RTFileSeek(pSSM->Strm.hFile, pDir->aEntries[i].off + cbUnitHdr, RTFILE_SEEK_BEGIN, NULL);
    37513863                AssertLogRelRCReturn(rc, rc);
    37523864
    3753                 if (pSSM->fChecksummed)
    3754                     pSSM->u32StreamCRC = RTCrc32Process(UnitHdr.u32CurStreamCRC, &UnitHdr, cbUnitHdr);
     3865                if (pSSM->Strm.fChecksummed)
     3866                    pSSM->Strm.u32StreamCRC = RTCrc32Process(UnitHdr.u32CurStreamCRC, &UnitHdr, cbUnitHdr);
    37553867
    37563868                ssmR3DataReadBeginV2(pSSM);
     
    37803892     */
    37813893    SSMFILEFTR      Footer;
    3782     int rc = RTFileReadAt(pSSM->hFile, pSSM->u.Read.cbLoadFile - sizeof(Footer), &Footer, sizeof(Footer), NULL);
     3894/** @todo this doesn't work when we get an I/O thread... */
     3895    int rc = RTFileReadAt(pSSM->Strm.hFile, pSSM->u.Read.cbLoadFile - sizeof(Footer), &Footer, sizeof(Footer), NULL);
    37833896    AssertLogRelRCReturn(rc, rc);
    37843897    AssertLogRelReturn(!memcmp(Footer.szMagic, SSMFILEFTR_MAGIC, sizeof(Footer.szMagic)), VERR_SSM_INTEGRITY);
     
    39374050    {
    39384051        cbHdr = 2;
    3939         abHdr[1] = cb;
     4052        abHdr[1] = (uint8_t)cb;
    39404053    }
    39414054    else if (cb < 0x00000800)
    39424055    {
    39434056        cbHdr = 3;
    3944         abHdr[1] = 0xc0 | (cb >> 6);
    3945         abHdr[2] = 0x80 | (cb & 0x3f);
     4057        abHdr[1] = (uint8_t)(0xc0 | (cb >> 6));
     4058        abHdr[2] = (uint8_t)(0x80 | (cb & 0x3f));
    39464059    }
    39474060    else if (cb < 0x00010000)
    39484061    {
    39494062        cbHdr = 4;
    3950         abHdr[1] = 0xe0 | (cb >> 12);
    3951         abHdr[2] = 0x80 | ((cb >> 6) & 0x3f);
    3952         abHdr[3] = 0x80 | (cb & 0x3f);
     4063        abHdr[1] = (uint8_t)(0xe0 | (cb >> 12));
     4064        abHdr[2] = (uint8_t)(0x80 | ((cb >> 6) & 0x3f));
     4065        abHdr[3] = (uint8_t)(0x80 | (cb & 0x3f));
    39534066    }
    39544067    else if (cb < 0x00200000)
    39554068    {
    39564069        cbHdr = 5;
    3957         abHdr[1] = 0xf0 |  (cb >> 18);
    3958         abHdr[2] = 0x80 | ((cb >> 12) & 0x3f);
    3959         abHdr[3] = 0x80 | ((cb >>  6) & 0x3f);
    3960         abHdr[4] = 0x80 |  (cb        & 0x3f);
     4070        abHdr[1] = (uint8_t)(0xf0 |  (cb >> 18));
     4071        abHdr[2] = (uint8_t)(0x80 | ((cb >> 12) & 0x3f));
     4072        abHdr[3] = (uint8_t)(0x80 | ((cb >>  6) & 0x3f));
     4073        abHdr[4] = (uint8_t)(0x80 |  (cb        & 0x3f));
    39614074    }
    39624075    else if (cb < 0x04000000)
    39634076    {
    39644077        cbHdr = 6;
    3965         abHdr[1] = 0xf8 |  (cb >> 24);
    3966         abHdr[2] = 0x80 | ((cb >> 18) & 0x3f);
    3967         abHdr[3] = 0x80 | ((cb >> 12) & 0x3f);
    3968         abHdr[4] = 0x80 | ((cb >>  6) & 0x3f);
    3969         abHdr[5] = 0x80 |  (cb        & 0x3f);
     4078        abHdr[1] = (uint8_t)(0xf8 |  (cb >> 24));
     4079        abHdr[2] = (uint8_t)(0x80 | ((cb >> 18) & 0x3f));
     4080        abHdr[3] = (uint8_t)(0x80 | ((cb >> 12) & 0x3f));
     4081        abHdr[4] = (uint8_t)(0x80 | ((cb >>  6) & 0x3f));
     4082        abHdr[5] = (uint8_t)(0x80 |  (cb        & 0x3f));
    39704083    }
    39714084    else if (cb <= 0x7fffffff)
    39724085    {
    39734086        cbHdr = 7;
    3974         abHdr[1] = 0xfc | (cb >> 30);
    3975         abHdr[2] = 0x80 | ((cb >> 24) & 0x3f);
    3976         abHdr[3] = 0x80 | ((cb >> 18) & 0x3f);
    3977         abHdr[4] = 0x80 | ((cb >> 12) & 0x3f);
    3978         abHdr[5] = 0x80 | ((cb >> 6) & 0x3f);
    3979         abHdr[6] = 0x80 | (cb & 0x3f);
     4087        abHdr[1] = (uint8_t)(0xfc |  (cb >> 30));
     4088        abHdr[2] = (uint8_t)(0x80 | ((cb >> 24) & 0x3f));
     4089        abHdr[3] = (uint8_t)(0x80 | ((cb >> 18) & 0x3f));
     4090        abHdr[4] = (uint8_t)(0x80 | ((cb >> 12) & 0x3f));
     4091        abHdr[5] = (uint8_t)(0x80 | ((cb >>  6) & 0x3f));
     4092        abHdr[6] = (uint8_t)(0x80 | (cb & 0x3f));
    39804093    }
    39814094    else
     
    41614274    {
    41624275        memcpy(&pSSM->u.Write.abDataBuffer[0], pvBuf, cbBuf);
    4163         pSSM->u.Write.offDataBuffer = cbBuf;
     4276        pSSM->u.Write.offDataBuffer = (uint32_t)cbBuf;
    41644277    }
    41654278    return rc;
     
    41904303
    41914304    memcpy(&pSSM->u.Write.abDataBuffer[off], pvBuf, cbBuf);
    4192     pSSM->u.Write.offDataBuffer = off + cbBuf;
     4305    pSSM->u.Write.offDataBuffer = off + (uint32_t)cbBuf;
    41934306    return VINF_SUCCESS;
    41944307}
     
    48154928 * @param   pcbDecompr      Where to store the size of the decompressed data.
    48164929 */
    4817 DECLINLINE(int) ssmR3DataReadV2RawLzfHdr(PSSMHANDLE pSSM, size_t *pcbDecompr)
     4930DECLINLINE(int) ssmR3DataReadV2RawLzfHdr(PSSMHANDLE pSSM, uint32_t *pcbDecompr)
    48184931{
    48194932    *pcbDecompr = 0; /* shuts up gcc. */
     
    48314944    pSSM->u.Read.cbRecLeft -= sizeof(cKB);
    48324945
    4833     size_t cbDecompr = (size_t)cKB * _1K;
     4946    uint32_t cbDecompr = (uint32_t)cKB * _1K;
    48344947    AssertLogRelMsgReturn(   cbDecompr >= pSSM->u.Read.cbRecLeft
    48354948                          && cbDecompr <= RT_SIZEOFMEMB(SSMHANDLE, u.Read.abDataBuffer),
     
    48925005     * Read the two mandatory bytes.
    48935006     */
    4894     uint32_t u32StreamCRC = ssmR3StrmCurCRC(pSSM); /** @todo NOT good. */
     5007    uint32_t u32StreamCRC = ssmR3StrmCurCRC(&pSSM->Strm); /** @todo NOT good. */
    48955008    uint8_t  abHdr[8];
    48965009    int rc = ssmR3DataReadV2Raw(pSSM, abHdr, 2);
     
    50685181        Assert(cbBuf > cbToCopy);
    50695182        memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[off], cbToCopy);
    5070         pvBuf = (uint8_t *)pvBuf + cbToCopy;
     5183        pvBuf  = (uint8_t *)pvBuf + cbToCopy;
    50715184        cbBuf -= cbToCopy;
    5072         pSSM->u.Read.cbDataBuffer = 0;
     5185        pSSM->u.Read.cbDataBuffer  = 0;
    50735186        pSSM->u.Read.offDataBuffer = 0;
    50745187    }
     
    50935206         * Read data from the current record.
    50945207         */
    5095         size_t cbToRead;
     5208        uint32_t cbToRead;
    50965209        switch (pSSM->u.Read.u8TypeAndFlags & SSM_REC_TYPE_MASK)
    50975210        {
    50985211            case SSM_REC_TYPE_RAW:
    50995212            {
    5100                 cbToRead = RT_MIN(cbBuf, pSSM->u.Read.cbRecLeft);
     5213                cbToRead = (uint32_t)RT_MIN(cbBuf, pSSM->u.Read.cbRecLeft);
    51015214                int rc = ssmR3DataReadV2Raw(pSSM, pvBuf, cbToRead);
    51025215                if (RT_FAILURE(rc))
     
    51085221            case SSM_REC_TYPE_RAW_LZF:
    51095222            {
    5110                 size_t cbDecompr;
     5223                uint32_t cbDecompr;
    51115224                int rc = ssmR3DataReadV2RawLzfHdr(pSSM, &cbDecompr);
    51125225                if (RT_FAILURE(rc))
     
    51255238                    if (RT_FAILURE(rc))
    51265239                        return rc;
    5127                     memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[0], cbBuf);
    5128                     pSSM->u.Read.cbDataBuffer = cbDecompr;
    5129                     pSSM->u.Read.offDataBuffer = cbBuf;
    5130                     cbToRead = cbBuf;
     5240                    cbToRead = (uint32_t)cbBuf;
     5241                    memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[0], cbToRead);
     5242                    pSSM->u.Read.cbDataBuffer  = cbDecompr;
     5243                    pSSM->u.Read.offDataBuffer = cbToRead;
    51315244                }
    51325245                break;
     
    51705283        Assert(cbBuf > cbToCopy);
    51715284        memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[off], cbToCopy);
    5172         pvBuf = (uint8_t *)pvBuf + cbToCopy;
     5285        pvBuf  = (uint8_t *)pvBuf + cbToCopy;
    51735286        cbBuf -= cbToCopy;
    5174         pSSM->u.Read.cbDataBuffer = 0;
     5287        pSSM->u.Read.cbDataBuffer  = 0;
    51755288        pSSM->u.Read.offDataBuffer = 0;
    51765289    }
     
    51965309         * LATER: optimize by reading directly into the output buffer for some cases.
    51975310         */
    5198         size_t cbToRead;
     5311        uint32_t cbToRead;
    51995312        switch (pSSM->u.Read.u8TypeAndFlags & SSM_REC_TYPE_MASK)
    52005313        {
     
    52055318                if (RT_FAILURE(rc))
    52065319                    return pSSM->rc = rc;
    5207                 pSSM->u.Read.cbRecLeft -= cbToRead;
     5320                pSSM->u.Read.cbRecLeft   -= cbToRead;
    52085321                pSSM->u.Read.cbDataBuffer = cbToRead;
    52095322                break;
     
    52125325            case SSM_REC_TYPE_RAW_LZF:
    52135326            {
    5214                 size_t cbDecompr;
     5327                uint32_t cbDecompr;
    52155328                int rc = ssmR3DataReadV2RawLzfHdr(pSSM, &cbDecompr);
    52165329                if (RT_FAILURE(rc))
     
    52325345         * Copy data from the buffer.
    52335346         */
    5234         size_t cbToCopy = RT_MIN(cbBuf, cbToRead);
     5347        uint32_t cbToCopy = (uint32_t)RT_MIN(cbBuf, cbToRead);
    52355348        memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[0], cbToCopy);
    52365349        cbBuf -= cbToCopy;
     
    52765389
    52775390    memcpy(pvBuf, &pSSM->u.Read.abDataBuffer[off], cbBuf);
    5278     pSSM->u.Read.offDataBuffer = off + cbBuf;
     5391    pSSM->u.Read.offDataBuffer = off + (uint32_t)cbBuf;
    52795392    Log4((cbBuf
    52805393          ? "ssmR3DataRead: %08llx|%08llx/%08x/%08x: cbBuf=%#x %.*Rhxs%s\n"
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