omadrm/drmengine/legacy/src/DRMCommon.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002, 2003 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:  Implementation of common DRM operations
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <f32file.h>
       
    22 #include <s32file.h> 
       
    23 #include <apmstd.h>
       
    24 #include <WSPDecoder.h>
       
    25 #include <WSPEncoder.h>
       
    26 
       
    27 #include <barsc.h>
       
    28 #include <barsread.h> 
       
    29 
       
    30 #include <DRMCommon.rsg>
       
    31 
       
    32 #ifdef RD_MULTIPLE_DRIVE
       
    33 #include <DriveInfo.h>
       
    34 #endif
       
    35 
       
    36 #include "DRMCommon.h"
       
    37 #include "DRMRightsClient.h"
       
    38 #include "DcfCommon.h"
       
    39 #include "Oma1Dcf.h"
       
    40 #include "Oma2Dcf.h"
       
    41 #include "DRMPermission.h"
       
    42 
       
    43 // EXTERNAL DATA STRUCTURES
       
    44 
       
    45 // EXTERNAL FUNCTION PROTOTYPES  
       
    46 
       
    47 // CONSTANTS
       
    48 
       
    49 const TInt KBufferSize = 1024;
       
    50 _LIT8(KRiUrl, "Rights-Issuer");
       
    51 
       
    52 // MACROS
       
    53 
       
    54 // LOCAL CONSTANTS AND MACROS
       
    55 
       
    56 _LIT8(KFLPrefix, "flk:");
       
    57 _LIT8(KLDPrefix, "ldf:");
       
    58 _LIT8(KHeaderNameEnding, ":");
       
    59 _LIT8(KHeaderEnding, "\r\n");
       
    60 
       
    61  #ifdef RD_MULTIPLE_DRIVE
       
    62 _LIT(KDataTypesFile, "%c:\\system\\data\\DataType.dat");
       
    63 _LIT(KResourceFile, "%c:\\resource\\drmcommon.rsc");
       
    64 _LIT(KTempFile, "%c:\\system\\temp\\DataType.tmp");
       
    65 #else
       
    66 _LIT(KDataTypesFile, "c:\\system\\data\\DataType.dat");
       
    67 _LIT(KResourceFile, "z:\\resource\\drmcommon.rsc");
       
    68 _LIT(KTempFile, "c:\\system\\temp\\DataType.tmp");
       
    69 #endif
       
    70 
       
    71 
       
    72 // Version of the client
       
    73 
       
    74 const TUint8 KClientVersionMajor = 1;
       
    75 const TUint8 KClientVersionMinor = 1;
       
    76 const TUint16 KClientVersionBuild = 2;
       
    77 
       
    78 // Which lowest server version is supported
       
    79 
       
    80 const TUint8 KServerVersionMajor = 1;
       
    81 const TUint8 KServerVersionMinor = 2;
       
    82 const TUint16 KServerVersionBuild = 1;
       
    83 
       
    84 //_LIT(KNullDate,"00000000:000000.000000");
       
    85 
       
    86 
       
    87 // MODULE DATA STRUCTURES
       
    88 
       
    89 // LOCAL FUNCTION PROTOTYPES
       
    90 
       
    91 /*
       
    92 LOCAL_C TInt CheckContentRightsL(
       
    93     const TDesC8& aContentURI, 
       
    94     TUint32 aRightsSpec);
       
    95 
       
    96 LOCAL_C TInt IsProtectedContentL(
       
    97     const TDesC8& aContent, 
       
    98     TBool& aProtection);
       
    99 
       
   100 LOCAL_C TInt GetNewHeaderBuffer(
       
   101     HBufC8*& aOldHeaderBuf, 
       
   102     const TDesC8& aHeaderName, 
       
   103     const TDesC8& aHeaderValue, 
       
   104     HBufC8*& aNewHeaderBuf
       
   105     );
       
   106 
       
   107 LOCAL_C TInt WriteNewFile(
       
   108     TFileName aFileName, 
       
   109     HBufC8*& aHeaderBuf, 
       
   110     TUint32& aFirstPartLength, 
       
   111     TUint32& aDataLength, 
       
   112     TUint32& aDataPartPos
       
   113     );
       
   114 
       
   115 LOCAL_C TInt GetContentURIListL(
       
   116     RPointerArray<HBufC8>*& aURIList);
       
   117 
       
   118 LOCAL_C TInt DataTypesCountL(
       
   119     TInt& aCount);
       
   120 
       
   121 LOCAL_C TInt SupportedDataTypeL(
       
   122     const TInt aIndex, TDataType& aDataType);
       
   123 
       
   124 LOCAL_C TInt StaticDataTypesCountL(
       
   125     TInt& aCount);
       
   126 
       
   127 LOCAL_C TInt RegisterDataTypeL(
       
   128     const TDataType& aDataType);
       
   129 
       
   130 LOCAL_C TInt UnRegisterDataTypeL(
       
   131     const TInt aIndex);
       
   132 
       
   133 */
       
   134 
       
   135 LOCAL_C void GetActiveRightsL(
       
   136     const TDesC8& aContentURI, 
       
   137     TUint32 aRightsSpec, 
       
   138     CDRMRights*& aRightsObject);
       
   139 
       
   140 LOCAL_C TInt CheckFileRightsL(
       
   141     const TDesC& aFileName, 
       
   142     TUint32 aRightsSpec);
       
   143 
       
   144 LOCAL_C TInt CheckFileRightsL(
       
   145     RFile& aFileHandle, 
       
   146     TUint32 aRightsSpec);
       
   147 
       
   148 LOCAL_C TInt IsProtectedFileL(
       
   149     const TDesC& aFileName, 
       
   150     TBool& aProtection);
       
   151 
       
   152 LOCAL_C TInt IsProtectedFileL(
       
   153     RFile& aFileHandle, 
       
   154     TBool& aProtection);
       
   155 
       
   156 LOCAL_C TInt GetContentInfoL(
       
   157     const TDesC8& aContent, 
       
   158     DRMCommon::TContentProtection& aProtection, 
       
   159     HBufC8*& aMIMEType, 
       
   160     HBufC8*& aContentURI, 
       
   161     TUint& aDataLength);
       
   162 
       
   163 LOCAL_C TInt GetFileInfoL(
       
   164     const TDesC& aFileName, 
       
   165     DRMCommon::TContentProtection& aProtection, 
       
   166     HBufC8*& aMIMEType, 
       
   167     HBufC8*& aContentURI, 
       
   168     TUint& aDataLength);
       
   169 
       
   170 LOCAL_C TInt GetFileInfoL(
       
   171     RFile& aFileHandle, 
       
   172     DRMCommon::TContentProtection& aProtection, 
       
   173     HBufC8*& aMIMEType, 
       
   174     HBufC8*& aContentURI, 
       
   175     TUint& aDataLength);
       
   176 
       
   177 LOCAL_C void GetContentHeaderL(
       
   178     const TDesC8& aContent, 
       
   179     const TDesC8& aHeaderName, 
       
   180     HBufC8*& aHeaderValue);
       
   181 
       
   182 LOCAL_C void GetFileHeaderL(
       
   183     const TFileName& aFileName, 
       
   184     const TDesC8& aHeaderName, 
       
   185     HBufC8*& aHeaderValue);
       
   186 
       
   187 LOCAL_C void GetFileHeaderL(
       
   188     RFile& aFileHandle, 
       
   189     const TDesC8& aHeaderName, 
       
   190     HBufC8*& aHeaderValue);
       
   191 
       
   192 LOCAL_C void SetContentHeaderL(
       
   193     HBufC8*& aContent, 
       
   194     const TDesC8& aHeaderName, 
       
   195     const TDesC8& aHeaderValue);
       
   196 
       
   197 LOCAL_C void SetFileHeaderL(
       
   198     const TDesC16& aFileName, 
       
   199     const TDesC8& aHeaderName, 
       
   200     const TDesC8& aHeaderValue);
       
   201 
       
   202 LOCAL_C void SetFileHeaderL(
       
   203     RFile& aFileHandle, 
       
   204     const TDesC8& aHeaderName, 
       
   205     const TDesC8& aHeaderValue);
       
   206 
       
   207 LOCAL_C void GetSingleRightsObjectL(
       
   208     const TDesC8& aContentURI, 
       
   209     TUint32 aLocalID, 
       
   210     CDRMRights*& aRightsObject);
       
   211 
       
   212 LOCAL_C void GetDetailedContentRightsL(
       
   213     const TDesC8& aContentURI, 
       
   214     RPointerArray<CDRMRights>*& aRightsList);
       
   215 
       
   216 LOCAL_C void GetDetailedFileRightsL(
       
   217     const TDesC& aFileName, 
       
   218     RPointerArray<CDRMRights>*& aRightsList);
       
   219 
       
   220 LOCAL_C void GetDetailedFileRightsL(
       
   221     RFile& aFileHandle, 
       
   222     RPointerArray<CDRMRights>*& aRightsList);
       
   223 
       
   224 LOCAL_C TInt RegisterDynamicDataTypeL(
       
   225     const TDataType& aDataType);
       
   226 
       
   227 LOCAL_C TInt UnRegisterDynamicDataTypeL(
       
   228     const TInt aIndex);
       
   229 
       
   230 LOCAL_C TInt CalculatePaddingL(
       
   231     COma1Dcf* dcf);
       
   232 
       
   233 LOCAL_C void DoResetAndDestroy( TAny* aPtr );  
       
   234 
       
   235 
       
   236 LOCAL_C TInt GetFileHandleRead(
       
   237     RFs& aFileServer, 
       
   238     RFile& aFile, 
       
   239     const TDesC& aFileName );
       
   240 
       
   241 LOCAL_C void AddParents( 
       
   242     DRMCommon* aDrmCommon, 
       
   243     RPointerArray<CDRMRights>& aRights );
       
   244 	
       
   245 // FORWARD DECLARATIONS
       
   246 
       
   247 using namespace ContentAccess;
       
   248 
       
   249 // ============================= LOCAL FUNCTIONS ===============================
       
   250 
       
   251 // ----------------------------------------------------------------------------
       
   252 // DoResetAndDestroy
       
   253 // Does RPointerArray< >->ResetAndDestroy() for the given array aPtr.
       
   254 // ----------------------------------------------------------------------------
       
   255 //
       
   256 LOCAL_C void DoResetAndDestroy( TAny* aPtr )
       
   257     {
       
   258     ( reinterpret_cast< RPointerArray< CDRMPermission >* >( aPtr ) )->
       
   259         ResetAndDestroy();
       
   260     }
       
   261     
       
   262     
       
   263 // -----------------------------------------------------------------------------
       
   264 // GetPermission
       
   265 // -----------------------------------------------------------------------------
       
   266 LOCAL_C void GetPermission(
       
   267     RDRMRightsClient& aClient, 
       
   268     const TDesC8& aUri,
       
   269     TIntent aIntent,
       
   270     CDRMPermission*& aPermission )
       
   271     {
       
   272     TInt r = KErrNone;
       
   273     CDRMPermission* permission = NULL;
       
   274     TUint32 reason = 0;
       
   275     
       
   276     TRAP( r, permission = aClient.GetActiveRightsL( aIntent, aUri, reason ) );
       
   277     if ( permission != NULL )
       
   278         {
       
   279         if ( aPermission == NULL )
       
   280             {
       
   281             TRAP_IGNORE( aPermission = CDRMPermission::NewL() ); 
       
   282             }
       
   283         aPermission->Merge( *permission );
       
   284         delete permission;
       
   285         }
       
   286     }
       
   287     
       
   288 // -----------------------------------------------------------------------------
       
   289 // GetActiveRightsL
       
   290 // Returns a possible active rights object. Adopts to broken callers who still
       
   291 // use a bitmask for intent.
       
   292 // -----------------------------------------------------------------------------
       
   293 void GetActiveRightsL(
       
   294     const TDesC8& aContentURI,
       
   295     TUint32 aConstraints,
       
   296     CDRMRights*& aRightsObject)
       
   297     {
       
   298     CDRMPermission* permission = NULL;
       
   299     RDRMRightsClient client;
       
   300     HBufC8* uri = NULL;
       
   301         
       
   302     User::LeaveIfError( client.Connect() );
       
   303     CleanupClosePushL( client );
       
   304 
       
   305     if ( aConstraints == 0 )
       
   306         {
       
   307         GetPermission( client, aContentURI, EView, permission );
       
   308         GetPermission( client, aContentURI, EPlay, permission );
       
   309         GetPermission( client, aContentURI, EExecute, permission );
       
   310         GetPermission( client, aContentURI, EPrint, permission );
       
   311         }
       
   312     else
       
   313         {
       
   314         GetPermission( client, aContentURI, static_cast<TIntent>( aConstraints ), permission );
       
   315         }
       
   316 
       
   317     if ( permission != NULL )
       
   318         {
       
   319         CleanupStack::PushL( permission );
       
   320         aRightsObject = CDRMRights::NewL();
       
   321         CleanupStack::PushL( aRightsObject );
       
   322         uri = aContentURI.AllocL();
       
   323         CleanupStack::PushL( uri );
       
   324         aRightsObject->SetPermissionL( *permission );
       
   325         aRightsObject->SetContentURI( uri );
       
   326         CleanupStack::Pop( uri );
       
   327         CleanupStack::Pop( aRightsObject );
       
   328         CleanupStack::PopAndDestroy( permission );
       
   329         }
       
   330     else
       
   331         {
       
   332         aRightsObject = NULL;
       
   333         User::Leave( KErrCANoRights );
       
   334         }
       
   335     CleanupStack::PopAndDestroy(); // client
       
   336     }
       
   337 
       
   338 // -----------------------------------------------------------------------------
       
   339 // 
       
   340 //
       
   341 // Parameters:
       
   342 //
       
   343 // Returns:
       
   344 // -----------------------------------------------------------------------------
       
   345 LOCAL_C TInt IsProtectedFileL(
       
   346     const TDesC& aFileName, 
       
   347     TBool& aProtection)
       
   348     {
       
   349     RFs fs;
       
   350     RFile file;
       
   351     TInt r = KErrNone;
       
   352     CDcfCommon* dcf = NULL;
       
   353 
       
   354     User::LeaveIfError(fs.Connect());
       
   355     CleanupClosePushL(fs);
       
   356     
       
   357     
       
   358     r = GetFileHandleRead( fs, file, aFileName );
       
   359     User::LeaveIfError(r);
       
   360     
       
   361     CleanupClosePushL(file);
       
   362     dcf = CDcfCommon::NewL(file);
       
   363     if (dcf != NULL)
       
   364         {
       
   365         aProtection = ETrue;
       
   366         delete dcf;
       
   367         }
       
   368     else
       
   369         {
       
   370         aProtection = EFalse;
       
   371         }
       
   372     CleanupStack::PopAndDestroy(2); // file, fs
       
   373     return r;
       
   374     }
       
   375 
       
   376 // -----------------------------------------------------------------------------
       
   377 // 
       
   378 //
       
   379 // Parameters:
       
   380 //
       
   381 // Returns:
       
   382 // -----------------------------------------------------------------------------
       
   383 LOCAL_C TInt IsProtectedFileL(
       
   384     RFile& aFileHandle, 
       
   385     TBool& aProtection)
       
   386     {
       
   387     TInt r = KErrNone;
       
   388     CDcfCommon* dcf = NULL;
       
   389 
       
   390     dcf = CDcfCommon::NewL(aFileHandle);
       
   391     if (dcf != NULL)
       
   392         {
       
   393         aProtection = ETrue;
       
   394         delete dcf;
       
   395         }
       
   396     else
       
   397         {
       
   398         aProtection = EFalse;
       
   399         }
       
   400     return r;
       
   401     }
       
   402 
       
   403 // -----------------------------------------------------------------------------
       
   404 // 
       
   405 //
       
   406 // Parameters:
       
   407 //
       
   408 // Returns:
       
   409 // -----------------------------------------------------------------------------
       
   410 TInt GetContentInfoL(
       
   411     const TDesC8& aContent, 
       
   412     DRMCommon::TContentProtection& aProtection, 
       
   413     HBufC8*& aMIMEType, 
       
   414     HBufC8*& aContentURI, 
       
   415     TUint& aDataLength)
       
   416     {
       
   417     TInt r = KErrNone;
       
   418     COma1Dcf* dcf = NULL;
       
   419     TRAP(r, dcf = COma1Dcf::NewL(aContent));
       
   420     if (dcf != NULL)
       
   421         {
       
   422         CleanupStack::PushL( dcf );
       
   423             
       
   424         if (dcf->iContentID->Left(4).Compare(KLDPrefix) == 0 ||
       
   425             dcf->iContentID->Left(4).Compare(KFLPrefix) == 0 ||
       
   426             dcf->iRightsIssuerURL == NULL)
       
   427             {
       
   428             aProtection = DRMCommon::EForwardLocked;
       
   429             }
       
   430         else
       
   431             {
       
   432             aProtection = DRMCommon::ESuperDistributable;
       
   433             }
       
   434         if (dcf->iContentID != NULL)
       
   435             {
       
   436             aContentURI = dcf->iContentID->AllocL();
       
   437             }
       
   438         else
       
   439             {
       
   440             aContentURI = NULL;
       
   441             }
       
   442         if (dcf->iMimeType != NULL)
       
   443             {
       
   444             aMIMEType = dcf->iMimeType->AllocL();
       
   445             }
       
   446         else
       
   447             {
       
   448             aMIMEType = NULL;
       
   449             }
       
   450 
       
   451         CalculatePaddingL(dcf);
       
   452         aDataLength = dcf->iPlainTextLength;
       
   453         CleanupStack::PopAndDestroy();
       
   454         }
       
   455     else
       
   456         {
       
   457         aProtection = DRMCommon::ENoDCFFile;
       
   458         if (r == KErrArgument)
       
   459             {
       
   460             r = KErrNone;
       
   461             }
       
   462         User::LeaveIfError(r);
       
   463         }
       
   464 
       
   465     return r;
       
   466     }
       
   467 
       
   468 // -----------------------------------------------------------------------------
       
   469 // 
       
   470 //
       
   471 // Parameters:
       
   472 //
       
   473 // Returns:
       
   474 // -----------------------------------------------------------------------------
       
   475 TInt GetFileInfoL(
       
   476     const TDesC& aFileName, 
       
   477     DRMCommon::TContentProtection& aProtection, 
       
   478     HBufC8*& aMIMEType, 
       
   479     HBufC8*& aContentURI, 
       
   480     TUint& aDataLength)
       
   481     {
       
   482     RFs fs;
       
   483     RFile file;
       
   484     TInt r = KErrNone;
       
   485     CDcfCommon* dcf = NULL;
       
   486     COma2Dcf* dcf2 = NULL;
       
   487     CData* data = NULL;
       
   488 
       
   489     aProtection = DRMCommon::ENoDCFFile;
       
   490     User::LeaveIfError(fs.Connect());
       
   491     CleanupClosePushL(fs);
       
   492     
       
   493     r = GetFileHandleRead( fs, file, aFileName );
       
   494     User::LeaveIfError(r);
       
   495     CleanupClosePushL(file);
       
   496     
       
   497     dcf = CDcfCommon::NewL(file);
       
   498     if (dcf != NULL)
       
   499         {
       
   500         CleanupStack::PushL(dcf);
       
   501         if (dcf->iVersion == EOma2Dcf)
       
   502             {
       
   503             dcf2 = static_cast<COma2Dcf*>(dcf);
       
   504             }
       
   505             
       
   506         if (dcf->iContentID->Left(4).Compare(KLDPrefix) == 0 ||
       
   507             dcf->iContentID->Left(4).Compare(KFLPrefix) == 0 ||
       
   508             dcf->iRightsIssuerURL == NULL)
       
   509             {
       
   510             aProtection = DRMCommon::EForwardLocked;
       
   511             }
       
   512         else
       
   513             {
       
   514             aProtection = DRMCommon::ESuperDistributable;
       
   515             }
       
   516         if (dcf->iContentID != NULL)
       
   517             {
       
   518             aContentURI = dcf->iContentID->AllocL();
       
   519             }
       
   520         else
       
   521             {
       
   522             aContentURI = NULL;
       
   523             }
       
   524         if (dcf->iMimeType != NULL)
       
   525             {
       
   526             aMIMEType = dcf->iMimeType->AllocL();
       
   527             }
       
   528         else
       
   529             {
       
   530             aMIMEType = NULL;
       
   531             }
       
   532         
       
   533         // Insert domain RO if it exists
       
   534         if (dcf2 != NULL && dcf2->iRightsObjects.Count() > 0)
       
   535             {
       
   536             // Creating a CData object will insert the domain RO into the RDB
       
   537             data = CData::NewL(file, KDefaultContentObject);
       
   538             delete data;
       
   539             }
       
   540             
       
   541         if (dcf->iVersion == EOma1Dcf && !dcf->iPlainTextLengthValid)
       
   542             {
       
   543             CalculatePaddingL(static_cast<COma1Dcf*>(dcf));
       
   544             }
       
   545 
       
   546         aDataLength = dcf->iPlainTextLength;
       
   547         CleanupStack::PopAndDestroy(); // dcf
       
   548         }
       
   549     CleanupStack::PopAndDestroy(2); // file, fs
       
   550     return r;
       
   551     }
       
   552 
       
   553 // -----------------------------------------------------------------------------
       
   554 // 
       
   555 //
       
   556 // Parameters:
       
   557 //
       
   558 // Returns:
       
   559 // -----------------------------------------------------------------------------
       
   560 TInt GetFileInfoL(
       
   561     RFile& aFileHandle, 
       
   562     DRMCommon::TContentProtection& aProtection, 
       
   563     HBufC8*& aMIMEType, 
       
   564     HBufC8*& aContentURI, 
       
   565     TUint& aDataLength)
       
   566     {
       
   567     TInt r = KErrNone;
       
   568     CDcfCommon* dcf = NULL;
       
   569     COma2Dcf* dcf2 = NULL;
       
   570     CData* data = NULL;
       
   571     TInt initialPos = 0;
       
   572 
       
   573     // Store the initial filePos
       
   574     User::LeaveIfError(aFileHandle.Seek(ESeekCurrent, initialPos)); 
       
   575 
       
   576     aProtection = DRMCommon::ENoDCFFile;
       
   577 
       
   578     dcf = CDcfCommon::NewL(aFileHandle);
       
   579     if (dcf != NULL)
       
   580         {
       
   581         CleanupStack::PushL(dcf);
       
   582         if (dcf->iVersion == EOma2Dcf)
       
   583             {
       
   584             dcf2 = static_cast<COma2Dcf*>(dcf);
       
   585             }
       
   586 
       
   587         if (dcf->iContentID->Left(4).Compare(KLDPrefix) == 0 ||
       
   588             dcf->iContentID->Left(4).Compare(KFLPrefix) == 0 ||
       
   589             dcf->iRightsIssuerURL == NULL)
       
   590             {
       
   591             aProtection = DRMCommon::EForwardLocked;
       
   592             }
       
   593         else
       
   594             {
       
   595             aProtection = DRMCommon::ESuperDistributable;
       
   596             }
       
   597         if (dcf->iContentID != NULL)
       
   598             {
       
   599             aContentURI = dcf->iContentID->AllocL();
       
   600             }
       
   601         else
       
   602             {
       
   603             aContentURI = NULL;
       
   604             }
       
   605         if (dcf->iMimeType != NULL)
       
   606             {
       
   607             aMIMEType = dcf->iMimeType->AllocL();
       
   608             }
       
   609         else
       
   610             {
       
   611             aMIMEType = NULL;
       
   612             }
       
   613 
       
   614         // Insert domain RO if it exists
       
   615         if (dcf2 != NULL && dcf2->iRightsObjects.Count() > 0)
       
   616             {
       
   617             // Creating a CData object will insert the domain RO into the RDB
       
   618             data = CData::NewL(aFileHandle, KDefaultContentObject);
       
   619             delete data;
       
   620             }
       
   621             
       
   622         if (dcf->iVersion == EOma1Dcf && !dcf->iPlainTextLengthValid)
       
   623             {
       
   624             CalculatePaddingL(static_cast<COma1Dcf*>(dcf));
       
   625             }
       
   626 
       
   627         aDataLength = dcf->iPlainTextLength;
       
   628         CleanupStack::PopAndDestroy(); // dcf
       
   629         }
       
   630     // Restore filePos
       
   631     User::LeaveIfError(aFileHandle.Seek(ESeekStart, initialPos));
       
   632     return r;
       
   633     }
       
   634 
       
   635 // -----------------------------------------------------------------------------
       
   636 // 
       
   637 //
       
   638 // Parameters:
       
   639 //
       
   640 // Returns:
       
   641 // -----------------------------------------------------------------------------
       
   642 TInt CheckFileRightsL(
       
   643     const TDesC& aFileName, 
       
   644     TUint32 aRightsSpec)
       
   645     {
       
   646     RFs fs;
       
   647     RFile file;
       
   648     CDcfCommon* dcf = NULL;
       
   649     TInt r = DRMCommon::ENoRights;
       
   650     RDRMRightsClient client;
       
   651     TUint32 reason = 0;
       
   652     
       
   653     User::LeaveIfError(fs.Connect());
       
   654     CleanupClosePushL(fs);
       
   655     
       
   656     r = GetFileHandleRead(fs, file, aFileName);
       
   657     User::LeaveIfError(r);
       
   658     CleanupClosePushL(file);
       
   659     
       
   660     dcf = CDcfCommon::NewL(file);
       
   661     if ( dcf == NULL )
       
   662         {
       
   663         User::Leave(KErrArgument);
       
   664         }
       
   665     CleanupStack::PushL(dcf);
       
   666     User::LeaveIfError(client.Connect());
       
   667     CleanupClosePushL(client);
       
   668     r = client.CheckRights(aRightsSpec, *dcf->iContentID, reason);
       
   669     CleanupStack::PopAndDestroy(4); // dcf, file, client, fs
       
   670     return r;
       
   671     }
       
   672 
       
   673 // -----------------------------------------------------------------------------
       
   674 // 
       
   675 //
       
   676 // Parameters:
       
   677 //
       
   678 // Returns:
       
   679 // -----------------------------------------------------------------------------
       
   680 TInt CheckFileRightsL(
       
   681     RFile& aFileHandle, 
       
   682     TUint32 aRightsSpec)
       
   683     {
       
   684     CDcfCommon* dcf = NULL;
       
   685     TInt r = DRMCommon::ENoRights;
       
   686     RDRMRightsClient client;
       
   687     TUint32 reason = 0;
       
   688     
       
   689     dcf = CDcfCommon::NewL(aFileHandle);
       
   690     if ( dcf == NULL )
       
   691         {
       
   692         User::Leave(KErrArgument);
       
   693         }
       
   694     CleanupStack::PushL(dcf);
       
   695     User::LeaveIfError(client.Connect());
       
   696     CleanupClosePushL(client);
       
   697     r = client.CheckRights(aRightsSpec, *dcf->iContentID, reason);
       
   698     CleanupStack::PopAndDestroy(2); // dcf, file
       
   699     return r;
       
   700     }
       
   701 
       
   702 // -----------------------------------------------------------------------------
       
   703 // 
       
   704 //
       
   705 // Parameters:
       
   706 //
       
   707 // Returns:
       
   708 // -----------------------------------------------------------------------------
       
   709 void GetSingleRightsObjectL(
       
   710     const TDesC8& aContentURI, 
       
   711     TUint32 aLocalID, 
       
   712     CDRMRights*& aRightsObject)
       
   713     {
       
   714     RDRMRightsClient client;
       
   715     CDRMPermission* p = NULL;
       
   716     CDRMAsset* a = NULL;
       
   717     
       
   718     aRightsObject = NULL;
       
   719     User::LeaveIfError(client.Connect());
       
   720     CleanupClosePushL(client);
       
   721     
       
   722     aRightsObject = CDRMRights::NewL(); 
       
   723     CleanupStack::PushL(aRightsObject);
       
   724     
       
   725     p = client.GetDbEntryL(aContentURI, aLocalID);
       
   726     CleanupStack::PushL(p);
       
   727     aRightsObject->SetPermissionL(*p);
       
   728     CleanupStack::PopAndDestroy(); // p
       
   729 
       
   730     a = CDRMAsset::NewLC();
       
   731     aRightsObject->SetAssetL(*a);
       
   732     CleanupStack::PopAndDestroy();
       
   733     
       
   734     aRightsObject->SetContentURIAndLocalID(aContentURI.AllocL(), aLocalID);
       
   735     CleanupStack::Pop(); // aRightsObject
       
   736     CleanupStack::PopAndDestroy(); // client
       
   737     }
       
   738 
       
   739 // -----------------------------------------------------------------------------
       
   740 // 
       
   741 //
       
   742 // Parameters:
       
   743 //
       
   744 // Returns:
       
   745 // -----------------------------------------------------------------------------
       
   746 void GetDetailedContentRightsL(
       
   747     const TDesC8& aContentURI, 
       
   748     RPointerArray<CDRMRights>*& aRightsList)
       
   749     {
       
   750     RDRMRightsClient client;
       
   751     RPointerArray<CDRMPermission> rights;
       
   752     CDRMRights* ro = NULL;
       
   753     CDRMAsset* a  = NULL;
       
   754     TInt i;
       
   755     // Need a temporary pointer
       
   756     HBufC8* contentId = NULL;
       
   757     
       
   758     aRightsList = NULL;
       
   759     User::LeaveIfError(client.Connect());
       
   760     CleanupClosePushL(client);
       
   761 
       
   762     client.GetDBEntriesL(aContentURI, rights);
       
   763 
       
   764     TCleanupItem cleanup( DoResetAndDestroy, &rights );
       
   765     CleanupStack::PushL( cleanup );
       
   766 
       
   767     aRightsList = new(ELeave) RPointerArray<CDRMRights>(rights.Count());
       
   768 
       
   769     // Push the actual pointer reserved with NewL in
       
   770     CleanupStack::PushL( aRightsList );
       
   771 
       
   772     // Push the cleanup of the items as well into the cleanup stack
       
   773     TCleanupItem cleanup2( DoResetAndDestroy, aRightsList );
       
   774     CleanupStack::PushL( cleanup2 );
       
   775 
       
   776     for (i = 0; i < rights.Count(); i++)
       
   777         {
       
   778         // Needs to be pushed to the cleanup stack
       
   779         ro = CDRMRights::NewL();
       
   780         CleanupStack::PushL(ro);
       
   781 
       
   782         // Push to cleanp stack
       
   783         a = CDRMAsset::NewLC();
       
   784 
       
   785         // Set asset
       
   786         ro->SetAssetL(*a);
       
   787 
       
   788         // Destroy asset
       
   789         CleanupStack::PopAndDestroy(); // Asset
       
   790 
       
   791         // Set the permission, copies the object
       
   792         ro->SetPermissionL(*rights[i]);
       
   793 
       
   794         // take a copy of the content id
       
   795         contentId = aContentURI.AllocL();
       
   796 
       
   797         // set content uri and local id
       
   798         ro->SetContentURIAndLocalID(contentId, rights[i]->iUniqueID);
       
   799 
       
   800         // Append the ro to the rights list
       
   801         aRightsList->AppendL(ro);
       
   802 
       
   803         // Take the ro out of the cleanup stack
       
   804         CleanupStack::Pop(ro);
       
   805         }
       
   806 
       
   807     CleanupStack::Pop(2); // aRightsList ( two items )
       
   808     CleanupStack::PopAndDestroy(2); // rights, client
       
   809     }
       
   810 
       
   811 // -----------------------------------------------------------------------------
       
   812 // 
       
   813 //
       
   814 // Parameters:
       
   815 //
       
   816 // Returns:
       
   817 // -----------------------------------------------------------------------------
       
   818 void GetDetailedFileRightsL(
       
   819     const TDesC& aFileName, 
       
   820     RPointerArray<CDRMRights>*& aRightsList)
       
   821     {
       
   822     
       
   823     CDcfCommon* dcf = NULL;
       
   824     RFile file;
       
   825     RFs fs;
       
   826     TInt r = KErrNone;
       
   827     
       
   828     User::LeaveIfError(fs.Connect());
       
   829     CleanupClosePushL(fs);
       
   830 
       
   831     r = GetFileHandleRead( fs, file, aFileName );
       
   832     User::LeaveIfError(r);
       
   833     CleanupClosePushL(file);
       
   834     
       
   835     
       
   836     dcf = CDcfCommon::NewL(file);
       
   837     if (dcf == NULL)
       
   838         {
       
   839         User::Leave(KErrArgument);
       
   840         }
       
   841     CleanupStack::PushL(dcf);
       
   842     GetDetailedContentRightsL(*dcf->iContentID, aRightsList);
       
   843     CleanupStack::PopAndDestroy(3); // dcf, file, client
       
   844    
       
   845     }
       
   846 
       
   847 // -----------------------------------------------------------------------------
       
   848 // 
       
   849 //
       
   850 // Parameters:
       
   851 //
       
   852 // Returns:
       
   853 // -----------------------------------------------------------------------------
       
   854 void GetDetailedFileRightsL(
       
   855     RFile& aFileHandle, 
       
   856     RPointerArray<CDRMRights>*& aRightsList)
       
   857     {
       
   858     CDcfCommon* dcf = NULL;
       
   859     
       
   860     dcf = CDcfCommon::NewL(aFileHandle);
       
   861     if (dcf == NULL)
       
   862         {
       
   863         User::Leave(KErrArgument);
       
   864         }
       
   865     CleanupStack::PushL(dcf);
       
   866     GetDetailedContentRightsL(*dcf->iContentID, aRightsList);
       
   867     CleanupStack::PopAndDestroy(); // dcf
       
   868     }
       
   869 
       
   870 // -----------------------------------------------------------------------------
       
   871 // 
       
   872 //
       
   873 // Parameters:
       
   874 //
       
   875 // Returns:
       
   876 // -----------------------------------------------------------------------------
       
   877 LOCAL_C void  GetNewHeaderBufferL(
       
   878     HBufC8*& aOldHeaderBuf, 
       
   879     const TDesC8& aHeaderName, 
       
   880     const TDesC8& aHeaderValue, 
       
   881     HBufC8*& aNewHeaderBuf
       
   882     )
       
   883     {
       
   884     TInt oldHeaderValueLength = 0;
       
   885     TInt offset = 0;
       
   886     TPtr8 headerPtr(aOldHeaderBuf->Des());
       
   887     TPtr8 headerNamePtr(NULL, 0, 0);
       
   888     TPtr8 newHeaderPtr(NULL, 0, 0);
       
   889     
       
   890     HBufC8* headerName = HBufC8::NewL(aHeaderName.Length() + 1);
       
   891     // Find the position of the text in the header
       
   892     headerNamePtr.Set(headerName->Des());
       
   893     headerNamePtr.Copy(aHeaderName);
       
   894     headerNamePtr.Append(KHeaderNameEnding);
       
   895     offset = headerPtr.FindF(headerNamePtr);
       
   896     delete headerName;
       
   897     headerName = NULL;
       
   898     if (offset != KErrNotFound)
       
   899         {
       
   900         // All fields end with CRLF, search for the end of the field and if it is not found
       
   901         // return KErrArgument
       
   902         oldHeaderValueLength = headerPtr.Right(headerPtr.Length() -
       
   903             (offset + aHeaderName.Length() + 1)).Find(KHeaderEnding);
       
   904         User::LeaveIfError(oldHeaderValueLength);
       
   905         // reserve new buffer for the header, might be useless if the lengths are the same
       
   906         aNewHeaderBuf = HBufC8::NewL(headerPtr.Length() - oldHeaderValueLength +
       
   907             aHeaderValue.Length());
       
   908         newHeaderPtr.Set(aNewHeaderBuf->Des());
       
   909         newHeaderPtr.Copy(headerPtr.Left(offset + aHeaderName.Length() + 1));
       
   910         newHeaderPtr.Append(aHeaderValue);
       
   911         newHeaderPtr.Append(headerPtr.Right(headerPtr.Length() -
       
   912             (offset + aHeaderName.Length() + 1 + oldHeaderValueLength)));                        
       
   913         }
       
   914     else
       
   915         {
       
   916         aNewHeaderBuf = HBufC8::NewL(headerPtr.Length() + aHeaderName.Length() +
       
   917             aHeaderValue.Length() + 3);
       
   918         newHeaderPtr.Set(aNewHeaderBuf->Des());
       
   919         newHeaderPtr.Copy(headerPtr);
       
   920         newHeaderPtr.Append(aHeaderName);
       
   921         newHeaderPtr.Append(KHeaderNameEnding);
       
   922         newHeaderPtr.Append(aHeaderValue);
       
   923         newHeaderPtr.Append(KHeaderEnding);
       
   924         }        
       
   925     }
       
   926 
       
   927 // -----------------------------------------------------------------------------
       
   928 // 
       
   929 //
       
   930 // Parameters:
       
   931 //
       
   932 // Returns:
       
   933 // -----------------------------------------------------------------------------
       
   934 LOCAL_C void WriteNewFileL(
       
   935     RFs& aFs,
       
   936     RFile& aOriginalFile,
       
   937     const TDesC& aOriginalFileName,
       
   938     COma1Dcf& aDcf,
       
   939     TFileName& aTempFileName )
       
   940     {
       
   941     TPtrC tempFilePath;
       
   942     HBufC8* buffer = NULL;
       
   943     RFileWriteStream temp;
       
   944     TPtr8 ptr(NULL, 0, 0);
       
   945     TInt offset = 0;
       
   946     
       
   947     // Find the path of the file to be modified and put the tempfile
       
   948     // into that directory
       
   949     tempFilePath.Set(aOriginalFileName.Left(
       
   950         aOriginalFileName.LocateReverse('\\') + 1));
       
   951     User::LeaveIfError(temp.Temp(aFs, tempFilePath, aTempFileName, EFileWrite));
       
   952     CleanupClosePushL( temp );
       
   953     
       
   954     temp.WriteUint8L(1);
       
   955     temp.WriteUint8L(aDcf.iMimeType->Length());
       
   956     temp.WriteUint8L(aDcf.iContentID->Length());
       
   957     temp.WriteL(*aDcf.iMimeType);
       
   958     temp.WriteL(*aDcf.iContentID);
       
   959     buffer = TWspPrimitiveEncoder::UintVarL(aDcf.iHeaders->Length());
       
   960     CleanupStack::PushL(buffer);
       
   961     temp.WriteL(*buffer);
       
   962     CleanupStack::PopAndDestroy(buffer);
       
   963     buffer = TWspPrimitiveEncoder::UintVarL(aDcf.iDataLength);
       
   964     CleanupStack::PushL(buffer);
       
   965     temp.WriteL(*buffer);
       
   966     CleanupStack::PopAndDestroy(buffer);
       
   967     temp.WriteL(*aDcf.iHeaders);
       
   968 
       
   969     offset = aDcf.iOffset;
       
   970     User::LeaveIfError(aOriginalFile.Seek(ESeekStart, offset));
       
   971     buffer = HBufC8::NewLC(KBufferSize);
       
   972     do
       
   973         {
       
   974         ptr.Set(buffer->Des());
       
   975         ptr.SetLength(0);
       
   976         aOriginalFile.Read(ptr);         
       
   977         if (ptr.Length() > 0)
       
   978             {
       
   979             temp.WriteL(ptr);
       
   980             }
       
   981         }
       
   982     while (ptr.Length() > 0);
       
   983     temp.CommitL();           
       
   984     CleanupStack::PopAndDestroy(); // temp
       
   985     CleanupStack::PopAndDestroy(); //buffer
       
   986     }
       
   987 
       
   988 // -----------------------------------------------------------------------------
       
   989 // 
       
   990 //
       
   991 // Parameters:
       
   992 //
       
   993 // Returns:
       
   994 // -----------------------------------------------------------------------------
       
   995 LOCAL_C void WriteNewFileL(
       
   996     RFs& aFs,
       
   997     RFile& aOriginalFile,
       
   998     COma1Dcf& aDcf)
       
   999     {
       
  1000     TPtrC tempFilePath;
       
  1001     HBufC8* buffer = NULL;
       
  1002     RFile tempFileHandle;
       
  1003     RFileWriteStream temp;
       
  1004     RFileWriteStream orig;
       
  1005     TFileName tempFile;
       
  1006     TPtr8 ptr(NULL, 0, 0);
       
  1007     TInt offset = 0;
       
  1008     TInt fileSize = 0;
       
  1009     
       
  1010     // Find the path of the file to be modified and put the tempfile
       
  1011     // into that directory
       
  1012     tempFilePath.Set(_L("C:\\"));
       
  1013     User::LeaveIfError( temp.Temp( aFs, tempFilePath, tempFile, 
       
  1014                         EFileWrite));
       
  1015     CleanupClosePushL( temp );
       
  1016     
       
  1017     temp.WriteUint8L(1);
       
  1018     temp.WriteUint8L(aDcf.iMimeType->Length());
       
  1019     temp.WriteUint8L(aDcf.iContentID->Length());
       
  1020     temp.WriteL(*aDcf.iMimeType);
       
  1021     temp.WriteL(*aDcf.iContentID);
       
  1022     buffer = TWspPrimitiveEncoder::UintVarL(aDcf.iHeaders->Length());
       
  1023     CleanupStack::PushL(buffer);
       
  1024     temp.WriteL(*buffer);
       
  1025     CleanupStack::PopAndDestroy(buffer);
       
  1026     buffer = TWspPrimitiveEncoder::UintVarL(aDcf.iDataLength);
       
  1027     CleanupStack::PushL(buffer);
       
  1028     temp.WriteL(*buffer);
       
  1029     CleanupStack::PopAndDestroy(buffer);
       
  1030     temp.WriteL(*aDcf.iHeaders);
       
  1031 
       
  1032     offset = aDcf.iOffset;
       
  1033     User::LeaveIfError(aOriginalFile.Seek(ESeekStart, offset));
       
  1034     buffer = HBufC8::NewLC(KBufferSize);
       
  1035     do
       
  1036         {
       
  1037         ptr.Set(buffer->Des());
       
  1038         ptr.SetLength(0);
       
  1039         aOriginalFile.Read(ptr);         
       
  1040         if (ptr.Length() > 0)
       
  1041             {
       
  1042             temp.WriteL(ptr);
       
  1043             }
       
  1044         }
       
  1045     while (ptr.Length() > 0);
       
  1046     temp.CommitL();
       
  1047     
       
  1048     CleanupStack::PopAndDestroy(2); //buffer, temp
       
  1049     // Get the size of the temp file
       
  1050 
       
  1051 
       
  1052     User::LeaveIfError( tempFileHandle.Open( aFs, tempFile, EFileRead ));
       
  1053     CleanupClosePushL( tempFileHandle );    
       
  1054     tempFileHandle.Size( fileSize );               
       
  1055     
       
  1056     
       
  1057     // Set the fileSize of the original file
       
  1058     User::LeaveIfError( aOriginalFile.SetSize( fileSize ) );
       
  1059     offset = 0;
       
  1060     User::LeaveIfError( tempFileHandle.Seek( ESeekStart, offset ) );
       
  1061     orig.Attach( aOriginalFile );
       
  1062     CleanupClosePushL( orig );
       
  1063      
       
  1064     buffer = HBufC8::NewLC(KBufferSize);    
       
  1065     // Copy the file over because we can't use other copy things:
       
  1066     do
       
  1067         {
       
  1068         ptr.Set(buffer->Des());
       
  1069         ptr.SetLength(0);
       
  1070         tempFileHandle.Read(ptr);         
       
  1071         if (ptr.Length() > 0)
       
  1072             {
       
  1073             orig.WriteL(ptr);
       
  1074             }
       
  1075         }
       
  1076     while (ptr.Length() > 0);
       
  1077     
       
  1078     // Write the changes    
       
  1079     orig.CommitL();
       
  1080     
       
  1081     // close the tempfile
       
  1082     CleanupStack::PopAndDestroy(3); // buffer, tempFileHandle, orig
       
  1083     
       
  1084     // Delete the temp file
       
  1085     User::LeaveIfError( aFs.Delete( tempFile ) );
       
  1086     }
       
  1087 
       
  1088 // -----------------------------------------------------------------------------
       
  1089 // 
       
  1090 //
       
  1091 // Parameters:
       
  1092 //
       
  1093 // Returns:
       
  1094 // -----------------------------------------------------------------------------
       
  1095 LOCAL_C void SetFileHeaderL(
       
  1096     const TDesC16& aFileName, 
       
  1097     const TDesC8& aHeaderName, 
       
  1098     const TDesC8& aHeaderValue)
       
  1099     {
       
  1100     __UHEAP_MARK;
       
  1101     TFileName tempFileName;
       
  1102     HBufC8* newHeaderBuf = NULL;
       
  1103     COma1Dcf* dcf = NULL;
       
  1104     RFs fs;
       
  1105     RFile file;
       
  1106 
       
  1107     User::LeaveIfError( fs.Connect() );
       
  1108     CleanupClosePushL(fs);
       
  1109     User::LeaveIfError( GetFileHandleRead( fs, file, aFileName ) );
       
  1110 
       
  1111     CleanupClosePushL(file);
       
  1112     dcf = COma1Dcf::NewL(file);
       
  1113     
       
  1114     CleanupStack::PushL(dcf);
       
  1115     GetNewHeaderBufferL(dcf->iHeaders, aHeaderName, aHeaderValue, newHeaderBuf);
       
  1116     delete dcf->iHeaders;
       
  1117     dcf->iHeaders = newHeaderBuf;
       
  1118     dcf->iHeaderLength = newHeaderBuf->Length();
       
  1119     WriteNewFileL( fs, file, aFileName, *dcf, tempFileName );
       
  1120     CleanupStack::PopAndDestroy(2); // file, dcf
       
  1121     User::LeaveIfError( fs.Replace( tempFileName, aFileName ) );
       
  1122     CleanupStack::PopAndDestroy(); // fs
       
  1123     
       
  1124     __UHEAP_MARKEND;
       
  1125     }
       
  1126 
       
  1127 // -----------------------------------------------------------------------------
       
  1128 // 
       
  1129 //
       
  1130 // Parameters:
       
  1131 //
       
  1132 // Returns:
       
  1133 // -----------------------------------------------------------------------------
       
  1134 LOCAL_C void SetFileHeaderL(
       
  1135     RFile& aFileHandle, 
       
  1136     const TDesC8& aHeaderName, 
       
  1137     const TDesC8& aHeaderValue)
       
  1138     {
       
  1139     HBufC8* newHeaderBuf = NULL;
       
  1140     COma1Dcf* dcf = NULL;
       
  1141     RFs fs;
       
  1142     
       
  1143     User::LeaveIfError(fs.Connect());
       
  1144     CleanupClosePushL( fs );
       
  1145     
       
  1146     dcf = COma1Dcf::NewL(aFileHandle);
       
  1147     if (dcf == NULL)
       
  1148         {
       
  1149         User::Leave(KErrArgument);
       
  1150         }
       
  1151     CleanupStack::PushL(dcf);
       
  1152     GetNewHeaderBufferL(dcf->iHeaders, aHeaderName, aHeaderValue, newHeaderBuf);
       
  1153     delete dcf->iHeaders;
       
  1154     dcf->iHeaders = newHeaderBuf;
       
  1155     dcf->iHeaderLength = newHeaderBuf->Length();
       
  1156     WriteNewFileL(fs, aFileHandle, *dcf);
       
  1157     CleanupStack::PopAndDestroy(2); // dcf, fs
       
  1158     }
       
  1159 
       
  1160 // -----------------------------------------------------------------------------
       
  1161 // 
       
  1162 //
       
  1163 // Parameters:
       
  1164 //
       
  1165 // Returns:
       
  1166 // -----------------------------------------------------------------------------
       
  1167 void SetContentHeaderL(
       
  1168     HBufC8*& aContent, 
       
  1169     const TDesC8& aHeaderName, 
       
  1170     const TDesC8& aHeaderValue)
       
  1171     {
       
  1172     HBufC8* newHeaderBuf = NULL;
       
  1173     HBufC8* newContent = NULL;
       
  1174     HBufC8* buffer;
       
  1175     COma1Dcf* dcf = NULL;
       
  1176     TPtr8 ptr(0, 0);
       
  1177 
       
  1178     dcf = COma1Dcf::NewL(*aContent);
       
  1179     if (dcf == NULL)
       
  1180         {
       
  1181         User::Leave(KErrArgument);
       
  1182         }
       
  1183     CleanupStack::PushL(dcf);
       
  1184     GetNewHeaderBufferL(dcf->iHeaders, aHeaderName, aHeaderValue, newHeaderBuf);
       
  1185     delete dcf->iHeaders;
       
  1186     dcf->iHeaders = newHeaderBuf;
       
  1187     dcf->iHeaderLength = newHeaderBuf->Length();
       
  1188     newContent = HBufC8::NewLC(3 +
       
  1189         dcf->iContentID->Length() + dcf->iMimeType->Length() + 2 * 5 +
       
  1190         dcf->iHeaderLength + dcf->iDataLength);
       
  1191     ptr.Set(newContent->Des());
       
  1192     ptr.Append(1);
       
  1193     ptr.Append(dcf->iMimeType->Length());
       
  1194     ptr.Append(dcf->iContentID->Length());
       
  1195     ptr.Append(*dcf->iMimeType);
       
  1196     ptr.Append(*dcf->iContentID);
       
  1197     buffer = TWspPrimitiveEncoder::UintVarL(dcf->iHeaders->Length());
       
  1198     CleanupStack::PushL(buffer);
       
  1199     ptr.Append(*buffer);
       
  1200     CleanupStack::PopAndDestroy(buffer);
       
  1201     buffer = TWspPrimitiveEncoder::UintVarL(dcf->iDataLength);
       
  1202     CleanupStack::PushL(buffer);
       
  1203     ptr.Append(*buffer);
       
  1204     CleanupStack::PopAndDestroy(buffer);
       
  1205     ptr.Append(*dcf->iHeaders);
       
  1206     ptr.Append(aContent->Right(dcf->iDataLength));
       
  1207     CleanupStack::Pop(newContent);
       
  1208     CleanupStack::PopAndDestroy(dcf);
       
  1209     delete aContent;
       
  1210     aContent = newContent;
       
  1211     }
       
  1212 
       
  1213 // -----------------------------------------------------------------------------
       
  1214 // 
       
  1215 //
       
  1216 // Parameters:
       
  1217 //
       
  1218 // Returns:
       
  1219 // -----------------------------------------------------------------------------
       
  1220 void GetHeaderL(
       
  1221     const TDesC8& aContent, 
       
  1222     const TDesC8& aHeaderName, 
       
  1223     HBufC8*& aHeaderValue)
       
  1224     {
       
  1225     
       
  1226     TInt i;
       
  1227     TInt j;
       
  1228     TPtrC8 ptr( aContent );
       
  1229     
       
  1230     // Add Room for CRLF and Semicolon:
       
  1231     HBufC8* buffer = HBufC8::NewMaxLC( aHeaderName.Length() + 3 );  
       
  1232     TPtr8 searchBuf( const_cast<TUint8*>(buffer->Ptr()), 0, buffer->Des().MaxSize() );  
       
  1233 
       
  1234     searchBuf.Copy(aHeaderName);
       
  1235     searchBuf.Append(KHeaderNameEnding);
       
  1236     
       
  1237     // First see if the     
       
  1238     i = ptr.Find(searchBuf);
       
  1239     User::LeaveIfError( i );
       
  1240         
       
  1241     if( i > 0 )
       
  1242         {
       
  1243         // if it's not the first one, use the search buffer:
       
  1244         // Create the search buffer
       
  1245         searchBuf.Copy(KHeaderEnding);
       
  1246         searchBuf.Append(aHeaderName);
       
  1247         searchBuf.Append(KHeaderNameEnding);
       
  1248     
       
  1249         // First see if the     
       
  1250         i = ptr.Find(searchBuf);
       
  1251         User::LeaveIfError( i );      
       
  1252         }
       
  1253     // Move search buffer    
       
  1254     i += searchBuf.Length();  
       
  1255     
       
  1256     j = ptr.Mid(i).Find(KHeaderEnding);
       
  1257     User::LeaveIfError( j );
       
  1258             
       
  1259     aHeaderValue = ptr.Mid(i, j).AllocL();  
       
  1260     CleanupStack::PopAndDestroy(); // buffer  
       
  1261     }
       
  1262 
       
  1263 // -----------------------------------------------------------------------------
       
  1264 // 
       
  1265 //
       
  1266 // Parameters:
       
  1267 //
       
  1268 // Returns:
       
  1269 // -----------------------------------------------------------------------------
       
  1270 void GetContentHeaderL(
       
  1271     const TDesC8& aContent, 
       
  1272     const TDesC8& aHeaderName, 
       
  1273     HBufC8*& aHeaderValue)
       
  1274     {
       
  1275     COma1Dcf* dcf = NULL;
       
  1276 
       
  1277     dcf = COma1Dcf::NewL(aContent);
       
  1278     CleanupStack::PushL(dcf);
       
  1279     GetHeaderL(*dcf->iHeaders, aHeaderName, aHeaderValue);
       
  1280     CleanupStack::PopAndDestroy(); // dcf
       
  1281     }
       
  1282 // -----------------------------------------------------------------------------
       
  1283 // 
       
  1284 //
       
  1285 // Parameters:
       
  1286 //
       
  1287 // Returns:
       
  1288 // -----------------------------------------------------------------------------
       
  1289 void GetFileHeaderL(
       
  1290     const TFileName& aFileName, 
       
  1291     const TDesC8& aHeaderName, 
       
  1292     HBufC8*& aHeaderValue)
       
  1293     {
       
  1294     RFs fs;
       
  1295     RFile file;
       
  1296 
       
  1297     User::LeaveIfError(fs.Connect());
       
  1298     CleanupClosePushL(fs);
       
  1299     User::LeaveIfError( GetFileHandleRead( fs, file, aFileName ) );
       
  1300 
       
  1301     CleanupClosePushL(file);
       
  1302     GetFileHeaderL(file, aHeaderName, aHeaderValue);
       
  1303     CleanupStack::PopAndDestroy(2); // fs, file
       
  1304     }
       
  1305 
       
  1306 // -----------------------------------------------------------------------------
       
  1307 // 
       
  1308 //
       
  1309 // Parameters:
       
  1310 //
       
  1311 // Returns:
       
  1312 // -----------------------------------------------------------------------------
       
  1313 void GetFileHeaderL(
       
  1314     RFile& aFileHandle, 
       
  1315     const TDesC8& aHeaderName, 
       
  1316     HBufC8*& aHeaderValue)
       
  1317     {
       
  1318     CDcfCommon* dcf = NULL;
       
  1319 
       
  1320     aHeaderValue = NULL;
       
  1321     dcf = CDcfCommon::NewL(aFileHandle);
       
  1322     if (dcf == NULL)
       
  1323         {
       
  1324         User::Leave(KErrArgument);
       
  1325         }
       
  1326     CleanupStack::PushL(dcf);
       
  1327     if (dcf->iVersion == EOma1Dcf)
       
  1328         {
       
  1329         GetHeaderL(*static_cast<COma1Dcf*>(dcf)->iHeaders, aHeaderName,
       
  1330             aHeaderValue);
       
  1331         }
       
  1332     else
       
  1333         {
       
  1334         if (aHeaderName.Compare(KRiUrl) == 0 && dcf->iRightsIssuerURL != NULL)
       
  1335             {
       
  1336             aHeaderValue = dcf->iRightsIssuerURL->AllocL();
       
  1337             }
       
  1338         }
       
  1339     CleanupStack::PopAndDestroy(); // dcf
       
  1340     }
       
  1341 
       
  1342 // -----------------------------------------------------------------------------
       
  1343 // DynamicDataTypesCount: Returns the number of supported dynamic datatypes
       
  1344 //
       
  1345 // Parameters:
       
  1346 //      aCount: out parameter for the number of dynamically registered data
       
  1347 //          types
       
  1348 //
       
  1349 // Returns:
       
  1350 //      DRMCommon::EOk: Count returned successfully
       
  1351 // -----------------------------------------------------------------------------
       
  1352 LOCAL_C TInt DynamicDataTypesCount(
       
  1353     TInt& aCount)
       
  1354     {
       
  1355     TInt err = 0;
       
  1356     TDataType type;
       
  1357     RFs fs;
       
  1358     RFileReadStream reader;    
       
  1359     
       
  1360     aCount = 0;
       
  1361     err = fs.Connect();
       
  1362     if (!err) 
       
  1363         {
       
  1364         
       
  1365 #ifndef RD_MULTIPLE_DRIVE
       
  1366         
       
  1367     err = reader.Open(fs, KDataTypesFile, EFileRead);
       
  1368     
       
  1369 #else //RD_MULTIPLE_DRIVE
       
  1370     
       
  1371     TInt driveNumber( -1 );
       
  1372     TChar driveLetter;
       
  1373     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
  1374     fs.DriveToChar( driveNumber, driveLetter );
       
  1375     
       
  1376     TFileName dataTypesFile;
       
  1377 	dataTypesFile.Format( KDataTypesFile, (TUint)driveLetter );
       
  1378           
       
  1379     err = reader.Open(fs, dataTypesFile, EFileRead);
       
  1380     
       
  1381 #endif
       
  1382         
       
  1383         if (err == KErrNotFound)
       
  1384             {
       
  1385             err = DRMCommon::EOk;
       
  1386             }
       
  1387         else
       
  1388             {
       
  1389             while (err == KErrNone)
       
  1390                 {
       
  1391                 TRAP(err, (reader >> type));
       
  1392                 if (err == KErrNone) aCount++;
       
  1393                 }
       
  1394             
       
  1395             if (err == KErrEof)
       
  1396                 {
       
  1397                 err = DRMCommon::EOk;
       
  1398                 }
       
  1399             
       
  1400             reader.Release();
       
  1401             reader.Close();
       
  1402             }
       
  1403         }
       
  1404     
       
  1405     fs.Close();
       
  1406     
       
  1407     return err;
       
  1408     }
       
  1409 
       
  1410 // -----------------------------------------------------------------------------
       
  1411 // SupportedDynamicDataType: Returns a registered dynamic datatype
       
  1412 //
       
  1413 // Parameters:
       
  1414 //      aIndex: index of the dynamic data type
       
  1415 //      aDataType: out parameter for the data type
       
  1416 //
       
  1417 // Returns:
       
  1418 //      DRMCommon::EOk: Data type returned successfully
       
  1419 //      KErrEof: Data type not found
       
  1420 // -----------------------------------------------------------------------------
       
  1421 LOCAL_C TInt SupportedDynamicDataType(
       
  1422     const TInt aIndex, 
       
  1423     TDataType& aDataType)
       
  1424     {
       
  1425     TInt err = KErrNone;
       
  1426     TInt i;
       
  1427     RFs fs;
       
  1428     TDataType type;
       
  1429     RFileReadStream reader;
       
  1430     
       
  1431     err = fs.Connect();
       
  1432     if (!err) 
       
  1433         {
       
  1434         
       
  1435 #ifndef RD_MULTIPLE_DRIVE
       
  1436         
       
  1437         err = reader.Open(fs, KDataTypesFile, EFileRead);
       
  1438     
       
  1439 #else //RD_MULTIPLE_DRIVE
       
  1440     
       
  1441         TInt driveNumber( -1 );
       
  1442         TChar driveLetter;
       
  1443         DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
  1444     	fs.DriveToChar( driveNumber, driveLetter );
       
  1445         
       
  1446     	TFileName dataTypesFile;
       
  1447 	    dataTypesFile.Format( KDataTypesFile, (TUint)driveLetter );
       
  1448         
       
  1449         err = reader.Open(fs, dataTypesFile, EFileRead);
       
  1450     
       
  1451 #endif
       
  1452         
       
  1453         if (!err) 
       
  1454             {
       
  1455             i = -1;
       
  1456             while (err == 0 && i != aIndex) 
       
  1457                 {
       
  1458                 TRAP(err, (reader >> type));
       
  1459                 i++;
       
  1460                 }
       
  1461             
       
  1462             if (!err)
       
  1463                 {
       
  1464                 aDataType = type;  
       
  1465                 }
       
  1466             
       
  1467             reader.Release();
       
  1468             reader.Close();
       
  1469             }
       
  1470         }
       
  1471     
       
  1472     fs.Close();
       
  1473     
       
  1474     return err;
       
  1475     }
       
  1476 
       
  1477 // -----------------------------------------------------------------------------
       
  1478 // SupportedStaticDataType: Returns a static DRM datatype
       
  1479 //
       
  1480 // Parameters:
       
  1481 //      aIndex: index of the dynamic data type
       
  1482 //      aDataType: out parameter for the data type
       
  1483 //
       
  1484 // Returns:
       
  1485 //      DRMCommon::EOk: Data type returned successfully
       
  1486 //      KErrEof: Data type not found
       
  1487 // -----------------------------------------------------------------------------
       
  1488 LOCAL_C TInt SupportedStaticDataType(
       
  1489     const TInt aIndex, 
       
  1490     TDataType& aDataType)
       
  1491     {
       
  1492     TInt err = 0;
       
  1493     TInt index = 0;
       
  1494     TInt length = 0;
       
  1495     TInt i = 0;
       
  1496     RFs fs;
       
  1497     RResourceFile resourceFile;
       
  1498     HBufC8* res = NULL;
       
  1499     TResourceReader theReader;
       
  1500     
       
  1501     err = fs.Connect();
       
  1502     if (err)
       
  1503         {
       
  1504         fs.Close();
       
  1505         return err;
       
  1506         }
       
  1507     
       
  1508 #ifndef RD_MULTIPLE_DRIVE
       
  1509     
       
  1510     TRAP(err, resourceFile.OpenL(fs, KResourceFile));
       
  1511     
       
  1512 #else //RD_MULTIPLE_DRIVE
       
  1513     
       
  1514     TInt driveNumber( -1 );
       
  1515     TChar driveLetter;
       
  1516     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
  1517     fs.DriveToChar( driveNumber, driveLetter );
       
  1518     
       
  1519     TFileName resFile;
       
  1520 	resFile.Format( KResourceFile, (TUint)driveLetter );
       
  1521     
       
  1522     TRAP(err, resourceFile.OpenL(fs, resFile));
       
  1523     
       
  1524 #endif
       
  1525     if (err)
       
  1526         {
       
  1527         fs.Close();
       
  1528         return err;
       
  1529         }
       
  1530     
       
  1531     TRAP(err, (res = resourceFile.AllocReadL(DATATYPE))); 
       
  1532     if (err)
       
  1533         {
       
  1534         resourceFile.Close();
       
  1535         fs.Close();
       
  1536         return err;
       
  1537         }
       
  1538     
       
  1539     theReader.SetBuffer(res);
       
  1540     index = aIndex + 1;
       
  1541     TPtrC temp16(theReader.ReadTPtrC(index, res));
       
  1542     length = temp16.Length();
       
  1543     HBufC8* temp = NULL;
       
  1544     temp = HBufC8::NewMax(length); 
       
  1545     if (temp)
       
  1546         {
       
  1547         TPtr8 ptr(temp->Des());
       
  1548         ptr.SetLength(length);
       
  1549         for (i = 0; i < length; i++)
       
  1550             {
       
  1551             ptr[i] = (unsigned char) temp16[i];
       
  1552             }
       
  1553         aDataType = TDataType(ptr);
       
  1554         }
       
  1555     else
       
  1556         {
       
  1557         err = KErrNoMemory;
       
  1558         }
       
  1559     
       
  1560     if (temp) 
       
  1561         {
       
  1562         delete temp;
       
  1563         }
       
  1564     
       
  1565     temp = NULL;
       
  1566     resourceFile.Close();
       
  1567     if (res) 
       
  1568         {
       
  1569         delete res;
       
  1570         }
       
  1571     
       
  1572     res = NULL;
       
  1573     fs.Close();
       
  1574     return err;
       
  1575     }
       
  1576 
       
  1577 // -----------------------------------------------------------------------------
       
  1578 // UnRegisterDynamicDataTypeL: Unregister one of the dynamically addable data
       
  1579 //      types
       
  1580 //
       
  1581 // Parameters:
       
  1582 //      aIndex: index of the data type in the list of dynamic types
       
  1583 //
       
  1584 // Returns:
       
  1585 //      DRMCommon::EOK: Data type removed successfully
       
  1586 // -----------------------------------------------------------------------------
       
  1587 LOCAL_C TInt UnRegisterDynamicDataTypeL(
       
  1588     const TInt aIndex)
       
  1589     {
       
  1590     TInt r;
       
  1591     TInt i;
       
  1592     TDataType type;
       
  1593     RFileWriteStream writer;
       
  1594     RFileReadStream reader;
       
  1595     RFs fs;
       
  1596     
       
  1597     User::LeaveIfError(fs.Connect());
       
  1598     CleanupClosePushL(fs);
       
  1599     
       
  1600     // Open the data types file
       
  1601     
       
  1602 #ifndef RD_MULTIPLE_DRIVE
       
  1603 
       
  1604     User::LeaveIfError(reader.Open(fs, KDataTypesFile, EFileRead));
       
  1605     
       
  1606 #else //RD_MULTIPLE_DRIVE
       
  1607     
       
  1608     TInt driveNumber( -1 );
       
  1609     TChar driveLetter;
       
  1610     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
  1611     fs.DriveToChar( driveNumber, driveLetter );
       
  1612     
       
  1613     TFileName dataTypesFile;
       
  1614 	dataTypesFile.Format( KDataTypesFile, (TUint)driveLetter );
       
  1615     
       
  1616     User::LeaveIfError(reader.Open(fs, dataTypesFile, EFileRead));
       
  1617     
       
  1618 #endif
       
  1619     
       
  1620     CleanupReleasePushL(reader);
       
  1621     
       
  1622     // Create and open a replacement file
       
  1623     
       
  1624 #ifndef RD_MULTIPLE_DRIVE
       
  1625     
       
  1626     User::LeaveIfError(writer.Replace(fs, KTempFile, EFileWrite));
       
  1627     
       
  1628 #else //RD_MULTIPLE_DRIVE
       
  1629     
       
  1630     TFileName tempFile;
       
  1631     tempFile.Format( KTempFile, (TUint)driveLetter );
       
  1632     
       
  1633     User::LeaveIfError(writer.Replace(fs, tempFile, EFileWrite));
       
  1634     
       
  1635 #endif
       
  1636     
       
  1637     CleanupReleasePushL(writer);
       
  1638     
       
  1639     // Write all dynamic types into the replacement file, excluding the
       
  1640     // data type to be removed
       
  1641     
       
  1642     r = KErrNone;
       
  1643     for (i = 0; r == KErrNone; i++)
       
  1644         {
       
  1645         TRAP(r, reader >> type);
       
  1646         if (r == KErrNone && i != aIndex)
       
  1647             {
       
  1648             writer << type;
       
  1649             writer.CommitL();
       
  1650             }
       
  1651         }
       
  1652     CleanupStack::PopAndDestroy();
       
  1653     CleanupStack::PopAndDestroy();
       
  1654     
       
  1655     // Replace the data type file with the replacement file
       
  1656     
       
  1657 #ifndef RD_MULTIPLE_DRIVE
       
  1658 
       
  1659     fs.Replace(KTempFile, KDataTypesFile);
       
  1660     
       
  1661 #else //RD_MULTIPLE_DRIVE
       
  1662     
       
  1663     fs.Replace(tempFile, dataTypesFile);
       
  1664     
       
  1665 #endif
       
  1666     
       
  1667     
       
  1668     CleanupStack::PopAndDestroy();
       
  1669     
       
  1670     return DRMCommon::EOk;
       
  1671     }
       
  1672 
       
  1673 // -----------------------------------------------------------------------------
       
  1674 // RegisterDynamicDataTypeL: Register a new dynamically addable data type
       
  1675 //
       
  1676 // Parameters:
       
  1677 //      aDataType: new type to be added
       
  1678 //
       
  1679 // Returns:
       
  1680 //      DRMCommon::EOK: Data type removed successfully
       
  1681 // -----------------------------------------------------------------------------
       
  1682 LOCAL_C TInt RegisterDynamicDataTypeL(const TDataType& aDataType)
       
  1683     {
       
  1684     TInt err = KErrNone;
       
  1685     TDataType type;
       
  1686     RFileWriteStream writer;
       
  1687     RFileReadStream reader;
       
  1688     RFs fs;
       
  1689     
       
  1690     User::LeaveIfError(fs.Connect());
       
  1691     CleanupClosePushL(fs);
       
  1692     
       
  1693     // Create and open a replacement file
       
  1694     
       
  1695 #ifndef RD_MULTIPLE_DRIVE
       
  1696     
       
  1697     User::LeaveIfError(writer.Replace(fs, KTempFile, EFileWrite));
       
  1698     
       
  1699 #else //RD_MULTIPLE_DRIVE
       
  1700     
       
  1701     TInt driveNumber( -1 );
       
  1702     TChar driveLetter;
       
  1703     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultSystem, driveNumber );
       
  1704     fs.DriveToChar( driveNumber, driveLetter );
       
  1705     
       
  1706     TFileName tempFile;
       
  1707 	tempFile.Format( KTempFile, (TUint)driveLetter );
       
  1708     
       
  1709     User::LeaveIfError(writer.Replace(fs, tempFile, EFileWrite));
       
  1710     
       
  1711 #endif
       
  1712     
       
  1713     CleanupReleasePushL(writer);
       
  1714     
       
  1715     // Write the new data type into the replacement file
       
  1716     
       
  1717     writer << aDataType;
       
  1718     writer.CommitL();
       
  1719     
       
  1720     // Write all other dynamic types into the replacement file
       
  1721     
       
  1722 #ifndef RD_MULTIPLE_DRIVE
       
  1723     
       
  1724     err = reader.Open(fs, KDataTypesFile, EFileRead);
       
  1725     
       
  1726 #else //RD_MULTIPLE_DRIVE
       
  1727     
       
  1728     TFileName dataTypesFile;
       
  1729     dataTypesFile.Format( KDataTypesFile, (TUint)driveLetter );
       
  1730     
       
  1731     err = reader.Open(fs, dataTypesFile, EFileRead);
       
  1732     
       
  1733 #endif
       
  1734     
       
  1735     if (err == KErrNone)
       
  1736         {
       
  1737         CleanupReleasePushL(reader);
       
  1738         while (err == KErrNone)
       
  1739             {
       
  1740             TRAP(err, (reader >> type, writer << type, writer.CommitL()));
       
  1741             }
       
  1742         CleanupStack::PopAndDestroy();
       
  1743         }
       
  1744     
       
  1745     CleanupStack::PopAndDestroy();
       
  1746     
       
  1747     // Replace the data type file with the replacement file
       
  1748     
       
  1749 #ifndef RD_MULTIPLE_DRIVE
       
  1750     
       
  1751     fs.Replace(KTempFile, KDataTypesFile);
       
  1752     
       
  1753 #else //RD_MULTIPLE_DRIVE
       
  1754     
       
  1755     fs.Replace(tempFile, dataTypesFile);
       
  1756     
       
  1757 #endif
       
  1758     
       
  1759     CleanupStack::PopAndDestroy();
       
  1760     
       
  1761     return DRMCommon::EOk;
       
  1762     }
       
  1763 
       
  1764 TInt CalculatePaddingL(COma1Dcf* dcf)
       
  1765     {
       
  1766     RDRMRightsClient rdb;
       
  1767     TBuf8<KDCFKeySize * 2> blocks;
       
  1768     TInt pos;
       
  1769     TBool blocksValid = ETrue;
       
  1770 
       
  1771     if (!dcf->iPlainTextLengthValid)
       
  1772         {
       
  1773         User::LeaveIfError(rdb.Connect());
       
  1774         CleanupClosePushL(rdb);
       
  1775         if (dcf->iFile.SubSessionHandle() != KNullHandle)
       
  1776             {
       
  1777             pos = dcf->iOffset + dcf->iDataLength - 2 * KDCFKeySize;
       
  1778             dcf->iFile.Seek(ESeekStart, pos);
       
  1779             dcf->iFile.Read(blocks);
       
  1780             }
       
  1781         else if (dcf->iData->Length() >= dcf->iOffset + dcf->iDataLength)
       
  1782             {
       
  1783             blocks.Copy(&(dcf->iData->Des())[dcf->iOffset +
       
  1784                 dcf->iDataLength - 2 * KDCFKeySize],
       
  1785                 KDCFKeySize * 2);
       
  1786             }
       
  1787         else
       
  1788             {
       
  1789             blocksValid = EFalse;
       
  1790             }
       
  1791 
       
  1792         if (blocksValid && rdb.InitializeKey(*dcf->iContentID) == KErrNone)
       
  1793             {
       
  1794             dcf->iPadding = rdb.CalculatePadding(blocks);
       
  1795             if (dcf->iPadding >= 0)
       
  1796                 {
       
  1797                 dcf->iPlainTextLength -= dcf->iPadding;
       
  1798                 dcf->iPlainTextLengthValid = ETrue;
       
  1799                 }
       
  1800             }
       
  1801         CleanupStack::PopAndDestroy(); // rdb
       
  1802         }
       
  1803 
       
  1804     return dcf->iPadding;
       
  1805     }
       
  1806     
       
  1807 // ============================ MEMBER FUNCTIONS ===============================
       
  1808 
       
  1809 // -----------------------------------------------------------------------------
       
  1810 // DRMCommon::DRMCommon
       
  1811 // C++ default constructor can NOT contain any code, that
       
  1812 // might leave.
       
  1813 // -----------------------------------------------------------------------------
       
  1814 EXPORT_C DRMCommon::DRMCommon(void)
       
  1815     {
       
  1816     } 
       
  1817 
       
  1818 // -----------------------------------------------------------------------------
       
  1819 // DRMCommon::ConstructL
       
  1820 // Symbian 2nd phase constructor can leave.
       
  1821 // -----------------------------------------------------------------------------
       
  1822 EXPORT_C void DRMCommon::ConstructL()
       
  1823     {
       
  1824     } 
       
  1825 
       
  1826 // -----------------------------------------------------------------------------
       
  1827 // DRMCommon::NewL
       
  1828 // Two-phased constructor.
       
  1829 // -----------------------------------------------------------------------------
       
  1830 EXPORT_C DRMCommon* DRMCommon::NewL()
       
  1831     {
       
  1832     DRMCommon* self = new(ELeave) DRMCommon();
       
  1833     CleanupStack::PushL(self);
       
  1834     self->ConstructL();
       
  1835     CleanupStack::Pop(); // self
       
  1836     return self;
       
  1837     }
       
  1838 
       
  1839 
       
  1840 // Destructor
       
  1841 EXPORT_C DRMCommon::~DRMCommon()
       
  1842     {
       
  1843     }
       
  1844 
       
  1845 // -----------------------------------------------------------------------------
       
  1846 // DRMCommon::CheckContentRights
       
  1847 // Checks if the give rights for a specific content URI are available.
       
  1848 // -----------------------------------------------------------------------------
       
  1849 EXPORT_C TInt DRMCommon::CheckContentRights(
       
  1850     const TDesC8& aContentID, 
       
  1851     TUint32 aRightsSpec)
       
  1852     {
       
  1853     TInt r = ENoRights;
       
  1854     RDRMRightsClient client;
       
  1855     TUint32 reason = 0;
       
  1856     
       
  1857     if (client.Connect() == KErrNone)
       
  1858         {
       
  1859         r = client.CheckRights(aRightsSpec, aContentID, reason);
       
  1860         client.Close();
       
  1861         }
       
  1862     return r;
       
  1863     }
       
  1864 
       
  1865 // -----------------------------------------------------------------------------
       
  1866 // DRMCommon::CheckFileRights
       
  1867 // Checks if the give rights for a specific content URI are available by
       
  1868 // opening a given file and reading the content URI from there.
       
  1869 // -----------------------------------------------------------------------------
       
  1870 EXPORT_C TInt DRMCommon::CheckFileRights(
       
  1871     const TDesC& aFileName, 
       
  1872     TUint32 aRightsSpec)
       
  1873     {
       
  1874     TInt r = KErrNone;
       
  1875     TInt retValue = KErrNone;
       
  1876     TRAP(r, retValue = CheckFileRightsL(aFileName, aRightsSpec));
       
  1877     if (r != KErrNone)
       
  1878         {
       
  1879         return r;
       
  1880         }
       
  1881     return retValue;
       
  1882     }
       
  1883 
       
  1884 // -----------------------------------------------------------------------------
       
  1885 // DRMCommon::CheckFileRights
       
  1886 // Checks if the give rights for a specific content URI are available by
       
  1887 // opening a given file and reading the content URI from there.
       
  1888 // -----------------------------------------------------------------------------
       
  1889 EXPORT_C TInt DRMCommon::CheckFileRights(
       
  1890     RFile& aFileHandle, 
       
  1891     TUint32 aRightsSpec)
       
  1892     {
       
  1893     TInt r = KErrNone;
       
  1894     TInt retValue = KErrNone;
       
  1895     TRAP(r, retValue = CheckFileRightsL(aFileHandle, aRightsSpec));
       
  1896     if (r != KErrNone)
       
  1897         {
       
  1898         return r;
       
  1899         }
       
  1900     return retValue;
       
  1901     }
       
  1902 
       
  1903 // -----------------------------------------------------------------------------
       
  1904 // DRMCommon::IsProtectedContent
       
  1905 // Check if a memory buffer contains DRM proteced content by looking for a
       
  1906 // predefined header string.
       
  1907 // -----------------------------------------------------------------------------
       
  1908 EXPORT_C TInt DRMCommon::IsProtectedContent(
       
  1909     const TDesC8& aContent, 
       
  1910     TBool& aProtection)
       
  1911     {
       
  1912     if (COma1Dcf::IsValidDcf(aContent) || COma2Dcf::IsValidDcf(aContent))
       
  1913         {
       
  1914         aProtection = ETrue;
       
  1915         }
       
  1916     else
       
  1917         {
       
  1918         aProtection = EFalse;
       
  1919         }
       
  1920     return EOk;
       
  1921     }
       
  1922 
       
  1923 // -----------------------------------------------------------------------------
       
  1924 // DRMCommon::IsProtectedFile
       
  1925 // Check if a file contains DRM proteced content by looking for a
       
  1926 // predefined header string.
       
  1927 // -----------------------------------------------------------------------------
       
  1928 EXPORT_C TInt DRMCommon::IsProtectedFile(
       
  1929     const TDesC& aFileName, 
       
  1930     TBool& aProtection)
       
  1931     {
       
  1932     TInt r = KErrNone;
       
  1933     TRAP(r, IsProtectedFileL(aFileName, aProtection));
       
  1934     return r;
       
  1935     }
       
  1936 
       
  1937 // -----------------------------------------------------------------------------
       
  1938 // DRMCommon::IsProtectedFile
       
  1939 // Check if a file contains DRM proteced content by looking for a
       
  1940 // predefined header string.
       
  1941 // -----------------------------------------------------------------------------
       
  1942 EXPORT_C TInt DRMCommon::IsProtectedFile(
       
  1943     RFile& aFileHandle, 
       
  1944     TBool& aProtection)
       
  1945     {
       
  1946     TInt r = KErrNone;
       
  1947     TRAP(r, IsProtectedFileL(aFileHandle, aProtection));
       
  1948     return r;
       
  1949     }
       
  1950     
       
  1951 // -----------------------------------------------------------------------------
       
  1952 // DRMCommon::GetContentInfo
       
  1953 // Returns DRM information about a memory buffer.
       
  1954 // -----------------------------------------------------------------------------
       
  1955 EXPORT_C TInt DRMCommon::GetContentInfo(
       
  1956     const TDesC8& aContent, 
       
  1957     TContentProtection& aProtection, 
       
  1958     HBufC8*& aMIMEType, 
       
  1959     HBufC8*& aContentURI, 
       
  1960     TUint& aDataLength)
       
  1961     {
       
  1962     TInt r = KErrNone;
       
  1963     TRAP(r, GetContentInfoL(aContent, aProtection, aMIMEType, aContentURI, 
       
  1964         aDataLength));
       
  1965     return r;
       
  1966     }
       
  1967 
       
  1968 // -----------------------------------------------------------------------------
       
  1969 // DRMCommon::GetFileInfo
       
  1970 // Returns DRM information about a file.
       
  1971 // -----------------------------------------------------------------------------
       
  1972 EXPORT_C TInt DRMCommon::GetFileInfo(
       
  1973     const TDesC& aFileName, 
       
  1974     TContentProtection& aProtection, 
       
  1975     HBufC8*& aMIMEType, 
       
  1976     HBufC8*& aContentURI, 
       
  1977     TUint& aDataLength)
       
  1978     {
       
  1979     TInt r = KErrNone;
       
  1980     TRAP(r, GetFileInfoL(aFileName, aProtection, aMIMEType, aContentURI, 
       
  1981         aDataLength));
       
  1982     return r;
       
  1983     }
       
  1984 
       
  1985 // -----------------------------------------------------------------------------
       
  1986 // DRMCommon::GetFileInfo
       
  1987 // Returns DRM information about a file.
       
  1988 // -----------------------------------------------------------------------------
       
  1989 EXPORT_C TInt DRMCommon::GetFileInfo(
       
  1990     RFile& aFileHandle, 
       
  1991     TContentProtection& aProtection, 
       
  1992     HBufC8*& aMIMEType, 
       
  1993     HBufC8*& aContentURI, 
       
  1994     TUint& aDataLength)
       
  1995     {
       
  1996     TInt r = KErrNone;
       
  1997     TRAP(r, GetFileInfoL(aFileHandle, aProtection, aMIMEType, aContentURI, 
       
  1998         aDataLength));
       
  1999     return r;
       
  2000     }
       
  2001 
       
  2002 // -----------------------------------------------------------------------------
       
  2003 // DRMCommon::GetContentHeader
       
  2004 // Returns an optional header from a memory buffer containing encrypted content.
       
  2005 // -----------------------------------------------------------------------------
       
  2006 EXPORT_C TInt DRMCommon::GetContentHeader(
       
  2007     const TDesC8& aContent, 
       
  2008     const TDesC8& aHeaderName, 
       
  2009     HBufC8*& aHeaderValue)
       
  2010     {
       
  2011     TInt r = KErrNone;
       
  2012 
       
  2013     TRAP(r, GetContentHeaderL(aContent, aHeaderName, aHeaderValue));
       
  2014     return r;
       
  2015     }
       
  2016 
       
  2017 // -----------------------------------------------------------------------------
       
  2018 // DRMCommon::GetFileHeader
       
  2019 // Returns an optional header from a file containing encrypted content.
       
  2020 // -----------------------------------------------------------------------------
       
  2021 EXPORT_C TInt DRMCommon::GetFileHeader(
       
  2022     const TFileName& aFileName, 
       
  2023     const TDesC8& aHeaderName, 
       
  2024     HBufC8*& aHeaderValue)
       
  2025     {
       
  2026     TInt r = KErrNone;
       
  2027 
       
  2028     TRAP(r, GetFileHeaderL(aFileName, aHeaderName, aHeaderValue));
       
  2029     return r;
       
  2030     }
       
  2031 
       
  2032 // -----------------------------------------------------------------------------
       
  2033 // DRMCommon::GetFileHeader
       
  2034 // Returns an optional header from a file containing encrypted content.
       
  2035 // -----------------------------------------------------------------------------
       
  2036 EXPORT_C TInt DRMCommon::GetFileHeader(
       
  2037     RFile& aFileHandle, 
       
  2038     const TDesC8& aHeaderName, 
       
  2039     HBufC8*& aHeaderValue)
       
  2040     {
       
  2041     TInt r = KErrNone;
       
  2042 
       
  2043     TRAP(r, GetFileHeaderL(aFileHandle, aHeaderName, aHeaderValue));
       
  2044     return r;
       
  2045     }
       
  2046 
       
  2047 // -----------------------------------------------------------------------------
       
  2048 // DRMAuthenticated::SetContentHeader
       
  2049 // Sets the specified optional header field of a DCF buffer.
       
  2050 // -----------------------------------------------------------------------------
       
  2051 EXPORT_C TInt DRMCommon::SetContentHeader(
       
  2052     HBufC8*& aContent, 
       
  2053     const TDesC8& aHeaderName, 
       
  2054     const TDesC8& aHeaderValue)
       
  2055     {
       
  2056     TInt r = KErrNone;
       
  2057 
       
  2058     TRAP(r, SetContentHeaderL(aContent, aHeaderName, aHeaderValue));
       
  2059     return r;
       
  2060     }
       
  2061 
       
  2062 // -----------------------------------------------------------------------------
       
  2063 // DRMCommon::SetContentHeader
       
  2064 // Sets the specified optional header field of a DCF buffer.
       
  2065 // -----------------------------------------------------------------------------
       
  2066 EXPORT_C TInt DRMCommon::SetFileHeader(
       
  2067     const TDesC16& aFileName, 
       
  2068     const TDesC8& aHeaderName, 
       
  2069     const TDesC8& aHeaderValue)
       
  2070     {
       
  2071     TInt r = KErrNone;
       
  2072 
       
  2073     TRAP(r, SetFileHeaderL(aFileName, aHeaderName, aHeaderValue));
       
  2074     return r;
       
  2075     }
       
  2076 
       
  2077 // -----------------------------------------------------------------------------
       
  2078 // DRMCommon::SetContentHeader
       
  2079 // Sets the specified optional header field of a DCF buffer.
       
  2080 // -----------------------------------------------------------------------------
       
  2081 EXPORT_C TInt DRMCommon::SetFileHeader(
       
  2082     RFile& aFileHandle, 
       
  2083     const TDesC8& aHeaderName, 
       
  2084     const TDesC8& aHeaderValue)
       
  2085     {
       
  2086     TInt r = KErrNone;
       
  2087 
       
  2088     TRAP(r, SetFileHeaderL(aFileHandle, aHeaderName, aHeaderValue));
       
  2089     return r;
       
  2090     }
       
  2091 
       
  2092 // -----------------------------------------------------------------------------
       
  2093 // DRMCommon::GetSingleRightsObject
       
  2094 // Looks up the rights object using the content URI and the local ID.
       
  2095 // -----------------------------------------------------------------------------
       
  2096 EXPORT_C TInt DRMCommon::GetSingleRightsObject(
       
  2097     const TDesC8& aContentURI, 
       
  2098     TUint32 aLocalID, 
       
  2099     CDRMRights*& aRightsObject)
       
  2100     {
       
  2101     TInt r = KErrNone;
       
  2102     TRAP(r, GetSingleRightsObjectL(aContentURI, aLocalID, aRightsObject));
       
  2103     return r;
       
  2104     }
       
  2105 
       
  2106 // -----------------------------------------------------------------------------
       
  2107 // DRMCommon::GetDetailedContentRights
       
  2108 // Returns all rights objects for a content URI
       
  2109 // -----------------------------------------------------------------------------
       
  2110 EXPORT_C TInt DRMCommon::GetDetailedContentRights(
       
  2111     const TDesC8& aContentURI, 
       
  2112     RPointerArray<CDRMRights>*& aRightsList)
       
  2113     {
       
  2114     TInt r = KErrNone;
       
  2115 
       
  2116     TRAP(r, GetDetailedContentRightsL(aContentURI, aRightsList));
       
  2117     if (r == KErrNone)
       
  2118         {
       
  2119         AddParents( const_cast<DRMCommon*>(this), *aRightsList );
       
  2120         }
       
  2121     return r;
       
  2122     }
       
  2123 
       
  2124 // -----------------------------------------------------------------------------
       
  2125 // DRMCommon::GetDetailedFileRights
       
  2126 // Returns all rights objects for a content URI from a given file
       
  2127 // -----------------------------------------------------------------------------
       
  2128 EXPORT_C TInt DRMCommon::GetDetailedFileRights(
       
  2129     const TDesC& aFileName, 
       
  2130     RPointerArray<CDRMRights>*& aRightsList)
       
  2131     {
       
  2132     TInt r = KErrNone;
       
  2133 
       
  2134     TRAP(r, GetDetailedFileRightsL(aFileName, aRightsList));
       
  2135     if (r == KErrNone)
       
  2136         {
       
  2137         AddParents( const_cast<DRMCommon*>(this), *aRightsList );
       
  2138         }
       
  2139     return r;
       
  2140     }
       
  2141     
       
  2142 // -----------------------------------------------------------------------------
       
  2143 // DRMCommon::GetDetailedFileRights
       
  2144 // Returns all rights objects for a content URI from a given file
       
  2145 // -----------------------------------------------------------------------------
       
  2146 EXPORT_C TInt DRMCommon::GetDetailedFileRights(
       
  2147     RFile& aFileHandle, 
       
  2148     RPointerArray<CDRMRights>*& aRightsList)
       
  2149     {
       
  2150     TInt r = KErrNone;
       
  2151 
       
  2152     TRAP(r, GetDetailedFileRightsL(aFileHandle, aRightsList));
       
  2153     if (r == KErrNone)
       
  2154         {
       
  2155         AddParents( const_cast<DRMCommon*>(this), *aRightsList );
       
  2156         }
       
  2157     return r;
       
  2158     }    
       
  2159 
       
  2160 // -----------------------------------------------------------------------------
       
  2161 // DRMCommon::GetActiveRights
       
  2162 // Returns a possible active rights object
       
  2163 // -----------------------------------------------------------------------------
       
  2164 EXPORT_C TInt DRMCommon::GetActiveRights(
       
  2165     const TDesC8& aContentURI,
       
  2166     TUint32 aConstraints,
       
  2167     CDRMRights*& aRightsObject)
       
  2168     {
       
  2169     TInt r = KErrNone;
       
  2170     
       
  2171     TRAP( r, GetActiveRightsL( aContentURI, aConstraints, aRightsObject ) );
       
  2172     if(r != KErrNone)
       
  2173         {
       
  2174         return DRMCommon::ENoRights;    
       
  2175         }
       
  2176     else 
       
  2177         {
       
  2178         CDRMRights::TRestriction restriction;
       
  2179         CDRMRights::TExpiration expiration;
       
  2180         TUint32 constType(0);
       
  2181         aRightsObject->GetRightsInfo(aConstraints, restriction, expiration, constType);
       
  2182         return restriction;
       
  2183         }
       
  2184     }
       
  2185 
       
  2186 // -----------------------------------------------------------------------------
       
  2187 // DRMCommon::GetContentURIList
       
  2188 // Returns a list of all content URIs that have rights in the rights database.
       
  2189 // -----------------------------------------------------------------------------
       
  2190 EXPORT_C TInt DRMCommon::GetContentURIList(
       
  2191     RPointerArray<HBufC8>*& aURIList)
       
  2192     {
       
  2193     RDRMRightsClient client;
       
  2194     TInt error = client.Connect();
       
  2195     
       
  2196     aURIList = NULL;
       
  2197     if (!error)
       
  2198         {
       
  2199         aURIList = new RPointerArray<HBufC8>(10);
       
  2200         if (aURIList)
       
  2201             {
       
  2202             error = client.ExportContentIDList(*aURIList);
       
  2203             if (error)
       
  2204                 {
       
  2205                 aURIList->ResetAndDestroy();
       
  2206                 aURIList->Close();
       
  2207                 delete aURIList;
       
  2208                 aURIList = NULL;
       
  2209                 }
       
  2210             }
       
  2211         else
       
  2212             {
       
  2213             error = KErrNoMemory;
       
  2214             }
       
  2215         client.Close();
       
  2216         }
       
  2217     
       
  2218     return error;
       
  2219     }
       
  2220 
       
  2221 // -----------------------------------------------------------------------------
       
  2222 // DRMCommon::Connect
       
  2223 // Does nothing
       
  2224 // -----------------------------------------------------------------------------
       
  2225 EXPORT_C TInt DRMCommon::Connect()
       
  2226     {
       
  2227     RDRMRightsClient client; // Used to start RightsServer in bootup
       
  2228     TInt ignore = 0;				 // error will be ignored, if it fails to start the
       
  2229                              // rights server there is nothing we can really
       
  2230                              // do about it, and normally this would work and
       
  2231                              // return AOk, just used because of the SkinSrv
       
  2232                              // Bootup thing
       
  2233     ignore = client.Connect();        // Called by SkinSrv during bootup
       
  2234 		if( ignore ) 
       
  2235 		    {
       
  2236 		    // The error shouldn't matter since it will be retried	
       
  2237 		    }
       
  2238     client.Close();
       
  2239     return EOk;
       
  2240     }
       
  2241 
       
  2242 // -----------------------------------------------------------------------------
       
  2243 // DRMCommon::Disconnect
       
  2244 // Does nothing
       
  2245 // -----------------------------------------------------------------------------
       
  2246 EXPORT_C TInt DRMCommon::Disconnect()
       
  2247     {
       
  2248     return EOk;
       
  2249     }
       
  2250 
       
  2251 // -----------------------------------------------------------------------------
       
  2252 // DRMCommon::SupportedDRMMethods
       
  2253 // Returns the implementation level of the DRM system.
       
  2254 // -----------------------------------------------------------------------------
       
  2255 EXPORT_C TInt DRMCommon::SupportedDRMMethods(
       
  2256     TInt& aDRMMethod, TOMALevel& aOMALevel)
       
  2257     {
       
  2258     aDRMMethod =
       
  2259         DRMCommon::EForwardLock |
       
  2260         DRMCommon::ECombinedDelivery |
       
  2261         DRMCommon::ESeparateDelivery |
       
  2262         DRMCommon::ESuperDistribution;
       
  2263 #ifdef __DRM_OMA2
       
  2264     aOMALevel = EOMA_2_0;
       
  2265 #else
       
  2266     aOMALevel = EOMA_1_0;
       
  2267 #endif
       
  2268     return EOk;
       
  2269     }
       
  2270 
       
  2271 // -----------------------------------------------------------------------------
       
  2272 // DRMCommon::Version
       
  2273 // Returns the client side API version.
       
  2274 // -----------------------------------------------------------------------------
       
  2275 EXPORT_C TVersion DRMCommon::Version()
       
  2276     {
       
  2277     return TVersion(KClientVersionMajor, 
       
  2278         KClientVersionMinor, 
       
  2279         KClientVersionBuild);
       
  2280     }
       
  2281 
       
  2282 // -----------------------------------------------------------------------------
       
  2283 // DRMCommon::ServerVersion
       
  2284 // Returns the version of the DRM engine.
       
  2285 // -----------------------------------------------------------------------------
       
  2286 EXPORT_C TVersion DRMCommon::ServerVersion()
       
  2287     {
       
  2288     return TVersion(KServerVersionMajor, 
       
  2289         KServerVersionMinor, 
       
  2290         KServerVersionBuild);
       
  2291     }
       
  2292 
       
  2293 // -----------------------------------------------------------------------------
       
  2294 // DRMCommon::DataTypesCount
       
  2295 // Returns the number of MIME types the DRM system understands.
       
  2296 // -----------------------------------------------------------------------------
       
  2297 EXPORT_C TInt DRMCommon::DataTypesCount(
       
  2298     TInt& aCount)
       
  2299     {
       
  2300     TInt err = 0;
       
  2301     TInt staticTotal = 0;
       
  2302     TInt dynamicTotal = 0;
       
  2303     
       
  2304     err = StaticDataTypesCount(staticTotal);    
       
  2305     if (err)
       
  2306         {
       
  2307         return err;  
       
  2308         }
       
  2309     
       
  2310     err = DynamicDataTypesCount(dynamicTotal);
       
  2311     if (err)
       
  2312         {
       
  2313         return err;  
       
  2314         }
       
  2315     
       
  2316     aCount = staticTotal + dynamicTotal;
       
  2317     return err;
       
  2318     }
       
  2319 
       
  2320 // -----------------------------------------------------------------------------
       
  2321 // DRMCommon::SupportedDataType
       
  2322 // Returns a specific DRM enabled MIME type.
       
  2323 // -----------------------------------------------------------------------------
       
  2324 EXPORT_C TInt DRMCommon::SupportedDataType(
       
  2325     const TInt aIndex, 
       
  2326     TDataType& aDataType)
       
  2327     {
       
  2328     TInt err = KErrNone;
       
  2329     TInt total = 0;
       
  2330     TInt stat = 0;
       
  2331     
       
  2332     err = DataTypesCount(total);
       
  2333     if (!err) err = StaticDataTypesCount(stat);
       
  2334     if (!err)
       
  2335         {
       
  2336         if (0 <= aIndex && aIndex < stat)
       
  2337             {
       
  2338             err = SupportedStaticDataType(aIndex, aDataType);
       
  2339             }
       
  2340         else if (stat <= aIndex && aIndex < total)
       
  2341             {
       
  2342             err = SupportedDynamicDataType(aIndex - stat, aDataType);
       
  2343             }
       
  2344         else
       
  2345             {
       
  2346             err = KErrArgument;
       
  2347             }
       
  2348         }
       
  2349     
       
  2350     return err;
       
  2351     }
       
  2352 
       
  2353 // -----------------------------------------------------------------------------
       
  2354 // DRMCommon::StaticDataTypesCount
       
  2355 // Returns the number of supported built-in datatypes.
       
  2356 // -----------------------------------------------------------------------------
       
  2357 EXPORT_C TInt DRMCommon::StaticDataTypesCount(
       
  2358     TInt& aCount)
       
  2359     {
       
  2360     TInt err = 0;
       
  2361     RFs fs;
       
  2362     RResourceFile resourceFile;
       
  2363     TResourceReader theReader;
       
  2364     
       
  2365     err = fs.Connect();
       
  2366     
       
  2367     if (err)
       
  2368         {
       
  2369         fs.Close();
       
  2370         return err;
       
  2371         }
       
  2372     
       
  2373 #ifndef RD_MULTIPLE_DRIVE
       
  2374     
       
  2375     TRAP(err, resourceFile.OpenL(fs, KResourceFile));
       
  2376     
       
  2377 #else //RD_MULTIPLE_DRIVE
       
  2378     
       
  2379     TInt driveNumber( -1 );
       
  2380     TChar driveLetter;
       
  2381     DriveInfo::GetDefaultDrive( DriveInfo::EDefaultRom, driveNumber );
       
  2382     fs.DriveToChar( driveNumber, driveLetter );
       
  2383     
       
  2384     TFileName resFile;
       
  2385 	resFile.Format( KResourceFile, (TUint)driveLetter );
       
  2386     
       
  2387     TRAP(err, resourceFile.OpenL(fs, resFile));
       
  2388     
       
  2389 #endif
       
  2390     if (err)
       
  2391         {
       
  2392         fs.Close();
       
  2393         return err;
       
  2394         }
       
  2395     
       
  2396     HBufC8* res = NULL;
       
  2397     TRAP(err, (res = resourceFile.AllocReadL(DATATYPE))); 
       
  2398     if (err)
       
  2399         {
       
  2400         resourceFile.Close();
       
  2401         fs.Close();
       
  2402         return err;
       
  2403         }
       
  2404     
       
  2405     
       
  2406     theReader.SetBuffer(res);
       
  2407     aCount = theReader.ReadInt8();
       
  2408     
       
  2409     delete res;
       
  2410     res = NULL;
       
  2411     resourceFile.Close();
       
  2412     fs.Close();
       
  2413     return err;
       
  2414     }
       
  2415 
       
  2416 // -----------------------------------------------------------------------------
       
  2417 // DRMCommon::RegisterDataType
       
  2418 // Register a MIME type as being handled by the DRM system.
       
  2419 // -----------------------------------------------------------------------------
       
  2420 EXPORT_C TInt DRMCommon::RegisterDataType(
       
  2421     const TDataType& aDataType)
       
  2422     {
       
  2423     TInt total = 0;
       
  2424     TInt err = KErrNone;
       
  2425     TInt i;
       
  2426     TDataType type;
       
  2427     
       
  2428     if (aDataType.Des().Length() > 0)
       
  2429         {
       
  2430         err = DataTypesCount(total);
       
  2431         for (i = 0 ; err == KErrNone && i < total ; i++)
       
  2432             {
       
  2433             err = SupportedDataType(i, type);
       
  2434             if (type == aDataType)
       
  2435                 {
       
  2436                 err = KErrAlreadyExists;   
       
  2437                 }
       
  2438             }
       
  2439         
       
  2440         if (!err)
       
  2441             {
       
  2442             TRAP(err, RegisterDynamicDataTypeL(aDataType));
       
  2443             }
       
  2444         }
       
  2445     else
       
  2446         {
       
  2447         err = KErrArgument;  
       
  2448         }
       
  2449     return err;
       
  2450     }
       
  2451 
       
  2452 // -----------------------------------------------------------------------------
       
  2453 // DRMCommon::UnRegisterDataType
       
  2454 // Unregister a MIME type as being handled by the DRM system.
       
  2455 // -----------------------------------------------------------------------------
       
  2456 EXPORT_C TInt DRMCommon::UnRegisterDataType(
       
  2457     const TInt aIndex)
       
  2458     {
       
  2459     TInt r;
       
  2460     TInt count;
       
  2461     TInt total;
       
  2462     
       
  2463     StaticDataTypesCount(count);
       
  2464     r = DataTypesCount(total);
       
  2465     if (r == KErrNone && aIndex >= count && aIndex < total)
       
  2466         {
       
  2467         TRAP(r, UnRegisterDynamicDataTypeL(aIndex - count));
       
  2468         }
       
  2469     else
       
  2470         {
       
  2471         r = KErrArgument;
       
  2472         }
       
  2473     return r;
       
  2474     }
       
  2475 
       
  2476 
       
  2477 // -----------------------------------------------------------------------------
       
  2478 // DRMCommon::MergeParentAndChild
       
  2479 // Merge rights with their parent rights
       
  2480 // -----------------------------------------------------------------------------
       
  2481 EXPORT_C void DRMCommon::MergeParentAndChild(CDRMRights* /*aRights*/)
       
  2482     {
       
  2483     return;
       
  2484     /*
       
  2485     HBufC8* parent = aRights->GetPermission().iParentUID;
       
  2486     if (parent != NULL)
       
  2487         {
       
  2488         RPointerArray<CDRMRights>* parents;
       
  2489         if (GetDetailedContentRights(*parent, parents) == KErrNone &&
       
  2490             parents->Count() > 0)
       
  2491             {
       
  2492             TBool merged = EFalse;
       
  2493             for (TInt i = 0; !merged && i < parents->Count(); i++)
       
  2494                 {
       
  2495                 CDRMRights::TRestriction restriction;
       
  2496                 CDRMRights::TExpiration expiration;
       
  2497                 TUint32 constraints;
       
  2498                 aRights->GetRightsInfo(EUnknown, restriction, expiration, 
       
  2499                     constraints);
       
  2500                 if ( expiration == CDRMRights::EValidRights)
       
  2501                     {
       
  2502                     aRights->Merge(*(*parents)[i]);
       
  2503                     merged = ETrue;
       
  2504                     }
       
  2505                 }
       
  2506             parents->ResetAndDestroy();
       
  2507             delete parents;
       
  2508             }
       
  2509         }
       
  2510     */    
       
  2511     }
       
  2512  
       
  2513  
       
  2514  
       
  2515 
       
  2516 // -----------------------------------------------------------------------------
       
  2517 // GetFileHandle
       
  2518 // Get a file name trying to open it in the order required, which is:
       
  2519 // 1)  EFileShareReadersOrWriters
       
  2520 // 2)  EFileShareAny
       
  2521 // 3)  EFileShareReadersOnly
       
  2522 // -----------------------------------------------------------------------------
       
  2523 LOCAL_C TInt GetFileHandleRead(
       
  2524     RFs& aFileServer, 
       
  2525     RFile& aFile, 
       
  2526     const TDesC& aFileName )
       
  2527     {
       
  2528     TInt error = KErrNone;
       
  2529 
       
  2530     // 1) Try to open in EFileShareReadersOrWriters
       
  2531     error = aFile.Open( aFileServer, aFileName, EFileRead | EFileShareReadersOrWriters );
       
  2532     if( error != KErrNone )
       
  2533         {
       
  2534         // 2) Try to open in EFileShareAny
       
  2535         error = aFile.Open( aFileServer, aFileName, EFileRead | EFileShareAny );
       
  2536         if( error != KErrNone )
       
  2537             {
       
  2538             // 3) Try to open in EFileShareReadersOnly
       
  2539             error = aFile.Open( aFileServer, aFileName, EFileRead | EFileShareReadersOnly );            
       
  2540             }
       
  2541         }
       
  2542     return error;   
       
  2543     };
       
  2544 
       
  2545 // -----------------------------------------------------------------------------
       
  2546 // GetFileHandle
       
  2547 // Get a file name trying to open it in the order required, which is:
       
  2548 // 1)  EFileShareReadersOrWriters
       
  2549 // 2)  EFileShareAny
       
  2550 // 3)  EFileShareReadersOnly
       
  2551 // -----------------------------------------------------------------------------
       
  2552 LOCAL_C void AddParents( DRMCommon* aDrmCommon, 
       
  2553                          RPointerArray<CDRMRights>& aRights ) 
       
  2554     {
       
  2555     HBufC8* parent = NULL;
       
  2556     RPointerArray<CDRMRights>* parents = NULL;
       
  2557     TInt error = KErrNone;
       
  2558     RPointerArray<HBufC8> usedParents;
       
  2559     
       
  2560     
       
  2561     for( TInt i = 0; i < aRights.Count(); i++ )
       
  2562         {
       
  2563         parent = aRights[i]->GetPermission().iParentUID;
       
  2564         
       
  2565         for(TInt counter = 0; counter < usedParents.Count(); counter++ )
       
  2566             {
       
  2567             if( parent && !usedParents[counter]->Compare( *parent ) )
       
  2568                 {
       
  2569                 parent = NULL;
       
  2570                 }
       
  2571             }
       
  2572         
       
  2573         if( parent != NULL )
       
  2574             {
       
  2575             TRAP( error, usedParents.AppendL( parent ) );
       
  2576             
       
  2577             if( aDrmCommon->GetDetailedContentRights(parent->Des(), parents) == KErrNone )
       
  2578                 {
       
  2579                 for( TInt j = parents->Count()-1; j >= 0;j-- )
       
  2580                     { 
       
  2581                     TRAP( error, aRights.AppendL( (*parents)[j] ) );
       
  2582                     (*parents)[j] = NULL;
       
  2583                     parents->Remove(j);
       
  2584                     }
       
  2585                 parents->ResetAndDestroy();    
       
  2586                 delete parents;
       
  2587                 parents = NULL;    
       
  2588                 }
       
  2589             
       
  2590             }
       
  2591         }
       
  2592     usedParents.Reset();
       
  2593     usedParents.Close();        
       
  2594     }
       
  2595 
       
  2596 // -----------------------------------------------------------------------------
       
  2597 // DRMCommon::MapErrorCode
       
  2598 // Remaps DRM core specific error codes to DRM API error codes.
       
  2599 // -----------------------------------------------------------------------------
       
  2600 EXPORT_C TInt DRMCommon::MapErrorCode(
       
  2601     const TInt /* aCode */)
       
  2602     {
       
  2603     return EUnsupported;
       
  2604     }
       
  2605 
       
  2606 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  2607 
       
  2608 //  End of File