summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2022-08-22 09:46:34 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2022-08-22 10:06:22 +0200
commit02cf7ccf3f1abc245c494fd400ac19512c3904e6 (patch)
tree730bb2ad2a8105fd15a3f1ab5ece39e70a00b65f /comphelper
parent4e91f49339d5197f8d32f633a5f48375db68e808 (diff)
Optimize and deduplicate Base64::encode a bit
Change-Id: Ic549c8bf938ae363f9d1b83b8e3f4e6ee10603d6 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138657 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/source/misc/base64.cxx38
1 files changed, 15 insertions, 23 deletions
diff --git a/comphelper/source/misc/base64.cxx b/comphelper/source/misc/base64.cxx
index 5e3e3ca0afb5..2646f297d7aa 100644
--- a/comphelper/source/misc/base64.cxx
+++ b/comphelper/source/misc/base64.cxx
@@ -61,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)
@@ -94,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];
@@ -113,32 +109,28 @@ 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, std::size(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, std::size(aCharBuffer));
- i += 3;
- }
+ base64encode(aStrBuffer, aPass);
}
void Base64::decode(uno::Sequence<sal_Int8>& aBuffer, std::u16string_view sBuffer)