OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_compress_fuzz_target.cpp
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2019, Aous Naman
6// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2019, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_compress_fuzz_target.cpp
34// Fuzz target for the HTJ2K encoding (compression) path.
35//***************************************************************************/
36
37#include <cstdint>
38#include <vector>
39#include <cstdlib>
40#include <iostream>
41
42#include "ojph_mem.h"
43#include "ojph_file.h"
44#include "ojph_codestream.h"
45#include "ojph_params.h"
46
47// Input layout (4 control bytes + pixel data):
48// byte 0: [6:0] width-1 (1..128)
49// byte 1: [6:0] height-1 (1..128)
50// byte 2: [1:0] num_components-1 (1..4)
51// [3:2] bit_depth selector (8,10,12,16)
52// [4] is_signed
53// [5] reversible
54// [6] color_transform
55// byte 3: [2:0] num_decompositions (0..5, clamped)
56// [3] planar
57// bytes 4+: pixel data (each byte becomes one sample)
58
59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
60{
61 if (Size < 5) {
62 return 0;
63 }
64
65 ojph::ui32 width = (Data[0] & 0x7F) + 1;
66 ojph::ui32 height = (Data[1] & 0x7F) + 1;
67 ojph::ui32 num_comps = (Data[2] & 0x03) + 1;
68 ojph::ui32 bit_depth = (ojph::ui32[]){8, 10, 12, 16}[(Data[2] >> 2) & 0x03];
69 bool is_signed = (Data[2] >> 4) & 1;
70 bool reversible = (Data[2] >> 5) & 1;
71 bool color_transform = (Data[2] >> 6) & 1;
72 ojph::ui32 num_decomps = Data[3] & 0x07;
73 bool planar = (Data[3] >> 3) & 1;
74
75 if (num_decomps > 5) num_decomps = 5;
76 if (num_comps < 3) color_transform = false;
77 if (color_transform) planar = false;
78
79 const uint8_t *pixels = Data + 4;
80 size_t pixels_len = Size - 4;
81 size_t pix_idx = 0;
82
83 try
84 {
85 ojph::codestream cs;
86
87 ojph::param_siz siz = cs.access_siz();
88 siz.set_image_extent(ojph::point(width, height));
89 siz.set_num_components(num_comps);
90 for (ojph::ui32 c = 0; c < num_comps; ++c)
91 siz.set_component(c, ojph::point(1, 1), bit_depth, is_signed);
92
93 ojph::param_cod cod = cs.access_cod();
94 cod.set_num_decomposition(num_decomps);
95 cod.set_color_transform(color_transform);
96 cod.set_reversible(reversible);
97
98 if (!reversible)
99 cs.access_qcd().set_irrev_quant(0.0005f);
100
101 cs.set_planar(planar);
102
103 ojph::mem_outfile outfile;
104 outfile.open();
105 cs.write_headers(&outfile);
106
107 // Total rows to push: planar processes each component fully,
108 // interleaved processes one row from all components at a time.
109 ojph::ui32 total_rows = num_comps * height;
110 ojph::ui32 next_comp;
111 ojph::line_buf *line = cs.exchange(NULL, next_comp);
112
113 for (ojph::ui32 r = 0; r < total_rows; ++r)
114 {
115 ojph::si32 *dp = line->i32;
116 for (ojph::ui32 x = 0; x < width; ++x)
117 {
118 // Use fuzz bytes as sample values, wrapping around as needed
119 ojph::si32 val = (ojph::si32)pixels[pix_idx % pixels_len];
120 pix_idx++;
121 dp[x] = is_signed ? val - 128 : val;
122 }
123 line = cs.exchange(line, next_comp);
124 }
125
126 cs.flush();
127 cs.close();
128 }
129 catch (const std::exception &)
130 {
131 }
132 return 0;
133}
134
135#ifdef OJPH_FUZZ_TARGET_MAIN
136 int main(int argc, char **argv) {
137 if (argc != 2) {
138 return -1;
139 }
140 FILE *f = fopen(argv[1], "rb");
141 if (!f) { return -1; }
142 fseek(f, 0, SEEK_END);
143 long len = ftell(f);
144 if (len < 0) {
145 return -1;
146 }
147 rewind(f);
148 std::vector<uint8_t> buf(len);
149 size_t n = fread(buf.data(), 1, len, f);
150 if(n != static_cast<size_t>(len)) {
151 return -1;
152 }
153 fclose(f);
154 LLVMFuzzerTestOneInput(buf.data(), buf.size());
155 return 0;
156}
157#endif
ojph::codestream
The object represent a codestream.
Definition ojph_codestream.h:89
ojph::codestream::access_siz
param_siz access_siz()
Returns the underlying SIZ marker segment object.
Definition ojph_codestream.cpp:79
ojph::codestream::access_cod
param_cod access_cod()
Returns the underlying COD marker segment object.
Definition ojph_codestream.cpp:85
ojph::codestream::close
void close()
Call this function to close the underlying file; works for both encoding and decoding codestreams.
Definition ojph_codestream.cpp:206
ojph::codestream::set_planar
void set_planar(bool planar)
Sets the sequence of pushing or pull rows from the machinery.
Definition ojph_codestream.cpp:103
ojph::codestream::exchange
line_buf * exchange(line_buf *line, ui32 &next_component)
This call is used to send image data rows to the library. We expect to send one row from a single com...
Definition ojph_codestream.cpp:212
ojph::codestream::access_qcd
param_qcd access_qcd()
Returns the underlying QCD marker segment object.
Definition ojph_codestream.cpp:91
ojph::codestream::write_headers
void write_headers(outfile_base *file, const comment_exchange *comments=NULL, ui32 num_comments=0)
Writes codestream headers when the codestream is used for writing. This function should be called aft...
Definition ojph_codestream.cpp:159
ojph::codestream::flush
void flush()
This is the last call to a writing (encoding) codestream. This will write encoded bitstream data to t...
Definition ojph_codestream.cpp:200
ojph::line_buf
Definition ojph_mem.h:161
ojph::line_buf::i32
si32 * i32
Definition ojph_mem.h:185
ojph::mem_outfile
mem_outfile stores encoded j2k codestreams in memory
Definition ojph_file.h:127
ojph::mem_outfile::open
void open(size_t initial_size=65536, bool clear_mem=false)
Call this function to open a memory file.
Definition ojph_file.cpp:152
ojph::param_cod
Definition ojph_params.h:104
ojph::param_cod::set_num_decomposition
void set_num_decomposition(ui32 num_decompositions)
Definition ojph_params.cpp:164
ojph::param_cod::set_color_transform
void set_color_transform(bool color_transform)
Definition ojph_params.cpp:242
ojph::param_cod::set_reversible
void set_reversible(bool reversible)
Definition ojph_params.cpp:248
ojph::param_qcd::set_irrev_quant
void set_irrev_quant(float delta)
Set the irreversible quantization base delta.
Definition ojph_params.cpp:414
ojph::param_siz
Definition ojph_params.h:71
ojph::param_siz::set_component
void set_component(ui32 comp_num, const point &downsampling, ui32 bit_depth, bool is_signed)
Definition ojph_params.cpp:89
ojph::param_siz::set_num_components
void set_num_components(ui32 num_comps)
Definition ojph_params.cpp:83
ojph::param_siz::set_image_extent
void set_image_extent(point extent)
Definition ojph_params.cpp:59
ojph::si32
int32_t si32
Definition ojph_defs.h:55
ojph::ui32
uint32_t ui32
Definition ojph_defs.h:54
ojph_codestream.h
main
int main(int argc, char *argv[])
Definition ojph_compress.cpp:487
LLVMFuzzerTestOneInput
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
Definition ojph_compress_fuzz_target.cpp:59
ojph_file.h
ojph_mem.h
ojph_params.h
ojph::point
Definition ojph_base.h:58