os/kernelhwsrv/kernel/eka/debug/crashMonitor/src/scmchksum.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kernel/eka/debug/crashMonitor/src/scmchksum.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,168 @@
     1.4 +// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32\debug\crashMonitor\src\scmchksum.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +/**
    1.22 + @file
    1.23 + @internalTechnology
    1.24 +*/
    1.25 +
    1.26 +#include <scmdatatypes.h>
    1.27 +
    1.28 +namespace Debug
    1.29 +	{	
    1.30 +	/**
    1.31 +	 * Constructor
    1.32 +	 */
    1.33 +	TScmChecksum::TScmChecksum()
    1.34 +	: iLength(0)	
    1.35 +	, iSum(0)
    1.36 +	, iZeroCount(0)
    1.37 +		{	
    1.38 +		}
    1.39 +	
    1.40 +	/**
    1.41 +	 * ChecksumBlock - calculate checksum values for given data
    1.42 +	 * @param aData - the data to checksum
    1.43 +	 * @param aLen - the length of the data to checksum
    1.44 +	 * @return void
    1.45 +	 */
    1.46 +	void TScmChecksum::ChecksumBlock(const TUint8* aData, TUint aLen)
    1.47 +		{		
    1.48 +		/**
    1.49 +		 * 
    1.50 +		 * Note there is Symbian CRC implementation to be found in the following
    1.51 +		 * \src\cedar\generic\tools\e32tools\elf2e32\source\checksum.h
    1.52 +		 * \src\cedar\generic\tools\e32tools\elf2e32\source\checksum.cpp
    1.53 +		 * this however may be no good to us
    1.54 +		 * as we need to produce a single checksum even though the entire data may be
    1.55 +		 * read in different size blocks - and the entire data may not be available
    1.56 +		 * (the comm port requirement is for read only )
    1.57 +		 * If we do however want to use the CRC then this is the place to insert the code
    1.58 +		 */		
    1.59 +		if(!aData)
    1.60 +			{
    1.61 +			return;
    1.62 +			}
    1.63 +			
    1.64 +		for(TUint i=0;i<aLen;i++)
    1.65 +			{
    1.66 +			TUint8 val = *(aData+i);
    1.67 +			
    1.68 +			iLength++;
    1.69 +			if(val == 0)
    1.70 +				{
    1.71 +				iZeroCount++;
    1.72 +				}
    1.73 +			else
    1.74 +				{
    1.75 +				iSum += val;
    1.76 +				}			
    1.77 +			}		
    1.78 +		}
    1.79 +	
    1.80 +	/**
    1.81 +	 * ChecksumBlock - calculate checksum values for given data
    1.82 +	 * @param aData - descriptor containing the data to checksum
    1.83 +	 * @return void
    1.84 +	 */
    1.85 +	void TScmChecksum::ChecksumBlock(const TDesC8& aDes)
    1.86 +		{
    1.87 +		ChecksumBlock(aDes.Ptr(), aDes.Length());
    1.88 +		}
    1.89 +	
    1.90 +	/**
    1.91 +	 * ChecksumBlock - operator ==
    1.92 +	 * @param aOther - the TScmChecksum to compare too
    1.93 +	 * @return ETrue is objects match - otherwise EFalse
    1.94 +	 */
    1.95 +	TBool TScmChecksum::operator == (const TScmChecksum& aOther) const
    1.96 +		{
    1.97 +		return (iLength == aOther.iLength && iSum == aOther.iSum && iZeroCount == aOther.iZeroCount);
    1.98 +		}
    1.99 +
   1.100 +	/**
   1.101 +	 * ChecksumBlock - operator !=
   1.102 +	 * @param aOther - the TScmChecksum to compare too
   1.103 +	 * @return EFalse if objects match - otherwise ETrue
   1.104 +	 */
   1.105 +	TBool TScmChecksum::operator != (const TScmChecksum& aOther) const
   1.106 +		{
   1.107 +		return !(*this == aOther);
   1.108 +		}
   1.109 +	
   1.110 +	/**
   1.111 +	 * GetSize 
   1.112 +	 * @return size of this object when streamed in bytes
   1.113 +	 */
   1.114 +	TInt TScmChecksum::GetSize() const
   1.115 + 		{
   1.116 + 		return 12;
   1.117 + 		}
   1.118 +	
   1.119 +	/**
   1.120 +	 * Serialize - writes this object to the supplied byte stream
   1.121 +	 * @param aItem -  aWriter - the TByteStreamWriter that will be written to
   1.122 +	 * @return One of the OS wide codes
   1.123 +	 */
   1.124 +	TInt TScmChecksum::Serialize(TByteStreamWriter& aWriter)
   1.125 +		{	
   1.126 +		TInt startPos = aWriter.CurrentPosition();
   1.127 +		aWriter.WriteInt(iLength);
   1.128 +		aWriter.WriteInt(iSum);
   1.129 +		aWriter.WriteInt(iZeroCount);
   1.130 +		TInt sizeWritten = aWriter.CurrentPosition() - startPos;
   1.131 +		if(sizeWritten != GetSize())
   1.132 +			{
   1.133 +			// error between actual size & real size in data
   1.134 +			CLTRACE("TScmChecksum serialization size error");	
   1.135 +			return KErrCorrupt;
   1.136 +			}
   1.137 +		return KErrNone;
   1.138 +		}
   1.139 +	
   1.140 +
   1.141 +	/**
   1.142 +	 * Deserialize - read this objects state from the supplied byte stream
   1.143 +	 * @param aItem -  aReader - the TByteStreamReader that will be read from
   1.144 +	 * @return One of the OS wide codes
   1.145 +	 */
   1.146 +	TInt TScmChecksum::Deserialize(TByteStreamReader& aReader)
   1.147 +		{
   1.148 +		TInt startPos = aReader.CurrentPosition();
   1.149 +		// do we need a version check here - will it change ?
   1.150 +		iLength = aReader.ReadInt();	
   1.151 +		iSum = aReader.ReadInt();
   1.152 +		iZeroCount = aReader.ReadInt();	
   1.153 +
   1.154 +		TInt sizeRead = aReader.CurrentPosition() - startPos;
   1.155 +		if(sizeRead != GetSize())
   1.156 +			{
   1.157 +			// error between actual size & real size in data
   1.158 +			CLTRACE("TScmChecksum Deserialization size error");	
   1.159 +			return KErrCorrupt;
   1.160 +			}
   1.161 +		return KErrCorrupt;
   1.162 +		}
   1.163 +	
   1.164 +	
   1.165 +	void TScmChecksum::Reset()
   1.166 +		{
   1.167 +		iLength = iSum = iZeroCount = 0;	
   1.168 +		}		
   1.169 +	}
   1.170 +//eof
   1.171 +