os/security/cryptoservices/certificateandkeymgmt/asn1/bigintenc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/bigintenc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,117 @@
     1.4 +/*
     1.5 +* Copyright (c) 2001-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 +* Implementation for the class encoding Big Integers in ASN1 DER
    1.19 +*
    1.20 +*/
    1.21 +
    1.22 +
    1.23 +#include <asn1enc.h>
    1.24 +
    1.25 +#include <bigint.h>
    1.26 +
    1.27 +
    1.28 +EXPORT_C CASN1EncBigInt* CASN1EncBigInt::NewLC(const TInteger& aInteger)
    1.29 +	{
    1.30 +	CASN1EncBigInt* self = new (ELeave) CASN1EncBigInt();
    1.31 +	CleanupStack::PushL(self);
    1.32 +	self->ConstructL(aInteger);
    1.33 +	return self;
    1.34 +	}
    1.35 +
    1.36 +EXPORT_C CASN1EncBigInt* CASN1EncBigInt::NewL(const TInteger& aInteger)
    1.37 +	{
    1.38 +	CASN1EncBigInt* self = NewLC(aInteger);
    1.39 +	CleanupStack::Pop(self);
    1.40 +	return self;
    1.41 +	}
    1.42 +
    1.43 +
    1.44 +EXPORT_C CASN1EncBigInt::~CASN1EncBigInt()
    1.45 +	{
    1.46 +	delete iContents;
    1.47 +	}
    1.48 +
    1.49 +
    1.50 +CASN1EncBigInt::CASN1EncBigInt()
    1.51 +: CASN1EncPrimitive(EASN1Integer)
    1.52 +	{
    1.53 +	}
    1.54 +
    1.55 +void CASN1EncBigInt::ConstructL(const TInteger& aInteger)
    1.56 +{
    1.57 +	iContents = aInteger.BufferLC();
    1.58 +	
    1.59 +	CleanupStack::Pop(); // We take ownership of the contents
    1.60 +
    1.61 +	// May wish to skip leading byte(s) if they're unnecessary - use
    1.62 +	// iWriteContents to point to the minimal part of Contents, that we'll actually use.
    1.63 +	TInt last = iContents->Length() - 1;
    1.64 +	TInt first = 0;
    1.65 +//	if (aInteger >= 0)	//	Only signed integers now...
    1.66 +//		{
    1.67 +		while (first < last
    1.68 +			&& (*iContents)[first] == 0
    1.69 +			&& !((*iContents)[first + 1] & 0x80))
    1.70 +			{
    1.71 +			++first;
    1.72 +			}
    1.73 +//		}
    1.74 +/*	else
    1.75 +		{
    1.76 +		while (first < last
    1.77 +			&& (*iContents)[first] == 0xFF
    1.78 +			&& (*iContents)[first + 1] & 0x80)
    1.79 +			{
    1.80 +			++first;
    1.81 +			}
    1.82 +		}
    1.83 +*/
    1.84 +		//to encode positive integers correctly add a leading zero byte
    1.85 +		//if the first bit is set DEF038956
    1.86 +		if (iContents->Length() != 0)
    1.87 +			{
    1.88 +			if ((*iContents)[first] & 0x80)
    1.89 +				{
    1.90 +				_LIT8(KPadZero, "\0");
    1.91 +				TPtr8 ptr = iContents->Des();
    1.92 +				//check if the buffer does not have enough space to insert a byte
    1.93 +				if (ptr.Length() == ptr.MaxLength())
    1.94 +					{
    1.95 +					iContents = iContents->ReAllocL(iContents->Length() + 1);
    1.96 +					TPtr8 reallocptr = iContents->Des();
    1.97 +					reallocptr.Insert(0, KPadZero);
    1.98 +					}
    1.99 +				else
   1.100 +					ptr.Insert(0, KPadZero);
   1.101 +				}
   1.102 +			}
   1.103 +
   1.104 +
   1.105 +	iWriteContents.Set(iContents->Mid(first));
   1.106 +
   1.107 +	CASN1EncPrimitive::ConstructL();
   1.108 +	}
   1.109 +
   1.110 +
   1.111 +void CASN1EncBigInt::CalculateContentsLengthDER()
   1.112 +	{
   1.113 +	iContentsLengthDER = iWriteContents.Length();
   1.114 +	}
   1.115 +
   1.116 +
   1.117 +void CASN1EncBigInt::WriteContentsDERL(TDes8& aBuf) const
   1.118 +	{
   1.119 +	aBuf.Copy(iWriteContents);
   1.120 +	}