IMPSengine/datautils/src/impspacked.cpp
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2002 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 "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 * package entry and entity class.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    <s32std.h>
       
    22 #include    "impsfields.h"
       
    23 #include    "impspacked.h"
       
    24 #include    "impsutils.h"
       
    25 
       
    26 // MACROS
       
    27 #ifndef _DEBUG
       
    28 #define _NO_IMPS_LOGGING_
       
    29 #endif
       
    30 // ================= MEMBER FUNCTIONS =======================
       
    31 
       
    32 
       
    33 // ---------------------------------------------------------
       
    34 // TImpsPackedEntity::TImpsPackedEntity
       
    35 // ---------------------------------------------------------
       
    36 //
       
    37 EXPORT_C TImpsPackedEntity::TImpsPackedEntity( HBufC8*& aBuffer )
       
    38 : iBuffer( aBuffer )
       
    39     {}
       
    40 
       
    41 // ---------------------------------------------------------
       
    42 // TImpsPackedEntity::PackEntity
       
    43 // ---------------------------------------------------------
       
    44 //
       
    45 EXPORT_C TInt TImpsPackedEntity::PackEntity( const CImpsFields& aEntity )
       
    46     {
       
    47 #ifndef _NO_IMPS_LOGGING_
       
    48     CImpsClientLogger::Log(_L("TImpsPackedEntity::PackEntityL begins"));
       
    49 #endif
       
    50     // find the start and end of the buffer
       
    51     const TUint8* pS = iBuffer->Ptr();
       
    52     const TUint8* pE = PtrAdd( pS, iBuffer->Des().MaxSize() );
       
    53 
       
    54     TInt error = DoPackEntity( pS, pE, aEntity );
       
    55     if ( error==KErrNone )
       
    56         {
       
    57         // update the length of the buffer
       
    58         iBuffer->Des().SetLength( pS-iBuffer->Ptr() );
       
    59         }
       
    60 
       
    61 #ifndef _NO_IMPS_LOGGING_
       
    62     CImpsClientLogger::Log(_L("TImpsPackedEntity::PackEntityL ends with %d"), error);
       
    63 #endif
       
    64     return error;
       
    65     }
       
    66 
       
    67 // ---------------------------------------------------------
       
    68 // TImpsPackedEntity::UnpackEntityL
       
    69 // ---------------------------------------------------------
       
    70 //
       
    71 EXPORT_C void TImpsPackedEntity::UnpackEntityL( CImpsFields& aEntity )
       
    72     {
       
    73 #ifndef _NO_IMPS_LOGGING_
       
    74     CImpsClientLogger::Log(_L("TImpsPackedEntity::UnPackEntityL begins"));
       
    75 #endif
       
    76 
       
    77     TUint8* pS = CONST_CAST(TUint8*, iBuffer->Ptr());
       
    78     DoUnpackEntityL( pS, aEntity );
       
    79 
       
    80 #ifndef _NO_IMPS_LOGGING_
       
    81     CImpsClientLogger::Log(_L("TImpsPackedEntity::UnPackEntityL ends"));
       
    82 #endif
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // TImpsPackedEntity::DoUnpackEntityL
       
    87 // ---------------------------------------------------------
       
    88 void TImpsPackedEntity::DoUnpackEntityL( 
       
    89     TUint8*& aPtr, 
       
    90     CImpsFields& aEntity )
       
    91     {
       
    92     TInt sizePackedCont = Align4( KImpsPackedCopyData );
       
    93     
       
    94     // Get the entry from the start of the buffer
       
    95     Mem::Copy( &aEntity, aPtr, sizePackedCont);
       
    96     aPtr = aPtr + sizePackedCont;
       
    97 
       
    98     CImpsData* data = aEntity.Data( );
       
    99     DoUnpackDataL( aPtr, *data );
       
   100     }
       
   101 
       
   102 
       
   103 
       
   104 // ---------------------------------------------------------
       
   105 // TImpsPackedEntity::DoUnpackDataL
       
   106 // ---------------------------------------------------------
       
   107 void TImpsPackedEntity::DoUnpackDataL(
       
   108     TUint8*& aPtr, 
       
   109     CImpsData& aEntity )
       
   110     {
       
   111     TInt mySize = 0;
       
   112     Mem::Copy( &mySize, aPtr, sizeof( mySize ) );
       
   113     aPtr += sizeof( mySize );
       
   114     aEntity.UnPackL( aPtr );
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------
       
   118 // TImpsPackedEntity::DoPackEntity
       
   119 // Format is
       
   120 //   fundamental types copied 
       
   121 //   plus each descriptor: size + 8-bit data
       
   122 // NOTICE: This must be modified always when CImpsFields members is changed!!!
       
   123 // ---------------------------------------------------------
       
   124 TInt TImpsPackedEntity::DoPackEntity(
       
   125     const TUint8*& aPtrStart, 
       
   126     const TUint8* aPtrEnd, 
       
   127     const CImpsFields& aEntity )
       
   128 //
       
   129 // Fails with KErrOverflow if the packed entry is too large
       
   130 // aPtrStart is always returned pointing to the end of the packed entry (even if too large)
       
   131 //
       
   132     {
       
   133     // make sure the entry can fit into the memory area defined by the two pointers
       
   134 
       
   135     TInt size =  aEntity.Size();
       
   136     if ( ( aPtrStart + size ) > aPtrEnd )
       
   137         {
       
   138         aPtrStart += size;
       
   139         return KErrOverflow;
       
   140         }
       
   141 
       
   142     size = Align4( KImpsPackedCopyData );
       
   143     // copy the entry and descriptors into the memory area
       
   144     Mem::Copy((void*)aPtrStart, &aEntity, size );
       
   145     aPtrStart += size;
       
   146 
       
   147     // Notice: Login data is not packed at the very moment
       
   148 
       
   149     TInt ret = DoPackData( aPtrStart, aPtrEnd, *aEntity.Data( ) );
       
   150 
       
   151     return ret;
       
   152     }
       
   153 
       
   154 
       
   155 
       
   156 // ---------------------------------------------------------
       
   157 // TImpsPackedEntity::DoPackData
       
   158 // ---------------------------------------------------------
       
   159 TInt TImpsPackedEntity::DoPackData(
       
   160     const TUint8*& aPtrStart, 
       
   161     const TUint8* aPtrEnd, 
       
   162     const CImpsData& aEntity )
       
   163     {
       
   164     TInt size = aEntity.Size(); 
       
   165     size += (2 * sizeof (TInt));		//size,count
       
   166     if ( ( aPtrStart + size ) > aPtrEnd )
       
   167         {
       
   168         aPtrStart += size;
       
   169         return KErrOverflow;
       
   170         }
       
   171     // write the size first.
       
   172     Mem::Copy( (void*)aPtrStart, &size, sizeof( size ) );
       
   173     aPtrStart = aPtrStart + sizeof( size );
       
   174 
       
   175     TInt ret = aEntity.Pack( aPtrStart );
       
   176     return ret;
       
   177     }
       
   178 
       
   179 // ---------------------------------------------------------
       
   180 // TImpsPackedEntity::DoPackDescriptor
       
   181 // ---------------------------------------------------------
       
   182 void TImpsPackedEntity::DoPackDescriptor(
       
   183     const TUint8*& aPtrStart, 
       
   184     const TDesC& /*HBufC* */ aDescriptor )
       
   185     {
       
   186 
       
   187     TInt32 tempSize = 0;
       
   188     tempSize = aDescriptor.Size();
       
   189     Mem::Copy( (void*)aPtrStart, &tempSize, sizeof( tempSize) );
       
   190     aPtrStart = aPtrStart + sizeof( tempSize );
       
   191     if ( tempSize )
       
   192         {
       
   193         Mem::Copy( (void*)aPtrStart, aDescriptor.Ptr(), tempSize);
       
   194         aPtrStart = aPtrStart + tempSize ;
       
   195         }
       
   196     }
       
   197 
       
   198 // ---------------------------------------------------------
       
   199 // TImpsPackedEntity::DoUnpackDescriptorLC
       
   200 // ---------------------------------------------------------
       
   201 void TImpsPackedEntity::DoUnpackDescriptorLC(
       
   202     const TUint8*& aPtrStart, 
       
   203     HBufC*& aDescriptor )
       
   204     {
       
   205 
       
   206     const TUint8* textPtr = aPtrStart;
       
   207     TInt32 tempSize = 0;
       
   208 
       
   209     Mem::Copy( &tempSize, textPtr, sizeof( tempSize) );
       
   210     textPtr = textPtr + sizeof( tempSize );
       
   211     if ( tempSize > 0 )
       
   212         {
       
   213         // put and leave this to the CleanupStack because of other the
       
   214         // calling method can leave
       
   215         aDescriptor = HBufC::NewL( tempSize / 2 );  
       
   216         CleanupStack::PushL( aDescriptor );
       
   217         Mem::Copy( (void*)aDescriptor->Ptr(), textPtr, tempSize );
       
   218         aDescriptor->Des().SetLength( tempSize / 2 ); 
       
   219         textPtr = textPtr + tempSize; 
       
   220         }
       
   221     else
       
   222         {
       
   223         aDescriptor = NULL;
       
   224         }
       
   225     if ( tempSize < 0 )
       
   226         {
       
   227 #ifndef _NO_IMPS_LOGGING_
       
   228     CImpsClientLogger::Log(_L("TImpsPackedEntity: Descriptor Length 0"));
       
   229 #endif
       
   230         }
       
   231 
       
   232     aPtrStart = (TUint8*) textPtr;
       
   233     
       
   234     }
       
   235 
       
   236 // ---------------------------------------------------------
       
   237 // TImpsPackedEntity::DoPackArray
       
   238 // ---------------------------------------------------------
       
   239 EXPORT_C void TImpsPackedEntity::DoPackArray(
       
   240             const TUint8*& aPtrStart,
       
   241             const MDesCArray* aArray )
       
   242     {
       
   243 
       
   244     // First count nbr of elements
       
   245     TInt32 nbrElements = 0;
       
   246 
       
   247     if ( aArray != NULL )
       
   248         {
       
   249         nbrElements = aArray->MdcaCount();
       
   250         }
       
   251     Mem::Copy( (void*)aPtrStart, &nbrElements, sizeof( nbrElements ) );
       
   252     aPtrStart = aPtrStart + sizeof( nbrElements );
       
   253 
       
   254     // Store each array element
       
   255     for ( TInt i = 0; i < nbrElements; i++ )
       
   256         {
       
   257         TPtrC element = aArray->MdcaPoint(i);
       
   258         DoPackDescriptor( aPtrStart, element );
       
   259         }
       
   260 
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------
       
   264 // TImpsPackedEntity::DoUnpackArrayL
       
   265 // ---------------------------------------------------------
       
   266 EXPORT_C void TImpsPackedEntity::DoUnpackArrayL(
       
   267             const TUint8*& aPtrStart,
       
   268             CDesCArray* aArray )
       
   269     {
       
   270 
       
   271     const TUint8* textPtr = aPtrStart;
       
   272     TInt32 tempSize = 0;
       
   273 
       
   274     // Get the number of elements
       
   275     Mem::Copy( &tempSize, textPtr, sizeof( tempSize) );
       
   276     textPtr = textPtr + sizeof( tempSize );
       
   277 
       
   278     if ( tempSize == 0 )
       
   279         {
       
   280         // There are no elements in array
       
   281         aPtrStart = (TUint8*) textPtr;
       
   282         return;
       
   283         }
       
   284 
       
   285     // Unpack each element and add to the array
       
   286     HBufC* myPtr;
       
   287     HBufC*& descriptor = myPtr;
       
   288     for (  TInt i = 0; i < tempSize; i++ )
       
   289         {
       
   290         // This allocates memory for each element
       
   291         DoUnpackDescriptorLC( textPtr, descriptor );  
       
   292         // Array may contain zero length descriptor
       
   293         if ( descriptor != NULL )
       
   294             {
       
   295             aArray->AppendL( descriptor->Des() );
       
   296             //delete myPtr;
       
   297             CleanupStack::PopAndDestroy();
       
   298             }
       
   299         else
       
   300             {
       
   301             aArray->AppendL( KNullDesC );
       
   302             }
       
   303         myPtr = NULL;
       
   304         }
       
   305 
       
   306     // Increase the packed data pointer
       
   307     aPtrStart = (TUint8*) textPtr;
       
   308     }
       
   309 
       
   310 
       
   311 // ================= OTHER EXPORTED FUNCTIONS ==============
       
   312 
       
   313 //  End of File