author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:50:07 +0300 | |
changeset 201 | 43365a9b78a3 |
parent 200 | 73ea206103e6 |
child 244 | a77889bee936 |
permissions | -rw-r--r-- |
0 | 1 |
// Copyright (c) 1996-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 the License "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 |
// @file |
|
15 |
// various FAT utilities |
|
16 |
// |
|
17 |
// |
|
18 |
||
19 |
#include "f32_test_utils.h" |
|
20 |
using namespace F32_Test_Utils; |
|
21 |
||
22 |
#include "filesystem_fat.h" |
|
23 |
||
24 |
//------------------------------------------------------------------------------------------------------------------- |
|
25 |
||
26 |
//-- define this macro if it is necessarily to exclude all stuff that uses RFs, consoles etc. |
|
27 |
//-- may be useful if this code is used for file server extensions. mmp file is a good place to define this macro. |
|
28 |
#ifndef FAT_UTILS_LEAN_AND_MEAN |
|
29 |
||
30 |
#include <e32cons.h> |
|
31 |
#include <e32math.h> |
|
32 |
||
33 |
//-- Note that the writable static data are not allowed in DLLs i.e. plugins or somethig else. |
|
34 |
//-- Thus it needs to be thrown away by preprocessing in such a case. |
|
35 |
||
36 |
static CConsoleBase* pConsole = NULL; //-- pointer to the text console for printing out data |
|
37 |
static TBool bPrintOutEnabled = ETrue; //-- global flag, if EFalse, all printing out is disabled |
|
38 |
||
39 |
/** |
|
40 |
Set the console where the ouput will go. |
|
41 |
@param apConsole pointer to the console. if NULL, the print out will be debug port only. |
|
42 |
*/ |
|
43 |
void F32_Test_Utils::SetConsole(CConsoleBase* apConsole) |
|
44 |
{ |
|
45 |
pConsole = apConsole; |
|
46 |
} |
|
47 |
||
48 |
/** |
|
49 |
Enable or disable printing out. See DoPrintf() |
|
50 |
@param bEnable If ETrue, print out is enabled |
|
51 |
@return previous value of the global bPrintOutEnabled flag |
|
52 |
*/ |
|
53 |
TBool F32_Test_Utils::EnablePrintOutput(TBool bEnable) |
|
54 |
{ |
|
55 |
TBool bPrevVal = bPrintOutEnabled; |
|
56 |
bPrintOutEnabled = bEnable; |
|
57 |
||
58 |
return bPrevVal; |
|
59 |
} |
|
60 |
||
61 |
/** |
|
62 |
Print out the drive information. |
|
63 |
||
64 |
@param aFs reference to the FS session |
|
65 |
@param aDrive drive number |
|
66 |
@return system-wide error codes. |
|
67 |
*/ |
|
68 |
TInt F32_Test_Utils::PrintDrvInfo(RFs &aFs, TInt aDrive) |
|
69 |
{ |
|
70 |
TInt nRes; |
|
71 |
TDriveInfo driveInfo; |
|
72 |
TVolumeInfo volInfo; |
|
73 |
TBuf<256> Buf; |
|
74 |
||
75 |
//-- get drive info |
|
76 |
nRes = aFs.Drive(driveInfo, aDrive); |
|
77 |
if(nRes != KErrNone) |
|
78 |
return nRes; |
|
79 |
||
80 |
nRes = aFs.Volume(volInfo, aDrive); |
|
81 |
if(nRes != KErrNone) |
|
82 |
return nRes; |
|
83 |
||
84 |
DoPrintf(_L("Drive %c: #%d\n"), 'A'+aDrive, aDrive); |
|
85 |
||
86 |
//-- print the FS name |
|
87 |
nRes = aFs.FileSystemName(Buf, aDrive); |
|
88 |
if(nRes != KErrNone) |
|
89 |
return nRes; |
|
90 |
||
91 |
//-- to find out FS sub type |
|
92 |
TFSName fsName; |
|
93 |
nRes = aFs.FileSystemSubType(aDrive, fsName); |
|
94 |
if(nRes == KErrNone && Buf.CompareF(fsName) !=KErrNone) |
|
95 |
{ |
|
96 |
Buf.AppendFormat(_L(" (%S)"), &fsName); |
|
97 |
} |
|
98 |
||
99 |
||
100 |
DoPrintf(_L("FS name: %S\n"), &Buf); |
|
101 |
||
102 |
//-- print drive and media attributes |
|
103 |
DoPrintf(_L("MediaType: 0x%x\n"), driveInfo.iType); |
|
104 |
DoPrintf(_L("DriveAtt:0x%x\n"),driveInfo.iDriveAtt); |
|
105 |
DoPrintf(_L("MediaAtt:0x%x\n"),driveInfo.iMediaAtt); |
|
106 |
||
107 |
//-- volume information |
|
108 |
DoPrintf(_L("VolId:0x%x\n"),volInfo.iUniqueID); |
|
109 |
DoPrintf(_L("VolSz:%ld (%ldK)\n"),volInfo.iSize, volInfo.iSize/1024); |
|
110 |
DoPrintf(_L("Free:%ld (%ldK)\n"),volInfo.iFree, volInfo.iFree/1024); |
|
111 |
||
112 |
return KErrNone; |
|
113 |
} |
|
114 |
||
115 |
//------------------------------------------------------------------------------------------------------------------- |
|
116 |
||
117 |
||
118 |
/** |
|
119 |
Fill a media region with a given byte pattern |
|
120 |
||
121 |
@param aFs reference to the FS session |
|
122 |
@param aDrive drive number |
|
123 |
@param aMediaStartPos media region start position |
|
124 |
@param aMediaEndPos media region end position |
|
125 |
@param aBytePattern byte to fill the media region with |
|
126 |
||
127 |
@return system-wide error codes. |
|
128 |
*/ |
|
129 |
TInt F32_Test_Utils::FillMedia(RFs &aFs, TInt aDrive, TInt64 aMediaStartPos, TInt64 aMediaEndPos, TUint8 aBytePattern/*=0*/) |
|
130 |
{ |
|
131 |
DoPrintf(_L("~ F32_Test_Utils::FillMedia() drv:%d, from:%u to:%u\n"),aDrive, (TUint32)aMediaStartPos, (TUint32)aMediaEndPos); |
|
132 |
||
133 |
ASSERT(aMediaStartPos<=aMediaEndPos && aMediaStartPos >=0 && aMediaEndPos >=0); |
|
134 |
||
135 |
TInt nRes = KErrNone; |
|
136 |
RBuf8 buf; |
|
137 |
const TUint32 KBufSz=65536; //-- zero-buffer size, bytes |
|
138 |
||
139 |
//-- create a buffer, filled with given pattern |
|
140 |
nRes = buf.CreateMax(KBufSz); |
|
141 |
ASSERT(nRes == KErrNone); |
|
142 |
buf.Fill(aBytePattern); |
|
143 |
||
144 |
TUint32 rem = (TUint32)(aMediaEndPos - aMediaStartPos); |
|
145 |
while(rem) |
|
146 |
{ |
|
147 |
const TUint32 bytesToWrite=Min(rem, KBufSz); |
|
148 |
TPtrC8 ptrData(buf.Ptr(), bytesToWrite); |
|
149 |
||
150 |
nRes = MediaRawWrite(aFs, aDrive, aMediaStartPos, ptrData); |
|
151 |
if(nRes != KErrNone && nRes != KErrDiskFull) |
|
152 |
break; |
|
153 |
||
154 |
aMediaStartPos+=bytesToWrite; |
|
155 |
rem-=bytesToWrite; |
|
156 |
} |
|
157 |
||
158 |
buf.Close(); |
|
159 |
||
160 |
return nRes; |
|
161 |
} |
|
162 |
||
163 |
||
164 |
//------------------------------------------------------------------------------------------------------------------- |
|
165 |
||
166 |
/** |
|
167 |
Raw read from the media. |
|
168 |
||
169 |
@param aFs reference to the FS session |
|
170 |
@param aDrive drive number |
|
171 |
@param aMediaPos media position |
|
172 |
@param aLen how many bytes to read |
|
173 |
@param aData descriptor for the data |
|
174 |
||
175 |
@return system-wide error code. |
|
176 |
*/ |
|
177 |
TInt F32_Test_Utils::MediaRawRead(RFs &aFs, TInt aDrive, TInt64 aMediaPos, TUint32 aLen, TDes8& aData) |
|
178 |
{ |
|
179 |
TInt nRes=KErrNone; |
|
180 |
TRAP(nRes, DoMediaRawReadL(aFs, aDrive, aMediaPos, aLen, aData)); |
|
181 |
return nRes; |
|
182 |
} |
|
183 |
||
184 |
//------------------------------------------------------------------------------------------------------------------- |
|
185 |
||
186 |
/** |
|
187 |
Raw write to the media. |
|
188 |
||
189 |
@param aFs reference to the FS session |
|
190 |
@param aDrive drive number |
|
191 |
@param aMediaPos media position |
|
192 |
@param aData descriptor with the data to write |
|
193 |
||
194 |
@return system-wide error code. |
|
195 |
*/ |
|
196 |
TInt F32_Test_Utils::MediaRawWrite(RFs &aFs, TInt aDrive, TInt64 aMediaPos, const TDesC8& aData) |
|
197 |
{ |
|
198 |
TInt nRes=KErrNone; |
|
199 |
TRAP(nRes, DoMediaRawWriteL(aFs, aDrive, aMediaPos, aData)); |
|
200 |
return nRes; |
|
201 |
} |
|
202 |
||
203 |
||
204 |
//------------------------------------------------------------------------------------------------------------------- |
|
205 |
void F32_Test_Utils::DoMediaRawReadL(RFs &aFs, TInt aDrive, TInt64 aMediaPos, TUint32 aLen, TDes8& aData) |
|
206 |
{ |
|
207 |
ASSERT(aMediaPos>=0); |
|
208 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
209 |
||
210 |
if((TUint32)aData.MaxSize() < aLen) |
|
211 |
User::Leave(KErrArgument); |
|
212 |
||
213 |
if(aLen == 0) |
|
214 |
return; |
|
215 |
||
216 |
aData.SetLength(aLen); |
|
217 |
||
218 |
RRawDisk rawDisk; |
|
219 |
CleanupClosePushL(rawDisk); |
|
220 |
||
221 |
User::LeaveIfError(rawDisk.Open(aFs, aDrive)); |
|
222 |
User::LeaveIfError(rawDisk.Read(aMediaPos, aData)); |
|
223 |
||
224 |
CleanupStack::PopAndDestroy(&rawDisk); |
|
225 |
} |
|
226 |
||
227 |
//------------------------------------------------------------------------------------------------------------------- |
|
228 |
void F32_Test_Utils::DoMediaRawWriteL(RFs &aFs, TInt aDrive, TInt64 aMediaPos, const TDesC8& aData) |
|
229 |
{ |
|
230 |
ASSERT(aMediaPos>=0); |
|
231 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
232 |
||
233 |
if(aData.Size() == 0) |
|
234 |
return; |
|
235 |
||
236 |
RRawDisk rawDisk; |
|
237 |
CleanupClosePushL(rawDisk); |
|
238 |
||
239 |
User::LeaveIfError(rawDisk.Open(aFs, aDrive)); |
|
240 |
User::LeaveIfError(rawDisk.Write(aMediaPos, (TDesC8&)aData)); |
|
241 |
||
242 |
CleanupStack::PopAndDestroy(&rawDisk); |
|
243 |
} |
|
244 |
||
245 |
//------------------------------------------------------------------------------------------------------------------- |
|
246 |
||
247 |
_LIT(KFsName_LFFS, "lffs"); |
|
248 |
_LIT(KFsName_Win32, "Win32"); |
|
249 |
_LIT(KFsName_ExFAT, "ExFat"); |
|
250 |
_LIT(KFsName_AutoMonuter, "automounter"); |
|
200
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
251 |
_LIT(KFsName_HVFS, "HVFS"); |
0 | 252 |
|
253 |
/** @return ETrue if "Automounter" FS is mounted on this drive */ |
|
254 |
TBool F32_Test_Utils::Is_Automounter(RFs &aFs, TInt aDrive) |
|
255 |
{ |
|
256 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
257 |
TFSName f; |
|
258 |
TInt r = aFs.FileSystemName(f, aDrive); |
|
259 |
__ASSERT_ALWAYS((r==KErrNone) && (f.Length()>0), User::Invariant()); |
|
260 |
||
261 |
return (f.CompareF(KFsName_AutoMonuter) == 0 ); |
|
262 |
||
263 |
} |
|
264 |
||
265 |
/** @return ETrue if "lffs" FS is mounted on this drive */ |
|
266 |
TBool F32_Test_Utils::Is_Lffs(RFs &aFs, TInt aDrive) |
|
267 |
{ |
|
268 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
269 |
TFSName f; |
|
270 |
TInt r = aFs.FileSystemName(f, aDrive); |
|
271 |
__ASSERT_ALWAYS((r==KErrNone) && (f.Length()>0), User::Invariant()); |
|
272 |
||
273 |
return (f.CompareF(KFsName_LFFS) == 0 ); |
|
274 |
||
275 |
} |
|
276 |
||
200
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
277 |
/** @return ETrue if "Win32" FS is mounted on this drive (i.e this is emulator's drive C:) */ |
0 | 278 |
TBool F32_Test_Utils::Is_Win32(RFs &aFs, TInt aDrive) |
279 |
{ |
|
280 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
281 |
TFSName f; |
|
282 |
TInt r = aFs.FileSystemName(f, aDrive); |
|
283 |
__ASSERT_ALWAYS((r==KErrNone) && (f.Length()>0), User::Invariant()); |
|
284 |
||
285 |
return (f.CompareF(KFsName_Win32) == 0 ); |
|
286 |
} |
|
287 |
||
200
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
288 |
/** @return ETrue if "HVFS" is mounted on this drive (i.e PlatSim's drive C:) */ |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
289 |
TBool F32_Test_Utils::Is_HVFS(RFs &aFs, TInt aDrive) |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
290 |
{ |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
TFSName f; |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
TInt r = aFs.FileSystemName(f, aDrive); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
__ASSERT_ALWAYS((r==KErrNone) && (f.Length()>0), User::Invariant()); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
295 |
|
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
296 |
return (f.CompareF(KFsName_HVFS) == 0); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
297 |
} |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
|
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
299 |
/** @return ETrue if "HVFS" or "Win32" FS is mounted on this drive |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
300 |
* (i.e drive C: of PlatSim or the emulator) */ |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
301 |
TBool F32_Test_Utils::Is_SimulatedSystemDrive(RFs &aFs, TInt aDrive) |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
302 |
{ |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
303 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
304 |
TFSName f; |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
305 |
TInt r = aFs.FileSystemName(f, aDrive); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
306 |
__ASSERT_ALWAYS((r==KErrNone) && (f.Length()>0), User::Invariant()); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
307 |
|
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
308 |
return (f.CompareF(KFsName_HVFS) == 0 || f.CompareF(KFsName_Win32) == 0); |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
309 |
} |
73ea206103e6
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
310 |
|
0 | 311 |
/** @return ETrue if the filesystem if FAT (fat12/16/32) */ |
312 |
TBool F32_Test_Utils::Is_Fat(RFs &aFs, TInt aDrive) |
|
313 |
{ |
|
314 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
315 |
TFSName fsName; |
|
316 |
TInt nRes = aFs.FileSystemName(fsName, aDrive); |
|
317 |
__ASSERT_ALWAYS((nRes==KErrNone) && (fsName.Length()>0), User::Invariant()); |
|
318 |
||
319 |
if(fsName.CompareF(KFileSystemName_FAT) == 0) |
|
320 |
return ETrue; //-- "FAT" FS is explicitly mounted on this drive |
|
321 |
||
322 |
//-- try analyse file system subtype for the case of automounter FS |
|
323 |
nRes = aFs.FileSystemSubType(aDrive,fsName); |
|
324 |
if(nRes !=KErrNone) |
|
325 |
return EFalse; |
|
326 |
||
327 |
return (fsName.CompareF(KFSSubType_FAT16) == 0 || fsName.CompareF(KFSSubType_FAT32) == 0 || fsName.CompareF(KFSSubType_FAT12) == 0); |
|
328 |
} |
|
329 |
||
330 |
/** returns ETrue if "exFAT" FS is mounted on this drive */ |
|
331 |
TBool F32_Test_Utils::Is_ExFat(RFs &aFs, TInt aDrive) |
|
332 |
{ |
|
333 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
334 |
TFSName fsName; |
|
335 |
TInt nRes = aFs.FileSystemName(fsName, aDrive); |
|
336 |
__ASSERT_ALWAYS((nRes==KErrNone) && (fsName.Length()>0), User::Invariant()); |
|
337 |
||
338 |
if(fsName.CompareF(KFsName_ExFAT) == 0 ) |
|
339 |
return ETrue; //-- "exFAT" FS is explicitly mounted on this drive |
|
340 |
||
341 |
//-- try analyse file system subtype for the case of automounter FS |
|
342 |
nRes = aFs.FileSystemSubType(aDrive,fsName); |
|
343 |
if(nRes !=KErrNone) |
|
344 |
return EFalse; |
|
345 |
||
346 |
return (fsName.CompareF(KFsName_ExFAT) == 0); |
|
347 |
} |
|
348 |
||
349 |
/** @return ETrue if the filesystem if FAT32 */ |
|
350 |
TBool F32_Test_Utils::Is_Fat32(RFs &aFs, TInt aDrive) |
|
351 |
{ |
|
352 |
if(!Is_Fat(aFs, aDrive)) |
|
353 |
return EFalse; |
|
354 |
||
355 |
TFSName fsName; |
|
356 |
TInt nRes = aFs.FileSystemSubType(aDrive,fsName); |
|
357 |
||
358 |
if(nRes !=KErrNone) |
|
359 |
return EFalse; |
|
360 |
||
361 |
return (fsName.CompareF(KFSSubType_FAT32) == 0); |
|
362 |
||
363 |
} |
|
364 |
||
365 |
/** @return ETrue if the filesystem if FAT16 */ |
|
366 |
TBool F32_Test_Utils::Is_Fat16(RFs &aFs, TInt aDrive) |
|
367 |
{ |
|
368 |
if(!Is_Fat(aFs, aDrive)) |
|
369 |
return EFalse; |
|
370 |
||
371 |
TFSName fsName; |
|
372 |
TInt nRes = aFs.FileSystemSubType(aDrive,fsName); |
|
373 |
||
374 |
if(nRes !=KErrNone) |
|
375 |
return EFalse; |
|
376 |
||
377 |
return (fsName.CompareF(KFSSubType_FAT16) == 0); |
|
378 |
||
379 |
} |
|
380 |
||
381 |
/** @return ETrue if the filesystem if FAT12 */ |
|
382 |
TBool F32_Test_Utils::Is_Fat12(RFs &aFs, TInt aDrive) |
|
383 |
{ |
|
384 |
if(!Is_Fat(aFs, aDrive)) |
|
385 |
return EFalse; |
|
386 |
||
387 |
TFSName fsName; |
|
388 |
TInt nRes = aFs.FileSystemSubType(aDrive,fsName); |
|
389 |
||
390 |
if(nRes !=KErrNone) |
|
391 |
return EFalse; |
|
392 |
||
393 |
return (fsName.CompareF(KFSSubType_FAT12) == 0); |
|
394 |
||
395 |
} |
|
396 |
||
397 |
||
398 |
static void DoCreateCheckableFileL(RFile64& aFile, TUint64 aSize) |
|
399 |
{ |
|
400 |
||
401 |
ASSERT(aSize >= TMD5::HashSize); |
|
402 |
||
403 |
//-- 1. set file size |
|
404 |
User::LeaveIfError(aFile.SetSize((TInt)aSize)); |
|
405 |
||
406 |
//-- 2. leave place for the 16-bytes MD5 hash in the file beginning |
|
407 |
TInt64 filePos = TMD5::HashSize; |
|
408 |
User::LeaveIfError(aFile.Seek(ESeekStart,filePos)); |
|
409 |
aSize-=TMD5::HashSize; |
|
410 |
||
411 |
TMD5 md5Hash; |
|
412 |
||
413 |
//-- 3. fill the file with random bytes |
|
414 |
const TInt KBufSize=65536; //-- buffer size for writing data |
|
415 |
TInt64 rndSeed = aSize % 43283; |
|
416 |
if(!rndSeed) |
|
417 |
rndSeed = 33521; |
|
418 |
||
419 |
RBuf8 buf; |
|
420 |
buf.CreateMaxL(KBufSize); |
|
421 |
||
422 |
TUint64 prevSz = aSize; |
|
423 |
while(aSize) |
|
424 |
{ |
|
425 |
//-- initialize buffer with random rubbish |
|
426 |
for(TInt i=0; i<KBufSize; ++i) |
|
427 |
{ |
|
428 |
buf[i] = (TUint8)Math::Rand(rndSeed); |
|
429 |
} |
|
430 |
||
431 |
const TUint32 nBytes = (TUint32)Min((TUint64)KBufSize, aSize); |
|
432 |
TPtrC8 ptrData(buf.Ptr(), nBytes); |
|
433 |
User::LeaveIfError(aFile.Write(ptrData)); //-- write data to the file |
|
434 |
md5Hash.Update(ptrData); //-- update MD5 hash |
|
435 |
||
436 |
aSize-=nBytes; |
|
437 |
||
438 |
if((prevSz - aSize) >= K1MegaByte) |
|
439 |
{ |
|
440 |
prevSz = aSize; |
|
441 |
DoPrintf(_L(".")); |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
||
446 |
buf.Close(); |
|
447 |
DoPrintf(_L("\n")); |
|
448 |
||
449 |
//-- 4. write MD5 hash to the beginning of the file |
|
450 |
filePos = 0; |
|
451 |
User::LeaveIfError(aFile.Seek(ESeekStart,filePos)); |
|
452 |
User::LeaveIfError(aFile.Write(md5Hash.Final())); |
|
453 |
||
454 |
} |
|
455 |
||
456 |
||
457 |
static void DoVerifyCheckableFileL(RFile64& aFile) |
|
458 |
{ |
|
459 |
TInt64 fileSize; |
|
460 |
User::LeaveIfError(aFile.Size(fileSize)); |
|
461 |
||
462 |
if(fileSize < TMD5::HashSize) |
|
463 |
User::Leave(KErrCorrupt); //-- MD5 hash is 16 bytes, it's the minimal file size |
|
464 |
||
465 |
//-- 1. read MD5 header from the file |
|
466 |
TBuf8<TMD5::HashSize> md5Header(TMD5::HashSize); |
|
467 |
User::LeaveIfError(aFile.Read(md5Header)); |
|
468 |
fileSize -= TMD5::HashSize; |
|
469 |
||
470 |
//-- 2. read the rest of the data and calculate the checksum |
|
471 |
TMD5 md5Hash; |
|
472 |
RBuf8 buf; |
|
473 |
||
474 |
const TInt KBufSize=65536; //-- buffer size for writing data |
|
475 |
buf.CreateMaxL(KBufSize); |
|
476 |
||
477 |
TUint64 prevSz = fileSize; |
|
478 |
while(fileSize) |
|
479 |
{ |
|
480 |
User::LeaveIfError(aFile.Read(buf)); //-- read data from the file |
|
481 |
if (buf.Length() == 0) |
|
482 |
User::Leave(KErrEof); |
|
483 |
md5Hash.Update(buf); //-- update MD5 hash |
|
484 |
||
485 |
fileSize-=buf.Length(); |
|
486 |
||
487 |
if((prevSz - fileSize) >= K1MegaByte) |
|
488 |
{ |
|
489 |
prevSz = fileSize; |
|
490 |
DoPrintf(_L(".")); |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
buf.Close(); |
|
495 |
DoPrintf(_L("\n")); |
|
496 |
||
497 |
if(md5Hash.Final() != md5Header) |
|
498 |
User::Leave(KErrCorrupt); //-- the file is corrupt |
|
499 |
} |
|
500 |
||
501 |
//------------------------------------------------------------------------------------------------------------------- |
|
502 |
||
503 |
/** |
|
504 |
Creates a file filled with random data. The file has a 16-byte MD5 header at the start, so it is possible to |
|
505 |
verify the data validity later. Thus the minimal file size is 16 bytes. The file with the iven name may already |
|
506 |
exist; in this case it will be replaced. |
|
507 |
||
508 |
@param aFs reference to the FS session |
|
509 |
@param aFileName name of the file to be created / replaced |
|
510 |
@param aSize size of the file; 16 bytes min. |
|
511 |
||
512 |
@return Standard error code |
|
513 |
*/ |
|
514 |
TInt F32_Test_Utils::CreateCheckableStuffedFile(RFs& aFs, const TDesC& aFileName, TUint64 aSize) |
|
515 |
{ |
|
516 |
||
517 |
DoPrintf(_L("~ F32_Test_Utils::CreateCheckableStuffedFile() file:%S, Size:%LU\n"), &aFileName, aSize); |
|
518 |
||
519 |
if(aSize < TMD5::HashSize) |
|
520 |
return KErrArgument; //-- MD5 hash is 16 bytes, it's the minimal file size |
|
521 |
||
522 |
RFile64 file; |
|
523 |
||
524 |
//-- 1. create a file |
|
525 |
TInt nRes = file.Replace(aFs, aFileName, EFileWrite); |
|
526 |
if(nRes != KErrNone) |
|
527 |
return nRes; |
|
528 |
||
529 |
TRAP(nRes, DoCreateCheckableFileL(file, aSize)); |
|
530 |
||
531 |
file.Close(); |
|
532 |
return nRes; |
|
533 |
} |
|
534 |
||
535 |
||
536 |
/** |
|
537 |
Verify previously created file that has MD5 header at the beginning. See CreateCheckableStuffedFile(...) |
|
538 |
||
539 |
@param aFs reference to the FS session |
|
540 |
@param aFileName name of the file to be verified |
|
541 |
||
542 |
@return Standard error code. KErrNone if the file contents matches the MD5 header. |
|
543 |
*/ |
|
544 |
TInt F32_Test_Utils::VerifyCheckableFile(RFs& aFs, const TDesC& aFileName) |
|
545 |
{ |
|
546 |
DoPrintf(_L("~ F32_Test_Utils::VerifyCheckableFile() file:%S\n"), &aFileName); |
|
547 |
||
548 |
RFile64 file; |
|
549 |
||
550 |
//-- 1. create a file |
|
551 |
TInt nRes = file.Open(aFs, aFileName, EFileRead); |
|
552 |
if(nRes != KErrNone) |
|
553 |
return nRes; |
|
554 |
||
555 |
TRAP(nRes, DoVerifyCheckableFileL(file)); |
|
556 |
||
557 |
file.Close(); |
|
558 |
return nRes; |
|
559 |
||
560 |
} |
|
561 |
||
562 |
||
563 |
/** |
|
564 |
Create an empty file (not filled with anything). The size of the file is set by using RFile::SetSize(). |
|
565 |
For FAT this will result in allocating a cluster chain in FAT fable, for the file systems that support sparse files (like LFFS) |
|
566 |
this might not work as expected. |
|
567 |
||
568 |
@param aFs reference to the FS session |
|
569 |
@param aFileName name of the file to be created / replaced |
|
570 |
@param aSize size of the file |
|
571 |
||
572 |
@return Standard error code |
|
573 |
*/ |
|
574 |
TInt F32_Test_Utils::CreateEmptyFile(RFs& aFs, const TDesC& aFileName, TUint64 aSize) |
|
575 |
{ |
|
576 |
DoPrintf(_L("~ F32_Test_Utils::CreateEmptyFile() file:%S, sz:%LU\n"), &aFileName, aSize); |
|
577 |
||
578 |
RFile64 file; |
|
579 |
||
580 |
//-- 1. create a file |
|
581 |
TInt nRes = file.Replace(aFs, aFileName, EFileWrite); |
|
582 |
if(nRes != KErrNone) |
|
583 |
return nRes; |
|
584 |
||
585 |
//-- 2. set file size |
|
586 |
nRes = file.SetSize(aSize); |
|
587 |
||
588 |
file.Close(); |
|
589 |
||
590 |
return nRes; |
|
591 |
} |
|
592 |
||
593 |
//------------------------------------------------------------------------------------------------------------------- |
|
594 |
||
595 |
/** |
|
596 |
Dismount and mount the filesystem again, optionally taking time when the mount starts. |
|
597 |
The FS can have extensions added into it; this function will handle only the primary extension (if it is present) and |
|
598 |
will mont the FS with it. Non-primary extensions are not supported yet. |
|
599 |
||
600 |
||
601 |
@param aFs reference to the FS session |
|
602 |
@param aDrive drive number |
|
603 |
@param apTimeMountStart pointer to the TTime object, that can be called TTime::UniversalTime() on mount start (this can be |
|
604 |
used for measuring time taken to mount the FS). |
|
605 |
if NULL, no action will be taken. |
|
606 |
||
607 |
@return error code from the RFs::MountFileSystem() |
|
608 |
||
609 |
*/ |
|
610 |
TInt F32_Test_Utils::RemountFS(RFs& aFs, TInt aDrive, TTime* apTimeMountStart/*=NULL*/) |
|
611 |
{ |
|
612 |
TInt nRes; |
|
613 |
DoPrintf(_L("~ F32_Test_Utils::RemountingFS at drive:%d\n"), aDrive); |
|
614 |
||
615 |
TFSDescriptor fsDescriptor; |
|
616 |
||
617 |
//-- 1. get current FS information |
|
618 |
nRes = GetFileSystemDescriptor(aFs, aDrive, fsDescriptor); |
|
619 |
if(nRes != KErrNone) |
|
620 |
return nRes; |
|
621 |
||
622 |
//-- 2. dismount the file system |
|
623 |
if(fsDescriptor.iPExtName.Length() > 0) |
|
624 |
{ |
|
625 |
DoPrintf(_L("~ Dismounting FS:%S with ext:%S\n"), &fsDescriptor.iFsName, &fsDescriptor.iPExtName); |
|
626 |
} |
|
627 |
else |
|
628 |
{ |
|
629 |
DoPrintf(_L("~ Dismounting FS:%S\n"), &fsDescriptor.iFsName); |
|
630 |
} |
|
631 |
||
632 |
nRes = aFs.DismountFileSystem(fsDescriptor.iFsName, aDrive); |
|
633 |
if(nRes != KErrNone) |
|
634 |
{ |
|
635 |
ASSERT(0); |
|
636 |
return nRes; |
|
637 |
} |
|
638 |
||
639 |
//-- 3. mount it again |
|
640 |
if(apTimeMountStart) |
|
641 |
apTimeMountStart->UniversalTime(); //-- take Mount start time |
|
642 |
||
643 |
||
644 |
nRes = MountFileSystem(aFs, aDrive, fsDescriptor); |
|
645 |
return nRes; |
|
646 |
} |
|
647 |
||
648 |
//------------------------------------------------------------------------------------------------------------------- |
|
649 |
||
650 |
TFSDescriptor::TFSDescriptor() |
|
651 |
{ |
|
652 |
Init(); |
|
653 |
} |
|
654 |
||
655 |
void TFSDescriptor::Init() |
|
656 |
{ |
|
657 |
iFsName.SetLength(0); |
|
658 |
iPExtName.SetLength(0); |
|
659 |
iDriveSynch = EFalse; |
|
660 |
} |
|
661 |
||
662 |
TBool TFSDescriptor::operator==(const TFSDescriptor& aRhs) const |
|
663 |
{ |
|
664 |
ASSERT(this != &aRhs); |
|
665 |
return (iFsName.CompareF(aRhs.iFsName) == 0 && iPExtName.CompareF(aRhs.iPExtName) == 0 && iDriveSynch == aRhs.iDriveSynch); |
|
666 |
} |
|
667 |
||
668 |
||
669 |
//------------------------------------------------------------------------------------------------------------------- |
|
670 |
/** |
|
671 |
Gets the information about file system mounted on a drive. This information can be later used for mounting this FS back if it is going to be dismounted |
|
672 |
||
673 |
@param aFs reference to the FS session |
|
674 |
@param aDrive drive number |
|
675 |
@param aFsDesc file system descriptor |
|
676 |
||
677 |
@return standard error code |
|
678 |
*/ |
|
679 |
TInt F32_Test_Utils::GetFileSystemDescriptor(RFs &aFs, TInt aDrive, TFSDescriptor& aFsDesc) |
|
680 |
{ |
|
681 |
TInt nRes; |
|
682 |
||
683 |
//-- 1. get file system name |
|
684 |
nRes = aFs.FileSystemName(aFsDesc.iFsName, aDrive); |
|
685 |
if(nRes != KErrNone) |
|
686 |
{ |
|
687 |
ASSERT(0); |
|
688 |
return nRes; |
|
689 |
} |
|
690 |
||
691 |
//-- 2. find out if the drive sync/async |
|
692 |
TPckgBuf<TBool> drvSyncBuf; |
|
693 |
nRes = aFs.QueryVolumeInfoExt(aDrive, EIsDriveSync, drvSyncBuf); |
|
694 |
if(nRes != KErrNone) |
|
695 |
{//-- pretend that the drive is asynch. in the case of file system being corrupted. this is 99.9% true |
|
696 |
aFsDesc.iDriveSynch = EFalse; |
|
697 |
} |
|
698 |
else |
|
699 |
{ |
|
700 |
aFsDesc.iDriveSynch = drvSyncBuf(); |
|
701 |
} |
|
702 |
||
703 |
//-- 3. find out primary extension name if it is present; we will need to add it again when mounting the FS |
|
704 |
//-- other extensions (non-primary) are not supported yet |
|
705 |
nRes = aFs.ExtensionName(aFsDesc.iPExtName, aDrive, 0); |
|
706 |
if(nRes != KErrNone) |
|
707 |
{ |
|
708 |
aFsDesc.iPExtName.SetLength(0); |
|
709 |
} |
|
710 |
||
711 |
//-- 3.1 check if the drive has non-primary extensions, fail in this case |
|
712 |
TBuf<40> extName; |
|
713 |
nRes = aFs.ExtensionName(extName, aDrive, 1); |
|
714 |
if(nRes == KErrNone) |
|
715 |
{ |
|
716 |
DoPrintf(_L("~ F32_Test_Utils::GetFileSystemDescriptor: Non-primary extensions are not supported!\n")); |
|
717 |
return KErrNotSupported; |
|
718 |
} |
|
719 |
||
720 |
||
721 |
return KErrNone; |
|
722 |
} |
|
723 |
||
724 |
//------------------------------------------------------------------------------------------------------------------- |
|
725 |
/** |
|
726 |
Mount the file system by the information provided in the FS descriptor |
|
727 |
||
728 |
@param aFs reference to the FS session |
|
729 |
@param aDrive drive number |
|
730 |
@param aFsDesc file system descriptor containing all necessary information to mount the FS. |
|
731 |
||
732 |
@return standard error code |
|
733 |
*/ |
|
734 |
TInt F32_Test_Utils::MountFileSystem(RFs &aFs, TInt aDrive, const TFSDescriptor& aFsDesc) |
|
735 |
{ |
|
736 |
DoPrintf(_L("~ F32_Test_Utils::MountFileSystem() drive:%d Name:%S\n"), aDrive, &aFsDesc.iFsName); |
|
737 |
||
738 |
TInt nRes; |
|
739 |
if(aFsDesc.iFsName.Length() <=0 ) |
|
740 |
{ |
|
741 |
ASSERT(0); |
|
742 |
return KErrArgument; |
|
743 |
} |
|
744 |
||
745 |
||
746 |
//-- mount File system |
|
747 |
const TBool bPrimaryExt = (aFsDesc.iPExtName.Length() > 0); |
|
748 |
||
749 |
if(bPrimaryExt) |
|
750 |
{//-- we need to mount FS with the primary extension |
|
751 |
nRes = aFs.AddExtension(aFsDesc.iPExtName); |
|
752 |
if(nRes != KErrNone && nRes != KErrAlreadyExists) |
|
753 |
{ |
|
754 |
ASSERT(0); |
|
755 |
return nRes; |
|
756 |
} |
|
757 |
||
758 |
nRes = aFs.MountFileSystem(aFsDesc.iFsName, aFsDesc.iPExtName, aDrive, aFsDesc.iDriveSynch); |
|
759 |
} |
|
760 |
else |
|
761 |
{//-- the FS did not have primary extension |
|
762 |
nRes = aFs.MountFileSystem(aFsDesc.iFsName, aDrive, aFsDesc.iDriveSynch); |
|
763 |
} |
|
764 |
||
765 |
||
766 |
return nRes; |
|
767 |
} |
|
768 |
||
769 |
//------------------------------------------------------------------------------------------------------------------- |
|
770 |
/** |
|
771 |
Format volume, regardless the file system installed. |
|
772 |
||
773 |
@param aFs reference to the FS session |
|
774 |
@param aDrive drive number |
|
775 |
@param aQuickFormat if True, a quick format will be performed. otherwise - full |
|
776 |
@return system-wide error codes. |
|
777 |
*/ |
|
778 |
TInt F32_Test_Utils::FormatDrive(RFs &aFs, TInt aDrive, TBool aQuickFormat) |
|
779 |
{ |
|
780 |
TPtrC fmtTypeName = (aQuickFormat ? _L("Quick") : _L("Full")); |
|
781 |
DoPrintf(_L("~ F32_Test_Utils::FormatDrive() drv:%d, type:%S\n"),aDrive, &fmtTypeName); |
|
782 |
||
783 |
ASSERT(aDrive >= EDriveA && aDrive <= EDriveZ); |
|
784 |
||
785 |
RFormat format; |
|
786 |
TUint fmtMode=0; |
|
787 |
TInt fmtCnt=0; |
|
788 |
TInt prevCnt; |
|
789 |
TInt nRes; |
|
790 |
||
791 |
if(aQuickFormat) |
|
792 |
fmtMode |= EQuickFormat; |
|
793 |
||
794 |
//if(aForceErase) |
|
795 |
// fmtMode |= EForceErase; |
|
796 |
||
797 |
TBuf<10> drvName; |
|
798 |
drvName.Format(_L("%C:"),'A'+aDrive); |
|
799 |
||
800 |
nRes = format.Open(aFs, drvName, fmtMode, fmtCnt); |
|
801 |
if(nRes!=KErrNone) |
|
802 |
goto Fail; |
|
803 |
||
804 |
//-- do format steps |
|
805 |
prevCnt=fmtCnt; |
|
806 |
while(fmtCnt) |
|
807 |
{ |
|
808 |
nRes = format.Next(fmtCnt); |
|
809 |
if(nRes!=KErrNone) |
|
810 |
goto Fail; |
|
811 |
||
812 |
if(fmtCnt != prevCnt) |
|
813 |
{ |
|
814 |
DoPrintf(_L(".")); |
|
815 |
prevCnt = fmtCnt; |
|
816 |
} |
|
817 |
} |
|
818 |
||
819 |
//-- formatting has finished |
|
820 |
DoPrintf(_L("\n")); |
|
821 |
format.Close(); |
|
822 |
return KErrNone; |
|
823 |
||
824 |
Fail: |
|
825 |
format.Close(); |
|
826 |
DoPrintf(_L("~ F32_Test_Utils::FormatFatDrive() failed! code:%d\n"), nRes); |
|
827 |
||
828 |
return nRes; |
|
829 |
} |
|
830 |
||
831 |
||
832 |
#endif //FAT_UTILS_LEAN_AND_MEAN |
|
833 |
||
834 |
||
835 |
//------------------------------------------------------------------------------------------------------------------- |
|
836 |
/** |
|
837 |
printing interface. Prints out to the console (if is set) and to the debug interface |
|
838 |
if pConsole is NULL will print to the debug port only. |
|
839 |
*/ |
|
840 |
void F32_Test_Utils::DoPrintf(TRefByValue<const TDesC> aFmt,...) |
|
841 |
{ |
|
842 |
#ifndef FAT_UTILS_LEAN_AND_MEAN |
|
843 |
if(!bPrintOutEnabled) |
|
844 |
return; //-- disabled by global flag |
|
845 |
#endif //FAT_UTILS_LEAN_AND_MEAN |
|
846 |
||
847 |
VA_LIST list; |
|
848 |
VA_START(list, aFmt); |
|
849 |
||
850 |
TBuf<0x100> buf; |
|
851 |
buf.FormatList(aFmt, list); //-- ignore overflows |
|
852 |
||
853 |
#ifndef FAT_UTILS_LEAN_AND_MEAN |
|
854 |
if(pConsole) |
|
855 |
{ |
|
856 |
pConsole->Write(buf); |
|
857 |
} |
|
858 |
#endif //FAT_UTILS_LEAN_AND_MEAN |
|
859 |
||
860 |
const TInt bufLen = buf.Length(); |
|
861 |
if(bufLen >0 && buf[bufLen-1] == '\n') |
|
862 |
{ |
|
863 |
buf.Insert(bufLen-1, _L("\r")); |
|
864 |
} |
|
865 |
||
866 |
RDebug::RawPrint(buf); |
|
867 |
} |
|
868 |
||
869 |
//------------------------------------------------------------------------------------------------------------------- |
|
870 |
||
871 |
TBool F32_Test_Utils::IsPowerOf2(TUint32 aVal) |
|
872 |
{ |
|
873 |
if (aVal==0) |
|
874 |
return EFalse; |
|
875 |
||
876 |
return !(aVal & (aVal-1)); |
|
877 |
} |
|
878 |
||
879 |
||
880 |
//------------------------------------------------------------------------------------------------------------------- |
|
881 |
TUint32 F32_Test_Utils::Log2(TUint32 aVal) |
|
882 |
{ |
|
883 |
__ASSERT_COMPILE(sizeof(TUint32) == 4); |
|
884 |
ASSERT(aVal); |
|
885 |
||
886 |
TUint32 bitPos=31; |
|
887 |
||
888 |
if(!(aVal >> 16)) {bitPos-=16; aVal<<=16;} |
|
889 |
if(!(aVal >> 24)) {bitPos-=8; aVal<<=8 ;} |
|
890 |
if(!(aVal >> 28)) {bitPos-=4; aVal<<=4 ;} |
|
891 |
if(!(aVal >> 30)) {bitPos-=2; aVal<<=2 ;} |
|
892 |
if(!(aVal >> 31)) {bitPos-=1;} |
|
893 |
||
894 |
return bitPos; |
|
895 |
} |
|
896 |
||
897 |
||
898 |
//------------------------------------------------------------------------------------------------------------------- |
|
899 |
||
900 |
//################################################################################################################### |
|
901 |
//# TMD5 class implementation |
|
902 |
//################################################################################################################### |
|
903 |
||
904 |
||
905 |
#define T_MASK ((TUint32)~0) |
|
906 |
#define T1 /* 0xd76aa478 */ (T_MASK ^ 0x28955b87) |
|
907 |
#define T2 /* 0xe8c7b756 */ (T_MASK ^ 0x173848a9) |
|
908 |
#define T3 0x242070db |
|
909 |
#define T4 /* 0xc1bdceee */ (T_MASK ^ 0x3e423111) |
|
910 |
#define T5 /* 0xf57c0faf */ (T_MASK ^ 0x0a83f050) |
|
911 |
#define T6 0x4787c62a |
|
912 |
#define T7 /* 0xa8304613 */ (T_MASK ^ 0x57cfb9ec) |
|
913 |
#define T8 /* 0xfd469501 */ (T_MASK ^ 0x02b96afe) |
|
914 |
#define T9 0x698098d8 |
|
915 |
#define T10 /* 0x8b44f7af */ (T_MASK ^ 0x74bb0850) |
|
916 |
#define T11 /* 0xffff5bb1 */ (T_MASK ^ 0x0000a44e) |
|
917 |
#define T12 /* 0x895cd7be */ (T_MASK ^ 0x76a32841) |
|
918 |
#define T13 0x6b901122 |
|
919 |
#define T14 /* 0xfd987193 */ (T_MASK ^ 0x02678e6c) |
|
920 |
#define T15 /* 0xa679438e */ (T_MASK ^ 0x5986bc71) |
|
921 |
#define T16 0x49b40821 |
|
922 |
#define T17 /* 0xf61e2562 */ (T_MASK ^ 0x09e1da9d) |
|
923 |
#define T18 /* 0xc040b340 */ (T_MASK ^ 0x3fbf4cbf) |
|
924 |
#define T19 0x265e5a51 |
|
925 |
#define T20 /* 0xe9b6c7aa */ (T_MASK ^ 0x16493855) |
|
926 |
#define T21 /* 0xd62f105d */ (T_MASK ^ 0x29d0efa2) |
|
927 |
#define T22 0x02441453 |
|
928 |
#define T23 /* 0xd8a1e681 */ (T_MASK ^ 0x275e197e) |
|
929 |
#define T24 /* 0xe7d3fbc8 */ (T_MASK ^ 0x182c0437) |
|
930 |
#define T25 0x21e1cde6 |
|
931 |
#define T26 /* 0xc33707d6 */ (T_MASK ^ 0x3cc8f829) |
|
932 |
#define T27 /* 0xf4d50d87 */ (T_MASK ^ 0x0b2af278) |
|
933 |
#define T28 0x455a14ed |
|
934 |
#define T29 /* 0xa9e3e905 */ (T_MASK ^ 0x561c16fa) |
|
935 |
#define T30 /* 0xfcefa3f8 */ (T_MASK ^ 0x03105c07) |
|
936 |
#define T31 0x676f02d9 |
|
937 |
#define T32 /* 0x8d2a4c8a */ (T_MASK ^ 0x72d5b375) |
|
938 |
#define T33 /* 0xfffa3942 */ (T_MASK ^ 0x0005c6bd) |
|
939 |
#define T34 /* 0x8771f681 */ (T_MASK ^ 0x788e097e) |
|
940 |
#define T35 0x6d9d6122 |
|
941 |
#define T36 /* 0xfde5380c */ (T_MASK ^ 0x021ac7f3) |
|
942 |
#define T37 /* 0xa4beea44 */ (T_MASK ^ 0x5b4115bb) |
|
943 |
#define T38 0x4bdecfa9 |
|
944 |
#define T39 /* 0xf6bb4b60 */ (T_MASK ^ 0x0944b49f) |
|
945 |
#define T40 /* 0xbebfbc70 */ (T_MASK ^ 0x4140438f) |
|
946 |
#define T41 0x289b7ec6 |
|
947 |
#define T42 /* 0xeaa127fa */ (T_MASK ^ 0x155ed805) |
|
948 |
#define T43 /* 0xd4ef3085 */ (T_MASK ^ 0x2b10cf7a) |
|
949 |
#define T44 0x04881d05 |
|
950 |
#define T45 /* 0xd9d4d039 */ (T_MASK ^ 0x262b2fc6) |
|
951 |
#define T46 /* 0xe6db99e5 */ (T_MASK ^ 0x1924661a) |
|
952 |
#define T47 0x1fa27cf8 |
|
953 |
#define T48 /* 0xc4ac5665 */ (T_MASK ^ 0x3b53a99a) |
|
954 |
#define T49 /* 0xf4292244 */ (T_MASK ^ 0x0bd6ddbb) |
|
955 |
#define T50 0x432aff97 |
|
956 |
#define T51 /* 0xab9423a7 */ (T_MASK ^ 0x546bdc58) |
|
957 |
#define T52 /* 0xfc93a039 */ (T_MASK ^ 0x036c5fc6) |
|
958 |
#define T53 0x655b59c3 |
|
959 |
#define T54 /* 0x8f0ccc92 */ (T_MASK ^ 0x70f3336d) |
|
960 |
#define T55 /* 0xffeff47d */ (T_MASK ^ 0x00100b82) |
|
961 |
#define T56 /* 0x85845dd1 */ (T_MASK ^ 0x7a7ba22e) |
|
962 |
#define T57 0x6fa87e4f |
|
963 |
#define T58 /* 0xfe2ce6e0 */ (T_MASK ^ 0x01d3191f) |
|
964 |
#define T59 /* 0xa3014314 */ (T_MASK ^ 0x5cfebceb) |
|
965 |
#define T60 0x4e0811a1 |
|
966 |
#define T61 /* 0xf7537e82 */ (T_MASK ^ 0x08ac817d) |
|
967 |
#define T62 /* 0xbd3af235 */ (T_MASK ^ 0x42c50dca) |
|
968 |
#define T63 0x2ad7d2bb |
|
969 |
#define T64 /* 0xeb86d391 */ (T_MASK ^ 0x14792c6e) |
|
970 |
||
971 |
||
972 |
TMD5::TMD5() |
|
973 |
{ |
|
974 |
Reset(); |
|
975 |
} |
|
976 |
||
977 |
//------------------------------------------------------------------------------------------------------------------- |
|
978 |
||
979 |
void TMD5::Md5_process(const TUint8 *data /*[64]*/) |
|
980 |
{ |
|
981 |
TUint32 |
|
982 |
a = iState.abcd[0], b = iState.abcd[1], |
|
983 |
c = iState.abcd[2], d = iState.abcd[3]; |
|
984 |
TUint32 t; |
|
985 |
TUint32 xbuf[16]; |
|
986 |
const TUint32 *X; |
|
987 |
||
988 |
{ |
|
989 |
static const TInt w = 1; |
|
990 |
if (*((const TUint8 *)&w)) |
|
991 |
{ |
|
992 |
if (!((data - (const TUint8 *)0) & 3)) { |
|
993 |
X = (const TUint32 *)data; |
|
994 |
} else { |
|
995 |
memcpy(xbuf, data, 64); |
|
996 |
X = xbuf; |
|
997 |
} |
|
998 |
} |
|
999 |
else |
|
1000 |
{ |
|
1001 |
const TUint8 *xp = data; |
|
1002 |
TInt i; |
|
1003 |
||
1004 |
X = xbuf; /* (dynamic only) */ |
|
1005 |
for (i = 0; i < 16; ++i, xp += 4) |
|
1006 |
xbuf[i] = xp[0] + (xp[1] << 8) + (xp[2] << 16) + (xp[3] << 24); |
|
1007 |
} |
|
1008 |
} |
|
1009 |
||
1010 |
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) |
|
1011 |
||
1012 |
#define F(x, y, z) (((x) & (y)) | (~(x) & (z))) |
|
1013 |
#define SET(a, b, c, d, k, s, Ti)\ |
|
1014 |
t = a + F(b,c,d) + X[k] + Ti;\ |
|
1015 |
a = ROTATE_LEFT(t, s) + b |
|
1016 |
SET(a, b, c, d, 0, 7, T1); |
|
1017 |
SET(d, a, b, c, 1, 12, T2); |
|
1018 |
SET(c, d, a, b, 2, 17, T3); |
|
1019 |
SET(b, c, d, a, 3, 22, T4); |
|
1020 |
SET(a, b, c, d, 4, 7, T5); |
|
1021 |
SET(d, a, b, c, 5, 12, T6); |
|
1022 |
SET(c, d, a, b, 6, 17, T7); |
|
1023 |
SET(b, c, d, a, 7, 22, T8); |
|
1024 |
SET(a, b, c, d, 8, 7, T9); |
|
1025 |
SET(d, a, b, c, 9, 12, T10); |
|
1026 |
SET(c, d, a, b, 10, 17, T11); |
|
1027 |
SET(b, c, d, a, 11, 22, T12); |
|
1028 |
SET(a, b, c, d, 12, 7, T13); |
|
1029 |
SET(d, a, b, c, 13, 12, T14); |
|
1030 |
SET(c, d, a, b, 14, 17, T15); |
|
1031 |
SET(b, c, d, a, 15, 22, T16); |
|
1032 |
#undef SET |
|
1033 |
||
1034 |
#define G(x, y, z) (((x) & (z)) | ((y) & ~(z))) |
|
1035 |
#define SET(a, b, c, d, k, s, Ti)\ |
|
1036 |
t = a + G(b,c,d) + X[k] + Ti;\ |
|
1037 |
a = ROTATE_LEFT(t, s) + b |
|
1038 |
SET(a, b, c, d, 1, 5, T17); |
|
1039 |
SET(d, a, b, c, 6, 9, T18); |
|
1040 |
SET(c, d, a, b, 11, 14, T19); |
|
1041 |
SET(b, c, d, a, 0, 20, T20); |
|
1042 |
SET(a, b, c, d, 5, 5, T21); |
|
1043 |
SET(d, a, b, c, 10, 9, T22); |
|
1044 |
SET(c, d, a, b, 15, 14, T23); |
|
1045 |
SET(b, c, d, a, 4, 20, T24); |
|
1046 |
SET(a, b, c, d, 9, 5, T25); |
|
1047 |
SET(d, a, b, c, 14, 9, T26); |
|
1048 |
SET(c, d, a, b, 3, 14, T27); |
|
1049 |
SET(b, c, d, a, 8, 20, T28); |
|
1050 |
SET(a, b, c, d, 13, 5, T29); |
|
1051 |
SET(d, a, b, c, 2, 9, T30); |
|
1052 |
SET(c, d, a, b, 7, 14, T31); |
|
1053 |
SET(b, c, d, a, 12, 20, T32); |
|
1054 |
#undef SET |
|
1055 |
||
1056 |
#define H(x, y, z) ((x) ^ (y) ^ (z)) |
|
1057 |
#define SET(a, b, c, d, k, s, Ti)\ |
|
1058 |
t = a + H(b,c,d) + X[k] + Ti;\ |
|
1059 |
a = ROTATE_LEFT(t, s) + b |
|
1060 |
SET(a, b, c, d, 5, 4, T33); |
|
1061 |
SET(d, a, b, c, 8, 11, T34); |
|
1062 |
SET(c, d, a, b, 11, 16, T35); |
|
1063 |
SET(b, c, d, a, 14, 23, T36); |
|
1064 |
SET(a, b, c, d, 1, 4, T37); |
|
1065 |
SET(d, a, b, c, 4, 11, T38); |
|
1066 |
SET(c, d, a, b, 7, 16, T39); |
|
1067 |
SET(b, c, d, a, 10, 23, T40); |
|
1068 |
SET(a, b, c, d, 13, 4, T41); |
|
1069 |
SET(d, a, b, c, 0, 11, T42); |
|
1070 |
SET(c, d, a, b, 3, 16, T43); |
|
1071 |
SET(b, c, d, a, 6, 23, T44); |
|
1072 |
SET(a, b, c, d, 9, 4, T45); |
|
1073 |
SET(d, a, b, c, 12, 11, T46); |
|
1074 |
SET(c, d, a, b, 15, 16, T47); |
|
1075 |
SET(b, c, d, a, 2, 23, T48); |
|
1076 |
#undef SET |
|
1077 |
||
1078 |
#define I(x, y, z) ((y) ^ ((x) | ~(z))) |
|
1079 |
#define SET(a, b, c, d, k, s, Ti)\ |
|
1080 |
t = a + I(b,c,d) + X[k] + Ti;\ |
|
1081 |
a = ROTATE_LEFT(t, s) + b |
|
1082 |
SET(a, b, c, d, 0, 6, T49); |
|
1083 |
SET(d, a, b, c, 7, 10, T50); |
|
1084 |
SET(c, d, a, b, 14, 15, T51); |
|
1085 |
SET(b, c, d, a, 5, 21, T52); |
|
1086 |
SET(a, b, c, d, 12, 6, T53); |
|
1087 |
SET(d, a, b, c, 3, 10, T54); |
|
1088 |
SET(c, d, a, b, 10, 15, T55); |
|
1089 |
SET(b, c, d, a, 1, 21, T56); |
|
1090 |
SET(a, b, c, d, 8, 6, T57); |
|
1091 |
SET(d, a, b, c, 15, 10, T58); |
|
1092 |
SET(c, d, a, b, 6, 15, T59); |
|
1093 |
SET(b, c, d, a, 13, 21, T60); |
|
1094 |
SET(a, b, c, d, 4, 6, T61); |
|
1095 |
SET(d, a, b, c, 11, 10, T62); |
|
1096 |
SET(c, d, a, b, 2, 15, T63); |
|
1097 |
SET(b, c, d, a, 9, 21, T64); |
|
1098 |
#undef SET |
|
1099 |
||
1100 |
iState.abcd[0] += a; |
|
1101 |
iState.abcd[1] += b; |
|
1102 |
iState.abcd[2] += c; |
|
1103 |
iState.abcd[3] += d; |
|
1104 |
||
1105 |
} |
|
1106 |
||
1107 |
//------------------------------------------------------------------------------------------------------------------- |
|
1108 |
void TMD5::Md5_append(const TUint8 *data, TInt nbytes) |
|
1109 |
{ |
|
1110 |
const TUint8 *p = data; |
|
1111 |
||
1112 |
TInt left = nbytes; |
|
1113 |
||
1114 |
TInt offset = (iState.count[0] >> 3) & 63; |
|
1115 |
TUint32 nbits = (TUint32)(nbytes << 3); |
|
1116 |
||
1117 |
if (nbytes <= 0) |
|
1118 |
return; |
|
1119 |
||
1120 |
iState.count[1] += nbytes >> 29; |
|
1121 |
iState.count[0] += nbits; |
|
1122 |
if (iState.count[0] < nbits) |
|
1123 |
iState.count[1]++; |
|
1124 |
||
1125 |
if (offset) |
|
1126 |
{ |
|
1127 |
TInt copy = (offset + nbytes > 64 ? 64 - offset : nbytes); |
|
1128 |
||
1129 |
memcpy(iState.buf + offset, p, copy); |
|
1130 |
if (offset + copy < 64) |
|
1131 |
return; |
|
1132 |
p += copy; |
|
1133 |
left -= copy; |
|
1134 |
Md5_process(iState.buf); |
|
1135 |
} |
|
1136 |
||
1137 |
for (; left >= 64; p += 64, left -= 64) |
|
1138 |
Md5_process(p); |
|
1139 |
||
1140 |
if (left) |
|
1141 |
memcpy(iState.buf, p, left); |
|
1142 |
||
1143 |
} |
|
1144 |
||
1145 |
||
1146 |
void TMD5::Md5_finish() |
|
1147 |
{ |
|
1148 |
static const TUint8 pad[64] = { |
|
1149 |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1150 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1151 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
1152 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
|
1153 |
}; |
|
1154 |
TUint8 data[8]; |
|
1155 |
TInt i; |
|
1156 |
||
1157 |
for (i = 0; i < 8; ++i) |
|
1158 |
data[i] = (TUint8)(iState.count[i >> 2] >> ((i & 3) << 3)); |
|
1159 |
Md5_append(pad, ((55 - (iState.count[0] >> 3)) & 63) + 1); |
|
1160 |
Md5_append(data, 8); |
|
1161 |
for (i = 0; i < 16; ++i) |
|
1162 |
iDigest[i] = (TUint8)(iState.abcd[i >> 2] >> ((i & 3) << 3)); |
|
1163 |
||
1164 |
} |
|
1165 |
||
1166 |
//------------------------------------------------------------------------------------------------------------------- |
|
1167 |
||
1168 |
/** reset MD5 to initial state */ |
|
1169 |
void TMD5::Reset() |
|
1170 |
{ |
|
1171 |
iState.count[0] = iState.count[1] = 0; |
|
1172 |
iState.abcd[0] = 0x67452301; |
|
1173 |
iState.abcd[1] = /*0xefcdab89*/ T_MASK ^ 0x10325476; |
|
1174 |
iState.abcd[2] = /*0x98badcfe*/ T_MASK ^ 0x67452301; |
|
1175 |
iState.abcd[3] = 0x10325476; |
|
1176 |
} |
|
1177 |
||
1178 |
||
1179 |
/** |
|
1180 |
Update MD5 with some data |
|
1181 |
@param aMessage descriptor with data |
|
1182 |
*/ |
|
1183 |
void TMD5::Update(const TDesC8& aMessage) |
|
1184 |
{ |
|
1185 |
Md5_append((const TUint8*)aMessage.Ptr(), aMessage.Length()); |
|
1186 |
} |
|
1187 |
||
1188 |
/** |
|
1189 |
Finalise MD5 calculation |
|
1190 |
@param aMessage descriptor with data |
|
1191 |
@return pointer to 16-byte array with MD5 hash |
|
1192 |
*/ |
|
1193 |
TPtrC8 TMD5::Final(const TDesC8& aMessage) |
|
1194 |
{ |
|
1195 |
Update(aMessage); |
|
1196 |
Md5_finish(); |
|
1197 |
return TPtrC8(iDigest, HashSize); |
|
1198 |
} |
|
1199 |
||
1200 |
||
1201 |
/** |
|
1202 |
Finalise MD5 calculation |
|
1203 |
@return pointer to 16-byte array with MD5 hash |
|
1204 |
*/ |
|
1205 |
TPtrC8 TMD5::Final() |
|
1206 |
{ |
|
1207 |
Md5_finish(); |
|
1208 |
return TPtrC8(iDigest, HashSize); |
|
1209 |
} |
|
1210 |
||
1211 |
||
1212 |
||
1213 |
||
1214 |
||
1215 |
||
1216 |
||
1217 |
||
1218 |
||
1219 |
||
1220 |
||
1221 |
||
1222 |
||
1223 |
||
1224 |
||
1225 |
||
1226 |
||
1227 |
||
1228 |
||
1229 |
||
1230 |
||
1231 |