Handle IOException during filter detection

Change-Id: Ie2b8b65f0f2b7b34efbba478a7ccda7ef3719bd6
This commit is contained in:
Stephan Bergmann
2015-09-10 16:39:24 +02:00
parent 90b0a54792
commit 8ac129a59b
2 changed files with 93 additions and 84 deletions

View File

@@ -360,32 +360,36 @@ OUString SAL_CALL SVGFilter::detect(Sequence<PropertyValue>& rDescriptor) throw
if (!xInput.is()) if (!xInput.is())
return OUString(); return OUString();
if (isStreamGZip(xInput)) try {
{ if (isStreamGZip(xInput))
boost::scoped_ptr<SvStream> aStream(utl::UcbStreamHelper::CreateStream(xInput, true )); {
if(!aStream.get()) boost::scoped_ptr<SvStream> aStream(utl::UcbStreamHelper::CreateStream(xInput, true ));
return OUString(); if(!aStream.get())
return OUString();
SvStream* pMemoryStream = new SvMemoryStream; SvStream* pMemoryStream = new SvMemoryStream;
uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY); uno::Reference<io::XSeekable> xSeek(xInput, uno::UNO_QUERY);
if (!xSeek.is()) if (!xSeek.is())
return OUString(); return OUString();
xSeek->seek(0); xSeek->seek(0);
ZCodec aCodec; ZCodec aCodec;
aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, false, true); aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, false, true);
aCodec.Decompress(*aStream.get(), *pMemoryStream); aCodec.Decompress(*aStream.get(), *pMemoryStream);
aCodec.EndCompression(); aCodec.EndCompression();
pMemoryStream->Seek(STREAM_SEEK_TO_BEGIN); pMemoryStream->Seek(STREAM_SEEK_TO_BEGIN);
uno::Reference<io::XInputStream> xDecompressedInput(new utl::OSeekableInputStreamWrapper(pMemoryStream, true)); uno::Reference<io::XInputStream> xDecompressedInput(new utl::OSeekableInputStreamWrapper(pMemoryStream, true));
if (xDecompressedInput.is() && isStreamSvg(xDecompressedInput)) if (xDecompressedInput.is() && isStreamSvg(xDecompressedInput))
return OUString(constFilterName); return OUString(constFilterName);
} }
else else
{ {
if (isStreamSvg(xInput)) if (isStreamSvg(xInput))
return OUString(constFilterName); return OUString(constFilterName);
}
} catch (css::io::IOException & e) {
SAL_WARN("filter", "caught IOException " + e.Message);
} }
return OUString(); return OUString();
} }

View File

@@ -229,73 +229,78 @@ OUString SAL_CALL PDFDetector::detect( uno::Sequence< beans::PropertyValue >& rF
} }
if( xInput.is() ) if( xInput.is() )
{ {
uno::Reference< io::XSeekable > xSeek( xInput, uno::UNO_QUERY );
if( xSeek.is() )
xSeek->seek( 0 );
// read the first 1024 byte (see PDF reference implementation note 12)
const sal_Int32 nHeaderSize = 1024;
uno::Sequence< sal_Int8 > aBuf( nHeaderSize );
sal_uInt64 nBytes = 0;
nBytes = xInput->readBytes( aBuf, nHeaderSize );
if( nBytes > 5 )
{
const sal_Int8* pBytes = aBuf.getConstArray();
for( unsigned int i = 0; i < nBytes-5; i++ )
{
if( pBytes[i] == '%' &&
pBytes[i+1] == 'P' &&
pBytes[i+2] == 'D' &&
pBytes[i+3] == 'F' &&
pBytes[i+4] == '-' )
{
bSuccess = true;
break;
}
}
}
// check for hybrid PDF
oslFileHandle aFile = NULL; oslFileHandle aFile = NULL;
if( bSuccess && try {
( aURL.isEmpty() || !comphelper::isFileUrl(aURL) ) uno::Reference< io::XSeekable > xSeek( xInput, uno::UNO_QUERY );
) if( xSeek.is() )
{ xSeek->seek( 0 );
sal_uInt64 nWritten = 0; // read the first 1024 byte (see PDF reference implementation note 12)
if( osl_createTempFile( NULL, &aFile, &aURL.pData ) != osl_File_E_None ) const sal_Int32 nHeaderSize = 1024;
uno::Sequence< sal_Int8 > aBuf( nHeaderSize );
sal_uInt64 nBytes = 0;
nBytes = xInput->readBytes( aBuf, nHeaderSize );
if( nBytes > 5 )
{ {
bSuccess = false; const sal_Int8* pBytes = aBuf.getConstArray();
} for( unsigned int i = 0; i < nBytes-5; i++ )
else
{
#if OSL_DEBUG_LEVEL > 1
OSL_TRACE( "created temp file %s\n",
OUStringToOString( aURL, RTL_TEXTENCODING_UTF8 ).getStr() );
#endif
osl_writeFile( aFile, aBuf.getConstArray(), nBytes, &nWritten );
OSL_ENSURE( nWritten == nBytes, "writing of header bytes failed" );
if( nWritten == nBytes )
{ {
const sal_uInt32 nBufSize = 4096; if( pBytes[i] == '%' &&
aBuf = uno::Sequence<sal_Int8>(nBufSize); pBytes[i+1] == 'P' &&
// copy the bytes pBytes[i+2] == 'D' &&
do pBytes[i+3] == 'F' &&
pBytes[i+4] == '-' )
{ {
nBytes = xInput->readBytes( aBuf, nBufSize ); bSuccess = true;
if( nBytes > 0 ) break;
{ }
osl_writeFile( aFile, aBuf.getConstArray(), nBytes, &nWritten );
if( nWritten != nBytes )
{
bSuccess = false;
break;
}
}
} while( nBytes == nBufSize );
} }
} }
osl_closeFile( aFile );
// check for hybrid PDF
if( bSuccess &&
( aURL.isEmpty() || !comphelper::isFileUrl(aURL) )
)
{
sal_uInt64 nWritten = 0;
if( osl_createTempFile( NULL, &aFile, &aURL.pData ) != osl_File_E_None )
{
bSuccess = false;
}
else
{
#if OSL_DEBUG_LEVEL > 1
OSL_TRACE( "created temp file %s\n",
OUStringToOString( aURL, RTL_TEXTENCODING_UTF8 ).getStr() );
#endif
osl_writeFile( aFile, aBuf.getConstArray(), nBytes, &nWritten );
OSL_ENSURE( nWritten == nBytes, "writing of header bytes failed" );
if( nWritten == nBytes )
{
const sal_uInt32 nBufSize = 4096;
aBuf = uno::Sequence<sal_Int8>(nBufSize);
// copy the bytes
do
{
nBytes = xInput->readBytes( aBuf, nBufSize );
if( nBytes > 0 )
{
osl_writeFile( aFile, aBuf.getConstArray(), nBytes, &nWritten );
if( nWritten != nBytes )
{
bSuccess = false;
break;
}
}
} while( nBytes == nBufSize );
}
}
osl_closeFile( aFile );
}
} catch (css::io::IOException & e) {
SAL_WARN("sdext.pdfimport", "caught IOException " + e.Message);
return OUString();
} }
OUString aEmbedMimetype; OUString aEmbedMimetype;
xEmbedStream = getAdditionalStream( aURL, aEmbedMimetype, aPwd, m_xContext, rFilterData, false ); xEmbedStream = getAdditionalStream( aURL, aEmbedMimetype, aPwd, m_xContext, rFilterData, false );