summaryrefslogtreecommitdiff
path: root/vcl/workben/fftester.cxx
blob: d967541c012aaa9f836c3a9e4882d4cd6caa5ec2 (plain)
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * 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 file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

 /* e.g.
        export CC=afl-clang-fast
        export CXX=afl-clang-fast++
        make
        cp workdir/LinkTarget/Executable/fftester instdir/program
        LD_LIBRARY_PATH=`pwd`/instdir/program SAL_USE_VCLPLUGIN=svp AFL_PERSISTENT=1 afl-fuzz -t 50 -i ~/fuzz/in.png -o ~/fuzz/out.png -d -T png -m 50000000 instdir/program/fftester @@ png

        On slower file formats like .doc you can probably drop the -t and rely on the
        estimations, on faster file formats ironically not specifing a timeout will
        result in a hilarious dramatic falloff in performance from thousands per second
        to teens per second as tiny variations from the initial calculated
        timeout will trigger a shutdown of the fftester and a restart and the
        startup time is woeful (hence the AFL_PERSISTENT mode in the first place)
  */

#include <sal/main.h>
#include <tools/extendapplicationenvironment.hxx>

#include <cppuhelper/bootstrap.hxx>
#include <comphelper/processfactory.hxx>

#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <unotools/configmgr.hxx>
#include <vcl/dibtools.hxx>
#include <vcl/event.hxx>
#include <vcl/graphicfilter.hxx>
#include <vcl/pngread.hxx>
#include <vcl/svapp.hxx>
#include <vcl/wmf.hxx>
#include <vcl/wrkwin.hxx>
#include <vcl/fltcall.hxx>
#include <osl/file.hxx>
#include <unistd.h>
#include <signal.h>

#include <../source/filter/igif/gifread.hxx>
#include <../source/filter/ixbm/xbmread.hxx>
#include <../source/filter/ixpm/xpmread.hxx>
#include <../source/filter/jpeg/jpeg.hxx>

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace cppu;

#ifndef DISABLE_DYNLOADING
extern "C" { static void SAL_CALL thisModule() {} }
#endif

typedef bool (*WFilterCall)(const OUString &rUrl, const OUString &rFlt);
typedef bool (*HFilterCall)(const OUString &rUrl);

/* This constant specifies the number of inputs to process before restarting.
 * This is optional, but helps limit the impact of memory leaks and similar
 * hiccups. */

#define PERSIST_MAX 1000
static unsigned int persist_cnt;

SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
{
    int ret = -1;
    try
    {
        if (argc < 3)
        {
            fprintf(stderr, "Usage: fftester <filename> <wmf|jpg>\n");
            return -1;
        }

        OUString in(argv[1], strlen(argv[1]), RTL_TEXTENCODING_UTF8);
        OUString out;
        osl::File::getFileURLFromSystemPath(in, out);

        tools::extendApplicationEnvironment();

        Reference< XComponentContext > xContext = defaultBootstrap_InitialComponentContext();
        Reference< XMultiServiceFactory > xServiceManager( xContext->getServiceManager(), UNO_QUERY );
        if( !xServiceManager.is() )
            Application::Abort( "Failed to bootstrap" );
        comphelper::setProcessServiceFactory( xServiceManager );
        utl::ConfigManager::EnableAvoidConfig();
        InitVCL();

        setenv("SAL_WMF_COMPLEXCLIP_VIA_REGION", "1", 1);

try_again:

        {
            if (strcmp(argv[2], "wmf") == 0 || strcmp(argv[2], "emf") == 0)
            {
                GDIMetaFile aGDIMetaFile;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) ReadWindowMetafile(aFileStream, aGDIMetaFile);
                BitmapEx aTarget;
                (void)aGDIMetaFile.CreateThumbnail(aTarget);
            }
            else if (strcmp(argv[2], "jpg") == 0)
            {
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) ImportJPEG(aFileStream, aGraphic, nullptr, GraphicFilterImportFlags::NONE);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "gif") == 0)
            {
                SvFileStream aFileStream(out, StreamMode::READ);
                Graphic aGraphic;
                ret = (int) ImportGIF(aFileStream, aGraphic);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "xbm") == 0)
            {
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) ImportXBM(aFileStream, aGraphic);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "xpm") == 0)
            {
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) ImportXPM(aFileStream, aGraphic);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "png") == 0)
            {
                SvFileStream aFileStream(out, StreamMode::READ);
                vcl::PNGReader aReader(aFileStream);
                BitmapEx aTarget = aReader.Read();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "bmp") == 0)
            {
                Bitmap aTarget;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) ReadDIB(aTarget, aFileStream, true);
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "svm") == 0)
            {
                GDIMetaFile aGDIMetaFile;
                SvFileStream aFileStream(out, StreamMode::READ);
                ReadGDIMetaFile(aFileStream, aGDIMetaFile);
            }
#ifndef DISABLE_DYNLOADING
            else if (strcmp(argv[2], "pcd") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("icdGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "dxf") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("idxGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "met") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("imeGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if ((strcmp(argv[2], "pbm") == 0) || strcmp(argv[2], "ppm") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("ipbGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "psd") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("ipdGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "eps") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("ipsGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "pct") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("iptGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "pcx") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("ipxGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "ras") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("iraGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "tga") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("itgGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if (strcmp(argv[2], "tif") == 0)
            {
                static PFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libgielo.so");
                    pfnImport = reinterpret_cast<PFilterCall>(
                        aLibrary.getFunctionSymbol("itiGraphicImport"));
                    aLibrary.release();
                }
                Graphic aGraphic;
                SvFileStream aFileStream(out, StreamMode::READ);
                ret = (int) (*pfnImport)(aFileStream, aGraphic, nullptr);
                BitmapEx aTarget = aGraphic.GetBitmapEx();
                aTarget.Convert(BMP_CONVERSION_24BIT);
            }
            else if ( (strcmp(argv[2], "doc") == 0) ||
                      (strcmp(argv[2], "ww8") == 0) ||
                      (strcmp(argv[2], "ww6") == 0) ||
                      (strcmp(argv[2], "ww2") == 0) )
            {
                static WFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libmswordlo.so", SAL_LOADMODULE_LAZY);
                    pfnImport = reinterpret_cast<WFilterCall>(
                        aLibrary.getFunctionSymbol("TestImportDOC"));
                    aLibrary.release();
                }
                if (strcmp(argv[2], "ww6") == 0)
                    ret = (int) (*pfnImport)(out, OUString("CWW6"));
                else if (strcmp(argv[2], "ww2") == 0)
                    ret = (int) (*pfnImport)(out, OUString("WW6"));
                else
                    ret = (int) (*pfnImport)(out, OUString("CWW8"));
            }
            else if (strcmp(argv[2], "rtf") == 0)
            {
                static HFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libmswordlo.so", SAL_LOADMODULE_LAZY);
                    pfnImport = reinterpret_cast<HFilterCall>(
                        aLibrary.getFunctionSymbol("TestImportRTF"));
                    aLibrary.release();
                }
                ret = (int) (*pfnImport)(out);
            }
            else if ( (strcmp(argv[2], "xls") == 0) ||
                      (strcmp(argv[2], "wb2") == 0) )
            {
                static WFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libscfiltlo.so", SAL_LOADMODULE_LAZY);
                    pfnImport = reinterpret_cast<WFilterCall>(
                        aLibrary.getFunctionSymbol("TestImportSpreadsheet"));
                    aLibrary.release();
                }
                ret = (int) (*pfnImport)(out, OUString(argv[2], strlen(argv[2]), RTL_TEXTENCODING_UTF8));
            }
            else if (strcmp(argv[2], "hwp") == 0)
            {
                static HFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libhwplo.so", SAL_LOADMODULE_LAZY);
                    pfnImport = reinterpret_cast<HFilterCall>(
                        aLibrary.getFunctionSymbol("TestImportHWP"));
                    aLibrary.release();
                }
                ret = (int) (*pfnImport)(out);
            }
            else if (strcmp(argv[2], "602") == 0)
            {
                static HFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libt602filterlo.so", SAL_LOADMODULE_LAZY);
                    pfnImport = reinterpret_cast<HFilterCall>(
                        aLibrary.getFunctionSymbol("TestImport602"));
                    aLibrary.release();
                }
                ret = (int) (*pfnImport)(out);
            }
            else if (strcmp(argv[2], "lwp") == 0)
            {
                static HFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "liblwpftlo.so", SAL_LOADMODULE_LAZY);
                    pfnImport = reinterpret_cast<HFilterCall>(
                        aLibrary.getFunctionSymbol("TestImportLWP"));
                    aLibrary.release();
                }
                ret = (int) (*pfnImport)(out);
            }
            else if (strcmp(argv[2], "ppt") == 0)
            {
                static HFilterCall pfnImport(nullptr);
                if (!pfnImport)
                {
                    osl::Module aLibrary;
                    aLibrary.loadRelative(&thisModule, "libsdfiltlo.so", SAL_LOADMODULE_LAZY);
                    pfnImport = reinterpret_cast<HFilterCall>(
                        aLibrary.getFunctionSymbol("TestImportPPT"));
                    aLibrary.release();
                }
                ret = (int) (*pfnImport)(out);
            }
#endif
        }

        /* To signal successful completion of a run, we need to deliver
           SIGSTOP to our own process, then loop to the very beginning
           once we're resumed by the supervisor process. We do this only
           if AFL_PERSISTENT is set to retain normal behavior when the
           program is executed directly; and take note of PERSIST_MAX. */
        if (getenv("AFL_PERSISTENT") && persist_cnt++ < PERSIST_MAX)
        {
            raise(SIGSTOP);
            goto try_again;
        }

        /* If AFL_PERSISTENT not set or PERSIST_MAX exceeded, exit normally. */
    }
    catch (...)
    {
        abort();
    }

    _exit(ret);
}

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