realtimenetprots/sipfw/SIP/Codec/src/CSIPTokenizer.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // Name          : CSIPTokenizer.cpp
       
    15 // Part of       : Codec
       
    16 // Version       : SIP/3.0 
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 #include "CSIPTokenizer.h"
       
    23 #include "sipcodecerr.h"
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CSIPTokenizer::NewL
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 CSIPTokenizer* CSIPTokenizer::NewL(
       
    30     const TDesC8& aValue,
       
    31     const TChar& aSeparator,
       
    32     TBool aIgnoreParenthesis,
       
    33     TBool aIgnoreLastEmptyValue,
       
    34     TBool aSkipSeparatorsBeforeAt)
       
    35 	{
       
    36 	CSIPTokenizer* self = 
       
    37         CSIPTokenizer::NewLC( aValue,aSeparator, aIgnoreParenthesis,
       
    38         					  aIgnoreLastEmptyValue, aSkipSeparatorsBeforeAt );
       
    39 	CleanupStack::Pop(self);
       
    40 	return self;
       
    41 	} 
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // CSIPTokenizer::NewLC
       
    45 // -----------------------------------------------------------------------------
       
    46 //
       
    47 CSIPTokenizer* CSIPTokenizer::NewLC(
       
    48     const TDesC8& aValue,
       
    49     const TChar& aSeparator,
       
    50     TBool aIgnoreParenthesis,
       
    51     TBool aIgnoreLastEmptyValue,
       
    52     TBool aSkipSeparatorsBeforeAt)
       
    53 	{
       
    54     __ASSERT_ALWAYS (aValue.Length() > 0, User::Leave (KErrSipCodecTokenizer));
       
    55 	__ASSERT_ALWAYS (aSeparator != '\\', User::Leave (KErrSipCodecTokenizer));
       
    56 	__ASSERT_ALWAYS (aSeparator != '"', User::Leave (KErrSipCodecTokenizer));
       
    57 	__ASSERT_ALWAYS (aSeparator != '<', User::Leave (KErrSipCodecTokenizer));
       
    58 	__ASSERT_ALWAYS (aSeparator != '>', User::Leave (KErrSipCodecTokenizer));
       
    59     if(!aIgnoreParenthesis)
       
    60         {
       
    61         __ASSERT_ALWAYS (aSeparator != '(', User::Leave(KErrSipCodecTokenizer));
       
    62         __ASSERT_ALWAYS (aSeparator != ')', User::Leave(KErrSipCodecTokenizer));
       
    63         }
       
    64 
       
    65 	CSIPTokenizer* self = 
       
    66         new(ELeave)CSIPTokenizer(aValue,aSeparator,aIgnoreParenthesis,
       
    67         						  aIgnoreLastEmptyValue, 
       
    68         						  aSkipSeparatorsBeforeAt );
       
    69 	CleanupStack::PushL(self);
       
    70 	self->ConstructL();
       
    71 	return self;
       
    72 	}
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CSIPTokenizer::CSIPTokenizer
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CSIPTokenizer::CSIPTokenizer(const TDesC8& aValue, 
       
    79                              const TChar& aSeparator,
       
    80                              TBool aIgnoreParenthesis,
       
    81                              TBool aIgnoreLastEmptyValue,
       
    82                              TBool aSkipSeparatorsBeforeAt)
       
    83 : iOriginalValue (aValue),
       
    84   iLexer( aValue ),
       
    85   iSeparator (aSeparator),
       
    86   iPreviousWasBackSlash (EFalse),
       
    87   iInQuotes (EFalse),
       
    88   iInAngleBrackets  (EFalse),
       
    89   iInParenthesis (EFalse),
       
    90   iIgnoreParenthesis (aIgnoreParenthesis),
       
    91   iIgnoreLastEmptyValue ( aIgnoreLastEmptyValue ),
       
    92   iSkipSeparatorsBeforeAt ( aSkipSeparatorsBeforeAt )
       
    93 	{
       
    94 	}
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CSIPTokenizer::ConstructL
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void CSIPTokenizer::ConstructL()
       
   101 	{
       
   102 	iTokens = new(ELeave)CPtrC8Array(1);
       
   103 	TokenizeL();
       
   104 	}
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CSIPTokenizer::~CSIPTokenizer
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 CSIPTokenizer::~CSIPTokenizer()
       
   111 	{
       
   112     delete iTokens;
       
   113 	}
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CSIPTokenizer::Tokens
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 CPtrC8Array& CSIPTokenizer::Tokens()
       
   120 	{
       
   121 	return *iTokens;
       
   122 	}
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CSIPTokenizer::TokenizeL
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 void CSIPTokenizer::TokenizeL()
       
   129 	{
       
   130 	TChar chr = iLexer.Get();
       
   131 
       
   132 	while (chr)
       
   133 		{
       
   134 		iParsedSoFar++; iTokenLength++;
       
   135 		if (chr == '\\')
       
   136 			{
       
   137 			// If the previous character was already a backslash,
       
   138 			// then it is quoting the current backslash.
       
   139 			iPreviousWasBackSlash = !iPreviousWasBackSlash;
       
   140 			}
       
   141 		else
       
   142 			{
       
   143 			switch (chr)
       
   144 				{
       
   145 				case '"' : HandleQuote(); break;
       
   146 				case '<' : HandleOpeningAngleBracketL(); break;
       
   147 				case '>' : HandleClosingAngleBracketL(); break;
       
   148                 case '(' : HandleOpeningParenthesis(); break;
       
   149                 case ')' : HandleClosingParenthesis(); break;
       
   150 				default: HandleOtherCharacterL(chr); break;
       
   151 				}
       
   152 			iPreviousWasBackSlash = EFalse;
       
   153 			}
       
   154 		chr = iLexer.Get();
       
   155 		}
       
   156 
       
   157 	CheckMatchingQuotesBracketsL();
       
   158 	AddLastTokenL(); // Add the last token after all the other tokens.
       
   159 	}
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CSIPTokenizer::HandleQuote
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void CSIPTokenizer::HandleQuote()
       
   166 	{
       
   167 	if (iPreviousWasBackSlash) 
       
   168 		{
       
   169 		return;
       
   170 		}
       
   171 
       
   172 	iInQuotes = !iInQuotes;
       
   173 	iQuoteCount++;
       
   174 	}
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CSIPTokenizer::HandleOpeningAngleBracketL
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CSIPTokenizer::HandleOpeningAngleBracketL()
       
   181 	{
       
   182 	if (iInQuotes) 
       
   183 		{
       
   184 		return; // ignore
       
   185 		}
       
   186 
       
   187 	// Nested angle brackets are not allowed
       
   188 	__ASSERT_ALWAYS (!iInAngleBrackets,
       
   189 		             User::Leave (KErrSipCodecTokenizer));
       
   190 	
       
   191 	iInAngleBrackets = ETrue;
       
   192 	iAngleBracketCount++;
       
   193 	}
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CSIPTokenizer::HandleClosingAngleBracketL
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 void CSIPTokenizer::HandleClosingAngleBracketL()
       
   200 	{
       
   201 	if (iInQuotes) 
       
   202 		{
       
   203 		return; // ignore
       
   204 		}
       
   205 
       
   206 	// Matching opening angle bracket has to exist
       
   207 	__ASSERT_ALWAYS (iInAngleBrackets,
       
   208 		             User::Leave (KErrSipCodecTokenizer));
       
   209 	
       
   210 	iInAngleBrackets = EFalse;
       
   211 	iAngleBracketCount++;
       
   212 	}
       
   213 
       
   214 // -----------------------------------------------------------------------------
       
   215 // CSIPTokenizer::HandleOpeningParenthesis
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CSIPTokenizer::HandleOpeningParenthesis()
       
   219 	{
       
   220 	if (iInQuotes) 
       
   221 		{
       
   222 		return; // ignore
       
   223 		}
       
   224 	
       
   225 	iInParenthesis = ETrue;
       
   226 	iParenthesisCount++;
       
   227 	}
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CSIPTokenizer::HandleClosingParenthesis
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void CSIPTokenizer::HandleClosingParenthesis()
       
   234 	{
       
   235 	if (iInQuotes) 
       
   236 		{
       
   237 		return; // ignore
       
   238 		}
       
   239 
       
   240 	iInParenthesis = EFalse;
       
   241 	iParenthesisCount++;
       
   242 	}
       
   243 
       
   244 // -----------------------------------------------------------------------------
       
   245 // CSIPTokenizer::CheckMatchingQuotesBracketsL
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 void CSIPTokenizer::CheckMatchingQuotesBracketsL()
       
   249 	{
       
   250 	__ASSERT_ALWAYS (iQuoteCount%2 == 0,
       
   251 		             User::Leave (KErrSipCodecTokenizer));
       
   252 
       
   253 	__ASSERT_ALWAYS (iAngleBracketCount%2 == 0,
       
   254 		             User::Leave (KErrSipCodecTokenizer));
       
   255 
       
   256     if(!iIgnoreParenthesis)
       
   257         {
       
   258         __ASSERT_ALWAYS (iParenthesisCount%2 == 0,
       
   259                      User::Leave (KErrSipCodecTokenizer));
       
   260         }
       
   261 	}
       
   262 
       
   263 // -----------------------------------------------------------------------------
       
   264 // CSIPTokenizer::HandleOtherCharacterL
       
   265 // -----------------------------------------------------------------------------
       
   266 //
       
   267 void CSIPTokenizer::HandleOtherCharacterL(const TChar& aChr)
       
   268 	{
       
   269 	__ASSERT_ALWAYS (iTokenLength > 0,
       
   270 		             User::Leave (KErrSipCodecTokenizer));
       
   271 
       
   272 	if (iInQuotes || 
       
   273 	    iInAngleBrackets || 
       
   274 	    (iInParenthesis && !iIgnoreParenthesis))
       
   275 		{
       
   276 		return;
       
   277 		}	
       
   278 	
       
   279 	if (aChr != iSeparator) 
       
   280 		{
       
   281 		return;
       
   282 		}	
       
   283 	
       
   284 	TPtrC8 remainder(iLexer.Remainder());
       
   285 	if (aChr == iSeparator && 
       
   286 	    iSkipSeparatorsBeforeAt &&
       
   287 	    remainder.Locate('@') >= 0)
       
   288 	    {
       
   289 	    CSIPTokenizer* atChrTokenizer = 
       
   290 	        CSIPTokenizer::NewLC(
       
   291 	            remainder, '@', EFalse, ETrue, EFalse);
       
   292 	    
       
   293 	    TInt atCount = atChrTokenizer->Tokens().Count() - 1;	    
       
   294 	    
       
   295 	    CleanupStack::PopAndDestroy(atChrTokenizer);
       
   296 	    
       
   297 	    if (atCount > 0)
       
   298 	        {
       
   299 	        return; // skip the separators before '@'
       
   300 	        }
       
   301 	    }	
       
   302 
       
   303     TUint startPos = iParsedSoFar - iTokenLength;
       
   304 	TPtrC8 token (iOriginalValue.Mid(startPos, iTokenLength-1));
       
   305 	iTokens->AppendL(token);
       
   306 
       
   307 	// Start a new token
       
   308 	iTokenLength = 0;
       
   309 	}
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CSIPTokenizer::AddLastTokenL
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 void CSIPTokenizer::AddLastTokenL()
       
   316 	{
       
   317 	if ( iIgnoreLastEmptyValue && !(iTokenLength > 0))
       
   318 		{
       
   319 		return;
       
   320 		}
       
   321 		
       
   322 	__ASSERT_ALWAYS (iTokenLength > 0,
       
   323 		             User::Leave (KErrSipCodecTokenizer));
       
   324 
       
   325     TUint startPos = iParsedSoFar - iTokenLength;
       
   326 	TPtrC8 token (iOriginalValue.Mid(startPos, iTokenLength));
       
   327 
       
   328 	iTokens->AppendL(token);
       
   329 	}