1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
24 #include <comphelper/base64.hxx>
26 #include <com/sun/star/uno/Sequence.hxx>
28 #include <o3tl/safeint.hxx>
29 #include <osl/diagnose.h>
31 using namespace com::sun::star
;
33 namespace comphelper
{
36 char aBase64EncodeTable
[] =
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 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
40 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
41 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
44 sal_uInt8 aBase64DecodeTable
[] =
45 { 62,255,255,255, 63, // 43-47
48 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255, // 48-63
49 // 0 1 2 3 4 5 6 7 8 9 =
51 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 64-79
52 // A B C D E F G H I J K L M N O
54 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255, // 80-95
55 // P Q R S T U V W X Y Z
57 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 96-111
58 // a b c d e f g h i j k l m n o
60 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 }; // 112-123
61 // p q r s t u v w x y z
65 static void ThreeByteToFourByte(const sal_Int8
* pBuffer
, const sal_Int32 nStart
, const sal_Int32 nFullLen
, C
* aCharBuffer
)
67 const sal_Int32
nLen(std::min(nFullLen
- nStart
, sal_Int32(3)));
68 assert(nLen
> 0); // We are never expected to leave the output buffer uninitialized
75 nBinaer
= static_cast<sal_uInt8
>(pBuffer
[nStart
+ 0]) << 16;
80 nBinaer
= (static_cast<sal_uInt8
>(pBuffer
[nStart
+ 0]) << 16) +
81 (static_cast<sal_uInt8
>(pBuffer
[nStart
+ 1]) << 8);
86 nBinaer
= (static_cast<sal_uInt8
>(pBuffer
[nStart
+ 0]) << 16) +
87 (static_cast<sal_uInt8
>(pBuffer
[nStart
+ 1]) << 8) +
88 static_cast<sal_uInt8
>(pBuffer
[nStart
+ 2]);
93 aCharBuffer
[2] = aCharBuffer
[3] = '=';
95 sal_uInt8
nIndex (static_cast<sal_uInt8
>((nBinaer
& 0xFC0000) >> 18));
96 aCharBuffer
[0] = aBase64EncodeTable
[nIndex
];
98 nIndex
= static_cast<sal_uInt8
>((nBinaer
& 0x3F000) >> 12);
99 aCharBuffer
[1] = aBase64EncodeTable
[nIndex
];
102 nIndex
= static_cast<sal_uInt8
>((nBinaer
& 0xFC0) >> 6);
103 aCharBuffer
[2] = aBase64EncodeTable
[nIndex
];
106 nIndex
= static_cast<sal_uInt8
>((nBinaer
& 0x3F));
107 aCharBuffer
[3] = aBase64EncodeTable
[nIndex
];
112 template <typename Buffer
>
113 static void base64encode(Buffer
& aStrBuffer
, const uno::Sequence
<sal_Int8
>& aPass
)
116 sal_Int32
nBufferLength(aPass
.getLength());
117 aStrBuffer
.ensureCapacity(aStrBuffer
.getLength() + (nBufferLength
* 4 + 2) / 3);
118 const sal_Int8
* pBuffer
= aPass
.getConstArray();
119 while (i
< nBufferLength
)
121 ThreeByteToFourByte(pBuffer
, i
, nBufferLength
, aStrBuffer
.appendUninitialized(4));
126 void Base64::encode(OStringBuffer
& aStrBuffer
, const uno::Sequence
<sal_Int8
>& aPass
)
128 base64encode(aStrBuffer
, aPass
);
131 void Base64::encode(OUStringBuffer
& aStrBuffer
, const uno::Sequence
<sal_Int8
>& aPass
)
133 base64encode(aStrBuffer
, aPass
);
136 void Base64::decode(uno::Sequence
<sal_Int8
>& aBuffer
, std::u16string_view sBuffer
)
138 std::size_t nCharsDecoded
= decodeSomeChars( aBuffer
, sBuffer
);
139 OSL_ENSURE( nCharsDecoded
== sBuffer
.size(), "some bytes left in base64 decoding!" );
142 std::size_t Base64::decodeSomeChars(uno::Sequence
<sal_Int8
>& rOutBuffer
, std::u16string_view rInBuffer
)
144 std::size_t nInBufferLen
= rInBuffer
.size();
145 std::size_t nMinOutBufferLen
= (nInBufferLen
/ 4) * 3;
146 if( o3tl::make_unsigned(rOutBuffer
.getLength()) < nMinOutBufferLen
)
147 rOutBuffer
.realloc( nMinOutBufferLen
);
149 const sal_Unicode
*pInBuffer
= rInBuffer
.data();
150 sal_Int8
*pOutBuffer
= rOutBuffer
.getArray();
151 sal_Int8
*pOutBufferStart
= pOutBuffer
;
152 std::size_t nCharsDecoded
= 0;
154 sal_uInt8 aDecodeBuffer
[4];
155 sal_Int32 nBytesToDecode
= 0;
156 sal_Int32 nBytesGotFromDecoding
= 3;
157 std::size_t nInBufferPos
= 0;
158 while( nInBufferPos
< nInBufferLen
)
160 sal_Unicode cChar
= *pInBuffer
;
161 if( cChar
>= '+' && cChar
<= 'z' )
163 sal_uInt8 nByte
= aBase64DecodeTable
[cChar
-'+'];
166 // We have found a valid character!
167 aDecodeBuffer
[nBytesToDecode
++] = nByte
;
169 // One '=' character at the end means 2 out bytes
170 // Two '=' characters at the end mean 1 out bytes
171 if( '=' == cChar
&& nBytesToDecode
> 2 )
172 nBytesGotFromDecoding
--;
173 if( 4 == nBytesToDecode
)
175 // Four characters found, so we may convert now!
176 sal_uInt32 nOut
= (aDecodeBuffer
[0] << 18) +
177 (aDecodeBuffer
[1] << 12) +
178 (aDecodeBuffer
[2] << 6) +
181 *pOutBuffer
++ = static_cast<sal_Int8
>((nOut
& 0xff0000) >> 16);
182 if( nBytesGotFromDecoding
> 1 )
183 *pOutBuffer
++ = static_cast<sal_Int8
>((nOut
& 0xff00) >> 8);
184 if( nBytesGotFromDecoding
> 2 )
185 *pOutBuffer
++ = static_cast<sal_Int8
>(nOut
& 0xff);
186 nCharsDecoded
= nInBufferPos
+ 1;
188 nBytesGotFromDecoding
= 3;
204 if( (pOutBuffer
- pOutBufferStart
) != rOutBuffer
.getLength() )
205 rOutBuffer
.realloc( pOutBuffer
- pOutBufferStart
);
207 return nCharsDecoded
;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */