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 <cstdlib>
39#include <iostream>
40
41#include "ojph_mem.h"
42#include "ojph_file.h"
43#include "ojph_codestream.h"
44#include "ojph_params.h"
45
46// Input layout (4 control bytes + pixel data):
47// byte 0: [6:0] width-1 (1..128)
48// byte 1: [6:0] height-1 (1..128)
49// byte 2: [1:0] num_components-1 (1..4)
50// [3:2] bit_depth selector (8,10,12,16)
51// [4] is_signed
52// [5] reversible
53// [6] color_transform
54// byte 3: [2:0] num_decompositions (0..5, clamped)
55// [3] planar
56// bytes 4+: pixel data (each byte becomes one sample)
57
58extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
59{
60 if (Size < 5)
61 return 0;
62
63 ojph::ui32 width = (Data[0] & 0x7F) + 1;
64 ojph::ui32 height = (Data[1] & 0x7F) + 1;
65 ojph::ui32 num_comps = (Data[2] & 0x03) + 1;
66 ojph::ui32 bit_depth = (ojph::ui32[]){8, 10, 12, 16}[(Data[2] >> 2) & 0x03];
67 bool is_signed = (Data[2] >> 4) & 1;
68 bool reversible = (Data[2] >> 5) & 1;
69 bool color_transform = (Data[2] >> 6) & 1;
70 ojph::ui32 num_decomps = Data[3] & 0x07;
71 bool planar = (Data[3] >> 3) & 1;
72
73 if (num_decomps > 5) num_decomps = 5;
74 if (num_comps < 3) color_transform = false;
75 if (color_transform) planar = false;
76
77 const uint8_t *pixels = Data + 4;
78 size_t pixels_len = Size - 4;
79 size_t pix_idx = 0;
80
81 try
82 {
84
85 ojph::param_siz siz = cs.access_siz();
86 siz.set_image_extent(ojph::point(width, height));
87 siz.set_num_components(num_comps);
88 for (ojph::ui32 c = 0; c < num_comps; ++c)
89 siz.set_component(c, ojph::point(1, 1), bit_depth, is_signed);
90
91 ojph::param_cod cod = cs.access_cod();
92 cod.set_num_decomposition(num_decomps);
93 cod.set_color_transform(color_transform);
94 cod.set_reversible(reversible);
95
96 if (!reversible)
97 cs.access_qcd().set_irrev_quant(0.0005f);
98
99 cs.set_planar(planar);
100
101 ojph::mem_outfile outfile;
102 outfile.open();
103 cs.write_headers(&outfile);
104
105 // Total rows to push: planar processes each component fully,
106 // interleaved processes one row from all components at a time.
107 ojph::ui32 total_rows = num_comps * height;
108 ojph::ui32 next_comp;
109 ojph::line_buf *line = cs.exchange(NULL, next_comp);
110
111 for (ojph::ui32 r = 0; r < total_rows; ++r)
112 {
113 ojph::si32 *dp = line->i32;
114 for (ojph::ui32 x = 0; x < width; ++x)
115 {
116 // Use fuzz bytes as sample values, wrapping around as needed
117 ojph::si32 val = (ojph::si32)pixels[pix_idx % pixels_len];
118 pix_idx++;
119 dp[x] = is_signed ? val - 128 : val;
120 }
121 line = cs.exchange(line, next_comp);
122 }
123
124 cs.flush();
125 cs.close();
126 }
127 catch (const std::exception &)
128 {
129 }
130 return 0;
131}
The object represent a codestream.
param_siz access_siz()
Returns the underlying SIZ marker segment object.
param_cod access_cod()
Returns the underlying COD marker segment object.
void close()
Call this function to close the underlying file; works for both encoding and decoding codestreams.
void set_planar(bool planar)
Sets the sequence of pushing or pull rows from the machinery.
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...
param_qcd access_qcd()
Returns the underlying QCD marker segment object.
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...
void flush()
This is the last call to a writing (encoding) codestream. This will write encoded bitstream data to t...
mem_outfile stores encoded j2k codestreams in memory
Definition ojph_file.h:127
void open(size_t initial_size=65536, bool clear_mem=false)
Call this function to open a memory file.
void set_num_decomposition(ui32 num_decompositions)
void set_color_transform(bool color_transform)
void set_reversible(bool reversible)
void set_irrev_quant(float delta)
Set the irreversible quantization base delta.
void set_component(ui32 comp_num, const point &downsampling, ui32 bit_depth, bool is_signed)
void set_num_components(ui32 num_comps)
void set_image_extent(point extent)
int32_t si32
Definition ojph_defs.h:55
uint32_t ui32
Definition ojph_defs.h:54
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)