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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* This file contains the implementation of the ASN1 Sequence and Sequence-of 
sl@0
    16
* object, as described in X208 Sections 20 and 21 and X209 Sections 14 and 
sl@0
    17
* 15. The sequence object can contain an ordered series of zero, one or more
sl@0
    18
* other ASN1 objects. The sequence-of object contains zero, one or more of 
sl@0
    19
* the objects described in its definition. The data encodings of both these
sl@0
    20
* objects is identical. The ordering of the objects contained within the 
sl@0
    21
* sequence is defined outside the scope of this object, normally this would
sl@0
    22
* be handled by an object deriving from this object but it may be handled
sl@0
    23
* somewhere else. 
sl@0
    24
*
sl@0
    25
*/
sl@0
    26
sl@0
    27
sl@0
    28
#include "asn1dec.h"
sl@0
    29
sl@0
    30
sl@0
    31
void TASN1DecSequence::CleanupSequence(TAny* aArray)
sl@0
    32
	{
sl@0
    33
	CArrayPtrFlat<TASN1DecGeneric>* array = REINTERPRET_CAST(CArrayPtrFlat<TASN1DecGeneric>*, aArray);
sl@0
    34
	array->ResetAndDestroy();
sl@0
    35
	delete array;
sl@0
    36
	}
sl@0
    37
sl@0
    38
EXPORT_C TASN1DecSequence::TASN1DecSequence()
sl@0
    39
	{
sl@0
    40
	}
sl@0
    41
	
sl@0
    42
EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TDesC8& aSource,TInt& aPos)
sl@0
    43
sl@0
    44
	{
sl@0
    45
	CArrayPtrFlat<TASN1DecGeneric>* ret = new(ELeave) CArrayPtrFlat<TASN1DecGeneric> (1);
sl@0
    46
	TCleanupItem cleanupSeq(TASN1DecSequence::CleanupSequence, ret);
sl@0
    47
	CleanupStack::PushL(cleanupSeq);
sl@0
    48
	TASN1DecGeneric gen(aSource.Right(aSource.Length() - aPos));
sl@0
    49
	gen.InitL();
sl@0
    50
	aPos += gen.LengthDER();
sl@0
    51
	DecodeContentsL(gen.GetContentDER(), *ret);
sl@0
    52
	return ret;
sl@0
    53
	}
sl@0
    54
sl@0
    55
EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TASN1DecGeneric& aSource)
sl@0
    56
	{
sl@0
    57
	CArrayPtrFlat<TASN1DecGeneric>* ret = new(ELeave) CArrayPtrFlat<TASN1DecGeneric> (1);
sl@0
    58
	TCleanupItem cleanupSeq(TASN1DecSequence::CleanupSequence, ret);
sl@0
    59
	CleanupStack::PushL(cleanupSeq);
sl@0
    60
	DecodeContentsL(aSource.GetContentDER(), *ret);
sl@0
    61
	return ret;
sl@0
    62
	}
sl@0
    63
sl@0
    64
EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TDesC8& aSource,TInt& aPos, TInt aMin, TInt aMax)
sl@0
    65
sl@0
    66
	{
sl@0
    67
	CArrayPtrFlat<TASN1DecGeneric>* ret = DecodeDERLC(aSource, aPos);
sl@0
    68
	TInt count = ret->Count();
sl@0
    69
	if ((count < aMin) || (count > aMax))
sl@0
    70
		{
sl@0
    71
		User::Leave(KErrArgument);
sl@0
    72
		}
sl@0
    73
	return ret;
sl@0
    74
	}
sl@0
    75
sl@0
    76
EXPORT_C CArrayPtrFlat<TASN1DecGeneric>* TASN1DecSequence::DecodeDERLC(const TASN1DecGeneric& aSource, TInt aMin, TInt aMax)
sl@0
    77
	{
sl@0
    78
	CArrayPtrFlat<TASN1DecGeneric>* ret = DecodeDERLC(aSource);
sl@0
    79
	TInt count = ret->Count();
sl@0
    80
	if ((count < aMin) || (count > aMax))
sl@0
    81
		{
sl@0
    82
		User::Leave(KErrArgument);
sl@0
    83
		}
sl@0
    84
	return ret;
sl@0
    85
	}
sl@0
    86
sl@0
    87
void TASN1DecSequence::DecodeContentsL(const TDesC8& aSource, CArrayPtrFlat<TASN1DecGeneric>& aDest)
sl@0
    88
	{
sl@0
    89
	TInt pos = 0;
sl@0
    90
	while (pos < aSource.Length())
sl@0
    91
		{
sl@0
    92
		TPtrC8 p(aSource.Right(aSource.Length() - pos));
sl@0
    93
		TASN1DecGeneric* Decoded = new(ELeave) TASN1DecGeneric(p);
sl@0
    94
		CleanupStack::PushL(Decoded);
sl@0
    95
		Decoded->InitL();
sl@0
    96
		aDest.AppendL(Decoded);
sl@0
    97
		pos+=Decoded->LengthDER();
sl@0
    98
		CleanupStack::Pop();
sl@0
    99
		}
sl@0
   100
	}