summaryrefslogtreecommitdiff
path: root/comphelper/source/misc/base64.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'comphelper/source/misc/base64.cxx')
-rw-r--r--comphelper/source/misc/base64.cxx59
1 files changed, 28 insertions, 31 deletions
diff --git a/comphelper/source/misc/base64.cxx b/comphelper/source/misc/base64.cxx
index d73465601adb..2646f297d7aa 100644
--- a/comphelper/source/misc/base64.cxx
+++ b/comphelper/source/misc/base64.cxx
@@ -17,10 +17,15 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
+#include <sal/config.h>
+
+#include <cstddef>
+
#include <comphelper/base64.hxx>
#include <com/sun/star/uno/Sequence.hxx>
+#include <o3tl/safeint.hxx>
#include <osl/diagnose.h>
using namespace com::sun::star;
@@ -56,15 +61,11 @@ const
// p q r s t u v w x y z
-static void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, char* aCharBuffer)
+template <typename C>
+static void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, C* aCharBuffer)
{
- sal_Int32 nLen(nFullLen - nStart);
- if (nLen > 3)
- nLen = 3;
- if (nLen == 0)
- {
- return;
- }
+ const sal_Int32 nLen(std::min(nFullLen - nStart, sal_Int32(3)));
+ assert(nLen > 0); // We are never expected to leave the output buffer uninitialized
sal_Int32 nBinaer;
switch (nLen)
@@ -89,7 +90,7 @@ static void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart,
break;
}
- aCharBuffer[0] = aCharBuffer[1] = aCharBuffer[2] = aCharBuffer[3] = '=';
+ aCharBuffer[2] = aCharBuffer[3] = '=';
sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinaer & 0xFC0000) >> 18));
aCharBuffer[0] = aBase64EncodeTable [nIndex];
@@ -108,56 +109,52 @@ static void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart,
}
}
-void Base64::encode(OStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
+template <typename Buffer>
+static void base64encode(Buffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
{
sal_Int32 i(0);
sal_Int32 nBufferLength(aPass.getLength());
+ aStrBuffer.ensureCapacity(aStrBuffer.getLength() + (nBufferLength * 4 + 2) / 3);
const sal_Int8* pBuffer = aPass.getConstArray();
while (i < nBufferLength)
{
- char aCharBuffer[4];
- ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
- aStrBuffer.append(aCharBuffer, SAL_N_ELEMENTS(aCharBuffer));
+ ThreeByteToFourByte(pBuffer, i, nBufferLength, aStrBuffer.appendUninitialized(4));
i += 3;
}
}
+void Base64::encode(OStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
+{
+ base64encode(aStrBuffer, aPass);
+}
+
void Base64::encode(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
{
- sal_Int32 i(0);
- sal_Int32 nBufferLength(aPass.getLength());
- const sal_Int8* pBuffer = aPass.getConstArray();
- while (i < nBufferLength)
- {
- char aCharBuffer[4];
- ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
- aStrBuffer.appendAscii(aCharBuffer, SAL_N_ELEMENTS(aCharBuffer));
- i += 3;
- }
+ base64encode(aStrBuffer, aPass);
}
void Base64::decode(uno::Sequence<sal_Int8>& aBuffer, std::u16string_view sBuffer)
{
- sal_Int32 nCharsDecoded = decodeSomeChars( aBuffer, sBuffer );
- OSL_ENSURE( sal_uInt32(nCharsDecoded) == sBuffer.size(), "some bytes left in base64 decoding!" );
+ std::size_t nCharsDecoded = decodeSomeChars( aBuffer, sBuffer );
+ OSL_ENSURE( nCharsDecoded == sBuffer.size(), "some bytes left in base64 decoding!" );
}
-sal_Int32 Base64::decodeSomeChars(uno::Sequence<sal_Int8>& rOutBuffer, std::u16string_view rInBuffer)
+std::size_t Base64::decodeSomeChars(uno::Sequence<sal_Int8>& rOutBuffer, std::u16string_view rInBuffer)
{
- sal_Int32 nInBufferLen = rInBuffer.size();
- sal_Int32 nMinOutBufferLen = (nInBufferLen / 4) * 3;
- if( rOutBuffer.getLength() < nMinOutBufferLen )
+ std::size_t nInBufferLen = rInBuffer.size();
+ std::size_t nMinOutBufferLen = (nInBufferLen / 4) * 3;
+ if( o3tl::make_unsigned(rOutBuffer.getLength()) < nMinOutBufferLen )
rOutBuffer.realloc( nMinOutBufferLen );
const sal_Unicode *pInBuffer = rInBuffer.data();
sal_Int8 *pOutBuffer = rOutBuffer.getArray();
sal_Int8 *pOutBufferStart = pOutBuffer;
- sal_Int32 nCharsDecoded = 0;
+ std::size_t nCharsDecoded = 0;
sal_uInt8 aDecodeBuffer[4];
sal_Int32 nBytesToDecode = 0;
sal_Int32 nBytesGotFromDecoding = 3;
- sal_Int32 nInBufferPos= 0;
+ std::size_t nInBufferPos= 0;
while( nInBufferPos < nInBufferLen )
{
sal_Unicode cChar = *pInBuffer;