1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: Base64Codec.cxx,v $
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_filter.hxx"
33 #include "Base64Codec.hxx"
34 #include <rtl/ustrbuf.hxx>
35 #include <osl/diagnose.h>
38 using namespace com::sun::star
;
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', '+', '/' };
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
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
);
98 nBinaer
= ((sal_uInt8
)pBuffer
[nStart
+ 0]) << 16;
103 nBinaer
= (((sal_uInt8
)pBuffer
[nStart
+ 0]) << 16) +
104 (((sal_uInt8
)pBuffer
[nStart
+ 1]) << 8);
109 nBinaer
= (((sal_uInt8
)pBuffer
[nStart
+ 0]) << 16) +
110 (((sal_uInt8
)pBuffer
[nStart
+ 1]) << 8) +
111 ((sal_uInt8
)pBuffer
[nStart
+ 2]);
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
]);
126 nIndex
= static_cast< sal_uInt8
>((nBinaer
& 0xFC0) >> 6);
127 sBuffer
.setCharAt(2, aBase64EncodeTable
[nIndex
]);
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_Int8
>& aPass
)
138 sal_Int32
nBufferLength(aPass
.getLength());
139 const sal_Int8
* pBuffer
= aPass
.getConstArray();
140 while (i
< nBufferLength
)
142 rtl::OUStringBuffer sBuffer
;
143 ThreeByteToFourByte ((const sal_uInt8
*)pBuffer
, i
, nBufferLength
, sBuffer
);
144 aStrBuffer
.append(sBuffer
);
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
)
155 sal_Int32
nLen (sString
.getLength());
163 if (sString
.indexOf(s2equal
) == 2)
165 else if (sString
.indexOf(s1equal
) == 3)
170 sal_Int32
nBinaer ((aBase64DecodeTable
[sString
[0]] << 18) +
171 (aBase64DecodeTable
[sString
[1]] << 12) +
172 (aBase64DecodeTable
[sString
[2]] << 6) +
173 (aBase64DecodeTable
[sString
[3]]));
175 sal_uInt8 OneByte
= static_cast< sal_uInt8
>((nBinaer
& 0xFF0000) >> 16);
176 pBuffer
[nStart
+ 0] = (sal_uInt8
)OneByte
;
181 OneByte
= static_cast< sal_uInt8
>((nBinaer
& 0xFF00) >> 8);
182 pBuffer
[nStart
+ 1] = (sal_uInt8
)OneByte
;
187 OneByte
= static_cast< sal_uInt8
>(nBinaer
& 0xFF);
188 pBuffer
[nStart
+ 2] = (sal_uInt8
)OneByte
;
191 void Base64Codec::decodeBase64(uno::Sequence
< sal_uInt8
>& aBuffer
, const rtl::OUString
& sBuffer
)
193 sal_Int32
nFirstLength((sBuffer
.getLength() / 4) * 3);
194 sal_uInt8
* pBuffer
= new sal_uInt8
[nFirstLength
];
195 sal_Int32
nSecondLength(0);
196 sal_Int32
nLength(0);
199 while (i
< sBuffer
.getLength())
201 FourByteToThreeByte (pBuffer
, nLength
, k
, sBuffer
.copy(i
, 4));
202 nSecondLength
+= nLength
;
207 aBuffer
= uno::Sequence
<sal_uInt8
>(pBuffer
, nSecondLength
);