VirtualBox

Ignore:
Timestamp:
Apr 8, 2010 9:38:35 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
59835
Message:

Runtime/Sg: Add advanced compare method

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/misc/sg.cpp

    r28116 r28117  
    177177
    178178
     179RTDECL(int) RTSgBufCmpEx(PRTSGBUF pSgBuf1, PRTSGBUF pSgBuf2, size_t cbCmp,
     180                         size_t *pcbOff, bool fAdvance)
     181{
     182    AssertPtrReturn(pSgBuf1, 0);
     183    AssertPtrReturn(pSgBuf2, 0);
     184
     185    size_t cbLeft = cbCmp;
     186    size_t cbOff  = 0;
     187    RTSGBUF SgBuf1Tmp;
     188    RTSGBUF SgBuf2Tmp;
     189    PRTSGBUF pSgBuf1Tmp;
     190    PRTSGBUF pSgBuf2Tmp;
     191
     192    if (!fAdvance)
     193    {
     194        /* Set up the temporary buffers */
     195        RTSgBufClone(&SgBuf1Tmp, pSgBuf1);
     196        RTSgBufClone(&SgBuf2Tmp, pSgBuf2);
     197        pSgBuf1Tmp = &SgBuf1Tmp;
     198        pSgBuf2Tmp = &SgBuf2Tmp;
     199    }
     200    else
     201    {
     202        pSgBuf1Tmp = pSgBuf1;
     203        pSgBuf2Tmp = pSgBuf2;
     204    }
     205
     206    while (cbLeft)
     207    {
     208        size_t cbThisCmp = RT_MIN(RT_MIN(pSgBuf1Tmp->cbSegLeft, cbLeft), pSgBuf2Tmp->cbSegLeft);
     209        size_t cbTmp = cbThisCmp;
     210        uint8_t *pbBuf1;
     211        uint8_t *pbBuf2;
     212
     213        if (!cbCmp)
     214            break;
     215
     216        pbBuf1 = (uint8_t *)sgBufGet(pSgBuf1Tmp, &cbTmp);
     217        Assert(cbTmp == cbThisCmp);
     218        pbBuf2 = (uint8_t *)sgBufGet(pSgBuf2Tmp, &cbTmp);
     219        Assert(cbTmp == cbThisCmp);
     220
     221        int rc = memcmp(pbBuf1, pbBuf2, cbThisCmp);
     222        if (rc)
     223        {
     224            if (pcbOff)
     225            {
     226                /* Search for the correct offset */
     227                while (   cbThisCmp-- > 0
     228                       && (*pbBuf1 == *pbBuf2))
     229                {
     230                    pbBuf1++;
     231                    pbBuf2++;
     232                    cbOff++;
     233                }
     234
     235                *pcbOff = cbOff;
     236            }
     237            return rc;
     238        }
     239
     240        cbLeft -= cbThisCmp;
     241        cbOff  += cbThisCmp;
     242    }
     243
     244    return 0;
     245}
     246
     247
    179248RTDECL(size_t) RTSgBufSet(PRTSGBUF pSgBuf, uint8_t ubFill, size_t cbSet)
    180249{
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