upnpmediaserver/contentdirectoryservice/src/upnpcdutils.cpp
changeset 0 7f85d04be362
child 25 52826dcbed74
equal deleted inserted replaced
-1:000000000000 0:7f85d04be362
       
     1 /** @file
       
     2 * Copyright (c) 2005-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:  ContentDirectory utils
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <uri8.h>
       
    22 #include "upnpcdutils.h"
       
    23 #include "upnpcontentdirectoryglobals.h"
       
    24 #include "upnpdominterface.h"
       
    25 #include "upnperror.h"
       
    26 #include "upnpcommonupnplits.h"
       
    27 #include "upnpstring.h"
       
    28 #include "upnpcons.h"
       
    29 #include <utf.h>
       
    30 #include <xmlengdocument.h>
       
    31 #include <e32math.h>
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CUpnpContentDirectoryDb::RemoveWhiteSpacesL
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 void UpnpCdUtils::RemoveWhiteSpacesL(TDes8& aString)
       
    40 {
       
    41     for(TInt i=0; i<aString.Length(); i++)
       
    42     {
       
    43         if(    aString[i] == ' '
       
    44            ||  aString[i] == '\r'
       
    45            ||  aString[i] == '\n'
       
    46            ||  aString[i] == '\t')
       
    47         {
       
    48             aString.Replace(i,1,_L8(""));
       
    49             i--;
       
    50         }
       
    51     }
       
    52 }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // UpnpCdUtils::IsWhiteString
       
    56 // (other items were commented in a header).
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 TBool UpnpCdUtils::IsWhiteString(const TDesC8& aString)
       
    60 {
       
    61     TBool ret = ETrue;
       
    62     for(TInt i = 0; i < aString.Length(); i++)
       
    63     {
       
    64         if( !(  aString[i] == '\n' ||
       
    65                 aString[i] == '\r' ||
       
    66                 aString[i] == ' '  ||
       
    67                 aString[i] == '\t' )  ) 
       
    68         {
       
    69             ret = EFalse;
       
    70             break;
       
    71         }
       
    72     }
       
    73     return ret;
       
    74 }
       
    75 // -----------------------------------------------------------------------------
       
    76 // UpnpCdUtils::GetObjectElementL
       
    77 // (other items were commented in a header).
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 TXmlEngElement UpnpCdUtils::GetObjectElementL(RXmlEngDocument& aFragment)
       
    81 {
       
    82     TXmlEngElement element;
       
    83     // try to get container
       
    84     UpnpDomInterface::GetElementL( aFragment.DocumentElement(), element, KContainer );
       
    85     if(element.IsNull())
       
    86     { // this is not a container
       
    87         // try to get an item
       
    88         UpnpDomInterface::GetElementL( aFragment.DocumentElement(), element, KItem );
       
    89         if(element.IsNull())
       
    90         { // this is neither an item - error
       
    91             User::Leave(EInvalidArgs);
       
    92         }
       
    93     }
       
    94     return element;
       
    95 }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // UpnpCdUtils::HasRefIdL
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 TBool UpnpCdUtils::HasRefIdL( RXmlEngDocument& aFragment )
       
   102     {
       
   103     TXmlEngElement object = UpnpCdUtils::GetObjectElementL( aFragment );
       
   104     //reference id value
       
   105     TPtrC8 refID = UpnpDomInterface::GetAttrValueL( object, KRefID );
       
   106     return !refID.CompareF( KNullDesC8 ) ? EFalse : ETrue;
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // UpnpCdUtils::EncodeXmlStringL
       
   111 // (other items were commented in a header).
       
   112 // -----------------------------------------------------------------------------
       
   113 //
       
   114 HBufC8* UpnpCdUtils::EncodeXmlStringL(const TDesC8& aString)
       
   115 {
       
   116     // new buffer size 
       
   117     TInt extensionSize = 0;
       
   118     for(TInt i = 0; i < aString.Length(); i++)
       
   119     {
       
   120         if(aString[i] == '&')       extensionSize += KEtAmp().Length() - 1;
       
   121         else if(aString[i] == '<')  extensionSize += KEtlt().Length() - 1;
       
   122         else if(aString[i] == '>')  extensionSize += KEtgt().Length() - 1;
       
   123         else if(aString[i] == '\"') extensionSize += KEtQuot().Length() - 1;
       
   124         else if(aString[i] == '\'') extensionSize += KEtApos().Length() - 1;
       
   125     }
       
   126     
       
   127     // alloc new buffer
       
   128     HBufC8* ret = HBufC8::NewLC(aString.Length() + extensionSize);
       
   129     TPtr8 retPtr(ret->Des());
       
   130     retPtr.Copy(aString);
       
   131     
       
   132     // replace
       
   133     for(TInt i = 0; i < retPtr.Length(); i++)
       
   134     {
       
   135         if(retPtr[i] == '&')        retPtr.Replace(i, 1, KEtAmp);
       
   136         else if(retPtr[i] == '<')   retPtr.Replace(i, 1, KEtlt);
       
   137         else if(retPtr[i] == '>')   retPtr.Replace(i, 1, KEtgt);
       
   138         else if(retPtr[i] == '\"')  retPtr.Replace(i, 1, KEtQuot);
       
   139         else if(retPtr[i] == '\'')  retPtr.Replace(i, 1, KEtApos);
       
   140     }
       
   141     
       
   142     // clean up
       
   143     CleanupStack::Pop(ret);
       
   144     
       
   145     return ret; 
       
   146 }
       
   147 // -----------------------------------------------------------------------------
       
   148 // UpnpCdUtils::ReplaceTrueFalse
       
   149 // (other items were commented in a header).
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 void UpnpCdUtils::ReplaceTrueFalse(HBufC8* aBuf)
       
   153 {
       
   154     TPtr8 ptr(aBuf->Des());
       
   155     TInt pos;
       
   156     // false -> 0
       
   157     pos = ptr.FindC(KFalseString8);
       
   158     if (pos != KErrNotFound)
       
   159     {
       
   160         ptr.Replace(pos, KFalseString8().Length(), KFalseValue8);
       
   161     }
       
   162     // true -> 1
       
   163     pos = ptr.FindC(KTrueString8);
       
   164     if (pos != KErrNotFound)
       
   165     {
       
   166         ptr.Replace(pos, KTrueString8().Length(), KTrueValue8);
       
   167     }
       
   168 }
       
   169 // -----------------------------------------------------------------------------
       
   170 // UpnpCdUtils::GetElmNameWithNsL
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 HBufC8* UpnpCdUtils::GetElmNameWithNsL(TXmlEngElement aElement)
       
   174 {
       
   175     TPtrC8 elmName( aElement.Name() );
       
   176     HBufC8* ret = NULL;
       
   177     if( aElement.NamespaceDeclaration().Prefix().Length() )
       
   178     {
       
   179         TPtrC8 prefix( aElement.NamespaceDeclaration().Prefix() );
       
   180         ret = HBufC8::NewL(elmName.Length() + prefix.Length() + 1);
       
   181         TPtr8 ptr(ret->Des());
       
   182         
       
   183         // concatenate prefix and name
       
   184         ptr.Copy(prefix);
       
   185         ptr.Append(KCol);
       
   186         ptr.Append(elmName);
       
   187     }
       
   188     else
       
   189     {
       
   190         ret = elmName.AllocL();
       
   191     }
       
   192     
       
   193     return ret; // the caller should delete it
       
   194 }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // UpnpCdUtils::Des8ToDesL
       
   198 // (other items were commented in a header).
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 HBufC* UpnpCdUtils::Des8ToDesL(const TDesC8& aString)
       
   202 {
       
   203     HBufC* ret = Des8ToDesLC(aString);
       
   204     CleanupStack::Pop(ret);
       
   205     return ret;
       
   206 }
       
   207 // -----------------------------------------------------------------------------
       
   208 // UpnpCdUtils::Des8ToDesLC
       
   209 // (other items were commented in a header).
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 HBufC* UpnpCdUtils::Des8ToDesLC(const TDesC8& aString)
       
   213 {    
       
   214     HBufC* ret = UpnpString::ToUnicodeL(aString);
       
   215     CleanupStack::PushL(ret);
       
   216     return ret;
       
   217 }
       
   218 // -----------------------------------------------------------------------------
       
   219 // UpnpCdUtils::DesToDes8LC
       
   220 // (other items were commented in a header).
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 HBufC8* UpnpCdUtils::DesToDes8LC(const TDesC& aString)
       
   224 {
       
   225     HBufC8* ret = UpnpString::FromUnicodeL(aString);
       
   226     CleanupStack::PushL(ret);
       
   227     return ret;
       
   228 }
       
   229 // -----------------------------------------------------------------------------
       
   230 // UpnpCdUtils::ResIdFromUriL
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 TInt64 UpnpCdUtils::ResIdFromUriL(const TDesC8& aUri)
       
   234 {
       
   235     TUriParser8 up;
       
   236     User::LeaveIfError(up.Parse(aUri));
       
   237     TPtrC8 path(up.Extract(EUriPath));
       
   238     if (path.Length() == 0)
       
   239     {
       
   240     User::Leave(KErrNotFound);
       
   241     }
       
   242     
       
   243     if(path[0] == '/')
       
   244     {
       
   245         path.Set(path.Mid(1));
       
   246     }
       
   247     TInt index = path.Locate(TChar('/'));
       
   248     
       
   249     if ((index>0)&&(index != KMaxIDResLength))
       
   250     {
       
   251     	User::Leave(KErrNotFound);    
       
   252     }
       
   253     TLex8 lexer(path);
       
   254     TInt64 ret;
       
   255     User::LeaveIfError(lexer.Val(ret));
       
   256     return ret;
       
   257 }
       
   258 // -----------------------------------------------------------------------------
       
   259 // UpnpCdUtils::ResIdFromUriL
       
   260 // (other items were commented in a header).
       
   261 // -----------------------------------------------------------------------------
       
   262 //
       
   263 TPtrC8 UpnpCdUtils::ResIdFromUriDesL(const TDesC8& aUri)
       
   264 {
       
   265     TUriParser8 up;
       
   266     User::LeaveIfError(up.Parse(aUri));
       
   267     TPtrC8 path(up.Extract(EUriPath));
       
   268     if (path.Length() == 0)
       
   269     {
       
   270     User::Leave(KErrNotFound);
       
   271     }
       
   272     
       
   273     if(path[0] == '/')
       
   274     {
       
   275         path.Set(path.Mid(1));
       
   276     }
       
   277     
       
   278     TInt index = path.Locate(TChar('/'));
       
   279     
       
   280     if (index > 0)       
       
   281     {
       
   282         path.Set(path.Left(index));        
       
   283     }
       
   284     
       
   285     return path;
       
   286 }
       
   287 // -----------------------------------------------------------------------------
       
   288 // UpnpCdUtils::SetObjectIdL
       
   289 // (other items were commented in a header).
       
   290 // -----------------------------------------------------------------------------
       
   291 //
       
   292 void UpnpCdUtils::SetObjectIdL(TXmlEngElement aElement, TInt aId)
       
   293 {
       
   294     TXmlEngAttr idAttr = aElement.AttributeNodeL(KIdAttrName());
       
   295     TBuf8<KMaxIntegerLen> idVal;
       
   296     idVal.Num(aId);
       
   297     if(idAttr.NotNull())
       
   298     {
       
   299         idAttr.SetValueL(idVal);
       
   300     }
       
   301     else
       
   302     {
       
   303         aElement.AddNewAttributeL(KIdAttrName(), idVal);
       
   304     }
       
   305 }
       
   306 // -----------------------------------------------------------------------------
       
   307 // UpnpCdUtils::SetObjectRefIdL
       
   308 // -----------------------------------------------------------------------------
       
   309 //
       
   310 void UpnpCdUtils::SetObjectRefIdL(TXmlEngElement aElement, TInt aRefId)
       
   311 {
       
   312     TXmlEngAttr refIdAttr = aElement.AttributeNodeL( KRefID() );
       
   313     TBuf8<KMaxIntegerLen> refIdVal;
       
   314     refIdVal.Num( aRefId );
       
   315     if( refIdAttr.NotNull() )
       
   316         {
       
   317         refIdAttr.SetValueL( refIdVal );
       
   318         }
       
   319     else
       
   320         {
       
   321         aElement.AddNewAttributeL( KRefID(), refIdVal );
       
   322         }
       
   323 }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // UpnpCdUtils::SetObjectParentIdL
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 void UpnpCdUtils::SetObjectParentIdL(TXmlEngElement aElement, TInt aParentId)
       
   330 {
       
   331     TXmlEngAttr parentIdAttr = aElement.AttributeNodeL( KParentID() );
       
   332     TBuf8<KMaxIntegerLen> parentIdVal;
       
   333     parentIdVal.Num( aParentId );
       
   334     if( parentIdAttr.NotNull() )
       
   335         {
       
   336         parentIdAttr.SetValueL( parentIdVal );
       
   337         }
       
   338     else
       
   339         {
       
   340         aElement.AddNewAttributeL( KParentID(), parentIdVal );
       
   341         }
       
   342 }
       
   343 // -----------------------------------------------------------------------------
       
   344 // UpnpCdUtils::SetRestrictedFieldL
       
   345 // -----------------------------------------------------------------------------
       
   346 //
       
   347 void UpnpCdUtils::SetRestrictedFieldL(TXmlEngElement aElement, TBool aRestrictedFlag)
       
   348 {
       
   349     TXmlEngAttr restrictedField = aElement.AttributeNodeL( KRestricted() );    
       
   350     TBuf8<KMaxIntegerLen> restrictedVal;
       
   351     
       
   352     if( aRestrictedFlag )
       
   353         {
       
   354         restrictedVal.Copy( KTrueValue8 );
       
   355         }    
       
   356     else
       
   357         {
       
   358         restrictedVal.Copy( KFalseValue8 );
       
   359         }    
       
   360 
       
   361     if( restrictedField.NotNull() )
       
   362         {
       
   363         restrictedField.SetValueL( restrictedVal );
       
   364         }
       
   365     else
       
   366         {
       
   367         aElement.AddNewAttributeL( KRestricted(), restrictedVal );
       
   368         }
       
   369 }
       
   370 
       
   371 // UpnpCdUtils::SetObjectIdL
       
   372 // (other items were commented in a header).
       
   373 // -----------------------------------------------------------------------------
       
   374 //
       
   375 void UpnpCdUtils::SetContainerIdL(TXmlEngElement aElement, TInt aParentId)
       
   376 {
       
   377     TXmlEngAttr idAttr = aElement.AttributeNodeL(KParentID());
       
   378     TBuf8<KMaxIntegerLen> idVal;
       
   379     idVal.Num(aParentId);
       
   380     if(idAttr.NotNull())
       
   381     {
       
   382         idAttr.SetValueL(idVal);
       
   383     }
       
   384     else
       
   385     {
       
   386         aElement.AddNewAttributeL(KParentID(), idVal);
       
   387     }
       
   388 }
       
   389 // -----------------------------------------------------------------------------
       
   390 // UpnpCdUtils::IsElementRequiredL
       
   391 // (other items were commented in a header).
       
   392 // -----------------------------------------------------------------------------
       
   393 //
       
   394 TBool UpnpCdUtils::IsElementRequiredL(TXmlEngElement aElement)
       
   395 {
       
   396     TBool ret = EFalse;
       
   397     TXmlEngAttr reqAttr = aElement.AttributeNodeL( KRequiredAtrName() );
       
   398     if(reqAttr.NotNull())
       
   399     { // element required
       
   400         ret = ETrue;
       
   401         reqAttr.Remove();
       
   402     }   
       
   403     return ret;
       
   404 }
       
   405 // -----------------------------------------------------------------------------
       
   406 // UpnpCdUtils::BuildImportUriL
       
   407 // (other items were commented in a header).
       
   408 // -----------------------------------------------------------------------------
       
   409 HBufC8* UpnpCdUtils::BuildImportUriLC(TInt aResId)
       
   410 {
       
   411 	// get next resId
       
   412 	TBuf8<KMaxIntegerLen + KRandomRangeLength> num;
       
   413 	TBuf8<KMaxIntegerLen + KRandomRangeLength> uriId;
       
   414 	num.Num(aResId);
       
   415 	uriId.Justify(num, KMaxIntegerLen + KRandomRangeLength, ERight, '0');
       
   416 
       
   417 	// build uri
       
   418 	HBufC8* impUri = HBufC8::NewLC(
       
   419 						  KHttpTag().Length()
       
   420 						+ KIpPortPlaceholder8().Length()						
       
   421 						+ KSlash8().Length()						
       
   422 						+ KMaxLongIntegerLen
       
   423 						+ KRandomRangeLength );
       
   424 	TPtr8 impUriPtr(impUri->Des());
       
   425 	impUriPtr.Append(KHttpTag);
       
   426 	impUriPtr.Append(KIpPortPlaceholder8);
       
   427 	impUriPtr.Append(KSlash8);		
       
   428 	impUriPtr.Append(uriId);
       
   429 	
       
   430 	
       
   431 	return impUri;
       
   432 }
       
   433 // -----------------------------------------------------------------------------
       
   434 // UpnpCdUtils::BuildImportUriL
       
   435 // -----------------------------------------------------------------------------
       
   436 HBufC8* UpnpCdUtils::BuildImportUriShorterLC(TInt64 aResId)
       
   437 {
       
   438 	// get next resId
       
   439 	TBuf8<KMaxIntegerLen > num;
       
   440 	TBuf8<KMaxIntegerLen > uriId;
       
   441 	num.Num(aResId);
       
   442 	uriId.Justify(num, KMaxIntegerLen , ERight, '0');
       
   443 
       
   444 	// build uri
       
   445 	HBufC8* impUri = HBufC8::NewLC(
       
   446 						  KHttpTag().Length()
       
   447 						+ KIpPortPlaceholder8().Length()						
       
   448 						+ KSlash8().Length()						
       
   449 						+ KMaxLongIntegerLen
       
   450 						 );
       
   451 	TPtr8 impUriPtr(impUri->Des());
       
   452 	impUriPtr.Append(KHttpTag);
       
   453 	impUriPtr.Append(KIpPortPlaceholder8);
       
   454 	impUriPtr.Append(KSlash8);		
       
   455 	impUriPtr.Append(uriId);
       
   456 	
       
   457 	
       
   458 	return impUri;
       
   459 }
       
   460 // -----------------------------------------------------------------------------
       
   461 // UpnpCdUtils::BuildContentUriL
       
   462 // (other items were commented in a header).
       
   463 // -----------------------------------------------------------------------------
       
   464 HBufC8* UpnpCdUtils::BuildContentUriL(TInt aResId, const TDesC& aFileExten,
       
   465                                            TDesC8& aObjectId)
       
   466 {
       
   467     HBufC8* number = UpnpCdUtils::RandomizeL(KRandomMax);
       
   468     CleanupStack::PushL(number);
       
   469     HBufC8* number2 = UpnpCdUtils::RandomizeL(KRandomMax);
       
   470     CleanupStack::PushL(number2);
       
   471     
       
   472 	HBufC8* impUri = BuildImportUriShorterLC(aResId);	
       
   473 	
       
   474     HBufC8* fileExten8 = UpnpCdUtils::DesToDes8LC(aFileExten);
       
   475 	HBufC8* contUri = HBufC8::NewLC(
       
   476 								impUri->Length() 
       
   477 								+ number->Des().Length()
       
   478 								+ number2->Des().Length()
       
   479 								+ KSlash().Length()
       
   480                                 + aObjectId.Length()                                
       
   481                                 + fileExten8->Length());
       
   482 	TPtr8 contUriPtr(contUri->Des());
       
   483 	contUriPtr.Copy(*impUri);
       
   484 	contUriPtr.Append(*number);	
       
   485 	contUriPtr.Append(*number2);
       
   486 	contUriPtr.Append(KSlash);
       
   487     contUriPtr.Append(aObjectId);    
       
   488     contUriPtr.Append(*fileExten8);
       
   489 
       
   490     // clean up
       
   491 	CleanupStack::Pop(contUri);
       
   492     CleanupStack::PopAndDestroy(fileExten8);
       
   493 	CleanupStack::PopAndDestroy(impUri);
       
   494 	CleanupStack::PopAndDestroy(number2);
       
   495 	CleanupStack::PopAndDestroy(number);
       
   496 	return contUri;
       
   497 }
       
   498 // -----------------------------------------------------------------------------
       
   499 // UpnpCdUtils::RandomizeL
       
   500 // -----------------------------------------------------------------------------
       
   501 HBufC8* UpnpCdUtils::RandomizeL(TInt aRange)
       
   502 {
       
   503     TUint number = Math::Random();
       
   504     number = number % aRange;    
       
   505     HBufC8* numberDes = HBufC8::NewL(KRandomRangeLength);               
       
   506    	TBuf8<KRandomRangeLength> num;	
       
   507 	num.Num(number);
       
   508     numberDes->Des().Justify(num, KRandomRangeLength, ERight, '0');    
       
   509 
       
   510     return numberDes;
       
   511 }
       
   512 // -----------------------------------------------------------------------------
       
   513 // UpnpCdUtils::ValidateFilePath
       
   514 // -----------------------------------------------------------------------------
       
   515 void UpnpCdUtils::ValidateFilePath(TDes8& aPath)
       
   516 {
       
   517     // replace
       
   518     for(TInt i = 0; i < aPath.Length(); i++)
       
   519     {
       
   520         if( aPath[i] == ':' && i!=1 || 
       
   521             aPath[i] == '<'         ||
       
   522             aPath[i] == '>'         ||
       
   523             aPath[i] == '\"'        ||
       
   524             aPath[i] == '/'         ||    
       
   525             aPath[i] == '|'         ||
       
   526             aPath[i] == '*'       ||
       
   527             aPath[i] == '?'
       
   528           )
       
   529         {
       
   530             aPath[i] = '_';
       
   531         }
       
   532     }
       
   533 }
       
   534 // -----------------------------------------------------------------------------
       
   535 // UpnpCdUtils::ValidateFilePath
       
   536 // (other items were commented in a header).
       
   537 // -----------------------------------------------------------------------------
       
   538 void UpnpCdUtils::ValidateFilePath(TDes& aPath)
       
   539 {
       
   540     // replace
       
   541     for(TInt i = 0; i < aPath.Length(); i++)
       
   542     {
       
   543         if( aPath[i] == ':' && i!=1 || 
       
   544             aPath[i] == '<'         ||
       
   545             aPath[i] == '>'         ||
       
   546             aPath[i] == '\"'        ||
       
   547             aPath[i] == '/'         ||    
       
   548             aPath[i] == '|'         ||
       
   549             aPath[i] == '*'       ||
       
   550             aPath[i] == '?'
       
   551           )
       
   552         {
       
   553             aPath[i] = '_';
       
   554         }
       
   555     }
       
   556 }
       
   557 
       
   558 
       
   559 // -----------------------------------------------------------------------------
       
   560 // UpnpCdUtils::ValidateSrcUriL
       
   561 // (other items were commented in a header).
       
   562 // -----------------------------------------------------------------------------
       
   563 void UpnpCdUtils::ValidateSrcUriL(TDesC8& aPath,TDesC8& aIp )
       
   564 {
       
   565     // Check if parsing is correct
       
   566     TUriParser8 srcParser;
       
   567     if (!(srcParser.Parse(aPath) == KErrNone))
       
   568     {
       
   569         User::Leave( ENoSourceResource );                        
       
   570     }
       
   571     
       
   572     // check if source uri is correct one
       
   573     TInt pos = aPath.Find(aIp);
       
   574     if(pos != UpnpHTTP::KHTTPUrl().Length())
       
   575         {
       
   576         User::Leave( ENoSourceResource ); 
       
   577         }
       
   578 
       
   579     if (!(aPath.Find( UpnpHTTP::KHTTPUrl ) == 0))
       
   580     {
       
   581         User::Leave( ENoSourceResource );                        
       
   582     }
       
   583     TInt len = UpnpHTTP::KHTTPUrl().Length() + aIp.Length();
       
   584     if ( aPath.Length() <= len || aPath[ len ] != '/' )
       
   585         {
       
   586         User::Leave( ENoSourceResource );
       
   587         }
       
   588 }
       
   589 
       
   590 // -----------------------------------------------------------------------------
       
   591 // UpnpCdUtils::EscapeAposL
       
   592 // (other items were commented in a header).
       
   593 // -----------------------------------------------------------------------------
       
   594 HBufC* UpnpCdUtils::EscapeAposL(const TDesC&  aValue)
       
   595 {
       
   596     if (aValue.Length())
       
   597     {            
       
   598         HBufC8* value8 = DesToDes8LC(aValue);
       
   599         HBufC8* valueReplaced8 = UpnpString::StringReplaceL(*value8,_L8("'"),_L8("\'\'"));
       
   600         CleanupStack::Pop(value8);
       
   601         HBufC* valueReplaced16 = UpnpCdUtils::Des8ToDesL(*valueReplaced8);
       
   602         delete valueReplaced8;
       
   603         delete value8;        
       
   604         return valueReplaced16;
       
   605     }
       
   606     else
       
   607     {
       
   608         HBufC* valueReplaced16 = aValue.AllocL();
       
   609         return valueReplaced16;
       
   610     }   
       
   611 }
       
   612 
       
   613 // -----------------------------------------------------------------------------
       
   614 // CUpnpElementFactory::ValidateDateL()
       
   615 // Only Those date string is validate
       
   616 // YYYY-MM-DD
       
   617 // YYYY-MM-DDTHH:MM:SS
       
   618 // YYYY-MM-DDTHH:MM:SSZ
       
   619 // YYYY-MM-DDTHH:MM:SS.xxx
       
   620 // YYYY-MM-DDTHH:MM:SS.xxxZ
       
   621 // YYYY-MM-DDTHH:MM:SS+HH:MM
       
   622 // YYYY-MM-DDTHH:MM:SS.xxx+HH:MM
       
   623 // -----------------------------------------------------------------------------
       
   624 //
       
   625 TBool UpnpCdUtils::ValidateDateL( TPtrC8 aValue )
       
   626     {         
       
   627     TBool wrong = 0;
       
   628 
       
   629     TInt dateLength = aValue.Length();                                         
       
   630 
       
   631     if ((dateLength!=EDay)&&(dateLength!=ESecond)&&
       
   632         (dateLength!=ETimeOffset1)&&(dateLength!=EMiliSecond)&&
       
   633         (dateLength!=ETimeOffset2)&&(dateLength!=EZone1)&&
       
   634         (dateLength!=EZone2))                                     
       
   635         {
       
   636         return EFalse;                
       
   637         }
       
   638     else
       
   639         {
       
   640         const TChar KColo = ':';
       
   641         const TChar KMinus = '-';
       
   642         const TChar KPlus = '+';
       
   643         const TChar KDot = '.';       
       
   644         const TChar KT = 'T';
       
   645         const TChar KZ = 'Z'; 
       
   646 
       
   647         TChar ch;
       
   648         TLex8 input(aValue);         
       
   649         for (TInt i =0; i<aValue.Length();i++)
       
   650             {
       
   651             ch = input.Get();         
       
   652             if ((i!=EYear)&&(i!=EMonth)&&(i!=EDay)&&(i!=EHour)&&(i!=EMinute)&&(i!=ESecond)&&(i!=EHourOffset1)&&(i!=EMiliSecond)&&(i!=EHourOffset2))
       
   653                 {                
       
   654                 if (!ch.IsDigit())          
       
   655                     {
       
   656                     wrong = ETrue;
       
   657                     break;  
       
   658                     }
       
   659                 }
       
   660             else if (((i==EYear)||(i==EMonth))&&(ch!=KMinus))
       
   661                 {
       
   662                 wrong = ETrue;
       
   663                 break;  
       
   664                 }    
       
   665             else if ((i==EDay)&&(ch!= KT))
       
   666                 {
       
   667                 wrong = ETrue;
       
   668                 break;  
       
   669                 }     
       
   670             else if (((i==EHour)||(i==EMinute)||(i==EHourOffset2))&&(ch!=KColo))
       
   671                 {
       
   672                 wrong = ETrue;
       
   673                 break;  
       
   674                 }     
       
   675             else if (((i==ESecond)&&(dateLength==ETimeOffset1))&&((ch!=KPlus)&&(ch!=KMinus)))
       
   676                 {
       
   677                 wrong = ETrue;
       
   678                 break;  
       
   679                 }    
       
   680             else if (((i==ESecond)&&(dateLength==EZone1))&&(ch!=KZ))
       
   681                 {
       
   682                 wrong = ETrue;
       
   683                 break;  
       
   684                 }
       
   685             else if (((i==ESecond)&&((dateLength==ETimeOffset2)||(dateLength==EZone2)||(dateLength==EMiliSecond)))&&(ch!=KDot))
       
   686                 {
       
   687                 wrong = ETrue;
       
   688                 break;  
       
   689                 }              
       
   690             else if (((i==EHourOffset1)&&(dateLength==ETimeOffset1))&&(ch!=KColo))
       
   691                 {
       
   692                 wrong = ETrue;
       
   693                 break;  
       
   694                 }   
       
   695                                 
       
   696             else if ((i==EHourOffset1)&&((dateLength==ETimeOffset2)||(dateLength==EZone2)||(dateLength==EMiliSecond)))
       
   697                 {                    
       
   698                 if (!ch.IsDigit())          
       
   699                     {
       
   700                     wrong = ETrue;
       
   701                     break;  
       
   702                     }
       
   703                 }
       
   704             else if (((i==EMiliSecond)&&(dateLength==ETimeOffset2))&&((ch!=KPlus)&&(ch!=KMinus)))
       
   705                 {
       
   706                 wrong = ETrue;
       
   707                 break;  
       
   708                 }            
       
   709             else if (((i==EMiliSecond)&&(dateLength==EZone2))&&(ch!=KZ))
       
   710                 {
       
   711                 wrong = ETrue;
       
   712                 break;  
       
   713                 }      
       
   714             else if ((i==EMiliSecond)&&(dateLength==ETimeOffset1))
       
   715                 {                
       
   716                 if (!ch.IsDigit())          
       
   717                     {
       
   718                     wrong = ETrue;
       
   719                     break;  
       
   720                     }                               
       
   721                 }
       
   722             }
       
   723         }
       
   724             
       
   725         TInt yearInt=0;
       
   726         TInt monthInt=0;
       
   727         TInt dayInt=0;
       
   728         TInt hourInt=0;
       
   729         TInt minuteInt=0;
       
   730         TInt secondInt=0;
       
   731         TInt milisecondInt=0;
       
   732         TInt hhInt=0;
       
   733         TInt mmInt=0;
       
   734         TDateTime a;
       
   735 
       
   736         TPtrC8 yearPtrC = aValue.Left(4);
       
   737         TPtrC8 tmp = aValue.Mid(5);
       
   738         TPtrC8 monthPtrC = tmp.Left(2);
       
   739         tmp.Set(aValue.Mid(8));
       
   740         TPtrC8 dayPtrC = tmp.Left(2);
       
   741 
       
   742         TLex8 lexer(yearPtrC);	
       
   743         lexer.Val(yearInt);
       
   744         lexer.Assign(monthPtrC);
       
   745         lexer.Val(monthInt);
       
   746         monthInt--;
       
   747         TMonth monthEnum = (TMonth)monthInt;
       
   748         lexer.Assign(dayPtrC);
       
   749         lexer.Val(dayInt);
       
   750 
       
   751         if (dateLength>EDay)                
       
   752             {
       
   753             tmp.Set(aValue.Mid(11));
       
   754             TPtrC8 hourPtrC = tmp.Left(2);
       
   755             tmp.Set(aValue.Mid(14));
       
   756             TPtrC8 minutePtrC = tmp.Left(2);
       
   757             tmp.Set(aValue.Mid(17));
       
   758             TPtrC8 secondPtrC = tmp.Left(2);  
       
   759             lexer.Assign(hourPtrC);
       
   760             lexer.Val(hourInt);
       
   761             lexer.Assign(minutePtrC);
       
   762             lexer.Val(minuteInt);
       
   763             lexer.Assign(secondPtrC);
       
   764             lexer.Val(secondInt);  
       
   765             }                
       
   766         if ((dateLength==ETimeOffset2)||(dateLength==EZone2)||(dateLength==EMiliSecond))
       
   767             {
       
   768             tmp.Set(aValue.Mid(20)); 
       
   769             TPtrC8 milisecondPtrC = tmp.Left(3);  
       
   770             lexer.Assign(milisecondPtrC);
       
   771             lexer.Val(milisecondInt);
       
   772             }
       
   773         if (dateLength==ETimeOffset1)
       
   774             {
       
   775             tmp.Set(aValue.Mid(20)); 
       
   776             TPtrC8 hhOffset = tmp.Left(2);
       
   777             tmp.Set(aValue.Mid(23)); 
       
   778             TPtrC8 mmOffset = tmp.Left(2);      
       
   779             lexer.Assign(hhOffset);
       
   780             lexer.Val(hhInt);
       
   781             
       
   782             if (hhInt>12) 
       
   783                 {
       
   784                 wrong = ETrue;                
       
   785                 }
       
   786             
       
   787             lexer.Assign(mmOffset);
       
   788             lexer.Val(mmInt);     
       
   789             
       
   790             if (mmInt>59) 
       
   791                 {
       
   792                 wrong = ETrue;   
       
   793                 }
       
   794             }
       
   795             
       
   796         if (dateLength==ETimeOffset2)
       
   797             {
       
   798             tmp.Set(aValue.Mid(24)); 
       
   799             TPtrC8 hhOffset = tmp.Left(2);
       
   800             tmp.Set(aValue.Mid(27)); 
       
   801             TPtrC8 mmOffset = tmp.Left(2);     
       
   802             lexer.Assign(hhOffset);
       
   803             lexer.Val(hhInt);
       
   804             if (hhInt>12) 
       
   805                 {
       
   806                 wrong = ETrue;                
       
   807                 }
       
   808             lexer.Assign(mmOffset);
       
   809             lexer.Val(mmInt); 
       
   810                 
       
   811             if (mmInt>59) 
       
   812                 {
       
   813                 wrong = ETrue;                
       
   814                 }
       
   815             }
       
   816             
       
   817         TInt err = a.Set(yearInt,monthEnum,dayInt,hourInt,minuteInt,secondInt,milisecondInt);
       
   818         if (err<0) 
       
   819             {
       
   820             wrong = ETrue;    
       
   821             }
       
   822 
       
   823     if (wrong)
       
   824         {                        
       
   825         return EFalse;      
       
   826         }
       
   827 
       
   828     return ETrue;
       
   829     }
       
   830     
       
   831 // -----------------------------------------------------------------------------
       
   832 // UpnpCdUtils::StringToUint
       
   833 // -----------------------------------------------------------------------------
       
   834     TInt UpnpCdUtils::StringToTUint(const TDesC8& aStr, TUint* aInt)
       
   835 	{
       
   836 	if( 0 == aStr.Length() || aInt == NULL)
       
   837 		{
       
   838 		return KErrArgument;
       
   839 		}
       
   840 	TLex8 lex( aStr );
       
   841 	return lex.Val( *aInt );
       
   842 	}                   
       
   843 
       
   844 // -----------------------------------------------------------------------------
       
   845 // UpnpCdUtils::SplitStringByDelimeter
       
   846 // -----------------------------------------------------------------------------
       
   847 void UpnpCdUtils::SplitStringByDelimeter(TLex8& aLexeme, TChar aDelimeter)
       
   848 {
       
   849 	aLexeme.Mark(); 
       
   850 	while( (aLexeme.Peek() != aDelimeter) && (!aLexeme.Eos()) )
       
   851 	{
       
   852 		aLexeme.Inc();		
       
   853 	}
       
   854 }
       
   855 
       
   856 // -----------------------------------------------------------------------------
       
   857 // UpnpCdUtils::Skip
       
   858 // -----------------------------------------------------------------------------
       
   859 void UpnpCdUtils::Skip(TLex8& aLexer, TInt aValue)
       
   860 {
       
   861     if(!aLexer.Eos())
       
   862 	{
       
   863 	    aLexer.Inc(aValue);
       
   864 	}
       
   865 }	
       
   866 
       
   867 // -----------------------------------------------------------------------------
       
   868 // UpnpCdUtils::AreDigitsInSpecificRange
       
   869 // -----------------------------------------------------------------------------    
       
   870 TBool UpnpCdUtils::AreDigitsInSpecificRange(const TDesC8& aInput, TInt aNumber, TBool aExactDigitsNumber, TUint32 aRange)
       
   871 {
       
   872     // checking length
       
   873     TInt length = aInput.Length();
       
   874     
       
   875     if(aExactDigitsNumber)
       
   876     {
       
   877         // if exact number of digits = real number of digits -> ok
       
   878         if(length != aNumber) 
       
   879             return EFalse;
       
   880     }
       
   881     else
       
   882     {
       
   883         // if real number of digits fit into specific range -> ok
       
   884         if(length > aNumber || length < 1)
       
   885             return EFalse;
       
   886     }
       
   887     
       
   888     // checking digits
       
   889     for(TInt i=0; i < length; i++)
       
   890     {
       
   891         // if input characters are all digits -> ok
       
   892         TChar tmp( aInput[i] );       
       
   893         if( !tmp.IsDigit() )
       
   894             return EFalse;            
       
   895     }
       
   896     
       
   897     // checking range
       
   898     TLex8 field(aInput);	        
       
   899     TUint32 value;
       
   900     
       
   901     // if e.g minutes fit into range: 00-59 -> ok
       
   902     TInt err = field.BoundedVal(value, EDecimal, aRange);
       
   903     
       
   904     if(err != KErrNone)
       
   905         return EFalse;
       
   906     
       
   907     return ETrue;
       
   908 }    
       
   909 
       
   910 // -----------------------------------------------------------------------------
       
   911 // UpnpCdUtils::ValidateDurationValueL
       
   912 // -----------------------------------------------------------------------------
       
   913 TBool UpnpCdUtils::ValidateDurationValue(const TDesC8& aInput)
       
   914 {
       
   915 	/*
       
   916 	* 7.3.22 MM DIDL-Lite res@duration Format
       
   917 	* Requirement [7.3.22.1]: The syntax of the res@duration must be compliant to the following definition:
       
   918 	*    - duration = hours ":" minutes ":" seconds
       
   919 	*    - hours = 1*5 DIGIT; 0-99999
       
   920 	*    - minutes = 2 DIGIT ; 00-59
       
   921 	*    - seconds = 2 DIGIT ["." 3 DIGIT] ; 00-59 (.000-.999)
       
   922 	*/
       
   923     TChar KColon(':');
       
   924     TChar KDot('.');
       
   925 
       
   926     TInt fieldNum = 1;        
       
   927     TLex8 input(aInput);
       
   928 		
       
   929 	while( !input.Eos() )
       
   930 	{		
       
   931 	    // extract subsequent field
       
   932 		SplitStringByDelimeter( input, KColon );
       
   933 		TPtrC8 field( input.MarkedToken() );
       
   934 		
       
   935 		// if its length != 0 ...
       
   936 		if( field != KNullDesC8() )
       
   937 		{		
       
   938 		    if(fieldNum == 1)
       
   939 		    {	
       
   940 		        // if 1st field = 1-5 digits in range: 0-99999 -> ok
       
   941 		        if( !AreDigitsInSpecificRange(field, 5, EFalse, 99999) )
       
   942 		            return EFalse;
       
   943 		            
       
   944 		        ++fieldNum;
       
   945 		    }
       
   946 		    else if(fieldNum == 2)
       
   947 		    {
       
   948 		        // if 2nd field = exactly 2 digits in range: 0-59 -> ok
       
   949 		        if( !AreDigitsInSpecificRange(field, 2, ETrue, 59) )
       
   950 		            return EFalse;
       
   951 		        
       
   952 		        ++fieldNum;
       
   953 		    }		    
       
   954 		    else if(fieldNum == 3)
       
   955 		    {
       
   956 		        TInt length = field.Length();
       
   957 		    
       
   958 		        TLex8 fourth(field);		        		        
       
   959 		        SplitStringByDelimeter( fourth, KDot );
       
   960 		        
       
   961 		        // if 3rd field = exactly 2 digits in range: 0-59 -> ok
       
   962 		        TPtrC8 leftPart( fourth.MarkedToken() );
       
   963 		        if( !AreDigitsInSpecificRange(leftPart, 2, ETrue, 59) )
       
   964 		            return EFalse;
       
   965 		        
       
   966 		        Skip(fourth, 1);
       
   967 		        TPtrC8 rightPart( fourth.Remainder() );
       
   968 		        
       
   969 		        // if 3rd field is divided by dot and right part is exactly 3 digits in range: 000-999 -> ok
       
   970 		        if( rightPart != KNullDesC8() )
       
   971 		        {		        
       
   972 		            if( !AreDigitsInSpecificRange(rightPart, 3, ETrue, 999) )
       
   973 		                return EFalse;
       
   974 		        }
       
   975 		        else if( field[length-1] == '.' )
       
   976 		            return EFalse;
       
   977 		        
       
   978 		        ++fieldNum;
       
   979 		    }
       
   980 		    else
       
   981 		        return EFalse;
       
   982 		}
       
   983 		else
       
   984 		{
       
   985 		    return EFalse;
       
   986 		}
       
   987 				
       
   988 		Skip(input,1);	
       
   989 	}
       
   990 	
       
   991 	// if while loop is completed successfully -> ok
       
   992 	if(fieldNum == 4)
       
   993 	    return ETrue;
       
   994 	else
       
   995 	    return EFalse;	    
       
   996 }
       
   997 
       
   998 
       
   999 // -----------------------------------------------------------------------------
       
  1000 // CUpnpContentDirectory::StringToInteger
       
  1001 // -----------------------------------------------------------------------------
       
  1002 //
       
  1003 TInt UpnpCdUtils::StringToInteger(const TDesC8& aStr, TInt* aInt)
       
  1004     {
       
  1005     if( aStr.Length() == 0 || aInt == NULL)
       
  1006         {
       
  1007         return KErrArgument;
       
  1008         }
       
  1009     
       
  1010     TLex8 lex(aStr);    
       
  1011     lex.Mark();
       
  1012     TBool isFirst = ETrue;
       
  1013     
       
  1014     while( !lex.Eos() )
       
  1015         {   
       
  1016         TChar tmp = lex.Get();
       
  1017         
       
  1018         if( tmp.IsDigit() )
       
  1019             {
       
  1020             if(isFirst && tmp == '0' && aStr.Length() > 1)
       
  1021                 {
       
  1022                 return KErrArgument;
       
  1023                 }                        
       
  1024             }
       
  1025         else
       
  1026             {
       
  1027             return KErrArgument;
       
  1028             }
       
  1029         
       
  1030         isFirst = EFalse;        
       
  1031         }
       
  1032                
       
  1033     lex.UnGetToMark();                    
       
  1034     return lex.Val( *aInt );
       
  1035     }
       
  1036 
       
  1037 //  End of File