/src/exiv2/fuzz/fuzz-read-print-write.cpp
Line | Count | Source |
1 | | #include <exiv2/exiv2.hpp> |
2 | | |
3 | | #include <cassert> |
4 | | #include <iomanip> |
5 | | #include <iostream> |
6 | | |
7 | 27.1k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
8 | | // Invalid files generate a lot of warnings, so switch off logging. |
9 | 27.1k | Exiv2::LogMsg::setLevel(Exiv2::LogMsg::mute); |
10 | | |
11 | 27.1k | Exiv2::XmpParser::initialize(); |
12 | 27.1k | ::atexit(Exiv2::XmpParser::terminate); |
13 | | |
14 | 27.1k | try { |
15 | 27.1k | Exiv2::DataBuf data_copy(data, size); |
16 | 27.1k | Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open(data_copy.c_data(), size); |
17 | 27.1k | assert(image.get() != 0); |
18 | | |
19 | 27.1k | image->readMetadata(); |
20 | 28.5M | for (auto& md : image->exifData()) { |
21 | 28.5M | if (md.tagName().substr(0, 2) != "0x") { |
22 | 568k | md.print(); |
23 | 568k | md.print(&image->exifData()); |
24 | 568k | } |
25 | 28.5M | } |
26 | 27.1k | for (auto& md : image->iptcData()) { |
27 | 1.61k | if (md.tagName().substr(0, 2) != "0x") { |
28 | 787 | md.print(); |
29 | 787 | md.print(&image->exifData()); |
30 | 787 | } |
31 | 1.61k | } |
32 | 27.1k | for (auto& md : image->xmpData()) { |
33 | 9.08k | if (md.tagName().substr(0, 2) != "0x") { |
34 | 9.08k | md.print(); |
35 | 9.08k | md.print(&image->exifData()); |
36 | 9.08k | } |
37 | 9.08k | } |
38 | | |
39 | | // Print to a std::ostringstream so that the fuzzer doesn't |
40 | | // produce lots of garbage on stdout. |
41 | 27.1k | std::ostringstream buffer; |
42 | 27.1k | image->printStructure(buffer, Exiv2::kpsNone); |
43 | 27.1k | image->printStructure(buffer, Exiv2::kpsBasic); |
44 | 27.1k | image->printStructure(buffer, Exiv2::kpsXMP); |
45 | 27.1k | image->printStructure(buffer, Exiv2::kpsRecursive); |
46 | 27.1k | image->printStructure(buffer, Exiv2::kpsIccProfile); |
47 | 27.1k | image->printStructure(buffer, Exiv2::kpsIptcErase); |
48 | | |
49 | 27.1k | image->writeMetadata(); |
50 | | |
51 | 27.1k | } catch (...) { |
52 | | // Exiv2 throws an exception if the metadata is invalid. |
53 | 25.6k | } |
54 | | |
55 | 27.1k | return 0; |
56 | 27.1k | } |