townforge/tests/fuzz/unishox2.c
2022-08-23 05:20:28 +00:00

70 lines
2.4 KiB
C

// Copyright (c) 2017-2022, The Monero Project
//
// All rights reserved.
//
// 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.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// 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.
#include <stdint.h>
#include <stdio.h>
#include <malloc.h>
#include "common/unishox2.h"
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
{
static char out[16 * 65536];
unishox2_decompress((const char*)buf, len, out, sizeof(out), USX_HCODES_DFLT, USX_HCODE_LENS_DFLT, USX_FREQ_SEQ_TXT, USX_TEMPLATES);
return 0;
}
#ifndef OSSFUZZ
int main(int argc, const char **argv)
{
FILE *f;
long pos;
size_t len;
char *buf;
if (argc < 2)
{
printf("usage: %s <filename>\n", argv[0]);
return 1;
}
f = fopen(argv[1], "r");
if (!f) return 1;
if (fseek(f, 0, SEEK_END) < 0) return 1;
pos = ftell(f);
if (pos < 0) return 1;
len = pos;
if (fseek(f, 0, SEEK_SET) < 0) return 1;
buf = malloc(len);
if (!buf) return 1;
if (fread(buf, 1, len, f) != (size_t)len) return 1;
return LLVMFuzzerTestOneInput((const uint8_t*)buf, len);
}
#endif