6 EXPORT_C HBufC8
* TBase64::Base64_EncodeL( const TDesC8
& aSrcString
) {
8 const TInt base64Chars
[]= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
9 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
10 '0','1','2','3','4','5','6','7','8','9','+','/' };
13 TInt encodedLength
= 0;
15 if ( aSrcString
.Length() % 3 == 0 )
16 encodedLength
= (aSrcString
.Length() / 3) * 4;
18 encodedLength
= (aSrcString
.Length() / 3) * 4 + 4;
20 HBufC8
* encoded
= HBufC8::NewL( encodedLength
);
22 TBuf8
<3> char_array_3
;
24 char_array_3
.SetLength( 3 );
26 TBuf8
<4> char_array_4
;
28 char_array_4
.SetLength( 4 );
30 TInt index
= aSrcString
.Length();
35 char_array_3
[i
++] = aSrcString
[j
++];
38 char_array_4
[0] = (char_array_3
[0] & 0xfc) >> 2;
39 char_array_4
[1] = ((char_array_3
[0] & 0x03) << 4) + ((char_array_3
[1] & 0xf0) >> 4);
40 char_array_4
[2] = ((char_array_3
[1] & 0x0f) << 2) + ((char_array_3
[2] & 0xc0) >> 6);
41 char_array_4
[3] = char_array_3
[2] & 0x3f;
43 for(i
= 0; (i
<4) ; i
++)
44 encoded
->Des().Append( base64Chars
[char_array_4
[i
]] );
51 for(j
= i
; j
< 3; j
++)
52 char_array_3
[j
] = '\0';
54 char_array_4
[0] = (char_array_3
[0] & 0xfc) >> 2;
55 char_array_4
[1] = ((char_array_3
[0] & 0x03) << 4) + ((char_array_3
[1] & 0xf0) >> 4);
56 char_array_4
[2] = ((char_array_3
[1] & 0x0f) << 2) + ((char_array_3
[2] & 0xc0) >> 6);
57 char_array_4
[3] = char_array_3
[2] & 0x3f;
59 for (j
= 0; (j
< i
+ 1); j
++)
60 encoded
->Des().Append( base64Chars
[char_array_4
[j
]] );
63 encoded
->Des().Append( '=' );
71 EXPORT_C HBufC8
* TBase64::Base64_DecodeL( const TDesC8
& aSrcString
) {
73 const TInt base64Chars
[]= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
74 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
75 '0','1','2','3','4','5','6','7','8','9','+','/' };
77 TInt decodedLength
= aSrcString
.Length() / 4 * 3 + 3;
78 HBufC8
* decoded
= HBufC8::NewL( decodedLength
);
80 TInt index
= aSrcString
.Length();
85 TBuf8
<3> char_array_3
;
87 char_array_3
.SetLength( 3 );
89 TBuf8
<4> char_array_4
;
91 char_array_4
.SetLength( 4 );
93 while (index
-- && ( aSrcString
[in_
] != '=') /*&& is_base64(encoded_string[in_])*/ ) {
95 TInt inchar
= aSrcString
[in_
++];
96 // Include only '+', '/0123456789', 'A....Z' and 'a....z'
97 if( !(inchar
== 43 || (inchar
>= 47 && inchar
<= 57) || (inchar
>= 65 && inchar
<= 90) ||
98 (inchar
>= 97 && inchar
<= 122)) )
101 char_array_4
[i
++] = TChar(inchar
);
104 for (i
= 0; i
< 4; i
++)
105 for( TInt charIndex
= 0; charIndex
< 64 ; charIndex
++ ) {
106 if( base64Chars
[charIndex
] == char_array_4
[i
] ) {
107 char_array_4
[i
] = charIndex
;
112 char_array_3
[0] = (char_array_4
[0] << 2) + ((char_array_4
[1] & 0x30) >> 4);
113 char_array_3
[1] = ((char_array_4
[1] & 0xf) << 4) + ((char_array_4
[2] & 0x3c) >> 2);
114 char_array_3
[2] = ((char_array_4
[2] & 0x3) << 6) + char_array_4
[3];
116 for (i
= 0; (i
< 3); i
++)
117 decoded
->Des().Append( char_array_3
[i
] );
124 for (j
= i
; j
< 4; j
++)
127 for (j
= 0; j
<4; j
++)
128 for( TInt charIndex
= 0; charIndex
<64 ; charIndex
++ ) {
129 if( base64Chars
[charIndex
] == char_array_4
[j
] ) {
130 char_array_4
[j
] = charIndex
;
135 char_array_3
[0] = (char_array_4
[0] << 2) + ((char_array_4
[1] & 0x30) >> 4);
136 char_array_3
[1] = ((char_array_4
[1] & 0xf) << 4) + ((char_array_4
[2] & 0x3c) >> 2);
137 char_array_3
[2] = ((char_array_4
[2] & 0x3) << 6) + char_array_4
[3];
139 for (j
= 0; (j
< i
- 1); j
++) decoded
->Des().Append( char_array_3
[j
] );