/src/libtiff/contrib/oss-fuzz/write_fuzzer.cc
Line | Count | Source |
1 | | // Copyright 2022 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #include <cstdint> |
18 | | #include <cstdio> |
19 | | #include <cstdlib> |
20 | | #include <cstring> |
21 | | #include <sstream> |
22 | | #include <tiffio.h> |
23 | | #include <tiffio.hxx> |
24 | | |
25 | | #include <fuzzer/FuzzedDataProvider.h> |
26 | | |
27 | 0 | extern "C" void handle_error(const char *, const char *, va_list) { return; } |
28 | | |
29 | | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) |
30 | 199 | { |
31 | 199 | if (Size < 5) |
32 | 4 | { |
33 | 4 | return 0; |
34 | 4 | } |
35 | 195 | FuzzedDataProvider fdp(Data, Size); |
36 | 195 | uint32_t width = 10; |
37 | 195 | const uint32_t length = 40; |
38 | 195 | const uint32_t rows_per_strip = 1; |
39 | | |
40 | 195 | const char *filename = "test_packbits.tif"; |
41 | 195 | TIFF *tif1 = TIFFOpen(filename, "w"); |
42 | 195 | if (!tif1) |
43 | 0 | { |
44 | 0 | fprintf(stderr, "Can't create test TIFF file %s.\n", filename); |
45 | 0 | return 1; |
46 | 0 | } |
47 | | |
48 | 195 | switch (fdp.ConsumeIntegralInRange(0, 2)) |
49 | 195 | { |
50 | 129 | case 0: |
51 | 129 | if (!TIFFSetField(tif1, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS)) |
52 | 0 | { |
53 | 0 | fprintf(stderr, "Can't set Compression tag.\n"); |
54 | 0 | TIFFClose(tif1); |
55 | 0 | return 0; |
56 | 0 | } |
57 | 129 | if (!TIFFSetField(tif1, TIFFTAG_IMAGEWIDTH, width)) |
58 | 0 | { |
59 | 0 | fprintf(stderr, "Can't set ImageWidth tag.\n"); |
60 | 0 | TIFFClose(tif1); |
61 | 0 | return 0; |
62 | 0 | } |
63 | 129 | if (!TIFFSetField(tif1, TIFFTAG_IMAGELENGTH, length)) |
64 | 0 | { |
65 | 0 | fprintf(stderr, "Can't set ImageLength tag.\n"); |
66 | 0 | TIFFClose(tif1); |
67 | 0 | return 0; |
68 | 0 | } |
69 | 129 | if (!TIFFSetField(tif1, TIFFTAG_BITSPERSAMPLE, 8)) |
70 | 0 | { |
71 | 0 | fprintf(stderr, "Can't set BitsPerSample tag.\n"); |
72 | 0 | TIFFClose(tif1); |
73 | 0 | return 0; |
74 | 0 | } |
75 | 129 | if (!TIFFSetField(tif1, TIFFTAG_SAMPLESPERPIXEL, 1)) |
76 | 0 | { |
77 | 0 | fprintf(stderr, "Can't set SamplesPerPixel tag.\n"); |
78 | 0 | TIFFClose(tif1); |
79 | 0 | return 0; |
80 | 0 | } |
81 | 129 | if (!TIFFSetField(tif1, TIFFTAG_ROWSPERSTRIP, rows_per_strip)) |
82 | 0 | { |
83 | 0 | fprintf(stderr, "Can't set SamplesPerPixel tag.\n"); |
84 | 0 | TIFFClose(tif1); |
85 | 0 | return 0; |
86 | 0 | } |
87 | 129 | if (!TIFFSetField(tif1, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)) |
88 | 0 | { |
89 | 0 | fprintf(stderr, "Can't set PlanarConfiguration tag.\n"); |
90 | 0 | TIFFClose(tif1); |
91 | 0 | return 0; |
92 | 0 | } |
93 | 190 | case 1: |
94 | 190 | if (!TIFFSetField(tif1, TIFFTAG_COMPRESSION, COMPRESSION_NONE)) |
95 | 0 | { |
96 | 0 | fprintf(stderr, "Can't set Compression tag.\n"); |
97 | 0 | TIFFClose(tif1); |
98 | 0 | return 0; |
99 | 0 | } |
100 | 190 | if (!TIFFSetField(tif1, TIFFTAG_IMAGEWIDTH, width)) |
101 | 0 | { |
102 | 0 | fprintf(stderr, "Can't set ImageWidth tag.\n"); |
103 | 0 | TIFFClose(tif1); |
104 | 0 | return 0; |
105 | 0 | } |
106 | 190 | if (!TIFFSetField(tif1, TIFFTAG_IMAGELENGTH, length)) |
107 | 0 | { |
108 | 0 | fprintf(stderr, "Can't set ImageWidth tag.\n"); |
109 | 0 | TIFFClose(tif1); |
110 | 0 | return 0; |
111 | 0 | } |
112 | 190 | TIFFSetField(tif1, TIFFTAG_BITSPERSAMPLE, 8); |
113 | 190 | TIFFSetField(tif1, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); |
114 | 190 | TIFFSetField(tif1, TIFFTAG_SAMPLESPERPIXEL, 1); |
115 | 190 | TIFFSetField(tif1, TIFFTAG_ROWSPERSTRIP, 1); |
116 | 195 | } |
117 | | |
118 | 195 | tsize_t stripsize = TIFFStripSize(tif1); |
119 | 195 | tstrip_t stripcount = TIFFNumberOfStrips(tif1); |
120 | 195 | std::vector<uint8_t> rBytes = fdp.ConsumeRemainingBytes<uint8_t>(); |
121 | | |
122 | 195 | TIFFWriteEncodedStrip(tif1, tstrip_t(0), static_cast<void *>(rBytes.data()), |
123 | 195 | static_cast<int>(rBytes.size())); |
124 | 195 | TIFFClose(tif1); |
125 | | |
126 | 195 | return 0; |
127 | 195 | } |