1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/tcryptospi/src/filereader.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,208 @@
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 "filereader.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 +// a simple utility class to read from a text file given the path in its
1.34 +// NewLC / NewL
1.35 +
1.36 +CFileReader* CFileReader::NewL(TPtrC aFilePath)
1.37 + {
1.38 + CFileReader* self=CFileReader::NewLC(aFilePath);
1.39 + CleanupStack::Pop(self);
1.40 + return self;
1.41 + }
1.42 +
1.43 +CFileReader* CFileReader::NewLC(TPtrC aFilePath)
1.44 + {
1.45 + CFileReader* self=new(ELeave) CFileReader();
1.46 + CleanupStack::PushL(self);
1.47 + self->ConstructL(aFilePath);
1.48 + return self;
1.49 + }
1.50 +
1.51 +
1.52 +CFileReader* CFileReader::NewL(TPtrC aFilePath, TInt aBlockSize)
1.53 + {
1.54 + CFileReader* self=CFileReader::NewLC(aFilePath, aBlockSize);
1.55 + CleanupStack::Pop(self);
1.56 + return self;
1.57 + }
1.58 +
1.59 +CFileReader* CFileReader::NewLC(TPtrC aFilePath, TInt aBlockSize)
1.60 + {
1.61 + CFileReader* self=new(ELeave) CFileReader();
1.62 + CleanupStack::PushL(self);
1.63 + self->ConstructL(aFilePath, aBlockSize);
1.64 + return self;
1.65 + }
1.66 +
1.67 +
1.68 +CFileReader::~CFileReader()
1.69 + {
1.70 + // nulled in constructor so safe to call
1.71 + delete iFileContents;
1.72 +
1.73 + delete iFilePath;
1.74 +
1.75 + iFile.Close();
1.76 + iRfs.Close();
1.77 + }
1.78 +
1.79 +CFileReader::CFileReader() : iFileContents(0), iFilePath(0)
1.80 + {
1.81 + }
1.82 +
1.83 +TBool CFileReader::ReadBlockL()
1.84 + {
1.85 + TInt moreData = EFalse;
1.86 +
1.87 + if(iBlockSize)
1.88 + {
1.89 + delete iFileContents;
1.90 + iFileContents = HBufC8::NewL(iBlockSize);
1.91 + }
1.92 + else
1.93 + {
1.94 + TInt length;
1.95 + iFile.Size(length);
1.96 + delete iFileContents;
1.97 + iFileContents = HBufC8::NewL(length);
1.98 + }
1.99 +
1.100 +
1.101 + TPtr8 ptr = iFileContents->Des();
1.102 +
1.103 + // use appropriate read method depending on whether
1.104 + // we are block reading or reading the whole file;
1.105 + if(iBlockSize)
1.106 + {
1.107 + iFile.Read(iFileOffset, ptr, iBlockSize);
1.108 +
1.109 + TInt size;
1.110 + iFile.Size(size);
1.111 +
1.112 + if(iFileOffset < size)
1.113 + moreData = ETrue;
1.114 +
1.115 + iFileOffset+=iBlockSize;
1.116 + }
1.117 + else
1.118 + {
1.119 + iFile.Read(ptr);
1.120 + }
1.121 +
1.122 +
1.123 + return moreData;
1.124 +
1.125 + }
1.126 +
1.127 +void CFileReader::ConstructL(TPtrC aFilePath)
1.128 + {
1.129 + iFilePath = HBufC::NewL(aFilePath.Length());
1.130 +
1.131 + iFilePath->Des().Copy(aFilePath);
1.132 +
1.133 + iFileOffset = 0;
1.134 +
1.135 + iBlockSize = 0;
1.136 +
1.137 + User::LeaveIfError(iRfs.Connect());
1.138 + User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
1.139 +
1.140 + ReadBlockL();
1.141 + }
1.142 +
1.143 +void CFileReader::ConstructL(TPtrC aFilePath, TInt aBlockSize)
1.144 + {
1.145 + iFilePath = HBufC::NewL(aFilePath.Length());
1.146 +
1.147 + iFilePath->Des().Copy(aFilePath);
1.148 +
1.149 + iFileOffset = 0;
1.150 +
1.151 + iBlockSize = aBlockSize;
1.152 +
1.153 + User::LeaveIfError(iRfs.Connect());
1.154 + User::LeaveIfError(iFile.Open(iRfs, iFilePath->Des(), EFileRead));
1.155 + }
1.156 +
1.157 +CFileReader::operator const TPtrC8()
1.158 + {
1.159 + return iFileContents->Des();
1.160 + }
1.161 +
1.162 +TInt CFileReader::NumBlocks()
1.163 + {
1.164 + TInt numBlocks;
1.165 + TInt fileSize;
1.166 + TInt mod;
1.167 +
1.168 + iFile.Size(fileSize);
1.169 +
1.170 + numBlocks = fileSize / iBlockSize;
1.171 +
1.172 + mod = fileSize % iBlockSize;
1.173 +
1.174 + if(mod)
1.175 + {
1.176 + numBlocks++;
1.177 + }
1.178 +
1.179 + return numBlocks;
1.180 + }
1.181 +
1.182 +RInteger CFileReader::ParseIntegerL()
1.183 + {
1.184 + HBufC8* buf = ParseBinaryL(iFileContents->Des());
1.185 + CleanupStack::PushL(buf);
1.186 + RInteger result = RInteger::NewL(*buf);
1.187 + CleanupStack::PopAndDestroy(buf);
1.188 +
1.189 + return result;
1.190 + }
1.191 +
1.192 +HBufC8* CFileReader::ParseBinaryL(const TDesC8& aDes)
1.193 + {
1.194 + __ASSERT_ALWAYS(aDes.Length() % 2 == 0, User::Panic(_L("ParseBinaryL"), KErrArgument));
1.195 + TInt length = aDes.Length() / 2;
1.196 + HBufC8* buf = HBufC8::NewL(length);
1.197 + TPtr8 ptr = buf->Des();
1.198 + ptr.SetLength(length);
1.199 +
1.200 + for (TInt i = 0 ; i < aDes.Length() ; i += 2)
1.201 + {
1.202 + TUint8 tmp;
1.203 + tmp=(TUint8)(aDes[i]-(aDes[i]>'9'?('A'-10):'0'));
1.204 + tmp*=16;
1.205 + tmp|=(TUint8)(aDes[i+1]-(aDes[i+1]>'9'?('A'-10):'0'));
1.206 + ptr[i / 2] = tmp;
1.207 + }
1.208 +
1.209 + return buf;
1.210 + }
1.211 +