1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericservices/httputils/EscapeUtils/EscapeUtils.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,662 @@
1.4 +// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +//
1.18 +
1.19 +#include <escapeutils.h>
1.20 +
1.21 +#include <utf.h>
1.22 +
1.23 +#include "EscapeUtilsInternal.h"
1.24 +
1.25 +// Constants
1.26 +//
1.27 +_LIT(KHexDigit, "0123456789ABCDEF");
1.28 +_LIT(KExcludedData, "{}|\\^`<>#%\"");
1.29 +_LIT8(KQueryData8, ";/?:@&=+$,[]");
1.30 +_LIT8(KPathData8, "/;=?[]");
1.31 +_LIT8(KAuthData8, ";:@?/[]");
1.32 +_LIT8(KUrlEncoded8, ";/?:@&=+$[]!\'()~*");
1.33 +_LIT16(KQueryData16, ";/?:@&=+$,[]");
1.34 +_LIT16(KPathData16, "/;=?[]");
1.35 +_LIT16(KAuthData16, ";:@?/[]");
1.36 +_LIT16(KUrlEncoded16, ";/?:@&=+$[]!\'()~");
1.37 +const TInt KEscapeUtilsConversionBufferSize = 50;
1.38 +const TInt KEscapeIndicator = '%';
1.39 +const TInt KEscapeTripleLength = 3;
1.40 +const TInt KEscDelimiterPos = 0;
1.41 +const TInt KMostSignificantNibblePos = 1;
1.42 +const TInt KLeastSignificantNibblePos = 2;
1.43 +
1.44 +// Panic category
1.45 +//
1.46 +#ifdef _DEBUG
1.47 + _LIT(KEscapeUtilsPanicCategory, "ESC-UTILS");
1.48 +#endif
1.49 +
1.50 +//
1.51 +//
1.52 +// Implementation of EscapeUtils
1.53 +//
1.54 +//
1.55 +
1.56 +/**
1.57 + Escape encodes excluded and reserved characters in the data as escape triples.
1.58 + The reserved characters are defined by the escape mode. These characters and the
1.59 + set of excluded characters specified by RFC2396 form the entire set of excluded data.
1.60 +
1.61 + @since 6.0
1.62 + @leave KUriUtilsErr16BitChar. A 16-Bit character was found in the data to be escape encoded.
1.63 + @param aData A descriptor with the data to encode.
1.64 + @param aEscapeMode An enum specifying the escape mode.
1.65 + @return A pointer to a descriptor buffer which contains the escape encoded data.
1.66 +*/
1.67 +EXPORT_C HBufC8* EscapeUtils::EscapeEncodeL(const TDesC8& aData, TEscapeMode aEscapeMode)
1.68 + {
1.69 + // Need descriptor pointer to reserved characters...
1.70 + TPtrC8 reserved;
1.71 +
1.72 + switch (aEscapeMode)
1.73 + {
1.74 + case EEscapeNormal:
1.75 + {
1.76 + // This is normal operation - no reserved chars
1.77 + reserved.Set(KNullDesC8);
1.78 + } break;
1.79 + case EEscapeQuery:
1.80 + {
1.81 + // Reserved data in a URI query - ; / ? : @ & = + $ ,
1.82 + reserved.Set(KQueryData8());
1.83 + } break;
1.84 + case EEscapePath:
1.85 + {
1.86 + // Reserved data in a URI path segment - / ; = ?
1.87 + reserved.Set(KPathData8());
1.88 + } break;
1.89 + case EEscapeAuth:
1.90 + {
1.91 + // Reserved data in a URI authority - ; : @ ? /
1.92 + reserved.Set(KAuthData8());
1.93 + } break;
1.94 + case EEscapeUrlEncoded:
1.95 + {
1.96 + // Reserved data in Url Encoded data - ; / ? : @ & = + $ [ ] ! ' ( ) ~ *
1.97 + reserved.Set(KUrlEncoded8());
1.98 + } break;
1.99 + default:
1.100 + // Not supported return NULL
1.101 + __ASSERT_DEBUG(EFalse, User::Panic(KEscapeUtilsPanicCategory, KUriUtilsErrBadEscapeMode));
1.102 + return NULL;
1.103 + }
1.104 + return EscapeEncodeL(aData, reserved);
1.105 + }
1.106 +
1.107 +/**
1.108 + Escape encodes excluded and reserved characters in the data as escape triples. The
1.109 + reserved characters are defined by the escape mode. These characters and the set of
1.110 + excluded characters specified by RFC2396 form the entire set of excluded data.
1.111 +
1.112 + @since 6.0
1.113 + @leave KUriUtilsErr16BitChar. A 16-Bit character was found in the data to be escape encoded.
1.114 + @param aData A descriptor with the data to encode.
1.115 + @param aEscapeMode An enum specifying the escape mode.
1.116 + @return A pointer to a descriptor buffer which contains the escape encoded data.
1.117 +*/
1.118 +EXPORT_C HBufC16* EscapeUtils::EscapeEncodeL(const TDesC16& aData, TEscapeMode aEscapeMode)
1.119 + {
1.120 + // Need to descriptor pointer to reserved chars
1.121 + TPtrC16 reserved;
1.122 +
1.123 + switch (aEscapeMode)
1.124 + {
1.125 + case EEscapeNormal:
1.126 + {
1.127 + // This is normal operation - no reserved chars
1.128 + reserved.Set(KNullDesC16);
1.129 + } break;
1.130 + case EEscapeQuery:
1.131 + {
1.132 + // Reserved data in a URI query - ; / ? : @ & = + $ [],
1.133 + reserved.Set(KQueryData16());
1.134 + } break;
1.135 + case EEscapePath:
1.136 + {
1.137 + // Reserved data in a URI path segment - / ; = ? []
1.138 + reserved.Set(KPathData16());
1.139 + } break;
1.140 + case EEscapeAuth:
1.141 + {
1.142 + // Reserved data in a URI authority - ; : @ ? / []
1.143 + reserved.Set(KAuthData16());
1.144 + } break;
1.145 + case EEscapeUrlEncoded:
1.146 + {
1.147 + // Reserved data in Url Encoded data - ; / ? : @ & = + $ [ ] ! ' ( ) ~
1.148 + reserved.Set(KUrlEncoded16());
1.149 + } break;
1.150 + default:
1.151 + // Not supported return NULL
1.152 + __ASSERT_DEBUG(EFalse, User::Panic(KEscapeUtilsPanicCategory, KUriUtilsErrBadEscapeMode));
1.153 + return NULL;
1.154 + }
1.155 + return EscapeEncodeL(aData, reserved);
1.156 + }
1.157 +
1.158 +/**
1.159 + Escape encodes excluded and reserved characters in the data as escape triples. These
1.160 + characters and the set of excluded characters specified by RFC2396 form the entire set
1.161 + of excluded data.
1.162 +
1.163 + @since 6.0
1.164 + @leave KUriUtilsErr16BitChar. A 16-Bit character was found in the data to be escape encoded.
1.165 + @param aData A descriptor with the data to encode.
1.166 + @param aReservedChars A descriptor with the reserved characters.
1.167 + @return A pointer to a descriptor buffer which contains the escape encoded data.
1.168 +*/
1.169 +EXPORT_C HBufC8* EscapeUtils::EscapeEncodeL(const TDesC8& aData, const TDesC8& aReservedChars)
1.170 + {
1.171 + // Allocate space to build escaped url - consider worse case; all characters are excluded => length x 3
1.172 + HBufC8* buf = HBufC8::NewLC(aData.Length()*3);
1.173 + TPtr8 escaped = buf->Des();
1.174 +
1.175 + User::LeaveIfError(EscapeEncodeData(aData, aReservedChars, escaped));
1.176 + HBufC8* encoded = escaped.AllocL();
1.177 +
1.178 + CleanupStack::PopAndDestroy(buf);
1.179 + return encoded;
1.180 + }
1.181 +
1.182 +/**
1.183 + Escape encodes excluded and reserved characters in the data as escape triples. These characters
1.184 + and the set of excluded characters specified by RFC2396 form the entire set of excluded data.
1.185 +
1.186 + @since 6.0
1.187 + @leave KUriUtilsErr16BitChar. A 16-Bit character was found in the data to be escape encoded.
1.188 + @param aData A descriptor with the data to encode.
1.189 + @param aReservedChars A descriptor with the reserved characters.
1.190 + @return A pointer to a descriptor buffer which contains the escape encoded data.
1.191 +*/
1.192 +EXPORT_C HBufC16* EscapeUtils::EscapeEncodeL(const TDesC16& aData, const TDesC16& aReservedChars)
1.193 + {
1.194 + // Allocate space to build escaped url - consider worse case; all characters are excluded => length x 3
1.195 + HBufC16* buf = HBufC16::NewLC(aData.Length()*3);
1.196 + TPtr16 escaped = buf->Des();
1.197 +
1.198 + User::LeaveIfError(EscapeEncodeData(aData, aReservedChars, escaped));
1.199 + HBufC16* encoded = escaped.AllocL();
1.200 +
1.201 + CleanupStack::PopAndDestroy(buf);
1.202 + return encoded;
1.203 + }
1.204 +
1.205 +/**
1.206 + Escape decodes the data.
1.207 +
1.208 + @since 6.0
1.209 + @param aData A descriptor with the data to decode.
1.210 + @return A pointer to a descriptor buffer which contains the escape decoded data.
1.211 +*/
1.212 +EXPORT_C HBufC8* EscapeUtils::EscapeDecodeL(const TDesC8& aData)
1.213 + {
1.214 + // Allocate space to build unescaped data
1.215 + HBufC8* buf = HBufC8::NewLC(aData.Length());
1.216 + TPtr8 unescaped = buf->Des();
1.217 +
1.218 + User::LeaveIfError(EscapeDecodeData(aData, unescaped));
1.219 + HBufC8* decoded = unescaped.AllocL();
1.220 +
1.221 + CleanupStack::PopAndDestroy(buf);
1.222 + return decoded;
1.223 + }
1.224 +
1.225 +/**
1.226 + Escape decodes the data.
1.227 +
1.228 + @since 6.0
1.229 + @param aData A descriptor with the data to decode.
1.230 + @return A pointer to a descriptor buffer which contains the escape decoded data.
1.231 +*/
1.232 +EXPORT_C HBufC16* EscapeUtils::EscapeDecodeL(const TDesC16& aData)
1.233 + {
1.234 + // Allocate space to build unescaped data
1.235 + HBufC16* buf = HBufC16::NewLC(aData.Length());
1.236 + TPtr16 unescaped = buf->Des();
1.237 +
1.238 + User::LeaveIfError(EscapeDecodeData(aData, unescaped));
1.239 + HBufC16* decoded = unescaped.AllocL();
1.240 +
1.241 + CleanupStack::PopAndDestroy(buf);
1.242 + return decoded;
1.243 + }
1.244 +
1.245 +/**
1.246 + @internalComponent
1.247 +
1.248 + escape encode only those characters that cannot be in a URI. assume all %hh are %encoded already.
1.249 +
1.250 + @param aData The descriptor buffer to be escape encoded.
1.251 + @return A pointer to a descriptor buffer which contains the escape encoded data.
1.252 +*/
1.253 +HBufC8* EscapeUtils::ReEscapeEncodeL(const TDesC8& aData)
1.254 + {
1.255 + // Descriptor to hex digits and excluded chars
1.256 + const TDesC& KHexChars = KHexDigit;
1.257 +
1.258 + const TInt length = aData.Length();
1.259 +
1.260 + // find out how many characters need escape encoding
1.261 + TInt count = 0;
1.262 + for( TInt i=0; i<length; ++i )
1.263 + {
1.264 + TChar current( aData[i] );
1.265 + if( EscapeUtils::IsExcludedChar(current) && current != KFragmentDelimiter &&
1.266 + !(current == KEscapeIndicator && i+2<length && TChar(aData[i+1]).IsHexDigit() && TChar(aData[i+2]).IsHexDigit() ) )
1.267 + {
1.268 + count++;
1.269 + }
1.270 + }
1.271 + if( count == 0) // no encoding needed, just allocate and return the whole string
1.272 + {
1.273 + return aData.AllocL();
1.274 + }
1.275 + // pre-allocate space for the descriptor
1.276 + HBufC8* buf = HBufC8::NewLC( aData.Length() + count*2 ); // two extra chars for each escaped
1.277 + TPtr8 escaped = buf->Des();
1.278 +
1.279 + for( TInt i=0; i<length; ++i )
1.280 + {
1.281 + // Check if current character must be escaped
1.282 + TChar current ( aData[i] );
1.283 + // Check if current character is excluded, but not if it appears to be escape encoded
1.284 + TBool excluded = EscapeUtils::IsExcludedChar(current) && current != KFragmentDelimiter &&
1.285 + !(current == KEscapeIndicator && i+2<length && TChar(aData[i+1]).IsHexDigit() && TChar(aData[i+2]).IsHexDigit() );
1.286 +
1.287 + if( excluded )
1.288 + {
1.289 + // Excluded char - escape encode
1.290 + escaped.Append(KEscapeIndicator);
1.291 + const TInt mostSignificantNibble = (current & 0xf0) >> 4; // Get msNibble by masking against 11110000 and dividing by 16 (>>4)
1.292 + escaped.Append(KHexChars[mostSignificantNibble]);
1.293 + const TInt leastSignificantNibble = (current & 0x0f); // Get lsNibble by masking against 00001111
1.294 + escaped.Append(KHexChars[leastSignificantNibble]);
1.295 + }
1.296 + else
1.297 + {
1.298 + // Not an excluded char - just append
1.299 + escaped.Append(current);
1.300 + }
1.301 + }
1.302 + CleanupStack::Pop(buf);
1.303 + return buf;
1.304 + }
1.305 +
1.306 +
1.307 +/**
1.308 +
1.309 + Converts UNICODE data into UTF8 format.
1.310 +
1.311 + @since 6.0
1.312 + @leave KUriUtilsCannotConvert. When the input data cannot be converted.
1.313 + @param aString A descriptor with the data to convert.
1.314 + @return A pointer to an 8-bit descriptor buffer which contains UTF8 data.
1.315 +*/
1.316 +EXPORT_C HBufC8* EscapeUtils::ConvertFromUnicodeToUtf8L(const TDesC& aString)
1.317 + {
1.318 + // Return an empty buffer straight-away
1.319 + if( aString.Compare(KNullDesC) == 0 )
1.320 + return KNullDesC8().AllocL();
1.321 +
1.322 + // Convert from Unicode to UTF8
1.323 + TPtrC unicode = aString;
1.324 + TBuf8<KEscapeUtilsConversionBufferSize> buf;
1.325 + HBufC8* utf8Buffer = HBufC8::NewLC(unicode.Length());
1.326 + TPtr8 utf8 = utf8Buffer->Des();
1.327 +
1.328 + // Loop until all of the filename is converted
1.329 + FOREVER
1.330 + {
1.331 + const TInt returnValue = CnvUtfConverter::ConvertFromUnicodeToUtf8(buf, unicode);
1.332 + if( returnValue == CnvUtfConverter::EErrorIllFormedInput || returnValue < 0)
1.333 + User::Leave(KUriUtilsCannotConvert);
1.334 +
1.335 + // Is escapedFullPath too small?
1.336 + if( utf8.Length() + buf.Length() > utf8.MaxLength() )
1.337 + {
1.338 + utf8Buffer = utf8Buffer->ReAllocL(utf8.Length() + buf.Length());
1.339 + CleanupStack::Pop(); // utf8Buffer (old version)
1.340 + CleanupStack::PushL(utf8Buffer); // new version
1.341 + utf8.Set(utf8Buffer->Des());
1.342 + }
1.343 + // Copy converted characters
1.344 + utf8.Append(buf);
1.345 +
1.346 + if( returnValue == KErrNone )
1.347 + break; // All of aUnicodeText has been converted and handled
1.348 +
1.349 + // Set input descriptor to remaining characters
1.350 + unicode.Set(unicode.Right(returnValue));
1.351 + }
1.352 + CleanupStack::Pop(utf8Buffer);
1.353 + return utf8Buffer; // Ownership transfered to caller
1.354 + }
1.355 +
1.356 +/**
1.357 + Converts UTF8 format into UNICODE data.
1.358 +
1.359 + @since 6.0
1.360 + @leave KUriUtilsCannotConvert. When the input data cannot be converted.
1.361 + @param aString A descriptor with the data to convert.
1.362 + @return A pointer to a 16-bit descriptor buffer which contains UNICODE data.
1.363 +*/
1.364 +EXPORT_C HBufC* EscapeUtils::ConvertToUnicodeFromUtf8L(const TDesC8& aString)
1.365 + {
1.366 + // Return an empty buffer straight-away
1.367 + if( aString.Compare(KNullDesC8) == 0 )
1.368 + return KNullDesC().AllocL();
1.369 +
1.370 + // Convert from Unicode to UTF8
1.371 + TPtrC8 utf8 = aString;
1.372 + TBuf<KEscapeUtilsConversionBufferSize> buf;
1.373 + HBufC* unicodeBuffer = HBufC::NewLC(utf8.Length());
1.374 + TPtr unicode = unicodeBuffer->Des();
1.375 +
1.376 + // Loop until all of the filename is converted
1.377 + FOREVER
1.378 + {
1.379 + const TInt returnValue = CnvUtfConverter::ConvertToUnicodeFromUtf8(buf, utf8);
1.380 + if( returnValue == CnvUtfConverter::EErrorIllFormedInput || returnValue < 0)
1.381 + User::Leave(KUriUtilsCannotConvert);
1.382 +
1.383 + // Is escapedFullPath too small?
1.384 + if( unicode.Length() + buf.Length() > unicode.MaxLength() )
1.385 + {
1.386 + unicodeBuffer = unicodeBuffer->ReAllocL(unicode.Length() + buf.Length());
1.387 + CleanupStack::Pop(); // unicodeBuffer (old version)
1.388 + CleanupStack::PushL(unicodeBuffer); // new version
1.389 + unicode.Set(unicodeBuffer->Des());
1.390 + }
1.391 + // Copy converted characters
1.392 + unicode.Append(buf);
1.393 +
1.394 + if( returnValue==0 )
1.395 + break; // All of utf8 has been converted and handled
1.396 +
1.397 + // Set input descriptor to remaining characters
1.398 + utf8.Set(utf8.Right(returnValue));
1.399 + }
1.400 + CleanupStack::Pop(unicodeBuffer);
1.401 + return unicodeBuffer; // Ownership transfered to caller
1.402 + }
1.403 +
1.404 +/**
1.405 + Checks to see if the input argument is excluded.
1.406 +
1.407 + @since 6.0
1.408 + @param aChar The character to be checked.
1.409 + @return A boolean value of ETrue if the character is an excluded one, or
1.410 + EFalse if it is not.
1.411 + */
1.412 +EXPORT_C TBool EscapeUtils::IsExcludedChar(TChar aChar)
1.413 + {
1.414 + const TDesC& KExcludedChars = KExcludedData;
1.415 + TBool excluded = KExcludedChars.Locate(aChar) != KErrNotFound || aChar <= 0x1F || aChar == ' ' || aChar > 0x7E;
1.416 + return excluded;
1.417 + }
1.418 +
1.419 +/**
1.420 + Checks for an escape triple at the start of the input descriptor. If there is a triple
1.421 + its value is calculated and returned through the output argument aHexValue. If there is
1.422 + no escape triple then this argument is left unchanged.
1.423 +
1.424 + @since 6.0
1.425 + @param aData The descriptor to be checked for an escape triple.
1.426 + @param aHexValue The output argument with the value of the escape triple
1.427 + if it exists.
1.428 + @return A boolean value of ETrue if there is an escape triple at the start of
1.429 + the input descriptor, EFalse otherwise.
1.430 +*/
1.431 +EXPORT_C TBool EscapeUtils::IsEscapeTriple(const TDesC8& aData, TInt& aHexValue)
1.432 + {
1.433 + return CheckAndConvertEscapeTriple(aData, aHexValue);
1.434 + }
1.435 +
1.436 +/**
1.437 + Checks for an escape triple at the start of the input descriptor. If there is a triple
1.438 + its value is calculated and returned through the output argument aHexValue. If there is
1.439 + no escape triple then this argument is left unchanged.
1.440 +
1.441 + @since 6.0
1.442 + @param aData The descriptor to be checked for an escape triple.
1.443 + @param aHexValue The output argument with the value of the escape triple
1.444 + if it exists.
1.445 + @return A boolean value of ETrue if there is an escape triple at the start of
1.446 + the input descriptor, EFalse otherwise.
1.447 +*/
1.448 +EXPORT_C TBool EscapeUtils::IsEscapeTriple(const TDesC16& aData, TInt& aHexValue)
1.449 + {
1.450 + return CheckAndConvertEscapeTriple(aData, aHexValue);
1.451 + }
1.452 +
1.453 +/**
1.454 + returns the escape encoded descriptor output. This checks the every character of aData
1.455 + against aCharsToEscape and if it exist then it escape encodes that character.
1.456 +
1.457 + @param aData The descriptor to be checked against escaping set of characters.
1.458 + @param aCharsToEscape The set of escape characters.
1.459 + @return A pointer to the escape encoded descriptor.
1.460 +*/
1.461 +EXPORT_C HBufC8* EscapeUtils::SpecificEscapeEncodeL ( const TDesC8& aData, const TDesC8& aCharsToEscape )
1.462 + {
1.463 + // Descriptor to hex digits and excluded chars
1.464 + const TDesC& KHexChars = KHexDigit;
1.465 +
1.466 + const TInt length = aData.Length();
1.467 +
1.468 + // find out how many characters need escape encoding
1.469 + TInt count = 0;
1.470 + for( TInt i=0; i<length; ++i )
1.471 + {
1.472 + TChar current( aData[i] );
1.473 + if ( current <= 0x1F || aCharsToEscape.Locate ( current ) != KErrNotFound || current > 0x7E )
1.474 + {
1.475 + count++;
1.476 + }
1.477 + }
1.478 + if( count == 0) // no encoding needed, just allocate and return the whole string
1.479 + {
1.480 + return aData.AllocL();
1.481 + }
1.482 +
1.483 + // pre-allocate space for the descriptor
1.484 + HBufC8* buf = HBufC8::NewLC( length + count*2 ); // two extra chars for each escaped
1.485 + TPtr8 escaped = buf->Des();
1.486 +
1.487 + for( TInt i=0; i<length; ++i )
1.488 + {
1.489 + // Check if current character must be escaped
1.490 + TChar current ( aData[i] );
1.491 + // Check if current character is excluded ( control characters and the character specified for escaping )
1.492 + TBool excluded = current <= 0x1F || ( aCharsToEscape.Locate ( current ) != KErrNotFound ) || current > 0x7E;
1.493 +
1.494 + if( excluded )
1.495 + {
1.496 + // Excluded char - escape encode
1.497 + escaped.Append(KEscapeIndicator);
1.498 + const TInt mostSignificantNibble = (current & 0xf0) >> 4; // Get msNibble by masking against 11110000 and dividing by 16 (>>4)
1.499 + escaped.Append(KHexChars[mostSignificantNibble]);
1.500 + const TInt leastSignificantNibble = (current & 0x0f); // Get lsNibble by masking against 00001111
1.501 + escaped.Append(KHexChars[leastSignificantNibble]);
1.502 + }
1.503 + else
1.504 + {
1.505 + // Not an excluded char - just append
1.506 + escaped.Append(current);
1.507 + }
1.508 + }
1.509 + CleanupStack::Pop(buf);
1.510 + return buf;
1.511 + }
1.512 +
1.513 +/**
1.514 + The Dummy API is used to redirect to SpecificEscapeEncodeL() API in order to preserve BC and is made private
1.515 + to ensure no-one else starts using it.
1.516 +*/
1.517 +EXPORT_C HBufC8* EscapeUtils::DummyForwardingFunctionForCompatibility( const TDesC8& aData, const TDesC8& aCharsToEscape )
1.518 + {
1.519 + return EscapeUtils::SpecificEscapeEncodeL ( aData, aCharsToEscape );
1.520 + }
1.521 +
1.522 +
1.523 +//
1.524 +//
1.525 +// Implementation of LOCAL functions
1.526 +//
1.527 +//
1.528 +/**
1.529 + Escape encodes the data, converting the reserved characters and excluded characters defined by
1.530 + RFC2396 as escape triples.
1.531 +
1.532 + @since 6.0
1.533 + @warning This function will panic if the output descriptor aEncodedData is
1.534 + not big enough to append all the data.
1.535 + @param aData A descriptor with the data to encode.
1.536 + @param aReservedChars Reserved characters set.
1.537 + @param aEncodedData The output descriptor pointer where the escaped encoded
1.538 + data is placed.
1.539 + @return An error code of KUriUtilsErr16BitChar if the data contains a 16-bit
1.540 + character. KErrNone if the data was successfully encoded.
1.541 + */
1.542 +template<class TDesCType, class TPtrType>
1.543 +TInt EscapeEncodeData(const TDesCType& aData, const TDesCType& aReservedChars, TPtrType& aEncodedData)
1.544 + {
1.545 + // Descriptor to hex digits and excluded chars
1.546 + const TDesC& KHexChars = KHexDigit;
1.547 +
1.548 + const TInt length = aData.Length();
1.549 + for( TInt i=0; i<length; ++i )
1.550 + {
1.551 + // Check if current character must be escaped, will return error if not 8-bit character
1.552 + TChar current = aData[i];
1.553 + if( current > 0xff )
1.554 + {
1.555 + __ASSERT_DEBUG(EFalse, User::Panic(KEscapeUtilsPanicCategory, KUriUtilsErr16BitChar));
1.556 + return (KUriUtilsErr16BitChar);
1.557 + }
1.558 + // Check if current character is excluded, a control character or a space
1.559 + TBool excluded = EscapeUtils::IsExcludedChar(current) || aReservedChars.Locate(current) != KErrNotFound;
1.560 + if ( excluded )
1.561 + {
1.562 + // Excluded char - escape encode
1.563 + aEncodedData.Append(KEscapeIndicator);
1.564 + const TInt mostSignificantNibble = (current & 0xf0) >> 4; // Get msNibble by masking against 11110000 and dividing by 16 (>>4)
1.565 + aEncodedData.Append(KHexChars[mostSignificantNibble]);
1.566 + const TInt leastSignificantNibble = (current & 0x0f); // Get lsNibble by masking against 00001111
1.567 + aEncodedData.Append(KHexChars[leastSignificantNibble]);
1.568 + }
1.569 + else
1.570 + {
1.571 + // Not an excluded char or It's already Escape encode - just append
1.572 + aEncodedData.Append(current);
1.573 + }
1.574 + }
1.575 + return KErrNone;
1.576 + }
1.577 +
1.578 +/**
1.579 + Escape decodes the data, converting escape triples back to their single character value.
1.580 +
1.581 + @since 6.0
1.582 + @warning This function will panic if the output descriptor aDecodedData is not big
1.583 + enough to append all the data.
1.584 + @param aData A descriptor with the data to decode.
1.585 + @param aDecodedData The output descriptor pointer where the escaped decoded data
1.586 + is placed.
1.587 + @return An error code of KUriUtilsErr16BitChar if the data contains a 16-bit character.
1.588 + KErrNone if the data was successfully encoded.
1.589 + */
1.590 +template<class TDesCType, class TPtrType>
1.591 +TInt EscapeDecodeData(const TDesCType& aData, TPtrType& aDecodedData)
1.592 + {
1.593 + // Go through the descriptor
1.594 + const TInt length = aData.Length();
1.595 + for( TInt i=0; i<length; ++i )
1.596 + {
1.597 + // See if at start of an escape triple
1.598 + TChar current = aData[i];
1.599 + if( current == KEscapeIndicator )
1.600 + {
1.601 + TInt hex;
1.602 + if( !CheckAndConvertEscapeTriple(aData.Mid(i), hex) )
1.603 + {
1.604 + // Either of the nibbles were not a valid hex character
1.605 + return KUriUtilsErrBadEscapeTriple;
1.606 + }
1.607 + // Append hex value
1.608 + aDecodedData.Append(hex);
1.609 +
1.610 + // Move index to get next character - add 2 to index
1.611 + i += 2;
1.612 + }
1.613 + else
1.614 + {
1.615 + // Not an escaped triple - just append
1.616 + aDecodedData.Append(current);
1.617 + }
1.618 + }
1.619 + return KErrNone;
1.620 + }
1.621 +
1.622 +/**
1.623 + Checks for an escape triple at the start of the input descriptor. If there is a triple its
1.624 + value is calculated and returned through the output argument aHexValue. If there is no escape
1.625 + then triple this argument is left unchanged.
1.626 +
1.627 + @since 6.0
1.628 + @param aData The descriptor to be checked for an escape triple.
1.629 + @param aHexValue The output argument with the value of the escape triple
1.630 + if it exists.
1.631 + @return A boolean value of ETrue if there is an escape triple at the start
1.632 + of the input descriptor, EFalse otherwise.
1.633 + */
1.634 +template<class TDesCType>
1.635 +TBool CheckAndConvertEscapeTriple(const TDesCType& aData, TInt& aHexValue)
1.636 + {
1.637 + // See if the descriptor is actually long enough
1.638 + if( aData.Length() < KEscapeTripleLength )
1.639 + {
1.640 + return EFalse;
1.641 + }
1.642 + // Check that the three characters form an escape triple - first char is '%'
1.643 + if( aData[KEscDelimiterPos] != KEscapeIndicator )
1.644 + {
1.645 + return EFalse;
1.646 + }
1.647 + // Descriptor to hex digits and excluded chars
1.648 + const TDesC& KHexChars = KHexDigit;
1.649 +
1.650 + // Check that next two characters are valid
1.651 + TChar mostSignificantNibble = aData[KMostSignificantNibblePos];
1.652 + TChar leastSignificantNibble = aData[KLeastSignificantNibblePos];
1.653 +
1.654 + TInt mostSignificantNibbleValue = KHexChars.LocateF(mostSignificantNibble);
1.655 + TInt leastSignificantNibbleValue = KHexChars.LocateF(leastSignificantNibble);
1.656 +
1.657 + if( mostSignificantNibbleValue == KErrNotFound || leastSignificantNibbleValue == KErrNotFound )
1.658 + {
1.659 + // Either of the nibbles were not a valid hex character
1.660 + return EFalse;
1.661 + }
1.662 + // Convert characters into hex value and return
1.663 + aHexValue = 0x10*mostSignificantNibbleValue + 0x01*leastSignificantNibbleValue;
1.664 + return ETrue;
1.665 + }