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  // MARKER(update_precomp.py): autogen include statement, do not remove
23 //This file is about the conversion of the UOF v2.0 and ODF document format
24 #include "precompiled_filter.hxx"
25 #include "XMLBase64Codec.hxx"
26 #include <rtl/ustrbuf.hxx>
27 #include <osl/diagnose.h>
28 
29 using namespace rtl;
30 using namespace osl;
31 using namespace com::sun::star;
32 
33 namespace{
34 
35 const sal_Char aBase64EncodeTable[] =
36     { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
37       'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
38       'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
39       'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
40       '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
41 
42 const 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 
128 }
129 
130 namespace XSLT {
encodeBase64(rtl::OUStringBuffer & aStrBuffer,const uno::Sequence<sal_Int8> & aPass)131 void XMLBase64Codec::encodeBase64(rtl::OUStringBuffer& aStrBuffer, const uno::Sequence < sal_Int8 >& aPass)
132 {
133 	sal_Int32 i(0);
134 	sal_Int32 nBufferLength(aPass.getLength());
135 	const sal_Int8* pBuffer = aPass.getConstArray();
136 	while (i < nBufferLength)
137 	{
138 		rtl::OUStringBuffer sBuffer;
139 		ThreeByteToFourByte ((const sal_uInt8*)pBuffer, i, nBufferLength, sBuffer);
140 		aStrBuffer.append(sBuffer);
141 		i += 3;
142 	}
143 }
144 
145 const rtl::OUString s2equal(RTL_CONSTASCII_USTRINGPARAM("=="));
146 const rtl::OUString s1equal(RTL_CONSTASCII_USTRINGPARAM("="));
147 #if 0
148 void FourByteToThreeByte (sal_uInt8* pBuffer, sal_Int32& nLength, const sal_Int32 nStart, const rtl::OUString& sString)
149 {
150 	nLength = 0;
151 	sal_Int32 nLen (sString.getLength());
152 
153 	if (nLen != 4)
154 	{
155 		return;
156 	}
157 
158 
159 	if (sString.indexOf(s2equal) == 2)
160 		nLength = 1;
161 	else if (sString.indexOf(s1equal) == 3)
162 		nLength = 2;
163 	else
164 		nLength = 3;
165 
166 	sal_Int32 nBinaer ((aBase64DecodeTable [sString [0]] << 18) +
167 			(aBase64DecodeTable [sString [1]] << 12) +
168 			(aBase64DecodeTable [sString [2]] <<  6) +
169 			(aBase64DecodeTable [sString [3]]));
170 
171 	sal_uInt8 OneByte = static_cast< sal_uInt8 >((nBinaer & 0xFF0000) >> 16);
172 	pBuffer[nStart + 0] = (sal_uInt8)OneByte;
173 
174 	if (nLength == 1)
175 		return;
176 
177 	OneByte = static_cast< sal_uInt8 >((nBinaer & 0xFF00) >> 8);
178 	pBuffer[nStart + 1] = (sal_uInt8)OneByte;
179 
180 	if (nLength == 2)
181 		return;
182 
183 	OneByte = static_cast< sal_uInt8 >(nBinaer & 0xFF);
184 	pBuffer[nStart + 2] = (sal_uInt8)OneByte;
185 }
186 
187 void XMLBase64Codec::decodeBase64(uno::Sequence< sal_uInt8 >& aBuffer, const rtl::OUString& sBuffer)
188 {
189 	sal_Int32 nFirstLength((sBuffer.getLength() / 4) * 3);
190 	sal_uInt8* pBuffer = new sal_uInt8[nFirstLength];
191 	sal_Int32 nSecondLength(0);
192 	sal_Int32 nLength(0);
193 	sal_Int32 i = 0;
194 	sal_Int32 k = 0;
195 	while (i < sBuffer.getLength())
196 	{
197 		FourByteToThreeByte (pBuffer, nLength, k, sBuffer.copy(i, 4));
198 		nSecondLength += nLength;
199 		nLength = 0;
200 		i += 4;
201 		k += 3;
202 	}
203 	aBuffer = uno::Sequence<sal_uInt8>(pBuffer, nSecondLength);
204 	delete[] pBuffer;
205 }
206 #endif
207 
208 }
209