os/security/securityanddataprivacytools/securitytools/certapp/utils/filestream.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include "filestream_private.h"
    20 #include "logger.h"
    21 #include "stringconv.h"
    22 #include "utils.h"
    23 
    24 FileStreamBuf::FileStreamBuf(const std::string &aFileName, bool aWritable, bool aSpecialTextHandling)
    25 	: iWritable(aWritable), iSpecialTextHandling(aSpecialTextHandling)
    26 {
    27 	if(iWritable)
    28 		{
    29 		OpenUtf8FStreamForWrite(iFile, aFileName.c_str());
    30 		}
    31 	else
    32 		{
    33 		OpenUtf8FStreamForRead(iFile, aFileName.c_str());
    34 		}
    35 	if(iFile.fail())
    36 		{
    37 		dbg << Log::Indent() << "Failed to open '" << aFileName << "' for " << ((iWritable)?("output"):("input")) << "!" << Log::Endl();
    38 		FatalError();
    39 		}
    40 	
    41 }
    42 
    43 static const char utf8Header[] = 
    44 	{
    45 		0xef, 0xbb, 0xbf
    46 	};
    47 
    48 static const char utf16HeaderBig[] = 
    49 	{
    50 		0xfe, 0xff
    51 	};
    52 
    53 static const char utf16HeaderLittle[] = 
    54 	{
    55 		0xff, 0xfe
    56 	};
    57 
    58 void FileStreamBuf::StripUtf8HeaderIfPresent()
    59 {
    60 	if(iWritable)
    61 		{
    62 		FatalError(); // Coding error
    63 		}
    64 
    65 	std::streampos savedPos = iFile.tellg();
    66 	char header[3];
    67 	iFile.read(header, sizeof(header));
    68 	
    69 	if(iFile.good() && memcmp(header, utf8Header, sizeof(utf8Header)) == 0)
    70 		{
    71 		// We read a UTF8 file header so simply return (and thereby skip it).
    72 		return;
    73 		}
    74 
    75 	if(iFile.good() && memcmp(header, utf16HeaderBig, sizeof(utf16HeaderBig)) == 0)
    76 		{
    77 		dbg << Log::Indent() << "Input file is Big Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
    78 		FatalError();
    79 		}
    80 	if(iFile.good() && memcmp(header, utf16HeaderLittle, sizeof(utf16HeaderLittle)) == 0)
    81 		{
    82 		dbg << Log::Indent() << "Input file is Little Endian UTF16 - Only UTF-8 and ASCII are supported" << Log::Endl();
    83 		FatalError();
    84 		}
    85 
    86 	iFile.clear();
    87 	iFile.seekg(savedPos);
    88 }
    89 
    90 
    91 TInt FileStreamBuf::DoReadL(TAny *aPtr,TInt aMaxLength)
    92 {
    93 BULLSEYE_OFF
    94 	if(iFile.fail())
    95 		{
    96 		dbg << Log::Indent() << "Read error" << Log::Endl();
    97 		FatalError(); // Read on broken stream
    98 		}
    99 BULLSEYE_RESTORE
   100 
   101 	iFile.read((char *)aPtr, aMaxLength);
   102     
   103 	if(iFile.eof())
   104 		{
   105 		dbg << Log::Indent() << "Encountered EOF" << Log::Endl();
   106 		TInt len = iFile.gcount();
   107 		if(iSpecialTextHandling)
   108 			{
   109 			// Add a synthetic NL to the returned data to handle a
   110 			// token immediately before the EOF
   111 			((TUint8 *)aPtr)[len++] = '\n';
   112 			}
   113 		return len;
   114 		}
   115 
   116 BULLSEYE_OFF
   117 	if(iFile.fail())
   118 		{
   119 		dbg << Log::Indent() << "Read error" << Log::Endl();
   120 		FatalError(); // error other than EOF
   121 		}
   122 BULLSEYE_RESTORE
   123 
   124 	return iFile.gcount();
   125 }
   126 
   127 void FileStreamBuf::DoWriteL(const TUint8* aPtr,TInt aLength)
   128 {
   129 	iFile.write((const char *)aPtr, aLength);
   130 BULLSEYE_OFF
   131 	if(iFile.fail())
   132 		{
   133 		FatalError(); // error other than EOF
   134 		}
   135 BULLSEYE_RESTORE
   136 	//#warning "flush all data for debugging"
   137 	//	iFile.flush();
   138 }
   139 
   140 TStreamPos FileStreamBuf::DoSeekL(TMark aMark,TStreamLocation aLocation,TInt anOffset)
   141 {
   142 	std::ios_base::seekdir dir;
   143 	switch(aLocation)
   144 		{
   145 		case EStreamBeginning:
   146 			dir = std::ios_base::beg;
   147 			break;
   148 		case EStreamMark:
   149 			dir = std::ios_base::cur;
   150 			break;
   151 		case EStreamEnd:
   152 			dir = std::ios_base::end;
   153 			break;
   154 BULLSEYE_OFF
   155 		default:
   156 			FatalError();
   157 BULLSEYE_RESTORE
   158 		}
   159 
   160 	if(aMark == ERead)
   161 		{
   162 		iFile.seekg(anOffset, dir);
   163 BULLSEYE_OFF
   164 		if(iFile.fail()) FatalError(); // error other than EOF
   165 BULLSEYE_RESTORE
   166 		return iFile.tellg();
   167 		}
   168 
   169 	if(aMark == EWrite)
   170 		{
   171 		iFile.seekp(anOffset, dir);
   172 BULLSEYE_OFF
   173 		if(iFile.fail()) FatalError(); // error other than EOF
   174 BULLSEYE_RESTORE
   175 		return iFile.tellp();
   176 		}
   177 	
   178 	FatalError();
   179 	return -1;
   180 }
   181 
   182 void FileStreamBuf::DoRelease()
   183 {
   184 	iFile.close();
   185 }
   186 
   187 FileWriteStream::FileWriteStream(const std::string &aFileName)
   188 	: RWriteStream()
   189 {
   190 	iSnk = new FileStreamBuf(aFileName, true);
   191 }
   192 
   193 FileWriteStream::~FileWriteStream()
   194 {
   195 BULLSEYE_OFF
   196 	if(iSnk)
   197 		{
   198 		dbg << Log::Indent() << "forgot to close FileWriteStream" << Log::Endl();
   199 		FatalError();
   200 		}
   201 BULLSEYE_RESTORE
   202 	iSnk = 0;
   203 }
   204 
   205 
   206 
   207 FileReadStream::FileReadStream(const std::string &aFileName, bool aSpecialTextHandling)
   208 	: RReadStream()
   209 {
   210 	iSrc = new FileStreamBuf(aFileName, false, aSpecialTextHandling);
   211 	if(aSpecialTextHandling)
   212 		{
   213 		static_cast<FileStreamBuf *>(iSrc)->StripUtf8HeaderIfPresent();
   214 		}
   215 }
   216 
   217 
   218 FileReadStream::~FileReadStream()
   219 {
   220 BULLSEYE_OFF
   221 	if(iSrc)
   222 		{
   223 		dbg << Log::Indent() << "forgot to close FileReadStream" << Log::Endl();
   224 		FatalError();
   225 		}
   226 BULLSEYE_RESTORE
   227 	iSrc = 0;
   228 }
   229 
   230 // End of file