cryptoservices/certificateandkeymgmt/asn1/genericdec.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 1998-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 "asn1dec.h"
       
    20 
       
    21 EXPORT_C TASN1DecGeneric::TASN1DecGeneric(const TDesC8& aSource):
       
    22  iEncoding(aSource)
       
    23 	{
       
    24 	}
       
    25 
       
    26 EXPORT_C void TASN1DecGeneric::InitL()
       
    27 
       
    28 // x209 6.2, 6.3
       
    29 	{
       
    30 	if(iEncoding.Length() < KASN1ObjectMinLength)
       
    31 		{
       
    32 		User::Leave(KErrArgument);
       
    33 		}	
       
    34 	
       
    35 	TInt i=iEncoding[0];
       
    36 	TInt b=i>>6;
       
    37 	iClass=(TASN1Class)b;
       
    38 
       
    39 	TInt Length=0;
       
    40 	TInt LengthOfLength=1;
       
    41 	TInt Pos=0;
       
    42 	TInt T=0;
       
    43 	TInt encodingLength = iEncoding.Length();
       
    44 	// work out tag 
       
    45 	if ((iEncoding[Pos]&0x1f)==0x1f)
       
    46 		{
       
    47 		// tag greater than 30
       
    48 		Pos++;
       
    49 		if (encodingLength <= Pos+1)
       
    50 			{
       
    51 			User::Leave(KErrArgument);
       
    52 			}
       
    53 		while (iEncoding[Pos]>=128)
       
    54 			{
       
    55 			T<<=7;
       
    56 			T+=(TUint8)(iEncoding[Pos]&127);		// this raises warning if TTagType is TUint8
       
    57 			Pos++;
       
    58 			if (encodingLength <= Pos+1)
       
    59 				{
       
    60 				User::Leave(KErrArgument);
       
    61 				}
       
    62 			}
       
    63 		T<<=7;
       
    64 		T+=(TUint8)iEncoding[Pos];
       
    65 		}
       
    66 	else
       
    67 		{
       
    68 		T=(TUint8)(iEncoding[Pos]&0x1f);
       
    69 		}
       
    70 	iExplicitTag = T;
       
    71 	Pos++;
       
    72 
       
    73 	TUint8 l = iEncoding[Pos];
       
    74 	if (l > 128)
       
    75 		{
       
    76 		// this is a length of length
       
    77 		if (encodingLength <= Pos+1)
       
    78 			{
       
    79 			User::Leave(KErrArgument);
       
    80 			}
       
    81 		LengthOfLength=iEncoding[Pos++]&127;
       
    82 		Length=0;
       
    83 		while (LengthOfLength)
       
    84 			{
       
    85 			if (encodingLength <= Pos+1)
       
    86 				{
       
    87 				User::Leave(KErrArgument);
       
    88 				}
       
    89 			Length<<=8;
       
    90 			Length+=iEncoding[Pos++];
       
    91 			LengthOfLength--;
       
    92 			}
       
    93 		}
       
    94 	if (l == 128)
       
    95 		{
       
    96 		//this is constructed, indefinite length: we don't support this
       
    97 		User::Leave(KErrNotSupported);
       
    98 		}
       
    99 	if (l < 128)
       
   100 		{
       
   101 		//this is a straight length
       
   102 		Length = iEncoding[Pos++];
       
   103 		}
       
   104 	if (((Length+Pos)>encodingLength) || (Length < 0))
       
   105 		{
       
   106 		User::Leave(KErrArgument);
       
   107 		}
       
   108 	iStartOfContents = Pos;
       
   109 	iLength = Length;
       
   110 	}
       
   111 
       
   112 EXPORT_C TPtrC8 TASN1DecGeneric::GetContentDER(void) const
       
   113 	{
       
   114 	return iEncoding.Mid(iStartOfContents, iLength);
       
   115 	}
       
   116 
       
   117 EXPORT_C TInt TASN1DecGeneric::LengthDER(void) const
       
   118 	{
       
   119 	return iLength+iStartOfContents;
       
   120 	}
       
   121 
       
   122 EXPORT_C TInt TASN1DecGeneric::LengthDERContent(void) const
       
   123 	{
       
   124 	return iLength;
       
   125 	}
       
   126 
       
   127 EXPORT_C TInt TASN1DecGeneric::LengthDERHeader() const
       
   128 	{
       
   129 	return LengthDER() - LengthDERContent();
       
   130 	}
       
   131 
       
   132 EXPORT_C TPtrC8 TASN1DecGeneric::Encoding() const
       
   133 	{
       
   134 	return iEncoding.Left(LengthDER());
       
   135 	}
       
   136 
       
   137 EXPORT_C TTagType TASN1DecGeneric::Tag() const
       
   138 // we can inline this
       
   139 	{
       
   140 	return iExplicitTag;
       
   141 	}
       
   142 
       
   143 EXPORT_C TASN1Class TASN1DecGeneric::Class() const
       
   144 // we can inline this
       
   145 	{
       
   146 	return iClass;
       
   147 	}