# Cppcheck report - LibreOffice: writerperfect/source/writer/WordPerfectImportFilter.cxx

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393

    /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
    /*
     * This Source Code Form is subject to the terms of the Mozilla Public
     * License, v. 2.0. If a copy of the MPL was not distributed with this
     * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     */
    /* "This product is not manufactured, approved, or supported by
     * Corel Corporation or Corel Corporation Limited."
     */

    #include <osl/diagnose.h>
    #include <rtl/tencinfo.h>

    #include <com/sun/star/io/XInputStream.hpp>
    #include <com/sun/star/xml/sax/XAttributeList.hpp>
    #include <com/sun/star/xml/sax/XDocumentHandler.hpp>
    #include <com/sun/star/xml/sax/InputSource.hpp>
    #include <com/sun/star/xml/sax/XParser.hpp>
    #include <com/sun/star/io/XSeekable.hpp>
    #include <com/sun/star/uno/Reference.h>
    #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
    #include <cppuhelper/supportsservice.hxx>

    #include <writerperfect/DocumentHandler.hxx>
    #include <writerperfect/WPXSvInputStream.hxx>

    #include <xmloff/attrlist.hxx>
    #include <sfx2/passwd.hxx>
    #include <ucbhelper/content.hxx>

    #include <libodfgen/libodfgen.hxx>
    #include <libwpd/libwpd.h>
    #include <libwpg/libwpg.h>

    #include "WordPerfectImportFilter.hxx"

    using ::ucbhelper::Content;
    using com::sun::star::uno::Sequence;
    using com::sun::star::uno::Reference;
    using com::sun::star::uno::Any;
    using com::sun::star::uno::UNO_QUERY;
    using com::sun::star::uno::XInterface;
    using com::sun::star::uno::Exception;
    using com::sun::star::uno::RuntimeException;
    using com::sun::star::uno::XComponentContext;
    using com::sun::star::beans::PropertyValue;
    using com::sun::star::document::XFilter;
    using com::sun::star::document::XExtendedFilterDetection;
    using com::sun::star::ucb::XCommandEnvironment;

    using com::sun::star::io::XInputStream;
    using com::sun::star::document::XImporter;
    using com::sun::star::xml::sax::InputSource;
    using com::sun::star::xml::sax::XAttributeList;
    using com::sun::star::xml::sax::XDocumentHandler;
    using com::sun::star::xml::sax::XParser;

    using writerperfect::DocumentHandler;
    using writerperfect::WPXSvInputStream;

    static bool handleEmbeddedWPGObject(const librevenge::RVNGBinaryData &data, OdfDocumentHandler *pHandler,  const OdfStreamType streamType)
    {
        OdgGenerator exporter;
        exporter.addDocumentHandler(pHandler, streamType);

        libwpg::WPGFileFormat fileFormat = libwpg::WPG_AUTODETECT;

        if (!libwpg::WPGraphics::isSupported(data.getDataStream()))
            fileFormat = libwpg::WPG_WPG1;

        return libwpg::WPGraphics::parse(data.getDataStream(), &exporter, fileFormat);
    }

    static bool handleEmbeddedWPGImage(const librevenge::RVNGBinaryData &input, librevenge::RVNGBinaryData &output)
    {
        libwpg::WPGFileFormat fileFormat = libwpg::WPG_AUTODETECT;

        if (!libwpg::WPGraphics::isSupported(input.getDataStream()))
            fileFormat = libwpg::WPG_WPG1;

        librevenge::RVNGStringVector svgOutput;
        librevenge::RVNGSVGDrawingGenerator aSVGGenerator(svgOutput, "");

        if (!libwpg::WPGraphics::parse(input.getDataStream(), &aSVGGenerator, fileFormat))
            return false;

        assert(1 == svgOutput.size());

        output.clear();
        output.append(reinterpret_cast<const unsigned char *>(svgOutput[0].cstr()), svgOutput[0].size());
        return true;
    }

    bool SAL_CALL WordPerfectImportFilter::importImpl(const Sequence< ::com::sun::star::beans::PropertyValue > &aDescriptor)
    throw (RuntimeException, std::exception)
    {
        sal_Int32 nLength = aDescriptor.getLength();
        const PropertyValue *pValue = aDescriptor.getConstArray();
        Reference < XInputStream > xInputStream;
        for (sal_Int32 i = 0 ; i < nLength; i++)
        {
            if (pValue[i].Name == "InputStream")
                pValue[i].Value >>= xInputStream;
        }
        if (!xInputStream.is())
        {
            OSL_ASSERT(false);
            return false;
        }

        WPXSvInputStream input(xInputStream);

        OString aUtf8Passwd;

        libwpd::WPDConfidence confidence = libwpd::WPDocument::isFileFormatSupported(&input);

        if (libwpd::WPD_CONFIDENCE_SUPPORTED_ENCRYPTION == confidence)
        {
            int unsuccessfulAttempts = 0;
            while (true)
            {
                SfxPasswordDialog aPasswdDlg(0);
                aPasswdDlg.SetMinLen(0);
                if (!aPasswdDlg.Execute())
                    return false;
                OUString aPasswd = aPasswdDlg.GetPassword();
                aUtf8Passwd = OUStringToOString(aPasswd, RTL_TEXTENCODING_UTF8);
                if (libwpd::WPD_PASSWORD_MATCH_OK == libwpd::WPDocument::verifyPassword(&input, aUtf8Passwd.getStr()))
                    break;
                else
                    unsuccessfulAttempts++;
                if (unsuccessfulAttempts == 3) // timeout after 3 password atempts
                    return false;
            }
        }

        // An XML import service: what we push sax messages to..
        Reference < XDocumentHandler > xInternalHandler(
            mxContext->getServiceManager()->createInstanceWithContext(
                "com.sun.star.comp.Writer.XMLOasisImporter", mxContext),
            css::uno::UNO_QUERY_THROW);

        // The XImporter sets up an empty target document for XDocumentHandler to write to..
        Reference < XImporter > xImporter(xInternalHandler, UNO_QUERY);
        xImporter->setTargetDocument(mxDoc);

        // OO Document Handler: abstract class to handle document SAX messages, concrete implementation here
        // writes to in-memory target doc
        DocumentHandler xHandler(xInternalHandler);

        OdtGenerator collector;
        collector.addDocumentHandler(&xHandler, ODF_FLAT_XML);
        collector.registerEmbeddedObjectHandler("image/x-wpg", &handleEmbeddedWPGObject);
        collector.registerEmbeddedImageHandler("image/x-wpg", &handleEmbeddedWPGImage);
        if (libwpd::WPD_OK == libwpd::WPDocument::parse(&input, &collector, aUtf8Passwd.isEmpty() ? 0 : aUtf8Passwd.getStr()))
            return true;
        return false;
    }

    sal_Bool SAL_CALL WordPerfectImportFilter::filter(const Sequence< ::com::sun::star::beans::PropertyValue > &aDescriptor)
    throw (RuntimeException, std::exception)
    {
        return importImpl(aDescriptor);
    }
    void SAL_CALL WordPerfectImportFilter::cancel()
    throw (RuntimeException, std::exception)
    {
    }

    // XImporter
    void SAL_CALL WordPerfectImportFilter::setTargetDocument(const Reference< ::com::sun::star::lang::XComponent > &xDoc)
    throw (::com::sun::star::lang::IllegalArgumentException, RuntimeException, std::exception)
    {
        mxDoc = xDoc;
    }

    // XExtendedFilterDetection
    OUString SAL_CALL WordPerfectImportFilter::detect(Sequence< PropertyValue > &Descriptor)
    throw(RuntimeException, std::exception)
    {
        libwpd::WPDConfidence confidence = libwpd::WPD_CONFIDENCE_NONE;
        OUString sTypeName;
        sal_Int32 nLength = Descriptor.getLength();
        sal_Int32 location = nLength;
        const PropertyValue *pValue = Descriptor.getConstArray();
        Reference < XInputStream > xInputStream;
        for (sal_Int32 i = 0 ; i < nLength; i++)
        {
            if (pValue[i].Name == "TypeName")
                location=i;
            else if (pValue[i].Name == "InputStream")
                pValue[i].Value >>= xInputStream;
        }

        if (!xInputStream.is())
            return OUString();

        WPXSvInputStream input(xInputStream);

        confidence = libwpd::WPDocument::isFileFormatSupported(&input);

        if (confidence == libwpd::WPD_CONFIDENCE_EXCELLENT || confidence == libwpd::WPD_CONFIDENCE_SUPPORTED_ENCRYPTION)
            sTypeName = "writer_WordPerfect_Document";

        if (!sTypeName.isEmpty())
        {
            if (location == nLength)
            {
                Descriptor.realloc(nLength+1);
                Descriptor[location].Name = "TypeName";
            }

            Descriptor[location].Value <<=sTypeName;
        }

        return sTypeName;
    }

    // XInitialization
    void SAL_CALL WordPerfectImportFilter::initialize(const Sequence< Any > &aArguments)
    throw (Exception, RuntimeException, std::exception)
    {
        Sequence < PropertyValue > aAnySeq;
        sal_Int32 nLength = aArguments.getLength();
        if (nLength && (aArguments[0] >>= aAnySeq))
        {
            const PropertyValue *pValue = aAnySeq.getConstArray();
            nLength = aAnySeq.getLength();
            for (sal_Int32 i = 0 ; i < nLength; i++)
            {
                if (pValue[i].Name == "Type")
                {
                    pValue[i].Value >>= msFilterName;
                    break;
                }
            }
        }
    }
    OUString WordPerfectImportFilter_getImplementationName()
    throw (RuntimeException)
    {
        return OUString("com.sun.star.comp.Writer.WordPerfectImportFilter");
    }

    Sequence< OUString > SAL_CALL WordPerfectImportFilter_getSupportedServiceNames()
    throw (RuntimeException)
    {
        Sequence < OUString > aRet(2);
        OUString *pArray = aRet.getArray();
        pArray[0] =  "com.sun.star.document.ImportFilter";
        pArray[1] =  "com.sun.star.document.ExtendedTypeDetection";
        return aRet;
    }

    Reference< XInterface > SAL_CALL WordPerfectImportFilter_createInstance(const Reference< XComponentContext > &rContext)
    throw(Exception)
    {
        return (cppu::OWeakObject *) new WordPerfectImportFilter(rContext);
    }

    // XServiceInfo
    OUString SAL_CALL WordPerfectImportFilter::getImplementationName()
    throw (RuntimeException, std::exception)
    {
        return WordPerfectImportFilter_getImplementationName();
    }
    sal_Bool SAL_CALL WordPerfectImportFilter::supportsService(const OUString &rServiceName)
    throw (RuntimeException, std::exception)
    {
        return cppu::supportsService(this, rServiceName);
    }
    Sequence< OUString > SAL_CALL WordPerfectImportFilter::getSupportedServiceNames()
    throw (RuntimeException, std::exception)
    {
        return WordPerfectImportFilter_getSupportedServiceNames();
    }

    WordPerfectImportFilterDialog::WordPerfectImportFilterDialog(const Reference< XComponentContext > &rContext) :
        mxContext(rContext) {}

    WordPerfectImportFilterDialog::~WordPerfectImportFilterDialog()
    {
    }

    void SAL_CALL WordPerfectImportFilterDialog::setTitle(const OUString &)
    throw (RuntimeException, std::exception)
    {
    }

    sal_Int16 SAL_CALL WordPerfectImportFilterDialog::execute()
    throw (RuntimeException, std::exception)
    {
        WPXSvInputStream input(mxInputStream);

        OString aUtf8Passwd;

        libwpd::WPDConfidence confidence = libwpd::WPDocument::isFileFormatSupported(&input);

        if (libwpd::WPD_CONFIDENCE_SUPPORTED_ENCRYPTION == confidence)
        {
            int unsuccessfulAttempts = 0;
            while (true)
            {
                SfxPasswordDialog aPasswdDlg(0);
                aPasswdDlg.SetMinLen(0);
                if (!aPasswdDlg.Execute())
                    return com::sun::star::ui::dialogs::ExecutableDialogResults::CANCEL;
                msPassword = aPasswdDlg.GetPassword().getStr();
                aUtf8Passwd = OUStringToOString(msPassword, RTL_TEXTENCODING_UTF8);
                if (libwpd::WPD_PASSWORD_MATCH_OK == libwpd::WPDocument::verifyPassword(&input, aUtf8Passwd.getStr()))
                    break;
                else
                    unsuccessfulAttempts++;
                if (unsuccessfulAttempts == 3) // timeout after 3 password atempts
                    return com::sun::star::ui::dialogs::ExecutableDialogResults::CANCEL;
            }
        }
        return com::sun::star::ui::dialogs::ExecutableDialogResults::OK;
    }

    Sequence<PropertyValue> SAL_CALL WordPerfectImportFilterDialog::getPropertyValues() throw(RuntimeException, std::exception)
    {
        Sequence<PropertyValue> aRet(1);
        PropertyValue *pArray = aRet.getArray();

        pArray[0].Name = "Password";
        pArray[0].Value <<= msPassword;

        return aRet;
    }

    void SAL_CALL WordPerfectImportFilterDialog::setPropertyValues(const Sequence<PropertyValue> &aProps)
    throw(com::sun::star::beans::UnknownPropertyException, com::sun::star::beans::PropertyVetoException,
          com::sun::star::lang::IllegalArgumentException, com::sun::star::lang::WrappedTargetException, RuntimeException, std::exception)
    {
        const PropertyValue *pPropArray = aProps.getConstArray();
        long nPropCount = aProps.getLength();
        for (long i = 0; i < nPropCount; i++)
        {
            const PropertyValue &rProp = pPropArray[i];
            OUString aPropName = rProp.Name;

            if (aPropName == "Password")
                rProp.Value >>= msPassword;
            else if (aPropName == "InputStream")
                rProp.Value >>= mxInputStream;
        }
    }

    // XServiceInfo
    OUString SAL_CALL WordPerfectImportFilterDialog::getImplementationName()
    throw (RuntimeException, std::exception)
    {
        return WordPerfectImportFilterDialog_getImplementationName();
    }

    sal_Bool SAL_CALL WordPerfectImportFilterDialog::supportsService(const OUString &rServiceName)
    throw (RuntimeException, std::exception)
    {
        return cppu::supportsService(this, rServiceName);
    }

    Sequence< OUString > SAL_CALL WordPerfectImportFilterDialog::getSupportedServiceNames()
    throw (RuntimeException, std::exception)
    {
        return WordPerfectImportFilterDialog_getSupportedServiceNames();
    }

    OUString WordPerfectImportFilterDialog_getImplementationName()
    throw (RuntimeException)
    {
        return OUString("com.sun.star.comp.Writer.WordPerfectImportFilterDialog");
    }

    Sequence< OUString > SAL_CALL WordPerfectImportFilterDialog_getSupportedServiceNames()
    throw (RuntimeException)
    {
        Sequence < OUString > aRet(1);
        OUString *pArray = aRet.getArray();
        pArray[0] = "com.sun.star.ui.dialogs.FilterOptionsDialog";
        return aRet;
    }

    Reference< XInterface > SAL_CALL WordPerfectImportFilterDialog_createInstance(const Reference< XComponentContext > &rContext)<--- The function 'WordPerfectImportFilterDialog_createInstance' is never used.
    throw(Exception)
    {
        return (cppu::OWeakObject *) new WordPerfectImportFilterDialog(rContext);
    }

    /* vim:set shiftwidth=4 softtabstop=4 expandtab: */