Update ooo320-m1
[ooovba.git] / package / source / manifest / Base64Codec.cxx
blob7b4ecda8230bea8ea200c94abc45db1fa4627b44
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Base64Codec.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_package.hxx"
33 #include "Base64Codec.hxx"
34 #include <rtl/ustrbuf.hxx>
35 #include <osl/diagnose.h>
36 using namespace rtl;
37 using namespace osl;
38 using namespace com::sun::star;
40 const
41 sal_Char aBase64EncodeTable[] =
42 { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
43 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
44 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
45 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
46 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
48 const
49 sal_uInt8 aBase64DecodeTable[] =
50 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0-15
52 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, // 32-47
55 // + /
57 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, // 48-63
58 // 0 1 2 3 4 5 6 7 8 9 =
60 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
61 // A B C D E F G H I J K L M N O
63 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, // 80-95
64 // P Q R S T U V W X Y Z
66 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
67 // a b c d e f g h i j k l m n o
69 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, // 112-127
70 // p q r s t u v w x y z
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
75 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
76 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
82 void ThreeByteToFourByte (const sal_uInt8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, rtl::OUStringBuffer& sBuffer)
84 sal_Int32 nLen(nFullLen - nStart);
85 if (nLen > 3)
86 nLen = 3;
87 if (nLen == 0)
89 sBuffer.setLength(0);
90 return;
93 sal_Int32 nBinaer;
94 switch (nLen)
96 case 1:
98 nBinaer = ((sal_uInt8)pBuffer[nStart + 0]) << 16;
100 break;
101 case 2:
103 nBinaer = (((sal_uInt8)pBuffer[nStart + 0]) << 16) +
104 (((sal_uInt8)pBuffer[nStart + 1]) << 8);
106 break;
107 default:
109 nBinaer = (((sal_uInt8)pBuffer[nStart + 0]) << 16) +
110 (((sal_uInt8)pBuffer[nStart + 1]) << 8) +
111 ((sal_uInt8)pBuffer[nStart + 2]);
113 break;
116 sBuffer.appendAscii("====");
118 sal_uInt8 nIndex = static_cast< sal_uInt8 >((nBinaer & 0xFC0000) >> 18);
119 sBuffer.setCharAt(0, aBase64EncodeTable [nIndex]);
121 nIndex = static_cast< sal_uInt8 >((nBinaer & 0x3F000) >> 12);
122 sBuffer.setCharAt(1, aBase64EncodeTable [nIndex]);
123 if (nLen == 1)
124 return;
126 nIndex = static_cast< sal_uInt8 >((nBinaer & 0xFC0) >> 6);
127 sBuffer.setCharAt(2, aBase64EncodeTable [nIndex]);
128 if (nLen == 2)
129 return;
131 nIndex = static_cast< sal_uInt8 >(nBinaer & 0x3F);
132 sBuffer.setCharAt(3, aBase64EncodeTable [nIndex]);
135 void Base64Codec::encodeBase64(rtl::OUStringBuffer& aStrBuffer, const uno::Sequence < sal_uInt8 >& aPass)
137 sal_Int32 i(0);
138 sal_Int32 nBufferLength(aPass.getLength());
139 const sal_uInt8* pBuffer = aPass.getConstArray();
140 while (i < nBufferLength)
142 rtl::OUStringBuffer sBuffer;
143 ThreeByteToFourByte (pBuffer, i, nBufferLength, sBuffer);
144 aStrBuffer.append(sBuffer);
145 i += 3;
149 const rtl::OUString s2equal(RTL_CONSTASCII_USTRINGPARAM("=="));
150 const rtl::OUString s1equal(RTL_CONSTASCII_USTRINGPARAM("="));
152 void FourByteToThreeByte (sal_uInt8* pBuffer, sal_Int32& nLength, const sal_Int32 nStart, const rtl::OUString& sString)
154 nLength = 0;
155 sal_Int32 nLen (sString.getLength());
157 OSL_ASSERT( nLen == 4 );
158 if (nLen != 4)
159 return;
161 if (sString.indexOf(s2equal) == 2)
162 nLength = 1;
163 else if (sString.indexOf(s1equal) == 3)
164 nLength = 2;
165 else
166 nLength = 3;
168 sal_Int32 nBinaer ((aBase64DecodeTable [sString [0]] << 18) +
169 (aBase64DecodeTable [sString [1]] << 12) +
170 (aBase64DecodeTable [sString [2]] << 6) +
171 (aBase64DecodeTable [sString [3]]));
173 sal_uInt8 OneByte = static_cast< sal_uInt8 >((nBinaer & 0xFF0000) >> 16);
174 pBuffer[nStart + 0] = (sal_uInt8)OneByte;
176 if (nLength == 1)
177 return;
179 OneByte = static_cast< sal_uInt8 >((nBinaer & 0xFF00) >> 8);
180 pBuffer[nStart + 1] = OneByte;
182 if (nLength == 2)
183 return;
185 OneByte = static_cast< sal_uInt8 >(nBinaer & 0xFF);
186 pBuffer[nStart + 2] = OneByte;
189 void Base64Codec::decodeBase64(uno::Sequence< sal_uInt8 >& aBuffer, const rtl::OUString& sBuffer)
191 sal_Int32 nFirstLength((sBuffer.getLength() / 4) * 3);
192 sal_uInt8* pBuffer = new sal_uInt8[nFirstLength];
193 sal_Int32 nSecondLength(0);
194 sal_Int32 nLength(0);
195 sal_Int32 i = 0;
196 sal_Int32 k = 0;
197 while (i < sBuffer.getLength())
199 FourByteToThreeByte (pBuffer, nLength, k, sBuffer.copy(i, 4));
200 nSecondLength += nLength;
201 nLength = 0;
202 i += 4;
203 k += 3;
205 aBuffer = uno::Sequence<sal_uInt8>(pBuffer, nSecondLength);
206 delete[] pBuffer;