• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     _size = size;
34     _data = new char[_size];
35     file.seekg(0, ios::beg);
36     file.read(_data, size);
37     file.close();
38 
39     encoderFormat.set(fileName, wstring(PathFindExtension(fileName.c_str())));
40 
41     return(true);
42   }
43 
start()44   void start()
45   {
46     const uint8_t
47       *data;
48 
49     data = reinterpret_cast<const uint8_t *>(_data);
50     LLVMFuzzerTestOneInput(data, _size);
51 
52     delete _data;
53   }
54 
55 
56 private:
57   char * _data;
58   size_t _size;
59 };
60 
wmain(int argc,wchar_t * argv[])61 int wmain(int argc, wchar_t *argv[])
62 {
63   FuzzingDebugger
64     debugger;
65 
66   int
67     debug;
68 
69   wstring
70     fileName;
71 
72   if (argc == 1)
73   {
74     wchar_t
75       fullPath[BUFSIZE],
76       **lppPart;
77 
78     lppPart = NULL;
79     GetFullPathName(argv[0], BUFSIZE, fullPath, lppPart);
80     PathRemoveExtension(fullPath);
81     fileName = wstring(fullPath) + L".input";
82   }
83   else
84     fileName = wstring(argv[1]);
85 
86   debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
87   debug |= _CRTDBG_DELAY_FREE_MEM_DF;
88   debug |= _CRTDBG_LEAK_CHECK_DF;
89   (void) _CrtSetDbgFlag(debug);
90 
91   //_CrtSetBreakAlloc(42);
92 
93   {
94     if (!debugger.load(fileName))
95     {
96       wcerr << L"Unable to load " << fileName;
97       cin.get();
98     }
99     else
100       debugger.start();
101   }
102 
103   Magick::TerminateMagick();
104 
105   _CrtCheckMemory();
106 }
107