secureswitools/swisistools/source/sisxlibrary/structure.h
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-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 SIS structure container, provides common functionality on members, see fieldroot.h for purpose of functions
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 /**
       
    24  @file 
       
    25  @internalComponent
       
    26  @released
       
    27 */
       
    28 
       
    29 #ifndef __STRUCTURE_H__
       
    30 #define __STRUCTURE_H__
       
    31 
       
    32 #include "container.h"
       
    33 #include "fieldroot.h"
       
    34 
       
    35 template <CSISFieldRoot::TFieldType FieldType> class CStructure : public CContainer <CSISFieldRoot, FieldType>
       
    36 
       
    37 	{
       
    38 public:
       
    39 	CStructure ();
       
    40 	CStructure (const bool aRequired);
       
    41 	CStructure (const CStructure& s);
       
    42 
       
    43 	virtual void Read (TSISStream& aFile, const CSISFieldRoot::TFieldSize& aContainerSize, const CSISFieldRoot::TFieldType aArrayType = CSISFieldRoot::ESISUndefined);
       
    44 	
       
    45 	virtual void Write (TSISStream& aFile, const bool aIsArrayElement) const;  
       
    46 	virtual CSISFieldRoot::TFieldSize ByteCount (const bool aIsArray) const;
       
    47 	virtual void CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const;
       
    48 
       
    49 	TUint64 SkipRead (TSISStream& aFile, const CSISFieldRoot::TFieldSize& aContainerSize, const CSISFieldRoot::TFieldType aArrayType, const TUint32 aSkipCount, const bool aCompleteWithNormalRead);
       
    50 	void RawSkipWrite (TSISStream& aFile, const bool aIsArrayElement, TUint8* aRawBuffer, const TUint64 aBufferSize, const TUint32 aSkipCount) const;
       
    51 	
       
    52 protected:
       
    53 	void InsertMember (CSISFieldRoot& aMember);	
       
    54 		// intended to be used by inheriting constructors *ONLY*
       
    55 
       
    56 	};
       
    57 
       
    58 
       
    59 
       
    60 template <CSISFieldRoot::TFieldType FieldType> inline
       
    61 	CStructure <FieldType>::CStructure ()
       
    62 	{
       
    63 	}
       
    64 
       
    65 template <CSISFieldRoot::TFieldType FieldType> inline
       
    66 		CStructure <FieldType>::CStructure (const bool aRequired) :
       
    67 			CContainer<CSISFieldRoot, FieldType> (aRequired)
       
    68 	{
       
    69 	}
       
    70 
       
    71 template <CSISFieldRoot::TFieldType FieldType> inline
       
    72 		CStructure <FieldType>::CStructure (const CStructure& s) :
       
    73 			CContainer<CSISFieldRoot, FieldType> (s, 0)
       
    74 	{ 
       
    75 	}
       
    76 
       
    77 
       
    78 
       
    79 template <CSISFieldRoot::TFieldType FieldType>
       
    80 		void CStructure <FieldType>::Read (TSISStream& aFile, const CSISFieldRoot::TFieldSize& aContainerSize, const CSISFieldRoot::TFieldType aArrayType)
       
    81 	{
       
    82 	SetPreHeaderPos (aFile.tell ());
       
    83 
       
    84 	CSISHeader header (aArrayType);
       
    85 	if (IsDataThere (aFile, header, aArrayType))
       
    86 		{
       
    87 		SetByteCount (header.DataSize ());
       
    88 		TSISStream::pos_type pos = aFile.tell ();
       
    89 
       
    90 		SetPostHeaderPos (pos);
       
    91 #ifdef _DEBUG
       
    92 		TSISStream::pos_type expected = pos + static_cast <TSISStream::pos_type> (AlignedSize (header.DataSize ()));
       
    93 #endif
       
    94 		TUint64 available = aContainerSize;
       
    95 		for (ContainerIter(CSISFieldRoot) iterMemb = ContainerMem(CSISFieldRoot, FieldType).begin(); 
       
    96 				iterMemb != ContainerMem(CSISFieldRoot, FieldType).end(); 
       
    97 				++iterMemb)
       
    98 			{
       
    99 			(*iterMemb) -> Read (aFile, available);
       
   100 			available = aContainerSize - (aFile.tell () - pos);
       
   101 			}
       
   102 		ReadFiller (aFile);
       
   103 #ifdef _DEBUG
       
   104 		TSISStream::pos_type got = aFile.tell ();
       
   105 		assert (expected == got);
       
   106 #endif
       
   107 		}
       
   108 	}
       
   109 
       
   110 
       
   111 template <CSISFieldRoot::TFieldType FieldType>
       
   112 		TUint64 CStructure <FieldType>::SkipRead (TSISStream& aFile, const CSISFieldRoot::TFieldSize& aContainerSize, const CSISFieldRoot::TFieldType aArrayType, const TUint32 aSkipCount, const bool aCompleteWithNormalRead)
       
   113 	{
       
   114 	CSISHeader header (CSISFieldRoot::ESISUndefined);
       
   115 	TUint32 skipped = 0;
       
   116 	TUint64 offset = 0;
       
   117 	if (IsDataThere (aFile, header, aArrayType))
       
   118 		{
       
   119 		SetByteCount (header.DataSize ());
       
   120 		TSISStream::pos_type pos = aFile.tell ();
       
   121 #ifdef _DEBUG
       
   122 		TSISStream::pos_type expected = pos + static_cast <TSISStream::pos_type> (AlignedSize (header.DataSize ()));
       
   123 #endif
       
   124 		TUint64 available = aContainerSize;
       
   125 		for (ContainerIter(CSISFieldRoot) iterMemb = ContainerMem(CSISFieldRoot,FieldType).begin (); iterMemb != ContainerMem(CSISFieldRoot, FieldType).end (); iterMemb++)
       
   126 			{
       
   127 			if (! aCompleteWithNormalRead || (skipped < aSkipCount))
       
   128 				{
       
   129 				(*iterMemb) -> Skip (aFile, available);
       
   130 				if (++skipped == aSkipCount)
       
   131 					{
       
   132 					offset = static_cast <TUint64> (aFile.tell ()) - pos;
       
   133 					assert (AlignedSize (offset) == offset);
       
   134 					}
       
   135 				}
       
   136 			else
       
   137 				{
       
   138 				(*iterMemb) -> Read (aFile, available);
       
   139 				}
       
   140 			available = aContainerSize - (aFile.tell () - pos);
       
   141 			}
       
   142 		ReadFiller (aFile);
       
   143 #ifdef _DEBUG
       
   144 		TSISStream::pos_type got = aFile.tell ();
       
   145 		assert (expected == got);
       
   146 #endif
       
   147 		}
       
   148 	return offset;
       
   149 	}
       
   150 
       
   151 template <CSISFieldRoot::TFieldType FieldType>
       
   152 		void CStructure <FieldType>::Write (TSISStream& aFile, const bool aIsArrayElement) const
       
   153 	{
       
   154 #ifdef GENERATE_ERRORS
       
   155 	unsigned int unknownLen = rand()%1000;
       
   156 #endif // GENERATE_ERRORS
       
   157 
       
   158 	bool writeData = ! WasteOfSpace ();
       
   159 	if (writeData)
       
   160 		{
       
   161 #ifdef GENERATE_ERRORS
       
   162 		bool unknownField = CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBugUnknownField);
       
   163 #endif // GENERATE_ERRORS
       
   164 #ifdef _DEBUG
       
   165 		TSISStream::pos_type before = aFile.tell ();
       
   166 #endif
       
   167 		CSISHeader (
       
   168 #ifdef GENERATE_ERRORS
       
   169 						static_cast <const CSISFieldRoot::TFieldType> (unknownField ? 
       
   170 							static_cast <TUint32> (rand () % 0x0FFF + CSISFieldRoot::EBugUnknownField) :
       
   171 							FieldType),
       
   172 #else // GENERATE_ERRORS
       
   173 						FieldType, 
       
   174 #endif // GENERATE_ERRORS
       
   175 						ByteCount (false)).Write (aFile, aIsArrayElement);
       
   176 #ifdef _DEBUG
       
   177 		TSISStream::pos_type pos = aFile.tell ();
       
   178 		TSISStream::pos_type expected = aFile.tell () + static_cast <TSISStream::pos_type> (AlignedSize (ByteCount (false)));
       
   179 #endif
       
   180 		for (ContainerIter(CSISFieldRoot) iterMemb = ContainerMem(CSISFieldRoot,FieldType).begin(); iterMemb != ContainerMem(CSISFieldRoot,FieldType).end(); iterMemb++)
       
   181 			{
       
   182 #ifdef GENERATE_ERRORS
       
   183 			if (! CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBugMissingField))
       
   184 				{
       
   185 				if (CSISFieldRoot::IsBugToBeCreated (CSISFieldRoot::EBugUnexpectedField))
       
   186 					{
       
   187 					(*iterMemb) -> Write (aFile, false);
       
   188 					}
       
   189 				(*iterMemb) -> Write (aFile, false);
       
   190 				}
       
   191 			if (CSISFieldRoot::IsBugToBeCreated(CSISFieldRoot::EBugUnknownData))
       
   192 				{
       
   193 				CSISFieldRoot::AddRawDataValue(aFile,unknownLen);
       
   194 				}
       
   195 #else // GENERATE_ERRORS
       
   196 			(*iterMemb) -> Write (aFile, false);
       
   197 
       
   198 #endif // GENERATE_ERRORS
       
   199 #ifdef _DEBUG
       
   200 			TSISStream::pos_type ici = aFile.tell ();
       
   201 #endif
       
   202 			}
       
   203 		TSISStream::pos_type beforeFiller = aFile.tell ();
       
   204 		WriteFiller (aFile);
       
   205 #ifdef _DEBUG
       
   206 		TSISStream::pos_type actual = aFile.tell ();
       
   207 #ifdef GENERATE_ERRORS
       
   208 		if (! IsAnyBugSet ())
       
   209 			{
       
   210 			assert (expected == actual);
       
   211 			}
       
   212 		else 
       
   213 			{	
       
   214 			if (expected != actual)
       
   215 				{
       
   216 				aFile.seek (before);
       
   217 				CSISHeader (FieldType, beforeFiller - pos).Write (aFile, aIsArrayElement);
       
   218 				aFile.seek (actual);
       
   219 				}
       
   220 			}
       
   221 #else
       
   222 			assert (expected == actual);
       
   223 #endif  // GENERATE_ERRORS
       
   224 #endif  // DEBUG
       
   225 		}
       
   226 	}
       
   227 
       
   228 template <CSISFieldRoot::TFieldType FieldType>
       
   229 		void  CStructure <FieldType>::RawSkipWrite (TSISStream& aFile, const bool aIsArrayElement, TUint8* aRawBuffer, const TUint64 aBufferSize, const TUint32 aSkipCount) const
       
   230 	{
       
   231 	assert (! WasteOfSpace ());	// not tested
       
   232 	assert ((! aBufferSize) ^ (aRawBuffer != NULL));
       
   233 	TUint32 skipped = 0;
       
   234 	TUint64 before = static_cast <TUint64> (aFile.tell ());
       
   235 	CSISHeader (FieldType, 0).Write (aFile, aIsArrayElement);
       
   236 	TUint64 after = static_cast <TUint64> (aFile.tell ());
       
   237 	if (aRawBuffer)
       
   238 		{
       
   239 		aFile.write (aRawBuffer, aBufferSize);
       
   240 		WriteFiller (aFile);
       
   241 		}
       
   242 	for (ContainerIter(CSISFieldRoot) iterMemb = ContainerMem(CSISFieldRoot,FieldType).begin (); iterMemb != ContainerMem(CSISFieldRoot,FieldType).end (); iterMemb++)
       
   243 		{
       
   244 		if (skipped++ >= aSkipCount)
       
   245 			{
       
   246 			(*iterMemb) -> Write (aFile, false);
       
   247 			}
       
   248 		else
       
   249 		if (skipped == aSkipCount)
       
   250 			{
       
   251 			(*iterMemb) -> SkipOldWriteNew (aFile);
       
   252 			}
       
   253 		}
       
   254 	WriteFiller (aFile);
       
   255 	TUint64 actual = static_cast <TUint64> (aFile.tell ());
       
   256 	CSISException::ThrowIf (CSISHeader::IsBigHeader (actual - after), 
       
   257 							CSISException::EFileFormat,
       
   258 							"inconsistent structure size"); // why is SISController so huge;
       
   259 	aFile.seek (before); 
       
   260 	CSISHeader (FieldType, actual - after).Write (aFile, aIsArrayElement);
       
   261 	aFile.seek (actual);
       
   262 	}
       
   263 
       
   264 template <CSISFieldRoot::TFieldType FieldType> inline
       
   265 		void CStructure <FieldType>::CalculateCrc (TCRC& aCRC, const bool aIsArrayElement) const
       
   266 	{
       
   267 	if(!WasteOfSpace())
       
   268 		{
       
   269 		CSISHeader (FieldType, ByteCount (false)).CalculateCrc (aCRC, aIsArrayElement); 
       
   270 		CContainer<CSISFieldRoot, FieldType>::CalculateCrc (aCRC, false);
       
   271 		}
       
   272 	}
       
   273 
       
   274 template <CSISFieldRoot::TFieldType FieldType> inline
       
   275 		void CStructure <FieldType>::InsertMember (CSISFieldRoot& aMember)
       
   276 	{
       
   277 	ContainerMem(CSISFieldRoot,FieldType).push_back (&aMember);
       
   278 	}
       
   279 
       
   280 template <CSISFieldRoot::TFieldType FieldType> inline
       
   281 		CSISFieldRoot::TFieldSize CStructure <FieldType>::ByteCount (const bool aIsArray) const
       
   282 	{
       
   283 	assert (! aIsArray);
       
   284 	return CContainer<CSISFieldRoot, FieldType>::ByteCount (false);
       
   285 	}
       
   286 
       
   287 
       
   288 #endif // __STRUCTURE_H__
       
   289