omadrm/drmengine/ro/src/DRMConstraint.cpp
changeset 23 493788a4a8a4
parent 0 95b198f216e5
equal deleted inserted replaced
5:79d62d1d7957 23:493788a4a8a4
    17 
    17 
    18 
    18 
    19 // INCLUDE FILES
    19 // INCLUDE FILES
    20 #include <s32strm.h>
    20 #include <s32strm.h>
    21 #include <s32mem.h>
    21 #include <s32mem.h>
    22 #include "DRMConstraint.h"
    22 #include "DrmConstraint.h"
    23 
    23 
    24 // CONSTANTS
    24 // CONSTANTS
    25 
    25 
    26 // Synchronizing marker in the beginning of the stream in order to synchronize
    26 // Synchronizing marker in the beginning of the stream in order to synchronize
    27 // to an externalized Constraint object having the new structure.  
    27 // to an externalized Constraint object having the new structure.
    28 const TInt32 KSyncMark = 0xAFCE;
    28 const TInt32 KSyncMark = 0xAFCE;
    29 
    29 
    30 // Old and new version number of the Constraint object
    30 // Old and new version number of the Constraint object
    31 const TInt8 KVersion3_2_0 = 0;
    31 const TInt8 KVersion3_2_0 = 0;
    32 const TInt8 KVersion3_2_1 = 1; 
    32 const TInt8 KVersion3_2_1 = 1;
    33    
    33 
    34 const TInt KSanityDataLengthLow = 0;
    34 const TInt KSanityDataLengthLow = 0;
    35 const TInt KSanityDataLengthHigh = 32768;
    35 const TInt KSanityDataLengthHigh = 32768;
    36 
    36 
    37 
    37 
    38 // ============================ LOCAL FUNCTIONS ===============================
    38 // ============================ LOCAL FUNCTIONS ===============================
    53 // -----------------------------------------------------------------------------
    53 // -----------------------------------------------------------------------------
    54 // AppendToArrayL
    54 // AppendToArrayL
    55 // Appends the strings of array aFrom to array aTo
    55 // Appends the strings of array aFrom to array aTo
    56 // -----------------------------------------------------------------------------
    56 // -----------------------------------------------------------------------------
    57 //
    57 //
    58 LOCAL_C void AppendToArrayL( RPointerArray<HBufC8>& aTo, 
    58 LOCAL_C void AppendToArrayL( RPointerArray<HBufC8>& aTo,
    59                              const RPointerArray<HBufC8>& aFrom )
    59                              const RPointerArray<HBufC8>& aFrom )
    60     {
    60     {
    61     HBufC8* addData = NULL;
    61     HBufC8* addData = NULL;
    62     
    62 
    63     for( TInt i = 0; i < aFrom.Count(); i++ )
    63     for( TInt i = 0; i < aFrom.Count(); i++ )
    64         {
    64         {
    65         addData = aFrom[i]->AllocLC();
    65         addData = aFrom[i]->AllocLC();
    66         aTo.AppendL( addData );
    66         aTo.AppendL( addData );
    67         CleanupStack::Pop( addData ); 
    67         CleanupStack::Pop( addData );
    68         }    
    68         }
    69     }
    69     }
    70 
    70 
    71 // -----------------------------------------------------------------------------
    71 // -----------------------------------------------------------------------------
    72 // CountArrayStoreSize
    72 // CountArrayStoreSize
    73 // Returns the size in bytes how much the array needs for storing
    73 // Returns the size in bytes how much the array needs for storing
    74 // -----------------------------------------------------------------------------
    74 // -----------------------------------------------------------------------------
    75 //
    75 //
    76 LOCAL_C TInt CountArrayStoreSize( const RPointerArray<HBufC8>& aArray )
    76 LOCAL_C TInt CountArrayStoreSize( const RPointerArray<HBufC8>& aArray )
    77     {
    77     {
    78     TInt size = 0;
    78     TInt size = 0;
    79     
    79 
    80     for(TInt i = 0; i < aArray.Count(); i++ )
    80     for(TInt i = 0; i < aArray.Count(); i++ )
    81         {
    81         {
    82         size += sizeof(TInt);
    82         size += sizeof(TInt);
    83         size += aArray[i]->Size();
    83         size += aArray[i]->Size();
    84         }
    84         }
    85     return size;    
    85     return size;
    86     }
    86     }
    87 // -----------------------------------------------------------------------------
    87 // -----------------------------------------------------------------------------
    88 // WriteArrayToStreamL
    88 // WriteArrayToStreamL
    89 // Write the array to the stream
    89 // Write the array to the stream
    90 // -----------------------------------------------------------------------------
    90 // -----------------------------------------------------------------------------
    96         {
    96         {
    97         aStream.WriteInt32L( aArray[i]->Size() );
    97         aStream.WriteInt32L( aArray[i]->Size() );
    98         aStream.WriteL( aArray[i]->Des() );
    98         aStream.WriteL( aArray[i]->Des() );
    99         }
    99         }
   100     }
   100     }
   101     
   101 
   102 // -----------------------------------------------------------------------------
   102 // -----------------------------------------------------------------------------
   103 // ReadArrayFromStringL
   103 // ReadArrayFromStringL
   104 // Reads the array from the string
   104 // Reads the array from the string
   105 // -----------------------------------------------------------------------------
   105 // -----------------------------------------------------------------------------
   106 //    
   106 //
   107 LOCAL_C void ReadArrayFromStringL( const TDesC8& aString,
   107 LOCAL_C void ReadArrayFromStringL( const TDesC8& aString,
   108                                    RPointerArray<HBufC8>& aArray )
   108                                    RPointerArray<HBufC8>& aArray )
   109     {
   109     {
   110     RMemReadStream inRead( static_cast<const TAny*>( aString.Ptr() ), aString.Size() );
   110     RMemReadStream inRead( static_cast<const TAny*>( aString.Ptr() ), aString.Size() );
   111     TInt size = 0;
   111     TInt size = 0;
   112     HBufC8* addData = NULL;
   112     HBufC8* addData = NULL;
   113     TPtr8 dataBuffer(NULL,0,0);
   113     TPtr8 dataBuffer(NULL,0,0);
   114     CleanupClosePushL( inRead );
   114     CleanupClosePushL( inRead );
   115     
   115 
   116     
   116 
   117     aArray.ResetAndDestroy();
   117     aArray.ResetAndDestroy();
   118     
   118 
   119     
   119 
   120     for( TInt i = 0; i < aString.Size();)
   120     for( TInt i = 0; i < aString.Size();)
   121         {
   121         {
   122         // If there is not enough data to read the integer 
   122         // If there is not enough data to read the integer
   123         // it means that it's an old version and the whole thing is the
   123         // it means that it's an old version and the whole thing is the
   124         // string since in previous versions only one string is stored        
   124         // string since in previous versions only one string is stored
   125         if(( aString.Size() - i) < sizeof(TInt) )
   125         if(( aString.Size() - i) < sizeof(TInt) )
   126             {
   126             {
   127             aArray.ResetAndDestroy();
   127             aArray.ResetAndDestroy();
   128             addData = aString.AllocLC();
   128             addData = aString.AllocLC();
   129             aArray.AppendL( addData );
   129             aArray.AppendL( addData );
   130             CleanupStack::Pop();
   130             CleanupStack::Pop();
   131             CleanupStack::PopAndDestroy(); // inRead              
   131             CleanupStack::PopAndDestroy(); // inRead
   132             return;            
   132             return;
   133             }
   133             }
   134         
   134 
   135         size = inRead.ReadInt32L();
   135         size = inRead.ReadInt32L();
   136         i += sizeof(TInt);
   136         i += sizeof(TInt);
   137         
   137 
   138         // If the size is negative or the size left is not large enough
   138         // If the size is negative or the size left is not large enough
   139         // it means that it's an old version and the whole thing is the
   139         // it means that it's an old version and the whole thing is the
   140         // string since in previous versions only one string is stored.
   140         // string since in previous versions only one string is stored.
   141         if( size < 0 || size > ( aString.Size() - i ) )
   141         if( size < 0 || size > ( aString.Size() - i ) )
   142             {
   142             {
   143             aArray.ResetAndDestroy();
   143             aArray.ResetAndDestroy();
   144             addData = aString.AllocLC();
   144             addData = aString.AllocLC();
   145             aArray.AppendL( addData );
   145             aArray.AppendL( addData );
   146             CleanupStack::Pop();
   146             CleanupStack::Pop();
   147             CleanupStack::PopAndDestroy(); // inRead            
   147             CleanupStack::PopAndDestroy(); // inRead
   148             return;
   148             return;
   149             }
   149             }
   150         addData = HBufC8::NewMaxLC( size );    
   150         addData = HBufC8::NewMaxLC( size );
   151 
   151 
   152         // Set the read buffer:
   152         // Set the read buffer:
   153         dataBuffer.Set(const_cast<TUint8*>(addData->Ptr()), 0, size);
   153         dataBuffer.Set(const_cast<TUint8*>(addData->Ptr()), 0, size);
   154         
   154 
   155         // Read the data:
   155         // Read the data:
   156         inRead.ReadL( dataBuffer );
   156         inRead.ReadL( dataBuffer );
   157         
   157 
   158         aArray.AppendL( addData );
   158         aArray.AppendL( addData );
   159         CleanupStack::Pop( addData );
   159         CleanupStack::Pop( addData );
   160 
   160 
   161         i += size;
   161         i += size;
   162         }
   162         }
   163     CleanupStack::PopAndDestroy();    
   163     CleanupStack::PopAndDestroy();
   164     return;
   164     return;
   165     }
   165     }
   166     
   166 
   167 // -----------------------------------------------------------------------------
   167 // -----------------------------------------------------------------------------
   168 // IsIndividualConstraintValid
   168 // IsIndividualConstraintValid
   169 // -----------------------------------------------------------------------------
   169 // -----------------------------------------------------------------------------
   170 //
   170 //
   171 LOCAL_C TBool IsIndividualConstraintValid( const RPointerArray<HBufC8>& aConstraint, 
   171 LOCAL_C TBool IsIndividualConstraintValid( const RPointerArray<HBufC8>& aConstraint,
   172                                            const RPointerArray<HBufC8>& aValidConstraints)
   172                                            const RPointerArray<HBufC8>& aValidConstraints)
   173     {
   173     {
   174     TInt retVal = 0;
   174     TInt retVal = 0;
   175     
   175 
   176     for( TInt i = 0; i < aConstraint.Count(); i++ )
   176     for( TInt i = 0; i < aConstraint.Count(); i++ )
   177         {
   177         {
   178         for( TInt j = 0; j < aValidConstraints.Count(); j++ )
   178         for( TInt j = 0; j < aValidConstraints.Count(); j++ )
   179             {
   179             {
   180             retVal = aConstraint[i]->Des().Compare( aValidConstraints[j]->Des() );
   180             retVal = aConstraint[i]->Des().Compare( aValidConstraints[j]->Des() );
   182                 {
   182                 {
   183                 return ETrue;
   183                 return ETrue;
   184                 }
   184                 }
   185             }
   185             }
   186         }
   186         }
   187     return EFalse;    
   187     return EFalse;
   188     };
   188     };
   189 
   189 
   190 // ============================ MEMBER FUNCTIONS ===============================
   190 // ============================ MEMBER FUNCTIONS ===============================
   191 
   191 
   192 
   192 
   198 EXPORT_C CDRMConstraint* CDRMConstraint::NewLC()
   198 EXPORT_C CDRMConstraint* CDRMConstraint::NewLC()
   199     {
   199     {
   200     CDRMConstraint* self = new( ELeave ) CDRMConstraint();
   200     CDRMConstraint* self = new( ELeave ) CDRMConstraint();
   201     CleanupStack::PushL( self );
   201     CleanupStack::PushL( self );
   202     self->ConstructL();
   202     self->ConstructL();
   203     
   203 
   204     return self;    
   204     return self;
   205     };
   205     };
   206 
   206 
   207 // -----------------------------------------------------------------------------
   207 // -----------------------------------------------------------------------------
   208 // CDRMConstraint::NewL
   208 // CDRMConstraint::NewL
   209 // Two-phased constructor.
   209 // Two-phased constructor.
   211 //
   211 //
   212 EXPORT_C CDRMConstraint* CDRMConstraint::NewL()
   212 EXPORT_C CDRMConstraint* CDRMConstraint::NewL()
   213     {
   213     {
   214     CDRMConstraint* self = NewLC();
   214     CDRMConstraint* self = NewLC();
   215     CleanupStack::Pop();
   215     CleanupStack::Pop();
   216     
   216 
   217     return self;
   217     return self;
   218     }; 
   218     };
   219 
   219 
   220 // -----------------------------------------------------------------------------
   220 // -----------------------------------------------------------------------------
   221 // Default Constructor - First phase.
   221 // Default Constructor - First phase.
   222 // Can be used by itself to generate an empty object
   222 // Can be used by itself to generate an empty object
   223 // -----------------------------------------------------------------------------
   223 // -----------------------------------------------------------------------------
   224 //
   224 //
   225 EXPORT_C CDRMConstraint::CDRMConstraint() :
   225 EXPORT_C CDRMConstraint::CDRMConstraint() :
   226     iSyncMark( KSyncMark ),
   226     iSyncMark( KSyncMark ),
   227     iVersion( KVersion3_2_1 ), // Version number for differentiation 
   227     iVersion( KVersion3_2_1 ), // Version number for differentiation
   228                                // in InternalizeL. 
   228                                // in InternalizeL.
   229     iStartTime( Time::NullTTime() ),
   229     iStartTime( Time::NullTTime() ),
   230     iEndTime( Time::NullTTime() ),
   230     iEndTime( Time::NullTTime() ),
   231     iIntervalStart( Time::NullTTime() ),
   231     iIntervalStart( Time::NullTTime() ),
   232     iInterval( 0 ),
   232     iInterval( 0 ),
   233     iCounter( 0 ),
   233     iCounter( 0 ),
   234     iOriginalCounter( 0 ),
   234     iOriginalCounter( 0 ),
   235     iTimedCounter( 0 ),
   235     iTimedCounter( 0 ),
   236 	iTimedInterval( 0 ),
   236     iTimedInterval( 0 ),
   237 	iAccumulatedTime( 0 ),
   237     iAccumulatedTime( 0 ),
   238 	iVendorId( TUid::Null() ),
   238     iVendorId( TUid::Null() ),
   239 	iSecureId( TUid::Null() ),
   239     iSecureId( TUid::Null() ),
   240     iActiveConstraints( 0 ),
   240     iActiveConstraints( 0 ),
   241     iDrmMeteringInfo( NULL ),
   241     iDrmMeteringInfo( NULL ),
   242     iOriginalTimedCounter( 0 ) 
   242     iOriginalTimedCounter( 0 )
   243     {
   243     {
   244     }
   244     }
   245     
   245 
   246 // -----------------------------------------------------------------------------
   246 // -----------------------------------------------------------------------------
   247 // Destructor
   247 // Destructor
   248 // -----------------------------------------------------------------------------
   248 // -----------------------------------------------------------------------------
   249 // 
   249 //
   250 EXPORT_C CDRMConstraint::~CDRMConstraint()
   250 EXPORT_C CDRMConstraint::~CDRMConstraint()
   251     {
   251     {
   252 
   252 
   253     iIndividual.ResetAndDestroy();
   253     iIndividual.ResetAndDestroy();
   254     iIndividual.Close();
   254     iIndividual.Close();
   255     
   255 
   256     iSystem.ResetAndDestroy();
   256     iSystem.ResetAndDestroy();
   257     iSystem.Close(); 
   257     iSystem.Close();
   258             
   258 
   259 #ifdef RD_DRM_METERING       
   259 #ifdef RD_DRM_METERING
   260     if( iDrmMeteringInfo )
   260     if( iDrmMeteringInfo )
   261         {
   261         {
   262         delete iDrmMeteringInfo;
   262         delete iDrmMeteringInfo;
   263         iDrmMeteringInfo = NULL;
   263         iDrmMeteringInfo = NULL;
   264         }
   264         }
   265 #endif
   265 #endif
   266         
   266 
   267     }; 
   267     };
   268 
   268 
   269 // -----------------------------------------------------------------------------
   269 // -----------------------------------------------------------------------------
   270 // CDRMConstraint::ExternalizeL
   270 // CDRMConstraint::ExternalizeL
   271 // -----------------------------------------------------------------------------
   271 // -----------------------------------------------------------------------------
   272 //
   272 //
   273 EXPORT_C void CDRMConstraint::ExternalizeL( RWriteStream& aStream ) const
   273 EXPORT_C void CDRMConstraint::ExternalizeL( RWriteStream& aStream ) const
   274     {
   274     {
   275     
   275 
   276     // used for the buffers
   276     // used for the buffers
   277     TInt32 dataLength = 0;
   277     TInt32 dataLength = 0;
   278     
   278 
   279     // write the synchronizing marker
   279     // write the synchronizing marker
   280     aStream.WriteInt32L( iSyncMark );
   280     aStream.WriteInt32L( iSyncMark );
   281     
   281 
   282     // Write the version number
   282     // Write the version number
   283     aStream.WriteInt32L( iVersion );
   283     aStream.WriteInt32L( iVersion );
   284     
   284 
   285     // Write the start time
   285     // Write the start time
   286     WriteInt64L( iStartTime.Int64(), aStream );
   286     WriteInt64L( iStartTime.Int64(), aStream );
   287     
   287 
   288     // Write the end time
   288     // Write the end time
   289     WriteInt64L( iEndTime.Int64(), aStream );
   289     WriteInt64L( iEndTime.Int64(), aStream );
   290     
   290 
   291     // Write the interval start time
   291     // Write the interval start time
   292     WriteInt64L( iIntervalStart.Int64(), aStream ); 
   292     WriteInt64L( iIntervalStart.Int64(), aStream );
   293     
   293 
   294     // Write the interval   
   294     // Write the interval
   295     aStream.WriteInt32L( iInterval.Int() );
   295     aStream.WriteInt32L( iInterval.Int() );
   296     
   296 
   297     // Write the counter
   297     // Write the counter
   298     aStream.WriteInt32L( iCounter );
   298     aStream.WriteInt32L( iCounter );
   299     
   299 
   300     // Write the original counter
   300     // Write the original counter
   301     aStream.WriteInt32L( iOriginalCounter );
   301     aStream.WriteInt32L( iOriginalCounter );
   302     
   302 
   303     // Write the timed counter
   303     // Write the timed counter
   304     aStream.WriteInt32L( iTimedCounter );
   304     aStream.WriteInt32L( iTimedCounter );
   305 
   305 
   306     // Write the timed interval
   306     // Write the timed interval
   307     aStream.WriteInt32L( iTimedInterval.Int() );
   307     aStream.WriteInt32L( iTimedInterval.Int() );
   308     
   308 
   309     // Write the accumulated time
   309     // Write the accumulated time
   310     aStream.WriteInt32L( iAccumulatedTime.Int() );
   310     aStream.WriteInt32L( iAccumulatedTime.Int() );
   311     
   311 
   312     // Write the individual
   312     // Write the individual
   313     dataLength = 0;
   313     dataLength = 0;
   314     if ( iIndividual.Count() )
   314     if ( iIndividual.Count() )
   315         {
   315         {
   316         dataLength = CountArrayStoreSize( iIndividual );
   316         dataLength = CountArrayStoreSize( iIndividual );
   317         }
   317         }
   318     aStream.WriteInt32L( dataLength );
   318     aStream.WriteInt32L( dataLength );
   319 
   319 
   320     if ( dataLength )
   320     if ( dataLength )
   321         {
   321         {
   322         WriteArrayToStreamL( aStream, iIndividual );       
   322         WriteArrayToStreamL( aStream, iIndividual );
   323         }         
   323         }
   324     
   324 
   325     // Software Vendor Id
   325     // Software Vendor Id
   326     aStream.WriteInt32L( iVendorId.iUid );
   326     aStream.WriteInt32L( iVendorId.iUid );
   327     
   327 
   328     // Secure Id of the allowed application
   328     // Secure Id of the allowed application
   329     aStream.WriteInt32L( iSecureId.iUid );
   329     aStream.WriteInt32L( iSecureId.iUid );
   330     
   330 
   331     // Active constraints
   331     // Active constraints
   332     aStream.WriteUint32L( iActiveConstraints );
   332     aStream.WriteUint32L( iActiveConstraints );
   333     
   333 
   334     // Metering info
   334     // Metering info
   335 #ifdef RD_DRM_METERING    
   335 #ifdef RD_DRM_METERING
   336     dataLength = 0;
   336     dataLength = 0;
   337     if ( iDrmMeteringInfo )
   337     if ( iDrmMeteringInfo )
   338         {
   338         {
   339         dataLength = sizeof( TTimeIntervalSeconds ) + sizeof( TUint8 );
   339         dataLength = sizeof( TTimeIntervalSeconds ) + sizeof( TUint8 );
   340         }
   340         }
   341     
   341 
   342     aStream.WriteInt32L( dataLength );
   342     aStream.WriteInt32L( dataLength );
   343     
   343 
   344     if ( dataLength )
   344     if ( dataLength )
   345         {
   345         {
   346         aStream.WriteInt32L( iDrmMeteringInfo->iGraceTime.Int() );
   346         aStream.WriteInt32L( iDrmMeteringInfo->iGraceTime.Int() );
   347         aStream.WriteInt8L( iDrmMeteringInfo->iAllowUseWithoutMetering );
   347         aStream.WriteInt8L( iDrmMeteringInfo->iAllowUseWithoutMetering );
   348         }
   348         }
   349    
   349 
   350 #endif
   350 #endif
   351     
   351 
   352     // Write the system
   352     // Write the system
   353     dataLength = 0;
   353     dataLength = 0;
   354     if ( iSystem.Count() )
   354     if ( iSystem.Count() )
   355         {
   355         {
   356         dataLength = CountArrayStoreSize( iSystem );
   356         dataLength = CountArrayStoreSize( iSystem );
   357         }
   357         }
   358     
   358 
   359     aStream.WriteInt32L( dataLength );
   359     aStream.WriteInt32L( dataLength );
   360         
   360 
   361     if ( dataLength )
   361     if ( dataLength )
   362         {
   362         {
   363         WriteArrayToStreamL( aStream, iSystem );
   363         WriteArrayToStreamL( aStream, iSystem );
   364         }
   364         }
   365             
   365 
   366     // write the original timed counter
   366     // write the original timed counter
   367     aStream.WriteInt32L( iOriginalTimedCounter );
   367     aStream.WriteInt32L( iOriginalTimedCounter );
   368         
   368 
   369     // For future use
   369     // For future use
   370     aStream.WriteInt32L( 0 );
   370     aStream.WriteInt32L( 0 );
   371     
   371 
   372     };
   372     };
   373     
   373 
   374 // -----------------------------------------------------------------------------
   374 // -----------------------------------------------------------------------------
   375 // CDRMConstraint::InternalizeL
   375 // CDRMConstraint::InternalizeL
   376 // -----------------------------------------------------------------------------
   376 // -----------------------------------------------------------------------------
   377 //
   377 //
   378 EXPORT_C void CDRMConstraint::InternalizeL( RReadStream& aStream )
   378 EXPORT_C void CDRMConstraint::InternalizeL( RReadStream& aStream )
   379     {
   379     {
   380 
   380 
   381     TInt64 timeData = 0;
   381     TInt64 timeData = 0;
   382     TInt32 temp = 0;
   382     TInt32 temp = 0;
   383     
   383 
   384     // used for the buffers
   384     // used for the buffers
   385     TInt dataLength = 0;
   385     TInt dataLength = 0;
   386     HBufC8* dataPart = NULL;
   386     HBufC8* dataPart = NULL;
   387     TPtr8 dataBuffer(NULL,0,0);
   387     TPtr8 dataBuffer(NULL,0,0);
   388     
   388 
   389     // Read the (possible) synchronizing marker.
   389     // Read the (possible) synchronizing marker.
   390     iSyncMark = aStream.ReadInt32L();
   390     iSyncMark = aStream.ReadInt32L();
   391     
   391 
   392     if ( iSyncMark != KSyncMark )
   392     if ( iSyncMark != KSyncMark )
   393         {
   393         {
   394          
   394 
   395         // The structure of the externalized Permission object is the old one.
   395         // The structure of the externalized Permission object is the old one.
   396         // The first four bytes constitute half of the eight bytes of Start time.
   396         // The first four bytes constitute half of the eight bytes of Start time.
   397         // Read another four bytes from the stream (and apply bit modifications) 
   397         // Read another four bytes from the stream (and apply bit modifications)
   398         // in order to reconstruct the Start time (iStartTime).
   398         // in order to reconstruct the Start time (iStartTime).
   399         temp = aStream.ReadInt32L();
   399         temp = aStream.ReadInt32L();
   400         
   400 
   401         timeData = temp;
   401         timeData = temp;
   402         timeData <<= 32;
   402         timeData <<= 32;
   403         
   403 
   404         Mem::Copy( &timeData, &iSyncMark, sizeof(TInt32) );
   404         Mem::Copy( &timeData, &iSyncMark, sizeof(TInt32) );
   405         
   405 
   406         iStartTime = timeData;
   406         iStartTime = timeData;
   407         timeData = 0;
   407         timeData = 0;
   408          
   408 
   409         // The version is marked as old version for differentiation in 
   409         // The version is marked as old version for differentiation in
   410         // InternalizeL.
   410         // InternalizeL.
   411         iVersion = KVersion3_2_0;
   411         iVersion = KVersion3_2_0;
   412                              
   412 
   413         }
   413         }
   414     else 
   414     else
   415         {
   415         {
   416         // The structure of the externalized Permission object is the new one.
   416         // The structure of the externalized Permission object is the new one.
   417         // Read the version and Start time.    
   417         // Read the version and Start time.
   418         iVersion = aStream.ReadInt32L();
   418         iVersion = aStream.ReadInt32L();
   419         
   419 
   420         // Read the start time
   420         // Read the start time
   421         ReadInt64L( timeData, aStream );
   421         ReadInt64L( timeData, aStream );
   422         iStartTime = timeData;
   422         iStartTime = timeData;
   423         
   423 
   424         }
   424         }
   425      
   425 
   426     // Read the end time
   426     // Read the end time
   427     ReadInt64L( timeData, aStream );
   427     ReadInt64L( timeData, aStream );
   428     iEndTime = timeData;
   428     iEndTime = timeData;
   429     
   429 
   430     // Read the interval start time
   430     // Read the interval start time
   431     ReadInt64L( timeData, aStream ); 
   431     ReadInt64L( timeData, aStream );
   432     iIntervalStart = timeData;
   432     iIntervalStart = timeData;
   433     
   433 
   434     // Read the interval   
   434     // Read the interval
   435     iInterval = aStream.ReadInt32L();
   435     iInterval = aStream.ReadInt32L();
   436     
   436 
   437     // Read the counter
   437     // Read the counter
   438     iCounter = aStream.ReadInt32L();
   438     iCounter = aStream.ReadInt32L();
   439     
   439 
   440     // Read the original counter
   440     // Read the original counter
   441     iOriginalCounter = aStream.ReadInt32L();
   441     iOriginalCounter = aStream.ReadInt32L();
   442     
   442 
   443     // Read the timed counter
   443     // Read the timed counter
   444     iTimedCounter = aStream.ReadInt32L();
   444     iTimedCounter = aStream.ReadInt32L();
   445 
   445 
   446     // Read the timed interval
   446     // Read the timed interval
   447     iTimedInterval = aStream.ReadInt32L();
   447     iTimedInterval = aStream.ReadInt32L();
   448     
   448 
   449     // Read the accumulated time
   449     // Read the accumulated time
   450     iAccumulatedTime = aStream.ReadInt32L();
   450     iAccumulatedTime = aStream.ReadInt32L();
   451     
   451 
   452     // Read the individual
   452     // Read the individual
   453     dataLength = aStream.ReadInt32L();
   453     dataLength = aStream.ReadInt32L();
   454     
   454 
   455     SanitizeL( dataLength );
   455     SanitizeL( dataLength );
   456     
   456 
   457     if( dataLength > 0 )
   457     if( dataLength > 0 )
   458         {
   458         {
   459         // Reserve a new buffer:
   459         // Reserve a new buffer:
   460         dataPart = HBufC8::NewMaxLC( dataLength );
   460         dataPart = HBufC8::NewMaxLC( dataLength );
   461         
   461 
   462         // Set the read buffer:
   462         // Set the read buffer:
   463         dataBuffer.Set(const_cast<TUint8*>(dataPart->Ptr()), 0, dataLength);
   463         dataBuffer.Set(const_cast<TUint8*>(dataPart->Ptr()), 0, dataLength);
   464         
   464 
   465         // Read the data:
   465         // Read the data:
   466         aStream.ReadL( dataBuffer );
   466         aStream.ReadL( dataBuffer );
   467         
   467 
   468         
   468 
   469         // Fill the array from the string
   469         // Fill the array from the string
   470         ReadArrayFromStringL( dataBuffer, iIndividual);
   470         ReadArrayFromStringL( dataBuffer, iIndividual);
   471         
   471 
   472         // Pop the buffer 
   472         // Pop the buffer
   473         CleanupStack::PopAndDestroy(); // dataPart
   473         CleanupStack::PopAndDestroy(); // dataPart
   474                     
   474 
   475         }
   475         }
   476     else
   476     else
   477         {
   477         {
   478         iIndividual.ResetAndDestroy();       
   478         iIndividual.ResetAndDestroy();
   479         }           
   479         }
   480     
   480 
   481     
   481 
   482     // Read the system 
   482     // Read the system
   483     if ( iVersion == KVersion3_2_0 ) // Constraint has the old structure.
   483     if ( iVersion == KVersion3_2_0 ) // Constraint has the old structure.
   484         {
   484         {
   485         
   485 
   486         dataLength = aStream.ReadInt32L();
   486         dataLength = aStream.ReadInt32L();
   487         
   487 
   488         SanitizeL( dataLength );
   488         SanitizeL( dataLength );
   489         
   489 
   490         if( dataLength > 0 )
   490         if( dataLength > 0 )
   491             {
   491             {
   492             // Reserve a new buffer:
   492             // Reserve a new buffer:
   493             dataPart = HBufC8::NewMaxLC( dataLength );
   493             dataPart = HBufC8::NewMaxLC( dataLength );
   494         
   494 
   495             // Set the read buffer:
   495             // Set the read buffer:
   496             dataBuffer.Set( const_cast<TUint8*>(dataPart->Ptr()), 0, 
   496             dataBuffer.Set( const_cast<TUint8*>(dataPart->Ptr()), 0,
   497                             dataLength );
   497                             dataLength );
   498         
   498 
   499             // Read the data:
   499             // Read the data:
   500             aStream.ReadL( dataBuffer );
   500             aStream.ReadL( dataBuffer );
   501         
   501 
   502             // Pop the buffer 
   502             // Pop the buffer
   503             CleanupStack::Pop(); // dataPart
   503             CleanupStack::Pop(); // dataPart
   504                 
   504 
   505             // If an old content identifier exists delete it        
   505             // If an old content identifier exists delete it
   506             if ( iSystem.Count() )
   506             if ( iSystem.Count() )
   507                 {
   507                 {
   508                 iSystem.ResetAndDestroy();
   508                 iSystem.ResetAndDestroy();
   509                 }
   509                 }
   510         
   510 
   511             // assign the new content id
   511             // assign the new content id
   512             iSystem.AppendL( dataPart );    
   512             iSystem.AppendL( dataPart );
   513             }
   513             }
   514         else
   514         else
   515             {
   515             {
   516             // If an old system exists delete it 
   516             // If an old system exists delete it
   517             if ( iSystem.Count() )
   517             if ( iSystem.Count() )
   518                 {
   518                 {
   519                 iSystem.ResetAndDestroy();
   519                 iSystem.ResetAndDestroy();
   520                 }        
   520                 }
   521             }  
   521             }
   522         }
   522         }
   523     
   523 
   524     // Software Vendor Id
   524     // Software Vendor Id
   525     iVendorId.iUid = aStream.ReadInt32L();
   525     iVendorId.iUid = aStream.ReadInt32L();
   526     
   526 
   527     // Secure Id of the allowed application
   527     // Secure Id of the allowed application
   528     iSecureId.iUid = aStream.ReadInt32L(); 
   528     iSecureId.iUid = aStream.ReadInt32L();
   529     
   529 
   530     // Active constraints
   530     // Active constraints
   531     iActiveConstraints = aStream.ReadUint32L();    
   531     iActiveConstraints = aStream.ReadUint32L();
   532 
   532 
   533 #ifdef RD_DRM_METERING    
   533 #ifdef RD_DRM_METERING
   534     
   534 
   535     // Do not read metering information if the version
   535     // Do not read metering information if the version
   536     // is the old one because Metering is not activated in it.
   536     // is the old one because Metering is not activated in it.
   537     if ( iVersion == KVersion3_2_1 )
   537     if ( iVersion == KVersion3_2_1 )
   538         {
   538         {
   539         
   539 
   540         // Metering info
   540         // Metering info
   541         dataLength = aStream.ReadInt32L();
   541         dataLength = aStream.ReadInt32L();
   542     
   542 
   543         SanitizeL( dataLength );
   543         SanitizeL( dataLength );
   544         
   544 
   545         if( dataLength > 0 )
   545         if( dataLength > 0 )
   546             {
   546             {
   547         
   547 
   548             if( !iDrmMeteringInfo )
   548             if( !iDrmMeteringInfo )
   549                 {
   549                 {
   550                 // Reserve a new metering information instance
   550                 // Reserve a new metering information instance
   551                 iDrmMeteringInfo = new (ELeave)TDrmMeteringInfo;
   551                 iDrmMeteringInfo = new (ELeave)TDrmMeteringInfo;
   552                 }
   552                 }
   553             else 
   553             else
   554                 {
   554                 {
   555                 iDrmMeteringInfo->iGraceTime = 0;
   555                 iDrmMeteringInfo->iGraceTime = 0;
   556                 iDrmMeteringInfo->iAllowUseWithoutMetering = EFalse;
   556                 iDrmMeteringInfo->iAllowUseWithoutMetering = EFalse;
   557                 }
   557                 }
   558         
   558 
   559             // Read grace time 
   559             // Read grace time
   560             iDrmMeteringInfo->iGraceTime = aStream.ReadInt32L();
   560             iDrmMeteringInfo->iGraceTime = aStream.ReadInt32L();
   561         
   561 
   562             // Read whether content can be consumed without 
   562             // Read whether content can be consumed without
   563             // metering being used
   563             // metering being used
   564             iDrmMeteringInfo->iAllowUseWithoutMetering = 
   564             iDrmMeteringInfo->iAllowUseWithoutMetering =
   565                 aStream.ReadInt8L();
   565                 aStream.ReadInt8L();
   566            
   566 
   567             }
   567             }
   568         else
   568         else
   569             {
   569             {
   570         
   570 
   571             // If old metering information exists delete it 
   571             // If old metering information exists delete it
   572             if( iDrmMeteringInfo )
   572             if( iDrmMeteringInfo )
   573                 {
   573                 {
   574                 delete iDrmMeteringInfo;
   574                 delete iDrmMeteringInfo;
   575                 iDrmMeteringInfo = NULL;
   575                 iDrmMeteringInfo = NULL;
   576                 }        
   576                 }
   577             }
   577             }
   578         }
   578         }
   579     
   579 
   580 #endif //RD_DRM_METERING
   580 #endif //RD_DRM_METERING
   581 
   581 
   582     // Read the system and original timed counter
   582     // Read the system and original timed counter
   583     // according to the new structure.
   583     // according to the new structure.
   584     if ( iVersion == KVersion3_2_1 )
   584     if ( iVersion == KVersion3_2_1 )
   585         {
   585         {
   586         
   586 
   587         dataLength = aStream.ReadInt32L();
   587         dataLength = aStream.ReadInt32L();
   588         
   588 
   589         SanitizeL( dataLength );
   589         SanitizeL( dataLength );
   590             
   590 
   591         if( dataLength > 0 )
   591         if( dataLength > 0 )
   592             {
   592             {
   593             // Reserve a new buffer:
   593             // Reserve a new buffer:
   594             dataPart = HBufC8::NewMaxLC( dataLength );
   594             dataPart = HBufC8::NewMaxLC( dataLength );
   595         
   595 
   596             // Set the read buffer:
   596             // Set the read buffer:
   597             dataBuffer.Set(const_cast<TUint8*>(dataPart->Ptr()), 0, dataLength);
   597             dataBuffer.Set(const_cast<TUint8*>(dataPart->Ptr()), 0, dataLength);
   598         
   598 
   599             // Read the data:
   599             // Read the data:
   600             aStream.ReadL( dataBuffer );
   600             aStream.ReadL( dataBuffer );
   601         
   601 
   602             // Fill the array from the string
   602             // Fill the array from the string
   603             ReadArrayFromStringL( dataBuffer, iSystem);
   603             ReadArrayFromStringL( dataBuffer, iSystem);
   604         
   604 
   605             // Pop the buffer 
   605             // Pop the buffer
   606             CleanupStack::PopAndDestroy(); // dataPart
   606             CleanupStack::PopAndDestroy(); // dataPart
   607                     
   607 
   608             }
   608             }
   609         else
   609         else
   610             {
   610             {
   611             iSystem.ResetAndDestroy();       
   611             iSystem.ResetAndDestroy();
   612             }         
   612             }
   613     
   613 
   614         // Read the original timed counter
   614         // Read the original timed counter
   615         iOriginalTimedCounter = aStream.ReadInt32L();
   615         iOriginalTimedCounter = aStream.ReadInt32L();
   616     
   616 
   617         // For future use or development, reads the data at the end of the stream
   617         // For future use or development, reads the data at the end of the stream
   618         dataLength = aStream.ReadInt32L();
   618         dataLength = aStream.ReadInt32L();
   619         
   619 
   620         SanitizeL( dataLength );
   620         SanitizeL( dataLength );
   621         
   621 
   622         if ( dataLength > 0 )
   622         if ( dataLength > 0 )
   623             {
   623             {
   624       
   624 
   625             // Reserve a new buffer:
   625             // Reserve a new buffer:
   626             dataPart = HBufC8::NewMaxLC( dataLength );
   626             dataPart = HBufC8::NewMaxLC( dataLength );
   627         
   627 
   628             // Set the read buffer:
   628             // Set the read buffer:
   629             dataBuffer.Set(const_cast<TUint8*>(dataPart->Ptr()), 0, dataLength);
   629             dataBuffer.Set(const_cast<TUint8*>(dataPart->Ptr()), 0, dataLength);
   630         
   630 
   631             // Read the data:
   631             // Read the data:
   632             aStream.ReadL( dataBuffer );
   632             aStream.ReadL( dataBuffer );
   633         
   633 
   634             // Pop the buffer 
   634             // Pop the buffer
   635             CleanupStack::PopAndDestroy( dataPart );
   635             CleanupStack::PopAndDestroy( dataPart );
   636             }
   636             }
   637         }
   637         }
   638     
   638 
   639     // Constraint can be considered to have the new structure from now on. 
   639     // Constraint can be considered to have the new structure from now on.
   640     if ( iVersion == KVersion3_2_0 ) 
   640     if ( iVersion == KVersion3_2_0 )
   641         {
   641         {
   642         iSyncMark = KSyncMark;
   642         iSyncMark = KSyncMark;
   643         iVersion = KVersion3_2_1; 
   643         iVersion = KVersion3_2_1;
   644         }
   644         }
   645     
   645 
   646     };
   646     };
   647     
   647 
   648 // -----------------------------------------------------------------------------
   648 // -----------------------------------------------------------------------------
   649 // CDRMConstraint::Stateful
   649 // CDRMConstraint::Stateful
   650 // -----------------------------------------------------------------------------
   650 // -----------------------------------------------------------------------------
   651 //
   651 //
   652 EXPORT_C TBool CDRMConstraint::Stateful() const
   652 EXPORT_C TBool CDRMConstraint::Stateful() const
   653     {
   653     {
   654     // counters, timed counters and accumulated are stateful
   654     // counters, timed counters and accumulated are stateful
   655     if ( iActiveConstraints &  (EConstraintCounter |
   655     if ( iActiveConstraints &  (EConstraintCounter |
   656                                EConstraintTimedCounter |
   656                                EConstraintTimedCounter |
   657                                EConstraintAccumulated ) ) 
   657                                EConstraintAccumulated ) )
   658         {
   658         {
   659         return ETrue;
   659         return ETrue;
   660         }
   660         }
   661         
   661 
   662      // Non-Activated interval is stateful
   662      // Non-Activated interval is stateful
   663     if ( ( iActiveConstraints & EConstraintInterval ) &&
   663     if ( ( iActiveConstraints & EConstraintInterval ) &&
   664         iIntervalStart == Time::NullTTime() ) 
   664         iIntervalStart == Time::NullTTime() )
   665         {
   665         {
   666         return ETrue;
   666         return ETrue;
   667         }
   667         }
   668         
   668 
   669     return EFalse;
   669     return EFalse;
   670     };
   670     };
   671 
   671 
   672 // -----------------------------------------------------------------------------
   672 // -----------------------------------------------------------------------------
   673 // CDRMConstraint::Size
   673 // CDRMConstraint::Size
   674 // -----------------------------------------------------------------------------
   674 // -----------------------------------------------------------------------------
   675 //   
   675 //
   676 EXPORT_C TInt CDRMConstraint::Size() const
   676 EXPORT_C TInt CDRMConstraint::Size() const
   677     {
   677     {
   678     TInt size = 0;
   678     TInt size = 0;
   679     
   679 
   680     // synchronizing marker
   680     // synchronizing marker
   681     size += sizeof(TInt32);
   681     size += sizeof(TInt32);
   682     
   682 
   683     // version number
   683     // version number
   684     size += sizeof(TInt32);
   684     size += sizeof(TInt32);
   685     
   685 
   686     // usage start time
   686     // usage start time
   687     size += sizeof(TTime);
   687     size += sizeof(TTime);
   688 
   688 
   689     // usage end time
   689     // usage end time
   690     size += sizeof(TTime);
   690     size += sizeof(TTime);
   700 
   700 
   701     // original counter value
   701     // original counter value
   702     size += sizeof(TDRMCounter);
   702     size += sizeof(TDRMCounter);
   703 
   703 
   704     // timed counter
   704     // timed counter
   705 	size += sizeof(TDRMCounter);
   705     size += sizeof(TDRMCounter);
   706 
   706 
   707     // Interval of the timed counter constraint
   707     // Interval of the timed counter constraint
   708 	size += sizeof(TTimeIntervalSeconds);
   708     size += sizeof(TTimeIntervalSeconds);
   709 
   709 
   710     // accumulated time
   710     // accumulated time
   711 	size += sizeof(TTimeIntervalSeconds);
   711     size += sizeof(TTimeIntervalSeconds);
   712 
   712 
   713     // individual allowed usage
   713     // individual allowed usage
   714     size += sizeof(TInt32);
   714     size += sizeof(TInt32);
   715     size += CountArrayStoreSize( iIndividual );
   715     size += CountArrayStoreSize( iIndividual );
   716 
   716 
   717     // Software Vendor Id
   717     // Software Vendor Id
   718     size += sizeof(TUid);
   718     size += sizeof(TUid);
   719     
   719 
   720     // Secure Id of the allowed application
   720     // Secure Id of the allowed application
   721     size += sizeof(TUid);
   721     size += sizeof(TUid);
   722 	
   722 
   723 	// Bitmask of active constraints
   723     // Bitmask of active constraints
   724     size += sizeof(TUint32);
   724     size += sizeof(TUint32);
   725     
   725 
   726 #ifdef RD_DRM_METERING    
   726 #ifdef RD_DRM_METERING
   727     // Metering information
   727     // Metering information
   728     size += sizeof(TInt32);
   728     size += sizeof(TInt32);
   729     
   729 
   730     if (iDrmMeteringInfo)
   730     if (iDrmMeteringInfo)
   731         {
   731         {
   732         size += sizeof(TTimeIntervalSeconds); 
   732         size += sizeof(TTimeIntervalSeconds);
   733         size += sizeof(TUint8);
   733         size += sizeof(TUint8);
   734         }
   734         }
   735     
   735 
   736 #endif //RD_DRM_METERING
   736 #endif //RD_DRM_METERING
   737     
   737 
   738     // system allowed usage
   738     // system allowed usage
   739     size += sizeof(TInt32);
   739     size += sizeof(TInt32);
   740     size += CountArrayStoreSize(iSystem);
   740     size += CountArrayStoreSize(iSystem);
   741     
   741 
   742     // original timed counter value
   742     // original timed counter value
   743     size += sizeof(TDRMCounter);
   743     size += sizeof(TDRMCounter);
   744     
   744 
   745     // For future use
   745     // For future use
   746     size += sizeof(TInt32);
   746     size += sizeof(TInt32);
   747     
   747 
   748     return size;
   748     return size;
   749        
   749 
   750     };    
   750     };
   751 
   751 
   752 // -----------------------------------------------------------------------------
   752 // -----------------------------------------------------------------------------
   753 // CDRMConstraint::Expired
   753 // CDRMConstraint::Expired
   754 // -----------------------------------------------------------------------------
   754 // -----------------------------------------------------------------------------
   755 //      
   755 //
   756 EXPORT_C TBool CDRMConstraint::Expired( const TTime& aTime ) const
   756 EXPORT_C TBool CDRMConstraint::Expired( const TTime& aTime ) const
   757     {
   757     {
   758 	// Full Rights do not expire
   758     // Full Rights do not expire
   759 	if ( iActiveConstraints == EConstraintNone )
   759     if ( iActiveConstraints == EConstraintNone )
   760 		{
   760         {
   761 		return EFalse;
   761         return EFalse;
   762 		}
   762         }
   763 
   763 
   764 	// First check counters, accumulated time and timed counters
   764     // First check counters, accumulated time and timed counters
   765 	// if any of these is expired the whole thing is expired regardless of the
   765     // if any of these is expired the whole thing is expired regardless of the
   766 	// actual time based constrants or future rights
   766     // actual time based constrants or future rights
   767 	
   767 
   768     // Counters
   768     // Counters
   769     if ( ( iActiveConstraints & EConstraintCounter ) && iCounter < 1 ) 
   769     if ( ( iActiveConstraints & EConstraintCounter ) && iCounter < 1 )
   770     	{
   770         {
   771     	return ETrue;	
   771         return ETrue;
   772     	}
   772         }
   773     		
   773 
   774     // Accumulated time	
   774     // Accumulated time
   775     if ( ( iActiveConstraints & EConstraintAccumulated ) && iAccumulatedTime.Int() == 0 )
   775     if ( ( iActiveConstraints & EConstraintAccumulated ) && iAccumulatedTime.Int() == 0 )
   776     	{
   776         {
   777     	return ETrue;
   777         return ETrue;
   778     	}
   778         }
   779     		
   779 
   780      // Timed Counters
   780      // Timed Counters
   781     if ( ( iActiveConstraints & EConstraintTimedCounter ) && iTimedCounter < 1 ) 
   781     if ( ( iActiveConstraints & EConstraintTimedCounter ) && iTimedCounter < 1 )
   782     	{
   782         {
   783     	return ETrue;	
   783         return ETrue;
   784     	} 
   784         }
   785     		
   785 
   786 
   786 
   787     // Dont check time based rights
   787     // Dont check time based rights
   788     if ( aTime != Time::NullTTime() )
   788     if ( aTime != Time::NullTTime() )
   789     	{	
   789         {
   790     	
   790 
   791     	// Check for activated intervals
   791         // Check for activated intervals
   792     	if ( ( iActiveConstraints & EConstraintInterval) && iIntervalStart != Time::NullTTime() )
   792         if ( ( iActiveConstraints & EConstraintInterval) && iIntervalStart != Time::NullTTime() )
   793     		{
   793             {
   794         	TTimeIntervalSeconds current;
   794             TTimeIntervalSeconds current;
   795         
   795 
   796         	aTime.SecondsFrom( iIntervalStart, current );
   796             aTime.SecondsFrom( iIntervalStart, current );
   797         
   797 
   798         	if ( current >= iInterval )
   798             if ( current >= iInterval )
   799             	{
   799                 {
   800             	return ETrue;
   800                 return ETrue;
   801             	}    			
   801                 }
   802     		}
   802             }
   803     	
   803 
   804     	// Check for end time
   804         // Check for end time
   805     	if ( ( iActiveConstraints & EConstraintEndTime ) && aTime >= iEndTime )
   805         if ( ( iActiveConstraints & EConstraintEndTime ) && aTime >= iEndTime )
   806     		{
   806             {
   807     		return ETrue;	
   807             return ETrue;
   808     		}	
   808             }
   809     		
   809 
   810     	// Check for start time, future rights	
   810         // Check for start time, future rights
   811     	if ( ( iActiveConstraints & EConstraintStartTime ) && aTime < iStartTime )
   811         if ( ( iActiveConstraints & EConstraintStartTime ) && aTime < iStartTime )
   812     		{
   812             {
   813     		return EFalse;	
   813             return EFalse;
   814     		}
   814             }
   815     	}
   815         }
   816 
   816 
   817     return EFalse;
   817     return EFalse;
   818     }
   818     }
   819 // -----------------------------------------------------------------------------
   819 // -----------------------------------------------------------------------------
   820 // CDRMConstraint::Merge
   820 // CDRMConstraint::Merge
   821 // -----------------------------------------------------------------------------
   821 // -----------------------------------------------------------------------------
   822 //      
   822 //
   823 EXPORT_C void CDRMConstraint::Merge( const CDRMConstraint& aConstraint )
   823 EXPORT_C void CDRMConstraint::Merge( const CDRMConstraint& aConstraint )
   824     {
   824     {
   825     TInt error = KErrNone;
   825     TInt error = KErrNone;
   826     
   826 
   827     if ( this != &aConstraint )
   827     if ( this != &aConstraint )
   828         {
   828         {
   829         if ( aConstraint.iActiveConstraints & EConstraintStartTime )
   829         if ( aConstraint.iActiveConstraints & EConstraintStartTime )
   830             {
   830             {
   831             if ( iActiveConstraints & EConstraintStartTime )
   831             if ( iActiveConstraints & EConstraintStartTime )
   836                 {
   836                 {
   837                 iStartTime = aConstraint.iStartTime;
   837                 iStartTime = aConstraint.iStartTime;
   838                 iActiveConstraints |= EConstraintStartTime;
   838                 iActiveConstraints |= EConstraintStartTime;
   839                 }
   839                 }
   840             }
   840             }
   841             
   841 
   842         if ( aConstraint.iActiveConstraints & EConstraintEndTime )
   842         if ( aConstraint.iActiveConstraints & EConstraintEndTime )
   843             {
   843             {
   844             if ( iActiveConstraints & EConstraintEndTime )
   844             if ( iActiveConstraints & EConstraintEndTime )
   845                 {
   845                 {
   846                 iEndTime = Min( aConstraint.iEndTime, iEndTime );
   846                 iEndTime = Min( aConstraint.iEndTime, iEndTime );
   849                 {
   849                 {
   850                 iEndTime = aConstraint.iEndTime;
   850                 iEndTime = aConstraint.iEndTime;
   851                 iActiveConstraints |= EConstraintEndTime;
   851                 iActiveConstraints |= EConstraintEndTime;
   852                 }
   852                 }
   853             }
   853             }
   854             
   854 
   855         if ( aConstraint.iActiveConstraints & EConstraintCounter )
   855         if ( aConstraint.iActiveConstraints & EConstraintCounter )
   856             {
   856             {
   857             if ( iActiveConstraints & EConstraintCounter )
   857             if ( iActiveConstraints & EConstraintCounter )
   858                 {
   858                 {
   859                 iCounter = Min( iCounter, aConstraint.iCounter );
   859                 iCounter = Min( iCounter, aConstraint.iCounter );
   864                 iCounter = aConstraint.iCounter;
   864                 iCounter = aConstraint.iCounter;
   865                 iOriginalCounter = aConstraint.iOriginalCounter;
   865                 iOriginalCounter = aConstraint.iOriginalCounter;
   866                 iActiveConstraints |= EConstraintCounter;
   866                 iActiveConstraints |= EConstraintCounter;
   867                 }
   867                 }
   868             }
   868             }
   869         
   869 
   870         if ( aConstraint.iActiveConstraints & EConstraintInterval )
   870         if ( aConstraint.iActiveConstraints & EConstraintInterval )
   871             {
   871             {
   872             if ( iActiveConstraints & EConstraintInterval )
   872             if ( iActiveConstraints & EConstraintInterval )
   873                 {
   873                 {
   874                 iIntervalStart = Max( iIntervalStart, aConstraint.iIntervalStart );
   874                 iIntervalStart = Max( iIntervalStart, aConstraint.iIntervalStart );
   879                 iIntervalStart = aConstraint.iIntervalStart;
   879                 iIntervalStart = aConstraint.iIntervalStart;
   880                 iInterval = aConstraint.iInterval;
   880                 iInterval = aConstraint.iInterval;
   881                 iActiveConstraints |= EConstraintInterval;
   881                 iActiveConstraints |= EConstraintInterval;
   882                 }
   882                 }
   883             }
   883             }
   884             
   884 
   885         if ( aConstraint.iActiveConstraints & EConstraintTimedCounter )
   885         if ( aConstraint.iActiveConstraints & EConstraintTimedCounter )
   886             {
   886             {
   887             if ( iActiveConstraints & EConstraintTimedCounter )
   887             if ( iActiveConstraints & EConstraintTimedCounter )
   888                 {
   888                 {
   889                 if ( aConstraint.iTimedCounter < iTimedCounter )
   889                 if ( aConstraint.iTimedCounter < iTimedCounter )
   897                 iTimedCounter = aConstraint.iTimedCounter;
   897                 iTimedCounter = aConstraint.iTimedCounter;
   898                 iTimedInterval = aConstraint.iTimedInterval;
   898                 iTimedInterval = aConstraint.iTimedInterval;
   899                 iActiveConstraints |= EConstraintTimedCounter;
   899                 iActiveConstraints |= EConstraintTimedCounter;
   900                 }
   900                 }
   901             }
   901             }
   902         
   902 
   903         if ( aConstraint.iActiveConstraints & EConstraintAccumulated )
   903         if ( aConstraint.iActiveConstraints & EConstraintAccumulated )
   904             {
   904             {
   905             if ( iActiveConstraints & EConstraintAccumulated )
   905             if ( iActiveConstraints & EConstraintAccumulated )
   906                 {
   906                 {
   907                 iAccumulatedTime = Min( iAccumulatedTime, aConstraint.iAccumulatedTime );
   907                 iAccumulatedTime = Min( iAccumulatedTime, aConstraint.iAccumulatedTime );
   910                 {
   910                 {
   911                 iAccumulatedTime = aConstraint.iAccumulatedTime;
   911                 iAccumulatedTime = aConstraint.iAccumulatedTime;
   912                 iActiveConstraints |= EConstraintAccumulated;
   912                 iActiveConstraints |= EConstraintAccumulated;
   913                 }
   913                 }
   914             }
   914             }
   915              
   915 
   916         if( aConstraint.iActiveConstraints & EConstraintIndividual )
   916         if( aConstraint.iActiveConstraints & EConstraintIndividual )
   917             {
   917             {
   918             // Ignore the error since we don't return an error code or leave
   918             // Ignore the error since we don't return an error code or leave
   919             TRAP( error, AppendToArrayL( iIndividual, aConstraint.iIndividual ) );
   919             TRAP( error, AppendToArrayL( iIndividual, aConstraint.iIndividual ) );
   920             }
   920             }
   921             
   921 
   922         }
   922         }
   923     }
   923     }
   924 
   924 
   925 // -----------------------------------------------------------------------------
   925 // -----------------------------------------------------------------------------
   926 // CDRMConstraint::Consume  
   926 // CDRMConstraint::Consume
   927 // -----------------------------------------------------------------------------
   927 // -----------------------------------------------------------------------------
   928 //  
   928 //
   929 EXPORT_C void CDRMConstraint::Consume( const TTime& aCurrentTime )
   929 EXPORT_C void CDRMConstraint::Consume( const TTime& aCurrentTime )
   930     {
   930     {
   931     if ( ( iActiveConstraints & EConstraintInterval ) &&
   931     if ( ( iActiveConstraints & EConstraintInterval ) &&
   932         iIntervalStart == Time::NullTTime() )
   932         iIntervalStart == Time::NullTTime() )
   933         {
   933         {
   934         iIntervalStart = aCurrentTime;
   934         iIntervalStart = aCurrentTime;
   935         }
   935         }
   936         
   936 
   937     if ( iActiveConstraints & EConstraintCounter )
   937     if ( iActiveConstraints & EConstraintCounter )
   938         {
   938         {
   939         --iCounter;
   939         --iCounter;
   940         }
   940         }
   941     }
   941     }
   944 // CDRMConstraint::DuplicateL
   944 // CDRMConstraint::DuplicateL
   945 // -----------------------------------------------------------------------------
   945 // -----------------------------------------------------------------------------
   946 //
   946 //
   947 EXPORT_C void CDRMConstraint::DuplicateL( const CDRMConstraint& aConstraint )
   947 EXPORT_C void CDRMConstraint::DuplicateL( const CDRMConstraint& aConstraint )
   948     {
   948     {
   949     
   949 
   950     // synchronizing marker
   950     // synchronizing marker
   951     iSyncMark = aConstraint.iSyncMark;
   951     iSyncMark = aConstraint.iSyncMark;
   952     
   952 
   953     // version number
   953     // version number
   954     iVersion = aConstraint.iVersion;
   954     iVersion = aConstraint.iVersion;
   955     
   955 
   956     // usage start time
   956     // usage start time
   957     iStartTime = aConstraint.iStartTime;
   957     iStartTime = aConstraint.iStartTime;
   958 
   958 
   959     // usage end time
   959     // usage end time
   960     iEndTime = aConstraint.iEndTime;
   960     iEndTime = aConstraint.iEndTime;
   970 
   970 
   971     // original counter value
   971     // original counter value
   972     iOriginalCounter = aConstraint.iOriginalCounter;
   972     iOriginalCounter = aConstraint.iOriginalCounter;
   973 
   973 
   974     // timed counter
   974     // timed counter
   975 	iTimedCounter = aConstraint.iTimedCounter;
   975     iTimedCounter = aConstraint.iTimedCounter;
   976 
   976 
   977     // Interval of the timed counter constraint
   977     // Interval of the timed counter constraint
   978 	iTimedInterval = aConstraint.iTimedInterval;
   978     iTimedInterval = aConstraint.iTimedInterval;
   979 
   979 
   980     // accumulated time
   980     // accumulated time
   981 	iAccumulatedTime = aConstraint.iAccumulatedTime;
   981     iAccumulatedTime = aConstraint.iAccumulatedTime;
   982 
   982 
   983     // individual allowed usage
   983     // individual allowed usage
   984     iIndividual.ResetAndDestroy();
   984     iIndividual.ResetAndDestroy();
   985     
   985 
   986     AppendToArrayL( iIndividual, aConstraint.iIndividual );
   986     AppendToArrayL( iIndividual, aConstraint.iIndividual );
   987 	
   987 
   988     // Software Vendor Id
   988     // Software Vendor Id
   989     iVendorId = aConstraint.iVendorId;
   989     iVendorId = aConstraint.iVendorId;
   990 	
   990 
   991     // Secure Id of the allowed application
   991     // Secure Id of the allowed application
   992     iSecureId = aConstraint.iSecureId;
   992     iSecureId = aConstraint.iSecureId;
   993 	
   993 
   994 	// Bitmask of active constraints
   994     // Bitmask of active constraints
   995     iActiveConstraints = aConstraint.iActiveConstraints;	// Bitmask
   995     iActiveConstraints = aConstraint.iActiveConstraints;    // Bitmask
   996 
   996 
   997 #ifdef RD_DRM_METERING    
   997 #ifdef RD_DRM_METERING
   998     // Metering information
   998     // Metering information
   999     if ( aConstraint.iDrmMeteringInfo )
   999     if ( aConstraint.iDrmMeteringInfo )
  1000         {
  1000         {
  1001         
  1001 
  1002         if( !iDrmMeteringInfo )
  1002         if( !iDrmMeteringInfo )
  1003             {
  1003             {
  1004             iDrmMeteringInfo = new (ELeave) TDrmMeteringInfo;
  1004             iDrmMeteringInfo = new (ELeave) TDrmMeteringInfo;
  1005             }
  1005             }
  1006     
  1006 
  1007         iDrmMeteringInfo->iGraceTime = aConstraint.iDrmMeteringInfo->iGraceTime;
  1007         iDrmMeteringInfo->iGraceTime = aConstraint.iDrmMeteringInfo->iGraceTime;
  1008         iDrmMeteringInfo->iAllowUseWithoutMetering = aConstraint.iDrmMeteringInfo->iAllowUseWithoutMetering; 
  1008         iDrmMeteringInfo->iAllowUseWithoutMetering = aConstraint.iDrmMeteringInfo->iAllowUseWithoutMetering;
  1009         }
  1009         }
  1010 #endif
  1010 #endif
  1011     
  1011 
  1012     // system allowed usage
  1012     // system allowed usage
  1013     iSystem.ResetAndDestroy();
  1013     iSystem.ResetAndDestroy();
  1014     AppendToArrayL( iSystem, aConstraint.iSystem );
  1014     AppendToArrayL( iSystem, aConstraint.iSystem );
  1015     
  1015 
  1016     // original timed counter value
  1016     // original timed counter value
  1017     iOriginalTimedCounter = aConstraint.iOriginalTimedCounter;
  1017     iOriginalTimedCounter = aConstraint.iOriginalTimedCounter;
  1018     
  1018 
  1019     };
  1019     };
  1020 
  1020 
  1021 
  1021 
  1022 // -----------------------------------------------------------------------------
  1022 // -----------------------------------------------------------------------------
  1023 // CDRMConstraint::Valid
  1023 // CDRMConstraint::Valid
  1024 // -----------------------------------------------------------------------------
  1024 // -----------------------------------------------------------------------------
  1025 //
  1025 //
  1026 EXPORT_C TBool CDRMConstraint::Valid( const TTime& aTime,
  1026 EXPORT_C TBool CDRMConstraint::Valid( const TTime& aTime,
  1027                                       const RPointerArray<HBufC8>& aIndividual,
  1027                                       const RPointerArray<HBufC8>& aIndividual,
  1028                                       TUint32& aRejection ) const
  1028                                       TUint32& aRejection ) const
  1029 	{
  1029     {
  1030 	TBool drmTime = EFalse;
  1030     TBool drmTime = EFalse;
  1031 	// Null the rejection requirement:
  1031     // Null the rejection requirement:
  1032 	aRejection = EConstraintNone;
  1032     aRejection = EConstraintNone;
  1033 	
  1033 
  1034 	// Full Rights are always valid
  1034     // Full Rights are always valid
  1035 	if ( iActiveConstraints == EConstraintNone )
  1035     if ( iActiveConstraints == EConstraintNone )
  1036 		{
  1036         {
  1037 		return ETrue;
  1037         return ETrue;
  1038 		}
  1038         }
  1039 
  1039 
  1040 	// First check counters, accumulated time and timed counters
  1040     // First check counters, accumulated time and timed counters
  1041 	// if any of these are invalid the whole thing is invalid regardless of the
  1041     // if any of these are invalid the whole thing is invalid regardless of the
  1042 	// actual time based constraints
  1042     // actual time based constraints
  1043 	
  1043 
  1044     // Counters
  1044     // Counters
  1045     if ( ( iActiveConstraints & EConstraintCounter ) && iCounter < 1 ) 
  1045     if ( ( iActiveConstraints & EConstraintCounter ) && iCounter < 1 )
  1046     	{
  1046         {
  1047     	aRejection |= EConstraintCounter;
  1047         aRejection |= EConstraintCounter;
  1048     	}
  1048         }
  1049     		
  1049 
  1050     // Accumulated time	
  1050     // Accumulated time
  1051     if ( ( iActiveConstraints & EConstraintAccumulated ) && iAccumulatedTime.Int() == 0 )
  1051     if ( ( iActiveConstraints & EConstraintAccumulated ) && iAccumulatedTime.Int() == 0 )
  1052     	{
  1052         {
  1053     	aRejection |= EConstraintAccumulated;
  1053         aRejection |= EConstraintAccumulated;
  1054     	}
  1054         }
  1055     		
  1055 
  1056      // Timed Counters
  1056      // Timed Counters
  1057     if ( ( iActiveConstraints & EConstraintTimedCounter ) && iTimedCounter < 1 ) 
  1057     if ( ( iActiveConstraints & EConstraintTimedCounter ) && iTimedCounter < 1 )
  1058     	{
  1058         {
  1059     	aRejection |= EConstraintTimedCounter;	
  1059         aRejection |= EConstraintTimedCounter;
  1060     	} 
  1060         }
  1061     		
  1061 
  1062     // Dont check time based rights
  1062     // Dont check time based rights
  1063     if ( aTime != Time::NullTTime() )
  1063     if ( aTime != Time::NullTTime() )
  1064     	{	
  1064         {
  1065     	drmTime = ETrue;
  1065         drmTime = ETrue;
  1066     	// Check for activated intervals
  1066         // Check for activated intervals
  1067     	if ( (iActiveConstraints & EConstraintInterval) && iIntervalStart != Time::NullTTime() )
  1067         if ( (iActiveConstraints & EConstraintInterval) && iIntervalStart != Time::NullTTime() )
  1068     		{
  1068             {
  1069         	TTimeIntervalSeconds current;
  1069             TTimeIntervalSeconds current;
  1070         
  1070 
  1071         	aTime.SecondsFrom( iIntervalStart, current );
  1071             aTime.SecondsFrom( iIntervalStart, current );
  1072         
  1072 
  1073         	if ( ( current >= iInterval ) || ( aTime < iIntervalStart ) )
  1073             if ( ( current >= iInterval ) || ( aTime < iIntervalStart ) )
  1074             	{
  1074                 {
  1075             	aRejection |= EConstraintInterval;
  1075                 aRejection |= EConstraintInterval;
  1076             	}    			
  1076                 }
  1077     		}
  1077             }
  1078     	
  1078 
  1079     	// Check for end time
  1079         // Check for end time
  1080     	if ( ( iActiveConstraints & EConstraintEndTime ) && aTime >= iEndTime ) 
  1080         if ( ( iActiveConstraints & EConstraintEndTime ) && aTime >= iEndTime )
  1081     	    {
  1081             {
  1082     	    aRejection |= EConstraintEndTime;
  1082             aRejection |= EConstraintEndTime;
  1083     	    }
  1083             }
  1084     	    
  1084 
  1085     	if ( ( iActiveConstraints & EConstraintStartTime ) && aTime < iStartTime )     
  1085         if ( ( iActiveConstraints & EConstraintStartTime ) && aTime < iStartTime )
  1086     		{
  1086             {
  1087     		aRejection |= EConstraintStartTime;	
  1087             aRejection |= EConstraintStartTime;
  1088     		}	
  1088             }
  1089     	}
  1089         }
  1090     else
  1090     else
  1091         {
  1091         {
  1092         drmTime = EFalse;
  1092         drmTime = EFalse;
  1093         
  1093 
  1094     	// Check for activated intervals
  1094         // Check for activated intervals
  1095     	if ( (iActiveConstraints & EConstraintInterval) )
  1095         if ( (iActiveConstraints & EConstraintInterval) )
  1096     		{
  1096             {
  1097             aRejection |= EConstraintInterval; 			
  1097             aRejection |= EConstraintInterval;
  1098     		}
  1098             }
  1099     	
  1099 
  1100     	// Check for end time
  1100         // Check for end time
  1101     	if ( ( iActiveConstraints & EConstraintEndTime ) ) 
  1101         if ( ( iActiveConstraints & EConstraintEndTime ) )
  1102     	    {
  1102             {
  1103     	    aRejection |= EConstraintEndTime;
  1103             aRejection |= EConstraintEndTime;
  1104     	    }
  1104             }
  1105     	    
  1105 
  1106     	if( ( iActiveConstraints & EConstraintStartTime ) )     
  1106         if( ( iActiveConstraints & EConstraintStartTime ) )
  1107     		{
  1107             {
  1108     		aRejection |= EConstraintStartTime;	
  1108             aRejection |= EConstraintStartTime;
  1109     		}
  1109             }
  1110         }	
  1110         }
  1111 
  1111 
  1112     // IMSI Checking:
  1112     // IMSI Checking:
  1113     if( iActiveConstraints & EConstraintIndividual )
  1113     if( iActiveConstraints & EConstraintIndividual )
  1114         {
  1114         {
  1115         if ( !aIndividual.Count() && !iIndividual.Count() )
  1115         if ( !aIndividual.Count() && !iIndividual.Count() )
  1118             }
  1118             }
  1119         else if( !IsIndividualConstraintValid( iIndividual, aIndividual ) )
  1119         else if( !IsIndividualConstraintValid( iIndividual, aIndividual ) )
  1120             {
  1120             {
  1121             aRejection |= EConstraintIndividual;
  1121             aRejection |= EConstraintIndividual;
  1122             }
  1122             }
  1123         }        
  1123         }
  1124 
  1124 
  1125     if( aRejection )
  1125     if( aRejection )
  1126         {
  1126         {
  1127         // drmTime is null, so some constraints may have been discarded because of that
  1127         // drmTime is null, so some constraints may have been discarded because of that
  1128         if( !drmTime )
  1128         if( !drmTime )
  1129             {
  1129             {
  1130             aRejection |= EConstraintNullDrmTime;
  1130             aRejection |= EConstraintNullDrmTime;
  1131             }       
  1131             }
  1132         return EFalse;        
  1132         return EFalse;
  1133         }
  1133         }
  1134 
  1134 
  1135     return ETrue;		
  1135     return ETrue;
  1136 	};
  1136     };
  1137 
  1137 
  1138 
  1138 
  1139 // -----------------------------------------------------------------------------
  1139 // -----------------------------------------------------------------------------
  1140 // CDRMConstraint::ConstructL
  1140 // CDRMConstraint::ConstructL
  1141 // 2nd phase constructor
  1141 // 2nd phase constructor
  1142 // -----------------------------------------------------------------------------
  1142 // -----------------------------------------------------------------------------
  1143 //
  1143 //
  1144 void CDRMConstraint::ConstructL()
  1144 void CDRMConstraint::ConstructL()
  1145     {
  1145     {
  1146     };
  1146     };
  1147     
  1147 
  1148 // -----------------------------------------------------------------------------
  1148 // -----------------------------------------------------------------------------
  1149 // CDRMConstraint::WriteInt64L
  1149 // CDRMConstraint::WriteInt64L
  1150 // -----------------------------------------------------------------------------
  1150 // -----------------------------------------------------------------------------
  1151 //
  1151 //
  1152 void CDRMConstraint::WriteInt64L( const TInt64& aWrite, RWriteStream& aStream ) const
  1152 void CDRMConstraint::WriteInt64L( const TInt64& aWrite, RWriteStream& aStream ) const
  1153     {
  1153     {
  1154     TPtr8 output(NULL,0,0);
  1154     TPtr8 output(NULL,0,0);
  1155     
  1155 
  1156     output.Set( reinterpret_cast<TUint8*>(const_cast<TInt64*>(&aWrite)), 
  1156     output.Set( reinterpret_cast<TUint8*>(const_cast<TInt64*>(&aWrite)),
  1157                 sizeof(TInt64), sizeof(TInt64) );
  1157                 sizeof(TInt64), sizeof(TInt64) );
  1158     
  1158 
  1159     aStream.WriteL( output, sizeof(TInt64) );
  1159     aStream.WriteL( output, sizeof(TInt64) );
  1160     };
  1160     };
  1161 
  1161 
  1162 // -----------------------------------------------------------------------------
  1162 // -----------------------------------------------------------------------------
  1163 // CDRMConstraint::ReadInt64L
  1163 // CDRMConstraint::ReadInt64L
  1164 // -----------------------------------------------------------------------------
  1164 // -----------------------------------------------------------------------------
  1165 //
  1165 //
  1166 void CDRMConstraint::ReadInt64L( TInt64& aRead, RReadStream& aStream )
  1166 void CDRMConstraint::ReadInt64L( TInt64& aRead, RReadStream& aStream )
  1167     {
  1167     {
  1168     TPtr8 input(NULL,0,0);
  1168     TPtr8 input(NULL,0,0);
  1169     
  1169 
  1170     input.Set( reinterpret_cast<TUint8*>(&aRead), 0, sizeof(TInt64) );
  1170     input.Set( reinterpret_cast<TUint8*>(&aRead), 0, sizeof(TInt64) );
  1171     
  1171 
  1172     aStream.ReadL( input, sizeof(TInt64) );    
  1172     aStream.ReadL( input, sizeof(TInt64) );
  1173     };        
  1173     };
  1174 // End of File
  1174 // End of File