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