1 #define WINVER 0x0501
2 #define BUFSIZE 4096
3 #pragma comment(lib, "Shlwapi.lib")
4 
5 #include <string>
6 #include <iostream>
7 #include <fstream>
8 #include <Shlwapi.h>
9 #include <stdint.h>
10 #include "encoder_format.h"
11 #include <Magick++/Functions.h>
12 using namespace std;
13 
14 extern EncoderFormat encoderFormat;
15 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
16 
17 class FuzzingDebugger
18 {
19 public:
load(wstring fileName)20   bool load(wstring fileName)
21   {
22     ifstream
23       file;
24 
25     streampos
26       size;
27 
28     file = ifstream(fileName, ios::in | ios::binary | ios::ate);
29     if (!file.is_open())
30       return(false);
31 
32     size = file.tellg();
33     if (size < 1)
34       return(false);
35 
36     _size = size;
37     _data = new char[_size];
38     file.seekg(0, ios::beg);
39     file.read(_data, size);
40     file.close();
41 
42     encoderFormat.set(fileName, wstring(PathFindExtension(fileName.c_str())));
43 
44     return(true);
45   }
46 
start()47   void start()
48   {
49     const uint8_t
50       *data;
51 
52     data = reinterpret_cast<const uint8_t *>(_data);
53     LLVMFuzzerTestOneInput(data, _size);
54 
55     delete _data;
56   }
57 
58 
59 private:
60   char * _data;
61   size_t _size;
62 };
63 
wmain(int argc,wchar_t * argv[])64 int wmain(int argc, wchar_t *argv[])
65 {
66   FuzzingDebugger
67     debugger;
68 
69   int
70     debug;
71 
72   wstring
73     fileName;
74 
75   if (argc == 1)
76   {
77     wchar_t
78       fullPath[BUFSIZE],
79       **lppPart;
80 
81     lppPart = NULL;
82     GetFullPathName(argv[0], BUFSIZE, fullPath, lppPart);
83     PathRemoveExtension(fullPath);
84     fileName = wstring(fullPath) + L".input";
85   }
86   else
87     fileName = wstring(argv[1]);
88 
89   debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
90   debug |= _CRTDBG_DELAY_FREE_MEM_DF;
91   debug |= _CRTDBG_LEAK_CHECK_DF;
92   (void) _CrtSetDbgFlag(debug);
93 
94   //_CrtSetBreakAlloc(42);
95 
96   {
97     if (!debugger.load(fileName))
98     {
99       wcerr << L"Unable to load " << fileName;
100       cin.get();
101     }
102     else
103       debugger.start();
104   }
105 
106   Magick::TerminateMagick();
107 
108   _CrtCheckMemory();
109 }
110