os/ossrv/genericservices/httputils/wspcodec/WSPDecoder.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/httputils/wspcodec/WSPDecoder.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,466 @@
     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 <e32base.h>
    1.20 +#include <stringpool.h>
    1.21 +#include <wspdecoder.h>
    1.22 +
    1.23 +// Constants
    1.24 +const TUint8 KWapQuote = 0x7F;
    1.25 +const TUint8 KCarryBitMask = 0x80;
    1.26 +#define KTopBitMask KCarryBitMask
    1.27 +const TUint8 KTop3BitSet = 0x70;
    1.28 +const TUint8 KQuoteChar = '\"';
    1.29 +
    1.30 +// Panic category
    1.31 +_LIT(KUriPanicCategory,"WSPDECODER");
    1.32 +
    1.33 +
    1.34 +//***********************************************************************
    1.35 +//	TWspHeaderSegmenter
    1.36 +//**********************************************************************/
    1.37 +
    1.38 +
    1.39 +/**
    1.40 +  NextL iterates through the buffer.  Each call returns a TWspField in the paramater.
    1.41 +
    1.42 +  @param aHeader Out - a TWspField containing the header <name,value> pair.  
    1.43 +  @warning The TWspField::iHdrName will be opened internally.
    1.44 +			It must be closed by the caller before this class is destroyed.
    1.45 +  @return	KErrNone if next field is returned
    1.46 +			KErrNotFound at the end of the buffer - no TWspField param returned
    1.47 +			KErrCorrupt if segmenting does not parse correctly
    1.48 +  @leave	RStringPool::OpenFStringL StringPool leave code if opening string fails.
    1.49 +*/
    1.50 +EXPORT_C TInt TWspHeaderSegmenter::NextL(TWspField& aHeader)
    1.51 +	{
    1.52 +	// have we run out of buffer?
    1.53 +	if (iOffset >= iBuffer.Length())
    1.54 +		return KErrNotFound;
    1.55 +
    1.56 +	// Set decoder to current buffer
    1.57 +	TWspPrimitiveDecoder decoder(iBuffer.Mid(iOffset));
    1.58 +	TInt bufLen = 0;
    1.59 +	
    1.60 +
    1.61 +	// Get the Field Name 
    1.62 +	switch(decoder.VarType())
    1.63 +		{
    1.64 +		case TWspPrimitiveDecoder::EString:
    1.65 +			{
    1.66 +			TPtrC8 name;
    1.67 +			bufLen = decoder.String(name);
    1.68 +			if (bufLen < KErrNone) return bufLen;
    1.69 +			aHeader.iHdrName = iPool.OpenFStringL(name);
    1.70 +			}
    1.71 +			break;
    1.72 +
    1.73 +		case TWspPrimitiveDecoder::E7BitVal:
    1.74 +			{
    1.75 +			TUint8 name;
    1.76 +			bufLen = decoder.Val7Bit(name);
    1.77 +			if (bufLen < KErrNone) return bufLen;
    1.78 +			
    1.79 +			aHeader.iHdrName = iPool.StringF(name, iStringTable);
    1.80 +			}
    1.81 +			break;
    1.82 +
    1.83 +		default:	// header name can't be anything else
    1.84 +			return KErrCorrupt;
    1.85 +		}
    1.86 +	
    1.87 +	// move our pointer past header name
    1.88 +	iOffset += bufLen;
    1.89 +
    1.90 +	
    1.91 +	// Get the value buffer by figuring out the type, then set the pointer to span the entire
    1.92 +	// value.  Note - further parsing will happen later to pull out specific value data.
    1.93 +	switch(decoder.VarType())
    1.94 +		{
    1.95 +		case TWspPrimitiveDecoder::ELengthVal:
    1.96 +			{
    1.97 +			TInt len;
    1.98 +			bufLen = decoder.LengthVal(len);
    1.99 +			bufLen += len;		
   1.100 +			}
   1.101 +			break;
   1.102 +		case TWspPrimitiveDecoder::EQuotedString:
   1.103 +		case TWspPrimitiveDecoder::EString:
   1.104 +			{
   1.105 +			TPtrC8 strBuf;
   1.106 +			bufLen = decoder.String(strBuf);
   1.107 +			}
   1.108 +			break;
   1.109 +		case TWspPrimitiveDecoder::E7BitVal:
   1.110 +			bufLen = 1;
   1.111 +			break;
   1.112 +		default:
   1.113 +			return KErrCorrupt;
   1.114 +		}
   1.115 +	
   1.116 +	if (bufLen < 0) 
   1.117 +		return bufLen;
   1.118 +
   1.119 +	if (iOffset + bufLen > iBuffer.Length())
   1.120 +          return KErrCorrupt;
   1.121 +
   1.122 +	aHeader.iValBuffer.Set(iBuffer.Mid(iOffset, bufLen));
   1.123 +	iOffset += bufLen;
   1.124 +	return KErrNone;
   1.125 +	}
   1.126 +
   1.127 +
   1.128 + //
   1.129 + // WAP-WSP 8.4.1.2
   1.130 + //
   1.131 +
   1.132 +/**
   1.133 +	Looks at the byte currently pointed at in this buffer and returns the type.
   1.134 +	
   1.135 +	@return TWspHeaderType - the type of this data octet
   1.136 +*/
   1.137 +EXPORT_C TWspPrimitiveDecoder::TWspHeaderType TWspPrimitiveDecoder::VarType() const
   1.138 +	{
   1.139 +	TWspHeaderType type = ENotSet;
   1.140 +	
   1.141 +	// Check that the offset has not overflowed the buffer
   1.142 +	if( iOffset >= iBuffer.Length() )
   1.143 +		return type;
   1.144 +
   1.145 +	TInt octet = iBuffer[iOffset];
   1.146 +
   1.147 +	if (octet >= 0 && octet <= 31)
   1.148 +		type = ELengthVal;
   1.149 +	else if (octet == 34)
   1.150 +		type = EQuotedString;
   1.151 +	else if (octet >= 32 && octet <= 127)
   1.152 +		type = EString;
   1.153 +	else if (octet >= 128 && octet <= 255)
   1.154 +		type = E7BitVal;
   1.155 +
   1.156 +	return type;
   1.157 +	}
   1.158 +
   1.159 +
   1.160 +/**
   1.161 +	Returns length of the data following this byte.  
   1.162 +	
   1.163 +	@pre iBuffer[iOffset] must be valid, VarType() == TWspHeaderType::ELengthVal
   1.164 +	@post internal offset gets updated to move past this primitive 
   1.165 +	@param aVal Out - the length encoded in this byte that indicates the size of the
   1.166 +		  	     data that follows.
   1.167 +	@return postive number indicating the number of bytes read from the buffer
   1.168 +			KErrCorrupt if data is not formatted correctly.
   1.169 +*/
   1.170 +EXPORT_C TInt TWspPrimitiveDecoder::LengthVal(TInt& aVal)
   1.171 +	{
   1.172 +	// have we run out of buffer?
   1.173 +	if (iOffset >= iBuffer.Length())
   1.174 +		return KErrCorrupt;
   1.175 +
   1.176 +	TInt bufLen = 0;
   1.177 +	aVal = iBuffer[iOffset++];
   1.178 +
   1.179 +	if (aVal == 31)
   1.180 +		{
   1.181 +		TUint32 uintVarLen = 0;
   1.182 +		bufLen = UintVar(uintVarLen);
   1.183 +		if (bufLen < KErrNone) return bufLen;
   1.184 +		aVal = (TInt)uintVarLen;
   1.185 +		}
   1.186 +
   1.187 +	// add the 1 byte read at to get first aVal
   1.188 +	++bufLen;
   1.189 +	return bufLen;
   1.190 +	}
   1.191 +
   1.192 +
   1.193 +/**
   1.194 +   Returns a TPtrC holding the string the buffer currently points at without the NULL 
   1.195 +   termination. If the String type is a quoted string then the quotes are not included 
   1.196 +   in the returned buffer.
   1.197 +   
   1.198 +   @pre iBuffer[iOffset] must be valid, VarType() == TWspType::EString
   1.199 +   @post internal offset gets updated to move past this primitive 
   1.200 +   @param aString Out - the string
   1.201 +   @return postive number indicating the number of bytes read from the buffer
   1.202 +   		  KErrCorrupt if data is not formatted correctly.
   1.203 +*/
   1.204 +EXPORT_C TInt TWspPrimitiveDecoder::String(TPtrC8& aString)
   1.205 +	{
   1.206 +	TWspHeaderType type = VarType();
   1.207 +	if( type != EString && type != EQuotedString)
   1.208 +		return KErrCorrupt;
   1.209 +
   1.210 +	TInt nullIndex = iBuffer.Mid(iOffset).Locate('\0');
   1.211 +	if( nullIndex == KErrNotFound )
   1.212 +		return KErrCorrupt;
   1.213 +
   1.214 +	// Set buffer to data not including the NULL terminator
   1.215 +	TPtrC8 buf = iBuffer.Mid(iOffset, nullIndex);
   1.216 +
   1.217 +	// is there a WAP Quote (0x7F) or a " at the start - step over it
   1.218 +	TInt bufferOffset = 0;
   1.219 +	const TUint8 firstByte = iBuffer[iOffset];
   1.220 +	if( firstByte == KQuoteChar || firstByte == KWapQuote )
   1.221 +		++bufferOffset;
   1.222 +
   1.223 +	// Set the descriptor with the correct buffer segment
   1.224 +	aString.Set(buf.Mid(bufferOffset));
   1.225 +
   1.226 +	// Step over the NULL
   1.227 +	++nullIndex;
   1.228 +
   1.229 +	// update the offset
   1.230 +	iOffset += nullIndex;
   1.231 +
   1.232 +	return nullIndex;
   1.233 +	} 
   1.234 +
   1.235 +/**
   1.236 +	Returns a token, a short int or an octet value with the top bit cleared
   1.237 +	
   1.238 +	@pre iBuffer[iOffset] must be valid, VarType() == TWspHeaderType::E7BitVal
   1.239 +	@post internal offset gets updated to move past this primitive 
   1.240 +	@param aVal Out - the 7 bit value with top bit cleared
   1.241 +	@return postive number indicating the number of bytes read from the buffer
   1.242 +		    KErrCorrupt if data is not formatted correctly.
   1.243 +*/
   1.244 +EXPORT_C TInt TWspPrimitiveDecoder::Val7Bit(TUint8& aVal)
   1.245 +	{
   1.246 +	// have we run out of buffer?
   1.247 +	if (iOffset >= iBuffer.Length())
   1.248 +		return KErrCorrupt;
   1.249 +
   1.250 +	aVal = (TUint8)(iBuffer[iOffset] & KWapQuote);
   1.251 +	++iOffset;
   1.252 +
   1.253 +	// 1 byte read
   1.254 +	return 1;
   1.255 +	}
   1.256 +
   1.257 +
   1.258 +/**
   1.259 +	Returns an Integer - could be short or long.
   1.260 +	
   1.261 +	@pre iBuffer[iOffset] must be valid, VarType() == TWspHeaderType::ELengthVal or
   1.262 +		   VarType() == TWspHeaderType::E7BitVal
   1.263 +	@post internal offset gets updated to move past this primitive 
   1.264 +	@param aVal Out - the long int
   1.265 +	@return postive number indicating the number of bytes read from the buffer
   1.266 +			  KErrCorrupt if data is not formatted correctly.
   1.267 +*/
   1.268 +EXPORT_C TInt TWspPrimitiveDecoder::Integer(TUint32& aVal)
   1.269 +	{
   1.270 +	// have we run out of buffer?
   1.271 +	if (iOffset >= iBuffer.Length())
   1.272 +		return KErrCorrupt;
   1.273 +
   1.274 +	TInt bufLen = 0;
   1.275 +
   1.276 +	// read the first byte
   1.277 +	aVal = iBuffer[iOffset];
   1.278 +	
   1.279 +	// short integers have the top bit set
   1.280 +	if (aVal & KTopBitMask)
   1.281 +		{
   1.282 +		aVal &= KWapQuote;
   1.283 +		++iOffset;
   1.284 +		++bufLen;
   1.285 +		}
   1.286 +	else
   1.287 +		{
   1.288 +		bufLen = LongInt(aVal);
   1.289 +		}
   1.290 +
   1.291 +	return bufLen;
   1.292 +	}
   1.293 +
   1.294 +/**
   1.295 +	Returns a long int the buffer is currently pointing at. 
   1.296 +	
   1.297 +	@pre iBuffer[iOffset] must be valid, VarType() == TWspHeaderType::ELengthVal  
   1.298 +	@post internal offset gets updated to move past this primitive 
   1.299 +	@param aVal Out - the long int
   1.300 +	@return postive number indicating the number of bytes read from the buffer
   1.301 +		  KErrCorrupt if data is not formatted correctly.
   1.302 +*/
   1.303 +EXPORT_C TInt TWspPrimitiveDecoder::LongInt(TUint32& aVal)
   1.304 +	{
   1.305 +	// have we run out of buffer?
   1.306 +	if (iOffset >= iBuffer.Length())
   1.307 +		return KErrCorrupt;
   1.308 +
   1.309 +	__ASSERT_DEBUG(aVal <= KMaxTUint, User::Panic(KUriPanicCategory, EWspDecoderLongIntOverflow));
   1.310 +	// initialize
   1.311 +	aVal = 0;
   1.312 +
   1.313 +	// Get num bytes encoding [len] [byte1] ... [byten]
   1.314 +	// we are positioned at that location in the source descriptor
   1.315 +	TUint8 numBytes = 0;
   1.316 +	TInt bufLen = Val7Bit(numBytes);
   1.317 +	if (bufLen < KErrNone) return bufLen;
   1.318 +
   1.319 +	// len can be up to 30 and verify we have enough buffer
   1.320 +	if (numBytes > 30 || numBytes > iBuffer.Mid(iOffset).Length())
   1.321 +		return KErrCorrupt;
   1.322 +	
   1.323 +	// Loop over the buffer, taking each byte and shifting it in count times.  
   1.324 +	for (TInt count = 0; count < numBytes ; ++count)
   1.325 +		aVal = (aVal << 8) + iBuffer[iOffset++];
   1.326 +	
   1.327 +	return (bufLen + numBytes);
   1.328 +	}
   1.329 +
   1.330 +/**
   1.331 +	Returns a TUint32 
   1.332 +	
   1.333 +	@pre iBuffer[iOffset] must be valid, VarType() == TWspHeaderType::ELengthVal or
   1.334 +		   VarType() == TWspHeaderType::E7BitVal 
   1.335 +	@post internal offset gets updated to move past this primitive 
   1.336 +	@param aVal Out - the TUint32 decoded 
   1.337 +	@return postive number indicating the number of bytes read from the buffer
   1.338 +		    KErrCorrupt if data is not formatted correctly.
   1.339 +*/
   1.340 +EXPORT_C TInt TWspPrimitiveDecoder::UintVar(TUint32& aVal)
   1.341 +	{
   1.342 +	// have we run out of buffer?
   1.343 +	if (iOffset >= iBuffer.Length())
   1.344 +		return KErrCorrupt;
   1.345 +
   1.346 +	// initialize return val
   1.347 +	aVal = 0;
   1.348 +
   1.349 +	// maximum length for a uintvar is 5
   1.350 +	TInt lenLeft = Min(iBuffer.Mid(iOffset).Length(), 5);
   1.351 +
   1.352 +	// get the first octet
   1.353 +	TUint8 byte = iBuffer[iOffset++];
   1.354 +	TInt numBytes = 1;
   1.355 +
   1.356 +	--lenLeft;	
   1.357 +
   1.358 +	// Check if any of the top 3 bits, ignoring the very top 'continue' bit, are set.  
   1.359 +	// Later if we see that this is a 5 byte number - we'll know it is corrupt.  
   1.360 +	// Encoding uses 7 bits/number 7x5=35 and we only support a maxiumum number 
   1.361 +	// of 32 bits.
   1.362 +	TBool topThreeBitsSet = byte & KTop3BitSet; 
   1.363 +	
   1.364 +	// copy over data from the byte into our return value (the top bit is a carry bit)
   1.365 +	aVal = byte & KWapQuote;
   1.366 +
   1.367 +	// while the 'continue' bit is set and we have more data
   1.368 +	while ((byte & KCarryBitMask) && (lenLeft > 0))
   1.369 +		{
   1.370 +		// shift our last value up
   1.371 +		aVal <<= 7;
   1.372 +		// get the next byte
   1.373 +		byte = iBuffer[iOffset++];
   1.374 +		// copy it over to the lowest byte
   1.375 +		aVal |= byte & KWapQuote;
   1.376 +		--lenLeft;
   1.377 +		++numBytes;
   1.378 +		} 
   1.379 +
   1.380 +	// last octet has continue bit set ... NOT allowed Or
   1.381 +	// this was encoded wrong - can't have a number bigger than 32 bits
   1.382 +	if ((byte & KCarryBitMask) || (numBytes == 5 && topThreeBitsSet))
   1.383 +		return KErrCorrupt;
   1.384 +
   1.385 +	// number of bytes read
   1.386 +	return numBytes;
   1.387 +	}
   1.388 +
   1.389 +
   1.390 +/**
   1.391 +	Returns a formatted version string 
   1.392 +	
   1.393 +	@pre iBuffer[iOffset] must be valid, VarType() == TWspHeaderType::ELengthVal  
   1.394 +	@post internal offset gets updated to move past this primitive 
   1.395 +	@param aPool In - an opened string pool
   1.396 +	@param aVer Out - a formatted version string.  Caller must close this string.
   1.397 +	@return postive number indicating the number of bytes read from the buffer
   1.398 +		    KErrCorrupt if data is not formatted correctly.
   1.399 +*/
   1.400 +EXPORT_C TInt TWspPrimitiveDecoder::VersionL(RStringPool aPool, RStringF& aVer)
   1.401 +	{
   1.402 +	const TInt KMaxBufLength=5;
   1.403 +	TInt bufLen = 0;
   1.404 +	TInt byte = iBuffer[iOffset];
   1.405 +	if (!(byte & KTopBitMask))
   1.406 +		{
   1.407 +		TPtrC8 str;
   1.408 +		bufLen = String(str);
   1.409 +		if (bufLen < KErrNone) return KErrCorrupt;
   1.410 +		aVer = aPool.OpenFStringL(str);
   1.411 +		}
   1.412 +	else
   1.413 +		{
   1.414 +		// Major 0-7 , Minor 0-15 [xxx][yyyy]
   1.415 +		TUint8 val;
   1.416 +		bufLen = Val7Bit(val);
   1.417 +		if (bufLen < KErrNone) return KErrCorrupt;
   1.418 +
   1.419 +
   1.420 +		TInt minVer =  val & 0x0F;
   1.421 +		TInt majVer =  val & 0xF0;
   1.422 +		majVer >>= 4;
   1.423 +
   1.424 +		if (majVer < 0 || majVer > 7)
   1.425 +			return KErrCorrupt;
   1.426 +
   1.427 +		TBuf8<KMaxBufLength> buf;
   1.428 +		if (minVer == 0x0F)
   1.429 +			{
   1.430 +			_LIT8(KVersionFormat, "%D");
   1.431 +			buf.Format(KVersionFormat, majVer);
   1.432 +			}
   1.433 +		else
   1.434 +			{
   1.435 +			_LIT8(KVersionFormat, "%D.%D");
   1.436 +			buf.Format(KVersionFormat, majVer, minVer);
   1.437 +			}
   1.438 +			aVer = aPool.OpenFStringL(buf);
   1.439 +		}
   1.440 +
   1.441 +	return bufLen;
   1.442 +	}
   1.443 +
   1.444 +/**
   1.445 +	Returns a TDateTime offset from January 1, 1970 - WAP WSP Section 8.4.2.3 Panics if 
   1.446 +	the time val is greater then the maximum allowable integer size (32 bits).
   1.447 +	
   1.448 +	@pre iBuffer[iOffset] must be valid, VarType() == TWspHeaderType::ELengthVal  
   1.449 +	@post internal offset gets updated to move past this primitive 
   1.450 +	@param aDateTime Out - a WAP Date
   1.451 +	@return postive number indicating the number of bytes read from the buffer
   1.452 +			  KErrCorrupt if data is not formatted correctly.
   1.453 +*/
   1.454 +EXPORT_C TInt TWspPrimitiveDecoder::Date(TDateTime& aDateTime)
   1.455 +	{
   1.456 +	TUint32 secVal;
   1.457 +	TInt bufLen = LongInt(secVal);
   1.458 +	__ASSERT_ALWAYS(bufLen <= KMaxTInt, User::Panic(KUriPanicCategory, EWspDecoderDateOverflow));
   1.459 +	if (bufLen < KErrNone) return bufLen;
   1.460 +
   1.461 +	TDateTime dt(1970,EJanuary,0,0,0,0,0);
   1.462 +	TTime time(dt);
   1.463 +
   1.464 +	TInt sec = STATIC_CAST(TInt, secVal);
   1.465 +	time += TTimeIntervalSeconds(sec);
   1.466 +	aDateTime = time.DateTime();
   1.467 +
   1.468 +	return bufLen;
   1.469 +	}