pkiutilities/PKCS12/CrBer/Src/crber.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2000, 2004 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:   This module contains the implementation of CCrBer class. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 //  INCLUDE FILES
       
    21 #include "crber.h"
       
    22 #include <e32math.h>    // Pow
       
    23 
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CCrBer
       
    27 // Constructor 
       
    28 // This function constructs CCrBer object
       
    29 // -----------------------------------------------------------------------------
       
    30 CCrBer::CCrBer(TInt aLevel /* = 0 */)
       
    31     {
       
    32     TReal size = 0;
       
    33     
       
    34     Math::Pow(size, 256, sizeof(TUint));
       
    35     iMaxUint = STATIC_CAST(TUint, size - 1);
       
    36     
       
    37     Math::Pow(size, 128, sizeof(TInt));
       
    38     iMaxInt = STATIC_CAST(TInt, size - 1);
       
    39     
       
    40     
       
    41     iEOCBytes[0] = 0x00;
       
    42     iEOCBytes[1] = 0x00;
       
    43 
       
    44     iType = KBerUnknown;
       
    45     
       
    46     iIndefinite = EFalse;
       
    47 
       
    48     iObjectBegin = 0;
       
    49     iContentBegin = 0;
       
    50     iContentLen = 0;
       
    51     iObjectLen = 0;
       
    52 
       
    53     iLevel = aLevel;
       
    54 
       
    55     iData = NULL;
       
    56     }
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CCrBer
       
    60 // Destructor 
       
    61 // This function destructs CCrBer object
       
    62 // -----------------------------------------------------------------------------
       
    63 EXPORT_C CCrBer::~CCrBer()
       
    64     {
       
    65     iData = NULL;
       
    66 	iInt.Close();
       
    67     }
       
    68 
       
    69 // -----------------------------------------------------------------------------
       
    70 // CCrBer::ConstructL
       
    71 // This function initializes CCrBer object's member objects.
       
    72 // -----------------------------------------------------------------------------
       
    73 TAny CCrBer::ConstructL()
       
    74     {
       
    75     }
       
    76 // -----------------------------------------------------------------------------
       
    77 // CCrBer::NewLC
       
    78 // -----------------------------------------------------------------------------
       
    79 EXPORT_C CCrBer* CCrBer::NewLC(TInt aLevel /* = 0 */)
       
    80     {
       
    81     CCrBer* self = new (ELeave) CCrBer(aLevel);
       
    82     CleanupStack::PushL(self);
       
    83 
       
    84     self->ConstructL();
       
    85 
       
    86     return self; 
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CCrBer::NewL
       
    91 // -----------------------------------------------------------------------------
       
    92 EXPORT_C CCrBer* CCrBer::NewL(TInt aLevel /* = 0 */)
       
    93     {
       
    94     CCrBer* self = NewLC(aLevel);
       
    95     CleanupStack::Pop();
       
    96 
       
    97     return self; 
       
    98     }
       
    99 
       
   100 // -----------------------------------------------------------------------------
       
   101 // CCrBer::Read
       
   102 // Read next BER encoded object from the current position of
       
   103 // given CCrData to this object. Return the type of the BER
       
   104 // object, or KBerUnknown, if not of any known type. Only pointer
       
   105 // to aData is stored into this object, so Get* functions are
       
   106 // meaningful only if original data object is still existing,
       
   107 // when these functions are used.
       
   108 // -----------------------------------------------------------------------------
       
   109 EXPORT_C TUint8 CCrBer::Read(CCrData* aData)
       
   110     {
       
   111     if ((iType = ReadType(aData)) != KBerUnknown)
       
   112         {
       
   113         TInt  move;
       
   114         TInt  pos = 0;
       
   115         TBool conLenKnown;
       
   116 
       
   117 
       
   118         // If type is known, this object begins right here,
       
   119         // well, to be exact, one byte before this point.
       
   120         if ((aData->Seek(ESeekCurrent, pos)) == KErrNone)
       
   121             {
       
   122             conLenKnown = ETrue;
       
   123 
       
   124             // Now we have at least something sensible. Store pointer to
       
   125             // the data object (and hope that the caller doesn't delete it :)
       
   126             iData = aData;
       
   127 
       
   128             iObjectBegin = pos - 1;
       
   129 
       
   130             // Read length.
       
   131             iContentLen = ReadLen(aData, iIndefinite, &iLenLen);
       
   132 
       
   133             // Content begins right after type tag and length bytes.
       
   134             iContentBegin = pos + iLenLen;
       
   135 
       
   136             // If length is indefinite and type isn't set, sequence,
       
   137             // or explicit/implicit constructed, find next end-of-content
       
   138             // tag to define exact length.
       
   139             if (iIndefinite)
       
   140                 {
       
   141                 if ((iType != KBerSeq) &&
       
   142                     (iType != KBerSet) &&
       
   143                     (iType != KBerImplicitConstructed) &&
       
   144                     (iType != KBerExplicitConstructed))
       
   145                     {
       
   146                     // Give special treatment to constructed encoding.
       
   147                     if ((iType & KBerConstructedBit) ||
       
   148                         (iType & KBerImplicitConstructed))
       
   149                         {
       
   150                         iContentLen = OpenConstructedEncodingWithTagL(
       
   151                                         aData, *this);
       
   152                         
       
   153                         }
       
   154                     else
       
   155                         {
       
   156                         iContentLen = FindEndOfContent(aData);
       
   157                         }
       
   158                     }
       
   159                 else
       
   160                     {
       
   161                     // We really can't know what the length is,
       
   162                     // until we open up the whole inner content.
       
   163                     conLenKnown = EFalse;
       
   164                     }
       
   165                 }
       
   166 
       
   167             // Now, if we know content length, then we can calculate whole
       
   168             // object's length; it is tag + length bytes + content length.
       
   169             if (conLenKnown)
       
   170                 {
       
   171                 SetObjectLen();
       
   172 
       
   173                 // Also move data pointer at the end of this object, except
       
   174                 // if object was set or sequence, in which case don't move,
       
   175                 // because we still have to open inner items.
       
   176                 // At indefinite case this is already done (other than
       
   177                 // sequence or set) or this can't be done (sequence or
       
   178                 // set). So, if not indefinite and not sequence or set,
       
   179                 // move data pointer the amount of content length.
       
   180                 if (!(iIndefinite) && !(IsSeqOrSet(iType)))
       
   181                     {
       
   182                     move = iContentLen;
       
   183 
       
   184                     aData->Seek(ESeekCurrent, move);
       
   185                     }
       
   186                 }
       
   187 
       
   188             // If tag was end-of-content tag, check that also length
       
   189             // was 0 and not indefinite, this guarantees that we really
       
   190             // have an end-of-content tag (00 00).
       
   191             if (iType == KBerEndOfContent)
       
   192                 {
       
   193                 if ((iContentLen != 0) || (iIndefinite))
       
   194                     {
       
   195                     iType = KBerUnknown;
       
   196                     }
       
   197                 }
       
   198             }
       
   199         
       
   200 	
       
   201 	 
       
   202 		}
       
   203     return iType;
       
   204     }
       
   205 
       
   206 
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CCrBer::ReadType
       
   210 // Read type tag from current position in given CCrData.
       
   211 // Returns type tag or KBerUnknown, if not of any known type.
       
   212 // -----------------------------------------------------------------------------
       
   213 EXPORT_C TUint8 CCrBer::ReadType(CCrData* aData)
       
   214     {
       
   215     TBuf8<1> byte;
       
   216     TUint8   type = KBerUnknown;   // By default we are pessimists.
       
   217 
       
   218     // Read type.
       
   219     if ((aData != 0) &&
       
   220         (aData->Read(byte, 1) == KErrNone) &&
       
   221         (byte.Length() > 0))
       
   222         {
       
   223         type = byte[0];
       
   224 
       
   225         if (!IsKnownType(type))
       
   226             {
       
   227             // Don't let type to be whatever byte happened to be
       
   228             // at that place. Set it to unknown, which we all know.
       
   229             type = KBerUnknown;
       
   230             }
       
   231         }
       
   232 
       
   233     return type;
       
   234     }
       
   235 
       
   236 // -----------------------------------------------------------------------------
       
   237 // CCrBer::ReadLen
       
   238 // Read length tags from current position in given CCrData.
       
   239 // Returns length. If length is indefinite, aIndefinite is set
       
   240 // to true, otherwise to false. In indefinite case length is 0.
       
   241 // Also sets amount of length bytes in aLenLen, if given.
       
   242 // -----------------------------------------------------------------------------
       
   243 EXPORT_C TUint CCrBer::ReadLen(
       
   244     CCrData* aData,
       
   245     TBool&   aIndefinite,
       
   246     TUint8*  aLenLen/* = 0 */)
       
   247     {
       
   248     TBuf8<LEN_BYTES_MAX> bytes;
       
   249     TUint8               byte = 0;
       
   250     TUint                len = 0;
       
   251 
       
   252     // Originally assume that length is definite,
       
   253     aIndefinite = EFalse;
       
   254 
       
   255     // First byte tells if the length bytes are in short or in long form.
       
   256     if ((aData != 0) && (aData->Read(bytes, 1) == KErrNone))
       
   257         {
       
   258         if (bytes.Length() <= 0)        
       
   259             {
       
   260             User::Leave(KErrArgument);
       
   261             }
       
   262         byte = bytes[0];
       
   263 
       
   264         // We have at least one length byte.
       
   265         if (aLenLen != 0)
       
   266             {
       
   267             *aLenLen = 1;
       
   268             }
       
   269 
       
   270         if (byte & KBerLongLengthBit)
       
   271             {
       
   272             // We have length bytes, but how many?
       
   273             TUint8 lenBytes = (TUint8)(byte & ~KBerLongLengthBit);
       
   274 
       
   275             // If length is in long form, but there are zero
       
   276             // length bytes, then length is indefinite.
       
   277             if (lenBytes == 0)
       
   278                 {
       
   279                 aIndefinite = ETrue;
       
   280                 }
       
   281             else
       
   282                 {
       
   283                 if (aLenLen != 0)
       
   284                     {
       
   285                     // Add amount of actual length bytes to one,
       
   286                     // which was for long length form byte.
       
   287                     for (TInt i = 0; i < lenBytes; i++)
       
   288                         {
       
   289                         (*aLenLen)++;
       
   290                         }
       
   291 					}
       
   292 
       
   293                 // Otherwise we have to interpret length bytes and
       
   294                 // move the result into len variable. First check
       
   295                 // that there aren't more bytes than fits to
       
   296                 // unsigned integer, we don't want any troubles here.
       
   297                 if (lenBytes > sizeof(TUint))
       
   298                     {
       
   299                     // Return max uint, caller can then decide
       
   300                     // what to do with this huge pile of...
       
   301                     return iMaxUint;
       
   302                     }
       
   303 
       
   304                 // Read length bytes
       
   305                 if (aData->Read(bytes, lenBytes) == KErrNone)
       
   306                     {
       
   307                     TUint8 i;
       
   308 
       
   309                     // and move them to len.
       
   310                     for (i = 0; i < lenBytes; i++)
       
   311                         {
       
   312                         len = len << 8;
       
   313 
       
   314                         len += bytes[i];
       
   315                         }
       
   316                     }
       
   317                 }
       
   318             }
       
   319         else
       
   320             {
       
   321             // Otherwise length was in short form and length
       
   322             // byte alone tells us the length of the contens.
       
   323             len = byte;
       
   324             }
       
   325         }
       
   326 
       
   327     return len;
       
   328     }
       
   329 
       
   330 
       
   331 // -----------------------------------------------------------------------------
       
   332 // CCrBer::IsKnownType
       
   333 // Returns true if given BER tag is identified one.
       
   334 // Returns:  ETrue of EFalse.
       
   335 // -----------------------------------------------------------------------------
       
   336 EXPORT_C TBool CCrBer::IsKnownType(TBerTag aTag)
       
   337     {
       
   338     TBool isKnown = EFalse;
       
   339 
       
   340     // Remove constructed bit, if it is on without other upper byte tags.
       
   341     if ((aTag & 0xF0) == KBerConstructedBit)
       
   342         {
       
   343         aTag &= ~KBerConstructedBit;
       
   344         }
       
   345 
       
   346     // Remove implicit/explicit constructed bit, 
       
   347     // if it is on without other upper byte tags.
       
   348     if ((aTag & 0xF0) == KBerImplicitConstructed)
       
   349         {
       
   350         aTag &= ~KBerImplicitConstructed;
       
   351         }
       
   352 
       
   353     switch (aTag)
       
   354         {
       
   355         case KBerEndOfContent:
       
   356         case KBerBoolean:
       
   357         case KBerInteger:
       
   358         case KBerBitString:
       
   359         case KBerOctetString:
       
   360         case KBerNull:
       
   361         case KBerOid:
       
   362 		case KBerNumS:
       
   363         case KBerPrS:
       
   364         case KBerT61S:
       
   365 		case KBerVideoS:
       
   366         case KBerIA5S:
       
   367         case KBerUtc:
       
   368 		case KBerGenTime:                                
       
   369 		case KBerGraphS: 
       
   370 		case KBerVisibleS:
       
   371 		case KBerGeneralS:
       
   372         case KBerBmpS:
       
   373         case KBerSeq:
       
   374         case KBerSet:
       
   375         case KBerImplicitConstructed:
       
   376         // Same as KBerExplicitConstructed
       
   377         case KBerImplicit:
       
   378         // Same as KBerExplicit
       
   379             {
       
   380             isKnown = ETrue;
       
   381             break;
       
   382             }
       
   383         default:
       
   384             {
       
   385             isKnown = EFalse;
       
   386             break;
       
   387             }
       
   388         }
       
   389 
       
   390     return isKnown;
       
   391     }
       
   392 
       
   393 
       
   394 
       
   395 // -----------------------------------------------------------------------------
       
   396 // CCrBer::IsSeqOrSet
       
   397 // Returns true if given BER tag is set, sequence,
       
   398 // implicit contructed, or explicit constructed tag.
       
   399 // -----------------------------------------------------------------------------
       
   400 EXPORT_C TBool CCrBer::IsSeqOrSet(TBerTag aTag)
       
   401     {
       
   402     TBool isSeqOrSet = EFalse;
       
   403 
       
   404     if ((aTag == KBerSeq) ||
       
   405         (aTag == KBerSet) ||
       
   406         (aTag == KBerImplicit) ||
       
   407         (aTag == KBerExplicit) ||
       
   408         (aTag == KBerImplicitConstructed) ||
       
   409         (aTag == KBerExplicitConstructed))
       
   410 
       
   411         {
       
   412         isSeqOrSet = ETrue;
       
   413         }
       
   414 
       
   415     return isSeqOrSet;
       
   416     }
       
   417 
       
   418 // -----------------------------------------------------------------------------
       
   419 // CCrBer::FindEndOfContent
       
   420 // Finds next end-of-content (00 00) tag from given data.
       
   421 // Returns the distance between current place and the tag,
       
   422 // or 0, if not found. Moves data pointer to the next byte
       
   423 // _after_ the tag, i.e. two bytes longer than you might
       
   424 // except from the return value.
       
   425 // -----------------------------------------------------------------------------
       
   426 EXPORT_C TUint CCrBer::FindEndOfContent(CCrData* aData)
       
   427     {
       
   428     TBool go = ETrue;
       
   429     TInt bufferOffset = 0;
       
   430     TInt dataOffset = 0;
       
   431     TInt move = 0;
       
   432 
       
   433     TBuf8<KReadBufMax> bytes;
       
   434 
       
   435     if (aData == 0)
       
   436         {
       
   437         return dataOffset;
       
   438         }
       
   439 
       
   440     // Read buffer of data. Go on, if there is something to go with.
       
   441     while ((aData->Read(bytes, KReadBufMax) == KErrNone) &&
       
   442            (bytes.Length() != 0) && go)
       
   443         {
       
   444         TBuf8<2> EOCBytes;
       
   445 
       
   446         EOCBytes.Append(iEOCBytes[0]);
       
   447         EOCBytes.Append(iEOCBytes[1]);
       
   448 
       
   449         // Try to find end-of-content tag.
       
   450         bufferOffset = bytes.Find(EOCBytes);
       
   451 
       
   452         // If found, move data pointer right _after_
       
   453         // the place of founded tag, and break.
       
   454         if (bufferOffset != KErrNotFound)
       
   455             {
       
   456             move = bufferOffset -
       
   457                        bytes.Length() +
       
   458                        EOCBytes.Length();
       
   459 
       
   460             aData->Seek(ESeekCurrent, move);
       
   461 
       
   462             dataOffset += bufferOffset;
       
   463 
       
   464             go = EFalse;                    //break;
       
   465             }
       
   466         else
       
   467             {
       
   468             // Otherwise keep searching. First check that the last
       
   469             // character of the buffer isn't 00. If it is, move the
       
   470             // data pointer one byte backwards to prevent us from
       
   471             // missing end-of-content tag, if it is divided between
       
   472             // buffers.
       
   473             if (bytes[bytes.Length() - 1] == KBerEndOfContent)
       
   474                 {
       
   475                 move = -1;
       
   476 
       
   477                 aData->Seek(ESeekCurrent, move);
       
   478 
       
   479                 dataOffset--;
       
   480                 }
       
   481 
       
   482             dataOffset += bytes.Length();
       
   483             }
       
   484         }
       
   485 
       
   486     // If nothing was found, move data pointer back
       
   487     // to where it was and return 0.
       
   488     if (bytes.Length() == 0)
       
   489         {
       
   490         dataOffset = -dataOffset;
       
   491 
       
   492         aData->Seek(ESeekCurrent, dataOffset);
       
   493 
       
   494         dataOffset = 0;
       
   495         }
       
   496 
       
   497     return dataOffset;
       
   498     }
       
   499 
       
   500 // -----------------------------------------------------------------------------
       
   501 // CCrBer::OpenConstructedEncodingL
       
   502 // Open constructed encoding from this object.  
       
   503 // Parameters: aTarget; target for contentbytes.
       
   504 // Return Values:  Number of objects in this contructed object.
       
   505 // -----------------------------------------------------------------------------
       
   506 EXPORT_C TInt CCrBer::OpenConstructedEncodingL(CCrData& aTarget)
       
   507     {      
       
   508     TInt err = KErrNone;
       
   509     TInt count = 0;
       
   510     TInt offset = iContentBegin;
       
   511     TInt objectNum = 0;
       
   512     TInt type = Type();
       
   513     CCrBer* object = NULL;
       
   514 
       
   515     if (type <= KBerConstructed)        // not valid type
       
   516         {
       
   517         User::Leave(KErrArgument);
       
   518         }
       
   519     else  
       
   520         {
       
   521         type -= KBerConstructed;        // type what we are looking for
       
   522         }
       
   523 
       
   524     CCrBerSet* newSet = CCrBerSet::NewL(1);
       
   525     CleanupStack::PushL(newSet);
       
   526     iData->Seek(ESeekStart, offset);    // seek right place; content begin
       
   527     newSet->OpenL(iData, KOpenAllLevels);
       
   528     count = newSet->Count();            
       
   529 
       
   530     HBufC8* obj = NULL;                 // buffer for CCrBer-objects
       
   531 
       
   532     for (objectNum = 0; (objectNum < count); objectNum++)
       
   533         {
       
   534         object = newSet->At(objectNum); // select object
       
   535         
       
   536         if (object->Type() == type)
       
   537             {
       
   538             obj = HBufC8::NewLC(object->ContentLen());
       
   539             TPtr8 pObj  = obj->Des();
       
   540             
       
   541             err = iData->Read(object->ContentBegin(), pObj, object->ContentLen());
       
   542             
       
   543             if (err < KErrNone)         // if error -> leave
       
   544                 {
       
   545                 CleanupStack::PopAndDestroy(2);  // delete newSet, obj;
       
   546                 User::Leave(err);
       
   547                 }    
       
   548             err = aTarget.Write(pObj);  // write content to CCrData  
       
   549             
       
   550             CleanupStack::PopAndDestroy();  // delete obj;
       
   551             obj = NULL;
       
   552             
       
   553             if (err < KErrNone)         // if error -> leave
       
   554                 {
       
   555                 CleanupStack::PopAndDestroy();  // delete newSet;
       
   556                 User::Leave(err);
       
   557                 }
       
   558             
       
   559             }
       
   560         else 
       
   561             {
       
   562             count = objectNum;
       
   563             }
       
   564         }
       
   565     CleanupStack::PopAndDestroy();      // delete newSet;
       
   566 
       
   567     return count;
       
   568     }
       
   569 
       
   570 // -----------------------------------------------------------------------------
       
   571 // CCrBer::OpenConstructedEncodingWithTagL
       
   572 // Open constructed encoding with given tag from given data.
       
   573 // Add all founded octets into the parameter string, if given.
       
   574 // Return amount of bytes read.
       
   575 // -----------------------------------------------------------------------------
       
   576 EXPORT_C TUint CCrBer::OpenConstructedEncodingWithTagL(
       
   577     CCrData* aData,
       
   578     CCrBer&  parentObj,
       
   579     HBufC8*  aBuf /* = 0 */)
       
   580     {
       
   581     TInt    read = 0;
       
   582     CCrBer  temp;
       
   583     TBerTag parentTag = KBerUnknown;
       
   584     TBerTag tempTag   = KBerUnknown;
       
   585     HBufC8* tempBuf   = NULL;
       
   586 
       
   587     parentTag = parentObj.Type();
       
   588 
       
   589     // Check given object. It should have constructed tag.
       
   590     if (parentTag & KBerConstructedBit)
       
   591         {
       
   592         TUint len = 0;
       
   593         TUint totalLen = iMaxUint;
       
   594         TBool isIndefinite = parentObj.Indefinite();
       
   595 
       
   596         TBufC8<4> buf = _L8("Test");
       
   597         TPtr8     ptr = buf.Des();
       
   598 
       
   599         if (aBuf != 0)
       
   600             {
       
   601             ptr = aBuf->Des();
       
   602             }
       
   603 
       
   604         tempBuf = HBufC8::NewLC(KReadBufMax);
       
   605 
       
   606         // After that we know that object either contains length or
       
   607         // it is indefinite. Set max int to len even in the case of
       
   608         // indefinite length to prevent this loop from going on forever.
       
   609         if (!isIndefinite)
       
   610             {
       
   611             totalLen = parentObj.ContentLen();
       
   612             }
       
   613 
       
   614         while (len < totalLen)
       
   615             {
       
   616             // Read objects one by one, calculate content lengths together,
       
   617             // add contents to target buffer, if given, and calculate also
       
   618             // total object lengths together to know when stop. In indefinite
       
   619             // case stop when end-of-content tag found.
       
   620             temp.Read(aData);
       
   621 
       
   622             tempTag = temp.Type();
       
   623 
       
   624             if ((tempTag == KBerEndOfContent) ||
       
   625                 (tempTag == KBerUnknown) ||
       
   626                 (!(tempTag & parentTag)))
       
   627                 {
       
   628                 CleanupStack::PopAndDestroy();  // delete tempBuf;
       
   629                 return read;                // break
       
   630                 }
       
   631 
       
   632             len += temp.ObjectLen();
       
   633             read += temp.ContentLen();
       
   634 
       
   635             if (aBuf != 0)
       
   636                 {
       
   637                 // Resize buffer, if needed, and append data to it.
       
   638                 if (aBuf->Length() < read)
       
   639                     {
       
   640                     // It is impossible to know how long buffer should be,
       
   641                     // but add some extra space, so reallocation won't occur
       
   642                     // each time.
       
   643                     aBuf->ReAllocL(read + (read / 5));
       
   644                     ptr = aBuf->Des();
       
   645                     }
       
   646 
       
   647                 temp.Content(tempBuf);
       
   648                 ptr.Append(*tempBuf);
       
   649                 }
       
   650             }
       
   651 
       
   652         CleanupStack::PopAndDestroy();      // delete tempBuf;
       
   653         }
       
   654 
       
   655     return read;
       
   656     }
       
   657 
       
   658 
       
   659 
       
   660 // -----------------------------------------------------------------------------
       
   661 // CCrBer::Type
       
   662 // Get type of the object.
       
   663 // -----------------------------------------------------------------------------
       
   664 EXPORT_C TBerTag CCrBer::Type()
       
   665     {
       
   666 	return iType;
       
   667     }
       
   668 
       
   669 // -----------------------------------------------------------------------------
       
   670 // CCrBer::Value
       
   671 // Get value of the object. Used in encoding.
       
   672 // -----------------------------------------------------------------------------
       
   673 TInt CCrBer::Value()
       
   674     {
       
   675     return iValue;
       
   676     }
       
   677 // -----------------------------------------------------------------------------
       
   678 // CCrBer::GetBigInt
       
   679 // -----------------------------------------------------------------------------
       
   680 RInteger CCrBer::GetBigInt()
       
   681     {
       
   682     return iInt;
       
   683     }
       
   684 
       
   685 // -----------------------------------------------------------------------------
       
   686 // CCrBer::SetValue
       
   687 // Set value of the object.
       
   688 // -----------------------------------------------------------------------------
       
   689 TAny CCrBer::SetValue(TInt aInt)
       
   690     {
       
   691     iValue = aInt;
       
   692     }
       
   693 
       
   694 // -----------------------------------------------------------------------------
       
   695 // CCrBer::ValuePtr
       
   696 // Get valuePtr of the object. (encoding)
       
   697 // -----------------------------------------------------------------------------
       
   698 TDesC8* CCrBer::ValuePtr()
       
   699     {
       
   700     return iValuePtr;
       
   701     }
       
   702 
       
   703 // -----------------------------------------------------------------------------
       
   704 // CCrBer::LenLen
       
   705 // Get amount of length bytes.
       
   706 // -----------------------------------------------------------------------------
       
   707 EXPORT_C TInt CCrBer::LenLen()
       
   708     {
       
   709     return iLenLen;
       
   710     }
       
   711 
       
   712 // -----------------------------------------------------------------------------
       
   713 // CCrBer::ObjectBegin
       
   714 // Return begin of the whole object.
       
   715 // -----------------------------------------------------------------------------
       
   716 EXPORT_C TInt CCrBer::ObjectBegin()
       
   717     {
       
   718     return iObjectBegin;
       
   719     }
       
   720 
       
   721 // -----------------------------------------------------------------------------
       
   722 // CCrBer::ContentBegin
       
   723 // Return begin of the content.
       
   724 // -----------------------------------------------------------------------------
       
   725 EXPORT_C TInt CCrBer::ContentBegin()
       
   726     {
       
   727     return iContentBegin;
       
   728     }
       
   729 
       
   730 // -----------------------------------------------------------------------------
       
   731 // CCrBer::ObjectLen
       
   732 // Get length of the whole object.
       
   733 // -----------------------------------------------------------------------------
       
   734 EXPORT_C TInt CCrBer::ObjectLen()
       
   735     {
       
   736 	return iObjectLen;
       
   737 	}
       
   738 
       
   739 // -----------------------------------------------------------------------------
       
   740 // CCrBer::ContentLen
       
   741 // Get length of the content.
       
   742 // -----------------------------------------------------------------------------
       
   743 EXPORT_C TInt CCrBer::ContentLen()
       
   744     {
       
   745     return iContentLen;
       
   746     }
       
   747 
       
   748 // -----------------------------------------------------------------------------
       
   749 // CCrBer::Indefinite
       
   750 // Returns true if this object is indefinite length.
       
   751 // -----------------------------------------------------------------------------
       
   752 EXPORT_C TBool CCrBer::Indefinite()
       
   753     {
       
   754     return iIndefinite;
       
   755     }
       
   756 
       
   757 // -----------------------------------------------------------------------------
       
   758 // CCrBer::Data
       
   759 // Return pointer to data object.
       
   760 // -----------------------------------------------------------------------------
       
   761 EXPORT_C CCrData* CCrBer::Data()
       
   762     {
       
   763     return iData;
       
   764     }
       
   765 
       
   766 // -----------------------------------------------------------------------------
       
   767 // CCrBer::Object
       
   768 // Return whole BER object with tag and stuff. For implementation
       
   769 // reasons ObjectL returns only max 255 bytes of data.
       
   770 // -----------------------------------------------------------------------------
       
   771 
       
   772 EXPORT_C TInt CCrBer::Object(HBufC8* aBuf)
       
   773     {
       
   774     return BufferL(aBuf, iData, iObjectBegin, iObjectLen);
       
   775     }
       
   776 
       
   777 // -----------------------------------------------------------------------------
       
   778 // CCrBer::Object
       
   779 // Return whole BER object with tag and stuff. For implementation
       
   780 // reasons ObjectL returns only max 255 bytes of data.
       
   781 // -----------------------------------------------------------------------------
       
   782 EXPORT_C TInt CCrBer::ObjectL(HBufC* aBuf)
       
   783     {
       
   784     TUint8 len = 255;
       
   785 
       
   786 
       
   787     if (iObjectLen < 255)
       
   788         {
       
   789         len = (TUint8)iObjectLen;
       
   790         }
       
   791 	
       
   792     return BufferL(aBuf, iData, iObjectBegin, len);
       
   793     }
       
   794 
       
   795 // -----------------------------------------------------------------------------
       
   796 // CCrBer::Content
       
   797 // Return content of this object. For implementation reasons
       
   798 // ContentL returns only max 255 bytes of data.
       
   799 // -----------------------------------------------------------------------------
       
   800 EXPORT_C TInt CCrBer::Content(HBufC8* aBuf)
       
   801     {
       
   802     return BufferL(aBuf, iData, iContentBegin, iContentLen);
       
   803     }
       
   804 
       
   805 // -----------------------------------------------------------------------------
       
   806 // CCrBer::ContentL
       
   807 // -----------------------------------------------------------------------------
       
   808 EXPORT_C TInt CCrBer::ContentL(HBufC* aBuf)
       
   809     {
       
   810     TUint8 len = 255;
       
   811 
       
   812     if (iContentLen < 255)
       
   813         {
       
   814         len = (TUint8)iContentLen;
       
   815         }
       
   816 
       
   817     return BufferL(aBuf, iData, iContentBegin, len);
       
   818     }
       
   819 
       
   820 // -----------------------------------------------------------------------------
       
   821 // CCrBer::BufferL
       
   822 // Read aAmount of data starting from begin to the buffer.
       
   823 // Return amount of data read. For implementation reasons
       
   824 // BufferL can be used only to read max 256 bytes of data.
       
   825 // -----------------------------------------------------------------------------
       
   826 EXPORT_C TInt CCrBer::BufferL(
       
   827     HBufC8*   aBuf,
       
   828     CCrData*  aData,
       
   829     TUint     aBegin  /* = 0 */,
       
   830     TUint     aAmount /* = KReadBufMax */)
       
   831     {
       
   832     TInt pos = 0;
       
   833     TInt begin = aBegin;
       
   834     TInt amount = aAmount;
       
   835     TInt read = 0;
       
   836 
       
   837     if ((aBuf == 0) || (aData == 0))
       
   838         {
       
   839         // Don't bother to continue.
       
   840         return read;
       
   841         }
       
   842 
       
   843     // Store current position,
       
   844     if ((aData->Seek(ESeekCurrent, pos)) != KErrNone)
       
   845         {
       
   846         // Don't bother to continue.
       
   847         return read;
       
   848         }
       
   849 
       
   850     // Set data pointer to wanted position.
       
   851     if (aData->Seek(ESeekStart, begin) == KErrNone)
       
   852         {
       
   853         // Resize buffer, if needed, and read data.
       
   854         if (aBuf->Length() < amount)
       
   855             {
       
   856             aBuf->ReAllocL(amount);
       
   857             }
       
   858 
       
   859         TPtr8 ptr = aBuf->Des();
       
   860 
       
   861         if (aData->Read(ptr, amount) == KErrNone)
       
   862             {
       
   863             read = aBuf->Length();
       
   864             }
       
   865         }
       
   866 
       
   867     // Put data pointer back to previous position.
       
   868     aData->Seek(ESeekStart, pos);
       
   869 
       
   870     return read;
       
   871     }
       
   872 
       
   873 EXPORT_C TInt CCrBer::BufferL(
       
   874     HBufC*   aBuf,
       
   875     CCrData* aData,
       
   876     TUint    aBegin  /* = 0 */,
       
   877     TUint8   aAmount /* = 255 */)
       
   878     {
       
   879     TInt      index = 0;
       
   880     TInt      len = 0;
       
   881     TInt      amount = NULL;
       
   882     TBuf<255> buf;
       
   883     HBufC8*   heapBuf = NULL;
       
   884 
       
   885     heapBuf = HBufC8::NewLC(255);
       
   886 
       
   887     amount = BufferL(heapBuf, aData, aBegin, aAmount);
       
   888 
       
   889     len = heapBuf->Length();
       
   890 
       
   891     for (index = 0; index < len; index++)
       
   892         {
       
   893         buf.Append((*heapBuf)[index]);
       
   894         }
       
   895 
       
   896     *aBuf = buf;
       
   897 
       
   898     CleanupStack::PopAndDestroy();      // delete heapBuf;
       
   899     // heapBuf = NULL;
       
   900 
       
   901     return amount;
       
   902     }
       
   903 
       
   904 
       
   905 // -----------------------------------------------------------------------------
       
   906 // CCrBer::Level
       
   907 // Return nesting level of this object.
       
   908 // -----------------------------------------------------------------------------
       
   909 EXPORT_C TUint CCrBer::Level()
       
   910     {
       
   911     return iLevel;
       
   912     }
       
   913 
       
   914 // -----------------------------------------------------------------------------
       
   915 // CCrBer::SetLevel
       
   916 // Set nesting level of this object.
       
   917 // -----------------------------------------------------------------------------
       
   918 EXPORT_C TUint CCrBer::SetLevel(TUint aLevel)
       
   919     {
       
   920     iLevel = aLevel;
       
   921 
       
   922     return iLevel;
       
   923     }
       
   924 
       
   925 // -----------------------------------------------------------------------------
       
   926 // CCrBer::AddToContentLen
       
   927 // Add given integer to content length of this object.
       
   928 // -----------------------------------------------------------------------------
       
   929 EXPORT_C TAny CCrBer::AddToContentLen(TInt iLen)
       
   930     {
       
   931     iContentLen += iLen;
       
   932     }
       
   933 
       
   934 // -----------------------------------------------------------------------------
       
   935 // CCrBer::SetObjectLen
       
   936 // Calculate object length from tag, length's length, and
       
   937 // content's length. Used to 'close' indefinite objects, meaning
       
   938 // that their end-of-contents tag is finally found, so their
       
   939 // length can be calculated.
       
   940 // -----------------------------------------------------------------------------
       
   941 EXPORT_C TAny CCrBer::SetObjectLen()
       
   942     {
       
   943     // Objects length is tag len (always 1) + length byte
       
   944     // amount + content length.
       
   945     iObjectLen = 1 + iLenLen + iContentLen;
       
   946 
       
   947     // If object was of indefinite length, there are
       
   948     // two additional end-of-contents bytes at the end.
       
   949     if (iIndefinite)
       
   950         {
       
   951         iObjectLen += 2;
       
   952         }
       
   953     }
       
   954 
       
   955 // -----------------------------------------------------------------------------
       
   956 // CCrBer::SetObjLenWithOutContent
       
   957 // Parameters:     Content length.
       
   958 // -----------------------------------------------------------------------------
       
   959 TAny CCrBer::SetObjLenWithOutContent(TUint aContentLen)
       
   960     {
       
   961     iObjectLen  = KBerShortLen;             // Tag + len
       
   962     if (aContentLen >= KBerLongLengthBit)
       
   963         {
       
   964         while (aContentLen > 0)                    
       
   965             {
       
   966             aContentLen >>= 8;              // Next byte
       
   967             iObjectLen++;                 
       
   968             }
       
   969         }
       
   970     }
       
   971 
       
   972 // -----------------------------------------------------------------------------
       
   973 // Get content functions.
       
   974 // -----------------------------------------------------------------------------
       
   975 
       
   976 // -----------------------------------------------------------------------------
       
   977 // CCrBer::GetBooleanL
       
   978 // Returns value of Boolean object.
       
   979 // -----------------------------------------------------------------------------
       
   980 EXPORT_C TBool CCrBer::GetBooleanL()
       
   981     {		
       
   982     TInt err = KErrNone;
       
   983     TASN1DecBoolean dec;                                // ASN1 lib (Symbian)
       
   984     TInt pos    = NULL;
       
   985     TBool ret   = EFalse;
       
   986 
       
   987     HBufC8* buf = HBufC8::NewLC(iObjectLen);            // buffer to object
       
   988 
       
   989     TPtr8 ptr   = buf->Des();
       
   990    
       
   991     err = iData->Read(iObjectBegin,ptr,iObjectLen);     // Read object
       
   992 
       
   993     if (err == KErrNone)
       
   994         {
       
   995         ret = dec.DecodeDERL(ptr, pos); 	        // decode object
       
   996         }
       
   997     else
       
   998         {        
       
   999         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1000         User::Leave(err);
       
  1001         }
       
  1002 
       
  1003     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1004  
       
  1005     return ret;
       
  1006     }	
       
  1007 
       
  1008 // -----------------------------------------------------------------------------
       
  1009 // CCrBer::GetIntegerL
       
  1010 // Returns value of Integer object.
       
  1011 // -----------------------------------------------------------------------------		
       
  1012 EXPORT_C TInt CCrBer::GetIntegerL()
       
  1013     {
       
  1014     TInt err = KErrNone;
       
  1015     TASN1DecInteger dec;                                // ASN1 lib (Symbian)
       
  1016     TInt pos    = NULL;
       
  1017     TInt ret    = NULL;
       
  1018 
       
  1019     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1020 
       
  1021     TPtr8 ptr   = buf->Des();
       
  1022    
       
  1023     err = iData->Read(iObjectBegin,ptr,iObjectLen);     // Read object
       
  1024 
       
  1025     if (err == KErrNone)
       
  1026         {
       
  1027         ret = dec.DecodeDERShortL(ptr, pos);        	// decode object
       
  1028         }
       
  1029     else
       
  1030         {        
       
  1031         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1032         User::Leave(err);
       
  1033         }
       
  1034 
       
  1035     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1036 
       
  1037     return ret;
       
  1038     }
       
  1039 
       
  1040 // -----------------------------------------------------------------------------
       
  1041 // CCrBer::GetIntegerL
       
  1042 // Returns value of Long Integer object.
       
  1043 // -----------------------------------------------------------------------------		
       
  1044 EXPORT_C RInteger CCrBer::GetLongIntegerL()
       
  1045     {	
       
  1046     TInt err    = KErrNone;
       
  1047     TInt pos    = NULL;
       
  1048     TASN1DecInteger dec;                                // ASN1 lib (Symbian)
       
  1049 
       
  1050     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1051 
       
  1052     TPtr8 ptr   = buf->Des();
       
  1053     RInteger value;
       
  1054 
       
  1055     // Read object to buf
       
  1056     err = iData->Read(iObjectBegin,ptr,iObjectLen);
       
  1057 
       
  1058     if (err == KErrNone)
       
  1059         {
       
  1060         value = dec.DecodeDERLongL(ptr, pos);           // decode object
       
  1061         }
       
  1062     else
       
  1063         {        
       
  1064         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1065         User::Leave(err);
       
  1066         }
       
  1067     
       
  1068     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1069 
       
  1070     return value;
       
  1071     }
       
  1072   
       
  1073 // -----------------------------------------------------------------------------
       
  1074 // HBufC* CCrBer::GetOidL
       
  1075 // Returns value of object identifier.
       
  1076 // -----------------------------------------------------------------------------
       
  1077 EXPORT_C HBufC* CCrBer::GetOidL()
       
  1078     {	
       
  1079     TInt err    = KErrNone;
       
  1080     TInt pos    = NULL;
       
  1081     HBufC* ret  = NULL;
       
  1082     TASN1DecObjectIdentifier dec;                       // ASN1 lib (Symbian)
       
  1083 
       
  1084     HBufC8* buf = HBufC8::NewLC(iObjectLen);            // buffer to object
       
  1085 
       
  1086     TPtr8 ptr   = buf->Des();
       
  1087 
       
  1088     // Read object to buf
       
  1089     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1090 
       
  1091     if (err == KErrNone)
       
  1092         {
       
  1093         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1094         }
       
  1095     else
       
  1096         {        
       
  1097         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1098         User::Leave(err);
       
  1099         }
       
  1100 
       
  1101     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1102 
       
  1103     return ret;
       
  1104     }
       
  1105 
       
  1106 // -----------------------------------------------------------------------------
       
  1107 // HBufC8* CCrBer::GetOctetStringL
       
  1108 // Returns value of octet string.
       
  1109 // -----------------------------------------------------------------------------
       
  1110 EXPORT_C HBufC8* CCrBer::GetOctetStringL()
       
  1111     {	
       
  1112     TInt pos = NULL;
       
  1113     HBufC8* ret = NULL;
       
  1114     TInt err = KErrNone;
       
  1115     TASN1DecOctetString dec;                            // ASN1 lib (Symbian)
       
  1116 
       
  1117     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1118 
       
  1119     TPtr8 ptr   = buf->Des();
       
  1120 
       
  1121     // Read object to buf
       
  1122     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1123 
       
  1124     if (err == KErrNone)
       
  1125         {
       
  1126         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1127         }
       
  1128     else
       
  1129         {        
       
  1130         CleanupStack::PopAndDestroy(buf);                  // delete buf;
       
  1131         User::Leave(err);
       
  1132         }
       
  1133 
       
  1134     CleanupStack::PopAndDestroy(buf);                      // delete buf;
       
  1135 
       
  1136     return ret;
       
  1137     }
       
  1138 
       
  1139 // -----------------------------------------------------------------------------
       
  1140 // HBufC8* CCrBer::GetOctetStringL
       
  1141 // Returns value of octet string.
       
  1142 // -----------------------------------------------------------------------------
       
  1143 EXPORT_C TAny CCrBer::GetOctetStringL(CCrData& Trg)
       
  1144     {	
       
  1145     TUint pos = iContentBegin;
       
  1146     TUint end = iContentBegin + iContentLen;
       
  1147     TInt err = KErrNone;
       
  1148     TInt len = KBufSize;
       
  1149     HBufC8* buf = HBufC8::NewL(KBufSize);              // buffer to object
       
  1150     CleanupStack::PushL(buf);
       
  1151 
       
  1152     TPtr8 ptr   = buf->Des();
       
  1153     // Read object to buf
       
  1154     while (pos < end)
       
  1155         {
       
  1156         len = end - pos;
       
  1157 
       
  1158         if (len > KBufSize)
       
  1159             {
       
  1160             len = KBufSize;
       
  1161             }
       
  1162 
       
  1163         err = iData->Read(pos, ptr, len);    
       
  1164         
       
  1165         if (err == KErrNone)
       
  1166             {
       
  1167             Trg.Write(ptr);
       
  1168             }
       
  1169         if (err < KErrNone)
       
  1170             {        
       
  1171             CleanupStack::PopAndDestroy();
       
  1172             // buf = NULL;
       
  1173             User::Leave(err);
       
  1174             }
       
  1175         pos += KBufSize;
       
  1176         }
       
  1177     CleanupStack::PopAndDestroy(); // buf
       
  1178     }
       
  1179 
       
  1180 // -----------------------------------------------------------------------------
       
  1181 // HBufC8* CCrBer::GetContentStringLC
       
  1182 // Note buf is left to CleanupStack.
       
  1183 // Returns value of content string.
       
  1184 // -----------------------------------------------------------------------------
       
  1185 EXPORT_C HBufC8* CCrBer::GetContentStringLC()
       
  1186     {	       
       
  1187     HBufC8* buf = HBufC8::NewL(iContentLen);             // buffer to Content
       
  1188     CleanupStack::PushL(buf);
       
  1189 
       
  1190     TPtr8 ptr   = buf->Des();
       
  1191 
       
  1192     // Read object to buf
       
  1193     iData->Read(iContentBegin, ptr, iContentLen);    
       
  1194 
       
  1195     return buf;
       
  1196     }
       
  1197 
       
  1198 // -----------------------------------------------------------------------------
       
  1199 // HBufC* CCrBer::GetIA5StringL
       
  1200 // Returns value of IA5 string object.
       
  1201 // -----------------------------------------------------------------------------
       
  1202 EXPORT_C HBufC* CCrBer::GetIA5StringL()
       
  1203     {		
       
  1204     TInt err    = KErrNone;
       
  1205     TInt pos    = NULL;
       
  1206     HBufC* ret  = NULL;
       
  1207     TASN1DecIA5String dec;                              // ASN1 lib (Symbian)
       
  1208 
       
  1209     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1210     TPtr8 ptr   = buf->Des();
       
  1211 
       
  1212     // Read object to buf
       
  1213     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1214 
       
  1215     if (err == KErrNone)
       
  1216         {
       
  1217         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1218         }
       
  1219     else
       
  1220         {        
       
  1221         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1222         User::Leave(err);
       
  1223         }
       
  1224      
       
  1225     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1226     
       
  1227     return ret;
       
  1228     }
       
  1229 
       
  1230 // -----------------------------------------------------------------------------
       
  1231 // TTime CCrBer::GetUTCTimeL
       
  1232 // Returns value of UTC time object.
       
  1233 // -----------------------------------------------------------------------------
       
  1234 EXPORT_C TTime CCrBer::GetUTCTimeL()
       
  1235     {
       
  1236     TInt err = KErrNone;
       
  1237     TInt pos = NULL;
       
  1238     TTime ret = TTime(0);
       
  1239     TASN1DecUTCTime dec;                                // ASN1 lib (Symbian)
       
  1240 
       
  1241     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1242     TPtr8 ptr   = buf->Des();
       
  1243 
       
  1244     // Read object to buf
       
  1245     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1246 
       
  1247     if (err == KErrNone)
       
  1248         {
       
  1249         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1250         }
       
  1251     else
       
  1252         {        
       
  1253         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1254         User::Leave(err);
       
  1255         }  
       
  1256 
       
  1257     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1258 
       
  1259     return ret;
       
  1260     }
       
  1261 
       
  1262 
       
  1263 
       
  1264 // -----------------------------------------------------------------------------
       
  1265 // TTime CCrBer::GetGeneralizedTimeL
       
  1266 // Returns value of generalised time object.
       
  1267 // -----------------------------------------------------------------------------
       
  1268 EXPORT_C TTime CCrBer::GetGeneralizedTimeL()
       
  1269     {
       
  1270     TInt err = KErrNone;
       
  1271     TInt pos = NULL;
       
  1272     TTime ret = TTime(0);
       
  1273     TASN1DecGeneralizedTime dec;                        // ASN1 lib (Symbian)
       
  1274 
       
  1275     HBufC8* buf = HBufC8::NewLC(iObjectLen);            // buffer to object
       
  1276     TPtr8 ptr   = buf->Des();
       
  1277 
       
  1278     // Read object to buf
       
  1279     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1280 
       
  1281     if (err == KErrNone)
       
  1282         {
       
  1283         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1284         }
       
  1285     else
       
  1286         {        
       
  1287         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1288         User::Leave(err);
       
  1289         }  
       
  1290 
       
  1291     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1292 
       
  1293     return ret;
       
  1294     }
       
  1295 
       
  1296 // -----------------------------------------------------------------------------
       
  1297 // HBufC* CCrBer::GetPrintStringL
       
  1298 // Returns value of printable string object.
       
  1299 // -----------------------------------------------------------------------------
       
  1300 EXPORT_C HBufC* CCrBer::GetPrintStringL()
       
  1301     {		
       
  1302     HBufC* ret = NULL;
       
  1303     TInt err = KErrNone;
       
  1304     TInt pos = NULL;
       
  1305     TASN1DecPrintableString dec;                        // ASN1 lib (Symbian)
       
  1306 
       
  1307     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1308     TPtr8 ptr   = buf->Des();
       
  1309 
       
  1310     // Read object to buf
       
  1311     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1312 
       
  1313     if (err == KErrNone)
       
  1314         {
       
  1315         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1316         }
       
  1317     else
       
  1318         {        
       
  1319         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1320         User::Leave(err);
       
  1321         }
       
  1322      
       
  1323     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1324 
       
  1325     return ret;
       
  1326     }
       
  1327 
       
  1328 // -----------------------------------------------------------------------------
       
  1329 // HBufC* CCrBer::GetTeletextStringL
       
  1330 // Returns value of teletext string object.
       
  1331 // -----------------------------------------------------------------------------
       
  1332 EXPORT_C HBufC* CCrBer::GetTeletextStringL()
       
  1333     {																			
       
  1334     TInt err = KErrNone;
       
  1335     TInt pos = NULL;
       
  1336     HBufC* ret = NULL;
       
  1337     TASN1DecTeletexString dec;                          // ASN1 lib (Symbian)
       
  1338 
       
  1339     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1340     TPtr8 ptr   = buf->Des();
       
  1341 
       
  1342     // Read object to buf
       
  1343     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1344 
       
  1345     if (err == KErrNone)
       
  1346         {
       
  1347         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1348         }
       
  1349     else
       
  1350         {        
       
  1351         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1352         User::Leave(err);
       
  1353         }
       
  1354     
       
  1355     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1356     
       
  1357     return ret;
       
  1358     }
       
  1359 
       
  1360 
       
  1361 
       
  1362 // -----------------------------------------------------------------------------
       
  1363 // HBufC8* CCrBer::GetSequence
       
  1364 // Returns value of segunce object.
       
  1365 // -----------------------------------------------------------------------------
       
  1366 EXPORT_C HBufC8* CCrBer::GetSequenceL()
       
  1367     {
       
  1368     TInt err = KErrNone;
       
  1369     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1370     TPtr8 ptr   = buf->Des();
       
  1371 
       
  1372     // Read object to buf
       
  1373     err = iData->Read(ContentBegin(),ptr,ContentLen());
       
  1374 	
       
  1375     if (err < KErrNone)
       
  1376         {        
       
  1377         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1378         // buf = NULL;
       
  1379         User::Leave(err);
       
  1380         }
       
  1381     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1382     // buf = NULL;
       
  1383 
       
  1384     return ptr.AllocL();
       
  1385     }
       
  1386 
       
  1387 // -----------------------------------------------------------------------------
       
  1388 // HBufC* CCrBer::GetVideoString
       
  1389 // Returns value of video string object.
       
  1390 // -----------------------------------------------------------------------------
       
  1391 EXPORT_C HBufC* CCrBer::GetVideoStringL()
       
  1392     {
       
  1393     TInt err = KErrNone;
       
  1394     HBufC* ret = NULL;
       
  1395     TInt pos   = NULL;
       
  1396     TASN1DecVideotexString dec;                         // ASN1 lib (Symbian)
       
  1397 
       
  1398     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1399     TPtr8 ptr   = buf->Des();
       
  1400 
       
  1401     // Read object to buf
       
  1402     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1403 
       
  1404     if (err == KErrNone)
       
  1405         {
       
  1406         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1407         }
       
  1408     else
       
  1409         {        
       
  1410         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1411         User::Leave(err);
       
  1412         }
       
  1413     
       
  1414     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1415 
       
  1416     return ret;
       
  1417     }
       
  1418 
       
  1419 // -----------------------------------------------------------------------------
       
  1420 // HBufC* CCrBer::GetGeneralString
       
  1421 // Returns value of general string object.
       
  1422 // -----------------------------------------------------------------------------
       
  1423 EXPORT_C HBufC* CCrBer::GetGeneralStringL()
       
  1424     {
       
  1425     TInt err    = KErrNone;
       
  1426     TInt pos    = NULL;
       
  1427     HBufC* ret  = NULL;
       
  1428     TASN1DecGeneralString dec;                          // ASN1 lib (Symbian)
       
  1429 
       
  1430     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1431     TPtr8 ptr   = buf->Des();
       
  1432 
       
  1433     // Read object to buf
       
  1434     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1435 
       
  1436     if (err == KErrNone)
       
  1437         {
       
  1438         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1439         }
       
  1440     else
       
  1441         {        
       
  1442         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1443         User::Leave(err);
       
  1444         }
       
  1445     
       
  1446     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1447 
       
  1448     return ret;
       
  1449     }
       
  1450 
       
  1451 // -----------------------------------------------------------------------------
       
  1452 // HBufC* CCrBer::GetVisibleStringL
       
  1453 // Returns value of visible string object.
       
  1454 // -----------------------------------------------------------------------------
       
  1455 EXPORT_C HBufC* CCrBer::GetVisibleStringL()
       
  1456     {
       
  1457     TInt err    = KErrNone;
       
  1458     HBufC* ret  = NULL;
       
  1459     TInt pos    = NULL;
       
  1460     TASN1DecVisibleString dec;                          // ASN1 lib (Symbian)
       
  1461 
       
  1462     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1463     TPtr8 ptr   = buf->Des();
       
  1464 
       
  1465     // Read object to buf
       
  1466     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1467 
       
  1468     if (err == KErrNone)
       
  1469         {
       
  1470         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1471         }
       
  1472     else
       
  1473         {        
       
  1474         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1475         User::Leave(err);
       
  1476         }
       
  1477     
       
  1478     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1479 
       
  1480     return ret;
       
  1481     }
       
  1482 
       
  1483 // -----------------------------------------------------------------------------
       
  1484 // HBufC* CCrBer::GetGraphicsStringL
       
  1485 // Returns value of graphical string object.
       
  1486 // -----------------------------------------------------------------------------
       
  1487 EXPORT_C HBufC* CCrBer::GetGraphicsStringL()
       
  1488     {
       
  1489     TInt err    = KErrNone;
       
  1490     HBufC* ret  = NULL;
       
  1491     TInt pos    = NULL;
       
  1492     TASN1DecGraphicString dec;                          // ASN1 lib (Symbian)
       
  1493 
       
  1494     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1495     TPtr8 ptr   = buf->Des();
       
  1496 
       
  1497     // Read object to buf
       
  1498     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1499 
       
  1500     if (err == KErrNone)
       
  1501         {
       
  1502         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1503         }
       
  1504     else
       
  1505         {        
       
  1506         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1507         User::Leave(err);
       
  1508         }
       
  1509    
       
  1510     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1511     
       
  1512     return ret;
       
  1513     }
       
  1514 
       
  1515 
       
  1516 
       
  1517 // -----------------------------------------------------------------------------
       
  1518 // HBufC* CCrBer::GetNumericStringL
       
  1519 // Returns value of numeric string object.
       
  1520 // -----------------------------------------------------------------------------
       
  1521 EXPORT_C HBufC* CCrBer::GetNumericStringL()
       
  1522     {
       
  1523     TInt err    = KErrNone;
       
  1524     HBufC* ret  = NULL;
       
  1525     TInt pos    = NULL;
       
  1526     TASN1DecNumericString dec;                          // ASN1 lib (Symbian)
       
  1527 
       
  1528     HBufC8* buf = HBufC8::NewLC(iObjectLen);             // buffer to object
       
  1529     TPtr8 ptr   = buf->Des();
       
  1530 
       
  1531     // Read object to buf
       
  1532     err = iData->Read(iObjectBegin,ptr,iObjectLen);    
       
  1533 
       
  1534     if (err == KErrNone)
       
  1535         {
       
  1536         ret = dec.DecodeDERL(ptr, pos);                 // decode object
       
  1537         }
       
  1538     else
       
  1539         {        
       
  1540         CleanupStack::PopAndDestroy();                  // delete buf;
       
  1541         User::Leave(err);
       
  1542         }
       
  1543    
       
  1544     CleanupStack::PopAndDestroy();                      // delete buf;
       
  1545     
       
  1546     return ret;
       
  1547     }
       
  1548 
       
  1549 // -----------------------------------------------------------------------------
       
  1550 //  Encoding
       
  1551 // -----------------------------------------------------------------------------
       
  1552 
       
  1553 // -----------------------------------------------------------------------------
       
  1554 // CCrBer::CreateBool
       
  1555 // Paremeters:     Value of the boolean object. True or False.
       
  1556 // -----------------------------------------------------------------------------
       
  1557 TAny CCrBer::CreateBool(TBool aBool)
       
  1558     {
       
  1559     iType       = KBerBoolean;
       
  1560     iObjectLen  = KBerBooleanLen;           // Boolean object len is always 3
       
  1561     iValue      = aBool; 
       
  1562     }                                       
       
  1563 
       
  1564 // -----------------------------------------------------------------------------
       
  1565 // CCrBer::CreateInt
       
  1566 // Paremeters:     Value of the object (positive, null or negative)
       
  1567 //                 e.g. 234 or 0xFFF or -45676.
       
  1568 // -----------------------------------------------------------------------------
       
  1569 TAny CCrBer::CreateInt(TInt aInt)
       
  1570     {
       
  1571     iType       = KBerInteger;
       
  1572     iValue      = aInt; 
       
  1573 
       
  1574     // Find out object len. Tag + len + value.
       
  1575     if (aInt == 0 || (aInt > (-0x81) &&  aInt < 0x80 ))
       
  1576         iObjectLen  = 0x03;                 
       
  1577     
       
  1578     else if (aInt > (-0x8001) && aInt < 0x8000)                            
       
  1579         iObjectLen  = 0x04;
       
  1580 
       
  1581     else if (aInt > (-0x800001) && aInt < 0x800000)
       
  1582         iObjectLen  = 0x05;
       
  1583 
       
  1584     else
       
  1585         iObjectLen  = 0x06;
       
  1586     }
       
  1587 
       
  1588 // -----------------------------------------------------------------------------
       
  1589 // CCrBer::CreateInt
       
  1590 // Paremeters:     Value of the object (positive, null or negative)
       
  1591 //                 e.g. 234 or 0xFFF or -45676.
       
  1592 // -----------------------------------------------------------------------------
       
  1593 TAny CCrBer::CreateLongInt(RInteger& aData)
       
  1594     {
       
  1595     TInt len    = aData.ByteCount();
       
  1596     iType       = KBerInteger;
       
  1597     iValue      = NULL;
       
  1598     iInt        = RInteger::NewL(aData);
       
  1599 
       
  1600     iObjectLen  = KBerShortLen + len;       // Tag + len(lenlen) + string 
       
  1601 
       
  1602     if (len >= KBerLongLengthBit)
       
  1603         {         
       
  1604         while (len > NULL)                  // add number of lenbytes 
       
  1605             {
       
  1606             len >>= KOctetWidth;            // Next byte
       
  1607             iObjectLen++;   
       
  1608             }
       
  1609         }
       
  1610     }
       
  1611 
       
  1612 // -----------------------------------------------------------------------------
       
  1613 // CCrBer::CreateNull
       
  1614 // -----------------------------------------------------------------------------
       
  1615 TAny CCrBer::CreateNull()
       
  1616     {
       
  1617     iType       = KBerNull;                 // Tag
       
  1618     iObjectLen  = KBerNullLen;              // 05 00
       
  1619     }
       
  1620 
       
  1621 // -----------------------------------------------------------------------------
       
  1622 // CCrBer::CreateOId
       
  1623 // Paremeters:     Pointer to string. e.g.(1.2.43.4335.242)
       
  1624 // -----------------------------------------------------------------------------
       
  1625 TAny CCrBer::CreateOIdL(TDesC8* aString)
       
  1626     {
       
  1627     CCrBerSet* set = CCrBerSet::NewLC(1);
       
  1628     TInt len    = set->AppendObjectIdL(*aString, ETrue);// ask Oid content len
       
  1629     iType       = KBerOid;
       
  1630     iValuePtr   = aString;
       
  1631         
       
  1632     iObjectLen  = (len > NULL) ? KBerShortLen + len : 0; // tag + len + content
       
  1633    
       
  1634     CleanupStack::PopAndDestroy();  // delete set;
       
  1635     // set = NULL;    
       
  1636     }
       
  1637 
       
  1638 
       
  1639 
       
  1640 // -----------------------------------------------------------------------------
       
  1641 // CCrBer::CreateString
       
  1642 // Paremeters:     Type of object and pointer to string.
       
  1643 // -----------------------------------------------------------------------------
       
  1644 TAny CCrBer::CreateString(TBerTag aTag, TDesC8* aString)
       
  1645     {
       
  1646     TInt len    = aString->Length();
       
  1647     iType       = aTag;
       
  1648     iValuePtr   = aString;  
       
  1649     iObjectLen  = KBerShortLen + len;       // Tag + len(lenlen) + string 
       
  1650 
       
  1651     if (len >= KBerLongLengthBit)
       
  1652         {         
       
  1653         while (len > NULL)                  // add number of lenbytes 
       
  1654             {
       
  1655             len >>= KOctetWidth;            // Next byte
       
  1656             iObjectLen++;   
       
  1657             }
       
  1658         }
       
  1659     }
       
  1660 
       
  1661 // -----------------------------------------------------------------------------
       
  1662 // CCrBer::CreateString
       
  1663 // -----------------------------------------------------------------------------
       
  1664 TAny CCrBer::CreateString(TBerTag aTag, CCrData* aData)
       
  1665     {
       
  1666     TInt len    =  0;
       
  1667     aData->Size(len);
       
  1668     iType       = aTag;
       
  1669     iData   = aData;  
       
  1670     iObjectLen  = KBerShortLen + len;       // Tag + len(lenlen) + string 
       
  1671 
       
  1672     if (len >= KBerLongLengthBit)
       
  1673         {         
       
  1674         while (len > NULL)                  // add number of lenbytes 
       
  1675             {
       
  1676             len >>= KOctetWidth;            // Next byte
       
  1677             iObjectLen++;   
       
  1678             }
       
  1679         }
       
  1680     }
       
  1681 
       
  1682 // -----------------------------------------------------------------------------
       
  1683 // CCrBer::CreateStart
       
  1684 // Paremeters:     First parameter is tag type (e.g. seq). Second param is
       
  1685 //                 True if definite length is wanted.
       
  1686 //                 False if indefinite (xx 80) .
       
  1687 // -----------------------------------------------------------------------------
       
  1688 TAny CCrBer::CreateStart(TBerTag aTag, TBool aDefinite)
       
  1689     {
       
  1690     iType = aTag;
       
  1691 
       
  1692     // Definite object length = 0 (set rigth length later).
       
  1693     // Indefinite object length is always 4 (Tag + 80 + 00 + 00)
       
  1694     if (!aDefinite)
       
  1695         {
       
  1696         iObjectLen = KBerIndefiniteLen;
       
  1697         }
       
  1698     }                                                   
       
  1699                                             
       
  1700 // -----------------------------------------------------------------------------
       
  1701 // CCrBer::CreateEnd
       
  1702 // -----------------------------------------------------------------------------
       
  1703 TAny CCrBer::CreateEnd()
       
  1704     {
       
  1705     iType   = KBerEndOfContent;                   // =0x00
       
  1706     iValue  = KBerEndOfContent;
       
  1707     }                                       
       
  1708 
       
  1709 // -----------------------------------------------------------------------------
       
  1710 // CCrBer::CreateBEREncodedObject
       
  1711 // This fuction can be used to create a BER object
       
  1712 // from buffer, which already contains a full BER
       
  1713 // encoded object.
       
  1714 // Paremeters:     Buffer containing BER encoded object.
       
  1715 // -----------------------------------------------------------------------------
       
  1716 TAny CCrBer::CreateBEREncodedObject(TDesC8* aBuffer)
       
  1717     {
       
  1718     iType      = KBerEncodedObject;
       
  1719     iValuePtr  = aBuffer;  
       
  1720     iObjectLen = aBuffer->Length();
       
  1721     }
       
  1722 
       
  1723 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
  1724                                                                                 
       
  1725 // ---------------------------------------------------------
       
  1726 // E32Dll
       
  1727 // DLL entry point
       
  1728 // ---------------------------------------------------------
       
  1729 //
       
  1730 #ifndef EKA2
       
  1731 GLDEF_C TInt E32Dll(TDllReason /*aReason*/)
       
  1732     {
       
  1733     return KErrNone;
       
  1734     }
       
  1735 #endif