omadrm/drmengine/agentv2/inc/DcfCache.h
changeset 0 95b198f216e5
child 12 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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:  Declaration of the DCF cache class
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #ifndef DCFCACHE_H
       
    21 #define DCFCACHE_H
       
    22 
       
    23 //  INCLUDES
       
    24 #include <dcfcommon.h>
       
    25 #include <mode.h>
       
    26 #include <symmetric.h>
       
    27 #include <rijndael.h>
       
    28 
       
    29 // enables async reading
       
    30 #define ASYNC_READ
       
    31 
       
    32 // FORWARD DECLARATIONS
       
    33 
       
    34 class RFile;
       
    35 class RDRMRightsClient;
       
    36 class CDcfCommon;
       
    37 
       
    38 // CLASS DECLARATION
       
    39 
       
    40 /**
       
    41  *  Cache for reading decrypted or plain data from a DCF file
       
    42  *
       
    43  *  @lib OmaDrmAgent.dll
       
    44  *  @since Series 60 3.0
       
    45  */
       
    46 
       
    47 class CDcfCache : public
       
    48 #ifdef ASYNC_READ
       
    49     CActive
       
    50 #else
       
    51     CBase
       
    52 #endif
       
    53     {
       
    54 
       
    55 public:
       
    56     // Constructors and destructor
       
    57 
       
    58     /**
       
    59      * Two-phased constructor.
       
    60      */
       
    61     static CDcfCache* NewL(
       
    62         RDRMRightsClient& aRightsClient,
       
    63         RFile& aFile,
       
    64         CDcfCommon& aDcf,
       
    65         TInt aPageSize = 2048,
       
    66         TInt aPageCount = 32 );
       
    67 
       
    68     /**
       
    69      * Destructor.
       
    70      */
       
    71     virtual ~CDcfCache();
       
    72 
       
    73 public:
       
    74     // New functions
       
    75 
       
    76     /**
       
    77      * Read data from the file via the cache. Depending on the decryption
       
    78      * mode, the data is decrypted in the RightsServer or in the cache
       
    79      * @since Series 60 3.0
       
    80      * @param aPos Position from the start of the file where to read from
       
    81      * @param aDes Descriptor holding the read data
       
    82      * @param aLength Amount of data to read
       
    83      * @return KErrNone if the data is read successfully
       
    84      */
       
    85     TInt Read(
       
    86         TInt& aPos,
       
    87         TDes8& aDes,
       
    88         TInt aLength );
       
    89 
       
    90     /**
       
    91      * Set the decryption key to be used if decryption shall happen in the
       
    92      * cache itself.
       
    93      * @since Series 60 3.0
       
    94      * @param aKey AES128 decryption key
       
    95      * @return ?description
       
    96      */
       
    97     void SetKey(
       
    98         const TDesC8& aKey );
       
    99 
       
   100 #ifdef ASYNC_READ
       
   101 
       
   102     TInt Read(
       
   103         TInt aPos,
       
   104         TDes8& aDes,
       
   105         TInt aLength,
       
   106         TRequestStatus& aStatus );
       
   107 
       
   108     void ReadCancel( TRequestStatus& aStatus );
       
   109 #endif
       
   110 
       
   111 protected:
       
   112     // New functions
       
   113 
       
   114     void CachedReadL(
       
   115         TInt& aPos,
       
   116         TDes8& aDes,
       
   117         TInt aLength );
       
   118 
       
   119     void UncachedReadL(
       
   120         TInt& aPos,
       
   121         TDes8& aDes,
       
   122         TInt aLength );
       
   123 
       
   124     /**
       
   125      * Check if a file position is in the give cache page
       
   126      * @since Series 60 3.0
       
   127      * @param aPage Page to check
       
   128      * @param aPosition Position to check
       
   129      * @return ETrue if the position is in the cache page
       
   130      */
       
   131     TBool InPage( TInt aPage, TInt aPosition );
       
   132 
       
   133     /**
       
   134      * Return the index of a free cache page
       
   135      * @since Series 60 3.0
       
   136      * @return Index of a free cache page
       
   137      */
       
   138     TInt GetFreePage();
       
   139 
       
   140     /**
       
   141      * Read a page of plain data for a given file position into the cache
       
   142      * @since Series 60 3.0
       
   143      * @param aPage Page to read
       
   144      * @param aPosition Position to read
       
   145      */
       
   146     void ReadPageL( TInt aPage, TInt aPosition );
       
   147 
       
   148     /**
       
   149      * Read a page of data for a given file position into the cache and
       
   150      * decrypt it
       
   151      * @since Series 60 3.0
       
   152      * @param aPage Page to read
       
   153      * @param aPosition Position to read
       
   154      */
       
   155     void ReadAndDecryptPageL( TInt aPage, TInt aPosition );
       
   156 
       
   157     /**
       
   158      * Copy data from a cache page into a descriptor
       
   159      * @since Series 60 3.0
       
   160      * @param aPage Page from which to read
       
   161      * @param aDes Decscriptor where the data is stored
       
   162      * @param aPosition Position in the file, updated with the new
       
   163      *                  position after the data has been read
       
   164      * @param aLength Amount of data to be read, updated with the actual
       
   165      *                amount that was read
       
   166      */
       
   167     void CopyOut( TInt aPage, TDes8& aDes, TInt& aPosition, TInt& aLength );
       
   168 
       
   169     /**
       
   170      * Decrypt a memory buffer using either the stored key or the
       
   171      * RightsServer.
       
   172      * @since Series 60 3.0
       
   173      * @param aIv AES CBC initialization vector
       
   174      * @param aPtr Data to be decrypted
       
   175      */
       
   176     void DecryptL( const TDesC8& aIv, TPtr8& aPtr );
       
   177 
       
   178 #ifdef ASYNC_READ
       
   179     void ReadAsyncL();
       
   180 #endif
       
   181 
       
   182 protected:
       
   183     // Functions from base classes
       
   184 
       
   185 #ifdef ASYNC_READ
       
   186 
       
   187     /**
       
   188      * From CActive
       
   189      * @since Series60 3.0
       
   190      */
       
   191     void RunL();
       
   192 
       
   193     /**
       
   194      * From CActive
       
   195      * @since Series60 3.0
       
   196      */
       
   197     void DoCancel();
       
   198 
       
   199     /**
       
   200      * From CActive
       
   201      */
       
   202     TInt RunError( TInt aError );
       
   203 #endif
       
   204 
       
   205 private:
       
   206 
       
   207     /**
       
   208      * C++ default constructor.
       
   209      */
       
   210     CDcfCache(
       
   211         RDRMRightsClient& aRightsClient,
       
   212         RFile& aFile,
       
   213         CDcfCommon& aDcf,
       
   214         TInt aPageSize,
       
   215         TInt aPageCount );
       
   216 
       
   217     /**
       
   218      * By default Symbian 2nd phase constructor is private.
       
   219      */
       
   220     void ConstructL();
       
   221 
       
   222 protected:
       
   223     // Types and enumerations
       
   224 
       
   225     enum TDecryptionMode
       
   226         {
       
   227         EServerSide,
       
   228         EClientSide,
       
   229         };
       
   230 
       
   231 #ifdef ASYNC_READ
       
   232 
       
   233     enum TReadMode
       
   234         {
       
   235         ENonPosRead,
       
   236         EPosRead,
       
   237         };
       
   238 
       
   239 #endif
       
   240 protected:
       
   241     // Data
       
   242     RFile& iFile;
       
   243     RDRMRightsClient& iRightsClient;
       
   244     CDcfCommon& iDcf;
       
   245     TInt iPageSize;
       
   246     TInt iPageCount;
       
   247     RPointerArray<TUint8> iPage;
       
   248     RArray<TInt> iPageUsageCount;
       
   249     RArray<TInt> iPagePosition;
       
   250     TEncryptionMethod iEncryptionMethod;
       
   251     TBuf8<KDCFKeySize> iKey;
       
   252     TDecryptionMode iDecryptionMode;
       
   253 
       
   254 #ifdef ASYNC_READ
       
   255     TRequestStatus* iAsyncStatus;
       
   256     TInt iPos;
       
   257     TDes8* iDes;
       
   258     TInt iLength;
       
   259     TReadMode iOperation;
       
   260     TBool iAsyncReadingOngoing;
       
   261     TInt iError;
       
   262 #endif
       
   263     };
       
   264 
       
   265 #endif      // DCFCACHE_H
       
   266 // End of File