Loading...
Searching...
No Matches
ojph_compress_fuzz_target.cpp
Go to the documentation of this file.
//***************************************************************************/
// This software is released under the 2-Clause BSD license, included
// below.
//
// Copyright (c) 2019, Aous Naman
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
// Copyright (c) 2019, The University of New South Wales, Australia
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//***************************************************************************/
// This file is part of the OpenJPH software implementation.
// File: ojph_compress_fuzz_target.cpp
// Fuzz target for the HTJ2K encoding (compression) path.
//***************************************************************************/
#include <cstdint>
#include <vector>
#include <cstdlib>
#include <iostream>
#include "ojph_mem.h"
#include "ojph_file.h"
#include "ojph_codestream.h"
#include "ojph_params.h"
// Input layout (4 control bytes + pixel data):
// byte 0: [6:0] width-1 (1..128)
// byte 1: [6:0] height-1 (1..128)
// byte 2: [1:0] num_components-1 (1..4)
// [3:2] bit_depth selector (8,10,12,16)
// [4] is_signed
// [5] reversible
// [6] color_transform
// byte 3: [2:0] num_decompositions (0..5, clamped)
// [3] planar
// bytes 4+: pixel data (each byte becomes one sample)
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
{
if (Size < 5) {
return 0;
}
ojph::ui32 width = (Data[0] & 0x7F) + 1;
ojph::ui32 height = (Data[1] & 0x7F) + 1;
ojph::ui32 num_comps = (Data[2] & 0x03) + 1;
ojph::ui32 bit_depth = (ojph::ui32[]){8, 10, 12, 16}[(Data[2] >> 2) & 0x03];
bool is_signed = (Data[2] >> 4) & 1;
bool reversible = (Data[2] >> 5) & 1;
bool color_transform = (Data[2] >> 6) & 1;
ojph::ui32 num_decomps = Data[3] & 0x07;
bool planar = (Data[3] >> 3) & 1;
if (num_decomps > 5) num_decomps = 5;
if (num_comps < 3) color_transform = false;
if (color_transform) planar = false;
const uint8_t *pixels = Data + 4;
size_t pixels_len = Size - 4;
size_t pix_idx = 0;
try
{
ojph::codestream cs;
ojph::param_siz siz = cs.access_siz();
siz.set_image_extent(ojph::point(width, height));
siz.set_num_components(num_comps);
for (ojph::ui32 c = 0; c < num_comps; ++c)
siz.set_component(c, ojph::point(1, 1), bit_depth, is_signed);
ojph::param_cod cod = cs.access_cod();
cod.set_num_decomposition(num_decomps);
cod.set_color_transform(color_transform);
cod.set_reversible(reversible);
if (!reversible)
cs.access_qcd().set_irrev_quant(0.0005f);
cs.set_planar(planar);
ojph::mem_outfile outfile;
outfile.open();
cs.write_headers(&outfile);
// Total rows to push: planar processes each component fully,
// interleaved processes one row from all components at a time.
ojph::ui32 total_rows = num_comps * height;
ojph::ui32 next_comp;
ojph::line_buf *line = cs.exchange(NULL, next_comp);
for (ojph::ui32 r = 0; r < total_rows; ++r)
{
ojph::si32 *dp = line->i32;
for (ojph::ui32 x = 0; x < width; ++x)
{
// Use fuzz bytes as sample values, wrapping around as needed
ojph::si32 val = (ojph::si32)pixels[pix_idx % pixels_len];
pix_idx++;
dp[x] = is_signed ? val - 128 : val;
}
line = cs.exchange(line, next_comp);
}
cs.flush();
cs.close();
}
catch (const std::exception &)
{
}
return 0;
}
#ifdef OJPH_FUZZ_TARGET_MAIN
int main(int argc, char **argv) {
if (argc != 2) {
return -1;
}
FILE *f = fopen(argv[1], "rb");
if (!f) { return -1; }
fseek(f, 0, SEEK_END);
long len = ftell(f);
if (len < 0) {
return -1;
}
rewind(f);
std::vector<uint8_t> buf(len);
size_t n = fread(buf.data(), 1, len, f);
if(n != static_cast<size_t>(len)) {
return -1;
}
fclose(f);
LLVMFuzzerTestOneInput(buf.data(), buf.size());
return 0;
}
#endif
main
int main(int argc, char *argv[])
Definition CMakeCCompilerId.c:907
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
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