ncdengine/provider/protocol/src/ncdprotocolutils.cpp
changeset 0 ba25891c3a9e
child 80 9dcba1ee99f7
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2006 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <utf.h>
       
    20 
       
    21 #include "ncdprotocolutils.h"
       
    22 
       
    23 
       
    24 _LIT8(KTrueString8, "true");
       
    25 _LIT8(KFalseString8, "false");
       
    26 _LIT16(KTrueString16, "true");
       
    27 _LIT16(KFalseString16, "false");
       
    28 
       
    29 // This circumvents an API "feature" and adds the XML prefix to the
       
    30 // name itself. The API changed (AFAIK) so that a certain parameter
       
    31 // set caused the prefix to be ignored.
       
    32 
       
    33 #define XML_PREFIX_HACK
       
    34 #warning XML DOM API PREFIX HACK ENABLED ON 3.2 AND 5.0 PLATFORMS
       
    35 
       
    36 
       
    37 
       
    38 void NcdProtocolUtils::AssignEmptyDesL( HBufC16*& aDes )
       
    39     {
       
    40     delete aDes;
       
    41     aDes = 0;
       
    42     aDes = KNullDesC16().AllocL();
       
    43     }
       
    44 
       
    45 void NcdProtocolUtils::AssignEmptyDesL( HBufC8*& aDes )
       
    46     {
       
    47     delete aDes;
       
    48     aDes = 0;
       
    49     aDes = KNullDesC8().AllocL();
       
    50     }
       
    51 
       
    52 
       
    53 void NcdProtocolUtils::AssignDesL( HBufC16*& aDes, const TDesC16& aSource )
       
    54     {
       
    55     delete aDes;
       
    56     aDes = 0;
       
    57     aDes = aSource.AllocL();
       
    58     }
       
    59 
       
    60 void NcdProtocolUtils::AssignDesL( HBufC16*& aDes, const TDesC8& aSource )
       
    61     {
       
    62     delete aDes;
       
    63     aDes = 0;
       
    64     aDes = ConvertUtf8ToUnicodeL( aSource );
       
    65     }
       
    66 
       
    67 void NcdProtocolUtils::AssignDesL( HBufC8*& aDes, const TDesC8& aSource )
       
    68     {
       
    69     delete aDes;
       
    70     aDes = 0;
       
    71     aDes = aSource.AllocL();
       
    72     }
       
    73 
       
    74 HBufC16* NcdProtocolUtils::ConvertUtf8ToUnicodeLC( const TDesC8& aUtfText )
       
    75     {
       
    76     HBufC16* t = ConvertUtf8ToUnicodeL( aUtfText );
       
    77     CleanupStack::PushL( t );
       
    78     return t;
       
    79     }
       
    80 
       
    81 HBufC16* NcdProtocolUtils::ConvertUtf8ToUnicodeL( const TDesC8& aUtfText )
       
    82     {
       
    83     HBufC16* buffer = HBufC16::NewLC( aUtfText.Length() );
       
    84     TPtr ptr( buffer->Des() );
       
    85     User::LeaveIfError( 
       
    86         CnvUtfConverter::ConvertToUnicodeFromUtf8( ptr, aUtfText ) );
       
    87     CleanupStack::Pop( buffer );
       
    88     return buffer;
       
    89     }
       
    90 
       
    91 HBufC8* NcdProtocolUtils::ConvertUnicodeToUtf8L( const TDesC16& aUnicodeText )
       
    92     {
       
    93     const TInt KConvertBufferSize = 32;
       
    94 
       
    95     // Place converted data here, initial size double the conversion buffer.
       
    96     HBufC8* convertedData = HBufC8::NewL( KConvertBufferSize*2 );
       
    97     CleanupStack::PushL( convertedData );
       
    98     TPtr8 destination( convertedData->Des() );
       
    99 
       
   100     // Create a small output buffer
       
   101     TBuf8<KConvertBufferSize> outputBuffer;
       
   102     // Create a buffer for the unconverted text - initialised with the input text
       
   103     TPtrC16 remainderOfUnicodeText( aUnicodeText );
       
   104 
       
   105     for ( ;; ) // conversion loop
       
   106         {
       
   107         // Start conversion. When the output buffer is full, return the 
       
   108         // number of characters that were not converted
       
   109         const TInt returnValue
       
   110             = CnvUtfConverter::ConvertFromUnicodeToUtf8( outputBuffer, 
       
   111                                                         remainderOfUnicodeText );
       
   112 
       
   113         // check to see that the descriptor isn’t corrupt - leave if it is
       
   114         if ( returnValue==CnvUtfConverter::EErrorIllFormedInput )
       
   115             User::Leave( KErrCorrupt );
       
   116         else if ( returnValue<0 ) // future-proof against "TError" expanding
       
   117             User::Leave( KErrGeneral );
       
   118 
       
   119         // Do something here to store the contents of the output buffer.
       
   120         if ( destination.Length() + outputBuffer.Length() >= destination.MaxLength() )
       
   121             {
       
   122             HBufC8* newBuffer = convertedData->ReAllocL(
       
   123                 ( destination.MaxLength() + outputBuffer.Length() ) * 2 );
       
   124             CleanupStack::Pop( convertedData );
       
   125             convertedData = newBuffer;
       
   126             CleanupStack::PushL( convertedData );
       
   127             destination.Set( convertedData->Des() );
       
   128             }
       
   129 
       
   130         destination.Append( outputBuffer );
       
   131         outputBuffer.Zero();
       
   132 
       
   133         // Finish conversion if there are no unconverted characters in the remainder buffer
       
   134         if ( returnValue==0 )
       
   135             break; 
       
   136 
       
   137         // Remove the converted source text from the remainder buffer.
       
   138         // The remainder buffer is then fed back into loop
       
   139         remainderOfUnicodeText.Set( remainderOfUnicodeText.Right( returnValue));
       
   140         }
       
   141     CleanupStack::Pop( convertedData );
       
   142     return convertedData;
       
   143     }
       
   144 
       
   145 TBool NcdProtocolUtils::IsWhitespace( const TDesC8& aBytes )
       
   146     {
       
   147     for( TInt i = 0; i < aBytes.Length(); i++ )
       
   148         {
       
   149         if( ! TChar( aBytes[i] ).IsSpace() )
       
   150             {
       
   151             return EFalse;
       
   152             }
       
   153         }
       
   154     return ETrue;
       
   155     }
       
   156 
       
   157 
       
   158 TInt NcdProtocolUtils::DesDecToIntL( const TDesC8& aDes )
       
   159     {
       
   160     TLex8 lex( aDes );
       
   161     TInt value;
       
   162     TInt error = lex.Val( value );
       
   163     if( error != KErrNone )
       
   164         {
       
   165         DLERROR(("Conversion error %d, %S",error,&aDes));
       
   166         User::Leave( error );
       
   167         }
       
   168     return value;
       
   169     }
       
   170 
       
   171 
       
   172 TInt NcdProtocolUtils::DesDecToIntL( const TDesC16& aDes )
       
   173     {
       
   174     TLex16 lex( aDes );
       
   175     TInt value;
       
   176     TInt error = lex.Val( value );
       
   177     if( error != KErrNone )
       
   178         {
       
   179         DLERROR((_L("Conversion error %d, %S"),error,&aDes));
       
   180         User::Leave( error );
       
   181         }
       
   182     return value;
       
   183     }
       
   184 
       
   185 
       
   186 TInt NcdProtocolUtils::DesDecToInt( const TDesC8& aDes, TInt& aValue )
       
   187     {
       
   188     if ( aDes != KNullDesC8 ) 
       
   189         {
       
   190         aValue = 0;
       
   191         TLex8 lex( aDes );
       
   192         return lex.Val( aValue );
       
   193         }
       
   194     return KErrNone;
       
   195     }
       
   196 
       
   197 TInt NcdProtocolUtils::DesDecToInt( const TDesC16& aDes, TInt& aValue )
       
   198     {
       
   199     if ( aDes != KNullDesC16 ) 
       
   200         {
       
   201         aValue = 0;
       
   202         TLex16 lex( aDes );
       
   203         return lex.Val( aValue );
       
   204         }
       
   205     return KErrNone;
       
   206     }
       
   207 
       
   208 TReal32 NcdProtocolUtils::DesDecToRealL( const TDesC8& aDes )
       
   209     {
       
   210     DLTRACEIN(("des8 to real32: %S", &aDes));
       
   211     if ( aDes != KNullDesC8 ) 
       
   212         {
       
   213         TReal32 value = 0;
       
   214         TLex8 lex( aDes );
       
   215         TInt error = lex.Val(value);
       
   216         DLINFO(("real32 value=%f, error=%d", value, error));
       
   217         User::LeaveIfError(error);
       
   218         return value;
       
   219         }
       
   220     return 0;
       
   221     }
       
   222 
       
   223 
       
   224 
       
   225 
       
   226 void NcdProtocolUtils::DesToNcdBool( TNcdBool& aBool, const TDesC8& aDes)
       
   227     {
       
   228     if ( aDes == KTrueString8 ) 
       
   229         {
       
   230         aBool = EValueTrue;
       
   231         }
       
   232     else if ( aDes == KFalseString8 )
       
   233         {
       
   234         aBool = EValueFalse;
       
   235         }
       
   236     else
       
   237         {
       
   238         aBool = EValueNotSet;
       
   239         }
       
   240     }
       
   241 
       
   242 void NcdProtocolUtils::DesToNcdBool( TNcdBool& aBool, const TDesC16& aDes )
       
   243     {
       
   244     if ( aDes == KTrueString16 ) 
       
   245         {
       
   246         aBool = EValueTrue;
       
   247         }
       
   248     else if ( aDes == KFalseString16 )
       
   249         {
       
   250         aBool = EValueFalse;
       
   251         }
       
   252     else
       
   253         {
       
   254         aBool = EValueNotSet;
       
   255         }
       
   256     }
       
   257 
       
   258 void NcdProtocolUtils::DesToBool( TBool& aBool, const TDesC8& aDes )
       
   259     {
       
   260     if ( aDes == KTrueString8 )
       
   261         {
       
   262         aBool = ETrue;
       
   263         }    
       
   264     else if ( aDes == KFalseString8 )
       
   265         {
       
   266         aBool = EFalse;
       
   267         }
       
   268     }
       
   269 
       
   270 void NcdProtocolUtils::DesToBool( TBool& aBool, const TDesC16& aDes )
       
   271     { 
       
   272     if ( aDes == KTrueString16 )
       
   273         {
       
   274         aBool = ETrue;
       
   275         }    
       
   276     else if ( aDes == KFalseString16 )
       
   277         {
       
   278         aBool = EFalse;
       
   279         }
       
   280     }
       
   281 
       
   282 const TDesC8& NcdProtocolUtils::BoolToDes( TBool aValue )
       
   283     {
       
   284     return aValue ? KTrueString8() : KFalseString8();
       
   285     }
       
   286 
       
   287 const TDesC8& NcdProtocolUtils::NcdBoolToDes( TNcdBool aValue )
       
   288     {
       
   289     switch( aValue )
       
   290         {
       
   291         case EValueNotSet:
       
   292             return KNullDesC8();
       
   293 
       
   294         case EValueTrue:
       
   295             return KTrueString8();
       
   296 
       
   297         case EValueFalse:
       
   298             return KFalseString8();
       
   299 
       
   300         default:
       
   301             DASSERT( EFalse );
       
   302             return KNullDesC8();
       
   303         };
       
   304     }
       
   305 
       
   306 
       
   307 
       
   308 void NcdProtocolUtils::BoolToStringL(TXmlEngString& aString, const TBool aBool) 
       
   309     {
       
   310     DLTRACEIN((""));
       
   311     aBool ? (aString.SetL(KTrueString8)) : (aString.SetL(KFalseString8));
       
   312     }
       
   313 
       
   314 void NcdProtocolUtils::NcdBoolToStringL(TXmlEngString& aString, const TNcdBool aBool) 
       
   315     {
       
   316     DLTRACEIN((""));
       
   317     if ( aBool == EValueTrue ) 
       
   318         {
       
   319         aString.SetL(KTrueString8);
       
   320         }
       
   321     else if ( aBool == EValueFalse ) 
       
   322         {
       
   323         aString.SetL(KFalseString8);
       
   324         }
       
   325     }
       
   326     
       
   327 void NcdProtocolUtils::DesToStringL(TXmlEngString& aString, const TDesC8& aValue) 
       
   328     {
       
   329     if ( aValue != KNullDesC8 ) 
       
   330         {
       
   331         aString.SetL( aValue );
       
   332         }
       
   333     else 
       
   334         {
       
   335         aString.Free();
       
   336         aString = TXmlEngString();
       
   337         }
       
   338     }
       
   339 
       
   340 void NcdProtocolUtils::DesToStringL(TXmlEngString& aString, const TDesC& aValue) 
       
   341     {
       
   342     if ( aValue != KNullDesC ) 
       
   343         {
       
   344         aString.SetL( aValue );
       
   345         }
       
   346     else 
       
   347         {
       
   348         aString.Free();
       
   349         aString = TXmlEngString();
       
   350         }
       
   351     }
       
   352 
       
   353 void NcdProtocolUtils::IntToStringL(TXmlEngString& aString, TInt aValue) 
       
   354     {
       
   355     TBuf8<256> buf;
       
   356     buf.AppendNum(aValue);
       
   357     aString.SetL(buf);
       
   358     }    
       
   359 
       
   360 
       
   361 void NcdProtocolUtils::NewBoolAttributeL(
       
   362     TXmlEngElement& aParent, const TDesC8& aKey, const TBool aValue)
       
   363     {
       
   364     DLTRACEIN((""));
       
   365     if ( aKey != KNullDesC8 ) 
       
   366         {
       
   367         aParent.SetAttributeL( aKey, NcdProtocolUtils::BoolToDes( aValue ) );
       
   368         }
       
   369     }
       
   370 
       
   371 void NcdProtocolUtils::NewNcdBoolAttributeL(
       
   372     TXmlEngElement& aParent, const TDesC8& aKey, const TNcdBool aValue)
       
   373     {
       
   374     DLTRACEIN((""));
       
   375     if ( aKey != KNullDesC8 && aValue != EValueNotSet ) 
       
   376         {
       
   377         aParent.SetAttributeL( aKey, NcdProtocolUtils::NcdBoolToDes( aValue ) );
       
   378         }
       
   379     }
       
   380 
       
   381 void NcdProtocolUtils::NewAttributeL(
       
   382     TXmlEngElement& aParent, const TDesC8& aKey, const TInt aValue)
       
   383     {
       
   384     TBuf8<32> buf;
       
   385     buf.Num( aValue );
       
   386     aParent.SetAttributeL( aKey, buf );
       
   387     }
       
   388 
       
   389 void NcdProtocolUtils::NewAttributeL(
       
   390     TXmlEngElement& aParent, const TDesC8& aKey, const TDesC8& aValue)
       
   391     {
       
   392     if ( aKey != KNullDesC8 && aValue != KNullDesC8 ) 
       
   393         {
       
   394         aParent.SetAttributeL( aKey, aValue );
       
   395         }
       
   396     }
       
   397 
       
   398 void NcdProtocolUtils::NewAttributeL(
       
   399     TXmlEngElement& aParent, const TDesC8& aKey, const TDesC& aValue)
       
   400     {
       
   401     if ( aKey != KNullDesC8 && aValue != KNullDesC ) 
       
   402         {
       
   403         HBufC8* utf8 = NcdProtocolUtils::ConvertUnicodeToUtf8L( aValue );
       
   404         CleanupStack::PushL( utf8 );
       
   405         aParent.SetAttributeL( aKey, *utf8 );
       
   406         CleanupStack::PopAndDestroy( utf8 );
       
   407         }
       
   408     }
       
   409 
       
   410 void NcdProtocolUtils::NewAttributeL(
       
   411     TXmlEngElement& aParent, const TDesC8& aKey, const TXmlEngString& aValue)
       
   412     {
       
   413     if ( aKey != KNullDesC8 && aValue.NotNull() ) 
       
   414         {
       
   415         aParent.SetAttributeL( aKey, aValue.PtrC8() );
       
   416         }
       
   417     }
       
   418 
       
   419 TXmlEngElement NcdProtocolUtils::NewElementL( RXmlEngDocument& aDocument,
       
   420     TXmlEngElement& aParent, const TDesC8& aName, const TDesC8& aPrefix)
       
   421     {
       
   422     DLTRACEIN(("1 aName=%S aPrefix=%S",&aName,&aPrefix ));
       
   423 #ifndef RD_XML_ENGINE_API_CHANGE
       
   424     TXmlEngString name;
       
   425     NcdProtocolUtils::DesToStringL(name, aName);
       
   426     if (aPrefix != KNullDesC8)
       
   427         {
       
   428         TXmlEngString prefix;
       
   429         NcdProtocolUtils::DesToStringL(prefix, aPrefix);
       
   430         TXmlEngElement element = aDocument.CreateElementL(name, NULL, prefix);
       
   431         aParent.AppendChildL(element);
       
   432         if ( name.NotNull() ) name.Free();
       
   433         if ( prefix.NotNull() ) prefix.Free();
       
   434         return element;
       
   435         }
       
   436     else 
       
   437         {
       
   438         TXmlEngElement element = aDocument.CreateElementL(name);
       
   439         aParent.AppendChildL(element);
       
   440         if ( name.NotNull() ) name.Free();
       
   441         return element;
       
   442         }
       
   443 #else
       
   444 #ifdef XML_PREFIX_HACK
       
   445     HBufC8* t = HBufC8::NewLC( aName.Length() + aPrefix.Length() + 1 );
       
   446     if( aPrefix != KNullDesC8 )
       
   447         {
       
   448         t->Des().Append( aPrefix );
       
   449         t->Des().Append( ':' );
       
   450         }
       
   451     t->Des().Append( aName );
       
   452     TXmlEngElement element = aDocument.CreateElementL( *t, KNullDesC8, KNullDesC8 );
       
   453     DLINFO(("PREFIX HACK DONE 1 %S",t));
       
   454     CleanupStack::PopAndDestroy( t );
       
   455 #else
       
   456     TXmlEngElement element = aDocument.CreateElementL( aName, KNullDesC8, aPrefix );
       
   457 #endif
       
   458     aParent.AppendChildL(element);
       
   459     return element;
       
   460 #endif
       
   461     }
       
   462 
       
   463 TXmlEngElement NcdProtocolUtils::NewElementL( RXmlEngDocument& aDocument,
       
   464     TXmlEngElement& aParent, const TDesC8& aName, const TXmlEngString& aPrefix )
       
   465     {
       
   466     DLTRACEIN(("2 aName=%S",&aName ));
       
   467 #ifndef RD_XML_ENGINE_API_CHANGE
       
   468     TXmlEngString name;
       
   469     NcdProtocolUtils::DesToStringL(name, aName);
       
   470     if (aPrefix.NotNull())
       
   471         {
       
   472         TNamespace ns = aParent.LookupNamespaceByPrefix(aPrefix);
       
   473         TXmlEngElement element = aDocument.CreateElementL(name, NULL, aPrefix);
       
   474         aParent.AppendChildL(element);
       
   475         if ( name.NotNull() ) name.Free();
       
   476         return element;
       
   477         }
       
   478     else 
       
   479         {
       
   480         TXmlEngElement element = aDocument.CreateElementL(name);
       
   481         aParent.AppendChildL(element);
       
   482         if ( name.NotNull() ) name.Free();
       
   483         return element;
       
   484         }
       
   485 #else
       
   486     return NcdProtocolUtils::NewElementL( aDocument, aParent, aName, aPrefix.PtrC8() );
       
   487 #endif
       
   488     }
       
   489 
       
   490 TXmlEngElement NcdProtocolUtils::NewElementL(
       
   491     RXmlEngDocument& aDocument, const TDesC8& aName, const TDesC8& aPrefix )
       
   492     {
       
   493     DLTRACEIN(("3 aName=%S aPrefix=%S",&aName,&aPrefix ));
       
   494 #ifndef RD_XML_ENGINE_API_CHANGE
       
   495     TXmlEngString name;
       
   496     NcdProtocolUtils::DesToStringL(name, aName);
       
   497     if (aPrefix != KNullDesC8)
       
   498         {
       
   499         TXmlEngString prefix;
       
   500         NcdProtocolUtils::DesToStringL(prefix, aPrefix);
       
   501         TXmlEngElement element = aDocument.CreateElementL(name, NULL, prefix);
       
   502         if ( name.NotNull() ) name.Free();
       
   503         if ( prefix.NotNull() ) prefix.Free();
       
   504         return element;
       
   505         }
       
   506     else 
       
   507         {
       
   508         TXmlEngElement element = aDocument.CreateElementL(name);
       
   509         if ( name.NotNull() ) name.Free();
       
   510         return element;
       
   511         }
       
   512 #else
       
   513 
       
   514 #ifdef XML_PREFIX_HACK
       
   515     HBufC8* t = HBufC8::NewLC( aName.Length() + aPrefix.Length() + 1 );
       
   516     if( aPrefix != KNullDesC8 )
       
   517         {
       
   518         t->Des().Append( aPrefix );
       
   519         t->Des().Append( ':' );
       
   520         }
       
   521     t->Des().Append( aName );
       
   522     TXmlEngElement element = aDocument.CreateElementL( *t, KNullDesC8, KNullDesC8 );
       
   523     DLINFO(("PREFIX HACK DONE 2 %S",t));
       
   524     CleanupStack::PopAndDestroy( t );
       
   525     return element;
       
   526 #else
       
   527     return aDocument.CreateElementL( aName, KNullDesC8, aPrefix );
       
   528 #endif
       
   529 
       
   530 #endif
       
   531     }
       
   532 
       
   533 TXmlEngElement NcdProtocolUtils::NewElementL(
       
   534     RXmlEngDocument& aDocument, const TDesC8& aName, const TXmlEngString& aPrefix )
       
   535     {
       
   536     DLTRACEIN(("4 aName=%S",&aName ));
       
   537 #ifndef RD_XML_ENGINE_API_CHANGE
       
   538     TXmlEngString name;
       
   539     NcdProtocolUtils::DesToStringL(name, aName);
       
   540     if (aPrefix.NotNull())
       
   541         {
       
   542         TXmlEngElement element = aDocument.CreateElementL(name, NULL, aPrefix);
       
   543         if ( name.NotNull() ) name.Free();
       
   544         return element;
       
   545         }
       
   546     else 
       
   547         {
       
   548         TXmlEngElement element = aDocument.CreateElementL(name);
       
   549         if ( name.NotNull() ) name.Free();
       
   550         return element;
       
   551         }
       
   552 #else
       
   553 
       
   554 #ifdef XML_PREFIX_HACK
       
   555     TPtrC8 prefix = aPrefix.PtrC8();
       
   556     HBufC8* t = HBufC8::NewLC( aName.Length() + prefix.Length() + 1 );
       
   557     if( prefix != KNullDesC8 )
       
   558         { 
       
   559         t->Des().Append( prefix );
       
   560         t->Des().Append( ':' );
       
   561         }
       
   562     t->Des().Append( aName );
       
   563     TXmlEngElement element = aDocument.CreateElementL( *t, KNullDesC8, KNullDesC8 );
       
   564     DLINFO(("PREFIX HACK DONE 3 %S",t));
       
   565     CleanupStack::PopAndDestroy( t );
       
   566     return element;
       
   567 #else
       
   568     return aDocument.CreateElementL( aName, KNullDesC8, aPrefix.PtrC8() );
       
   569 #endif
       
   570 
       
   571 
       
   572 
       
   573 #endif
       
   574     }
       
   575 
       
   576 HBufC8* NcdProtocolUtils::DecodeBase64LC( const TDesC8& aData )
       
   577     {
       
   578     TBuf8<1> temp;
       
   579     TInt decodedLength = DecodeBase64L( aData, temp, ETrue );
       
   580 
       
   581     HBufC8* buffer = HBufC8::NewLC( decodedLength );
       
   582     TPtr8 ptr( buffer->Des() );
       
   583     DecodeBase64L( aData, ptr, EFalse );
       
   584     return buffer;
       
   585     }
       
   586 
       
   587 HBufC8* NcdProtocolUtils::DecodeBase64L( const TDesC8& aData )
       
   588     {
       
   589     HBufC8* b = DecodeBase64LC( aData );
       
   590     CleanupStack::Pop( b );
       
   591     return b;
       
   592     }
       
   593 
       
   594 TInt NcdProtocolUtils::DecodeBase64L( const TDesC8& aData, 
       
   595                                       TDes8& aOutput,
       
   596                                       TBool aCalculateLength )
       
   597     {
       
   598     TInt i = 0;
       
   599     TInt len = aData.Length();
       
   600     TInt outPosition = 0;
       
   601         
       
   602     for ( ;; ) 
       
   603         {
       
   604         // Skip whitespace
       
   605         while ( i < len && aData[i] <= ' ' )
       
   606             {
       
   607             i++;
       
   608             }
       
   609             
       
   610         // End of data reached
       
   611         if ( i == len )
       
   612             {
       
   613             break;
       
   614             }
       
   615 
       
   616         TInt tri = 
       
   617             ( DecodeBase64L( aData[i] ) << 18 )
       
   618             + ( DecodeBase64L( aData[i + 1] ) << 12 )
       
   619             + ( DecodeBase64L( aData[i + 2] ) << 6 )
       
   620             + ( DecodeBase64L( aData[i + 3] ) );
       
   621 
       
   622         if ( ! aCalculateLength )
       
   623             {
       
   624             aOutput.Append( TUint8( ( tri >> 16 ) & 255 ) );
       
   625             }
       
   626         outPosition++;;
       
   627 
       
   628         if ( aData[i + 2] == '=' )
       
   629             {
       
   630             break;
       
   631             }
       
   632 
       
   633         if ( ! aCalculateLength )
       
   634             {
       
   635             aOutput.Append( TUint8( ( tri >> 8 ) & 255 ) );
       
   636             }
       
   637         outPosition++;
       
   638 
       
   639         if ( aData[i + 3] == '=' )
       
   640             {
       
   641             break;
       
   642             }
       
   643  
       
   644         if ( ! aCalculateLength )
       
   645             {
       
   646             aOutput.Append( TUint8( tri & 255 ) );
       
   647             }
       
   648         outPosition++;
       
   649         i += 4;
       
   650         }
       
   651     return outPosition;
       
   652     }
       
   653 
       
   654 TInt NcdProtocolUtils::DecodeBase64L( TInt c ) 
       
   655     {
       
   656     if ( c >= 'A' && c <= 'Z' )
       
   657         {
       
   658         return c - 65;
       
   659         }
       
   660     else if ( c >= 'a' && c <= 'z' )
       
   661         {
       
   662         return c - 97 + 26;
       
   663         }
       
   664     else if ( c >= '0' && c <= '9' )
       
   665         { 
       
   666         return c - 48 + 26 + 26;
       
   667         }
       
   668     else
       
   669         {
       
   670         switch ( c ) 
       
   671             {
       
   672             case '+':
       
   673                 return 62;
       
   674             case '/':
       
   675                 return 63;
       
   676             case '=':
       
   677                 return 0;
       
   678             default:
       
   679                 DLERROR(("Conversion error %d",c));
       
   680                 User::Leave( KErrCorrupt );
       
   681                 return 0; // to suppress compiler warning
       
   682             }
       
   683         }
       
   684     }
       
   685 
       
   686 
       
   687 TTime NcdProtocolUtils::DesToTimeL(const TDesC8& aDes) 
       
   688     {
       
   689     if (aDes == KNullDesC8)
       
   690         {
       
   691         return TTime(0);
       
   692         }
       
   693 
       
   694     // Date example: 2006-05-17T09:30:47.0Z
       
   695     // Month and day in TTime's date format begin from 0.
       
   696 
       
   697     // Read date components    
       
   698     TInt year = DesDecToIntL( aDes.Mid( 0,4 ) );
       
   699     TMonth month = TMonth(DesDecToIntL( aDes.Mid( 5,2 ) ) -1 ); // -1 needed
       
   700     TInt day = DesDecToIntL( aDes.Mid( 8,2 ) ) -1; // -1 needed
       
   701     TInt hour = DesDecToIntL( aDes.Mid( 11,2 ) );
       
   702     TInt minute = DesDecToIntL( aDes.Mid( 14,2 ) );
       
   703     TInt second = DesDecToIntL( aDes.Mid( 17,2 ) );
       
   704 
       
   705     // create TDateTime object from components
       
   706     // (no need to use format strings this way)
       
   707     TDateTime date;
       
   708     date.Set(year, month, day, hour, minute, second, 0);
       
   709 
       
   710     // create TTime object from TDateTime
       
   711     TTime time = TTime(date);
       
   712     return time;
       
   713     }
       
   714     
       
   715 TTime NcdProtocolUtils::DesToTimeL(const TDesC16& aDes) 
       
   716     {
       
   717     if (aDes == KNullDesC16)
       
   718         {
       
   719         return TTime(0);
       
   720         }
       
   721 
       
   722     // Date example: 2006-05-17T09:30:47.0Z
       
   723     // Month and day in TTime's date format begin from 0.
       
   724 
       
   725     // Read date components    
       
   726     TInt year = DesDecToIntL( aDes.Mid( 0,4 ) );
       
   727     TMonth month = TMonth(DesDecToIntL( aDes.Mid( 5,2 ) ) -1 ); // -1 needed
       
   728     TInt day = DesDecToIntL( aDes.Mid( 8,2 ) ) -1; // -1 needed
       
   729     TInt hour = DesDecToIntL( aDes.Mid( 11,2 ) );
       
   730     TInt minute = DesDecToIntL( aDes.Mid( 14,2 ) );
       
   731     TInt second = DesDecToIntL( aDes.Mid( 17,2 ) );
       
   732 
       
   733     // create TDateTime object from components
       
   734     // (no need to use format strings this way)
       
   735     TDateTime date;
       
   736     date.Set(year, month, day, hour, minute, second, 0);
       
   737 
       
   738     // create TTime object from TDateTime
       
   739     TTime time = TTime(date);
       
   740     return time;
       
   741     }
       
   742     
       
   743 void NcdProtocolUtils::TimeToDesL(const TTime aTime, HBufC16*& aDes)
       
   744     {
       
   745     TBuf<24> buf;
       
   746     _LIT(KFormat, "%F%Y-%M-%DT%H:%T:%S.0Y");
       
   747     aTime.FormatL(buf, KFormat);
       
   748     aDes = buf.AllocL();
       
   749     }
       
   750 
       
   751 // void NcdProtocolUtils::DumpInfo( const Xml::RTagInfo& aElement,
       
   752 //                                  const Xml::RAttributeArray& aAttributes )
       
   753 //     {
       
   754 //     DLINFO(("localname %S",&aElement.LocalName().DesC()));
       
   755 //     DLINFO(("      uri %S",&aElement.Uri().DesC()));
       
   756 //     DLINFO(("   prefix %S",&aElement.Prefix().DesC()));
       
   757 //     for( TInt i = 0; i < aAttributes.Count(); i++ )
       
   758 //         {
       
   759 //         DLINFO(("-attribute %d",i));
       
   760 //         DLINFO(("-      uri %S",  &aAttributes[i].Attribute().Uri().DesC() ));
       
   761 //         DLINFO(("-localname %S",  &aAttributes[i].Attribute().LocalName().DesC() ));
       
   762 //         DLINFO(("-   prefix %S",  &aAttributes[i].Attribute().Prefix().DesC() ));
       
   763 //         DLINFO(("-    value %S",  &aAttributes[i].Value().DesC() ));
       
   764 //         DLINFO(("-     type %d",  aAttributes[i].Type() ));
       
   765 //         }
       
   766 //     }
       
   767