javaextensions/satsa/pki/src.s60/tstscharactersetconverter.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 
       
    20 // INCLUDE FILES
       
    21 #include    "tstscharactersetconverter.h"
       
    22 #include    "stspkiconstants.h"
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 // ============================ MEMBER FUNCTIONS ===============================
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // TSTSCharacterSetConverter::TSTSCharacterSetConverter
       
    34 // C++ default constructor can NOT contain any code, that
       
    35 // might leave.
       
    36 // -----------------------------------------------------------------------------
       
    37 //
       
    38 TSTSCharacterSetConverter::TSTSCharacterSetConverter()
       
    39 {
       
    40 }
       
    41 
       
    42 
       
    43 // -----------------------------------------------------------------------------
       
    44 // TSTSCharacterSetConverter::EscapedUnicodeToPrintableL
       
    45 // Converts string from escaped unicode to printable string
       
    46 // (other items were commented in a header).
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 HBufC8* TSTSCharacterSetConverter::EscapedUnicodeToPrintableLC(
       
    50     const TDesC& aValue) const
       
    51 {
       
    52     TInt valueLength = aValue.Length();
       
    53     // returned value can never be longer than the original (it can be shorter)
       
    54     HBufC8* value = HBufC8::NewL(valueLength);
       
    55     CleanupStack::PushL(value);
       
    56     TPtr8 valueDes = value->Des();
       
    57 
       
    58     TLex lexer(aValue);
       
    59 
       
    60     while (!lexer.Eos())
       
    61     {
       
    62         TChar currentChar = lexer.Get();
       
    63         if (currentChar == '\\')
       
    64         {
       
    65             // we are escaping
       
    66             if (lexer.Eos())
       
    67             {
       
    68                 User::Leave(KSTSErrInvalidCAName);
       
    69             }
       
    70             lexer.Mark();
       
    71             currentChar = lexer.Get();
       
    72             if (!IsSpecial(currentChar))
       
    73             {
       
    74                 if (lexer.Eos())
       
    75                 {
       
    76                     User::Leave(KSTSErrInvalidCAName);
       
    77                 }
       
    78                 lexer.Get();
       
    79                 TLex hexPair(lexer.MarkedToken());
       
    80                 TUint hexChar;
       
    81                 User::LeaveIfError(hexPair.Val(hexChar, EHex));
       
    82                 currentChar = hexChar;
       
    83             }
       
    84         }
       
    85         if (IsPrintable(currentChar))
       
    86         {
       
    87             valueDes.Append(currentChar);
       
    88         }
       
    89         else
       
    90         {
       
    91             User::Leave(KSTSErrInvalidCharactersInCAName);
       
    92         }
       
    93     }
       
    94 
       
    95     return value;
       
    96 }
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 // TSTSCharacterSetConverter::IsSpecial
       
   100 // checks if the given character is "special"
       
   101 // -----------------------------------------------------------------------------
       
   102 //
       
   103 TBool TSTSCharacterSetConverter::IsSpecial(
       
   104     const TChar& aValue) const
       
   105 {
       
   106     switch (aValue)
       
   107     {
       
   108     case(','):
       
   109     case('+'):
       
   110     case('\"'):
       
   111     case('\\'):
       
   112     case('<'):
       
   113     case('>'):
       
   114     case(';'):
       
   115     {
       
   116         return ETrue;
       
   117     }
       
   118     default:
       
   119     {
       
   120         return EFalse;
       
   121     }
       
   122     }
       
   123 }
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // TSTSCharacterSetConverter::IsPrintable
       
   127 // checks if the given character is allowed in PrintableString
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 TBool TSTSCharacterSetConverter::IsPrintable(
       
   131     const TChar& aValue) const
       
   132 {
       
   133     if (aValue > 128)  // non-ascii characters are not allowed
       
   134     {
       
   135         return EFalse;
       
   136     }
       
   137     if (aValue.IsAlphaDigit())  // A-Z,a-z,0-9 are allowed
       
   138     {
       
   139         return ETrue;
       
   140     }
       
   141     switch (aValue)  // some other characters are also allowed
       
   142     {
       
   143     case(' '):
       
   144     case('\''):
       
   145     case('('):
       
   146     case(')'):
       
   147     case('+'):
       
   148     case(','):
       
   149     case('-'):
       
   150     case('.'):
       
   151     case('/'):
       
   152     case(':'):
       
   153     case('='):
       
   154     case('?'):
       
   155     {
       
   156         return ETrue;
       
   157     }
       
   158     default:
       
   159     {
       
   160         return EFalse;
       
   161     }
       
   162     }
       
   163 }
       
   164 
       
   165 
       
   166 
       
   167 
       
   168 //  End of File