Kaydet (Commit) 0d0bf413 authored tarafından Tomaž Vajngerl's avatar Tomaž Vajngerl Kaydeden (comit) Tomaž Vajngerl

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: 's avatarTomaž Vajngerl <quikee@gmail.com>
üst fc434c8f
...@@ -36,67 +36,77 @@ ...@@ -36,67 +36,77 @@
#include <comphelper/base64.hxx> #include <comphelper/base64.hxx>
#include <sal/log.hxx> #include <sal/log.hxx>
using namespace css;
using namespace ::com::sun::star; using namespace css::util;
using namespace ::com::sun::star::util;
namespace { namespace {
class Base64Test class Base64Test : public CppUnit::TestFixture
: public ::CppUnit::TestFixture
{ {
public: public:
void testBase64(); void testBase64Encode();
void testBase64Decode();
void testBase64EncodeForOStringBuffer();
CPPUNIT_TEST_SUITE(Base64Test); CPPUNIT_TEST_SUITE(Base64Test);
CPPUNIT_TEST(testBase64); CPPUNIT_TEST(testBase64Encode);
CPPUNIT_TEST(testBase64Decode);
CPPUNIT_TEST(testBase64EncodeForOStringBuffer);
CPPUNIT_TEST_SUITE_END(); 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) inputSequence = { 5, 2, 3, 0, 0, 1, 2, 3 };
{ comphelper::Base64::encode(aBuffer, inputSequence);
OUString const is(OUString::createFromAscii(pis)); CPPUNIT_ASSERT_EQUAL(OUString("BQIDAAABAgM="), aBuffer.makeStringAndClear());
OUStringBuffer buf;
comphelper::Base64::encode(buf, aPass); inputSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 };
SAL_INFO("sax.cppunit","" << buf.toString()); comphelper::Base64::encode(aBuffer, inputSequence);
CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear()); 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> decodedSequence;
uno::Sequence< sal_Int8 > tempSequence;
comphelper::Base64::decode(tempSequence, is); uno::Sequence<sal_Int8> expectedSequence = { 0, 0, 0, 0, 0, 1, 2, 3 };
SAL_INFO("sax.cppunit","" << is); comphelper::Base64::decode(decodedSequence, "AAAAAAABAgM=");
bool b = (tempSequence==aPass); CPPUNIT_ASSERT(std::equal(expectedSequence.begin(), expectedSequence.end(), decodedSequence.begin()));
CPPUNIT_ASSERT(b);
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 }; OStringBuffer aBuffer;
uno::Sequence< sal_Int8 > tempSequence = comphelper::containerToSequence(tempSeq); uno::Sequence<sal_Int8> inputSequence;
doTestEncodeBase64("AAAAAAABAgM=", tempSequence);
doTestDecodeBase64(tempSequence, "AAAAAAABAgM="); inputSequence = { 0, 0, 0, 0, 0, 1, 2, 3 };
tempSeq[0] = sal_Int8(5); comphelper::Base64::encode(aBuffer, inputSequence);
tempSeq[1] = sal_Int8(2); CPPUNIT_ASSERT_EQUAL(OString("AAAAAAABAgM="), OString(aBuffer.makeStringAndClear()));
tempSeq[2] = sal_Int8(3);
tempSequence = comphelper::containerToSequence(tempSeq); inputSequence = { 5, 2, 3, 0, 0, 1, 2, 3 };
doTestEncodeBase64("BQIDAAABAgM=", tempSequence); comphelper::Base64::encode(aBuffer, inputSequence);
doTestDecodeBase64(tempSequence, "BQIDAAABAgM="); CPPUNIT_ASSERT_EQUAL(OString("BQIDAAABAgM="), OString(aBuffer.makeStringAndClear()));
tempSeq[0] = sal_Int8(sal_uInt8(200));
tempSeq[1] = sal_Int8(31); inputSequence = { sal_Int8(sal_uInt8(200)), 31, 77, 111, 0, 1, 2, 3 };
tempSeq[2] = sal_Int8(77); comphelper::Base64::encode(aBuffer, inputSequence);
tempSeq[3] = sal_Int8(111); CPPUNIT_ASSERT_EQUAL(OString("yB9NbwABAgM="), OString(aBuffer.makeStringAndClear()));
tempSequence = comphelper::containerToSequence(tempSeq);
doTestEncodeBase64("yB9NbwABAgM=", tempSequence);
doTestDecodeBase64(tempSequence, "yB9NbwABAgM=");
} }
CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test); CPPUNIT_TEST_SUITE_REGISTRATION(Base64Test);
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include <com/sun/star/uno/Sequence.hxx> #include <com/sun/star/uno/Sequence.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/math.hxx> #include <rtl/math.hxx>
#include <sal/log.hxx> #include <sal/log.hxx>
#include <osl/diagnose.h> #include <osl/diagnose.h>
...@@ -61,7 +60,7 @@ const ...@@ -61,7 +60,7 @@ const
// p q r s t u v w x y z // 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); sal_Int32 nLen(nFullLen - nStart);
if (nLen > 3) if (nLen > 3)
...@@ -94,24 +93,37 @@ void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const ...@@ -94,24 +93,37 @@ void ThreeByteToFourByte(const sal_Int8* pBuffer, const sal_Int32 nStart, const
break; break;
} }
sal_Unicode buf[] = { '=', '=', '=', '=' }; aCharBuffer[0] = aCharBuffer[1] = aCharBuffer[2] = aCharBuffer[3] = '=';
sal_uInt8 nIndex (static_cast<sal_uInt8>((nBinaer & 0xFC0000) >> 18)); 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); nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F000) >> 12);
buf[1] = aBase64EncodeTable [nIndex]; aCharBuffer[1] = aBase64EncodeTable [nIndex];
if (nLen > 1) if (nLen > 1)
{ {
nIndex = static_cast<sal_uInt8>((nBinaer & 0xFC0) >> 6); nIndex = static_cast<sal_uInt8>((nBinaer & 0xFC0) >> 6);
buf[2] = aBase64EncodeTable [nIndex]; aCharBuffer[2] = aBase64EncodeTable [nIndex];
if (nLen > 2) if (nLen > 2)
{ {
nIndex = static_cast<sal_uInt8>((nBinaer & 0x3F)); 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) 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 ...@@ -121,7 +133,9 @@ void Base64::encode(OUStringBuffer& aStrBuffer, const uno::Sequence<sal_Int8>& a
const sal_Int8* pBuffer = aPass.getConstArray(); const sal_Int8* pBuffer = aPass.getConstArray();
while (i < nBufferLength) 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; i += 3;
} }
} }
......
...@@ -23,8 +23,8 @@ ...@@ -23,8 +23,8 @@
#include <comphelper/comphelperdllapi.h> #include <comphelper/comphelperdllapi.h>
#include <sal/types.h> #include <sal/types.h>
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx> #include <rtl/ustrbuf.hxx>
#include <rtl/strbuf.hxx>
#include <com/sun/star/uno/Sequence.h> #include <com/sun/star/uno/Sequence.h>
namespace comphelper { namespace comphelper {
...@@ -35,6 +35,8 @@ public: ...@@ -35,6 +35,8 @@ public:
/** encodes the given byte sequence into Base64 */ /** encodes the given byte sequence into Base64 */
static void encode(OUStringBuffer& aStrBuffer, const css::uno::Sequence<sal_Int8>& aPass); static void encode(OUStringBuffer& aStrBuffer, const css::uno::Sequence<sal_Int8>& aPass);
static void encode(OStringBuffer& aStrBuffer, const css::uno::Sequence<sal_Int8>& aPass);
// Decode a base 64 encoded string into a sequence of bytes. The first // Decode a base 64 encoded string into a sequence of bytes. The first
// version can be used for attribute values only, because it does not // version can be used for attribute values only, because it does not
// return any chars left from conversion. // return any chars left from conversion.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment