os/security/cryptoservices/certificateandkeymgmt/asn1/sequencedec.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 Sequence and Sequence-of 
    16 * object, as described in X208 Sections 20 and 21 and X209 Sections 14 and 
    17 * 15. The sequence object can contain an ordered series of zero, one or more
    18 * other ASN1 objects. The sequence-of object contains zero, one or more of 
    19 * the objects described in its definition. The data encodings of both these
    20 * objects is identical. The ordering of the objects contained within the 
    21 * sequence is defined outside the scope of this object, normally this would
    22 * be handled by an object deriving from this object but it may be handled
    23 * somewhere else. 
    24 *
    25 */
    26 
    27 
    28 #include "asn1dec.h"
    29 
    30 
    31 void TASN1DecSequence::CleanupSequence(TAny* aArray)
    32 	{
    33 	CArrayPtrFlat<TASN1DecGeneric>* array = REINTERPRET_CAST(CArrayPtrFlat<TASN1DecGeneric>*, aArray);
    34 	array->ResetAndDestroy();
    35 	delete array;
    36 	}
    37 
    38 EXPORT_C TASN1DecSequence::TASN1DecSequence()
    39 	{
    40 	}
    41 	
    42 EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TDesC8& aSource,TInt& aPos)
    43 
    44 	{
    45 	CArrayPtrFlat<TASN1DecGeneric>* ret = new(ELeave) CArrayPtrFlat<TASN1DecGeneric> (1);
    46 	TCleanupItem cleanupSeq(TASN1DecSequence::CleanupSequence, ret);
    47 	CleanupStack::PushL(cleanupSeq);
    48 	TASN1DecGeneric gen(aSource.Right(aSource.Length() - aPos));
    49 	gen.InitL();
    50 	aPos += gen.LengthDER();
    51 	DecodeContentsL(gen.GetContentDER(), *ret);
    52 	return ret;
    53 	}
    54 
    55 EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TASN1DecGeneric& aSource)
    56 	{
    57 	CArrayPtrFlat<TASN1DecGeneric>* ret = new(ELeave) CArrayPtrFlat<TASN1DecGeneric> (1);
    58 	TCleanupItem cleanupSeq(TASN1DecSequence::CleanupSequence, ret);
    59 	CleanupStack::PushL(cleanupSeq);
    60 	DecodeContentsL(aSource.GetContentDER(), *ret);
    61 	return ret;
    62 	}
    63 
    64 EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TDesC8& aSource,TInt& aPos, TInt aMin, TInt aMax)
    65 
    66 	{
    67 	CArrayPtrFlat<TASN1DecGeneric>* ret = DecodeDERLC(aSource, aPos);
    68 	TInt count = ret->Count();
    69 	if ((count < aMin) || (count > aMax))
    70 		{
    71 		User::Leave(KErrArgument);
    72 		}
    73 	return ret;
    74 	}
    75 
    76 EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TASN1DecGeneric& aSource, TInt aMin, TInt aMax)
    77 	{
    78 	CArrayPtrFlat<TASN1DecGeneric>* ret = DecodeDERLC(aSource);
    79 	TInt count = ret->Count();
    80 	if ((count < aMin) || (count > aMax))
    81 		{
    82 		User::Leave(KErrArgument);
    83 		}
    84 	return ret;
    85 	}
    86 
    87 void TASN1DecSequence::DecodeContentsL(const TDesC8& aSource, CArrayPtrFlat<TASN1DecGeneric>& aDest)
    88 	{
    89 	TInt pos = 0;
    90 	while (pos < aSource.Length())
    91 		{
    92 		TPtrC8 p(aSource.Right(aSource.Length() - pos));
    93 		TASN1DecGeneric* Decoded = new(ELeave) TASN1DecGeneric(p);
    94 		CleanupStack::PushL(Decoded);
    95 		Decoded->InitL();
    96 		aDest.AppendL(Decoded);
    97 		pos+=Decoded->LengthDER();
    98 		CleanupStack::Pop();
    99 		}
   100 	}