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