From 1663abc706497fb0f56ee0b548f345ff76257520 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Mon, 13 Jun 2011 21:08:00 +0100 Subject: #i49123# implement typedetection for supported .hwp format versions --- hwpfilter/qa/cppunit/data/fail/BK.hwp | Bin 0 -> 21504 bytes hwpfilter/qa/cppunit/data/pass/BK.hwp | Bin 21504 -> 0 bytes hwpfilter/source/hwpfile.cpp | 11 +---- hwpfilter/source/hwpfile.h | 7 +++ hwpfilter/source/hwpreader.hxx | 80 +++++++++++++++++++++++++--------- hwpfilter/source/makefile.mk | 1 + 6 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 hwpfilter/qa/cppunit/data/fail/BK.hwp delete mode 100644 hwpfilter/qa/cppunit/data/pass/BK.hwp (limited to 'hwpfilter') diff --git a/hwpfilter/qa/cppunit/data/fail/BK.hwp b/hwpfilter/qa/cppunit/data/fail/BK.hwp new file mode 100644 index 000000000000..d4da79fc127d Binary files /dev/null and b/hwpfilter/qa/cppunit/data/fail/BK.hwp differ diff --git a/hwpfilter/qa/cppunit/data/pass/BK.hwp b/hwpfilter/qa/cppunit/data/pass/BK.hwp deleted file mode 100644 index d4da79fc127d..000000000000 Binary files a/hwpfilter/qa/cppunit/data/pass/BK.hwp and /dev/null differ diff --git a/hwpfilter/source/hwpfile.cpp b/hwpfilter/source/hwpfile.cpp index 5d8b5f09e910..2b1bde79f7d1 100644 --- a/hwpfilter/source/hwpfile.cpp +++ b/hwpfilter/source/hwpfile.cpp @@ -43,14 +43,9 @@ #include "hcode.h" #include "hstream.h" -#define HWPIDLen 30 #define HWPHeadLen 128 #define HWPSummaryLen 1008 -#define V20SIGNATURE "HWP Document File V2.00 \032\1\2\3\4\5" -#define V21SIGNATURE "HWP Document File V2.10 \032\1\2\3\4\5" -#define V30SIGNATURE "HWP Document File V3.00 \032\1\2\3\4\5" - #define FILESTG_SIGNATURE 0xF8995567 #define FILESTG_SIGNATURE_NORMAL 0xF8995568 @@ -127,8 +122,7 @@ int HWPFile::ReadHwpFile(HStream & stream) return State(); } - -static int hwp_version(char *str) +int detect_hwp_version(const char *str) { if (memcmp(V20SIGNATURE, str, HWPIDLen) == 0) return HWP_V20; @@ -139,7 +133,6 @@ static int hwp_version(char *str) return 0; } - // HIODev wrapper int HWPFile::Open(HStream & stream) @@ -162,7 +155,7 @@ int HWPFile::Open(HStream & stream) char idstr[HWPIDLen]; if (ReadBlock(idstr, HWPIDLen) <= 0 - || HWP_V30 != (version = hwp_version(idstr))) + || HWP_V30 != (version = detect_hwp_version(idstr))) { return SetState(HWP_UNSUPPORTED_VERSION); } diff --git a/hwpfilter/source/hwpfile.h b/hwpfilter/source/hwpfile.h index 52f30829da9c..78cdbab0b99f 100644 --- a/hwpfilter/source/hwpfile.h +++ b/hwpfilter/source/hwpfile.h @@ -44,10 +44,17 @@ #include "hpara.h" #include "list.hxx" +#define HWPIDLen 30 +#define V20SIGNATURE "HWP Document File V2.00 \032\1\2\3\4\5" +#define V21SIGNATURE "HWP Document File V2.10 \032\1\2\3\4\5" +#define V30SIGNATURE "HWP Document File V3.00 \032\1\2\3\4\5" + #define HWP_V20 20 #define HWP_V21 21 #define HWP_V30 30 +int detect_hwp_version(const char *str); + struct FBox; struct EmPicture; struct HyperText; diff --git a/hwpfilter/source/hwpreader.hxx b/hwpfilter/source/hwpreader.hxx index 37e57d42f7f3..d547befeada6 100644 --- a/hwpfilter/source/hwpreader.hxx +++ b/hwpfilter/source/hwpreader.hxx @@ -52,11 +52,12 @@ #include #include #include +#include #include #include #include -#include +#include #include using namespace ::rtl; @@ -69,9 +70,12 @@ using namespace ::com::sun::star::registry; using namespace ::com::sun::star::document; using namespace ::com::sun::star::beans; using namespace ::com::sun::star::xml::sax; +using namespace ::com::sun::star::document; #include +#include + #include "hwpfile.h" #include "hcode.h" #include "hbox.h" @@ -82,8 +86,9 @@ using namespace ::com::sun::star::xml::sax; #define IMPLEMENTATION_NAME "com.sun.comp.hwpimport.HwpImportFilter" -#define SERVICE_NAME "com.sun.star.document.ImportFilter" -#define WRITER_IMPORTER_NAME "com.sun.star.comp.Writer.XMLImporter" +#define SERVICE_NAME1 "com.sun.star.document.ImportFilter" +#define SERVICE_NAME2 "com.sun.star.document.ExtendedTypeDetection" +#define WRITER_IMPORTER_NAME "com.sun.star.comp.Writer.XMLImporter" class MyDataSink : public ::cppu::WeakImplHelper2< XActiveDataControl, XActiveDataSink > { @@ -193,7 +198,7 @@ private: /* --------- Styles Parsing ------------ */ void makePageStyle(); - void makeColumns(ColumnDef *); + void makeColumns(ColumnDef *); void makeTStyle(CharShape *); void makePStyle(ParaShape *); void makeFStyle(FBoxStyle *); @@ -206,7 +211,7 @@ private: char* getPStyleName(int, char *); }; -class HwpImportFilter : public WeakImplHelper3< XFilter, XImporter, XServiceInfo > +class HwpImportFilter : public WeakImplHelper4< XFilter, XImporter, XServiceInfo, XExtendedFilterDetection > { public: HwpImportFilter( const Reference< XMultiServiceFactory > xFact ); @@ -217,18 +222,23 @@ public: static OUString getImplementationName_Static() throw(); public: - // XFilter + // XFilter virtual sal_Bool SAL_CALL filter( const Sequence< PropertyValue >& aDescriptor ) throw( RuntimeException ); virtual void SAL_CALL cancel() throw(RuntimeException); - // XImporter + + // XImporter virtual void SAL_CALL setTargetDocument( const Reference< XComponent >& xDoc) throw( IllegalArgumentException, RuntimeException ); - // XServiceInfo + + // XServiceInfo OUString SAL_CALL getImplementationName() throw (RuntimeException); Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw (::com::sun::star::uno::RuntimeException); sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw (::com::sun::star::uno::RuntimeException); + //XExtendedFilterDetection + virtual OUString SAL_CALL detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rDescriptor ) throw (::com::sun::star::uno::RuntimeException); + public: Reference< XFilter > rFilter; Reference< XImporter > rImporter; @@ -248,6 +258,7 @@ Sequence< OUString > HwpImportFilter::getSupportedServiceNames_Static( void ) th aRet.getArray()[0] = HwpImportFilter::getImplementationName_Static(); return aRet; } + HwpImportFilter::HwpImportFilter( const Reference< XMultiServiceFactory > xFact ) { OUString sService(RTL_CONSTASCII_USTRINGPARAM( WRITER_IMPORTER_NAME )); @@ -258,15 +269,14 @@ HwpImportFilter::HwpImportFilter( const Reference< XMultiServiceFactory > xFact HwpReader *p = new HwpReader; p->setDocumentHandler( xHandler ); - Sequence< Any > aArgs( 2 ); - aArgs[0] <<= OUString(RTL_CONSTASCII_USTRINGPARAM("Local")); - aArgs[1] <<= OUString(RTL_CONSTASCII_USTRINGPARAM("Office")); - Reference< XInterface > xUCB - ( xFact->createInstanceWithArguments - (OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.UniversalContentBroker")), - aArgs)); - p->setUCB( xUCB ); - + Sequence< Any > aArgs( 2 ); + aArgs[0] <<= OUString(RTL_CONSTASCII_USTRINGPARAM("Local")); + aArgs[1] <<= OUString(RTL_CONSTASCII_USTRINGPARAM("Office")); + Reference< XInterface > xUCB + ( xFact->createInstanceWithArguments + (OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.ucb.UniversalContentBroker")), + aArgs)); + p->setUCB( xUCB ); Reference< XImporter > xImporter = Reference< XImporter >( xHandler, UNO_QUERY ); rImporter = xImporter; @@ -314,6 +324,7 @@ OUString HwpImportFilter::getImplementationName() throw(::com::sun::star::uno::R { return OUString(RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME )); } + sal_Bool HwpImportFilter::supportsService( const OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException) { Sequence< OUString > aSNL = getSupportedServiceNames(); @@ -326,11 +337,40 @@ sal_Bool HwpImportFilter::supportsService( const OUString& ServiceName ) throw(: return sal_False; } +//XExtendedFilterDetection +OUString HwpImportFilter::detect( ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >& rDescriptor ) throw (::com::sun::star::uno::RuntimeException) +{ + rtl::OUString sTypeName; + + comphelper::MediaDescriptor aDescriptor(rDescriptor); + aDescriptor.addInputStream(); + + Reference< XInputStream > xInputStream( + aDescriptor[comphelper::MediaDescriptor::PROP_INPUTSTREAM()], UNO_QUERY); + + if (xInputStream.is()) + { + Sequence< sal_Int8 > aData; + sal_Int32 nLen = HWPIDLen; + if ( + nLen == xInputStream->readBytes(aData, nLen) && + detect_hwp_version(reinterpret_cast(aData.getConstArray())) + ) + { + sTypeName = OUString(RTL_CONSTASCII_USTRINGPARAM("writer_MIZI_Hwp_97")); + } + } + + return sTypeName; +} + Sequence< OUString> HwpImportFilter::getSupportedServiceNames( void ) throw(::com::sun::star::uno::RuntimeException) { - Sequence< OUString > seq(1); - seq.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM( SERVICE_NAME )); - return seq; + Sequence < OUString > aRet(2); + OUString* pArray = aRet.getArray(); + pArray[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICE_NAME1)); + pArray[1] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICE_NAME2)); + return aRet; } ///////////////////////////////////////////////////////////////////////////////////// diff --git a/hwpfilter/source/makefile.mk b/hwpfilter/source/makefile.mk index accfe79dfb29..a8e137b6851b 100644 --- a/hwpfilter/source/makefile.mk +++ b/hwpfilter/source/makefile.mk @@ -72,6 +72,7 @@ SLOFILES = \ SHL1TARGET= $(TARGET) SHL1STDLIBS= \ + $(COMPHELPERLIB) \ $(CPPULIB) \ $(CPPUHELPERLIB)\ $(SALLIB) \ -- cgit