Update and clean up fuzztest/fuzzstub
diff --git a/tests/fuzztest/SConscript b/tests/fuzztest/SConscript
index 52033e1..7723d99 100644
--- a/tests/fuzztest/SConscript
+++ b/tests/fuzztest/SConscript
@@ -36,14 +36,6 @@
     iterations = 10000
 env.RunTest(fuzz, ARGS = [str(seed), str(iterations)])
 
-fuzzstub = malloc_env.Program(["fuzzstub.c",
-                    "alltypes_pointer.pb.c",
-                    "alltypes_static.pb.c",
-                    "$COMMON/pb_encode_with_malloc.o",
-                    "$COMMON/pb_decode_with_malloc.o",
-                    "$COMMON/pb_common_with_malloc.o",
-                    "$COMMON/malloc_wrappers.o"])
-
 generate_message = malloc_env.Program(["generate_message.c",
                     "alltypes_static.pb.c",
                     "$COMMON/pb_encode.o",
diff --git a/tests/fuzztest/fuzzstub.c b/tests/fuzztest/fuzzstub.c
deleted file mode 100644
index ec9e2af..0000000
--- a/tests/fuzztest/fuzzstub.c
+++ /dev/null
@@ -1,189 +0,0 @@
-/* Fuzz testing for the nanopb core.
- * This can be used with external fuzzers, e.g. radamsa.
- * It performs most of the same checks as fuzztest, but does not feature data generation.
- */
-
-#include <pb_decode.h>
-#include <pb_encode.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#include <time.h>
-#include <malloc_wrappers.h>
-#include "alltypes_static.pb.h"
-#include "alltypes_pointer.pb.h"
-
-#define BUFSIZE 4096
-
-static bool do_static_decode(uint8_t *buffer, size_t msglen, bool assert_success)
-{
-    pb_istream_t stream;
-    bool status;
-    
-    alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
-    stream = pb_istream_from_buffer(buffer, msglen);
-    status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg);
-    
-    if (!status && assert_success)
-    {
-        /* Anything that was successfully encoded, should be decodeable.
-         * One exception: strings without null terminator are encoded up
-         * to end of buffer, but refused on decode because the terminator
-         * would not fit. */
-        if (strcmp(stream.errmsg, "string overflow") != 0)
-            assert(status);
-    }
-    
-    free_with_check(msg);
-    return status;
-}
-
-static bool do_pointer_decode(uint8_t *buffer, size_t msglen, bool assert_success)
-{
-    pb_istream_t stream;
-    bool status;
-    alltypes_pointer_AllTypes *msg;
-    
-    msg = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
-    memset(msg, 0, sizeof(alltypes_pointer_AllTypes));
-    stream = pb_istream_from_buffer(buffer, msglen);
-
-    assert(get_alloc_count() == 0);
-    status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg);
-    
-    if (assert_success)
-        assert(status);
-    
-    pb_release(alltypes_pointer_AllTypes_fields, msg);    
-    assert(get_alloc_count() == 0);
-    
-    free_with_check(msg);
-
-    return status;
-}
-
-/* Do a decode -> encode -> decode -> encode roundtrip */
-static void do_static_roundtrip(uint8_t *buffer, size_t msglen)
-{
-    bool status;
-    uint8_t *buf2 = malloc_with_check(BUFSIZE);
-    uint8_t *buf3 = malloc_with_check(BUFSIZE);
-    size_t msglen2, msglen3;
-    alltypes_static_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_static_AllTypes));
-    alltypes_static_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_static_AllTypes));
-    memset(msg1, 0, sizeof(alltypes_static_AllTypes));
-    memset(msg2, 0, sizeof(alltypes_static_AllTypes));
-    
-    {
-        pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
-        status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg1);
-        assert(status);
-    }
-    
-    {
-        pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
-        status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg1);
-        assert(status);
-        msglen2 = stream.bytes_written;
-    }
-    
-    {
-        pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
-        status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg2);
-        assert(status);
-    }
-    
-    {
-        pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
-        status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg2);
-        assert(status);
-        msglen3 = stream.bytes_written;
-    }
-    
-    assert(msglen2 == msglen3);
-    assert(memcmp(buf2, buf3, msglen2) == 0);
-    
-    free_with_check(msg1);
-    free_with_check(msg2);
-    free_with_check(buf2);
-    free_with_check(buf3);
-}
-
-/* Do decode -> encode -> decode -> encode roundtrip */
-static void do_pointer_roundtrip(uint8_t *buffer, size_t msglen)
-{
-    bool status;
-    uint8_t *buf2 = malloc_with_check(BUFSIZE);
-    uint8_t *buf3 = malloc_with_check(BUFSIZE);
-    size_t msglen2, msglen3;
-    alltypes_pointer_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
-    alltypes_pointer_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
-    memset(msg1, 0, sizeof(alltypes_pointer_AllTypes));
-    memset(msg2, 0, sizeof(alltypes_pointer_AllTypes));
-    
-    {
-        pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
-        status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg1);
-        assert(status);
-    }
-    
-    {
-        pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
-        status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg1);
-        assert(status);
-        msglen2 = stream.bytes_written;
-    }
-    
-    {
-        pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
-        status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg2);
-        assert(status);
-    }
-    
-    {
-        pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
-        status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg2);
-        assert(status);
-        msglen3 = stream.bytes_written;
-    }
-    
-    assert(msglen2 == msglen3);
-    assert(memcmp(buf2, buf3, msglen2) == 0);
-    
-    pb_release(alltypes_pointer_AllTypes_fields, msg1);
-    pb_release(alltypes_pointer_AllTypes_fields, msg2);
-    free_with_check(msg1);
-    free_with_check(msg2);
-    free_with_check(buf2);
-    free_with_check(buf3);
-}
-
-static void run_iteration()
-{
-    uint8_t *buffer = malloc_with_check(BUFSIZE);
-    size_t msglen;
-    bool status;
-    
-    msglen = fread(buffer, 1, BUFSIZE, stdin);
-
-    status = do_static_decode(buffer, msglen, false);
-    
-    if (status)
-        do_static_roundtrip(buffer, msglen);
-    
-    status = do_pointer_decode(buffer, msglen, false);
-    
-    if (status)
-        do_pointer_roundtrip(buffer, msglen);
-    
-    free_with_check(buffer);
-}
-
-int main(int argc, char **argv)
-{
-    run_iteration();
-    
-    return 0;
-}
-
diff --git a/tests/fuzztest/fuzztest.c b/tests/fuzztest/fuzztest.c
index 7e9ddb7..e6e9939 100644
--- a/tests/fuzztest/fuzztest.c
+++ b/tests/fuzztest/fuzztest.c
@@ -8,11 +8,13 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
-#include <time.h>
 #include <malloc_wrappers.h>
+#include "test_helpers.h"
 #include "alltypes_static.pb.h"
 #include "alltypes_pointer.pb.h"
 
+#define BUFSIZE 4096
+
 static uint64_t random_seed;
 
 /* Uses xorshift64 here instead of rand() for both speed and
@@ -198,8 +200,6 @@
     }
 }
 
-#define BUFSIZE 4096
-
 static bool do_static_encode(uint8_t *buffer, size_t *msglen)
 {
     pb_ostream_t stream;
@@ -444,19 +444,50 @@
     assert(get_alloc_count() == 0);
 }
 
+static void run_stub()
+{
+    uint8_t *buffer = malloc_with_check(BUFSIZE);
+    size_t msglen;
+    bool status;
+
+    SET_BINARY_MODE(stdin);
+    msglen = fread(buffer, 1, BUFSIZE, stdin);
+
+    status = do_static_decode(buffer, msglen, false);
+
+    if (status)
+        do_static_roundtrip(buffer, msglen);
+
+    status = do_pointer_decode(buffer, msglen, false);
+
+    if (status)
+        do_pointer_roundtrip(buffer, msglen);
+
+    free_with_check(buffer);
+}
+
 int main(int argc, char **argv)
 {
     int i;
     int iterations;
 
-    random_seed = atol(argv[1]);
-    iterations = atol(argv[2]);
-    if (iterations == 0) iterations = 10000;
-    fprintf(stderr, "Random seed: %u, iterations: %d\n", (unsigned)random_seed, iterations);
-    
-    for (i = 0; i < iterations; i++)
+    if (argc >= 2)
     {
-        run_iteration();
+        // Run in stand-alone mode
+        random_seed = atol(argv[1]);
+        iterations = atol(argv[2]);
+        if (iterations == 0) iterations = 10000;
+        fprintf(stderr, "Random seed: %u, iterations: %d\n", (unsigned)random_seed, iterations);
+
+        for (i = 0; i < iterations; i++)
+        {
+            run_iteration();
+        }
+    }
+    else
+    {
+        // Run as a stub for afl-fuzz and similar
+        run_stub();
     }
     
     return 0;
diff --git a/tests/fuzztest/generate_message.c b/tests/fuzztest/generate_message.c
index 73959fe..4ebe1cc 100644
--- a/tests/fuzztest/generate_message.c
+++ b/tests/fuzztest/generate_message.c
@@ -66,7 +66,7 @@
 static void generate_message()
 {
     alltypes_static_AllTypes msg;
-    uint8_t buf[8192];
+    uint8_t buf[4096];
     pb_ostream_t stream = {0};
     
     do {
@@ -83,16 +83,9 @@
 
 int main(int argc, char **argv)
 {
-    if (argc > 1)
-    {
-        random_seed = atol(argv[1]);
-    }
-    else
-    {
-        random_seed = time(NULL);
-    }
-    
-    fprintf(stderr, "Random seed: %llu\n", (long long unsigned)random_seed);
+    random_seed = atol(argv[1]);
+
+    fprintf(stderr, "Random seed: %u\n", (unsigned)random_seed);
     
     generate_message();
     
diff --git a/tests/fuzztest/run_radamsa.sh b/tests/fuzztest/run_radamsa.sh
deleted file mode 100755
index 52cd40a..0000000
--- a/tests/fuzztest/run_radamsa.sh
+++ /dev/null
@@ -1,12 +0,0 @@
-#!/bin/bash
-
-TMP=`tempfile`
-
-echo $TMP
-while true
-do
-   radamsa sample_data/* > $TMP
-   $1 < $TMP
-   test $? -gt 127 && break
-done
-