Exemple #1
0
hpatch_BOOL patch(TByte* out_newData,TByte* out_newData_end,
            const TByte* oldData,const TByte* oldData_end,
            const TByte* serializedDiff,const TByte* serializedDiff_end){
    const TByte *code_lengths, *code_lengths_end,
                *code_inc_newPos, *code_inc_newPos_end,
                *code_inc_oldPos, *code_inc_oldPos_end,
                *code_newDataDiff, *code_newDataDiff_end;
    TUInt       ctrlCount;

    assert(out_newData<=out_newData_end);
    assert(oldData<=oldData_end);
    assert(serializedDiff<=serializedDiff_end);
    ctrlCount=unpackUInt(&serializedDiff, serializedDiff_end);
    {   //head
        TUInt lengthSize=unpackUInt(&serializedDiff, serializedDiff_end);
        TUInt inc_newPosSize=unpackUInt(&serializedDiff, serializedDiff_end);
        TUInt inc_oldPosSize=unpackUInt(&serializedDiff, serializedDiff_end);
        TUInt newDataDiffSize=unpackUInt(&serializedDiff, serializedDiff_end);
#ifdef __RUN_MEM_SAFE_CHECK
        if (lengthSize>(TUInt)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_lengths=serializedDiff;     serializedDiff+=lengthSize;
        code_lengths_end=serializedDiff;
#ifdef __RUN_MEM_SAFE_CHECK
        if (inc_newPosSize>(TUInt)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_inc_newPos=serializedDiff; serializedDiff+=inc_newPosSize;
        code_inc_newPos_end=serializedDiff;
#ifdef __RUN_MEM_SAFE_CHECK
        if (inc_oldPosSize>(TUInt)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_inc_oldPos=serializedDiff; serializedDiff+=inc_oldPosSize;
        code_inc_oldPos_end=serializedDiff;
#ifdef __RUN_MEM_SAFE_CHECK
        if (newDataDiffSize>(TUInt)(serializedDiff_end-serializedDiff)) return _hpatch_FALSE;
#endif
        code_newDataDiff=serializedDiff; serializedDiff+=newDataDiffSize;
        code_newDataDiff_end=serializedDiff;
    }

    //decode rle ; rle data begin==serializedDiff;
    if (!_bytesRle_load(out_newData, out_newData_end, serializedDiff, serializedDiff_end))
        return _hpatch_FALSE;

    {   //patch
        const TUInt newDataSize=(TUInt)(out_newData_end-out_newData);
        TUInt oldPosBack=0;
        TUInt newPosBack=0;
        TUInt i;
        for (i=0; i<ctrlCount; ++i){
            TUInt oldPos,inc_oldPos,inc_oldPos_sign;

            TUInt copyLength=unpackUInt(&code_inc_newPos, code_inc_newPos_end);
            TUInt addLength=unpackUInt(&code_lengths, code_lengths_end);
#ifdef __RUN_MEM_SAFE_CHECK
            if (code_inc_oldPos>=code_inc_oldPos_end) return _hpatch_FALSE;
#endif
            inc_oldPos_sign=(*code_inc_oldPos)>>(8-1);
            inc_oldPos=unpackUIntWithTag(&code_inc_oldPos, code_inc_oldPos_end, 1);
            if (inc_oldPos_sign==0)
                oldPos=oldPosBack+inc_oldPos;
            else
                oldPos=oldPosBack-inc_oldPos;
            if (copyLength>0){
#ifdef __RUN_MEM_SAFE_CHECK
                if (copyLength>(TUInt)(newDataSize-newPosBack)) return _hpatch_FALSE;
                if (copyLength>(TUInt)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
#endif
                memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
                code_newDataDiff+=copyLength;
                newPosBack+=copyLength;
            }
#ifdef __RUN_MEM_SAFE_CHECK
            if ((addLength>(TUInt)(newDataSize-newPosBack))) return _hpatch_FALSE;
            if ((oldPos>(TUInt)(oldData_end-oldData))||(addLength>(TUInt)(oldData_end-oldData-oldPos))) return _hpatch_FALSE;
#endif
            addData(out_newData+newPosBack,oldData+oldPos,addLength);
            oldPosBack=oldPos;
            newPosBack+=addLength;
        }

        if (newPosBack<newDataSize){
            TUInt copyLength=newDataSize-newPosBack;
#ifdef __RUN_MEM_SAFE_CHECK
            if (copyLength>(TUInt)(code_newDataDiff_end-code_newDataDiff)) return _hpatch_FALSE;
#endif
            memcpy(out_newData+newPosBack,code_newDataDiff,copyLength);
            code_newDataDiff+=copyLength;
            newPosBack=newDataSize;
        }
    }

    if (  (code_lengths==code_lengths_end)
        &&(code_inc_newPos==code_inc_newPos_end)
        &&(code_inc_oldPos==code_inc_oldPos_end)
        &&(code_newDataDiff==code_newDataDiff_end))
        return !hpatch_FALSE;
    else
        return _hpatch_FALSE;
}
Exemple #2
0
hpatch_BOOL patch(TByte* newData,TByte* newData_end,
            const TByte* oldData,const TByte* oldData_end,
            const TByte* serializedDiff,const TByte* serializedDiff_end){
    TUInt32 ctrlCount,lengthSize,inc_newPosSize,inc_oldPosSize,newDataDiffSize;
    const TByte *code_length,*code_length_end, *code_inc_newPos,*code_inc_newPos_end,
                *code_inc_oldPos,*code_inc_oldPos_end, *code_newDataDiff,*code_newDataDiff_end;
    TUInt32 i,oldPosBack,newPos_end,inc_newPos,newPos,addLength,copyLength,oldPos,inc_oldPos,newDataSize;
    int     inc_oldPos_sign;
    
    ctrlCount=unpack32Bit(&serializedDiff, serializedDiff_end);
    lengthSize=unpack32Bit(&serializedDiff, serializedDiff_end);
    inc_newPosSize=unpack32Bit(&serializedDiff, serializedDiff_end);
    inc_oldPosSize=unpack32Bit(&serializedDiff, serializedDiff_end);
    newDataDiffSize=unpack32Bit(&serializedDiff, serializedDiff_end);
#ifdef PATCH_RUN_MEM_SAFE_CHECK
    if (lengthSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
    code_length=serializedDiff;     serializedDiff+=lengthSize;
    code_length_end=serializedDiff;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
    if (inc_newPosSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
    code_inc_newPos=serializedDiff; serializedDiff+=inc_newPosSize;
    code_inc_newPos_end=serializedDiff;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
    if (inc_oldPosSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
    code_inc_oldPos=serializedDiff; serializedDiff+=inc_oldPosSize;
    code_inc_oldPos_end=serializedDiff;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
    if (newDataDiffSize>(TUInt32)(serializedDiff_end-serializedDiff)) return hpatch_FALSE;
#endif
    code_newDataDiff=serializedDiff; serializedDiff+=newDataDiffSize;
    code_newDataDiff_end=serializedDiff;
    
    //rle data begin==serializedDiff;
    if (!_bytesRle_load(newData, newData_end, serializedDiff, serializedDiff_end))
        return hpatch_FALSE;

    oldPosBack=0;
    newPos_end=0;
    for (i=0; i<ctrlCount; ++i){
        inc_newPos=unpack32Bit(&code_inc_newPos, code_inc_newPos_end);
        newPos=newPos_end+inc_newPos;
        addLength=unpack32Bit(&code_length, code_length_end);
        inc_oldPos_sign=(*code_inc_oldPos)>>(8-1);
        inc_oldPos=unpack32BitWithTag(&code_inc_oldPos, code_inc_oldPos_end, 1);
        if (inc_oldPos_sign==0)
            oldPos=oldPosBack+inc_oldPos;
        else
            oldPos=oldPosBack-inc_oldPos;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
        if ((oldPos>(TUInt32)(oldData_end-oldData))||(addLength>(TUInt32)(oldData_end-oldData-oldPos))) return hpatch_FALSE;
        if ((newPos>(TUInt32)(newData_end-newData))||(addLength>(TUInt32)(newData_end-newData-newPos))) return hpatch_FALSE;
#endif
        if (newPos>newPos_end){
            copyLength=newPos-newPos_end;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
            if (copyLength>(TUInt32)(code_newDataDiff_end-code_newDataDiff)) return hpatch_FALSE;
#endif
            memcpy(newData+newPos_end,code_newDataDiff,copyLength);
            code_newDataDiff+=copyLength;
        }
        addData(newData+newPos,oldData+oldPos,addLength);
        oldPosBack=oldPos;
        newPos_end=newPos+addLength;
    }
    
    newDataSize=(TUInt32)(newData_end-newData);
    if (newPos_end<newDataSize){
        copyLength=newDataSize-newPos_end;
#ifdef PATCH_RUN_MEM_SAFE_CHECK
        if (copyLength>(TUInt32)(code_newDataDiff_end-code_newDataDiff)) return hpatch_FALSE;
#endif
        memcpy(newData+newPos_end,code_newDataDiff,copyLength);
        code_newDataDiff+=copyLength;
        newPos_end=newDataSize;
    }
    return (code_length==code_length_end)&&(code_inc_newPos==code_inc_newPos_end)
            &&(code_inc_oldPos==code_inc_oldPos_end)&&(code_newDataDiff==code_newDataDiff_end);
}