File: color_conversion_fuzzer.cc

package info (click to toggle)
libheif 1.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,572 kB
  • sloc: cpp: 83,285; python: 3,032; sh: 952; ansic: 453; javascript: 160; makefile: 76
file content (281 lines) | stat: -rw-r--r-- 8,774 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * HEIF codec.
 * Copyright (c) 2019 struktur AG, Joachim Bauch <bauch@struktur.de>
 *
 * This file is part of libheif.
 *
 * libheif is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * libheif is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>

#include <sstream>

#include "bitstream.h"
#include "color-conversion/colorconversion.h"
#include "pixelimage.h"

static bool is_valid_chroma(uint8_t chroma)
{
  switch (chroma) {
    case heif_chroma_monochrome:
    case heif_chroma_420:
    case heif_chroma_422:
    case heif_chroma_444:
    case heif_chroma_interleaved_RGB:
    case heif_chroma_interleaved_RGBA:
    case heif_chroma_interleaved_RRGGBB_BE:
    case heif_chroma_interleaved_RRGGBBAA_BE:
    case heif_chroma_interleaved_RRGGBB_LE:
    case heif_chroma_interleaved_RRGGBBAA_LE:
      return true;
    default:
      return false;
  }
}

static bool is_valid_colorspace(uint8_t colorspace)
{
  switch (colorspace) {
    case heif_colorspace_YCbCr:
    case heif_colorspace_RGB:
    case heif_colorspace_monochrome:
      return true;
    default:
      return false;
  }
}

static bool read_plane(BitstreamRange* range,
                       std::shared_ptr<HeifPixelImage> image, heif_channel channel,
                       uint32_t width, uint32_t height, int bit_depth)
{
  if (width <= 0 || height <= 0) {
    return false;
  }
  if (std::numeric_limits<size_t>::max()/width/height == 0) {
    return false;
  }
  if (!range->prepare_read(static_cast<size_t>(width) * height)) {
    return false;
  }
  if (auto err = image->add_plane(channel, width, height, bit_depth, heif_get_disabled_security_limits())) {
    return false;
  }
  size_t stride;
  uint8_t* plane = image->get_plane(channel, &stride);
  assert(stride >= width);
  auto stream = range->get_istream();
  for (uint32_t y = 0; y < height; y++, plane += stride) {
    assert(stream->read(plane, width));
  }
  return true;
}

static bool read_plane_interleaved(BitstreamRange* range,
                                   std::shared_ptr<HeifPixelImage> image, heif_channel channel,
                                   uint32_t width, uint32_t height, int bit_depth, int comps)
{
  if (width <= 0 || height <= 0) {
    return false;
  }
  if (std::numeric_limits<size_t>::max()/width/height/comps == 0) {
    return false;
  }
  if (!range->prepare_read(static_cast<size_t>(width) * height * comps)) {
    return false;
  }
  if (auto err = image->add_plane(channel, width, height, bit_depth, heif_get_disabled_security_limits())) {
    return false;
  }
  size_t stride;
  uint8_t* plane = image->get_plane(channel, &stride);
  assert(stride >= width * comps);
  auto stream = range->get_istream();
  for (uint32_t y = 0; y < height; y++, plane += stride) {
    assert(stream->read(plane, width * comps));
  }
  return true;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
  auto reader = std::make_shared<StreamReader_memory>(data, size, false);
  BitstreamRange range(reader, size);

  uint32_t width;
  uint32_t height;
  int bit_depth;
  bool alpha;
  uint8_t in_chroma;
  uint8_t in_colorspace;
  uint8_t out_chroma;
  uint8_t out_colorspace;
  if (!range.prepare_read(10)) {
    return 0;
  }

  width = range.read16();
  height = range.read16();
  bit_depth = range.read8();
  alpha = range.read8() == 1;
  in_chroma = range.read8();
  in_colorspace = range.read8();
  out_chroma = range.read8();
  out_colorspace = range.read8();

  // Width / height must be a multiple of 2.
  if (width == 0 || height == 0 || (width & 1) != 0 || (height & 1) != 0) {
    return 0;
  }

  switch (bit_depth) {
    case 8:
      break;
    default:
      // TODO: Add support for more color depths.
      return 0;
  }

  if (!is_valid_chroma(in_chroma) || !is_valid_colorspace(in_colorspace) ||
      !is_valid_chroma(out_chroma) || !is_valid_colorspace(out_colorspace)) {
    return 0;
  }

  auto in_image = std::make_shared<HeifPixelImage>();
  in_image->create(width, height, static_cast<heif_colorspace>(in_colorspace),
                   static_cast<heif_chroma>(in_chroma));

  switch (in_colorspace) {
    case heif_colorspace_YCbCr:
      switch (in_chroma) {
        case heif_chroma_420:
          if (!read_plane(&range, in_image, heif_channel_Y,
                          width, height, bit_depth)) {
            return 0;
          }
          if (!read_plane(&range, in_image, heif_channel_Cb,
                          width / 2, height / 2, bit_depth)) {
            return 0;
          }
          if (!read_plane(&range, in_image, heif_channel_Cr,
                          width / 2, height / 2, bit_depth)) {
            return 0;
          }
          break;
        case heif_chroma_422:
          if (!read_plane(&range, in_image, heif_channel_Y,
                          width, height, bit_depth)) {
            return 0;
          }
          if (!read_plane(&range, in_image, heif_channel_Cb,
                          width / 2, height, bit_depth)) {
            return 0;
          }
          if (!read_plane(&range, in_image, heif_channel_Cr,
                          width / 2, height, bit_depth)) {
            return 0;
          }
          break;
        case heif_chroma_444:
          if (!read_plane(&range, in_image, heif_channel_Y,
                          width, height, bit_depth)) {
            return 0;
          }
          if (!read_plane(&range, in_image, heif_channel_Cb,
                          width, height, bit_depth)) {
            return 0;
          }
          if (!read_plane(&range, in_image, heif_channel_Cr,
                          width, height, bit_depth)) {
            return 0;
          }
          break;
        default:
          return 0;
      }
      break;
    case heif_colorspace_RGB:
      switch (in_chroma) {
        case heif_chroma_interleaved_RGB:
          if (!read_plane_interleaved(&range, in_image,
                                      heif_channel_interleaved, width, height, bit_depth, 3)) {
            return 0;
          }
          break;
        case heif_chroma_interleaved_RGBA:
          if (!read_plane_interleaved(&range, in_image,
                                      heif_channel_interleaved, width, height, bit_depth, 4)) {
            return 0;
          }
          alpha = false;  // Already part of interleaved data.
          break;
        default:
          // TODO: Support other RGB chromas.
          return 0;
      }
      break;
    case heif_colorspace_monochrome:
      if (in_chroma != heif_chroma_monochrome) {
        return 0;
      }
      if (!read_plane(&range, in_image, heif_channel_Y,
                      width, height, bit_depth)) {
        return 0;
      }
      break;
    default:
      assert(false);
  }

  if (alpha) {
    if (!read_plane(&range, in_image, heif_channel_Alpha,
                    width, height, bit_depth)) {
      return 0;
    }
  }

  // TODO: also fuzz these parameters.
  int output_bpp = 0; // Same as input.
  heif_encoding_options* options = heif_encoding_options_alloc();

  heif_color_conversion_options_ext* options_ext = heif_color_conversion_options_ext_alloc();

  auto out_image_result = convert_colorspace(in_image,
                                             static_cast<heif_colorspace>(out_colorspace),
                                             static_cast<heif_chroma>(out_chroma),
                                             nullptr,
                                             output_bpp,
                                             options->color_conversion_options,
                                             options_ext,
                                             heif_get_disabled_security_limits());

  heif_encoding_options_free(options);
  heif_color_conversion_options_ext_free(options_ext);

  if (out_image_result.error) {
    // Conversion is not supported.
    return 0;
  }

  auto out_image = *out_image_result;

  assert(out_image->get_width() == width);
  assert(out_image->get_height() == height);
  assert(out_image->get_chroma_format() ==
         static_cast<heif_chroma>(out_chroma));
  assert(out_image->get_colorspace() ==
         static_cast<heif_colorspace>(out_colorspace));
  return 0;
}