# Path: afl_utils/afl_collect.py
# def show_info():
# def get_fuzzer_instances(sync_dir, crash_dirs=True):
# def get_crash_directories(sync_dir, fuzzer_instances):
# def get_queue_directories(sync_dir, fuzzer_instances):
# def get_samples_from_dir(sample_subdir, abs_path=False):
# def collect_samples(sync_dir, fuzzer_instances):
# def build_sample_index(sync_dir, out_dir, fuzzer_instances, db=None, min_filename=False, omit_fuzzer_name=False):
# def copy_samples(sample_index):
# def generate_sample_list(list_filename, files_collected):
# def stdin_mode(target_cmd):
# def generate_gdb_exploitable_script(script_filename, sample_index, target_cmd, script_id=0, intermediate=False):
# def execute_gdb_script(out_dir, script_filename, num_samples, num_threads):
# def main(argv):
# 
# Path: afl_utils/afl_vcrash.py
# def show_info():
# def verify_samples(num_threads, samples, target_cmd, timeout_secs=60):
# def remove_samples(crash_samples, quiet=True):
# def build_target_cmd(target_cmdline):
# def main(argv):
# 
# Path: afl_utils/AflThread.py
# class VerifyThread(threading.Thread):
# class GdbThread(threading.Thread):
# class AflTminThread(threading.Thread):
#     def __init__(self, thread_id, timeout_secs, target_cmd, in_queue, out_queue, in_queue_lock, out_queue_lock):
#     def run(self):
#     def __init__(self, thread_id, gdb_cmd, out_dir, grep_for, out_queue, out_queue_lock):
#     def run(self):
#     def __init__(self, thread_id, tmin_cmd, target_cmd, output_dir, in_queue, out_queue, in_queue_lock, out_queue_lock):
#     def run(self):
# 
# Path: afl_utils/AflPrettyPrint.py
# class clr:
#     # taken from AFL's debug.h
#     BLK = "\x1b[0;30m"
#     RED = "\x1b[0;31m"
#     GRN = "\x1b[0;32m"
#     BRN = "\x1b[0;33m"
#     BLU = "\x1b[0;34m"
#     MGN = "\x1b[0;35m"
#     CYA = "\x1b[0;36m"
#     NOR = "\x1b[0;37m"
#     GRA = "\x1b[1;30m"
#     LRD = "\x1b[1;31m"
#     LGN = "\x1b[1;32m"
#     YEL = "\x1b[1;33m"
#     LBL = "\x1b[1;34m"
#     PIN = "\x1b[1;35m"
#     LCY = "\x1b[1;36m"
#     BRI = "\x1b[1;37m"
#     RST = "\x1b[0m"
# 
# def print_ok(msg_str):
#     print("{0}[*] {1}{2}".format(clr.LGN, clr.RST, msg_str))
# 
# def print_warn(msg_str):
#     print("{0}[!] {1}{2}".format(clr.YEL, clr.RST, msg_str))
# 
# def print_err(msg_str):
#     print("{0}[!] {1}{2}".format(clr.LRD, clr.RST, msg_str))


import argparse
import os
import shutil
import subprocess
import sys
import threading
import time
import queue
import afl_utils
from afl_utils import afl_collect, afl_vcrash, AflThread
from afl_utils.AflPrettyPrint import clr, print_ok, print_warn, print_err
"""




def show_info():
    print(clr.CYA + "afl-minimize " + clr.BRI + "%s" % afl_utils.__version__ + clr.RST + " by %s" %
          afl_utils.__author__)
    print("Corpus minimization utility for afl-fuzz corpora.")
    print("")


def invoke_cmin(input_dir, output_dir, target_cmd, mem_limit=None, timeout=None, qemu=False):
    success = True
    cmin_cmd = "afl-cmin "

    if mem_limit is not None:
        cmin_cmd += "-m %d " % int(mem_limit)

    if timeout is not None:
        cmin_cmd += "-t %d " % int(timeout)

    if qemu:
        cmin_cmd += "-Q "

    cmd = "%s-i %s -o %s -- %s" % (cmin_cmd, input_dir, output_dir, target_cmd)
    print_ok("Executing: %s" % cmd)
    try:
        subprocess.check_call(cmd, shell=True)
    except subprocess.CalledProcessError as e:

# Path: afl_utils/afl_collect.py
# def show_info():
# def get_fuzzer_instances(sync_dir, crash_dirs=True):
# def get_crash_directories(sync_dir, fuzzer_instances):
# def get_queue_directories(sync_dir, fuzzer_instances):
# def get_samples_from_dir(sample_subdir, abs_path=False):
# def collect_samples(sync_dir, fuzzer_instances):
# def build_sample_index(sync_dir, out_dir, fuzzer_instances, db=None, min_filename=False, omit_fuzzer_name=False):
# def copy_samples(sample_index):
# def generate_sample_list(list_filename, files_collected):
# def stdin_mode(target_cmd):
# def generate_gdb_exploitable_script(script_filename, sample_index, target_cmd, script_id=0, intermediate=False):
# def execute_gdb_script(out_dir, script_filename, num_samples, num_threads):
# def main(argv):
# 
# Path: afl_utils/afl_vcrash.py
# def show_info():
# def verify_samples(num_threads, samples, target_cmd, timeout_secs=60):
# def remove_samples(crash_samples, quiet=True):
# def build_target_cmd(target_cmdline):
# def main(argv):
# 
# Path: afl_utils/AflThread.py
# class VerifyThread(threading.Thread):
# class GdbThread(threading.Thread):
# class AflTminThread(threading.Thread):
#     def __init__(self, thread_id, timeout_secs, target_cmd, in_queue, out_queue, in_queue_lock, out_queue_lock):
#     def run(self):
#     def __init__(self, thread_id, gdb_cmd, out_dir, grep_for, out_queue, out_queue_lock):
#     def run(self):
#     def __init__(self, thread_id, tmin_cmd, target_cmd, output_dir, in_queue, out_queue, in_queue_lock, out_queue_lock):
#     def run(self):
# 
# Path: afl_utils/AflPrettyPrint.py
# class clr:
#     # taken from AFL's debug.h
#     BLK = "\x1b[0;30m"
#     RED = "\x1b[0;31m"
#     GRN = "\x1b[0;32m"
#     BRN = "\x1b[0;33m"
#     BLU = "\x1b[0;34m"
#     MGN = "\x1b[0;35m"
#     CYA = "\x1b[0;36m"
#     NOR = "\x1b[0;37m"
#     GRA = "\x1b[1;30m"
#     LRD = "\x1b[1;31m"
#     LGN = "\x1b[1;32m"
#     YEL = "\x1b[1;33m"
#     LBL = "\x1b[1;34m"
#     PIN = "\x1b[1;35m"
#     LCY = "\x1b[1;36m"
#     BRI = "\x1b[1;37m"
#     RST = "\x1b[0m"
# 
# def print_ok(msg_str):
#     print("{0}[*] {1}{2}".format(clr.LGN, clr.RST, msg_str))
# 
# def print_warn(msg_str):
#     print("{0}[!] {1}{2}".format(clr.YEL, clr.RST, msg_str))
# 
# def print_err(msg_str):
#     print("{0}[!] {1}{2}".format(clr.LRD, clr.RST, msg_str))


# Path: afl_utils/afl_minimize.py
import argparse
import os
import shutil
import subprocess
import sys
import threading
import time
import queue
import afl_utils
from afl_utils import afl_collect, afl_vcrash, AflThread
from afl_utils.AflPrettyPrint import clr, print_ok, print_warn, print_err

"""




def show_info():
    print(clr.CYA + "afl-minimize " + clr.BRI + "%s" % afl_utils.__version__ + clr.RST + " by %s" %
          afl_utils.__author__)
    print("Corpus minimization utility for afl-fuzz corpora.")
    print("")


def invoke_cmin(input_dir, output_dir, target_cmd, mem_limit=None, timeout=None, qemu=False):
    success = True
    cmin_cmd = "afl-cmin "

    if mem_limit is not None:
        cmin_cmd += "-m %d " % int(mem_limit)

    if timeout is not None:
        cmin_cmd += "-t %d " % int(timeout)

    if qemu:
        cmin_cmd += "-Q "

    cmd = "%s-i %s -o %s -- %s" % (cmin_cmd, input_dir, output_dir, target_cmd)
    print_ok("Executing: %s" % cmd)
    try:
        subprocess.check_call(cmd, shell=True)
    except subprocess.CalledProcessError as e:

        print_warn("afl-cmin failed with exit code %d!" % e.returncode)