omadrm/drmengine/dcf/src/Oma1Dcf.cpp
changeset 0 95b198f216e5
child 12 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32base.h>
       
    22 #include <f32file.h>
       
    23 #include <utf.h>
       
    24 #include <WSPDecoder.h>
       
    25 #include "Oma1Dcf.h"
       
    26 #include "DRMRightsClient.h"
       
    27 #include <centralrepository.h>
       
    28 #include <UTF.h>
       
    29 
       
    30 #include "DrmUtilityInternalcrkeys.h"      // Cenrep extension for OmaBased
       
    31 
       
    32 // LOCAL CONSTANTS AND MACROS
       
    33 _LIT8(KMimeApplication, "application");
       
    34 _LIT8(KMimeText, "text");
       
    35 _LIT8(KMimeAudio, "audio");
       
    36 _LIT8(KMimeImage, "image");
       
    37 _LIT8(KMimeVideo, "video");
       
    38 _LIT8(KMimeModel, "model");
       
    39 _LIT8(KMimeMessage, "message");
       
    40 _LIT8(KMimeMultipart, "multipart");
       
    41 _LIT8(KXHeader, "x-");
       
    42 _LIT8(KColon, ":");
       
    43 //_LIT8(KSemiColon, ";");
       
    44 //_LIT8(KNewLine, "\n");
       
    45 _LIT8(KEndLine, "\r\n");
       
    46 
       
    47 const TInt KCenRepDataLength( 50 );
       
    48 
       
    49 // ============================ MEMBER FUNCTIONS ===============================
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // COma1Dcf::COma1Dcf
       
    53 // C++ default constructor can NOT contain any code, that
       
    54 // might leave.
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 COma1Dcf::COma1Dcf():
       
    58     iHeaders(NULL)
       
    59     {
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // COma1Dcf::ConstructL
       
    64 // Symbian 2nd phase constructor can leave.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void COma1Dcf::ConstructL(const RFile& aFile)
       
    68     {
       
    69     CDcfCommon::ConstructL(aFile);
       
    70     User::LeaveIfError(ReadHeaderL());
       
    71     }
       
    72 
       
    73 void COma1Dcf::ConstructL(const TDesC8& aMemoryBlock)
       
    74     {
       
    75     User::LeaveIfError(ReadHeaderL(aMemoryBlock));
       
    76     iData = aMemoryBlock.AllocL();
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // COma1Dcf::FetchOmaBasedInfoL
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 HBufC8* COma1Dcf::FetchOmaBasedInfoL()
       
    84     {
       
    85     CRepository* repository( NULL );
       
    86     RBuf bOmaBasedMimeType;
       
    87     HBufC8* mimetype = NULL;
       
    88 
       
    89     CleanupClosePushL(bOmaBasedMimeType);
       
    90     bOmaBasedMimeType.CreateL( KCenRepDataLength );    
       
    91     
       
    92     repository = CRepository::NewL( KCRUidOmaBased );
       
    93 
       
    94     CleanupStack::PushL( repository );
       
    95 
       
    96     User::LeaveIfError(repository->Get( KOmaBasedMimeType, bOmaBasedMimeType ));
       
    97     mimetype = CnvUtfConverter::ConvertFromUnicodeToUtf8L( bOmaBasedMimeType );
       
    98     CleanupStack::PopAndDestroy( repository );
       
    99     
       
   100     CleanupStack::PopAndDestroy();
       
   101     
       
   102     return mimetype;
       
   103     
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // COma1Dcf::NewL
       
   108 // Two-phased constructor.
       
   109 // -----------------------------------------------------------------------------
       
   110 //
       
   111 EXPORT_C COma1Dcf* COma1Dcf::NewL(const RFile& aFile)
       
   112     {
       
   113     COma1Dcf* self = new(ELeave) COma1Dcf;
       
   114     
       
   115     CleanupStack::PushL(self);
       
   116     self->ConstructL(aFile);
       
   117     CleanupStack::Pop();
       
   118 
       
   119     return self;
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // COma1Dcf::
       
   124 // 
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C COma1Dcf* COma1Dcf::NewL(const TDesC8& aMemoryBlock)
       
   128     {
       
   129     COma1Dcf* self = new(ELeave) COma1Dcf;
       
   130     
       
   131     CleanupStack::PushL(self);
       
   132     self->ConstructL(aMemoryBlock);
       
   133     CleanupStack::Pop();
       
   134 
       
   135     return self;
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // COma1Dcf::
       
   140 // 
       
   141 // -----------------------------------------------------------------------------
       
   142 //
       
   143 EXPORT_C COma1Dcf::~COma1Dcf()
       
   144     {
       
   145     delete iHeaders;
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // COma1Dcf::
       
   150 // 
       
   151 // -----------------------------------------------------------------------------
       
   152 //
       
   153 TInt COma1Dcf::ReadHeaderL(const TDesC8& aMemoryBlock)
       
   154     {
       
   155     TInt r = KErrNone;
       
   156     TInt cidLength = 0;
       
   157     TInt mimeLength = 0;
       
   158     TInt fieldLength = 0;
       
   159     TBuf8<10> lengthFields;
       
   160     TUint32 length;
       
   161     TPtr8 ptr(NULL,0,0);
       
   162     
       
   163     if (aMemoryBlock.Length()<3)
       
   164         {
       
   165         User::Leave(KErrArgument);
       
   166         }
       
   167         
       
   168     iVersion = aMemoryBlock[0];
       
   169     
       
   170     if (iVersion!=1)
       
   171         {
       
   172         // for OMA Version 1, DCF version must be 1
       
   173         User::Leave(KErrArgument);
       
   174         }
       
   175         
       
   176     mimeLength = aMemoryBlock[1];
       
   177     cidLength = aMemoryBlock[2];
       
   178     
       
   179     if (mimeLength + cidLength + 3 + 5 + 5 > aMemoryBlock.Length())
       
   180         {
       
   181         User::Leave(KErrArgument);
       
   182         }
       
   183 
       
   184     
       
   185     if (mimeLength!=0)
       
   186         {
       
   187         iMimeType = aMemoryBlock.Mid(3, mimeLength).AllocL();
       
   188         ptr.Set(iMimeType->Des());
       
   189         r = ptr.FindF(_L8("/"));
       
   190         if (r==KErrNotFound)
       
   191             {
       
   192             User::Leave(KErrArgument);
       
   193             }
       
   194         }
       
   195     else
       
   196         {
       
   197         User::Leave(KErrArgument);
       
   198         }
       
   199     
       
   200     
       
   201     if (cidLength!=0)
       
   202         {
       
   203         iContentID = aMemoryBlock.Mid(3 + mimeLength, cidLength).AllocL();
       
   204         }
       
   205     else
       
   206         {
       
   207         User::Leave(KErrArgument);    
       
   208         }
       
   209 
       
   210 
       
   211     lengthFields.Copy(aMemoryBlock.Mid(3 + mimeLength + cidLength, 10));
       
   212     TWspPrimitiveDecoder decoder(lengthFields);
       
   213     fieldLength = decoder.UintVar(length);
       
   214     if( fieldLength < 0 ) 
       
   215         {
       
   216         User::Leave(KErrArgument);
       
   217         }
       
   218     iHeaderLength = length;
       
   219     fieldLength += decoder.UintVar(length);
       
   220     if( fieldLength < 0 )
       
   221         {
       
   222         User::Leave(KErrArgument);
       
   223         }
       
   224     
       
   225     iDataLength = length;
       
   226     iOffset = 3 + mimeLength + cidLength + fieldLength + iHeaderLength;
       
   227     if (iDataLength == 0)
       
   228         {
       
   229         iDataLength = aMemoryBlock.Length() - iOffset;
       
   230         }
       
   231     iPlainTextLength = iDataLength - KDCFKeySize;
       
   232     iPlainTextLengthValid = EFalse;
       
   233 
       
   234     if (mimeLength + cidLength + 3 + 5 + 5 + iHeaderLength <=
       
   235         aMemoryBlock.Length())
       
   236         {
       
   237         iHeaders = aMemoryBlock.Mid(3 + mimeLength + cidLength +
       
   238                                     fieldLength, iHeaderLength).AllocL();
       
   239 
       
   240         ReadHeaderValuesL();
       
   241         }
       
   242     return r;
       
   243     }
       
   244 
       
   245 // -----------------------------------------------------------------------------
       
   246 // COma1Dcf::ReadHeaderValuesL
       
   247 // 
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 void COma1Dcf::ReadHeaderValuesL(void)
       
   251     {
       
   252     TPtrC8 ptr(NULL, 0);
       
   253     
       
   254     if (GetHeaderL(KRightsIssuer, ptr) == KErrNone)
       
   255         {
       
   256         if( iRightsIssuerURL )
       
   257             {
       
   258             delete iRightsIssuerURL;
       
   259             iRightsIssuerURL = NULL;
       
   260             }
       
   261         iRightsIssuerURL = ptr.AllocL();
       
   262         }
       
   263     if (GetHeaderL(KContentName, ptr) == KErrNone)
       
   264         {
       
   265         if( iTitle ) 
       
   266             {
       
   267             delete iTitle;
       
   268             iTitle = NULL;
       
   269             }        
       
   270         iTitle = ptr.AllocL();
       
   271         }
       
   272     if (GetHeaderL(KContentDescription, ptr) == KErrNone)
       
   273         {
       
   274         if( iDescription )
       
   275             {
       
   276             delete iDescription;
       
   277             iDescription = NULL;
       
   278             }          
       
   279         iDescription = ptr.AllocL();
       
   280         }
       
   281     if (GetHeaderL(KIconURI, ptr) == KErrNone)
       
   282         {
       
   283         if( iIconUri )
       
   284             {
       
   285             delete iIconUri;
       
   286             iIconUri = NULL;
       
   287             }         
       
   288         iIconUri = ptr.AllocL();
       
   289         }
       
   290     }
       
   291 
       
   292 // -----------------------------------------------------------------------------
       
   293 // COma1Dcf::
       
   294 // 
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 TInt COma1Dcf::ReadHeaderL(void)
       
   298     {
       
   299     TInt r = KErrNone;
       
   300     TInt pos = 0;
       
   301     TUint8 v = 0;
       
   302     TPtr8 p(&v, 1, 1);
       
   303     TInt cidLength = 0;
       
   304     TInt mimeLength = 0;
       
   305     TInt fieldLength = 0;
       
   306     TBuf8<10> lengthFields;
       
   307     TUint32 length;
       
   308     TInt size;
       
   309 
       
   310     iFile.Size(size);
       
   311     if (size<3)
       
   312         {
       
   313         User::Leave(KErrArgument);
       
   314         }
       
   315     iFile.Seek(ESeekStart, pos);
       
   316 
       
   317     iFile.Read(p);
       
   318     iVersion = v;
       
   319     if (iVersion!=1)
       
   320         {
       
   321         // for OMA Version 1, DCF version must be 1
       
   322         User::Leave(KErrArgument);
       
   323         }
       
   324 
       
   325     iFile.Read(p);
       
   326     mimeLength = v;
       
   327     if (mimeLength==0)
       
   328         {
       
   329         User::Leave(KErrArgument);
       
   330         }
       
   331 
       
   332     iFile.Read(p);
       
   333     cidLength = v;
       
   334     if (cidLength==0)
       
   335         {
       
   336         User::Leave(KErrArgument);
       
   337         }
       
   338 
       
   339     iMimeType = HBufC8::NewMax(mimeLength);
       
   340     User::LeaveIfNull(iMimeType);
       
   341     p.Set(iMimeType->Des());
       
   342     iFile.Read(p, mimeLength);
       
   343     p.Set(iMimeType->Des());
       
   344     r = p.FindF(_L8("/"));
       
   345     if (r==KErrNotFound)
       
   346         {
       
   347         User::Leave(KErrArgument);
       
   348         }
       
   349 
       
   350     iContentID = HBufC8::NewMax(cidLength);
       
   351     User::LeaveIfNull(iContentID);
       
   352     p.Set(iContentID->Des());
       
   353     iFile.Read(p, cidLength);
       
   354     
       
   355     pos = 0;
       
   356     iFile.Seek(ESeekCurrent, pos);
       
   357     iFile.Read(lengthFields);
       
   358     TWspPrimitiveDecoder decoder(lengthFields);
       
   359     fieldLength = decoder.UintVar(length);
       
   360     if( fieldLength < 0 )
       
   361         {
       
   362         User::Leave(KErrArgument);
       
   363         }
       
   364     iHeaderLength = length;
       
   365     fieldLength += decoder.UintVar(length);
       
   366     if( fieldLength < 0 )
       
   367         {
       
   368         User::Leave(KErrArgument);
       
   369         }
       
   370     
       
   371     iDataLength = length;
       
   372     iOffset = pos + fieldLength + iHeaderLength;
       
   373     if (iDataLength == 0)
       
   374         {
       
   375         iDataLength = size - iOffset;
       
   376         }
       
   377     iPlainTextLength = iDataLength - KDCFKeySize;
       
   378     iPlainTextLengthValid = EFalse;
       
   379 
       
   380     pos = pos + fieldLength;
       
   381     iFile.Seek(ESeekStart, pos);
       
   382     iHeaders = HBufC8::NewL(iHeaderLength);
       
   383     p.Set(iHeaders->Des());
       
   384     iFile.Read(p, iHeaderLength);
       
   385 
       
   386     ReadHeaderValuesL();
       
   387 
       
   388     return r;
       
   389     }
       
   390 
       
   391 // -----------------------------------------------------------------------------
       
   392 // COma1Dcf::
       
   393 // 
       
   394 // -----------------------------------------------------------------------------
       
   395 //
       
   396 EXPORT_C TBool COma1Dcf::IsValidDcf(const TDesC8& aDcfFragment)
       
   397     {
       
   398     TPtrC8 mimeType;
       
   399     TInt i;
       
   400     TBool r = EFalse;
       
   401     TInt error = KErrNone;
       
   402     HBufC8* omaBasedBuf = NULL;
       
   403 
       
   404     if (aDcfFragment.Length() > 15)
       
   405         {
       
   406         i = aDcfFragment.Right(aDcfFragment.Length() - 3).Locate('/');
       
   407         if (aDcfFragment[0] == 1 && i != KErrNotFound)
       
   408             {
       
   409             mimeType.Set(aDcfFragment.Mid(3, i));
       
   410             if (mimeType.CompareF(KMimeImage) == 0 ||
       
   411                 mimeType.CompareF(KMimeAudio) == 0 ||
       
   412                 mimeType.CompareF(KMimeApplication) == 0 ||
       
   413                 mimeType.CompareF(KMimeText) == 0 ||
       
   414                 mimeType.CompareF(KMimeModel) == 0 ||
       
   415                 mimeType.CompareF(KMimeMessage) == 0 ||
       
   416                 mimeType.CompareF(KMimeVideo) == 0 ||
       
   417                 mimeType.CompareF(KMimeMultipart) == 0 ||
       
   418                 aDcfFragment.Mid(3, 2).CompareF(KXHeader) == 0)
       
   419                 {
       
   420                 r = ETrue;
       
   421                 // Check for specific mimetype
       
   422                 
       
   423                 TRAP( error, omaBasedBuf = FetchOmaBasedInfoL() );
       
   424                 
       
   425                 if( !error )
       
   426                     {
       
   427                     mimeType.Set(aDcfFragment.Mid(3, 
       
   428                             omaBasedBuf->Length()));
       
   429                     
       
   430                 
       
   431                     if (mimeType.CompareF( *omaBasedBuf ) == 0)
       
   432                         {
       
   433                         r = EFalse;
       
   434                         }
       
   435                     }
       
   436                 delete omaBasedBuf;
       
   437                 }
       
   438             }
       
   439         }
       
   440     return r;
       
   441     }
       
   442 
       
   443 // -----------------------------------------------------------------------------
       
   444 // COma1Dcf::
       
   445 // 
       
   446 // -----------------------------------------------------------------------------
       
   447 //
       
   448 TInt COma1Dcf::CheckUniqueId(const TDesC& aUniqueId)
       
   449 	{
       
   450 	TInt r = CDcfCommon::CheckUniqueId(aUniqueId);
       
   451 	
       
   452 	if (r == KErrNotFound)
       
   453 	    {
       
   454 	    HBufC8* id = NULL;
       
   455     	TRAPD(err, id = CnvUtfConverter::ConvertFromUnicodeToUtf8L(aUniqueId));
       
   456     	if (err == KErrNone)
       
   457     	    {
       
   458     	    if (iContentID->Compare(*id) == 0)
       
   459     		    {
       
   460     		    r = 0;
       
   461     		    }
       
   462             }
       
   463         else
       
   464             {
       
   465             r = err;
       
   466             }
       
   467     	delete id;
       
   468     	}
       
   469 	return r;
       
   470 	}
       
   471 	
       
   472 // -----------------------------------------------------------------------------
       
   473 // COma1Dcf::
       
   474 // 
       
   475 // -----------------------------------------------------------------------------
       
   476 //
       
   477 TInt COma1Dcf::OpenPart(const TDesC& aUniqueId)
       
   478 	{
       
   479 	return OpenPart(CheckUniqueId(aUniqueId));
       
   480 	}
       
   481 	
       
   482 // -----------------------------------------------------------------------------
       
   483 // COma1Dcf::
       
   484 // 
       
   485 // -----------------------------------------------------------------------------
       
   486 //
       
   487 TInt COma1Dcf::OpenPart(TInt aPart)
       
   488 	{
       
   489 	if (aPart == 0)
       
   490 		{
       
   491 		return KErrNone;
       
   492 		}
       
   493 	else
       
   494 		{
       
   495 		return KErrNotFound;
       
   496 		}
       
   497 	}
       
   498 	
       
   499 // -----------------------------------------------------------------------------
       
   500 // COma1Dcf::
       
   501 // 
       
   502 // -----------------------------------------------------------------------------
       
   503 //
       
   504 void COma1Dcf::GetPartIdsL(RPointerArray<HBufC8>& aPartList)
       
   505     {
       
   506     aPartList.ResetAndDestroy();
       
   507     aPartList.AppendL(iContentID->Des().AllocL());
       
   508     }
       
   509 	
       
   510 // -----------------------------------------------------------------------------
       
   511 // COma1Dcf::GetHeaderL
       
   512 // 
       
   513 // -----------------------------------------------------------------------------
       
   514 //
       
   515 EXPORT_C TInt COma1Dcf::GetHeaderL(
       
   516     const TDesC8& aHeaderName,
       
   517     TPtrC8& aHeaderValue)
       
   518     {
       
   519     TInt i;
       
   520     TInt j;
       
   521     TPtrC8 ptr( iHeaders->Des() );
       
   522     
       
   523     // Add Room for CRLF and Semicolon:
       
   524     HBufC8* buffer = HBufC8::NewMaxLC( aHeaderName.Length() + 3 );  
       
   525     TPtr8 searchBuf( const_cast<TUint8*>(buffer->Ptr()), 0, buffer->Des().MaxSize() );  
       
   526 
       
   527     searchBuf.Copy(aHeaderName);
       
   528     searchBuf.Append(KColon);
       
   529     
       
   530     // First see if the     
       
   531     i = ptr.Find(searchBuf);
       
   532     if( i < 0 )
       
   533         {
       
   534         CleanupStack::PopAndDestroy(); // buffer
       
   535         return KErrNotFound; 
       
   536         }
       
   537         
       
   538    if( i > 0 )
       
   539         {
       
   540         // if it's not the first one, use the search buffer:
       
   541         // Create the search buffer
       
   542         searchBuf.Copy(KEndLine);
       
   543         searchBuf.Append(aHeaderName);
       
   544         searchBuf.Append(KColon);
       
   545     
       
   546         // First see if the     
       
   547         i = ptr.Find(searchBuf);
       
   548         if ( i < 0 )
       
   549             {
       
   550             CleanupStack::PopAndDestroy(); // buffer            
       
   551             return KErrNotFound;
       
   552             }        
       
   553         }
       
   554     // Move search buffer    
       
   555     i += searchBuf.Length();  
       
   556     
       
   557     j = ptr.Mid(i).Find(KEndLine);
       
   558     if( j < 0 )
       
   559         {
       
   560         CleanupStack::PopAndDestroy(); // buffer        
       
   561         return KErrNotFound;
       
   562         }
       
   563         
       
   564     aHeaderValue.Set( ptr.Mid(i, j) );  
       
   565     
       
   566     CleanupStack::PopAndDestroy(); // buffer
       
   567     return KErrNone;
       
   568     }
       
   569 
       
   570 //  End of File