# libvibe API Reference [VIBE configuration format](../SPECIFICATION.md). Everything lives in a single header, `vibe.h`, and links against `libvibe` (or the vendored `vibe.c`). The library has **no dependencies** beyond the C standard library, keeps **no global mutable state** (aside from the optional allocator hooks), and every parse is independent, so one `VibeParser` per thread is fully re-entrant. ## Table of Contents - [Conventions](#conventions) - [Versioning](#versioning) - [Types](#types) - [Parser Lifecycle](#parser-lifecycle) - [Parsing](#parsing) - [Error Handling](#error-handling) - [Resource Limits](#resource-limits) - [Allocator Hooks](#allocator-hooks) - [Value Constructors](#value-constructors) - [Value Inspection & Equality](#value-inspection--equality) - [Path Access](#path-access) - [Container Operations](#container-operations) - [Emitting](#emitting) - [Miscellaneous](#miscellaneous) - [Command-Line Tool](#command-line-tool) - [Examples](#examples) --- ## Conventions - **Ownership.** A successful parse returns a heap-allocated root `VibeValue` (always an `OBJECT`). You own it; free it with `vibe_value_free()`, which recursively frees the whole subtree. `vibe_object_set()` and `vibe_array_push()` **take ownership** of the value you hand them. - **Buffers returned to you** (e.g. from `vibe_emit()`) must be freed with `vibe_free()`, so the right allocator is used. - **Failure.** Parsing functions return `NULL` on failure; query the parser with `vibe_get_last_error()` (or pass a `VibeError*` to the stateless `vibe_parse()`). - **Const-ness.** Read-only helpers accept `const` pointers where the legacy signature allowed it; the tree itself is a plain tagged union you may inspect directly (`value->type`, `value->as_integer`, …). --- ## Versioning `VIBE_VERSION_*` describes **this library**; `VIBE_FORMAT_VERSION` describes the **language** it implements. They advance independently. Use the compile-time macros for `#if` guards and the runtime functions to verify the library you actually linked against. --- ## Types ### `VibeType` ### `VibeValue` A tagged union. Inspect `.type`, then read the matching member: ### `VibeObject` / `VibeArray` Both preserve insertion order. Objects are last-wins on duplicate keys (matching the parser). Per the **First Law of VIBE**, arrays only ever hold scalars. ### `VibeErrorCode` / `VibeError` The first eleven codes match the [specification's error model](../SPECIFICATION.md#error-model) 1:1. ### `VibeLimits` See [Resource Limits](#resource-limits). --- ## Parser Lifecycle Create once, reuse across many parses. `vibe_parser_free()` is NULL-safe and also releases the last error message. --- ## Parsing | Function | Use it when | |----------|-------------| | `vibe_parse_string` | You have a NUL-terminated string (embedded NUL truncates). | | `vibe_parse_buffer` | You have `(ptr, len)` and the data may contain NUL bytes. | | `vibe_parse_file` | You want the library to read the file for you. | | `vibe_parse` | One-shot; no parser object. Fills `out_error` (if non-NULL) with an **owned** copy you free via `vibe_error_free()`. | All return the root `OBJECT` on success or `NULL` on failure. The input is not retained after the call returns. Empty, whitespace-only, and comment-only documents parse to a valid **empty object**. --- ## Error Handling `vibe_get_last_error()` returns a shallow copy whose `message` points into the parser's own buffer — **do not** free it, and treat it as valid only until the next parse. The `VibeError` filled by `vibe_parse()` owns its message and **must** be released with `vibe_error_free()`. | `VibeErrorCode` | `vibe_error_code_string()` | Meaning | |-----------------|----------------------------|---------| | `VIBE_ERROR_ENCODING` | `encoding-error` | e.g. a leading UTF-8 BOM | | `VIBE_ERROR_ILLEGAL_CHARACTER` | `illegal-character` | byte not allowed at that position | | `VIBE_ERROR_UNEXPECTED_TOKEN` | `unexpected-token` | token where another was required (incl. empty key) | | `VIBE_ERROR_UNCLOSED_OBJECT` | `unclosed-object` | missing `}` | | `VIBE_ERROR_UNCLOSED_ARRAY` | `unclosed-array` | missing `]` | | `VIBE_ERROR_UNTERMINATED_STRING` | `unterminated-string` | quote never closed | | `VIBE_ERROR_NESTED_CONTAINER` | `nested-container` | object/array inside an array | | `VIBE_ERROR_INVALID_ESCAPE` | `invalid-escape` | unknown `\x` in a quoted string | | `VIBE_ERROR_INVALID_NUMBER` | `invalid-number` | out-of-range or malformed number | | `VIBE_ERROR_LIMIT_EXCEEDED` | `limit-exceeded` | a `VibeLimits` bound was hit | | `VIBE_ERROR_IO` | `io-error` | file could not be read | | `VIBE_ERROR_OUT_OF_MEMORY` | `out-of-memory` | allocation failed | --- ## Resource Limits libvibe bounds every parse so untrusted input cannot exhaust memory or the stack. Defaults are the **required minimums** from the spec; raise them when you trust the source. Exceeding any bound yields `VIBE_ERROR_LIMIT_EXCEEDED`. --- ## Allocator Hooks Optional. Install a custom allocator **once at startup**, before any other libvibe call, and do not change it while values are live (it frees them too). Passing `NULL` for a slot resets it to the C standard library. --- ## Value Constructors Each returns a new heap value (or `NULL` on allocation failure). String constructors copy their input. Build trees, then `vibe_emit()` them. --- ## Value Inspection & Equality When you already hold a `VibeValue *` (e.g. from `vibe_array_get` or `vibe_object_get`), these read it **without a dotted path** and without touching the union directly. The typed readers return a sentinel (`0` / `false` / `NULL`) on type mismatch; the `*_or` forms take an explicit fallback. **Deep structural equality:** Equal iff same type and equal contents. Scalars compare by value (strings byte-for-byte, floats bit-exact so `NaN == NaN` and `+0.0 != -0.0`); note `1` (integer) and `1.0` (float) are **not** equal — different types. Arrays compare element-wise in order; objects compare as **key/value sets regardless of insertion order**. `NULL == NULL`. A `vibe_value_clone(x)` always equals `x`. --- ## Path Access Dotted paths walk nested objects (`"server.tls.port"`). The plain readers return a zero/NULL sentinel when the path is missing or the wrong type; the `*_or` variants take the fallback explicitly. Passing `NULL` as the path returns/reads the value itself — handy for typing a value you already hold: `vibe_get_int(elem, NULL)`. --- ## Container Operations **Objects** — insertion-ordered key/value maps with amortised O(1) lookup: **Arrays** — ordered lists of **scalars only** (the First Law): `vibe_object_set()` and `vibe_array_push()` return `bool` — `true` when the value is stored, `false` on allocation failure, a NULL argument, or (for push) a First-Law violation. **In every case the passed value's ownership is consumed:** on failure it is freed, so a rejected mutation never leaks and you never double-free. `vibe_object_set()` replaces an existing key (freeing the old value). Pushing an object or array into an array is **refused** (the value is freed, the array is unchanged, the call returns `false`) — arrays hold scalars, always. --- ## Emitting Serialise a value tree back to **canonical VIBE text**. The result re-parses to an equal tree, and `vibe fmt` is idempotent. Notes: keys/strings are emitted bare when they re-lex unambiguously, quoted (with escapes) otherwise; floats use plain decimal (VIBE has no exponent syntax), so values of extreme magnitude are outside the round-trip guarantee. --- ## Miscellaneous --- ## Command-Line Tool Installed as `vibe`: Exit codes: `0` success, `1` parse/lookup error, `2` usage error, `3` I/O error. --- ## Examples ### Read a config Build (installed): `cc app.c $(pkg-config --cflags --libs vibe) -o app` Build (vendored): `cc app.c vibe.c -o app` ### Build a tree and emit it ### Validate untrusted input strictly --- ## Security & Memory Safety libvibe is designed to parse **hostile input** without crashing, leaking, or corrupting memory. The guarantees below are enforced by the code and exercised by the `Security & Robustness` group of the test suite (fuzzing, adversarial UTF-8, resource-exhaustion, and NULL-argument cases), which runs clean under `-fsanitize=address,undefined -fno-sanitize-recover=all`. - **No integer-overflow-driven heap overflow.** Every capacity growth and every `count * size` allocation goes through overflow-checked helpers; a request that would wrap `size_t` is refused (allocation fails, the original block is left intact) rather than silently under-allocating. - **Fail closed.** Malformed input never yields a partial tree. On any error the parser frees everything it built and returns `NULL` with a stable `VibeErrorCode` and a `line:column`. There is no "lenient mode". - **Bounded by construction.** Depth, document size, key/string length, array and object cardinality, and numeric-token length are all capped (`VibeLimits`) so untrusted input cannot exhaust memory or the stack. The parse loop is iterative (an explicit frame stack), so deep nesting hits the depth limit instead of overflowing the C stack. - **Whole-input UTF-8 validation.** The entire byte stream is validated as well-formed UTF-8 (RFC 3629 — no overlongs, surrogates, or code points above U+10FFFF) with no embedded NUL and no byte-order mark, *before* any value is built. Ill-formed bytes are rejected with `VIBE_ERROR_ENCODING`. - **No silent numeric coercion.** An integer outside `int64_t`, or a float outside `double`, is a hard parse error — never a wrapped or saturated value. - **NULL-safe API.** Every public function tolerates `NULL` handles: readers return a sentinel, mutators no-op, and `vibe_value_free(NULL)` / `vibe_free(NULL)` are no-ops. - **Ownership is explicit.** `vibe_object_set` / `vibe_array_push` **take ownership** of the value (and free it on allocation failure or First-Law rejection, so a rejected value is never leaked). `*_get` / `*_value_at` return **borrowed** pointers you must not free. --- For the language itself, see the [SPECIFICATION](../SPECIFICATION.md) and the [conformance suite](../tests/conformance). Keep calm and VIBE on. 🌊