diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2014-06-06 11:12:42 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2014-06-06 11:13:07 +0200 |
commit | 805b57cd5b34454589ad6ce11c16507695fd3ff3 (patch) | |
tree | ba9e4b99bb2650ba8e22d252d84ab319ffff2424 /tools | |
parent | 75e0fe06ac08e55e24ef82de9e4b1102518f4e6f (diff) |
Various INetURLObject::getData() fixes
Change-Id: I5eafd6250bbde7227dcc0447a4280b7741c320de
Diffstat (limited to 'tools')
-rw-r--r-- | tools/qa/cppunit/test_urlobj.cxx | 90 | ||||
-rw-r--r-- | tools/source/fsys/urlobj.cxx | 63 |
2 files changed, 130 insertions, 23 deletions
diff --git a/tools/qa/cppunit/test_urlobj.cxx b/tools/qa/cppunit/test_urlobj.cxx index 5cc1d1bd9762..6cfca9609bdb 100644 --- a/tools/qa/cppunit/test_urlobj.cxx +++ b/tools/qa/cppunit/test_urlobj.cxx @@ -12,6 +12,7 @@ #include <sal/types.h> #include "cppunit/TestFixture.h" #include <cppunit/extensions/HelperMacros.h> +#include <tools/stream.hxx> #include <tools/urlobj.hxx> #define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() ) @@ -243,6 +244,94 @@ namespace tools_urlobj } } + void urlobjTest_data() { + INetURLObject url; + SvMemoryStream * strm; + unsigned char const * buf; + + url = INetURLObject("data:"); + //TODO: CPPUNIT_ASSERT(url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm == 0); + + url = INetURLObject("data:,"); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm != 0); + CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize()); + delete strm; + + url = INetURLObject("data:,,%C3%A4%90"); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm != 0); + CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize()); + buf = static_cast<unsigned char const *>(strm->GetData()); + CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0])); + CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1])); + CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2])); + CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3])); + delete strm; + + url = INetURLObject("data:base64,"); + //TODO: CPPUNIT_ASSERT(url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm == 0); + + url = INetURLObject("data:;base64,"); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm != 0); + CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize()); + delete strm; + + url = INetURLObject("data:;bAsE64,"); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm != 0); + CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize()); + delete strm; + + url = INetURLObject("data:;base64,YWJjCg=="); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm != 0); + CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize()); + buf = static_cast<unsigned char const *>(strm->GetData()); + CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0])); + CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1])); + CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2])); + CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3])); + delete strm; + + url = INetURLObject("data:;base64,YWJjCg="); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm == 0); + + url = INetURLObject("data:;base64,YWJ$Cg=="); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm == 0); + + url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ=="); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm != 0); + CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize()); + buf = static_cast<unsigned char const *>(strm->GetData()); + CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0])); + CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1])); + CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2])); + CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3])); + delete strm; + + url = INetURLObject("http://example.com"); + CPPUNIT_ASSERT(!url.HasError()); + strm = url.getData(); + CPPUNIT_ASSERT(strm == 0); + } + // Change the following lines only, if you add, remove or rename // member functions of the current class, // because these macros are need by auto register mechanism. @@ -256,6 +345,7 @@ namespace tools_urlobj CPPUNIT_TEST( urlobjTest_006 ); CPPUNIT_TEST( urlobjCmisTest ); CPPUNIT_TEST( urlobjTest_emptyPath ); + CPPUNIT_TEST( urlobjTest_data ); CPPUNIT_TEST_SUITE_END( ); }; // class createPool diff --git a/tools/source/fsys/urlobj.cxx b/tools/source/fsys/urlobj.cxx index 08583fb9e4cc..55a5ddd73ccf 100644 --- a/tools/source/fsys/urlobj.cxx +++ b/tools/source/fsys/urlobj.cxx @@ -17,6 +17,10 @@ * the License at http://www.apache.org/licenses/LICENSE-2.0 . */ +#include <sal/config.h> + +#include <boost/checked_delete.hpp> +#include <o3tl/heap_ptr.hxx> #include <tools/urlobj.hxx> #include <tools/debug.hxx> #include <tools/inetmime.hxx> @@ -588,6 +592,21 @@ void INetURLObject::setInvalid() m_aFragment.clear(); } +namespace { + +SvMemoryStream * memoryStream(void const * data, sal_Int32 length) { + o3tl::heap_ptr<char, boost::checked_array_deleter<char> > b( + new char[length]); + memcpy(b.get(), data, length); + o3tl::heap_ptr<SvMemoryStream> s( + new SvMemoryStream(b.get(), length, STREAM_READ)); + s->ObjectOwnsMemory(true); + b.release(); + return s.release(); +} + +} + SvMemoryStream* INetURLObject::getData() { if( GetProtocol() != INET_PROT_DATA ) @@ -596,34 +615,32 @@ SvMemoryStream* INetURLObject::getData() } OUString sURLPath = GetURLPath( DECODE_WITH_CHARSET, RTL_TEXTENCODING_ISO_8859_1 ); - OUString sType, sSubType; - OUString sBase64Enc(";base64,"); - - INetContentTypeParameterList params; - sal_Unicode const * pSkippedMediatype = INetMIME::scanContentType( sURLPath.getStr(), sURLPath.getStr() + sURLPath.getLength(), &sType, &sSubType, ¶ms ); - sal_Int32 nCharactersSkipped = pSkippedMediatype-sURLPath.getStr(); - sal_Int32 nCommaIndex = sURLPath.indexOf( ",", nCharactersSkipped ); - sal_Int32 nBase64Index = sURLPath.indexOf( sBase64Enc, nCharactersSkipped ); - SvMemoryStream* aStream=NULL; - - if( nBase64Index >= 0 && nBase64Index < nCommaIndex ) + sal_Unicode const * pSkippedMediatype = INetMIME::scanContentType( sURLPath.getStr(), sURLPath.getStr() + sURLPath.getLength(), NULL, NULL, NULL ); + sal_Int32 nCharactersSkipped = pSkippedMediatype == NULL + ? 0 : pSkippedMediatype-sURLPath.getStr(); + if (sURLPath.match(",", nCharactersSkipped)) + { + nCharactersSkipped += strlen(","); + OString sURLEncodedData( + sURLPath.getStr() + nCharactersSkipped, + sURLPath.getLength() - nCharactersSkipped, + RTL_TEXTENCODING_ISO_8859_1, OUSTRING_TO_OSTRING_CVTFLAGS); + return memoryStream( + sURLEncodedData.getStr(), sURLEncodedData.getLength()); + } + else if (sURLPath.matchIgnoreAsciiCase(";base64,", nCharactersSkipped)) { - // base64 decoding - OUString sBase64Data = sURLPath.copy( nBase64Index + sBase64Enc.getLength() ); + nCharactersSkipped += strlen(";base64,"); + OUString sBase64Data = sURLPath.copy( nCharactersSkipped ); css::uno::Sequence< sal_Int8 > aDecodedData; - ::sax::Converter::decodeBase64( aDecodedData, sBase64Data ); - if( aDecodedData.hasElements() ) + if (sax::Converter::decodeBase64SomeChars(aDecodedData, sBase64Data) + == sBase64Data.getLength()) { - aStream = new SvMemoryStream( aDecodedData.getArray(), aDecodedData.getLength(), STREAM_READ ); + return memoryStream( + aDecodedData.getArray(), aDecodedData.getLength()); } } - else - { - // URL decoding - OUString sURLEncodedData = sURLPath.copy( nCommaIndex+1 ); - aStream = new SvMemoryStream( const_cast< sal_Char * >(OUStringToOString(sURLEncodedData, RTL_TEXTENCODING_UTF8).getStr()), sURLEncodedData.getLength(), STREAM_READ); - } - return aStream; + return NULL; } namespace unnamed_tools_urlobj { |