omadrm/drmengine/roap/src/RoapParser.cpp
changeset 0 95b198f216e5
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002-2008 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:  ROAP response parser
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32base.h>
       
    22 
       
    23 #include "DeviceHello.h"
       
    24 #include "JoinDomainReq.h"
       
    25 #include "JoinDomainResp.h"
       
    26 #include "LeaveDomainReq.h"
       
    27 #include "LeaveDomainResp.h"
       
    28 #ifdef RD_DRM_METERING
       
    29 #include "MeteringReportReq.h"
       
    30 #include "MeteringReportResp.h"
       
    31 #include "MeteringReportRespParser.h"
       
    32 #endif
       
    33 #include "RIHello.h"
       
    34 #include "RegistrationReq.h"
       
    35 #include "RegistrationResp.h"
       
    36 #include "RightsReq.h"
       
    37 #include "RightsResp.h"
       
    38 #include "RoapTrigger.h"
       
    39 #include "RoapMessage.h"
       
    40 
       
    41 #include "RoapParser.h"
       
    42 #include "RespParser.h"
       
    43 #include "JoinDomainRespParser.h"
       
    44 #include "LeaveDomainRespParser.h"
       
    45 #include "RIHelloParser.h"
       
    46 #include "RegistrationRespParser.h"
       
    47 #include "RightsRespParser.h"
       
    48 #include "RoapTriggerParser.h"
       
    49 #include "RoapLog.h"
       
    50 
       
    51 using namespace Roap;
       
    52 
       
    53 // LOCAL CONSTANTS AND MACROS
       
    54 #define ELEMENT_COUNT(x) static_cast<TInt>((sizeof(x) / sizeof (x[0])))
       
    55 
       
    56 const TInt KParserChunkSize = 512;
       
    57 const TInt KRoapElementCount = ELast;
       
    58 const TInt KMaxElementNesting = 24;
       
    59 
       
    60 struct TRoapElements
       
    61     {
       
    62     const TText8* iString;
       
    63     TInt iNumber;
       
    64     };
       
    65 
       
    66 struct TStackState
       
    67     {
       
    68     TParserStackState iState;
       
    69     TRoapElementEnum iStack[KMaxElementNesting];
       
    70     };
       
    71 
       
    72 static const TRoapElements KStatusValues[] =
       
    73     {
       
    74     {_S8("Success"), ESuccess},
       
    75     {_S8("UnknownError"), EUnknownError},
       
    76     {_S8("Abort"), EAbort},
       
    77     {_S8("NotSupported"), ENotSupported},
       
    78     {_S8("AccessDenied"), Roap::EAccessDenied},
       
    79     {_S8("NotFound"), ENotFound},
       
    80     {_S8("MalformedRequest"), EMalformedRequest},
       
    81     {_S8("UnknownRequest"), EUnknownRequest},
       
    82     {_S8("UnknownCriticalExtension"), EUnknownCriticalExtension},
       
    83     {_S8("UnsupportedVersion"), EUnsupportedVersion},
       
    84     {_S8("UnsupportedAlgorithm"), EUnsupportedAlgorithm},
       
    85     {_S8("NoCertificateChain"), ENoCertificateChain},
       
    86     {_S8("InvalidCertificateChain"), EInvalidCertificateChain},
       
    87     {_S8("TrustedRootCertificateNotPresent"), ETrustedRootCertificateNotPresent},
       
    88     {_S8("SignatureError"), ESignatureError},
       
    89     {_S8("DeviceTimeError"), EDeviceTimeError},
       
    90     {_S8("NotRegistered"), ENotRegistered},
       
    91     {_S8("InvalidDCFHash"), EInvalidDCFHash},
       
    92     {_S8("InvalidDomain"), EInvalidDomain},
       
    93     {_S8("DomainFull"), EDomainFull},
       
    94     {_S8("MalformedMeteringReport"), EMalformedMeteringReport},
       
    95     {_S8("UnableToDecryptMeteringReport"), EUnableToDecryptMeteringReport},
       
    96     {_S8("UnableToValidateMeteringReportMAC"), EUnableToValidateMeteringReportMAC},
       
    97     };
       
    98 
       
    99 static const TRoapElements KRoapElements[ELast] =
       
   100     {
       
   101     {_S8("certificate"), ECertificate},
       
   102     {_S8("certificateChain"), ECertificateChain},
       
   103     {_S8("CipherData"), ECipherData},
       
   104     {_S8("CipherValue"), ECipherValue},
       
   105     {_S8("contentID"), Roap::EContentID},
       
   106     {_S8("dcfHash"), EDcfHash},
       
   107     {_S8("deviceDetails"), EDeviceDetails},
       
   108     {_S8("deviceID"), EDeviceID},
       
   109     {_S8("DigestValue"), EDigestValue},
       
   110     {_S8("dn"), EDomainName},
       
   111     {_S8("domainAlias"), EDomainAlias},
       
   112     {_S8("domainID"), EDomainID},
       
   113     {_S8("domainInfo"), EDomainInfo},
       
   114     {_S8("domainKey"), EDomainKey},
       
   115     {_S8("encKey"), EEncKey},
       
   116     {_S8("EncryptionMethod"), EEncryptionMethod},
       
   117     {_S8("extension"), EExtension},
       
   118     {_S8("extensions"), EExtensions},
       
   119     {_S8("hash"), EHash},
       
   120     {_S8("id"), EId},
       
   121     {_S8("identifier"), EIdentifier},
       
   122     {_S8("joinDomain"), EJoinDomain},
       
   123     {_S8("joinDomainResponse"), EJoinDomainResponse},
       
   124     {_S8("keyIdentifier"), EKeyIdentifier},
       
   125     {_S8("leaveDomain"), ELeaveDomain},
       
   126     {_S8("leaveDomainResponse"), ELeaveDomainResponse},
       
   127     {_S8("mac"), EMac},
       
   128     {_S8("manufacturer"), EManufacturer},
       
   129     {_S8("model"), EModel},
       
   130     {_S8("nonce"), ENonce},
       
   131     {_S8("notAfter"), ENotAfter},
       
   132     {_S8("ocspResponse"), EOcspResponse},
       
   133     {_S8("protectedRO"), EProtectedRO},
       
   134     {_S8("registrationRequest"), ERegistrationRequest},
       
   135     {_S8("registrationResponse"), ERegistrationResponse},
       
   136     {_S8("riAlias"), ERiAlias},
       
   137     {_S8("rights"), Roap::ERights},
       
   138     {_S8("riHello"), ERiHello},
       
   139     {_S8("riID"), ERiID},
       
   140     {_S8("riNonce"), ERiNonce},
       
   141     {_S8("riURL"), ERiURL},
       
   142     {_S8("ro"), ERo},
       
   143     {_S8("roAcquisition"), ERoAcquisition},
       
   144     {_S8("roapTrigger"), ERoapTrigger},
       
   145     {_S8("roapURL"), ERoapURL},
       
   146     {_S8("roID"), ERoID},
       
   147     {_S8("roInfo"), ERoInfo},
       
   148     {_S8("roResponse"), ERoResponse},
       
   149     {_S8("selectedAlgorithm"), ESelectedAlgorithm},
       
   150     {_S8("selectedVersion"), ESelectedVersion},
       
   151     {_S8("serverInfo"), EServerInfo},
       
   152     {_S8("signature"), ESignature},
       
   153     {_S8("SignatureValue"), ESignatureValue},
       
   154     {_S8("supportedAlgorithm"), ESupportedAlgorithm},
       
   155     {_S8("time"), ETime},
       
   156     {_S8("timeStamp"), ETimeStamp},
       
   157     {_S8("trustedAuthorities"), ETrustedAuthorities},
       
   158     {_S8("version"), EVersion},
       
   159     {_S8("X509SPKIHash"), EX509SPKIHash},
       
   160 #ifdef RD_DRM_METERING
       
   161     {_S8("extendedTrigger"), EExtendedTriggerElement},
       
   162     {_S8("meteringReport"), EMeteringReportRequest},
       
   163     {_S8("meteringReportResponse"), EMeteringReportResponse},
       
   164     {_S8("prURL"), EPrUrl}
       
   165 #endif
       
   166     };
       
   167 
       
   168 // The stack states have to be sorted with descending length. The
       
   169 // MatchStackState function searches for the first matching state.
       
   170 static const TStackState KParserStackStates[] =
       
   171     {
       
   172     // 4 elements --------------------------------------------------------------
       
   173     {ERiIdRoResponseState, {EHash, EKeyIdentifier, ERiID, ERoResponse, ELast}},
       
   174     {ERiIdJoinDomainResponseState, {EHash, EKeyIdentifier, ERiID, EJoinDomainResponse, ELast}},
       
   175     // 3 elements --------------------------------------------------------------
       
   176 #ifdef RD_DRM_METERING
       
   177     {EPostResponseUrlState, {EPrUrl, EExtension, EExtensions, ELast}},
       
   178 #endif
       
   179     {EDeviceIdState, {EHash, EKeyIdentifier, EDeviceID, ELast}},
       
   180     {ETrustedAuthoritiesState, {EHash, EKeyIdentifier, ETrustedAuthorities, ELast}},
       
   181     {EPeerKeyIdentifierRIHelloState, {EIdentifier, EExtension, EExtensions, ELast}},
       
   182     {ETransactionIdState, {EId, EExtension, EExtensions, ELast}},
       
   183     {ETransactionIdContentIdState, {Roap::EContentID, EExtension, EExtensions, ELast}},
       
   184     {EWhiteListState, {EDomainName, EExtension, EExtensions, ELast}},
       
   185     {EDeviceDetailsState, {EDeviceDetails, EExtensions, ERiHello, ELast}},
       
   186     {ERiIdState, {EHash, EKeyIdentifier, ERiID, ELast}},
       
   187     {EEncKeyState, {ECipherValue, ECipherData, EEncKey, ELast}},
       
   188     {EDomainKeyEncryptionMethodState, {EEncryptionMethod, EEncKey, EDomainKey, ELast}},
       
   189     {EMacJoinDomainResponseState, {EMac, EDomainKey, EDomainInfo, ELast}},
       
   190     // 2 elements --------------------------------------------------------------
       
   191     {EDomainKeyEncKeyState, {EEncKey, EDomainKey, ELast}},
       
   192     {ECertificateState, {ECertificate, ECertificateChain, ELast}},
       
   193     {ESignatureRoResponseState, {ESignature, ERoResponse, ELast}},
       
   194     {EExtensionState, {EExtension, EExtensions, ELast}},
       
   195     // 1 element ---------------------------------------------------------------
       
   196     {ERiAliasState, {ERiAlias, ELast}},
       
   197     {EDomainAliasState, {EDomainAlias, ELast}},
       
   198     {ERiHelloState, {ERiHello, ELast}},
       
   199     {ESelectedVersionState, {ESelectedVersion, ELast}},
       
   200     {ERiNonceState, {ERiNonce, ELast}},
       
   201     {EOcspResponseState, {EOcspResponse, ELast}},
       
   202     {ERiUrlState, {ERiURL, ELast}},
       
   203     {ESignatureState, {ESignature, ELast}},
       
   204     {ERegistrationResponseState, {ERegistrationResponse, ELast}},
       
   205     {ERoResponseState, {ERoResponse, ELast}},
       
   206     {EJoinDomainResponseState, {EJoinDomainResponse, ELast}},
       
   207     {ELeaveDomainResponseState, {ELeaveDomainResponse, ELast}},
       
   208     {EDomainIdState, {EDomainID, ELast}},
       
   209     {ENonceState, {ENonce, ELast}},
       
   210     {ENotAfterState, {ENotAfter, ELast}},
       
   211     {EDomainKeyState, {EDomainKey, ELast}},
       
   212     {ERoapTriggerState, {ERoapTrigger, ELast}},
       
   213     {ERoapUrlState, {ERoapURL, ELast}},
       
   214     {ERegistrationRequestState, {ERegistrationRequest, ELast}},
       
   215     {ERoAcquisitionState, {ERoAcquisition, ELast}},
       
   216     {EJoinDomainState, {EJoinDomain, ELast}},
       
   217     {ELeaveDomainState, {ELeaveDomain, ELast}},
       
   218     {ERoIdState, {ERoID, ELast}},
       
   219     {EContentIdState, {Roap::EContentID, ELast}},
       
   220     {ESignatureValueState, {ESignatureValue, ELast}},
       
   221     {EDigestValueState, {EDigestValue, ELast}},
       
   222     {EServerInfoState, {EServerInfo, ELast}},
       
   223     {ESupportedAlgorithmState, {ESupportedAlgorithm, ELast}},
       
   224     {ESelectedAlgorithmState, {ESelectedAlgorithm, ELast}},
       
   225 #ifdef RD_DRM_METERING
       
   226     {EExtendedTriggerElementState, {EExtendedTriggerElement, ELast}},
       
   227     {EMeteringReportRequestState, {EMeteringReportRequest, ELast}},
       
   228     {EMeteringReportResponseState, {EMeteringReportResponse, ELast}}
       
   229 #endif
       
   230     };
       
   231 
       
   232 _LIT8(KParserMimeType, "text/xml");
       
   233 _LIT8(KDomainKey, "domainKey");
       
   234 _LIT8(KMac, "mac>");
       
   235 _LIT8(KMacEnd, "</mac>");
       
   236 
       
   237 // ============================ auto_handde helper class =======================
       
   238 //Auto handle for easening handle release on exceptional exit situations
       
   239 template<class T> class auto_handle
       
   240     {
       
   241     public:
       
   242 
       
   243     auto_handle() {}
       
   244     auto_handle(T aHandle) : iHandle( aHandle ) {}
       
   245     auto_handle( auto_handle<T>& aHandle) : iHandle( aHandle.release() ) {}
       
   246     ~auto_handle() { iHandle.Close(); }
       
   247     const T& operator()() const { return iHandle; }
       
   248     T& operator()() { return iHandle; }
       
   249     T get() const { return iHandle; }
       
   250     T release() { T temp = iHandle; iHandle = 0; return temp; }
       
   251 
       
   252     private:
       
   253     T iHandle;
       
   254     };
       
   255 
       
   256 
       
   257 // ============================ MEMBER FUNCTIONS ===============================
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CRoapParser::CRoapParser
       
   261 // C++ default constructor can NOT contain any code, that
       
   262 // might leave.
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 CRoapParser::CRoapParser():
       
   266     iParser(NULL),
       
   267     iResponseParser(NULL)
       
   268     {
       
   269     }
       
   270 
       
   271 // -----------------------------------------------------------------------------
       
   272 // CRoapParser::ConstructL
       
   273 // Symbian 2nd phase constructor can leave.
       
   274 // -----------------------------------------------------------------------------
       
   275 //
       
   276 void CRoapParser::ConstructL()
       
   277     {
       
   278     TInt i;
       
   279 
       
   280     iParser = CParser::NewL(KParserMimeType, *this);
       
   281     for (i = 0; i < KRoapElementCount; i++)
       
   282         {
       
   283         TPtrC8 ptr(KRoapElements[i].iString, User::StringLength(
       
   284             KRoapElements[i].iString));
       
   285         iRoapElements[KRoapElements[i].iNumber] =
       
   286             iParser->StringPool().OpenStringL(ptr);
       
   287         }
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CRoapParser::NewL
       
   292 // Two-phased constructor.
       
   293 // -----------------------------------------------------------------------------
       
   294 //
       
   295 CRoapParser* CRoapParser::NewL()
       
   296     {
       
   297     CRoapParser* self = new( ELeave ) CRoapParser;
       
   298 
       
   299     CleanupStack::PushL( self );
       
   300     self->ConstructL();
       
   301     CleanupStack::Pop( self );
       
   302 
       
   303     return self;
       
   304     }
       
   305 
       
   306 
       
   307 // Destructor
       
   308 CRoapParser::~CRoapParser()
       
   309     {
       
   310     TInt i;
       
   311     for (i = 0; i < ELEMENT_COUNT(KRoapElements); i++)
       
   312         {
       
   313         iRoapElements[KRoapElements[i].iNumber].Close();
       
   314         }
       
   315     delete iParser;
       
   316     delete iContent;
       
   317     }
       
   318 
       
   319 // -----------------------------------------------------------------------------
       
   320 // CRoapParser::ParseMeteringRespL
       
   321 // -----------------------------------------------------------------------------
       
   322 //
       
   323 #ifndef RD_DRM_METERING
       
   324 CMeteringResp* CRoapParser::ParseMeteringRespL( const TDesC8& /*aMessage*/ )
       
   325     {
       
   326     return NULL;
       
   327     }
       
   328 #else
       
   329 CMeteringResp* CRoapParser::ParseMeteringRespL( const TDesC8& aMessage )
       
   330     {
       
   331     LOG( _L("CRoapParser::ParseMeteringRespL") );
       
   332     CMeteringResp* r = CMeteringResp::NewL();
       
   333     CleanupStack::PushL(r);
       
   334     TMeteringRespParser p(r);
       
   335     iResponseParser = &p;
       
   336     ParseL(aMessage);
       
   337     CleanupStack::Pop(r);
       
   338     return r;
       
   339     }
       
   340 #endif
       
   341 // -----------------------------------------------------------------------------
       
   342 // CRoapParser::ParseJoinDomainRespL
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 CJoinDomainResp* CRoapParser::ParseJoinDomainRespL(
       
   346     const TDesC8& aMessage, RPointerArray<HBufC8>& aDomainKeyElements)
       
   347     {
       
   348     LOG( _L("CRoapParser::ParseJoinDomainRespL") );
       
   349 
       
   350     CJoinDomainResp* r = CJoinDomainResp::NewL();
       
   351     CleanupStack::PushL(r);
       
   352     TJoinDomainRespParser p(r);
       
   353     iResponseParser = &p;
       
   354     ParseL(aMessage);
       
   355     if ( r->iStatus == ESuccess )
       
   356         {
       
   357         User::LeaveIfError(
       
   358             ExtractDomainKeyElements(aMessage, aDomainKeyElements));
       
   359         }
       
   360     CleanupStack::Pop(r);
       
   361     return r;
       
   362     }
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CRoapParser::ParseLeaveDomainRespL
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 CLeaveDomainResp* CRoapParser::ParseLeaveDomainRespL(
       
   369     const TDesC8& aMessage)
       
   370     {
       
   371     LOG( _L("CRoapParser::ParseLeaveDomainRespL") );
       
   372 
       
   373     CLeaveDomainResp* r = CLeaveDomainResp::NewL();
       
   374     CleanupStack::PushL(r);
       
   375     TLeaveDomainRespParser p(r);
       
   376     iResponseParser = &p;
       
   377     ParseL(aMessage);
       
   378     CleanupStack::Pop(r);
       
   379     return r;
       
   380     }
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CRoapParser::ParseRegistrationResp
       
   384 // -----------------------------------------------------------------------------
       
   385 //
       
   386 CRegistrationResp* CRoapParser::ParseRegistrationRespL(
       
   387     const TDesC8& aMessage)
       
   388     {
       
   389     LOG( _L("CRoapParser::ParseRegistrationRespL") );
       
   390 
       
   391     CRegistrationResp* r = CRegistrationResp::NewL();
       
   392     CleanupStack::PushL(r);
       
   393     TRegistrationRespParser p(r);
       
   394     iResponseParser = &p;
       
   395     ParseL(aMessage);
       
   396     CleanupStack::Pop(r);
       
   397     return r;
       
   398     }
       
   399 
       
   400 // -----------------------------------------------------------------------------
       
   401 // CRoapParser::ParseRightsRespL
       
   402 // -----------------------------------------------------------------------------
       
   403 //
       
   404 CRightsResp* CRoapParser::ParseRightsRespL(
       
   405     const TDesC8& aMessage)
       
   406     {
       
   407     LOG( _L("CRoapParser::ParseRightsRespL") );
       
   408 
       
   409     CRightsResp* r = CRightsResp::NewL();
       
   410     CleanupStack::PushL(r);
       
   411     TRightsRespParser p(r);
       
   412     iResponseParser = &p;
       
   413     ParseL(aMessage);
       
   414     CleanupStack::Pop(r);
       
   415     return r;
       
   416     }
       
   417 
       
   418 // -----------------------------------------------------------------------------
       
   419 // CRoapParser::ParseRIHelloL
       
   420 // -----------------------------------------------------------------------------
       
   421 //
       
   422 CRIHello* CRoapParser::ParseRIHelloL(
       
   423     const TDesC8& aMessage)
       
   424     {
       
   425     LOG( _L("CRoapParser::ParseRIHelloL") );
       
   426 
       
   427     CRIHello* r = CRIHello::NewL();
       
   428     CleanupStack::PushL(r);
       
   429     TRIHelloParser p(r);
       
   430     iResponseParser = &p;
       
   431     ParseL(aMessage);
       
   432     CleanupStack::Pop(r);
       
   433     return r;
       
   434     }
       
   435 
       
   436 // -----------------------------------------------------------------------------
       
   437 // CRoapParser::ParseRoapTriggerL
       
   438 // -----------------------------------------------------------------------------
       
   439 //
       
   440 CRoapTrigger* CRoapParser::ParseRoapTriggerL(
       
   441     const TDesC8& aMessage)
       
   442     {
       
   443     LOG( _L("CRoapParser::ParseRoapTriggerL") );
       
   444 
       
   445     CRoapTrigger* r = CRoapTrigger::NewL();
       
   446     CleanupStack::PushL(r);
       
   447     TRoapTriggerParser p(r);
       
   448     iResponseParser = &p;
       
   449     ParseL(aMessage);
       
   450     CleanupStack::Pop(r);
       
   451     return r;
       
   452     }
       
   453 
       
   454 // -----------------------------------------------------------------------------
       
   455 // CRoapParser::ParseL
       
   456 // -----------------------------------------------------------------------------
       
   457 //
       
   458 void CRoapParser::ParseL(
       
   459     const TDesC8& aMessage)
       
   460     {
       
   461     TInt i;
       
   462     TInt n;
       
   463 
       
   464     iElementStackDepth = 0;
       
   465     delete iContent;
       
   466     iContent = NULL;
       
   467     iParser->ParseBeginL();
       
   468     i = 0;
       
   469     while (i < aMessage.Length())
       
   470         {
       
   471         n = Min(aMessage.Length() - i, KParserChunkSize);
       
   472         iParser->ParseL(aMessage.Mid(i, n));
       
   473         i += n;
       
   474         }
       
   475     iParser->ParseEndL();
       
   476     }
       
   477 
       
   478 // -----------------------------------------------------------------------------
       
   479 // CRoapParser::OnStartDocumentL
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 void CRoapParser::OnStartDocumentL(
       
   483     const RDocumentParameters& /*aDocParam*/,
       
   484     TInt /*aErrorCode*/)
       
   485     {
       
   486     }
       
   487 
       
   488 // -----------------------------------------------------------------------------
       
   489 // CRoapParser::OnEndDocumentL
       
   490 // -----------------------------------------------------------------------------
       
   491 //
       
   492 void CRoapParser::OnEndDocumentL(
       
   493     TInt /*aErrorCode*/)
       
   494     {
       
   495     }
       
   496 
       
   497 // -----------------------------------------------------------------------------
       
   498 // CRoapParser::OnStartElementL
       
   499 // -----------------------------------------------------------------------------
       
   500 //
       
   501 void CRoapParser::OnStartElementL(
       
   502     const RTagInfo& aElement,
       
   503     const RAttributeArray& aAttributes,
       
   504     TInt /*aErrorCode*/)
       
   505     {
       
   506     TInt i;
       
   507     TInt state;
       
   508 
       
   509     if (iContent)
       
   510         {
       
   511         delete iContent;
       
   512         iContent = NULL;
       
   513         iContent = HBufC8::NewL(0);
       
   514         }
       
   515 
       
   516     for (i = 0; i < KRoapElementCount; i++)
       
   517         {
       
   518         if (aElement.LocalName() == iRoapElements[i])
       
   519             {
       
   520             iElementStack[iElementStackDepth] =
       
   521                 static_cast<TRoapElementEnum>(i);
       
   522             iElementStackDepth++;
       
   523             state = MatchStackState();
       
   524             iResponseParser->OnStartElementL(*this, state, aElement,
       
   525                 aAttributes);
       
   526             if (iElementStackDepth == KMaxElementNesting)
       
   527                 {
       
   528                 User::Leave(EXmlUnexpectedState);
       
   529                 }
       
   530             }
       
   531         }
       
   532     }
       
   533 
       
   534 // -----------------------------------------------------------------------------
       
   535 // CRoapParser::OnEndElementL
       
   536 // -----------------------------------------------------------------------------
       
   537 //
       
   538 void CRoapParser::OnEndElementL(
       
   539     const RTagInfo& aElement,
       
   540     TInt /*aErrorCode*/)
       
   541     {
       
   542     TInt i;
       
   543     TInt state;
       
   544 
       
   545     for (i = 0; i < KRoapElementCount; i++)
       
   546         {
       
   547         if (aElement.LocalName() == iRoapElements[i])
       
   548             {
       
   549             state = MatchStackState();
       
   550             iResponseParser->OnEndElementL(*this, state, aElement);
       
   551             iElementStackDepth--;
       
   552             if (iElementStackDepth < 0)
       
   553                 {
       
   554                 User::Leave(EXmlUnexpectedState);
       
   555                 }
       
   556             }
       
   557         }
       
   558     }
       
   559 
       
   560 // -----------------------------------------------------------------------------
       
   561 // CRoapParser::OnContentL
       
   562 // -----------------------------------------------------------------------------
       
   563 //
       
   564 void CRoapParser::OnContentL(
       
   565     const TDesC8& aBytes,
       
   566     TInt /*aErrorCode*/)
       
   567     {
       
   568     if ( !iContent )
       
   569         {
       
   570         iContent = HBufC8::NewL(aBytes.Size());
       
   571         *iContent = aBytes;
       
   572         }
       
   573     else
       
   574         {
       
   575         iContent = iContent->ReAllocL(iContent->Size() + aBytes.Size());
       
   576         TPtr8 c(iContent->Des());
       
   577         c.Append(aBytes);
       
   578         }
       
   579     }
       
   580 
       
   581 // -----------------------------------------------------------------------------
       
   582 // CRoapParser::OnStartPrefixMappingL
       
   583 // -----------------------------------------------------------------------------
       
   584 //
       
   585 void CRoapParser::OnStartPrefixMappingL(
       
   586     const RString& /*aPrefix*/,
       
   587     const RString& /*aUri*/,
       
   588     TInt /*aErrorCode*/)
       
   589     {
       
   590     }
       
   591 
       
   592 // -----------------------------------------------------------------------------
       
   593 // CRoapParser::OnEndPrefixMappingL
       
   594 // -----------------------------------------------------------------------------
       
   595 //
       
   596 void CRoapParser::OnEndPrefixMappingL(
       
   597     const RString& /*aPrefix*/,
       
   598     TInt /*aErrorCode*/)
       
   599     {
       
   600     }
       
   601 
       
   602 // -----------------------------------------------------------------------------
       
   603 // CRoapParser::OnIgnorableWhiteSpaceL
       
   604 // -----------------------------------------------------------------------------
       
   605 //
       
   606 void CRoapParser::OnIgnorableWhiteSpaceL(
       
   607     const TDesC8& /*aBytes*/,
       
   608     TInt /*aErrorCode*/)
       
   609     {
       
   610     }
       
   611 
       
   612 // -----------------------------------------------------------------------------
       
   613 // CRoapParser::OnSkippedEntityL
       
   614 // -----------------------------------------------------------------------------
       
   615 //
       
   616 void CRoapParser::OnSkippedEntityL(
       
   617     const RString& /*aName*/,
       
   618     TInt /*aErrorCode*/)
       
   619     {
       
   620     }
       
   621 
       
   622 // -----------------------------------------------------------------------------
       
   623 // CRoapParser::OnProcessingInstructionL
       
   624 // -----------------------------------------------------------------------------
       
   625 //
       
   626 void CRoapParser::OnProcessingInstructionL(
       
   627     const TDesC8& /*aTarget*/,
       
   628     const TDesC8& /*aData*/,
       
   629     TInt /*aErrorCode*/)
       
   630     {
       
   631     }
       
   632 
       
   633 // -----------------------------------------------------------------------------
       
   634 // CRoapParser::OnOutOfData
       
   635 // -----------------------------------------------------------------------------
       
   636 //
       
   637 void CRoapParser::OnOutOfData()
       
   638     {
       
   639     }
       
   640 
       
   641 // -----------------------------------------------------------------------------
       
   642 // CRoapParser::OnError
       
   643 // -----------------------------------------------------------------------------
       
   644 //
       
   645 void CRoapParser::OnError(
       
   646     TInt /*aErrorCode*/)
       
   647     {
       
   648     }
       
   649 
       
   650 // -----------------------------------------------------------------------------
       
   651 // CRoapParser::GetExtendedInterface
       
   652 // -----------------------------------------------------------------------------
       
   653 //
       
   654 TAny* CRoapParser::GetExtendedInterface(
       
   655     const TInt32 /*aUid*/)
       
   656     {
       
   657     return NULL;
       
   658     }
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // CRoapParser::MatchStackState
       
   662 // -----------------------------------------------------------------------------
       
   663 //
       
   664 TParserStackState CRoapParser::MatchStackState(void)
       
   665     {
       
   666     TInt i;
       
   667     TInt j;
       
   668     TInt k;
       
   669     TParserStackState r = EUnknownState;
       
   670 
       
   671     for (i = 0;
       
   672         r == EUnknownState &&
       
   673         i < ELEMENT_COUNT(KParserStackStates); i++)
       
   674         {
       
   675         for (j = iElementStackDepth - 1, k = 0;
       
   676             j > 0 && KParserStackStates[i].iStack[k] != ELast;
       
   677             j--, k++)
       
   678             {
       
   679             if (iElementStack[j] != KParserStackStates[i].iStack[k])
       
   680                 {
       
   681                 break;
       
   682                 }
       
   683             }
       
   684         if ((j == 0 && iElementStack[j] == KParserStackStates[i].iStack[k]) ||
       
   685             KParserStackStates[i].iStack[k] == ELast)
       
   686             {
       
   687             r = KParserStackStates[i].iState;
       
   688             }
       
   689         }
       
   690     return r;
       
   691     }
       
   692 
       
   693 // -----------------------------------------------------------------------------
       
   694 // CRoapParser::GetAttributeValueL
       
   695 // -----------------------------------------------------------------------------
       
   696 //
       
   697 HBufC8* CRoapParser::GetAttributeValueL(
       
   698     const RAttributeArray& aAttrList,
       
   699     const TDesC8& aAttrName)
       
   700     {
       
   701     HBufC8* r = NULL;
       
   702     RAttribute a;
       
   703     TInt i;
       
   704 
       
   705     for (i = 0; !r && i < aAttrList.Count(); i++)
       
   706         {
       
   707         a = aAttrList[i];
       
   708         if (a.Attribute().LocalName().DesC().Compare(aAttrName) == 0)
       
   709             {
       
   710             r = a.Value().DesC().AllocL();
       
   711             }
       
   712         }
       
   713     return r;
       
   714     }
       
   715 
       
   716 // -----------------------------------------------------------------------------
       
   717 // CRoapParser::ConvertRoapStatus
       
   718 // -----------------------------------------------------------------------------
       
   719 //
       
   720 TRoapStatus CRoapParser::ConvertRoapStatus(
       
   721     const TDesC8& aStatus)
       
   722     {
       
   723     TInt i;
       
   724     TRoapStatus r = EUnknownStatus;
       
   725 
       
   726     for (i = 0; r == EUnknownStatus && i < ELEMENT_COUNT(KStatusValues); i++)
       
   727         {
       
   728         if (aStatus.Compare(TPtrC8(KStatusValues[i].iString,
       
   729             aStatus.Size())) == 0)
       
   730             {
       
   731             r = static_cast<TRoapStatus>(KStatusValues[i].iNumber);
       
   732             }
       
   733         }
       
   734     return r;
       
   735     }
       
   736 
       
   737 // -----------------------------------------------------------------------------
       
   738 // CRoapParser::ExtractElement
       
   739 // Note: uses auto_handle helper class
       
   740 // -----------------------------------------------------------------------------
       
   741 //
       
   742 TPtrC8 CRoapParser::ExtractElement(
       
   743     const TDesC8& aMessage, const TDesC8& aElement, TInt& aOffset ) const
       
   744     {
       
   745     LOG( _L("CRoapParser::ExtractElement") );
       
   746 
       
   747     TPtrC8 temp( KNullDesC8 );
       
   748     TInt pos = ( 0 );
       
   749     TInt startPos = ( 0 );
       
   750     TInt endPos = ( 0 );
       
   751     TInt ret( 0 );
       
   752     TInt startLength ( 0 );
       
   753     const TInt KSpaceForDelimiters( 3 ); // max "</" aElement ">"
       
   754 
       
   755     auto_handle< RBuf8 > tagToBeFound;
       
   756     // Must be nonleaving since this function is nonleaving
       
   757     ret = tagToBeFound().Create( aElement.Length() + KSpaceForDelimiters );
       
   758     if ( ret != KErrNone )
       
   759         {
       
   760         aOffset = -1;
       
   761         return KNullDesC8();
       
   762         }
       
   763 
       
   764     //First we try to find start tag (as localname)
       
   765     tagToBeFound().SetLength( 0 );
       
   766     tagToBeFound().AppendFormat( _L8( "<%S" ), &aElement );
       
   767 
       
   768     temp.Set( aMessage.Mid( aOffset ) );
       
   769 
       
   770 
       
   771     startPos = temp.Find( tagToBeFound() );
       
   772 
       
   773     if ( startPos < 0 )
       
   774         {//Not found, let's try with namespace start
       
   775         tagToBeFound().SetLength( 0 );
       
   776         tagToBeFound().AppendFormat( _L8( ":%S" ), &aElement );
       
   777         pos = temp.Find( tagToBeFound() );
       
   778         if ( pos < 0 )
       
   779             {//Not possible to find valid tag with given name
       
   780             aOffset = -1;
       
   781             return KNullDesC8();
       
   782             }
       
   783         temp.Set( aMessage.Left( pos ) );
       
   784         startPos = temp.LocateReverse('<');
       
   785         if ( startPos < 0 )
       
   786             {
       
   787             aOffset = -1;
       
   788             return KNullDesC8();
       
   789             }
       
   790         //Calculate length for the new string to be stored
       
   791         ret = tagToBeFound().ReAlloc(
       
   792             ( pos - startPos ) + aElement.Length() + KSpaceForDelimiters );
       
   793         if ( ret != KErrNone )
       
   794             {
       
   795             aOffset = -1;
       
   796             return KNullDesC8();
       
   797             }
       
   798         tagToBeFound().SetLength( 0 );
       
   799         tagToBeFound().Append( temp.Mid( startPos ) );
       
   800         tagToBeFound().AppendFormat( _L8( ":%S" ), &aElement );
       
   801         }
       
   802 
       
   803     startLength = tagToBeFound().Length(); //needed in later calculations
       
   804     startPos += aOffset;
       
   805     temp.Set( aMessage.Mid( startPos + startLength ) );
       
   806 
       
   807     //Now find the end element. First prepare element to be found
       
   808     tagToBeFound().Insert( 1, _L8( "/" ) ); // '/' after '<'
       
   809     tagToBeFound().Append( _L8( ">" ) ); // '>' as last
       
   810 
       
   811 
       
   812     endPos = temp.Find(tagToBeFound());
       
   813     if ( endPos < 0)
       
   814         {
       
   815         aOffset = -1;
       
   816         return KNullDesC8();
       
   817         }
       
   818     endPos += startPos + startLength + tagToBeFound().Length();
       
   819 
       
   820     temp.Set( aMessage.Mid(startPos, endPos - startPos) );
       
   821 
       
   822     aOffset = endPos;
       
   823     LOG( _L( "Extracted element" ) );
       
   824     LOGHEX( temp.Ptr(), temp.Length() );
       
   825 
       
   826     //auto_handle closes and frees allocated resources
       
   827     return temp;
       
   828     }
       
   829 
       
   830 // -----------------------------------------------------------------------------
       
   831 // CRoapParser::ExtractDomainKeyElements
       
   832 // -----------------------------------------------------------------------------
       
   833 //
       
   834 TInt CRoapParser::ExtractDomainKeyElements(
       
   835     const TDesC8& aMessage, RPointerArray<HBufC8>& aDomainKeyElements ) const
       
   836     {
       
   837     LOG( _L("CRoapParser::ExtractDomainKeyElements") );
       
   838 
       
   839     TPtrC8 temp(KNullDesC8);
       
   840     TPtrC8 elementPtr(KNullDesC8);
       
   841     TInt pos = 0;
       
   842     HBufC8* element = NULL;
       
   843     TInt err = KErrNone;
       
   844     TPtr8 ptr(0, 0);
       
   845     TInt macStart = 0;
       
   846     TInt macEnd = 0;
       
   847 
       
   848     // extract DomainKey elements to aDomainKeyElements array
       
   849     while (true)
       
   850         {
       
   851         elementPtr.Set( ExtractElement( aMessage, KDomainKey(), pos ) );
       
   852 
       
   853         if( pos < 0 )
       
   854             {
       
   855             // no more DomainKeys present
       
   856             if(aDomainKeyElements.Count())
       
   857                 {
       
   858                 return KErrNone;
       
   859                 }
       
   860             else
       
   861                 {
       
   862                 aDomainKeyElements.ResetAndDestroy();
       
   863                 return KErrRoapServerFatal;
       
   864                 }
       
   865             }
       
   866 
       
   867         element = elementPtr.Alloc();
       
   868         if(!element)
       
   869             {
       
   870             return KErrNoMemory;
       
   871             }
       
   872 
       
   873         // remove mac element from found DomainKeyElement
       
   874         macStart = element->Find( KMac );
       
   875         temp.Set(element->Left(macStart));
       
   876         macStart = temp.LocateReverse('<');
       
   877         macEnd = element->Find( KMacEnd ) + KMacEnd().Length();
       
   878         if ( macStart < 0 || macEnd < 0 )
       
   879             {
       
   880             aDomainKeyElements.ResetAndDestroy();
       
   881             return KErrRoapServerFatal;
       
   882             }
       
   883         ptr.Set( element->Des() );
       
   884         ptr.Replace( macStart, macEnd - macStart, KNullDesC8 );
       
   885 
       
   886         err = aDomainKeyElements.Append(element);
       
   887         if(err)
       
   888             {
       
   889             delete element;
       
   890             element = NULL;
       
   891             aDomainKeyElements.ResetAndDestroy();
       
   892             return err;
       
   893             }
       
   894         }
       
   895     }
       
   896 
       
   897 //  End of File