| 1 | /* |
| 2 | * wh_buf.h - a tiny growable string buffer with JSON escapes. |
| 3 | * |
| 4 | * Every response this plugin writes is assembled in one of these and emitted |
| 5 | * with a single fputs(). That is deliberate: a body built half in a buffer and |
| 6 | * half with printf() silently reorders itself (and a half-written JSON object |
| 7 | * is not JSON), which is a bug that only a parser catches. |
| 8 | * |
| 9 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 10 | */ |
| 11 | #ifndef SX_WH_BUF_H |
| 12 | #define SX_WH_BUF_H |
| 13 | |
| 14 | #include <stddef.h> |
| 15 | |
| 16 | typedef struct { |
| 17 | char *p; |
| 18 | size_t n; |
| 19 | size_t cap; |
| 20 | int oom; |
| 21 | } wh_buf; |
| 22 | |
| 23 | void wh_buf_init(wh_buf *b); |
| 24 | void wh_buf_free(wh_buf *b); |
| 25 | void wh_buf_add(wh_buf *b, const char *s, size_t n); |
| 26 | void wh_buf_str(wh_buf *b, const char *s); |
| 27 | void wh_buf_printf(wh_buf *b, const char *fmt, ...); |
| 28 | const char *wh_buf_cstr(wh_buf *b); /* NUL-terminates; "" when empty */ |
| 29 | |
| 30 | /* Append `s` as a JSON string literal (quoted, escaped, NUL-safe). */ |
| 31 | void wh_buf_json_str(wh_buf *b, const char *s); |
| 32 | /* Append a JSON object key: ,"name": -- the caller writes the value and the |
| 33 | * leading comma is decided by `first`. */ |
| 34 | void wh_buf_key(wh_buf *b, const char *key, int *first); |
| 35 | |
| 36 | /* HTML-escape into a fresh buffer (caller frees with free()). */ |
| 37 | char *wh_html_escape(const char *s); |
| 38 | |
| 39 | #endif /* SX_WH_BUF_H */ |