cryptoservices/certificateandkeymgmt/asn1/bigintenc.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2001-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 * Implementation for the class encoding Big Integers in ASN1 DER
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <asn1enc.h>
       
    21 
       
    22 #include <bigint.h>
       
    23 
       
    24 
       
    25 EXPORT_C CASN1EncBigInt* CASN1EncBigInt::NewLC(const TInteger& aInteger)
       
    26 	{
       
    27 	CASN1EncBigInt* self = new (ELeave) CASN1EncBigInt();
       
    28 	CleanupStack::PushL(self);
       
    29 	self->ConstructL(aInteger);
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 EXPORT_C CASN1EncBigInt* CASN1EncBigInt::NewL(const TInteger& aInteger)
       
    34 	{
       
    35 	CASN1EncBigInt* self = NewLC(aInteger);
       
    36 	CleanupStack::Pop(self);
       
    37 	return self;
       
    38 	}
       
    39 
       
    40 
       
    41 EXPORT_C CASN1EncBigInt::~CASN1EncBigInt()
       
    42 	{
       
    43 	delete iContents;
       
    44 	}
       
    45 
       
    46 
       
    47 CASN1EncBigInt::CASN1EncBigInt()
       
    48 : CASN1EncPrimitive(EASN1Integer)
       
    49 	{
       
    50 	}
       
    51 
       
    52 void CASN1EncBigInt::ConstructL(const TInteger& aInteger)
       
    53 {
       
    54 	iContents = aInteger.BufferLC();
       
    55 	
       
    56 	CleanupStack::Pop(); // We take ownership of the contents
       
    57 
       
    58 	// May wish to skip leading byte(s) if they're unnecessary - use
       
    59 	// iWriteContents to point to the minimal part of Contents, that we'll actually use.
       
    60 	TInt last = iContents->Length() - 1;
       
    61 	TInt first = 0;
       
    62 //	if (aInteger >= 0)	//	Only signed integers now...
       
    63 //		{
       
    64 		while (first < last
       
    65 			&& (*iContents)[first] == 0
       
    66 			&& !((*iContents)[first + 1] & 0x80))
       
    67 			{
       
    68 			++first;
       
    69 			}
       
    70 //		}
       
    71 /*	else
       
    72 		{
       
    73 		while (first < last
       
    74 			&& (*iContents)[first] == 0xFF
       
    75 			&& (*iContents)[first + 1] & 0x80)
       
    76 			{
       
    77 			++first;
       
    78 			}
       
    79 		}
       
    80 */
       
    81 		//to encode positive integers correctly add a leading zero byte
       
    82 		//if the first bit is set DEF038956
       
    83 		if (iContents->Length() != 0)
       
    84 			{
       
    85 			if ((*iContents)[first] & 0x80)
       
    86 				{
       
    87 				_LIT8(KPadZero, "\0");
       
    88 				TPtr8 ptr = iContents->Des();
       
    89 				//check if the buffer does not have enough space to insert a byte
       
    90 				if (ptr.Length() == ptr.MaxLength())
       
    91 					{
       
    92 					iContents = iContents->ReAllocL(iContents->Length() + 1);
       
    93 					TPtr8 reallocptr = iContents->Des();
       
    94 					reallocptr.Insert(0, KPadZero);
       
    95 					}
       
    96 				else
       
    97 					ptr.Insert(0, KPadZero);
       
    98 				}
       
    99 			}
       
   100 
       
   101 
       
   102 	iWriteContents.Set(iContents->Mid(first));
       
   103 
       
   104 	CASN1EncPrimitive::ConstructL();
       
   105 	}
       
   106 
       
   107 
       
   108 void CASN1EncBigInt::CalculateContentsLengthDER()
       
   109 	{
       
   110 	iContentsLengthDER = iWriteContents.Length();
       
   111 	}
       
   112 
       
   113 
       
   114 void CASN1EncBigInt::WriteContentsDERL(TDes8& aBuf) const
       
   115 	{
       
   116 	aBuf.Copy(iWriteContents);
       
   117 	}