File size: 10,495 Bytes
d086025
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# 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. 🌊