1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/wtlscert/wtlsdec.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,144 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include "wtlsdec.h"
1.23 +
1.24 +//stolen from stdlib
1.25 +#define WTLS_UNIX_BASE TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000)) // 00:00, Jan 1st 1970
1.26 +const TInt KTimeLength = 4;
1.27 +
1.28 +
1.29 +TWTLSDecVector::TWTLSDecVector(const TDesC8& aSource, TInt aMinLength, TInt aMaxLength)
1.30 + :iEncoding(aSource), iMinLength(aMinLength), iMaxLength(aMaxLength), iLengthOfContents(0), iLengthOfLength(0)
1.31 + {
1.32 + }
1.33 +
1.34 +void TWTLSDecVector::InitL()
1.35 + {
1.36 + if (iMaxLength <= 255)
1.37 + {
1.38 + iLengthOfLength = 1;
1.39 + }
1.40 + else
1.41 + {
1.42 + if (iMaxLength <= 65535)
1.43 + {
1.44 + iLengthOfLength = 2;
1.45 + }
1.46 + else
1.47 + {
1.48 + if (iMaxLength <= 16777215)
1.49 + {
1.50 + iLengthOfLength = 3;
1.51 + }
1.52 + else
1.53 + {
1.54 + iLengthOfLength = 4;
1.55 + }
1.56 + }
1.57 + }
1.58 + if (iEncoding.Length() < (iLengthOfLength + iMinLength))
1.59 + {
1.60 + User::Leave(KErrArgument);
1.61 + }
1.62 + TWTLSDecUnsignedInteger decLength;
1.63 + TInt pos = 0;
1.64 + iLengthOfContents = decLength.DecodeShortL(iEncoding, pos, iLengthOfLength);
1.65 + if (iEncoding.Length() < (iLengthOfLength + iLengthOfContents))
1.66 + {
1.67 + User::Leave(KErrArgument);
1.68 + }
1.69 + }
1.70 +/*
1.71 +TPtrC8 TWTLSDecVector::Encoding() const
1.72 + {
1.73 + return iEncoding.Left(EncodingLength());
1.74 + }
1.75 +*/
1.76 +TInt TWTLSDecVector::EncodingLength() const
1.77 + {
1.78 + return iLengthOfLength + iLengthOfContents;
1.79 + }
1.80 +/*
1.81 +TPtrC8 TWTLSDecVector::Content() const
1.82 + {
1.83 + return iEncoding.Mid(iLengthOfLength, iLengthOfContents);
1.84 + }
1.85 +
1.86 +TInt TWTLSDecVector::ContentLength() const
1.87 + {
1.88 + return iLengthOfContents;
1.89 + }
1.90 +
1.91 +TInt TWTLSDecVector::HeaderLength() const
1.92 + {
1.93 + return iLengthOfLength;
1.94 + }
1.95 +*/
1.96 +TWTLSDecUnsignedInteger::TWTLSDecUnsignedInteger()
1.97 + {
1.98 + }
1.99 +
1.100 +TInt TWTLSDecUnsignedInteger::DecodeShortL(const TDesC8& aSource,TInt& aPos, TInt aLength)
1.101 + {
1.102 + if (aLength > 4)
1.103 + {
1.104 + User::Leave(KErrOverflow);
1.105 + }
1.106 + TInt sourceLength = aSource.Length();
1.107 + TInt res=0;
1.108 + while (aLength)
1.109 + {
1.110 + if (sourceLength <= aPos)
1.111 + {
1.112 + User::Leave(KErrArgument);
1.113 + }
1.114 + res<<=8;
1.115 + res+=aSource[aPos++];
1.116 + aLength--;
1.117 + }
1.118 + return res;
1.119 + }
1.120 +
1.121 +RInteger TWTLSDecUnsignedInteger::DecodeLongL(const TDesC8& aSource,TInt& aPos, TInt aLength)
1.122 + {
1.123 + if (aSource.Length() < (aPos + aLength))
1.124 + {
1.125 + User::Leave(KErrArgument);
1.126 + }
1.127 + const TPtrC8 ptr = aSource.Mid(aPos, aLength);
1.128 + aPos += aLength;
1.129 + RInteger res = RInteger::NewL(ptr);
1.130 + return res;
1.131 + }
1.132 +
1.133 +
1.134 +TWTLSDecTime::TWTLSDecTime()
1.135 + {
1.136 + }
1.137 +
1.138 +TTime TWTLSDecTime::DecodeL(const TDesC8& aSource, TInt& aPos)
1.139 + {
1.140 + if ((aSource.Length() - aPos) < KTimeLength)
1.141 + {
1.142 + User::Leave(KErrArgument);
1.143 + }
1.144 + TWTLSDecUnsignedInteger decInt;
1.145 + TInt inc = decInt.DecodeShortL(aSource, aPos, 4);
1.146 + return WTLS_UNIX_BASE + TTimeIntervalSeconds(inc);
1.147 + }