omadrm/drmengine/server/src/DRMRightsData.cpp
changeset 0 95b198f216e5
child 12 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2004 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 for Rights Constraint
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <s32file.h>
       
    22 #include <s32std.h>
       
    23 #include <caf/caf.h>
       
    24 
       
    25 #include "DRMPointerArray.h"
       
    26 #include "DRMCommonData.h"
       
    27 #include "DRMPermission.h"
       
    28 #include "DRMRightsData.h"
       
    29 
       
    30 // EXTERNAL DATA STRUCTURES
       
    31 
       
    32 // EXTERNAL FUNCTION PROTOTYPES  
       
    33 
       
    34 // CONSTANTS
       
    35 const TInt KEncryptionKeySize = 16;
       
    36 
       
    37 // MACROS
       
    38 
       
    39 // LOCAL CONSTANTS AND MACROS
       
    40 
       
    41 // MODULE DATA STRUCTURES
       
    42 
       
    43 // LOCAL FUNCTION PROTOTYPES
       
    44 
       
    45 // FORWARD DECLARATIONS
       
    46 
       
    47 class TPermData 
       
    48     {
       
    49     public: // Constructor
       
    50     TPermData( HBufC8* aParentId, const TInt aIndex, TBool aIsValid ) :
       
    51         iParentId( aParentId ),
       
    52         iIndex( aIndex ),
       
    53         iIsValid( aIsValid ) { };
       
    54     
       
    55     public: // Member variables    
       
    56     HBufC8* iParentId; 
       
    57     TInt iIndex;
       
    58     TBool iIsValid;
       
    59     };
       
    60 
       
    61 
       
    62 // ============================= LOCAL FUNCTIONS ===============================
       
    63 // ============================ MEMBER FUNCTIONS ===============================
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // CDRMRightsData::NewLC
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 CDRMRightsData* CDRMRightsData::NewLC( CDRMCommonData* aData,
       
    70                                        const TDesC8& aKey, 
       
    71                                        const TFileName& aRightsFile,
       
    72                                        RFs& aFileServer )
       
    73     {
       
    74     CDRMRightsData* self = new( ELeave ) CDRMRightsData( aData, aFileServer );
       
    75     CleanupStack::PushL( self );
       
    76     self->ConstructL( aRightsFile, aKey );
       
    77 
       
    78     return self;    
       
    79     };
       
    80     
       
    81 // -----------------------------------------------------------------------------
       
    82 // CDRMRightsData::NewL
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CDRMRightsData* CDRMRightsData::NewL( CDRMCommonData* aData, 
       
    86                                       const TDesC8& aKey,
       
    87                                       const TFileName& aRightsFile,
       
    88                                       RFs& aFileServer )
       
    89     {
       
    90     CDRMRightsData* self = NewLC( aData, aKey, aRightsFile, aFileServer );
       
    91     CleanupStack::Pop();	
       
    92     
       
    93     return self;    
       
    94     };
       
    95                                  
       
    96 // -----------------------------------------------------------------------------
       
    97 // CDRMRightsData::OpenLC
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CDRMRightsData* CDRMRightsData::OpenLC( const TFileName& aRightsFile,
       
   101                                         RFs& aFileServer )
       
   102     {
       
   103     CDRMCommonData* common = CDRMCommonData::NewL();
       
   104 
       
   105     CDRMRightsData* self = new( ELeave ) CDRMRightsData( common, aFileServer );
       
   106     CleanupStack::PushL( self );    
       
   107     self->ConstructL( aRightsFile );
       
   108     
       
   109     return self;
       
   110     };    
       
   111     
       
   112 // -----------------------------------------------------------------------------
       
   113 // CDRMRightsData::OpenL
       
   114 // -----------------------------------------------------------------------------
       
   115 //
       
   116 CDRMRightsData* CDRMRightsData::OpenL( const TFileName& aRightsFile,
       
   117                                        RFs& aFileServer )
       
   118     {
       
   119     CDRMRightsData* self = OpenLC( aRightsFile, aFileServer );
       
   120     CleanupStack::Pop();
       
   121     
       
   122     return self;    
       
   123     };    
       
   124     
       
   125 // -----------------------------------------------------------------------------
       
   126 // Destructor
       
   127 // -----------------------------------------------------------------------------
       
   128 // 
       
   129 CDRMRightsData::~CDRMRightsData()
       
   130     {
       
   131     // Permanent File Store
       
   132     if( iStore )
       
   133         {;
       
   134         delete iStore;
       
   135         iStore = NULL;
       
   136         }
       
   137         
       
   138     // index array    
       
   139     iArray.Close();
       
   140     
       
   141     // Common Data
       
   142     if( iData )
       
   143         {
       
   144         delete iData;
       
   145         iData = NULL;
       
   146         }
       
   147     }; 
       
   148 
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CDRMRightsData::GetCommonDataL
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 const CDRMCommonData* CDRMRightsData::GetCommonDataL() const
       
   155     {
       
   156     return iData;
       
   157     };
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CDRMRightsData::UpdateCommonDataL
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CDRMRightsData::UpdateCommonDataL( CDRMCommonData* aData )
       
   164     {
       
   165     RStoreWriteStream stream;
       
   166 
       
   167     // If not data is given, that is an error
       
   168     if( !aData )
       
   169         {
       
   170         User::Leave( KErrArgument );
       
   171         }
       
   172            
       
   173     // Open the stream 
       
   174     stream.ReplaceLC( *iStore, iCommonId );
       
   175     
       
   176     // Delete the old data if it's around
       
   177     // if the old data has been modified, write it as such
       
   178     if( iData != aData )
       
   179     	{
       
   180         if( iData )
       
   181         	{
       
   182         	delete iData;
       
   183         	iData = NULL;        	
       
   184         	}
       
   185     	iData = aData;        
       
   186         }
       
   187     
       
   188     // Write the data to the stream
       
   189     iData->ExternalizeL( stream );
       
   190     
       
   191     // commit the stream
       
   192     stream.CommitL();
       
   193     
       
   194     // commit the store
       
   195     iStore->CommitL();
       
   196     
       
   197     // pop the stream
       
   198     CleanupStack::PopAndDestroy();
       
   199     };        
       
   200     
       
   201 // -----------------------------------------------------------------------------
       
   202 // CDRMRightsData::StoreNewPermissionL
       
   203 // -----------------------------------------------------------------------------
       
   204 //
       
   205 void CDRMRightsData::StoreNewPermissionL( CDRMPermission& aPermission,
       
   206                                           TDRMUniqueID& aStream )
       
   207     {
       
   208     TStreamId sid;
       
   209     TStreamId rootId;
       
   210     RStoreWriteStream stream;
       
   211     RStoreWriteStream rootStream;
       
   212        
       
   213     // Create a new stream to the store:
       
   214     sid = stream.CreateLC( *iStore );
       
   215     
       
   216     aPermission.iUniqueID = sid.Value();   
       
   217     aStream = sid.Value();
       
   218     
       
   219     // Externalize the permission data
       
   220     aPermission.ExternalizeL( stream );
       
   221     stream.CommitL();
       
   222         
       
   223     // add the id to the array
       
   224     iArray.InsertInUnsignedKeyOrder( aStream );
       
   225 
       
   226     // Get the stream id of the root
       
   227     rootId = iStore->Root();
       
   228     
       
   229     // Open the root stream
       
   230     rootStream.ReplaceLC( *iStore, rootId );
       
   231         
       
   232     // Store the changed index
       
   233     StoreIndexL( rootStream );    
       
   234     rootStream.CommitL();
       
   235 
       
   236     // Commit the store
       
   237     iStore->CommitL();
       
   238     
       
   239     CleanupStack::PopAndDestroy();  // root stream
       
   240     CleanupStack::PopAndDestroy();  // stream
       
   241     
       
   242     iStore->CompactL();
       
   243     iStore->CommitL();
       
   244     };    
       
   245     
       
   246 // -----------------------------------------------------------------------------
       
   247 // CDRMRightsData::UpdatePermissionL
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 void CDRMRightsData::UpdatePermissionL( const CDRMPermission& aPermission )
       
   251     {
       
   252     TStreamId sid( aPermission.iUniqueID );
       
   253     RStoreWriteStream stream;
       
   254     TInt index = 0;
       
   255     
       
   256     // get the id from the array if it doesn't exist, error
       
   257     index = iArray.FindInUnsignedKeyOrder( aPermission.iUniqueID );
       
   258     
       
   259     if( index == KErrNotFound )
       
   260         {
       
   261         User::Leave( KErrCANoPermission );
       
   262         }
       
   263     
       
   264     // Replace the existing stream
       
   265     stream.ReplaceLC( *iStore, sid );
       
   266     
       
   267     // Externalize the permission data
       
   268     aPermission.ExternalizeL( stream );
       
   269     
       
   270     // Required by the ReplaceLC
       
   271     stream.CommitL();
       
   272 
       
   273     // Commit the store    
       
   274     iStore->CommitL();
       
   275     
       
   276     CleanupStack::PopAndDestroy();
       
   277     };
       
   278     
       
   279 // -----------------------------------------------------------------------------
       
   280 // CDRMRightsData::FetchPermissionL
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 void CDRMRightsData::FetchPermissionL( CDRMPermission& aPermission,
       
   284                                        const TDRMUniqueID& aStream ) const
       
   285     {
       
   286     TStreamId sid( aStream );    
       
   287     RStoreReadStream stream;
       
   288     TInt index = 0;
       
   289 
       
   290     // get the id from the array if it doesn't exist, error
       
   291     index = iArray.FindInUnsignedKeyOrder( aStream );
       
   292     
       
   293     if( index == KErrNotFound )
       
   294         {
       
   295         User::Leave( KErrCANoPermission );
       
   296         }
       
   297     
       
   298     // Open the root stream
       
   299     stream.OpenLC( *iStore, sid );
       
   300 
       
   301     // Read the common id
       
   302     aPermission.InternalizeL( stream );
       
   303     
       
   304     // Close the stream
       
   305     CleanupStack::PopAndDestroy();        
       
   306     };
       
   307     
       
   308 // -----------------------------------------------------------------------------
       
   309 // CDRMRightsData::DeletePermissionL
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CDRMRightsData::DeletePermissionL( const TDRMUniqueID& aStream )
       
   313     {
       
   314     TStreamId sid( aStream );
       
   315     TStreamId rootId;
       
   316     TInt index = 0;
       
   317     RStoreWriteStream rootStream;
       
   318         
       
   319     // get the id from the array if it doesn't exist, error
       
   320     index = iArray.FindInUnsignedKeyOrder( aStream );
       
   321     
       
   322     if( index == KErrNotFound )
       
   323         {
       
   324         User::Leave( KErrCANoPermission );
       
   325         }
       
   326 
       
   327     // Delete the stream from the store
       
   328     iStore->DeleteL( sid );
       
   329             
       
   330     iArray.Remove( index );       
       
   331     
       
   332     
       
   333     // Get the stream id of the root
       
   334     rootId = iStore->Root();
       
   335     
       
   336     // Open the root stream
       
   337     rootStream.ReplaceLC( *iStore, rootId );
       
   338         
       
   339     // Store the changed index
       
   340     StoreIndexL( rootStream );    
       
   341     rootStream.CommitL();
       
   342 
       
   343     // Commit the store
       
   344     iStore->CommitL();
       
   345     
       
   346     CleanupStack::PopAndDestroy();  // root stream
       
   347     
       
   348     // Compact and commit the changes
       
   349     iStore->CompactL();
       
   350     iStore->CommitL();        
       
   351     };
       
   352                            
       
   353 // -----------------------------------------------------------------------------
       
   354 // CDRMRightsData::FetchAllPermissionsL
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 void CDRMRightsData::FetchAllPermissionsL( RPointerArray<CDRMPermission>& 
       
   358                                            aPointerArray)
       
   359     {
       
   360     CDRMPermission* perm = NULL;
       
   361 
       
   362     if ( iArray.Count() == 0) 
       
   363         {
       
   364         User::Leave( KErrCANoPermission );
       
   365         }
       
   366     
       
   367     for( TInt count = 0; count < iArray.Count(); count++ )
       
   368         {
       
   369         perm = CDRMPermission::NewLC();
       
   370         FetchPermissionL( *perm, iArray[ count ] );
       
   371         aPointerArray.AppendL( perm );
       
   372 		CleanupStack::Pop();
       
   373         }
       
   374     };    
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // CDRMRightsData::DeleteAllPermissionsL
       
   378 // -----------------------------------------------------------------------------
       
   379 //
       
   380 void CDRMRightsData::DeleteAllPermissionsL()
       
   381     {
       
   382     TStreamId rootId;
       
   383     RStoreWriteStream rootStream;
       
   384     
       
   385     // Delete all permissions from the store
       
   386     for( TInt i = 0; i < iArray.Count(); i++ )
       
   387         {
       
   388         TStreamId sid( iArray[i] );
       
   389         iStore->DeleteL( sid );
       
   390         }
       
   391         
       
   392     // Reset the array    
       
   393     iArray.Reset();
       
   394     
       
   395     // Get the stream id of the root
       
   396     rootId = iStore->Root();
       
   397     
       
   398     // Open the root stream
       
   399     rootStream.ReplaceLC( *iStore, rootId );
       
   400         
       
   401     // Store the changed index
       
   402     StoreIndexL( rootStream );    
       
   403     rootStream.CommitL();
       
   404 
       
   405     // Commit the store
       
   406     iStore->CommitL();
       
   407     
       
   408     CleanupStack::PopAndDestroy();  // root stream
       
   409     
       
   410     // Compact and Commit the store
       
   411     iStore->CompactL();
       
   412     iStore->CommitL();          
       
   413     };
       
   414 
       
   415 // -----------------------------------------------------------------------------
       
   416 // CDRMRightsData::DeleteExpiredPermissionsL
       
   417 // Deletes all rights objects which have been expired
       
   418 // if the given time aTime is Time::NullTTime() then
       
   419 // deletes only non time based rights
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 TInt CDRMRightsData::DeleteExpiredPermissionsL( const TTime& aTime, TBool& aParents )
       
   423 	{
       
   424 	CDRMPermission* permission = NULL;	   
       
   425     RStoreReadStream stream;   
       
   426     TStreamId rootId;
       
   427     RStoreWriteStream rootStream;
       
   428     TBool remove = EFalse;
       
   429     TInt k = 0;
       
   430     TBool performDelete = ETrue;
       
   431     
       
   432     // A list for the permissions:
       
   433     CDRMPointerArray<CDRMPermission>* permList = CDRMPointerArray<CDRMPermission>::NewLC();
       
   434     permList->SetAutoCleanup( ETrue );
       
   435     CDRMPointerArray<TPermData>* permDataList = CDRMPointerArray<TPermData>::NewLC();
       
   436     permDataList->SetAutoCleanup( ETrue );
       
   437     
       
   438     // Fill the array:
       
   439     for( TInt j = 0  ; j < iArray.Count(); j++ )
       
   440         {
       
   441         TPermData* perm = NULL;
       
   442         // Create the sid:
       
   443         TStreamId psid( iArray[j] );
       
   444 
       
   445     	// Open the correct stream
       
   446     	stream.OpenLC( *iStore, psid );
       
   447 
       
   448 	    permission = CDRMPermission::NewLC();
       
   449 
       
   450     	// Read the permission
       
   451     	permission->InternalizeL( stream );
       
   452     
       
   453         permList->AppendL( permission );
       
   454     	CleanupStack::Pop(); // permission
       
   455     	        
       
   456         // if the permission has a parent check if we found now or have found an
       
   457         // expired one or a valid one,
       
   458         // store info if required 
       
   459         if( permission->iParentUID )
       
   460             {
       
   461             if( !aParents ) 
       
   462                 {
       
   463                 aParents = ETrue;
       
   464                 }
       
   465             
       
   466             for( k = 0; k < permDataList->Count(); k++ )
       
   467                 {
       
   468                 if( !(*permDataList)[k]->iParentId->Compare( *permission->iParentUID ) )
       
   469                     {
       
   470                     if( !(*permDataList)[k]->iIsValid && !permission->Expired( aTime ) )
       
   471                         {
       
   472                         (*permDataList)[k]->iParentId = permission->iParentUID;
       
   473                         (*permDataList)[k]->iIndex = j;
       
   474                         (*permDataList)[k]->iIsValid = ETrue;
       
   475                         }
       
   476                     break;
       
   477                     }
       
   478                 }
       
   479                 
       
   480             // if it's not found, add it:
       
   481             if( k == permDataList->Count() )
       
   482                 {
       
   483                 if( !permission->Expired( aTime ) )
       
   484                     {
       
   485                     perm = new(ELeave) TPermData( permission->iParentUID, j, ETrue);
       
   486                     }
       
   487                 else 
       
   488                     {
       
   489                     perm = new(ELeave) TPermData( permission->iParentUID, j, EFalse);                       
       
   490                     }
       
   491                 CleanupStack::PushL( perm );
       
   492                 permDataList->AppendL( perm );
       
   493                 CleanupStack::Pop(); // perm   
       
   494                 }
       
   495             }
       
   496         
       
   497         // Close the stream
       
   498     	CleanupStack::PopAndDestroy();  // stream       
       
   499         }
       
   500           
       
   501 
       
   502     // Delete all expired permissions from the store
       
   503     for( TInt i = iArray.Count() - 1 ; i >= 0; i-- )
       
   504         {
       
   505         permission = (*permList)[i];
       
   506         
       
   507 		// Check if the permission is expired
       
   508 		// if it is, check if it has a parent and if it can be deleted
       
   509 		if( permission->Expired( aTime ) )
       
   510 			{
       
   511 			// if it has a parent go through the list and see if this can be deleted
       
   512 			// or not
       
   513 			if( permission->iParentUID )
       
   514 			    {
       
   515 			    performDelete = EFalse;
       
   516 			    for( k = 0; k < permDataList->Count(); k++ )
       
   517 			        {
       
   518 			        TPermData* aData = (*permDataList)[k];
       
   519 			        
       
   520 			        // since it's set up like this a pointer comparison is enough:
       
   521 			        if( !(*permDataList)[k]->iParentId->Compare( *permission->iParentUID  ) ) 
       
   522 			            {
       
   523 			            if( i != (*permDataList)[k]->iIndex ) 
       
   524 			                {
       
   525 			                performDelete = ETrue;
       
   526 			                }
       
   527 			            }
       
   528 			        }
       
   529 			    }
       
   530 			
       
   531 		    if( performDelete )	
       
   532 		        {
       
   533                 // Create the sid:
       
   534                 TStreamId sid( iArray[i] );
       
   535                 		        
       
   536 			    iStore->Delete( sid );
       
   537 			
       
   538 	            // remove from the array		
       
   539 		        iArray.Remove( i );				
       
   540 		    
       
   541 		        if( !remove )
       
   542 		            {
       
   543 		            remove = ETrue;    
       
   544 		            }
       
   545 			    }    
       
   546 			}
       
   547 		performDelete = ETrue;			
       
   548         }
       
   549 
       
   550     CleanupStack::PopAndDestroy( 2 ); // permDataList, permList 
       
   551         
       
   552     // Write the data into the the file   
       
   553     // if some of the rights have been removed
       
   554     if( remove ) 
       
   555         {
       
   556         // Get the stream id of the root
       
   557         rootId = iStore->Root();
       
   558     
       
   559         // Open the root stream
       
   560         rootStream.ReplaceLC( *iStore, rootId );
       
   561         
       
   562         // Store the changed index
       
   563         StoreIndexL( rootStream );    
       
   564         rootStream.CommitL();
       
   565 
       
   566         // Commit the store
       
   567         iStore->CommitL();
       
   568     
       
   569         CleanupStack::PopAndDestroy();  // root stream
       
   570         
       
   571         // Compact and Commit the store
       
   572         iStore->CompactL();
       
   573         iStore->CommitL();             
       
   574         }
       
   575            
       
   576 	return iArray.Count();	
       
   577 	}
       
   578 
       
   579     
       
   580 // -----------------------------------------------------------------------------
       
   581 // CDRMRightsData::StoreKeyL
       
   582 // -----------------------------------------------------------------------------
       
   583 //
       
   584 void CDRMRightsData::StoreKeyL()
       
   585     {
       
   586     RStoreWriteStream stream;
       
   587     TUint32 size = 0;
       
   588     
       
   589     stream.OpenLC( *iStore, iKeyId );
       
   590     
       
   591     if( iKeyExists ) 
       
   592         {
       
   593         size = KEncryptionKeySize;
       
   594         }
       
   595         
       
   596     stream.WriteUint32L( size );        
       
   597     
       
   598     if( size )
       
   599         {
       
   600         stream.WriteL( iKey, KEncryptionKeySize );        
       
   601         }
       
   602         
       
   603     stream.CommitL();
       
   604     iStore->CommitL();
       
   605         
       
   606     CleanupStack::PopAndDestroy();
       
   607     };
       
   608 
       
   609 // -----------------------------------------------------------------------------
       
   610 // CDRMRightsData::GetKeyL
       
   611 // -----------------------------------------------------------------------------
       
   612 //
       
   613 HBufC8* CDRMRightsData::GetKeyL() const
       
   614     {
       
   615     HBufC8* key = NULL;
       
   616     
       
   617     if( iKeyExists )
       
   618         {
       
   619         key = HBufC8::NewL( KEncryptionKeySize );
       
   620         *key = iKey;
       
   621         }
       
   622         
       
   623     return key;
       
   624     }
       
   625         
       
   626 // -----------------------------------------------------------------------------
       
   627 // CDRMRightsData::FetchKeyL
       
   628 // -----------------------------------------------------------------------------
       
   629 //
       
   630 void CDRMRightsData::FetchKeyL()
       
   631     {
       
   632     RStoreReadStream stream;
       
   633     TUint32 size = 0;
       
   634     
       
   635     // Open the root stream
       
   636     stream.OpenLC( *iStore, iKeyId );
       
   637     
       
   638     size = stream.ReadUint32L();  
       
   639     
       
   640     if( !size ) 
       
   641         {
       
   642         iKeyExists = EFalse;
       
   643         }
       
   644     else 
       
   645         {
       
   646         stream.ReadL( iKey, KEncryptionKeySize );
       
   647         iKeyExists = ETrue;
       
   648         }
       
   649     
       
   650     CleanupStack::PopAndDestroy();    
       
   651     };
       
   652 
       
   653 // -----------------------------------------------------------------------------
       
   654 // CDRMRightsData::CreateAndInitializeStoreL
       
   655 // -----------------------------------------------------------------------------
       
   656 //
       
   657 void CDRMRightsData::CreateAndInitializeStoreL( const TFileName& aRightsStore )
       
   658     {
       
   659     TStreamId rootId;
       
   660     RStoreWriteStream stream;
       
   661     RStoreWriteStream stream2;    
       
   662     RStoreWriteStream rootStream;
       
   663     TUint32 size = 0;    
       
   664     
       
   665     // Create and initialize the permanent file store
       
   666     iStore = CPermanentFileStore::ReplaceL( iFileServer, aRightsStore,
       
   667                                             EFileRead|EFileWrite );                                          
       
   668     iStore->SetTypeL( KPermanentFileStoreLayoutUid );
       
   669     iStore->CommitL(); 
       
   670     
       
   671     // Create the root stream:                                                   
       
   672     rootId = rootStream.CreateLC( *iStore );
       
   673     rootStream.CommitL();
       
   674     iStore->SetRootL( rootId );    
       
   675     iStore->CommitL(); 
       
   676         
       
   677     // Create the common data
       
   678     iCommonId = stream.CreateLC( *iStore );
       
   679     iData->ExternalizeL( stream );
       
   680     stream.CommitL();
       
   681     iStore->CommitL(); 
       
   682         
       
   683     CleanupStack::PopAndDestroy(); // stream
       
   684     
       
   685     // Create the key
       
   686     iKeyId = stream2.CreateLC( *iStore );
       
   687 
       
   688     // if the key exists set the key size accordingly
       
   689     if( iKeyExists ) 
       
   690         {
       
   691         size = KEncryptionKeySize;
       
   692         }
       
   693     stream2.WriteUint32L( size );
       
   694 
       
   695     if( size )
       
   696         {
       
   697         stream2.WriteL( iKey, KEncryptionKeySize );
       
   698         }
       
   699     stream2.CommitL();
       
   700     iStore->CommitL(); 
       
   701            
       
   702     CleanupStack::PopAndDestroy(); // stream2                                      
       
   703     CleanupStack::PopAndDestroy(); // rootStream  
       
   704     
       
   705     // Create the index now that we have all the data
       
   706     rootId = iStore->Root(); 
       
   707     
       
   708     rootStream.ReplaceLC(*iStore, rootId);
       
   709     StoreIndexL( rootStream );
       
   710     rootStream.CommitL();
       
   711     iStore->CommitL(); 
       
   712         
       
   713     // Commit the changes to the store as well
       
   714     CleanupStack::PopAndDestroy(); // rootStream       
       
   715     };
       
   716 
       
   717 
       
   718 // -----------------------------------------------------------------------------
       
   719 // CDRMRightsData::StoreIndexL
       
   720 // -----------------------------------------------------------------------------
       
   721 //
       
   722 void CDRMRightsData::StoreIndexL( RWriteStream& aStream ) const
       
   723     {
       
   724     // Write the common id to the stream
       
   725     iCommonId.ExternalizeL( aStream );
       
   726     
       
   727     // Write the key id to the stream
       
   728     iKeyId.ExternalizeL( aStream );
       
   729     
       
   730     // Write the array size and possibly the array to the stream
       
   731     aStream.WriteInt32L( iArray.Count() );
       
   732     
       
   733     for( TInt count = 0; count < iArray.Count(); count++ )
       
   734         {
       
   735         aStream.WriteUint32L( iArray[ count ] );
       
   736         }
       
   737     };
       
   738     
       
   739 // -----------------------------------------------------------------------------
       
   740 // CDRMRightsData::FetchIndexL
       
   741 // -----------------------------------------------------------------------------
       
   742 //
       
   743 void CDRMRightsData::FetchIndexL()
       
   744     {
       
   745     TStreamId rootId;
       
   746     RStoreReadStream stream;
       
   747     TInt count = 0;
       
   748     TDRMUniqueID id;
       
   749  
       
   750     // Get the stream id of the root
       
   751     rootId = iStore->Root();
       
   752     
       
   753     // Open the root stream
       
   754     stream.OpenLC( *iStore, rootId );
       
   755     
       
   756     // Do the actual reading, reading also the key id and the common id
       
   757 
       
   758     // Read the common id
       
   759     iCommonId.InternalizeL( stream );
       
   760     
       
   761     // Read the key id 
       
   762     iKeyId.InternalizeL( stream );
       
   763     
       
   764     // read the count of the array:
       
   765     iArray.Reset();
       
   766     
       
   767     count = stream.ReadInt32L();
       
   768     
       
   769     for( ; count > 0; count-- )
       
   770         {
       
   771         id = stream.ReadUint32L();
       
   772         iArray.InsertInUnsignedKeyOrder( id );
       
   773         }
       
   774      
       
   775     // Close the stream
       
   776     CleanupStack::PopAndDestroy();    
       
   777     };
       
   778     
       
   779 // -----------------------------------------------------------------------------
       
   780 // CDRMRightsData::FetchCommonDataL
       
   781 // -----------------------------------------------------------------------------
       
   782 //
       
   783 void CDRMRightsData::FetchCommonDataL()
       
   784     {
       
   785     RStoreReadStream stream;
       
   786     
       
   787     // Open the root stream
       
   788     stream.OpenLC( *iStore, iCommonId );
       
   789     
       
   790     iData->InternalizeL( stream );
       
   791     
       
   792     CleanupStack::PopAndDestroy();
       
   793     };
       
   794     
       
   795 
       
   796 // -----------------------------------------------------------------------------
       
   797 // Default Constructor - First phase.
       
   798 // Can be used by itself to generate an empty object
       
   799 // -----------------------------------------------------------------------------
       
   800 //
       
   801 CDRMRightsData::CDRMRightsData( CDRMCommonData* aData, RFs& aFileServer ) :
       
   802     iFileServer( aFileServer ), iData( aData )
       
   803     {
       
   804     };
       
   805 
       
   806 // -----------------------------------------------------------------------------
       
   807 // 2nd phase Constructor
       
   808 // -----------------------------------------------------------------------------
       
   809 //
       
   810 void CDRMRightsData::ConstructL( const TFileName& aRightsStore )
       
   811     {
       
   812     // Open the file store
       
   813     iStore = CPermanentFileStore::OpenL( iFileServer, aRightsStore, EFileRead|EFileWrite );
       
   814     
       
   815     // Get the index
       
   816     FetchIndexL();
       
   817     
       
   818     // Get the common data
       
   819     FetchCommonDataL();
       
   820     
       
   821     // Get the key
       
   822     FetchKeyL();
       
   823     };
       
   824 
       
   825 // -----------------------------------------------------------------------------
       
   826 // 2nd phase Constructor
       
   827 // -----------------------------------------------------------------------------
       
   828 //
       
   829 void CDRMRightsData::ConstructL( const TFileName& aRightsStore,
       
   830                                  const TDesC8& aKey )
       
   831     {
       
   832     
       
   833     // Check if the key is given or a null desc
       
   834     if( aKey.Length() ) 
       
   835         {
       
   836         iKey = aKey;
       
   837         iKeyExists = ETrue;
       
   838         }
       
   839     else
       
   840         {
       
   841         iKeyExists = EFalse;
       
   842         }    
       
   843     
       
   844     // Creates the required stores and indexes
       
   845     CreateAndInitializeStoreL( aRightsStore );
       
   846     };
       
   847     
       
   848             
       
   849 // End of File