secureswitools/swisistools/source/sisxlibrary/checksum.h
changeset 0 ba25891c3a9e
child 26 04d4a7bbc3e0
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * Note: This file may contain code to generate corrupt files for test purposes.
       
    16 * Such code is excluded from production builds by use of compiler defines;
       
    17 * it is recommended that such code should be removed if this code is ever published publicly.
       
    18 * generic base for all SIS checksums
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 /**
       
    24  @file 
       
    25  @internalComponent
       
    26  @released
       
    27 */
       
    28 
       
    29 #ifndef __CHECKSUM_H__
       
    30 #define __CHECKSUM_H__
       
    31 
       
    32 #include "structure.h"
       
    33 #include "numeric.h"
       
    34 
       
    35 #include <stdlib.h>
       
    36 
       
    37 
       
    38 template <CSISFieldRoot::TFieldType FieldType> class CChecksum : public CStructure <FieldType>
       
    39 	{
       
    40 public:
       
    41 	/**
       
    42 	 * Default constructor
       
    43 	 */
       
    44 	CChecksum (const bool aRequired = true);
       
    45 	/**
       
    46 	 * Copy Constructor
       
    47 	 */
       
    48 	CChecksum (const CChecksum& aInitialiser);
       
    49 	
       
    50 public:
       
    51 	/**
       
    52 	 * Class name
       
    53 	 */
       
    54 	virtual std::string Name () const;
       
    55 	/**
       
    56 	 * Checks if the structure is required or not. Used for externalizing
       
    57 	 * the class. If its a waste of space then we need not write this into
       
    58 	 * the file.
       
    59 	 */
       
    60 	virtual bool WasteOfSpace () const;
       
    61 #ifdef GENERATE_ERRORS
       
    62 	virtual void CreateDefects ();
       
    63 #endif // GENERATE_ERRORS
       
    64 	/**
       
    65 	 * Set the CRC value
       
    66 	 * @param aCrc new CRC value
       
    67 	 */
       
    68 	void Set (const TCRC aCrc);
       
    69 	/**
       
    70 	 * Adds the write the package details into the stream.
       
    71 	 * @param aStream - Stream in which the package entries need to be written.
       
    72 	 * @param aVerbose - If this option is set then detail description of pkg
       
    73 	 * 			will be written into the stream.
       
    74 	 */
       
    75 	void AddPackageEntry(std::wostream& aStream, bool aVerbose) const;
       
    76 
       
    77 private:
       
    78 	CSISUInt16	iDataChecksum;
       
    79 	};
       
    80 
       
    81 
       
    82 template <CSISFieldRoot::TFieldType FieldType> inline
       
    83 	CChecksum <FieldType>::CChecksum (const bool aRequired) :
       
    84 		CStructure <FieldType> (aRequired)
       
    85 	{ 
       
    86 	InsertMember (iDataChecksum); 
       
    87 	}
       
    88 
       
    89 template <CSISFieldRoot::TFieldType FieldType> inline
       
    90 	CChecksum <FieldType>::CChecksum (const CChecksum& aInitialiser) :
       
    91 		CStructure <FieldType> (aInitialiser),
       
    92 		iDataChecksum (aInitialiser.iDataChecksum)
       
    93 	{ 
       
    94 	InsertMember (iDataChecksum); 
       
    95 	}
       
    96 
       
    97 template <CSISFieldRoot::TFieldType FieldType> inline
       
    98 		std::string CChecksum <FieldType>::Name () const
       
    99 	{
       
   100 	return "Checksum";
       
   101 	}
       
   102 
       
   103 template <CSISFieldRoot::TFieldType FieldType> inline
       
   104 		void CChecksum <FieldType>::Set (const TCRC aCrc)
       
   105 	{
       
   106 	iDataChecksum = aCrc; 
       
   107 	}
       
   108 
       
   109 
       
   110 template <CSISFieldRoot::TFieldType FieldType> inline
       
   111 	bool CChecksum <FieldType>::WasteOfSpace () const
       
   112 	{
       
   113 	return iDataChecksum.Value()? false: true;
       
   114 	}
       
   115 
       
   116 
       
   117 #ifdef GENERATE_ERRORS
       
   118 template <CSISFieldRoot::TFieldType FieldType>
       
   119 		void CChecksum <FieldType>::CreateDefects ()
       
   120 	{
       
   121 	if (IsBugToBeCreated (CSISFieldRoot::EBugCRCError))
       
   122 		{
       
   123 		iDataChecksum |= static_cast <TUint16> (rand () & 0xFFFF);
       
   124 		}
       
   125 	}
       
   126 #endif // GENERATE_ERRORS
       
   127 
       
   128 template <CSISFieldRoot::TFieldType FieldType> inline
       
   129 		void CChecksum <FieldType>::AddPackageEntry(std::wostream& aStream, bool aVerbose) const
       
   130 	{
       
   131 	if(aVerbose)
       
   132 		{
       
   133 		aStream << L"; CRC16: " << std::hex << iDataChecksum.Value() << std::dec << std::endl;
       
   134 		}
       
   135 	}
       
   136 
       
   137 #endif // __SISDATACHECKSUM_H__
       
   138