/* Educational state-machine model. NOT Brotli code or an exploit stream.
 * clang -g -O0 -fsanitize=address -fno-omit-frame-pointer \
 *   decoder-state.c -o decoder-state
 * ./decoder-state          # AddressSanitizer: heap-buffer-overflow
 * ./decoder-state --fixed  # invalid format rejected, exits successfully
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum Result { FORMAT_ERROR, NEEDS_MORE_INPUT };

int main(int argc, char **argv) {
    int fixed = argc > 1 && strcmp(argv[1], "--fixed") == 0;
    unsigned char *input_buffer = malloc(8);
    if (!input_buffer) return 1;
    size_t buffered = 0;

    /* The format is permanently invalid, but the old model requests more input. */
    enum Result result = fixed ? FORMAT_ERROR : NEEDS_MORE_INPUT;
    for (size_t chunk = 0; chunk < 9; ++chunk) {
        if (result == FORMAT_ERROR) {
            puts("Invalid format rejected before buffering more input.");
            free(input_buffer);
            return 0;
        }
        input_buffer[buffered++] = (unsigned char)chunk;
    }
    printf("Buffered %zu bytes\n", buffered);
    free(input_buffer);
    return 0;
}
