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 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 // e32\debug\crashMonitor\src\scmchksum.cpp |
|
15 // |
|
16 // |
|
17 |
|
18 /** |
|
19 @file |
|
20 @internalTechnology |
|
21 */ |
|
22 |
|
23 #include <scmdatatypes.h> |
|
24 |
|
25 namespace Debug |
|
26 { |
|
27 /** |
|
28 * Constructor |
|
29 */ |
|
30 TScmChecksum::TScmChecksum() |
|
31 : iLength(0) |
|
32 , iSum(0) |
|
33 , iZeroCount(0) |
|
34 { |
|
35 } |
|
36 |
|
37 /** |
|
38 * ChecksumBlock - calculate checksum values for given data |
|
39 * @param aData - the data to checksum |
|
40 * @param aLen - the length of the data to checksum |
|
41 * @return void |
|
42 */ |
|
43 void TScmChecksum::ChecksumBlock(const TUint8* aData, TUint aLen) |
|
44 { |
|
45 /** |
|
46 * |
|
47 * Note there is Symbian CRC implementation to be found in the following |
|
48 * \src\cedar\generic\tools\e32tools\elf2e32\source\checksum.h |
|
49 * \src\cedar\generic\tools\e32tools\elf2e32\source\checksum.cpp |
|
50 * this however may be no good to us |
|
51 * as we need to produce a single checksum even though the entire data may be |
|
52 * read in different size blocks - and the entire data may not be available |
|
53 * (the comm port requirement is for read only ) |
|
54 * If we do however want to use the CRC then this is the place to insert the code |
|
55 */ |
|
56 if(!aData) |
|
57 { |
|
58 return; |
|
59 } |
|
60 |
|
61 for(TUint i=0;i<aLen;i++) |
|
62 { |
|
63 TUint8 val = *(aData+i); |
|
64 |
|
65 iLength++; |
|
66 if(val == 0) |
|
67 { |
|
68 iZeroCount++; |
|
69 } |
|
70 else |
|
71 { |
|
72 iSum += val; |
|
73 } |
|
74 } |
|
75 } |
|
76 |
|
77 /** |
|
78 * ChecksumBlock - calculate checksum values for given data |
|
79 * @param aData - descriptor containing the data to checksum |
|
80 * @return void |
|
81 */ |
|
82 void TScmChecksum::ChecksumBlock(const TDesC8& aDes) |
|
83 { |
|
84 ChecksumBlock(aDes.Ptr(), aDes.Length()); |
|
85 } |
|
86 |
|
87 /** |
|
88 * ChecksumBlock - operator == |
|
89 * @param aOther - the TScmChecksum to compare too |
|
90 * @return ETrue is objects match - otherwise EFalse |
|
91 */ |
|
92 TBool TScmChecksum::operator == (const TScmChecksum& aOther) const |
|
93 { |
|
94 return (iLength == aOther.iLength && iSum == aOther.iSum && iZeroCount == aOther.iZeroCount); |
|
95 } |
|
96 |
|
97 /** |
|
98 * ChecksumBlock - operator != |
|
99 * @param aOther - the TScmChecksum to compare too |
|
100 * @return EFalse if objects match - otherwise ETrue |
|
101 */ |
|
102 TBool TScmChecksum::operator != (const TScmChecksum& aOther) const |
|
103 { |
|
104 return !(*this == aOther); |
|
105 } |
|
106 |
|
107 /** |
|
108 * GetSize |
|
109 * @return size of this object when streamed in bytes |
|
110 */ |
|
111 TInt TScmChecksum::GetSize() const |
|
112 { |
|
113 return 12; |
|
114 } |
|
115 |
|
116 /** |
|
117 * Serialize - writes this object to the supplied byte stream |
|
118 * @param aItem - aWriter - the TByteStreamWriter that will be written to |
|
119 * @return One of the OS wide codes |
|
120 */ |
|
121 TInt TScmChecksum::Serialize(TByteStreamWriter& aWriter) |
|
122 { |
|
123 TInt startPos = aWriter.CurrentPosition(); |
|
124 aWriter.WriteInt(iLength); |
|
125 aWriter.WriteInt(iSum); |
|
126 aWriter.WriteInt(iZeroCount); |
|
127 TInt sizeWritten = aWriter.CurrentPosition() - startPos; |
|
128 if(sizeWritten != GetSize()) |
|
129 { |
|
130 // error between actual size & real size in data |
|
131 CLTRACE("TScmChecksum serialization size error"); |
|
132 return KErrCorrupt; |
|
133 } |
|
134 return KErrNone; |
|
135 } |
|
136 |
|
137 |
|
138 /** |
|
139 * Deserialize - read this objects state from the supplied byte stream |
|
140 * @param aItem - aReader - the TByteStreamReader that will be read from |
|
141 * @return One of the OS wide codes |
|
142 */ |
|
143 TInt TScmChecksum::Deserialize(TByteStreamReader& aReader) |
|
144 { |
|
145 TInt startPos = aReader.CurrentPosition(); |
|
146 // do we need a version check here - will it change ? |
|
147 iLength = aReader.ReadInt(); |
|
148 iSum = aReader.ReadInt(); |
|
149 iZeroCount = aReader.ReadInt(); |
|
150 |
|
151 TInt sizeRead = aReader.CurrentPosition() - startPos; |
|
152 if(sizeRead != GetSize()) |
|
153 { |
|
154 // error between actual size & real size in data |
|
155 CLTRACE("TScmChecksum Deserialization size error"); |
|
156 return KErrCorrupt; |
|
157 } |
|
158 return KErrCorrupt; |
|
159 } |
|
160 |
|
161 |
|
162 void TScmChecksum::Reset() |
|
163 { |
|
164 iLength = iSum = iZeroCount = 0; |
|
165 } |
|
166 } |
|
167 //eof |
|
168 |
|