initial checkin
[xmlrpc4s.git] / src / Base64.cpp
blob651d196cb98a9a8b5636c3807b5a5619b07e748e
1 #include "Base64.h"
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;
17 else
18 encodedLength = (aSrcString.Length() / 3) * 4 + 4;
20 HBufC8* encoded = HBufC8::NewL( encodedLength );
22 TBuf8<3> char_array_3;
23 char_array_3.Zero();
24 char_array_3.SetLength( 3 );
26 TBuf8<4> char_array_4;
27 char_array_4.Zero();
28 char_array_4.SetLength( 4 );
30 TInt index = aSrcString.Length();
31 TInt i = 0;
32 TInt j = 0;
34 while ( index-- ) {
35 char_array_3[i++] = aSrcString[j++];
37 if (i == 3) {
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]] );
45 i = 0;
49 if (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]] );
62 while((i++ < 3))
63 encoded->Des().Append( '=' );
67 return encoded;
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();
81 TInt i = 0;
82 TInt j = 0;
83 TInt in_ = 0;
85 TBuf8<3> char_array_3;
86 char_array_3.Zero();
87 char_array_3.SetLength( 3 );
89 TBuf8<4> char_array_4;
90 char_array_4.Zero();
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)) )
99 continue;
101 char_array_4[i++] = TChar(inchar);
103 if (i == 4) {
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;
108 break;
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] );
119 i = 0;
123 if (i) {
124 for (j = i; j < 4; j++)
125 char_array_4[j] = 0;
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;
131 break;
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] );
142 return decoded;