summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2018-06-27 00:39:34 +0200
committerTomaž Vajngerl <quikee@gmail.com>2018-07-06 18:25:49 +0200
commit0d0bf4132d7cf81306a3de6592fce40ab69ef040 (patch)
tree124be65dcdc8652d640729959d299b2e2858bac8 /comphelper
parentfc434c8fd62ccb6d083af075f1ab83da6bd76b8d (diff)
base64: change impl. of encodig to also work with OStringBuffer
+ make test simpler and add a test case for the new behaviour Change-Id: Ifc743835f0cd634c79929ce22dc36b5a822a7e88 Reviewed-on: https://gerrit.libreoffice.org/56969 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/qa/unit/base64_test.cxx94
-rw-r--r--comphelper/source/misc/base64.cxx32
2 files changed, 75 insertions, 51 deletions
diff --git a/comphelper/qa/unit/base64_test.cxx b/comphelper/qa/unit/base64_test.cxx
index b3f2143d4a41..6a18682ca6bd 100644
--- a/comphelper/qa/unit/base64_test.cxx
+++ b/comphelper/qa/unit/base64_test.cxx
@@ -36,67 +36,77 @@
#include <comphelper/base64.hxx>
#include <sal/log.hxx>
-
-using namespace ::com::sun::star;
-using namespace ::com::sun::star::util;
-
+using namespace css;
+using namespace css::util;
namespace {
-class Base64Test
- : public ::CppUnit::TestFixture
+class Base64Test : public CppUnit::TestFixture
{
public:
- void testBase64();
+ void testBase64Encode();
+ void testBase64Decode();
+ void testBase64EncodeForOStringBuffer();
CPPUNIT_TEST_SUITE(Base64Test);
- CPPUNIT_TEST(testBase64);
+ CPPUNIT_TEST(testBase64Encode);
+ CPPUNIT_TEST(testBase64Decode);
+ CPPUNIT_TEST(testBase64EncodeForOStringBuffer);
CPPUNIT_TEST_SUITE_END();
-
-private:
};
+void Base64Test::testBase64Encode()
+{
+ OUStringBuffer aBuffer;
+ uno::Sequence<sal_Int8> inputSequence;
+ inputSequence = { 0, 0, 0, 0, 0, 1, 2, 3 };
+ comphelper::Base64::encode(aBuffer, inputSequence);
+ CPPUNIT_ASSERT_EQUAL(OUString("AAAAAAABAgM="), aBuffer.makeStringAndClear());
-void doTestEncodeBase64(char const*const pis, const uno::Sequence<sal_Int8>& aPass)
-{
- OUString const is(OUString::createFromAscii(pis));
- OUStringBuffer buf;
- comphelper::Base64::encode(buf, aPass);
- SAL_INFO("sax.cppunit","" << buf.toString());
- CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
+ inputSequence = { 5, 2, 3, 0, 0, 1, 2, 3 };
+ comphelper::Base64::encode(aBuffer, inputSequence);
+ CPPUNIT_ASSERT_EQUAL(OUString("BQIDAAABAgM="), aBuffer.makeStringAndClear());
+
+ inputSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 };
+ comphelper::Base64::encode(aBuffer, inputSequence);
+ CPPUNIT_ASSERT_EQUAL(OUString("yB9NbwABAgM="), aBuffer.makeStringAndClear());
}
-void doTestDecodeBase64(const uno::Sequence<sal_Int8>& aPass, char const*const pis)
+void Base64Test::testBase64Decode()
{
- OUString const is(OUString::createFromAscii(pis));
- uno::Sequence< sal_Int8 > tempSequence;
- comphelper::Base64::decode(tempSequence, is);
- SAL_INFO("sax.cppunit","" << is);
- bool b = (tempSequence==aPass);
- CPPUNIT_ASSERT(b);
+ uno::Sequence<sal_Int8> decodedSequence;
+
+ uno::Sequence<sal_Int8> expectedSequence = { 0, 0, 0, 0, 0, 1, 2, 3 };
+ comphelper::Base64::decode(decodedSequence, "AAAAAAABAgM=");
+ CPPUNIT_ASSERT(std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin()));
+
+ expectedSequence = { 5, 2, 3, 0, 0, 1, 2, 3 };
+ comphelper::Base64::decode(decodedSequence, "BQIDAAABAgM=");
+ CPPUNIT_ASSERT(std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin()));
+
+ expectedSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 };
+ comphelper::Base64::decode(decodedSequence, "yB9NbwABAgM=");
+ CPPUNIT_ASSERT(std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin()));
}
-void Base64Test::testBase64()
+void Base64Test::testBase64EncodeForOStringBuffer()
{
- std::vector< sal_Int8 > tempSeq { 0, 0, 0, 0, 0, 1, 2, 3 };
- uno::Sequence< sal_Int8 > tempSequence = comphelper::containerToSequence(tempSeq);
- doTestEncodeBase64("AAAAAAABAgM=", tempSequence);
- doTestDecodeBase64(tempSequence, "AAAAAAABAgM=");
- tempSeq[0] = sal_Int8(5);
- tempSeq[1] = sal_Int8(2);
- tempSeq[2] = sal_Int8(3);
- tempSequence = comphelper::containerToSequence(tempSeq);
- doTestEncodeBase64("BQIDAAABAgM=", tempSequence);
- doTestDecodeBase64(tempSequence, "BQIDAAABAgM=");
- tempSeq[0] = sal_Int8(sal_uInt8(200));
- tempSeq[1] = sal_Int8(31);
- tempSeq[2] = sal_Int8(77);
- tempSeq[3] = sal_Int8(111);
- tempSequence = comphelper::containerToSequence(tempSeq);
- doTestEncodeBase64("yB9NbwABAgM=", tempSequence);
- doTestDecodeBase64(tempSequence, "yB9NbwABAgM=");
+ OStringBuffer aBuffer;
+ uno::Sequence<sal_Int8> inputSequence;
+
+ inputSequence = { 0, 0, 0, 0, 0, 1, 2, 3 };
+ comphelper::Base64::encode(aBuffer, inputSequence);
+ CPPUNIT_ASSERT_EQUAL(OString("AAAAAAABAgM="), OString(aBuffer.makeStringAndClear()));
+
+ inputSequence = { 5, 2, 3, 0, 0, 1, 2, 3 };
+ comphelper::Base64::encode(aBuffer, inputSequence);
+ CPPUNIT_ASSERT_EQUAL(OString("BQIDAAABAgM="), OString(aBuffer.makeStringAndClear()));
+
+ inputSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 };
+ comphelper::Base64::encode(aBuffer, inputSequence);
+ CPPUNIT_ASSERT_EQUAL(OString("yB9NbwABAgM="), OString(aBuffer.makeStringAndClear()));
}
CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test);
diff --git a/comphelper/source/misc/base64.cxx b/comphelper/source/misc/base64.cxx
index e7f20cdfa693..4e3a6bc6ef00 100644
--- a/comphelper/source/misc/base64.cxx
+++ b/comphelper/source/misc/base64.cxx
@@ -21,7 +21,6 @@
#include <com/sun/star/uno/Sequence.hxx>
-#include <rtl/ustrbuf.hxx>
#include <rtl/math.hxx>
#include <sal/log.hxx>
#include <osl/diagnose.h>
@@ -61,7 +60,7 @@ const
// p q r s t u v w x y z
-void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, OUStringBuffer& sBuffer)
+void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const sal_Int32 nFullLen, sal_Char* aCharBuffer)
{
sal_Int32 nLen(nFullLen - nStart);
if (nLen > 3)
@@ -94,24 +93,37 @@ void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const
break;
}
- sal_Unicode buf[] = { '=', '=', '=', '=' };
+ aCharBuffer[0] = aCharBuffer[1] = aCharBuffer[2] = aCharBuffer[3] = '=';
sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinaer & 0xFC0000) >> 18));
- buf[0] = aBase64EncodeTable [nIndex];
+ aCharBuffer[0] = aBase64EncodeTable [nIndex];
nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F000) >> 12);
- buf[1] = aBase64EncodeTable [nIndex];
+ aCharBuffer[1] = aBase64EncodeTable [nIndex];
if (nLen > 1)
{
nIndex = static_cast<sal_uInt8>((nBinaer & 0xFC0) >> 6);
- buf[2] = aBase64EncodeTable [nIndex];
+ aCharBuffer[2] = aBase64EncodeTable [nIndex];
if (nLen > 2)
{
nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F));
- buf[3] = aBase64EncodeTable [nIndex];
+ aCharBuffer[3] = aBase64EncodeTable [nIndex];
}
}
- sBuffer.append(buf, SAL_N_ELEMENTS(buf));
+}
+
+void Base64::encode(OStringBuffer& 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)
+ {
+ sal_Char aCharBuffer[4];
+ ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
+ aStrBuffer.append(aCharBuffer, SAL_N_ELEMENTS(aCharBuffer));
+ i += 3;
+ }
}
void Base64::encode(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& aPass)
@@ -121,7 +133,9 @@ void Base64::encode(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& a
const sal_Int8* pBuffer = aPass.getConstArray();
while (i < nBufferLength)
{
- ThreeByteToFourByte(pBuffer, i, nBufferLength, aStrBuffer);
+ sal_Char aCharBuffer[4];
+ ThreeByteToFourByte(pBuffer, i, nBufferLength, aCharBuffer);
+ aStrBuffer.appendAscii(aCharBuffer, SAL_N_ELEMENTS(aCharBuffer));
i += 3;
}
}