Skip to content

Commit 1c5d9e5

Browse files
author
Grok Compression
committed
core decompress example: add fuzzer test code
1 parent 5ee6eec commit 1c5d9e5

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

examples/core/core_decompress.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,87 @@ int main([[maybe_unused]] int argc, [[maybe_unused]] const char** argv)
577577
return app.exit(e); // Handle parsing errors
578578
}
579579

580+
// #define TEST_FUZZER
581+
#ifdef TEST_FUZZER
582+
{
583+
grk_header_info headerInfo = {};
584+
grk_decompress_parameters parameters = {};
585+
parameters.dw_x1 = 1024;
586+
parameters.dw_y1 = 1024;
587+
grk_object* codec = nullptr;
588+
grk_stream_params stream_params = {};
589+
size_t len = 0;
590+
uint8_t* buf = nullptr;
591+
FILE* file = nullptr;
592+
593+
auto cleanup = [&]() {
594+
if(buf)
595+
free(buf);
596+
if(codec)
597+
grk_object_unref(codec);
598+
if(file)
599+
fclose(file);
600+
};
601+
602+
// Open the file
603+
file = fopen(inputFilePath.c_str(), "rb");
604+
if(!file)
605+
{
606+
fprintf(stderr, "Failed to open file: %s\n", inputFilePath.c_str());
607+
cleanup();
608+
return -1;
609+
}
610+
611+
// Get file size
612+
fseek(file, 0, SEEK_END);
613+
len = (size_t)ftell(file);
614+
fseek(file, 0, SEEK_SET);
615+
616+
// Allocate buffer
617+
buf = (uint8_t*)malloc(len);
618+
if(!buf)
619+
{
620+
fprintf(stderr, "Failed to allocate buffer for file\n");
621+
cleanup();
622+
return -1;
623+
}
624+
625+
// Read file into buffer
626+
size_t bytesRead = fread(buf, 1, len, file);
627+
if(bytesRead != len)
628+
{
629+
fprintf(stderr, "Failed to read file: %s\n", inputFilePath.c_str());
630+
cleanup();
631+
return -1;
632+
}
633+
634+
// Close the file
635+
fclose(file);
636+
637+
// Set stream parameters
638+
stream_params.buf = buf;
639+
stream_params.buf_len = len;
640+
641+
// Proceed with decompression
642+
codec = grk_decompress_init(&stream_params, &parameters);
643+
if(!codec)
644+
{
645+
cleanup();
646+
return -1;
647+
}
648+
if(!grk_decompress_read_header(codec, &headerInfo))
649+
{
650+
cleanup();
651+
return -1;
652+
}
653+
if(!grk_decompress(codec, nullptr))
654+
{
655+
cleanup();
656+
return -1;
657+
}
658+
}
659+
#endif
660+
580661
// initialize decompress parameters
581662
grk_decompress_parameters decompressParams = {};
582663
decompressParams.core.skip_allocate_composite = !doStore;

0 commit comments

Comments
 (0)