Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
18 const TInt TDBMS_CRCChecks::SetSessionPath(const TDesC& aPath)
20 return ifs.SetSessionPath(aPath);
23 TInt TDBMS_CRCChecks::FileCrcL(const RFile& aFile, TUint32 &asum)
25 const TInt KFileCrcBufSize = 1024;
29 // Seek to the beginning of the file.
30 if( (err = aFile.Seek(ESeekStart, pos)) != KErrNone)
34 if((err = buffer.Create(KFileCrcBufSize)) != KErrNone)
36 CleanupClosePushL(buffer);
40 err = aFile.Read(buffer, KFileCrcBufSize);
42 TInt len = buffer.Length();
44 Mem::Crc32(asum, (TAny*) buffer.Ptr(), len);
46 CleanupStack::PopAndDestroy(1, &buffer);
50 const TInt TDBMS_CRCChecks::GenerateCrcL(const TPtrC aFile)
53 TInt err = file.Open(ifs, aFile, EFileRead);
54 RDebug::Print(_L("==================== File open=%S, err=%d\n"), &aFile, err);
57 CleanupClosePushL(file);
60 err = file.Size(tcinf.filesz);
63 CleanupStack::PopAndDestroy(1);
66 err = FileCrcL(file, tcinf.crc);
69 CleanupStack::PopAndDestroy(1);
72 err = iarray.Append(tcinf);
73 CleanupStack::PopAndDestroy(1);
77 const TInt TDBMS_CRCChecks::RecordCount()
79 return iarray.Count();
82 const TestCheckInfo TDBMS_CRCChecks::operator[](const TInt aidx)
87 const TInt TDBMS_CRCChecks::DumpCrcRecordsL(const TDesC &alog)
90 TInt err = logfile.Replace(ifs, alog, EFileWrite);
91 RDebug::Print(_L("==================== File replace=%S, err=%d\n"), &alog, err);
94 CleanupClosePushL(logfile);
97 for(TInt i=0 ; i<RecordCount() ; i++)
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);
105 CleanupStack::PopAndDestroy(1);
109 const TInt TDBMS_CRCChecks::ValidateCrcRecordsL(const TDesC &alog)
112 TInt err = logfile.Open(ifs, alog, EFileRead);
113 RDebug::Print(_L("==================== File open=%S, err=%d\n"), &alog, err);
116 CleanupClosePushL(logfile);
119 if((err = logfile.Size(logfilesz)) != KErrNone)
121 CleanupStack::PopAndDestroy(1);
126 if((err = input.Create(logfilesz)) != KErrNone)
128 CleanupStack::PopAndDestroy(1);
131 CleanupClosePushL(input);
132 // Read the entire file.
133 err = logfile.Read(input);
136 CleanupStack::PopAndDestroy(2);
139 TInt nread = input.Length();
140 if(nread != logfilesz)
142 CleanupStack::PopAndDestroy(2);
143 User::Leave(KErrCorrupt); // wrong error.
149 TBuf8<0x100> expected;
150 TInt i; // we check this after the loop...
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
159 if(nextcr == KErrNotFound)
161 // 'got' is the current line from the file, including the carriage
163 TPtrC8 got = slice.Left(nextcr+1);
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())
171 err = ECrcCheckMoreRecords;
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)
183 err = ECrcCheckMismatch;
187 offset += nextcr + 1;
188 slice.Set(input.Mid(offset));
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);
199 const void TDBMS_CRCChecks::ErrorReportL(const TInt aerr, TPtrC& aerrmsg)
204 aerrmsg.Set(_L("Failed to open CRC log file.\n"));
207 aerrmsg.Set(_L("Out of memory.\n"));
211 aerrmsg.Set(_L("CRC check ok.\n"));
213 case ECrcCheckMismatch:
214 aerrmsg.Set(_L("CRC mismatch.\n"));
216 case ECrcCheckFewerRecords:
217 aerrmsg.Set(_L("Fewer CRCs than in the file!\n"));
219 case ECrcCheckMoreRecords:
220 aerrmsg.Set(_L("More CRCs in the file than I have!\n"));
223 aerrmsg.Set(_L("Broken!\n")); // PANIC?
232 CTrapCleanup *cleanup = CTrapCleanup::New();
233 __ASSERT_ALWAYS(cleanup != NULL, User::Invariant());
235 TDBMS_CRCChecks mycrc;
237 TRAPD(lc, err = mycrc.GenerateCrcL(_L("crcchecks.mmp")));
238 TRAP(lc, err = mycrc.GenerateCrcL(_L("bld.inf")));
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")));
245 case TDBMS_CRCChecks::ECrcCheckMismatch:
246 printf("Got CRC mismatch\n");
248 case TDBMS_CRCChecks::ECrcCheckFewerRecords:
249 printf("I have more CRCs than in the file!\n");
251 case TDBMS_CRCChecks::ECrcCheckMoreRecords:
252 printf("More CRCs in the file than I have!\n");
258 printf("Leavecode = %d, err = %d\n", lc, err );