cryptoservices/certificateandkeymgmt/asn1/integerdec.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 integer class.
       
    16 * As noted in Notes.txt there are two approaches to the storage of data by
       
    17 * this class, it can either hold a copy of the data or a pointer to something
       
    18 * else which holds a copy of the data.
       
    19 * The object contained within this file encapsulates the ASN1 integer object
       
    20 * as described in X208 section 14 and the encoding described in X209 section
       
    21 * 8. 
       
    22 *
       
    23 */
       
    24 
       
    25 
       
    26 #include "asn1dec.h"
       
    27 #include <bigint.h>
       
    28 
       
    29 EXPORT_C TASN1DecInteger::TASN1DecInteger()
       
    30 
       
    31 	{
       
    32 	}
       
    33 
       
    34 EXPORT_C TInt TASN1DecInteger::DecodeDERShortL(const TDesC8& aSource,TInt& aPos)
       
    35 	{
       
    36 	TPtrC8 Source=aSource.Mid(aPos);
       
    37 	TASN1DecGeneric gen(Source);
       
    38 	gen.InitL();
       
    39 	TInt res = DecodeDERShortL(gen);
       
    40 	aPos+=gen.LengthDER();
       
    41 	return res;
       
    42 	}
       
    43 
       
    44 EXPORT_C TInt TASN1DecInteger::DecodeDERShortL(const TASN1DecGeneric& aSource)
       
    45 	{
       
    46 	TInt res = 0;
       
    47 	TPtrC8 ptr(aSource.GetContentDER());
       
    48 	//stop stupid compiler warning
       
    49 	TUint length = (TUint)(ptr.Length());
       
    50 	if (length == 0) // DEF055722 
       
    51 		{
       
    52 		User::Leave(KErrArgument);
       
    53 		}
       
    54 	if (length > sizeof(TInt))
       
    55 		{
       
    56 		User::Leave(KErrTooBig);
       
    57 		}
       
    58 	TInt8 i;
       
    59 	if (ptr[0]&128)
       
    60 		{
       
    61 		res = -1;
       
    62 		}
       
    63 	for (i=0;i<ptr.Length();i++)
       
    64 		{
       
    65 		res<<=8;
       
    66 		res+=ptr[i];
       
    67 		}
       
    68 	return res;
       
    69 	}
       
    70 
       
    71 EXPORT_C RInteger TASN1DecInteger::DecodeDERLongL(const TDesC8& aSource,TInt& aPos)
       
    72 	{
       
    73 	TPtrC8 Source=aSource.Right(aSource.Length() - aPos);
       
    74 	TASN1DecGeneric gen(Source);
       
    75 	gen.InitL();
       
    76 	RInteger res = DecodeDERLongL(gen);
       
    77 	aPos+=gen.LengthDER();
       
    78 	return res;
       
    79 	}
       
    80 
       
    81 EXPORT_C RInteger TASN1DecInteger::DecodeDERLongL(const TASN1DecGeneric& aSource)
       
    82 	{
       
    83 	const TDesC8& data = aSource.GetContentDER();
       
    84 //	JCS 14/03/2003: Changed call to NewL from NewSignedL() because current
       
    85 //	problem in RInteger for signed code means this sometimes returns incorrect
       
    86 //	results causing failure in certstore testcode.  However, making this change
       
    87 //	does cause problems in TASN1 now (posted defect DEF021796).
       
    88 	RInteger res = RInteger::NewL(data);//aSource.GetContentDER());
       
    89 	return (res);
       
    90 	}