| 1 | /* |
| 2 | Copyright @ 2018 ImageMagick Studio LLC, a non-profit organization |
| 3 | dedicated to making software imaging solutions freely available. |
| 4 | |
| 5 | You may not use this file except in compliance with the License. You may |
| 6 | obtain a copy of the License at |
| 7 | |
| 8 | https://imagemagick.org/license/ |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <windows.h> |
| 18 | #include <fileapi.h> |
| 19 | #include <fstream> |
| 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <Magick++/Functions.h> |
| 23 | |
| 24 | #include "encoder_format.h" |
| 25 | |
| 26 | extern EncoderFormat encoderFormat; |
| 27 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data,size_t Size); |
| 28 | |
| 29 | class FuzzingDebugger |
| 30 | { |
| 31 | public: |
| 32 | bool load(const wstring fileName) |
| 33 | { |
| 34 | ifstream |
| 35 | file; |
| 36 | |
| 37 | streampos |
| 38 | size; |
| 39 | |
| 40 | file=ifstream(fileName,ios::in|ios::binary|ios::ate); |
| 41 | if (!file.is_open()) |
| 42 | return(false); |
| 43 | size=file.tellg(); |
| 44 | if (size < 1) |
| 45 | return(false); |
| 46 | _size=size; |
| 47 | _data=new char[_size]; |
| 48 | file.seekg(0,ios::beg); |
| 49 | file.read(_data,size); |
| 50 | file.close(); |
| 51 | encoderFormat.set(fileName); |
| 52 | return(true); |
| 53 | } |
| 54 | |
| 55 | void start() |
| 56 | { |
| 57 | const uint8_t |
| 58 | *data; |
| 59 | |
| 60 | data=reinterpret_cast<const uint8_t *>(_data); |
| 61 | LLVMFuzzerTestOneInput(data,_size); |
| 62 | delete[] _data; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | private: |
| 67 | char *_data; |
| 68 | size_t _size; |
| 69 | }; |
| 70 | |
| 71 | int wmain(int argc, wchar_t *argv[]) |
| 72 | { |
| 73 | FuzzingDebugger |
| 74 | debugger; |
| 75 | |
| 76 | int |
| 77 | debug; |
| 78 | |
| 79 | wstring |
| 80 | fileName; |
| 81 | |
| 82 | if (argc == 1) |
| 83 | { |
| 84 | wcerr << L"Filename must be specified as the first argument"; |
| 85 | return(1); |
| 86 | } |
| 87 | fileName=wstring(argv[1]); |
| 88 | if (!debugger.load(fileName)) |
| 89 | { |
| 90 | wcerr << L"Unable to load " << fileName; |
| 91 | cin.get(); |
| 92 | return(1); |
| 93 | } |
| 94 | debug=_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); |
| 95 | debug |= _CRTDBG_DELAY_FREE_MEM_DF; |
| 96 | debug |= _CRTDBG_LEAK_CHECK_DF; |
| 97 | (void) _CrtSetDbgFlag(debug); |
| 98 | //_CrtSetBreakAlloc(42); |
| 99 | debugger.start(); |
| 100 | Magick::TerminateMagick(); |
| 101 | _CrtCheckMemory(); |
| 102 | return(0); |
| 103 | } |