javaextensions/satsa/apdu/src.s60/cstspinconverter.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19: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 "cstspinconverter.h"
       
    21 #include <charconv.h>
       
    22 #include <f32file.h>
       
    23 
       
    24 namespace java
       
    25 {
       
    26 namespace satsa
       
    27 {
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 // -----------------------------------------------------------------------------
       
    32 // CSTSPinConverter::ConvertToUTF8L
       
    33 //
       
    34 // -----------------------------------------------------------------------------
       
    35 void CSTSPinConverter::ConvertToUTF8L(const TDesC& aPinValue,
       
    36                                       TPtr8& aConvertedPIN, TBool aUpperCase)
       
    37 {
       
    38 
       
    39     // RFs for CCnvCharacterSetConverter
       
    40     RFs fsSession;
       
    41     User::LeaveIfError(fsSession.Connect());
       
    42     CleanupClosePushL(fsSession);
       
    43 
       
    44     CCnvCharacterSetConverter* converter = CCnvCharacterSetConverter::NewLC();
       
    45 
       
    46     // transform pin to UTF8
       
    47     if (converter->PrepareToConvertToOrFromL(KCharacterSetIdentifierUtf8,
       
    48             fsSession) != CCnvCharacterSetConverter::EAvailable)
       
    49     {
       
    50         User::Leave(KErrNotSupported);
       
    51     }
       
    52 
       
    53     TInt error = converter->ConvertFromUnicode(aConvertedPIN, aPinValue);
       
    54 
       
    55     // check to see that the descriptor isn’t corrupt - leave if it is
       
    56     if (error == CCnvCharacterSetConverter::EErrorIllFormedInput)
       
    57     {
       
    58         User::Leave(KErrCorrupt);
       
    59     }
       
    60     if (error < 0)
       
    61     {
       
    62         User::Leave(KErrGeneral);
       
    63     }
       
    64 
       
    65     //convert to uppercase
       
    66     if (aUpperCase)
       
    67     {
       
    68         aConvertedPIN.UpperCase();
       
    69     }
       
    70 
       
    71     CleanupStack::PopAndDestroy(converter);
       
    72     CleanupStack::PopAndDestroy(&fsSession);
       
    73 
       
    74 }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CSTSPinConverter::ConvertToBCDL
       
    78 // Converts gived value to BCD (Binary Coded Desimal) format. In normal case,
       
    79 // sets each upper nibble to 0. If halfBCD is used, sets each upper nibble to F.
       
    80 // Returns: aConvertedPIN: Puts converted data into this pointer
       
    81 // -----------------------------------------------------------------------------
       
    82 void CSTSPinConverter::ConvertToBCDL(const TDesC& aPinValue,
       
    83                                      TPtr8& aConvertedPIN, TBool aHalfBCD)
       
    84 {
       
    85 
       
    86     //verify that each character is a digit
       
    87     if (!IsAllDigits(aPinValue))
       
    88     {
       
    89         User::Leave(KErrCorrupt);
       
    90     }
       
    91     //encode the characters as BCD (see Section 2) digits: x = BCD(PIN)
       
    92     //BCD=Binary Coded Decimal
       
    93     TUint8 mask = 0x00;
       
    94     if (aHalfBCD)
       
    95     {
       
    96         //upper nibble
       
    97         mask = 0xF0;//IIII0000
       
    98     }
       
    99 
       
   100     TInt length = aPinValue.Length();
       
   101     for (TInt i = 0; i < length; i++)
       
   102     {
       
   103         TUint8 currentNumber = (TUint8) aPinValue[i];
       
   104 
       
   105         //ignore first nibble
       
   106         TUint8 firstNibleMask = 0x0F;
       
   107         TUint8 firstNibleIgnored = (TUint8)(currentNumber & firstNibleMask);
       
   108 
       
   109         TUint8 result = (TUint8)(firstNibleIgnored | mask);
       
   110 
       
   111         aConvertedPIN.Append(result);
       
   112     }
       
   113 }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CSTSPinConverter::ConvertToASCIIL
       
   117 // Just basic conversion from TDesC to TDes8 is needed
       
   118 // -----------------------------------------------------------------------------
       
   119 void CSTSPinConverter::ConvertToASCIIL(const TDesC& aPinValue,
       
   120                                        TPtr8& aConvertedPIN)
       
   121 {
       
   122 
       
   123     // verify that each character is a digit in the current code-page
       
   124     if (!IsAllDigits(aPinValue))
       
   125     {
       
   126         User::Leave(KErrCorrupt);
       
   127     }
       
   128     // if needed encode the characters as ASCII (ANSI X3.4:1968)
       
   129     aConvertedPIN.Append(aPinValue);
       
   130 }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CSTSPinConverter::IsAllDigits
       
   134 // Checks, are all values in gived descriptor digits or not.
       
   135 // Returns: ETrue: All was digits
       
   136 //          EFalse: At least one value was not digit
       
   137 // -----------------------------------------------------------------------------
       
   138 TBool CSTSPinConverter::IsAllDigits(const TDesC& aPinValue)
       
   139 {
       
   140 
       
   141     //if empty
       
   142     if (aPinValue.Length() == 0)
       
   143     {
       
   144         return EFalse;
       
   145     }
       
   146     TLex lex;
       
   147     lex.Assign(aPinValue);
       
   148     while (lex.Peek() != 0)
       
   149     {
       
   150         if (!(lex.Get()).IsDigit())
       
   151         {
       
   152             //if was not digit
       
   153             return EFalse;
       
   154         }
       
   155     }
       
   156     return ETrue;
       
   157 }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CSTSPinConverter::DoBadding
       
   161 // Reallocs gived buffer if needed. aConvertedPIN MUST NOT be in cleanupStack.
       
   162 // Pads only if PIN value length is less than gived length.
       
   163 // -----------------------------------------------------------------------------
       
   164 TInt CSTSPinConverter::DoBadding(const TDesC& aPinValue,
       
   165                                  HBufC8*& aConvertedPIN, TInt aLength, TUint8 aPadChar)
       
   166 {
       
   167     TInt currentLength = aPinValue.Length();
       
   168     // do padding
       
   169     if (currentLength < aLength)
       
   170     {
       
   171         // check that buffer is large enough
       
   172         if (aConvertedPIN->Des().MaxLength() < aLength)
       
   173         {
       
   174             HBufC8* tmp = aConvertedPIN->ReAlloc(aLength);
       
   175             if (!(tmp))
       
   176             {
       
   177                 return KErrNoMemory;
       
   178             }
       
   179             aConvertedPIN = tmp;
       
   180         }
       
   181         // add padding characters up to stored length
       
   182         aConvertedPIN->Des().AppendFill(aPadChar, aLength - currentLength);
       
   183     }
       
   184     return KErrNone;
       
   185 }
       
   186 
       
   187 } // namespace satsa
       
   188 } // namespace java
       
   189 //  End of File