--- /dev/null
+SRC := $(wildcard */*.c)
+EXE := $(SRC:.c=)
+
+LDLIBS := -lwim
+LDFLAGS := -L../../.libs
+CPPFLAGS := -I../../include
+
+all:$(EXE)
+
+clean:
+ rm -f $(EXE)
--- /dev/null
+#include <assert.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <wimlib.h>
+
+int main(int argc, char *argv[])
+{
+ int fd;
+ struct stat stbuf;
+ uint8_t ctype;
+ uint8_t level;
+ struct wimlib_compressor *c;
+ struct wimlib_decompressor *d;
+ size_t usize, csize;
+ void *udata, *cdata, *decompressed;
+ int ret;
+
+ fd = open(argv[1], O_RDONLY);
+ assert(fd >= 0);
+ ret = fstat(fd, &stbuf);
+ assert(!ret);
+
+ if (stbuf.st_size < 2)
+ return 0;
+ ret = read(fd, &ctype, 1);
+ assert(ret == 1);
+ ret = read(fd, &level, 1);
+ assert(ret == 1);
+ ctype = 1 + ((ctype - 1) % 3); /* 1-3 */
+ level = 1 + (level % 100); /* 1-100 */
+ usize = stbuf.st_size - 2;
+
+ udata = malloc(usize);
+ cdata = malloc(usize);
+ decompressed = malloc(usize);
+
+ ret = read(fd, udata, usize);
+ assert(ret == usize);
+
+ ret = wimlib_create_compressor(ctype, usize, level, &c);
+ if (ret == 0) {
+ ret = wimlib_create_decompressor(ctype, usize, &d);
+ assert(ret == 0);
+
+ csize = wimlib_compress(udata, usize, cdata, usize, c);
+ if (csize) {
+ ret = wimlib_decompress(cdata, csize,
+ decompressed, usize, d);
+ assert(ret == 0);
+ assert(memcmp(udata, decompressed, usize) == 0);
+ }
+ wimlib_free_compressor(c);
+ wimlib_free_decompressor(d);
+ }
+ free(udata);
+ free(cdata);
+ free(decompressed);
+ return 0;
+}
--- /dev/null
+\ 3\14CHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 32CHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 3PCHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 2\14CHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 22CHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 2PCHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 1\14CHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 12CHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+\ 1PCHAPTER 1. Loomings.\r
+\r
+Call me Ishmael. Some years ago—never mind how long precisely—having\r
+little or no money in my purse, and nothing particular to interest me\r
+on shore, I thought I would sail about a little and see the watery part\r
+of the world. It is a way I have of driving off the spleen and\r
+regulating the circulation. Whenever I find myself growing grim about\r
+the mouth; whenever it is a damp, drizzly November in my soul; whenever\r
+I find myself involuntarily pausing before coffin warehouses, and\r
+bringing up the rear of every funeral I meet; and especially whenever\r
+my hypos get such an upper hand of me, that it requires a strong moral\r
+principle to prevent me from deliberately stepping into the street, and\r
+methodically knocking people’s hats off—then, I account it high time to\r
+get to sea as soon as I can. This is my substitute for pistol and ball.\r
+With a philosophical flourish Cato throws himself upon his sword; I\r
+quietly take to the ship. There is nothing su
\ No newline at end of file
--- /dev/null
+#include <assert.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <wimlib.h>
+
+int main(int argc, char *argv[])
+{
+ int fd;
+ struct stat stbuf;
+ uint8_t ctype;
+ size_t csize, uspace;
+ void *cdata, *udata;
+ struct wimlib_decompressor *d;
+ int ret;
+
+ fd = open(argv[1], O_RDONLY);
+ assert(fd >= 0);
+ ret = fstat(fd, &stbuf);
+ assert(!ret);
+
+ if (stbuf.st_size < 1)
+ return 0;
+ ret = read(fd, &ctype, 1);
+ assert(ret == 1);
+ ctype = 1 + ((ctype - 1) % 3); /* 1-3 */
+ csize = stbuf.st_size - 1;
+ uspace = csize * 8;
+
+ cdata = malloc(csize);
+ udata = malloc(uspace);
+
+ ret = read(fd, cdata, csize);
+ assert(ret == csize);
+
+ ret = wimlib_create_decompressor(ctype, uspace, &d);
+ if (ret == 0)
+ wimlib_decompress(cdata, csize, udata, uspace, d);
+
+ free(udata);
+ free(cdata);
+ wimlib_free_decompressor(d);
+ return 0;
+}
--- /dev/null
+#!/bin/bash
+
+set -e -u -o pipefail
+
+cd "$(dirname "$0")"
+
+read -r -a AVAILABLE_TARGETS < <(echo */fuzz.c | sed 's@/fuzz.c@@g')
+
+usage()
+{
+ cat << EOF
+Usage: $0 [OPTION]... [TARGET]...
+
+Fuzz wimlib with afl-fuzz.
+
+Options:
+ --asan Enable AddressSanitizer
+ --no-resume Don't resume existing afl-fuzz session; start a new one
+ --ubsan Enable UndefinedBehaviorSanitizer
+
+Available targets: ${AVAILABLE_TARGETS[*]}
+EOF
+}
+
+die()
+{
+ echo "$*" 1>&2
+ exit 1
+}
+
+asan=false
+ubsan=false
+may_resume=true
+
+longopts_array=(
+asan
+help
+no-resume
+ubsan
+)
+longopts=$(echo "${longopts_array[@]}" | tr ' ' ',')
+
+if ! options=$(getopt -o "" -l "$longopts" -- "$@"); then
+ usage 1>&2
+ exit 1
+fi
+eval set -- "$options"
+while (( $# >= 0 )); do
+ case "$1" in
+ --asan)
+ asan=true
+ ;;
+ --help)
+ usage
+ exit 0
+ ;;
+ --no-resume)
+ may_resume=false
+ ;;
+ --ubsan)
+ ubsan=true
+ ;;
+ --)
+ shift
+ break
+ ;;
+ *)
+ echo 1>&2 "Invalid option: \"$1\""
+ usage 1>&2
+ exit 1
+ esac
+ shift
+done
+
+if $asan && $ubsan; then
+ die "--asan and --ubsan are mutually exclusive"
+fi
+
+if ! type -P afl-fuzz > /dev/null; then
+ die "afl-fuzz is not installed"
+fi
+
+if (( $# == 0 )); then
+ targets=("${AVAILABLE_TARGETS[@]}")
+else
+ for target; do
+ found=false
+ for t in "${AVAILABLE_TARGETS[@]}"; do
+ if [ "$target" = "$t" ]; then
+ found=true
+ fi
+ done
+ if ! $found; then
+ echo 1>&2 "Unknown target '$target'"
+ echo 1>&2 "Available targets: ${AVAILABLE_TARGETS[*]}"
+ exit 1
+ fi
+ done
+ targets=("$@")
+fi
+if (( ${#targets[@]} > 1 )) && ! type -P urxvt > /dev/null; then
+ die "urxvt is not installed"
+fi
+
+afl_opts=""
+if $asan; then
+ export AFL_USE_ASAN=1
+ export CFLAGS="-O2 -m32"
+ export CC=afl-clang
+ afl_opts+=" -m 800"
+elif $ubsan; then
+ export CFLAGS="-fsanitize=undefined -fno-sanitize-recover=undefined"
+ export CC=afl-gcc
+else
+ export AFL_HARDEN=1
+ export CFLAGS="-O2"
+ export CC=afl-gcc
+fi
+
+sudo sh -c "echo core > /proc/sys/kernel/core_pattern"
+sudo sh -c "echo performance | tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor"
+
+NPROC=$(getconf _NPROCESSORS_ONLN)
+
+(
+cd ../../
+./configure CC="$CC" CFLAGS="$CFLAGS"
+make "-j$NPROC"
+)
+make "-j$NPROC" -B
+export LD_LIBRARY_PATH=$PWD/../../.libs
+
+for dir in "${targets[@]}"; do
+ workdir=/tmp/wimlib_$dir
+ cp -vaT "$dir" "$workdir"
+ indir=$workdir/inputs
+ outdir=$workdir/outputs
+ if [ -e "$outdir" ]; then
+ if $may_resume; then
+ indir="-"
+ else
+ rm -rf "${outdir:?}"/*
+ fi
+ else
+ mkdir "$outdir"
+ fi
+ cmd="afl-fuzz -i $indir -o $outdir -T wimlib_$dir $afl_opts -- $workdir/fuzz @@"
+ if (( ${#targets[@]} > 1 )); then
+ urxvt -e bash -c "$cmd" &
+ else
+ $cmd
+ fi
+done
+wait