os/security/cryptoservices/certificateandkeymgmt/wtlscert/wtlsdec.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 1997-2009 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 the License "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: 
    15 *
    16 */
    17 
    18 
    19 #include "wtlsdec.h"
    20 
    21 //stolen from stdlib
    22 #define WTLS_UNIX_BASE   TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000))    // 00:00, Jan 1st 1970
    23 const TInt KTimeLength = 4;
    24 
    25 
    26 TWTLSDecVector::TWTLSDecVector(const TDesC8& aSource, TInt aMinLength, TInt aMaxLength)
    27 	:iEncoding(aSource), iMinLength(aMinLength), iMaxLength(aMaxLength), iLengthOfContents(0), iLengthOfLength(0)
    28 	{
    29 	}
    30 
    31 void TWTLSDecVector::InitL()
    32 	{
    33 	if (iMaxLength <= 255)
    34 		{
    35 		iLengthOfLength = 1;
    36 		}
    37 	else
    38 		{
    39 		if (iMaxLength <= 65535)
    40 			{
    41 			iLengthOfLength = 2;
    42 			}
    43 		else
    44 			{
    45 			if (iMaxLength <= 16777215)
    46 				{
    47 				iLengthOfLength = 3;
    48 				}
    49 			else
    50 				{
    51 				iLengthOfLength = 4;
    52 				}
    53 			}
    54 		}
    55 	if (iEncoding.Length() < (iLengthOfLength + iMinLength))
    56 		{
    57 		User::Leave(KErrArgument);
    58 		}
    59 	TWTLSDecUnsignedInteger decLength;
    60 	TInt pos = 0;
    61 	iLengthOfContents = decLength.DecodeShortL(iEncoding, pos, iLengthOfLength);
    62 	if (iEncoding.Length() < (iLengthOfLength + iLengthOfContents))
    63 		{
    64 		User::Leave(KErrArgument);
    65 		}
    66 	}
    67 /*
    68 TPtrC8 TWTLSDecVector::Encoding() const
    69 	{
    70 	return iEncoding.Left(EncodingLength());
    71 	}
    72 */
    73 TInt TWTLSDecVector::EncodingLength() const
    74 	{
    75 	return iLengthOfLength + iLengthOfContents;
    76 	}
    77 /*	
    78 TPtrC8 TWTLSDecVector::Content() const
    79 	{
    80 	return iEncoding.Mid(iLengthOfLength, iLengthOfContents);
    81 	}
    82 
    83 TInt TWTLSDecVector::ContentLength() const
    84 	{
    85 	return iLengthOfContents;
    86 	}
    87 	
    88 TInt TWTLSDecVector::HeaderLength() const
    89 	{
    90 	return iLengthOfLength;
    91 	}
    92 */
    93 TWTLSDecUnsignedInteger::TWTLSDecUnsignedInteger()
    94 	{
    95 	}
    96 
    97 TInt TWTLSDecUnsignedInteger::DecodeShortL(const TDesC8& aSource,TInt& aPos, TInt aLength)
    98 	{
    99 	if (aLength > 4)
   100 		{
   101 		User::Leave(KErrOverflow);
   102 		}
   103 	TInt sourceLength = aSource.Length();
   104 	TInt res=0;
   105 	while (aLength)
   106 		{
   107 		if (sourceLength <= aPos)
   108 			{
   109 			User::Leave(KErrArgument);
   110 			}
   111 		res<<=8;
   112 		res+=aSource[aPos++];
   113 		aLength--;
   114 		}
   115 	return res;
   116 	}
   117 
   118 RInteger TWTLSDecUnsignedInteger::DecodeLongL(const TDesC8& aSource,TInt& aPos, TInt aLength)
   119 	{
   120 	if (aSource.Length() < (aPos + aLength))
   121 		{
   122 		User::Leave(KErrArgument);
   123 		}
   124 	const TPtrC8 ptr = aSource.Mid(aPos, aLength);
   125 	aPos += aLength;
   126 	RInteger res = RInteger::NewL(ptr);
   127 	return res;
   128 	}
   129 
   130 
   131 TWTLSDecTime::TWTLSDecTime()
   132 	{
   133 	}
   134 
   135 TTime TWTLSDecTime::DecodeL(const TDesC8& aSource, TInt& aPos)
   136 	{
   137 	if ((aSource.Length() - aPos) < KTimeLength)
   138 		{
   139 		User::Leave(KErrArgument);
   140 		}
   141 	TWTLSDecUnsignedInteger decInt;
   142 	TInt inc = decInt.DecodeShortL(aSource, aPos, 4);
   143 	return WTLS_UNIX_BASE + TTimeIntervalSeconds(inc);
   144 	}