messagingfw/wappushfw/pushutils/src/WapDecoder.cpp
changeset 0 8e480a14352b
child 44 7c176670643f
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2000-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 //
       
    15 
       
    16 #include "wapdecoder.h"
       
    17 
       
    18 const TInt KMaxLongIntSize = 0x1E;	// Long-integer can have maximum 30 OCTETS (WSP Spec)
       
    19 
       
    20 /**
       
    21 Extracts a WAP variable length integer from a binary data buffer.
       
    22 
       
    23 @param	aSource 
       
    24 Input binary data buffer.
       
    25 
       
    26 @param	aStartIndex
       
    27 Position in aSource from which to extract the integer.
       
    28 
       
    29 @param	aMultiByte
       
    30 On return, the extracted variable length integer.
       
    31 
       
    32 @leave	KErrCorrupt
       
    33 A valid variable length integer was not found in the buffer.
       
    34 */
       
    35 EXPORT_C void TWapBinCodex::ExtractUIntvarL(const TDesC8& aSource, TInt aStartIndex, TWapBinCodex::TUintvar& aMultiByte)
       
    36 	{
       
    37 	TUint8 byte = 0, topThreeBits = 0;
       
    38 	aMultiByte.iValue = 0;
       
    39 	aMultiByte.iOctetSize = 0;
       
    40 	TInt lenLeft = aSource.Length() - aStartIndex;
       
    41 
       
    42 	if (lenLeft <= 0)
       
    43 		User::Leave(KErrCorrupt);
       
    44 	
       
    45 	// get the first octet
       
    46 	byte = aSource[aStartIndex];
       
    47 
       
    48 	// if the topThreeBits are set and we are using all 5 octets, this is corrupt data
       
    49 	topThreeBits |= byte & 0x70; 
       
    50 	
       
    51 	// copy over the top byte
       
    52 	aMultiByte.iValue |= byte & ~0x80;
       
    53 	++aMultiByte.iOctetSize;
       
    54 
       
    55 	// while the top 'continue' byte is set and we have more data
       
    56 	while ((byte & 0x80) && (aMultiByte.iOctetSize < 5) && (--lenLeft > 0))
       
    57 	{
       
    58 		aMultiByte.iValue <<= 7;
       
    59 		byte = aSource[aStartIndex + aMultiByte.iOctetSize++];
       
    60 		aMultiByte.iValue |= byte & ~0x80;
       
    61 	} 
       
    62 
       
    63 	// last octet had continue bit set ... NOT allowed or aSource wasn't complete
       
    64 	if (byte & 0x80)
       
    65 		User::Leave(KErrCorrupt);
       
    66 
       
    67 	// this was encoded wrong and the topThreeBits are about to shift off ... NOT allowed
       
    68 	if (aMultiByte.iOctetSize == 5 && topThreeBits)
       
    69 		User::Leave(KErrCorrupt);
       
    70 
       
    71 	}
       
    72 
       
    73 /**
       
    74 Extracts a WAP-encoded multi-octet integer from a binary data buffer.
       
    75 
       
    76 The function will panic if the binary data is corrupt.
       
    77 
       
    78 @param	aSource
       
    79 The binary data buffer containing the integer.
       
    80 
       
    81 @param	aInt
       
    82 The output argument with the parsed integer.
       
    83 
       
    84 @panic
       
    85 The binary data buffer is corrupted (debug only).
       
    86 
       
    87 @deprecated TWapBinCodex::ParseMultiOctetInteger function.
       
    88 Use the new 
       
    89 */
       
    90 EXPORT_C void TWapBinCodex::ExtractMultiOctetInteger(const TDesC8& aSource, TInt& aInt)
       
    91 /* 
       
    92  * NOTE - It is a straight copy from CHttpResponse as the function isn't exported, and it is  
       
    93  * not possible to change the interface to the Wapplugins dll. 
       
    94  */
       
    95 	{
       
    96 	// Get num bytes encoding the integer - 
       
    97 	// we are positioned at that location in the source descriptor
       
    98 	TUint8 numBytes = aSource[0];
       
    99 	aInt = 0;
       
   100 	__ASSERT_DEBUG(numBytes < aSource.Length(), User::Invariant());
       
   101 	// Loop over the source, taking each byte and shifting it in to the count.  
       
   102     for (TInt count = 1; (count <= numBytes); count++)
       
   103         aInt = (aInt << 8) + aSource[count];
       
   104 	}
       
   105 
       
   106 /**
       
   107 Extracts a WAP-encoded multi-octet integer from a binary data buffer.
       
   108 
       
   109 If the integer could not be parsed, then the output argument aInt is not valid.
       
   110 
       
   111 @param
       
   112 aSource	The binary data buffer containing the integer.
       
   113 
       
   114 @param
       
   115 aInt	The output argument with the parsed integer.
       
   116 
       
   117 @return
       
   118 The number of bytes parsed or an error code. A value of KErrCorrupt indicates 
       
   119 that the data buffer contained a corrupted multi-octet integer.
       
   120 */
       
   121 EXPORT_C TInt TWapBinCodex::ParseMultiOctetInteger(const TDesC8& aSource, TInt& aInt)
       
   122 	{
       
   123 	// Get num bytes encoding the integer.
       
   124 	TUint8 numBytes = aSource[0];
       
   125 	aInt = 0;
       
   126 	
       
   127 	if( numBytes > aSource.Length() - 1 || numBytes > KMaxLongIntSize )
       
   128 		return KErrCorrupt;
       
   129 	
       
   130 	// Loop over the source, taking each byte and shifting it in to the count.  
       
   131     for (TInt count = 1; (count <= numBytes); ++count)
       
   132         aInt = (aInt << 8) + aSource[count];
       
   133 
       
   134 	return numBytes + 1;
       
   135 	}
       
   136