cryptoservices/certificateandkeymgmt/asn1/oiddec.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 * This file contains the implementation of the ASN1 Object ID base class.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "asn1dec.h"
       
    21 
       
    22 const TInt KMaxOIDLength = 164;
       
    23 
       
    24 EXPORT_C TASN1DecObjectIdentifier::TASN1DecObjectIdentifier(void)
       
    25 	{
       
    26 	}
       
    27 
       
    28 EXPORT_C HBufC* TASN1DecObjectIdentifier::DecodeDERL(const TDesC8& aSource,TInt& aPos)
       
    29 	{
       
    30 	TASN1DecGeneric gen(aSource.Right(aSource.Length() - aPos));
       
    31 	gen.InitL();
       
    32 	HBufC* res = DecodeDERL(gen);
       
    33 	aPos+=gen.LengthDER();
       
    34 	return res;
       
    35 	}
       
    36 
       
    37 EXPORT_C HBufC* TASN1DecObjectIdentifier::DecodeDERL(const TASN1DecGeneric& aSource)
       
    38 	{
       
    39 	TFixedArray<TInt, KNumberOfIDs>* oid = new(ELeave) TFixedArray<TInt, KNumberOfIDs>;
       
    40 	CleanupStack::PushL(oid);
       
    41 	TInt count = DecodeContentsL(*oid, aSource.GetContentDER());
       
    42 	HBufC* res = HBufC::NewLC(KMaxOIDLength);
       
    43 	TPtr pRes = res->Des();
       
    44 	if (count > 0)
       
    45 		{
       
    46 		pRes.AppendNum((*oid)[0]);
       
    47 		for (TInt i = 1; i < count; i++)
       
    48 			{
       
    49 			pRes.Append('.');
       
    50 			pRes.AppendNum((*oid)[i]);
       
    51 			}
       
    52 		}
       
    53 	CleanupStack::Pop();//res
       
    54 	CleanupStack::PopAndDestroy();//oid
       
    55 	return res;
       
    56 	}
       
    57 
       
    58 TInt TASN1DecObjectIdentifier::DecodeContentsL(TFixedArray<TInt, KNumberOfIDs>& aArray, const TDesC8& aSource)
       
    59 	{
       
    60 	TInt l = aSource.Length();
       
    61 	TInt i=0;
       
    62 	TInt SubID=0;
       
    63 	TInt SIDn=0;
       
    64 	if(l < 1)
       
    65 		{
       
    66 		User::Leave(KErrArgument);
       
    67 		}
       
    68 	SubID+=aSource[i];
       
    69 	i++;
       
    70 	if (SubID>=80)
       
    71 		{
       
    72 		aArray[SIDn++]=2;
       
    73 		SubID-=80;
       
    74 		}
       
    75 	else
       
    76 		{
       
    77 		if (SubID>=40)
       
    78 			{
       
    79 			aArray[SIDn++]=1;
       
    80 			SubID-=40;
       
    81 			}
       
    82 		else
       
    83 			{
       
    84 			aArray[SIDn++]=0;
       
    85 			}
       
    86 		}
       
    87 	aArray[SIDn++]=SubID;
       
    88 	for (;i < l;)
       
    89 		{
       
    90 		SubID=0;
       
    91 		while (aSource[i]&0x80)
       
    92 			{
       
    93 			// if shifting by 7 does not increase the value its overflowed
       
    94 			if ( SubID && ((SubID << 7) <= SubID) )
       
    95 				{
       
    96 				User::Leave(KErrOverflow);
       
    97 				}
       
    98 				
       
    99 			SubID<<=7;
       
   100 			SubID+=aSource[i]&0x7f;
       
   101 			if(i == (l-1))
       
   102 				{
       
   103 				User::Leave(KErrArgument);
       
   104 				}
       
   105 			i++;
       
   106 			}
       
   107 		if ( SubID && ((SubID << 7) <= SubID) )
       
   108 			{
       
   109 			User::Leave(KErrOverflow);
       
   110 			}
       
   111 		SubID<<=7;
       
   112 		SubID+=aSource[i];
       
   113 		
       
   114 		i++;
       
   115 		if (SIDn >= KNumberOfIDs) 
       
   116 			{
       
   117 			User::Leave(KErrOverflow);
       
   118 			}
       
   119 		aArray[SIDn++]=SubID;
       
   120 		}
       
   121 	return SIDn;
       
   122 	}