persistentstorage/dbms/pcdbms/tdbms/src/crccheck.cpp
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "crccheck.h"
       
    17 
       
    18 const TInt TDBMS_CRCChecks::SetSessionPath(const TDesC& aPath)
       
    19 	{
       
    20 	return ifs.SetSessionPath(aPath);
       
    21 	}
       
    22 
       
    23 TInt TDBMS_CRCChecks::FileCrcL(const RFile& aFile, TUint32 &asum)
       
    24 	{
       
    25 	const TInt KFileCrcBufSize = 1024;
       
    26 
       
    27 	TInt err;
       
    28 	TInt pos = 0;
       
    29 	// Seek to the beginning of the file.
       
    30 	if( (err = aFile.Seek(ESeekStart, pos)) != KErrNone)
       
    31 		return err;
       
    32 
       
    33 	RBuf8 buffer;
       
    34 	if((err = buffer.Create(KFileCrcBufSize)) != KErrNone)
       
    35 		return err;
       
    36 	CleanupClosePushL(buffer);
       
    37 	asum=0;
       
    38 	for(;;)
       
    39 		{
       
    40 		err = aFile.Read(buffer, KFileCrcBufSize);
       
    41 		if(err) break;
       
    42 		TInt len = buffer.Length();
       
    43 		if(len == 0) break;
       
    44 		Mem::Crc32(asum, (TAny*) buffer.Ptr(), len);
       
    45 		}
       
    46 	CleanupStack::PopAndDestroy(1, &buffer);
       
    47 	return err;
       
    48 	}
       
    49 
       
    50 const TInt TDBMS_CRCChecks::GenerateCrcL(const TPtrC aFile)
       
    51 	{
       
    52 	RFile file;
       
    53 	TInt err = file.Open(ifs, aFile, EFileRead);
       
    54 	RDebug::Print(_L("==================== File open=%S, err=%d\n"), &aFile, err);
       
    55 	if(err != KErrNone) 
       
    56 		return err;
       
    57 	CleanupClosePushL(file);
       
    58 
       
    59 	TestCheckInfo tcinf;
       
    60 	err = file.Size(tcinf.filesz);
       
    61 	if(err)
       
    62 		{
       
    63 		CleanupStack::PopAndDestroy(1);
       
    64 		return err;
       
    65 		}
       
    66 	err = FileCrcL(file, tcinf.crc);
       
    67 	if(err)
       
    68 		{
       
    69 		CleanupStack::PopAndDestroy(1);
       
    70 		return err;
       
    71 		}
       
    72     err = iarray.Append(tcinf);
       
    73 	CleanupStack::PopAndDestroy(1);
       
    74 	return err;
       
    75 	}
       
    76 
       
    77 const TInt TDBMS_CRCChecks::RecordCount()
       
    78 	{
       
    79 	return iarray.Count();
       
    80 	}
       
    81 
       
    82 const TestCheckInfo TDBMS_CRCChecks::operator[](const TInt aidx)
       
    83 	{
       
    84 	return iarray[aidx];
       
    85 	}
       
    86 
       
    87 const TInt TDBMS_CRCChecks::DumpCrcRecordsL(const TDesC &alog)
       
    88 	{
       
    89 	RFile logfile;
       
    90 	TInt err = logfile.Replace(ifs, alog, EFileWrite);
       
    91 	RDebug::Print(_L("==================== File replace=%S, err=%d\n"), &alog, err);
       
    92 	if(err != KErrNone)
       
    93 		return err;
       
    94 	CleanupClosePushL(logfile);
       
    95 	TBuf8<0x100> output;
       
    96 
       
    97 	for(TInt i=0 ; i<RecordCount() ; i++)
       
    98 		{
       
    99 		output.SetLength(0);
       
   100 		TestCheckInfo tc = iarray[i];
       
   101 		output.AppendFormat(_L8("%d %d 0x%08x\n"), i+1, tc.filesz, tc.crc );
       
   102 		err = logfile.Write(output);
       
   103 		if(err) break;
       
   104 		}
       
   105 	CleanupStack::PopAndDestroy(1);
       
   106 	return err;
       
   107 	}
       
   108 
       
   109 const TInt TDBMS_CRCChecks::ValidateCrcRecordsL(const TDesC &alog)
       
   110 	{
       
   111 	RFile logfile;
       
   112 	TInt err = logfile.Open(ifs, alog, EFileRead);
       
   113 	RDebug::Print(_L("==================== File open=%S, err=%d\n"), &alog, err);
       
   114 	if(err != KErrNone)
       
   115 		return err;
       
   116 	CleanupClosePushL(logfile);
       
   117    	
       
   118 	TInt logfilesz;
       
   119 	if((err = logfile.Size(logfilesz)) != KErrNone)
       
   120 		{
       
   121 		CleanupStack::PopAndDestroy(1);
       
   122 		return err;
       
   123 		}
       
   124 
       
   125 	RBuf8 input;
       
   126 	if((err = input.Create(logfilesz)) != KErrNone)
       
   127 		{
       
   128 		CleanupStack::PopAndDestroy(1);
       
   129 		return err;
       
   130 		}
       
   131 	CleanupClosePushL(input);
       
   132 	// Read the entire file.
       
   133 	err = logfile.Read(input);
       
   134 	if(err != KErrNone)
       
   135 		{
       
   136 		CleanupStack::PopAndDestroy(2);
       
   137 		return err;
       
   138 		}
       
   139 	TInt nread = input.Length();
       
   140 	if(nread != logfilesz)
       
   141 		{
       
   142 		CleanupStack::PopAndDestroy(2);
       
   143 		User::Leave(KErrCorrupt);  // wrong error.
       
   144 		}
       
   145 
       
   146 	TPtrC8 slice;
       
   147 	slice.Set(input);
       
   148 	TInt offset=0;
       
   149 	TBuf8<0x100> expected;
       
   150 	TInt i; // we check this after the loop...
       
   151 	for(i=0; ; i++)
       
   152 		{
       
   153 		// Find the next carriage return in the file. 'slice' represents
       
   154 		// the next bit of the file before the next carriage return.
       
   155 		// Will this break on Symbian? That has '\r\n'...
       
   156 		TInt nextcr = slice.Locate(TChar('\n'));
       
   157 		// If no carriage return is found we must have reached the end of
       
   158 		// the file.
       
   159 		if(nextcr == KErrNotFound)
       
   160 		  	break;
       
   161 		// 'got' is the current line from the file, including the carriage
       
   162 		// return.
       
   163 		TPtrC8 got = slice.Left(nextcr+1);
       
   164 
       
   165 		// Before we construct the string this object expects to see,
       
   166 		// check we're not out of array..
       
   167 		// The number of lines in the file we checking should match what
       
   168 		// we have in our internal array.
       
   169 		if(i >= iarray.Count())
       
   170 			{
       
   171 			err = ECrcCheckMoreRecords;
       
   172 			break;
       
   173 			}
       
   174 
       
   175 		// Construct a string from our internal data that is expected to be
       
   176 		// the same as the data read in from the crc file.
       
   177 		expected.SetLength(0);
       
   178 		TestCheckInfo tc = iarray[i];
       
   179 		expected.AppendFormat(_L8("%d %d 0x%08x\n"), i+1, tc.filesz, tc.crc );
       
   180         // Compare what we've got to what we expect.
       
   181 		if(got.Compare(expected) != 0)
       
   182 			{
       
   183 			err = ECrcCheckMismatch;
       
   184 			break;
       
   185 			}
       
   186 
       
   187 		offset += nextcr + 1;
       
   188 		slice.Set(input.Mid(offset));
       
   189 		}
       
   190 	// The number of lines in the file we checking should match what
       
   191 	// we have in our internal array. Here this indicates that we seem to
       
   192 	// have more in our array than appear in the file.
       
   193 	if(!err && (i != iarray.Count()))
       
   194 		err = ECrcCheckFewerRecords;
       
   195 	CleanupStack::PopAndDestroy(2);
       
   196 	return err;
       
   197 	}
       
   198 
       
   199 const void TDBMS_CRCChecks::ErrorReportL(const TInt aerr, TPtrC& aerrmsg)
       
   200 	{
       
   201 	switch(aerr)
       
   202 		{
       
   203 		case KErrNotFound:
       
   204 				aerrmsg.Set(_L("Failed to open CRC log file.\n"));
       
   205 				break;
       
   206 		case KErrNoMemory:
       
   207 				aerrmsg.Set(_L("Out of memory.\n"));
       
   208 				break;
       
   209 		case KErrNone:
       
   210 		case ECrcCheckOk:
       
   211 				aerrmsg.Set(_L("CRC check ok.\n"));
       
   212 				break;
       
   213 		case ECrcCheckMismatch:
       
   214 				aerrmsg.Set(_L("CRC mismatch.\n"));
       
   215 				break;
       
   216 		case ECrcCheckFewerRecords:
       
   217 				aerrmsg.Set(_L("Fewer CRCs than in the file!\n"));
       
   218 				break;
       
   219 		case ECrcCheckMoreRecords:
       
   220 				aerrmsg.Set(_L("More CRCs in the file than I have!\n"));
       
   221 				break;
       
   222 		default:
       
   223 				aerrmsg.Set(_L("Broken!\n")); // PANIC?
       
   224 				break;
       
   225 		}
       
   226 	return;
       
   227 	}
       
   228 
       
   229 #ifdef CRC_TEST
       
   230 int E32Main(void)
       
   231 	{
       
   232 	CTrapCleanup *cleanup = CTrapCleanup::New();
       
   233 	__ASSERT_ALWAYS(cleanup != NULL, User::Invariant());
       
   234 
       
   235 	TDBMS_CRCChecks mycrc;
       
   236 	TInt err;
       
   237    	TRAPD(lc, err = mycrc.GenerateCrcL(_L("crcchecks.mmp")));
       
   238    	TRAP(lc, err = mycrc.GenerateCrcL(_L("bld.inf")));
       
   239 
       
   240 	TRAP(lc, err = mycrc.DumpCrcRecordsL(_L("wibble")));
       
   241    	TRAP(lc, err = mycrc.GenerateCrcL(_L("t_alter.mmp")));
       
   242 	TRAP(lc, err = mycrc.ValidateCrcRecordsL(_L("wibble")));
       
   243 	switch(err)
       
   244 		{
       
   245 		case TDBMS_CRCChecks::ECrcCheckMismatch:
       
   246 				printf("Got CRC mismatch\n");
       
   247 				break;
       
   248 		case TDBMS_CRCChecks::ECrcCheckFewerRecords:
       
   249 				printf("I have more CRCs than in the file!\n");
       
   250 				break;
       
   251 		case TDBMS_CRCChecks::ECrcCheckMoreRecords:
       
   252 				printf("More CRCs in the file than I have!\n");
       
   253 				break;
       
   254 		default:
       
   255 				printf("Broken!\n");
       
   256 				break;
       
   257 		}
       
   258 	printf("Leavecode = %d, err = %d\n", lc, err );
       
   259 
       
   260 	delete cleanup;
       
   261 	}
       
   262 #endif