26#include <com/sun/star/uno/Sequence.hxx>
29#include <osl/diagnose.h>
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',
'+',
'/' };
48 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
51 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
54 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255,255,
57 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };
67 const sal_Int32 nLen(std::min(nFullLen - nStart, sal_Int32(3)));
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] =
'=';
112template <
typename Buffer>
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)
126void Base64::encode(OStringBuffer& aStrBuffer,
const uno::Sequence<sal_Int8>& aPass)
131void Base64::encode(OUStringBuffer& aStrBuffer,
const uno::Sequence<sal_Int8>& aPass)
136void Base64::decode(uno::Sequence<sal_Int8>& aBuffer, std::u16string_view sBuffer)
139 OSL_ENSURE( nCharsDecoded == sBuffer.size(),
"some bytes left in base64 decoding!" );
144 std::size_t nInBufferLen = rInBuffer.size();
145 std::size_t nMinOutBufferLen = (nInBufferLen / 4) * 3;
147 rOutBuffer.realloc( nMinOutBufferLen );
150 sal_Int8 *pOutBuffer = rOutBuffer.getArray();
151 sal_Int8 *pOutBufferStart = pOutBuffer;
152 std::size_t nCharsDecoded = 0;
155 sal_Int32 nBytesToDecode = 0;
156 sal_Int32 nBytesGotFromDecoding = 3;
157 std::size_t nInBufferPos= 0;
158 while( nInBufferPos < nInBufferLen )
161 if( cChar >=
'+' && cChar <=
'z' )
167 aDecodeBuffer[nBytesToDecode++] = nByte;
171 if(
'=' == cChar && nBytesToDecode > 2 )
172 nBytesGotFromDecoding--;
173 if( 4 == nBytesToDecode )
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;
css::uno::Sequence< sal_Int8 > Buffer
static std::size_t decodeSomeChars(css::uno::Sequence< sal_Int8 > &aPass, std::u16string_view sBuffer)
static void encode(OUStringBuffer &aStrBuffer, const css::uno::Sequence< sal_Int8 > &aPass)
encodes the given byte sequence into Base64
static void decode(css::uno::Sequence< sal_Int8 > &aPass, std::u16string_view sBuffer)
static void base64encode(Buffer &aStrBuffer, const uno::Sequence< sal_Int8 > &aPass)
const sal_uInt8 aBase64DecodeTable[]
const char aBase64EncodeTable[]
static void ThreeByteToFourByte(const sal_Int8 *pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, C *aCharBuffer)
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
std::unique_ptr< char[]> aBuffer