1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/genericdec.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,148 @@
1.4 +/*
1.5 +* Copyright (c) 1998-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 "asn1dec.h"
1.23 +
1.24 +EXPORT_C TASN1DecGeneric::TASN1DecGeneric(const TDesC8& aSource):
1.25 + iEncoding(aSource)
1.26 + {
1.27 + }
1.28 +
1.29 +EXPORT_C void TASN1DecGeneric::InitL()
1.30 +
1.31 +// x209 6.2, 6.3
1.32 + {
1.33 + if(iEncoding.Length() < KASN1ObjectMinLength)
1.34 + {
1.35 + User::Leave(KErrArgument);
1.36 + }
1.37 +
1.38 + TInt i=iEncoding[0];
1.39 + TInt b=i>>6;
1.40 + iClass=(TASN1Class)b;
1.41 +
1.42 + TInt Length=0;
1.43 + TInt LengthOfLength=1;
1.44 + TInt Pos=0;
1.45 + TInt T=0;
1.46 + TInt encodingLength = iEncoding.Length();
1.47 + // work out tag
1.48 + if ((iEncoding[Pos]&0x1f)==0x1f)
1.49 + {
1.50 + // tag greater than 30
1.51 + Pos++;
1.52 + if (encodingLength <= Pos+1)
1.53 + {
1.54 + User::Leave(KErrArgument);
1.55 + }
1.56 + while (iEncoding[Pos]>=128)
1.57 + {
1.58 + T<<=7;
1.59 + T+=(TUint8)(iEncoding[Pos]&127); // this raises warning if TTagType is TUint8
1.60 + Pos++;
1.61 + if (encodingLength <= Pos+1)
1.62 + {
1.63 + User::Leave(KErrArgument);
1.64 + }
1.65 + }
1.66 + T<<=7;
1.67 + T+=(TUint8)iEncoding[Pos];
1.68 + }
1.69 + else
1.70 + {
1.71 + T=(TUint8)(iEncoding[Pos]&0x1f);
1.72 + }
1.73 + iExplicitTag = T;
1.74 + Pos++;
1.75 +
1.76 + TUint8 l = iEncoding[Pos];
1.77 + if (l > 128)
1.78 + {
1.79 + // this is a length of length
1.80 + if (encodingLength <= Pos+1)
1.81 + {
1.82 + User::Leave(KErrArgument);
1.83 + }
1.84 + LengthOfLength=iEncoding[Pos++]&127;
1.85 + Length=0;
1.86 + while (LengthOfLength)
1.87 + {
1.88 + if (encodingLength <= Pos+1)
1.89 + {
1.90 + User::Leave(KErrArgument);
1.91 + }
1.92 + Length<<=8;
1.93 + Length+=iEncoding[Pos++];
1.94 + LengthOfLength--;
1.95 + }
1.96 + }
1.97 + if (l == 128)
1.98 + {
1.99 + //this is constructed, indefinite length: we don't support this
1.100 + User::Leave(KErrNotSupported);
1.101 + }
1.102 + if (l < 128)
1.103 + {
1.104 + //this is a straight length
1.105 + Length = iEncoding[Pos++];
1.106 + }
1.107 + if (((Length+Pos)>encodingLength) || (Length < 0))
1.108 + {
1.109 + User::Leave(KErrArgument);
1.110 + }
1.111 + iStartOfContents = Pos;
1.112 + iLength = Length;
1.113 + }
1.114 +
1.115 +EXPORT_C TPtrC8 TASN1DecGeneric::GetContentDER(void) const
1.116 + {
1.117 + return iEncoding.Mid(iStartOfContents, iLength);
1.118 + }
1.119 +
1.120 +EXPORT_C TInt TASN1DecGeneric::LengthDER(void) const
1.121 + {
1.122 + return iLength+iStartOfContents;
1.123 + }
1.124 +
1.125 +EXPORT_C TInt TASN1DecGeneric::LengthDERContent(void) const
1.126 + {
1.127 + return iLength;
1.128 + }
1.129 +
1.130 +EXPORT_C TInt TASN1DecGeneric::LengthDERHeader() const
1.131 + {
1.132 + return LengthDER() - LengthDERContent();
1.133 + }
1.134 +
1.135 +EXPORT_C TPtrC8 TASN1DecGeneric::Encoding() const
1.136 + {
1.137 + return iEncoding.Left(LengthDER());
1.138 + }
1.139 +
1.140 +EXPORT_C TTagType TASN1DecGeneric::Tag() const
1.141 +// we can inline this
1.142 + {
1.143 + return iExplicitTag;
1.144 + }
1.145 +
1.146 +EXPORT_C TASN1Class TASN1DecGeneric::Class() const
1.147 +// we can inline this
1.148 + {
1.149 + return iClass;
1.150 + }
1.151 +