1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_filter.hxx"
26 #include "Base64Codec.hxx"
27 #include <rtl/ustrbuf.hxx>
28 #include <osl/diagnose.h>
29 using namespace rtl;
30 using namespace osl;
31 using namespace com::sun::star;
32 
33 const
34   sal_Char aBase64EncodeTable[] =
35     { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
36       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
37       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
38       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
39       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
40 
41 const
42   sal_uInt8 aBase64DecodeTable[]  =
43     { 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 0-15
44 
45       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, // 16-31
46 
47       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 62,  0,  0,  0, 63, // 32-47
48 //                                                +               /
49 
50      52, 53, 54, 55, 56, 57, 58, 59, 60, 61,  0,  0,  0,  0,  0,  0, // 48-63
51 //    0   1   2   3   4   5   6   7   8   9               =
52 
53       0,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, // 64-79
54 //        A   B   C   D   E   F   G   H   I   J   K   L   M   N   O
55 
56      15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,  0,  0,  0,  0,  0, // 80-95
57 //    P   Q   R   S   T   U   V   W   X   Y   Z
58 
59       0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
60 //        a   b   c   d   e   f   g   h   i   j   k   l   m   n   o
61 
62      41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,  0,  0,  0,  0,  0, // 112-127
63 //    p   q   r   s   t   u   v   w   x   y   z
64 
65       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
66       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
67       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
68       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
69       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
70       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
71       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
72       0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0};
73 
74 
ThreeByteToFourByte(const sal_uInt8 * pBuffer,const sal_Int32 nStart,const sal_Int32 nFullLen,rtl::OUStringBuffer & sBuffer)75 void ThreeByteToFourByte (const sal_uInt8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, rtl::OUStringBuffer& sBuffer)
76 {
77 	sal_Int32 nLen(nFullLen - nStart);
78 	if (nLen > 3)
79 		nLen = 3;
80 	if (nLen == 0)
81 	{
82 		sBuffer.setLength(0);
83 		return;
84 	}
85 
86 	sal_Int32 nBinaer;
87 	switch (nLen)
88 	{
89 		case 1:
90 		{
91 			nBinaer = ((sal_uInt8)pBuffer[nStart + 0]) << 16;
92 		}
93 		break;
94 		case 2:
95 		{
96 			nBinaer = (((sal_uInt8)pBuffer[nStart + 0]) << 16) +
97 					(((sal_uInt8)pBuffer[nStart + 1]) <<  8);
98 		}
99 		break;
100 		default:
101 		{
102 			nBinaer = (((sal_uInt8)pBuffer[nStart + 0]) << 16) +
103 					(((sal_uInt8)pBuffer[nStart + 1]) <<  8) +
104 					((sal_uInt8)pBuffer[nStart + 2]);
105 		}
106 		break;
107 	}
108 
109 	sBuffer.appendAscii("====");
110 
111 	sal_uInt8 nIndex = static_cast< sal_uInt8 >((nBinaer & 0xFC0000) >> 18);
112 	sBuffer.setCharAt(0, aBase64EncodeTable [nIndex]);
113 
114 	nIndex = static_cast< sal_uInt8 >((nBinaer & 0x3F000) >> 12);
115 	sBuffer.setCharAt(1, aBase64EncodeTable [nIndex]);
116 	if (nLen == 1)
117 		return;
118 
119 	nIndex = static_cast< sal_uInt8 >((nBinaer & 0xFC0) >> 6);
120 	sBuffer.setCharAt(2, aBase64EncodeTable [nIndex]);
121 	if (nLen == 2)
122 		return;
123 
124 	nIndex = static_cast< sal_uInt8 >((nBinaer & 0x3F));
125 	sBuffer.setCharAt(3, aBase64EncodeTable [nIndex]);
126 }
127 
encodeBase64(rtl::OUStringBuffer & aStrBuffer,const uno::Sequence<sal_Int8> & aPass)128 void Base64Codec::encodeBase64(rtl::OUStringBuffer& aStrBuffer, const uno::Sequence < sal_Int8 >& aPass)
129 {
130 	sal_Int32 i(0);
131 	sal_Int32 nBufferLength(aPass.getLength());
132 	const sal_Int8* pBuffer = aPass.getConstArray();
133 	while (i < nBufferLength)
134 	{
135 		rtl::OUStringBuffer sBuffer;
136 		ThreeByteToFourByte ((const sal_uInt8*)pBuffer, i, nBufferLength, sBuffer);
137 		aStrBuffer.append(sBuffer);
138 		i += 3;
139 	}
140 }
141 
142 const rtl::OUString s2equal(RTL_CONSTASCII_USTRINGPARAM("=="));
143 const rtl::OUString s1equal(RTL_CONSTASCII_USTRINGPARAM("="));
144 #if 0
145 void FourByteToThreeByte (sal_uInt8* pBuffer, sal_Int32& nLength, const sal_Int32 nStart, const rtl::OUString& sString)
146 {
147 	nLength = 0;
148 	sal_Int32 nLen (sString.getLength());
149 
150 	if (nLen != 4)
151 	{
152 		return;
153 	}
154 
155 
156 	if (sString.indexOf(s2equal) == 2)
157 		nLength = 1;
158 	else if (sString.indexOf(s1equal) == 3)
159 		nLength = 2;
160 	else
161 		nLength = 3;
162 
163 	sal_Int32 nBinaer ((aBase64DecodeTable [sString [0]] << 18) +
164 			(aBase64DecodeTable [sString [1]] << 12) +
165 			(aBase64DecodeTable [sString [2]] <<  6) +
166 			(aBase64DecodeTable [sString [3]]));
167 
168 	sal_uInt8 OneByte = static_cast< sal_uInt8 >((nBinaer & 0xFF0000) >> 16);
169 	pBuffer[nStart + 0] = (sal_uInt8)OneByte;
170 
171 	if (nLength == 1)
172 		return;
173 
174 	OneByte = static_cast< sal_uInt8 >((nBinaer & 0xFF00) >> 8);
175 	pBuffer[nStart + 1] = (sal_uInt8)OneByte;
176 
177 	if (nLength == 2)
178 		return;
179 
180 	OneByte = static_cast< sal_uInt8 >(nBinaer & 0xFF);
181 	pBuffer[nStart + 2] = (sal_uInt8)OneByte;
182 }
183 
184 void Base64Codec::decodeBase64(uno::Sequence< sal_uInt8 >& aBuffer, const rtl::OUString& sBuffer)
185 {
186 	sal_Int32 nFirstLength((sBuffer.getLength() / 4) * 3);
187 	sal_uInt8* pBuffer = new sal_uInt8[nFirstLength];
188 	sal_Int32 nSecondLength(0);
189 	sal_Int32 nLength(0);
190 	sal_Int32 i = 0;
191 	sal_Int32 k = 0;
192 	while (i < sBuffer.getLength())
193 	{
194 		FourByteToThreeByte (pBuffer, nLength, k, sBuffer.copy(i, 4));
195 		nSecondLength += nLength;
196 		nLength = 0;
197 		i += 4;
198 		k += 3;
199 	}
200 	aBuffer = uno::Sequence<sal_uInt8>(pBuffer, nSecondLength);
201 	delete[] pBuffer;
202 }
203 #endif
204