1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/filecompare.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,112 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +/**
1.23 + @file
1.24 + @internalTechnology
1.25 +*/
1.26 +#include "filecompare.h"
1.27 +
1.28 +#include "cryptospi/cryptospidef.h"
1.29 +#include <e32std.h>
1.30 +#include <e32def.h>
1.31 +#include <f32file.h>
1.32 +
1.33 +const TInt KCompareBlockSize = 0xff;
1.34 +
1.35 +//const TInt KCompareEqual = 0;
1.36 +const TInt KCompareGreaterThan = 1;
1.37 +const TInt KCompareLessThan = -1;
1.38 +
1.39 +// a simple utility class to compare 2 files, returns
1.40 +TInt TFileCompare::CompareL(TPtrC aFile1Path, TPtrC aFile2Path)
1.41 + {
1.42 + // assume is all well, compare always
1.43 + TInt retVal = 0;
1.44 +
1.45 + RFs rFs;
1.46 + RFile file1;
1.47 + RFile file2;
1.48 +
1.49 + User::LeaveIfError(rFs.Connect());
1.50 + CleanupClosePushL(rFs);
1.51 + User::LeaveIfError(file1.Open(rFs, aFile1Path, EFileRead));
1.52 + CleanupClosePushL(file1);
1.53 + User::LeaveIfError(file2.Open(rFs, aFile2Path, EFileRead));
1.54 + CleanupClosePushL(file2);
1.55 +
1.56 + TInt file1Size;
1.57 + file1.Size(file1Size);
1.58 +
1.59 + TInt file2Size;
1.60 + file2.Size(file2Size);
1.61 +
1.62 + // bail out if the sizes of the 2 files are different
1.63 + if(file1Size == file2Size)
1.64 + {
1.65 +
1.66 + TBuf8<KCompareBlockSize> tempBuf1;
1.67 + TBuf8<KCompareBlockSize> tempBuf2;
1.68 +
1.69 + TInt compareBlockSize;
1.70 + TInt loopCount;
1.71 +
1.72 + if(file1Size > KCompareBlockSize)
1.73 + {
1.74 + compareBlockSize = KCompareBlockSize;
1.75 + loopCount = file1Size / compareBlockSize;
1.76 + }
1.77 + else
1.78 + {
1.79 + loopCount = 1;
1.80 + compareBlockSize = file1Size;
1.81 + }
1.82 +
1.83 +
1.84 + // read through the files comparing each block
1.85 + for(TInt i = 0; i < loopCount; i++)
1.86 + {
1.87 +
1.88 + file1.Read(tempBuf1, compareBlockSize);
1.89 + file2.Read(tempBuf2, compareBlockSize);
1.90 +
1.91 + // remember, 0 is return value for a successful comparison
1.92 +
1.93 + retVal = tempBuf1.Compare(tempBuf2);
1.94 + if( retVal != 0 )
1.95 + {
1.96 + //if files fail to match, break out of the loop
1.97 + break;
1.98 + }
1.99 + }
1.100 + }
1.101 + else
1.102 + {
1.103 + if(file1Size > file2Size)
1.104 + retVal = KCompareGreaterThan;
1.105 + else
1.106 + retVal = KCompareLessThan;
1.107 + }
1.108 +
1.109 + CleanupStack::PopAndDestroy(&file2);
1.110 + CleanupStack::PopAndDestroy(&file1);
1.111 +
1.112 + CleanupStack::PopAndDestroy(&rFs);
1.113 +
1.114 + return retVal;
1.115 + }