omadrm/drmengine/utils/src/Base64.cpp
changeset 0 95b198f216e5
child 12 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002-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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <e32std.h>
       
    22 #include "base64.h"
       
    23 
       
    24 // LOCAL CONSTANTS AND MACROS
       
    25 LOCAL_C const TUint8* const KBase64Chars = 
       
    26     _S8("ABCDEFGHIJKLMNOPQRSTUVWXYZ"
       
    27     "abcdefghijklmnopqrstuvwxyz"
       
    28     "0123456789+/");
       
    29 
       
    30 // LOCAL FUNCTION PROTOTYPES
       
    31 LOCAL_C TUint8 FindBase64Char(
       
    32     TUint aChar);
       
    33 
       
    34 // ============================= LOCAL FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // ?function_name ?description.
       
    38 // ?description
       
    39 // Returns: ?value_1: ?description
       
    40 //          ?value_n: ?description_line1
       
    41 //                    ?description_line2
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 LOCAL_C TUint8 FindBase64Char(
       
    45     TUint aChar)
       
    46     {
       
    47     TUint8 i;
       
    48 
       
    49     for (i = 0; i < 64; i++)
       
    50         {
       
    51         if (aChar == KBase64Chars[i]) break;
       
    52         }
       
    53     return i;
       
    54     }
       
    55 
       
    56 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // ?function_name implements...
       
    60 // ?implementation_description.
       
    61 // Returns: ?value_1: ?description
       
    62 //          ?value_n: ?description
       
    63 //                    ?description
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 EXPORT_C HBufC8* Base64EncodeL(
       
    67     const TDesC8& aInput)
       
    68     {
       
    69     HBufC8* ret;
       
    70     TInt i = 0;
       
    71     TInt j = 0;
       
    72     TUint8 charArray3[3];
       
    73     TUint8 charArray4[4];
       
    74     TInt l = 0;
       
    75     
       
    76     ret = HBufC8::NewL((aInput.Length() * 4 + 1) / 3);
       
    77     TPtr8 des = ret->Des();
       
    78     while (l < aInput.Length())
       
    79         {
       
    80         charArray3[i++] = aInput[l];
       
    81         l++;
       
    82         if (i == 3)
       
    83             {
       
    84             charArray4[0] = static_cast<TUint8>((charArray3[0] & 0xfc) >> 2);
       
    85             charArray4[1] = static_cast<TUint8>(
       
    86                 ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4));
       
    87             charArray4[2] = static_cast<TUint8>(
       
    88                 ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6));
       
    89             charArray4[3] = static_cast<TUint8>(charArray3[2] & 0x3f);
       
    90         
       
    91             for (i = 0; i <4; i++)
       
    92                 {
       
    93                 des.Append(KBase64Chars[charArray4[i]]);
       
    94                 }
       
    95             i = 0;
       
    96             }
       
    97         }
       
    98     
       
    99     if (i != 0)
       
   100         {
       
   101         for (j = i; j < 3; j++)
       
   102             {
       
   103             charArray3[j] = '\0';
       
   104             }
       
   105         
       
   106         charArray4[0] = static_cast<TUint8>((charArray3[0] & 0xfc) >> 2);
       
   107         charArray4[1] = static_cast<TUint8>(
       
   108             ((charArray3[0] & 0x03) << 4) +((charArray3[1] & 0xf0) >> 4));
       
   109         charArray4[2] = static_cast<TUint8>(
       
   110             ((charArray3[1] & 0x0f) << 2) +((charArray3[2] & 0xc0) >> 6));
       
   111         charArray4[3] = static_cast<TUint8>(charArray3[2] & 0x3f);
       
   112         
       
   113         for (j = 0; j < i + 1; j++)
       
   114             {
       
   115             des.Append(KBase64Chars[charArray4[j]]);
       
   116             }
       
   117         
       
   118         while ((i++ < 3))
       
   119             {
       
   120             des.Append('=');
       
   121             }
       
   122         }
       
   123     
       
   124     return ret;
       
   125     }
       
   126 
       
   127 // -----------------------------------------------------------------------------
       
   128 // ?function_name implements...
       
   129 // ?implementation_description.
       
   130 // Returns: ?value_1: ?description
       
   131 //          ?value_n: ?description
       
   132 //                    ?description
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 EXPORT_C HBufC8* Base64DecodeL(
       
   136     const TDesC8& aInput)
       
   137     {
       
   138     TInt len = aInput.Length();
       
   139     TInt i = 0;
       
   140     TInt j = 0;
       
   141     TInt in = 0;
       
   142     TUint8 charArray4[4], charArray3[3];
       
   143     HBufC8* ret;
       
   144     
       
   145     ret = HBufC8::NewL((aInput.Length() * 3 + 1) / 4);
       
   146     TPtr8 des = ret->Des();
       
   147     while (len-- && aInput[in] != '=')
       
   148         {
       
   149         if (FindBase64Char(aInput[in]) < 64)
       
   150             {
       
   151             charArray4[i++] = aInput[in]; in++;
       
   152             }
       
   153         else
       
   154             {
       
   155             in++;
       
   156             }
       
   157             
       
   158         if (i == 4)
       
   159             {
       
   160             for (i = 0; i < 4; i++)
       
   161                 {
       
   162                 charArray4[i] = FindBase64Char(charArray4[i]);
       
   163                 }
       
   164             
       
   165             charArray3[0] = static_cast<TUint8>(
       
   166                 (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4));
       
   167             charArray3[1] = static_cast<TUint8>(
       
   168                 ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2));
       
   169             charArray3[2] = static_cast<TUint8>(
       
   170             ((charArray4[2] & 0x3) << 6) + charArray4[3]);
       
   171             
       
   172             for (i = 0; i < 3; i++)
       
   173                 {
       
   174                 des.Append(charArray3[i]);
       
   175                 }
       
   176             i = 0;
       
   177             }
       
   178         }
       
   179     
       
   180     if (i != 0)
       
   181         {
       
   182         for (j = i; j < 4; j++)
       
   183             {
       
   184             charArray4[j] = 0;
       
   185             }
       
   186     
       
   187         for (j = 0; j < 4; j++)
       
   188             {
       
   189             charArray4[j] = FindBase64Char(charArray4[j]);
       
   190             }
       
   191     
       
   192         charArray3[0] = static_cast<TUint8>(
       
   193             (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4));
       
   194         charArray3[1] = static_cast<TUint8>(
       
   195             ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2));
       
   196         charArray3[2] = static_cast<TUint8>(
       
   197             ((charArray4[2] & 0x3) << 6) + charArray4[3]);
       
   198     
       
   199         for (j = 0; j < i - 1; j++)
       
   200             {
       
   201             des.Append(charArray3[j]);
       
   202             }
       
   203         }
       
   204     
       
   205     return ret;
       
   206     }
       
   207 
       
   208 //  End of File