javaextensions/satsa/apdu/src.s60/cstsasn1sequence.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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 "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 // INCLUDE FILES
       
    20 #include "cstsasn1sequence.h"
       
    21 
       
    22 namespace java
       
    23 {
       
    24 namespace satsa
       
    25 {
       
    26 
       
    27 // CONSTANTS
       
    28 const TInt KSTSDefaultGranularity = 5;
       
    29 const TUint8 KSTSPaddingByte = 0xFF;
       
    30 const TUint8 KSTSRemovedRecordTag = 0x00;
       
    31 const TInt KSTSRecordHeaderLength = 2;
       
    32 
       
    33 // ============================ MEMBER FUNCTIONS ===============================
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CSTSASN1Sequence::CSTSASN1Sequence
       
    37 // C++ default constructor can NOT contain any code, that
       
    38 // might leave.
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CSTSASN1Sequence::CSTSASN1Sequence()
       
    42 {
       
    43 }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CSTSASN1Sequence::DecodeSequenceLC
       
    47 // Decodes raw-data to ASN.1 modules, pushes items to cleanupstack
       
    48 // -----------------------------------------------------------------------------
       
    49 
       
    50 CArrayPtr<TASN1DecGeneric>* CSTSASN1Sequence::DecodeSequenceLC(TBool aCheckTag,
       
    51         const TDesC8& aRawData)
       
    52 {
       
    53     CArrayPtr<TASN1DecGeneric>* items = NULL;
       
    54 
       
    55     // Check we've got a sequence
       
    56     TASN1DecGeneric decGen(aRawData);
       
    57     decGen.InitL();
       
    58     TTagType tag = decGen.Tag();
       
    59     // Accept only sequences or set if tag must be checked
       
    60     if ((tag != EASN1Sequence) && (tag != EASN1Set) && (aCheckTag))
       
    61     {
       
    62         User::Leave(KErrArgument);
       
    63     }
       
    64     else
       
    65     {
       
    66         // Decode sequences to array
       
    67         items = DecodeItemsLC(decGen);
       
    68     }
       
    69     return items;
       
    70 }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CSTSASN1Sequence::DecodeSequenceLC
       
    74 // Decodes raw-data to ASN.1 modules, pushes items to cleanupstack
       
    75 // Validates that there is right number of modules, leaves with KErrArgument if
       
    76 // not.
       
    77 // -----------------------------------------------------------------------------
       
    78 
       
    79 CArrayPtr<TASN1DecGeneric>* CSTSASN1Sequence::DecodeSequenceLC(TBool aCheckTag,
       
    80         const TDesC8& aRawData, TInt aMinTerms, TInt aMaxTerms)
       
    81 {
       
    82     CArrayPtr<TASN1DecGeneric>* items = DecodeSequenceLC(aCheckTag, aRawData);
       
    83     TInt count = items->Count();
       
    84     if ((count < aMinTerms) || (count > aMaxTerms))
       
    85     {
       
    86         // not in the range, leave
       
    87         User::Leave(KErrArgument);
       
    88     }
       
    89 
       
    90     return items;
       
    91 }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CSTSASN1Sequence::DecodeSequenceLC
       
    95 // Decodes raw-data to ASN.1 modules, pushes items to cleanupstack
       
    96 // Validates that there is right number of modules, leaves with KErrArgument if
       
    97 // not.
       
    98 // -----------------------------------------------------------------------------
       
    99 
       
   100 CArrayPtr<TASN1DecGeneric>* CSTSASN1Sequence::DecodeSequenceLC(TBool aCheckTag,
       
   101         const TDesC8& aRawData, TInt aMinTerms)
       
   102 {
       
   103     CArrayPtr<TASN1DecGeneric>* items = DecodeSequenceLC(aCheckTag, aRawData);
       
   104     TInt count = items->Count();
       
   105     if (count < aMinTerms)
       
   106     {
       
   107         // not in the range, leave
       
   108         User::Leave(KErrArgument);
       
   109     }
       
   110 
       
   111     return items;
       
   112 }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CSTSASN1Sequence::DecodeItemsLC
       
   116 // Decodes raw-data to ASN.1 modules, pushes items to cleanupstack
       
   117 // Checks removed records and padding bytes and ignores them.
       
   118 // -----------------------------------------------------------------------------
       
   119 CArrayPtrFlat<TASN1DecGeneric>* CSTSASN1Sequence::DecodeItemsLC(
       
   120     const TASN1DecGeneric& aRawData)
       
   121 {
       
   122     CArrayPtrFlat<TASN1DecGeneric>* items = new(ELeave) CArrayPtrFlat<
       
   123     TASN1DecGeneric> (KSTSDefaultGranularity);
       
   124     TCleanupItem cleanupSeq(CleanupSequence, items);
       
   125     CleanupStack::PushL(cleanupSeq);
       
   126 
       
   127     TPtrC8 source(aRawData.GetContentDER());
       
   128     TInt sourceLength = source.Length();
       
   129 
       
   130     TInt pos = 0;
       
   131     while (pos < sourceLength)
       
   132     {
       
   133         TPtrC8 restOfData(source.Right(sourceLength - pos));
       
   134         TUint8 tag = restOfData[0];//first byte is tag
       
   135 
       
   136         TInt restOfDataLength = restOfData.Length();
       
   137         //check does contains removed record or padding bytes
       
   138         if (tag == KSTSRemovedRecordTag)
       
   139         {
       
   140             //we can jump over this record
       
   141             if (restOfDataLength >= KSTSRecordHeaderLength)
       
   142             {
       
   143                 //2. byte is record length
       
   144                 TInt recordDataLength = restOfData[1];
       
   145                 //update position after this record
       
   146                 pos += recordDataLength + KSTSRecordHeaderLength;
       
   147             }
       
   148             else
       
   149             {
       
   150                 //there is only one byte left so in this Removed Tag case
       
   151                 //we can ignore this and continue without leaving
       
   152                 pos++;
       
   153             }
       
   154         }
       
   155         else if (tag == KSTSPaddingByte)
       
   156         {
       
   157             //we can jump over padding bytes
       
   158             TInt amountOfPaddingBytes = 1; //one padding byte is already found
       
   159             TBool paddingByte = ETrue;
       
   160 
       
   161             //we start from second byte, because first is already read
       
   162             for (TInt i = 1; (i < restOfDataLength) && (paddingByte); i++)
       
   163             {
       
   164                 //take next value
       
   165                 TUint8 nextByte = restOfData[i];
       
   166                 //if padding byte, increase amount of padding bytes
       
   167                 if (nextByte == KSTSPaddingByte)
       
   168                 {
       
   169                     amountOfPaddingBytes++;
       
   170                 }
       
   171                 else //end loop
       
   172                 {
       
   173                     paddingByte = EFalse;
       
   174                 }
       
   175             }
       
   176             //update position after padding bytes
       
   177             pos += amountOfPaddingBytes;
       
   178         }
       
   179         else //we can append record to array
       
   180         {
       
   181             //take next record
       
   182             TASN1DecGeneric* decGenRecord =
       
   183                 new(ELeave) TASN1DecGeneric(restOfData);
       
   184             CleanupStack::PushL(decGenRecord);
       
   185             decGenRecord->InitL();
       
   186             //append record to array
       
   187             items->AppendL(decGenRecord);
       
   188             //update position after record
       
   189             pos += decGenRecord->LengthDER();
       
   190             CleanupStack::Pop(decGenRecord);
       
   191         }
       
   192     }
       
   193 
       
   194     return items;
       
   195 
       
   196 }
       
   197 
       
   198 // -----------------------------------------------------------------------------
       
   199 // CSTSASN1Sequence::CleanupSequence
       
   200 //
       
   201 // -----------------------------------------------------------------------------
       
   202 void CSTSASN1Sequence::CleanupSequence(TAny* aArray)
       
   203 {
       
   204     CArrayPtrFlat< TASN1DecGeneric>* array =
       
   205         REINTERPRET_CAST(CArrayPtrFlat< TASN1DecGeneric>*, aArray);
       
   206     array->ResetAndDestroy();
       
   207     delete array;
       
   208 }
       
   209 
       
   210 } // namespace satsa
       
   211 } // namespace java
       
   212 //  End of File