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 @@
#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);
......
......@@ -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;
}
}
......
......@@ -23,8 +23,8 @@
#include <comphelper/comphelperdllapi.h>
#include <sal/types.h>
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
#include <rtl/strbuf.hxx>
#include <com/sun/star/uno/Sequence.h>
namespace comphelper {
......@@ -35,6 +35,8 @@ public:
/** encodes the given byte sequence into Base64 */
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
// version can be used for attribute values only, because it does not
// 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