os/security/cryptoservices/certificateandkeymgmt/asn1/encenc.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) 2005-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 *
    16 */
    17 
    18 
    19 /**
    20  @file
    21 */
    22 
    23 #include <e32std.h>
    24 #include <e32def.h>
    25 #include <asn1enc.h>
    26 #include <asn1dec.h>
    27 
    28 EXPORT_C CASN1EncEncoding::~CASN1EncEncoding()
    29 	{
    30 	delete iContents;
    31 	}
    32 
    33 // Set arbitrary initial values for tag type and class because they will be 
    34 // inited in ConstructL().
    35 EXPORT_C CASN1EncEncoding::CASN1EncEncoding() : 
    36 	CASN1EncBase(EASN1EOC, EUniversal)
    37 	{
    38 	}
    39 
    40 EXPORT_C CASN1EncEncoding* CASN1EncEncoding::NewLC(const TDesC8& aEncoding)
    41 	{
    42 	CASN1EncEncoding* self = new (ELeave) CASN1EncEncoding;
    43 	CleanupStack::PushL(self);
    44 	self->ConstructL(aEncoding);
    45 	return self;
    46 	}
    47 
    48 EXPORT_C CASN1EncEncoding* CASN1EncEncoding::NewL(const TDesC8& aEncoding)
    49 	{
    50 	CASN1EncEncoding* self = NewLC(aEncoding);
    51 	CleanupStack::Pop(self);
    52 	return self;
    53 	}
    54 
    55 EXPORT_C CASN1EncEncoding* CASN1EncEncoding::NewLC(const TDesC8& aEncoding, TTagType aType, TASN1Class aClass)
    56 	{
    57 	CASN1EncEncoding* self = new (ELeave) CASN1EncEncoding;
    58 	CleanupStack::PushL(self);
    59 	self->ConstructL(aEncoding, aType, aClass);
    60 	return self;		
    61 	}
    62 
    63 void CASN1EncEncoding::ConstructL(const TDesC8& aEncoding)
    64 	{
    65 	// Use decoder to get information about the outer wrapper of the passed 
    66 	// encoding.
    67 	TASN1DecGeneric decoder(aEncoding);
    68 	decoder.InitL();
    69 	iClass = decoder.Class();
    70 	iTag = decoder.Tag();
    71 	// Set this information so that the base class knows how to write 
    72 	// DER encoding.
    73 	SetTag(iTag, iClass);
    74 	// Copy just the contents of the passed encoding.
    75 	iContents = decoder.GetContentDER().AllocL();
    76 	// Save this for base class writing functions.
    77 	iContentsLengthDER = iContents->Length();
    78 	// This is base class' method which initializes length of length 
    79 	// encoding for proper DER writing.
    80 	CalculateLengthLengthDER();
    81 	}
    82 
    83 void CASN1EncEncoding::ConstructL(const TDesC8& aEncoding, TTagType aType, TASN1Class aClass)
    84 	{
    85 	// Use decoder to get information about the outer wrapper of the passed 
    86 	// encoding.
    87 	TASN1DecGeneric decoder(aEncoding);
    88 	decoder.InitL();
    89 	iClass = aClass;
    90 	iTag = aType;
    91 	// Set this information so that the base class knows how to write 
    92 	// DER encoding.
    93 	SetTag(aType, aClass);
    94 	// Copy just the contents of the passed encoding.
    95 	iContents = decoder.GetContentDER().AllocL();
    96 	// Save this for base class writing functions.
    97 	iContentsLengthDER = iContents->Length();
    98 	// This is base class' method which initializes length of length 
    99 	// encoding for proper DER writing.
   100 	CalculateLengthLengthDER();
   101 	}
   102 
   103 
   104 // This method is not called but is necessary because it overrides a 
   105 // pure virtual function of the base class. The variable is properly 
   106 // initialized in ConstructL().
   107 void CASN1EncEncoding::CalculateContentsLengthDER()
   108 	{
   109 	iContentsLengthDER = iContents->Length();
   110 	}
   111 
   112 TBool CASN1EncEncoding::IsConstructed() const
   113 	{
   114 	return ETrue;
   115 	}
   116 
   117 // When this method is called by the base class write helper, the tag 
   118 // and length are already written.
   119 void CASN1EncEncoding::WriteContentsDERL(TDes8& aBuf) const
   120 	{
   121 	aBuf.Copy(*iContents);
   122 	}