/*
 * acfg.h -- shared interface for the ACFG demonstration parser.
 *
 * Chapter 70 -- Vulnerability Research, examples/
 *
 * WHAT THIS IS
 *   ACFG ("Android ConFiGuration blob") is a small, deliberately Android-shaped
 *   binary container format invented for this chapter. It is a length-prefixed
 *   TLV (type-length-value) record stream, which is the same structural family
 *   as the real formats that historically produced Android's worst parser bugs:
 *   the resource table chunks in `resources.arsc`, the box tree in an MP4 file,
 *   the tag stream in an ID3 header, and the extra-field stream in a ZIP local
 *   file header.
 *
 *   Two implementations of `acfg_parse` ship against this one header:
 *
 *     acfg-vulnerable.c  -- carries one deliberately planted memory-safety bug
 *     acfg-fixed.c       -- the same parser with that bug remediated
 *
 *   Because both expose exactly this interface, the same fuzzing harness
 *   (fuzz-acfg.c) links against either one. That is the whole point: a harness
 *   is written against an interface, not against an implementation, so the
 *   identical corpus can be replayed against a patched build to confirm a fix.
 *
 * WHAT THIS IS NOT
 *   This is not a real Android format and nothing on a device parses it. It is
 *   a target you own, so you can fuzz it on the host without touching anyone
 *   else's software. See ../README.md section 7 for why that matters.
 *
 * WIRE FORMAT
 *
 *   File header (8 bytes)
 *     offset 0   4 bytes   magic, the ASCII bytes "ACFG"
 *     offset 4   2 bytes   version, little-endian uint16, must be 1
 *     offset 6   2 bytes   count,   little-endian uint16, number of records
 *
 *   Record header (4 bytes), repeated `count` times
 *     offset 0   1 byte    type, must be one of ACFG_T_*
 *     offset 1   1 byte    nlen, length of the name in bytes (0..255)
 *     offset 2   2 bytes   vlen, little-endian uint16, length of the value
 *
 *   Record body
 *     nlen bytes of name, NOT NUL-terminated on the wire
 *     vlen bytes of value, opaque
 *
 *   Note the asymmetry that the planted bug lives in: `nlen` can describe a
 *   name of up to 255 bytes, while the in-memory record reserves only
 *   ACFG_NAME_MAX bytes for it. Any format whose wire-level width exceeds its
 *   in-memory destination width is worth a second look; that mismatch is one of
 *   the most productive things to grep a real parser for.
 */

#ifndef ACFG_H
#define ACFG_H

#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Size of the fixed in-memory buffer that a record name is copied into. The
 * wire format allows 255. This is the mismatch. */
#define ACFG_NAME_MAX 32

#define ACFG_HEADER_SIZE     8
#define ACFG_REC_HEADER_SIZE 4
#define ACFG_MAX_VALUE       4096

/* Record type byte. A parser that accepts arbitrary type bytes is harder to
 * fuzz efficiently, because most of the input space is rejected late rather
 * than early. Real formats almost always have a small enumerated type space. */
#define ACFG_T_STRING 0x01
#define ACFG_T_INT    0x02
#define ACFG_T_BOOL   0x03
#define ACFG_T_BLOB   0x04

/* Return codes. Negative values are errors; ACFG_OK is success. */
#define ACFG_OK          0
#define ACFG_E_ARG      (-1)   /* NULL argument */
#define ACFG_E_SHORT    (-2)   /* input shorter than the file header */
#define ACFG_E_MAGIC    (-3)   /* magic bytes are not "ACFG" */
#define ACFG_E_VERSION  (-4)   /* version field is not 1 */
#define ACFG_E_TRUNC    (-5)   /* a record runs off the end of the input */
#define ACFG_E_TYPE     (-6)   /* record type byte is not a known ACFG_T_* */
#define ACFG_E_VALUE    (-7)   /* value length exceeds ACFG_MAX_VALUE */
#define ACFG_E_NAME     (-8)   /* name length does not fit ACFG_NAME_MAX */
#define ACFG_E_NOMEM    (-9)   /* allocation failed */

/*
 * One parsed record.
 *
 * `name` is an inline fixed-size array, not a pointer. That choice is what
 * makes the planted bug land INSIDE the records array rather than past its end
 * when count is greater than one -- which in turn is what makes one defect
 * produce a loud ASan abort, a silent-but-reported UBSan line, or nothing at
 * all, depending purely on two attacker-controlled header fields. All three
 * outcomes were measured; see the PLANTED BUG banner in acfg-vulnerable.c and
 * chapter 70 section 3.7.
 *
 * sizeof(acfg_record) is 56 on a 64-bit target: 1 byte of type, 32 of name,
 * 7 of padding, 8 of pointer, 2 of length, 6 of tail padding. Knowing that
 * number is what turns "the write went somewhere" into "the write went onto
 * the value pointer of the next record", which is the difference between a
 * crash report and a triage verdict.
 */
typedef struct {
    uint8_t  type;
    char     name[ACFG_NAME_MAX];  /* NUL-terminated after a successful parse */
    uint8_t *value;                /* heap, owned by the blob, may be NULL */
    uint16_t value_len;
} acfg_record;

typedef struct {
    uint16_t     version;
    uint16_t     count;    /* records claimed by the header */
    uint16_t     built;    /* records actually initialised -- see acfg_free */
    acfg_record *records;  /* heap array of `count` records, or NULL */
} acfg_blob;

/*
 * Parse `len` bytes at `buf` into `*out`.
 *
 * On ACFG_OK the caller owns `*out` and must release it with acfg_free().
 * On any error the function releases whatever it had allocated and leaves
 * `*out` zeroed, so the caller may -- but need not -- call acfg_free() anyway.
 * That "free is always safe" property is a deliberate part of the contract:
 * error paths that leave an object half-built are their own bug class, and
 * section 5.4 of the chapter covers them.
 */
int acfg_parse(const uint8_t *buf, size_t len, acfg_blob *out);

/* Release everything acfg_parse allocated. Safe on a zeroed blob. */
void acfg_free(acfg_blob *out);

/* Human-readable name for a return code. Used by the harness only. */
const char *acfg_strerror(int rc);

#ifdef __cplusplus
}
#endif

#endif /* ACFG_H */
