| 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 <cstdint> |
| 18 | #include <cstring> |
| 19 | |
| 20 | #include <Magick++/Blob.h> |
| 21 | #include <Magick++/Image.h> |
| 22 | |
| 23 | #include "utils.cc" |
| 24 | |
| 25 | #define FUZZ_ENCODER_STRING_LITERAL_X(name) FUZZ_ENCODER_STRING_LITERAL(name) |
| 26 | #define FUZZ_ENCODER_STRING_LITERAL(name) #name |
| 27 | |
| 28 | #ifndef FUZZ_ENCODER |
| 29 | #define FUZZ_ENCODER FUZZ_ENCODER_STRING_LITERAL_X(FUZZ_IMAGEMAGICK_ENCODER) |
| 30 | #endif |
| 31 | |
| 32 | #ifndef FUZZ_IMAGEMAGICK_INITIALIZER |
| 33 | #define FUZZ_IMAGEMAGICK_INITIALIZER "" |
| 34 | #endif |
| 35 | #define FUZZ_ENCODER_INITIALIZER FUZZ_ENCODER_STRING_LITERAL_X(FUZZ_IMAGEMAGICK_INITIALIZER) |
| 36 | |
| 37 | static ssize_t EncoderInitializer(const uint8_t *Data,const size_t magick_unused(Size),Magick::Image &image) |
| 38 | { |
| 39 | magick_unreferenced(Size); |
| 40 | |
| 41 | if (strcmp(FUZZ_ENCODER_INITIALIZER,"interlace") == 0) |
| 42 | { |
| 43 | Magick::InterlaceType |
| 44 | interlace=(Magick::InterlaceType) *reinterpret_cast<const char *>(Data); |
| 45 | |
| 46 | if (interlace > Magick::PNGInterlace) |
| 47 | return(-1); |
| 48 | image.interlaceType(interlace); |
| 49 | return(1); |
| 50 | } |
| 51 | if (strcmp(FUZZ_ENCODER_INITIALIZER,"png") == 0) |
| 52 | image.defineValue("png","ignore-crc","1"); |
| 53 | return(0); |
| 54 | } |
| 55 | |
| 56 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data,size_t Size) |
| 57 | { |
| 58 | Magick::Image |
| 59 | image; |
| 60 | |
| 61 | ssize_t |
| 62 | offset; |
| 63 | |
| 64 | std::string |
| 65 | encoder=FUZZ_ENCODER; |
| 66 | |
| 67 | if (IsInvalidSize(Size)) |
| 68 | return(0); |
| 69 | offset=EncoderInitializer(Data,Size,image); |
| 70 | if (offset < 0) |
| 71 | return(0); |
| 72 | image.magick(encoder); |
| 73 | image.fileName(std::string(encoder)+":"); |
| 74 | try |
| 75 | { |
| 76 | const Magick::Blob |
| 77 | blob(Data+offset,Size-offset); |
| 78 | |
| 79 | #if defined(BUILD_MAIN) |
| 80 | std::string |
| 81 | image_data; |
| 82 | |
| 83 | image_data=blob.base64(); |
| 84 | #endif |
| 85 | |
| 86 | image.read(blob); |
| 87 | } |
| 88 | #if defined(BUILD_MAIN) |
| 89 | catch (Magick::Exception &e) |
| 90 | { |
| 91 | std::cout << "Exception when reading: " << e.what() << std::endl; |
| 92 | return(0); |
| 93 | } |
| 94 | #else |
| 95 | catch (Magick::Exception) |
| 96 | { |
| 97 | return(0); |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | #if FUZZ_IMAGEMAGICK_ENCODER_WRITE || defined(BUILD_MAIN) |
| 102 | try |
| 103 | { |
| 104 | Magick::Blob |
| 105 | outBlob; |
| 106 | |
| 107 | image.write(&outBlob,encoder); |
| 108 | } |
| 109 | #if defined(BUILD_MAIN) |
| 110 | catch (Magick::Exception &e) |
| 111 | { |
| 112 | std::cout << "Exception when writing: " << e.what() << std::endl; |
| 113 | } |
| 114 | #else |
| 115 | catch (Magick::Exception) |
| 116 | { |
| 117 | } |
| 118 | #endif |
| 119 | #endif |
| 120 | return(0); |
| 121 | } |