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