Debian Codegraph @

fuzzing/color_conversion_fuzzer.cc

raw
1 /*
2 * HEIF codec.
3 * Copyright (c) 2019 struktur AG, Joachim Bauch <[email protected]>
4 *
5 * This file is part of libheif.
6 *
7 * libheif is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * libheif is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with libheif. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <assert.h>
22
23 #include <sstream>
24
25 #include "bitstream.h"
26 #include "color-conversion/colorconversion.h"
27 #include "pixelimage.h"
28
29 static bool is_valid_chroma(uint8_t chroma)
30 {
31 switch (chroma) {
32 case heif_chroma_monochrome:
33 case heif_chroma_420:
34 case heif_chroma_422:
35 case heif_chroma_444:
36 case heif_chroma_interleaved_RGB:
37 case heif_chroma_interleaved_RGBA:
38 case heif_chroma_interleaved_RRGGBB_BE:
39 case heif_chroma_interleaved_RRGGBBAA_BE:
40 case heif_chroma_interleaved_RRGGBB_LE:
41 case heif_chroma_interleaved_RRGGBBAA_LE:
42 return true;
43 default:
44 return false;
45 }
46 }
47
48 static bool is_valid_colorspace(uint8_t colorspace)
49 {
50 switch (colorspace) {
51 case heif_colorspace_YCbCr:
52 case heif_colorspace_RGB:
53 case heif_colorspace_monochrome:
54 return true;
55 default:
56 return false;
57 }
58 }
59
60 static bool read_plane(BitstreamRange* range,
61 std::shared_ptr<HeifPixelImage> image, heif_channel channel,
62 uint32_t width, uint32_t height, int bit_depth)
63 {
64 if (width <= 0 || height <= 0) {
65 return false;
66 }
67 if (std::numeric_limits<size_t>::max()/width/height == 0) {
68 return false;
69 }
70 if (!range->prepare_read(static_cast<size_t>(width) * height)) {
71 return false;
72 }
73 if (auto err = image->add_plane(channel, width, height, bit_depth, heif_get_disabled_security_limits())) {
74 return false;
75 }
76 size_t stride;
77 uint8_t* plane = image->get_plane(channel, &stride);
78 assert(stride >= width);
79 auto stream = range->get_istream();
80 for (uint32_t y = 0; y < height; y++, plane += stride) {
81 assert(stream->read(plane, width));
82 }
83 return true;
84 }
85
86 static bool read_plane_interleaved(BitstreamRange* range,
87 std::shared_ptr<HeifPixelImage> image, heif_channel channel,
88 uint32_t width, uint32_t height, int bit_depth, int comps)
89 {
90 if (width <= 0 || height <= 0) {
91 return false;
92 }
93 if (std::numeric_limits<size_t>::max()/width/height/comps == 0) {
94 return false;
95 }
96 if (!range->prepare_read(static_cast<size_t>(width) * height * comps)) {
97 return false;
98 }
99 if (auto err = image->add_plane(channel, width, height, bit_depth, heif_get_disabled_security_limits())) {
100 return false;
101 }
102 size_t stride;
103 uint8_t* plane = image->get_plane(channel, &stride);
104 assert(stride >= width * comps);
105 auto stream = range->get_istream();
106 for (uint32_t y = 0; y < height; y++, plane += stride) {
107 assert(stream->read(plane, width * comps));
108 }
109 return true;
110 }
111
112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114 auto reader = std::make_shared<StreamReader_memory>(data, size, false);
115 BitstreamRange range(reader, size);
116
117 uint32_t width;
118 uint32_t height;
119 int bit_depth;
120 bool alpha;
121 uint8_t in_chroma;
122 uint8_t in_colorspace;
123 uint8_t out_chroma;
124 uint8_t out_colorspace;
125 if (!range.prepare_read(10)) {
126 return 0;
127 }
128
129 width = range.read16();
130 height = range.read16();
131 bit_depth = range.read8();
132 alpha = range.read8() == 1;
133 in_chroma = range.read8();
134 in_colorspace = range.read8();
135 out_chroma = range.read8();
136 out_colorspace = range.read8();
137
138 // Width / height must be a multiple of 2.
139 if (width == 0 || height == 0 || (width & 1) != 0 || (height & 1) != 0) {
140 return 0;
141 }
142
143 switch (bit_depth) {
144 case 8:
145 break;
146 default:
147 // TODO: Add support for more color depths.
148 return 0;
149 }
150
151 if (!is_valid_chroma(in_chroma) || !is_valid_colorspace(in_colorspace) ||
152 !is_valid_chroma(out_chroma) || !is_valid_colorspace(out_colorspace)) {
153 return 0;
154 }
155
156 auto in_image = std::make_shared<HeifPixelImage>();
157 in_image->create(width, height, static_cast<heif_colorspace>(in_colorspace),
158 static_cast<heif_chroma>(in_chroma));
159
160 switch (in_colorspace) {
161 case heif_colorspace_YCbCr:
162 switch (in_chroma) {
163 case heif_chroma_420:
164 if (!read_plane(&range, in_image, heif_channel_Y,
165 width, height, bit_depth)) {
166 return 0;
167 }
168 if (!read_plane(&range, in_image, heif_channel_Cb,
169 width / 2, height / 2, bit_depth)) {
170 return 0;
171 }
172 if (!read_plane(&range, in_image, heif_channel_Cr,
173 width / 2, height / 2, bit_depth)) {
174 return 0;
175 }
176 break;
177 case heif_chroma_422:
178 if (!read_plane(&range, in_image, heif_channel_Y,
179 width, height, bit_depth)) {
180 return 0;
181 }
182 if (!read_plane(&range, in_image, heif_channel_Cb,
183 width / 2, height, bit_depth)) {
184 return 0;
185 }
186 if (!read_plane(&range, in_image, heif_channel_Cr,
187 width / 2, height, bit_depth)) {
188 return 0;
189 }
190 break;
191 case heif_chroma_444:
192 if (!read_plane(&range, in_image, heif_channel_Y,
193 width, height, bit_depth)) {
194 return 0;
195 }
196 if (!read_plane(&range, in_image, heif_channel_Cb,
197 width, height, bit_depth)) {
198 return 0;
199 }
200 if (!read_plane(&range, in_image, heif_channel_Cr,
201 width, height, bit_depth)) {
202 return 0;
203 }
204 break;
205 default:
206 return 0;
207 }
208 break;
209 case heif_colorspace_RGB:
210 switch (in_chroma) {
211 case heif_chroma_interleaved_RGB:
212 if (!read_plane_interleaved(&range, in_image,
213 heif_channel_interleaved, width, height, bit_depth, 3)) {
214 return 0;
215 }
216 break;
217 case heif_chroma_interleaved_RGBA:
218 if (!read_plane_interleaved(&range, in_image,
219 heif_channel_interleaved, width, height, bit_depth, 4)) {
220 return 0;
221 }
222 alpha = false; // Already part of interleaved data.
223 break;
224 default:
225 // TODO: Support other RGB chromas.
226 return 0;
227 }
228 break;
229 case heif_colorspace_monochrome:
230 if (in_chroma != heif_chroma_monochrome) {
231 return 0;
232 }
233 if (!read_plane(&range, in_image, heif_channel_Y,
234 width, height, bit_depth)) {
235 return 0;
236 }
237 break;
238 default:
239 assert(false);
240 }
241
242 if (alpha) {
243 if (!read_plane(&range, in_image, heif_channel_Alpha,
244 width, height, bit_depth)) {
245 return 0;
246 }
247 }
248
249 // TODO: also fuzz these parameters.
250 int output_bpp = 0; // Same as input.
251 heif_encoding_options* options = heif_encoding_options_alloc();
252
253 heif_color_conversion_options_ext* options_ext = heif_color_conversion_options_ext_alloc();
254
255 auto out_image_result = convert_colorspace(in_image,
256 static_cast<heif_colorspace>(out_colorspace),
257 static_cast<heif_chroma>(out_chroma),
258 nclx_profile::undefined(),
259 output_bpp,
260 options->color_conversion_options,
261 options_ext,
262 heif_get_disabled_security_limits());
263
264 heif_encoding_options_free(options);
265 heif_color_conversion_options_ext_free(options_ext);
266
267 if (!out_image_result) {
268 // Conversion is not supported.
269 return 0;
270 }
271
272 auto out_image = *out_image_result;
273
274 assert(out_image->get_width() == width);
275 assert(out_image->get_height() == height);
276 assert(out_image->get_chroma_format() ==
277 static_cast<heif_chroma>(out_chroma));
278 assert(out_image->get_colorspace() ==
279 static_cast<heif_colorspace>(out_colorspace));
280 return 0;
281 }