os/security/securityanddataprivacytools/securitytools/certapp/utils/filestream.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/securityanddataprivacytools/securitytools/certapp/utils/filestream.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,230 @@
     1.4 +/*
     1.5 +* Copyright (c) 2008-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 +#include "filestream_private.h"
    1.23 +#include "logger.h"
    1.24 +#include "stringconv.h"
    1.25 +#include "utils.h"
    1.26 +
    1.27 +FileStreamBuf::FileStreamBuf(const std::string &aFileName, bool aWritable, bool aSpecialTextHandling)
    1.28 +	: iWritable(aWritable), iSpecialTextHandling(aSpecialTextHandling)
    1.29 +{
    1.30 +	if(iWritable)
    1.31 +		{
    1.32 +		OpenUtf8FStreamForWrite(iFile, aFileName.c_str());
    1.33 +		}
    1.34 +	else
    1.35 +		{
    1.36 +		OpenUtf8FStreamForRead(iFile, aFileName.c_str());
    1.37 +		}
    1.38 +	if(iFile.fail())
    1.39 +		{
    1.40 +		dbg << Log::Indent() << "Failed to open '" << aFileName << "' for " << ((iWritable)?("output"):("input")) << "!" << Log::Endl();
    1.41 +		FatalError();
    1.42 +		}
    1.43 +	
    1.44 +}
    1.45 +
    1.46 +static const char utf8Header[] = 
    1.47 +	{
    1.48 +		0xef, 0xbb, 0xbf
    1.49 +	};
    1.50 +
    1.51 +static const char utf16HeaderBig[] = 
    1.52 +	{
    1.53 +		0xfe, 0xff
    1.54 +	};
    1.55 +
    1.56 +static const char utf16HeaderLittle[] = 
    1.57 +	{
    1.58 +		0xff, 0xfe
    1.59 +	};
    1.60 +
    1.61 +void FileStreamBuf::StripUtf8HeaderIfPresent()
    1.62 +{
    1.63 +	if(iWritable)
    1.64 +		{
    1.65 +		FatalError(); // Coding error
    1.66 +		}
    1.67 +
    1.68 +	std::streampos savedPos = iFile.tellg();
    1.69 +	char header[3];
    1.70 +	iFile.read(header, sizeof(header));
    1.71 +	
    1.72 +	if(iFile.good() && memcmp(header, utf8Header, sizeof(utf8Header)) == 0)
    1.73 +		{
    1.74 +		// We read a UTF8 file header so simply return (and thereby skip it).
    1.75 +		return;
    1.76 +		}
    1.77 +
    1.78 +	if(iFile.good() && memcmp(header, utf16HeaderBig, sizeof(utf16HeaderBig)) == 0)
    1.79 +		{
    1.80 +		dbg << Log::Indent() << "Input file is Big Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
    1.81 +		FatalError();
    1.82 +		}
    1.83 +	if(iFile.good() && memcmp(header, utf16HeaderLittle, sizeof(utf16HeaderLittle)) == 0)
    1.84 +		{
    1.85 +		dbg << Log::Indent() << "Input file is Little Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
    1.86 +		FatalError();
    1.87 +		}
    1.88 +
    1.89 +	iFile.clear();
    1.90 +	iFile.seekg(savedPos);
    1.91 +}
    1.92 +
    1.93 +
    1.94 +TInt FileStreamBuf::DoReadL(TAny *aPtr,TInt aMaxLength)
    1.95 +{
    1.96 +BULLSEYE_OFF
    1.97 +	if(iFile.fail())
    1.98 +		{
    1.99 +		dbg << Log::Indent() << "Read error" << Log::Endl();
   1.100 +		FatalError(); // Read on broken stream
   1.101 +		}
   1.102 +BULLSEYE_RESTORE
   1.103 +
   1.104 +	iFile.read((char *)aPtr, aMaxLength);
   1.105 +    
   1.106 +	if(iFile.eof())
   1.107 +		{
   1.108 +		dbg << Log::Indent() << "Encountered EOF" << Log::Endl();
   1.109 +		TInt len = iFile.gcount();
   1.110 +		if(iSpecialTextHandling)
   1.111 +			{
   1.112 +			// Add a synthetic NL to the returned data to handle a
   1.113 +			// token immediately before the EOF
   1.114 +			((TUint8 *)aPtr)[len++] = '\n';
   1.115 +			}
   1.116 +		return len;
   1.117 +		}
   1.118 +
   1.119 +BULLSEYE_OFF
   1.120 +	if(iFile.fail())
   1.121 +		{
   1.122 +		dbg << Log::Indent() << "Read error" << Log::Endl();
   1.123 +		FatalError(); // error other than EOF
   1.124 +		}
   1.125 +BULLSEYE_RESTORE
   1.126 +
   1.127 +	return iFile.gcount();
   1.128 +}
   1.129 +
   1.130 +void FileStreamBuf::DoWriteL(const TUint8* aPtr,TInt aLength)
   1.131 +{
   1.132 +	iFile.write((const char *)aPtr, aLength);
   1.133 +BULLSEYE_OFF
   1.134 +	if(iFile.fail())
   1.135 +		{
   1.136 +		FatalError(); // error other than EOF
   1.137 +		}
   1.138 +BULLSEYE_RESTORE
   1.139 +	//#warning "flush all data for debugging"
   1.140 +	//	iFile.flush();
   1.141 +}
   1.142 +
   1.143 +TStreamPos FileStreamBuf::DoSeekL(TMark aMark,TStreamLocation aLocation,TInt anOffset)
   1.144 +{
   1.145 +	std::ios_base::seekdir dir;
   1.146 +	switch(aLocation)
   1.147 +		{
   1.148 +		case EStreamBeginning:
   1.149 +			dir = std::ios_base::beg;
   1.150 +			break;
   1.151 +		case EStreamMark:
   1.152 +			dir = std::ios_base::cur;
   1.153 +			break;
   1.154 +		case EStreamEnd:
   1.155 +			dir = std::ios_base::end;
   1.156 +			break;
   1.157 +BULLSEYE_OFF
   1.158 +		default:
   1.159 +			FatalError();
   1.160 +BULLSEYE_RESTORE
   1.161 +		}
   1.162 +
   1.163 +	if(aMark == ERead)
   1.164 +		{
   1.165 +		iFile.seekg(anOffset, dir);
   1.166 +BULLSEYE_OFF
   1.167 +		if(iFile.fail()) FatalError(); // error other than EOF
   1.168 +BULLSEYE_RESTORE
   1.169 +		return iFile.tellg();
   1.170 +		}
   1.171 +
   1.172 +	if(aMark == EWrite)
   1.173 +		{
   1.174 +		iFile.seekp(anOffset, dir);
   1.175 +BULLSEYE_OFF
   1.176 +		if(iFile.fail()) FatalError(); // error other than EOF
   1.177 +BULLSEYE_RESTORE
   1.178 +		return iFile.tellp();
   1.179 +		}
   1.180 +	
   1.181 +	FatalError();
   1.182 +	return -1;
   1.183 +}
   1.184 +
   1.185 +void FileStreamBuf::DoRelease()
   1.186 +{
   1.187 +	iFile.close();
   1.188 +}
   1.189 +
   1.190 +FileWriteStream::FileWriteStream(const std::string &aFileName)
   1.191 +	: RWriteStream()
   1.192 +{
   1.193 +	iSnk = new FileStreamBuf(aFileName, true);
   1.194 +}
   1.195 +
   1.196 +FileWriteStream::~FileWriteStream()
   1.197 +{
   1.198 +BULLSEYE_OFF
   1.199 +	if(iSnk)
   1.200 +		{
   1.201 +		dbg << Log::Indent() << "forgot to close FileWriteStream" << Log::Endl();
   1.202 +		FatalError();
   1.203 +		}
   1.204 +BULLSEYE_RESTORE
   1.205 +	iSnk = 0;
   1.206 +}
   1.207 +
   1.208 +
   1.209 +
   1.210 +FileReadStream::FileReadStream(const std::string &aFileName, bool aSpecialTextHandling)
   1.211 +	: RReadStream()
   1.212 +{
   1.213 +	iSrc = new FileStreamBuf(aFileName, false, aSpecialTextHandling);
   1.214 +	if(aSpecialTextHandling)
   1.215 +		{
   1.216 +		static_cast<FileStreamBuf *>(iSrc)->StripUtf8HeaderIfPresent();
   1.217 +		}
   1.218 +}
   1.219 +
   1.220 +
   1.221 +FileReadStream::~FileReadStream()
   1.222 +{
   1.223 +BULLSEYE_OFF
   1.224 +	if(iSrc)
   1.225 +		{
   1.226 +		dbg << Log::Indent() << "forgot to close FileReadStream" << Log::Endl();
   1.227 +		FatalError();
   1.228 +		}
   1.229 +BULLSEYE_RESTORE
   1.230 +	iSrc = 0;
   1.231 +}
   1.232 +
   1.233 +// End of file