| 1 | /* |
| 2 | * wh_buf.c - growable buffer + JSON/HTML escapes. See wh_buf.h. |
| 3 | * |
| 4 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 5 | */ |
| 6 | #define _POSIX_C_SOURCE 200809L |
| 7 | |
| 8 | #include "wh_buf.h" |
| 9 | |
| 10 | #include <stdarg.h> |
| 11 | #include <stdio.h> |
| 12 | #include <stdlib.h> |
| 13 | #include <string.h> |
| 14 | |
| 15 | void wh_buf_init(wh_buf *b) |
| 16 | { |
| 17 | b->p = NULL; |
| 18 | b->n = 0; |
| 19 | b->cap = 0; |
| 20 | b->oom = 0; |
| 21 | } |
| 22 | |
| 23 | void wh_buf_free(wh_buf *b) |
| 24 | { |
| 25 | if (!b) return; |
| 26 | free(b->p); |
| 27 | b->p = NULL; |
| 28 | b->n = 0; |
| 29 | b->cap = 0; |
| 30 | } |
| 31 | |
| 32 | static int reserve(wh_buf *b, size_t extra) |
| 33 | { |
| 34 | size_t want; |
| 35 | char *np; |
| 36 | |
| 37 | if (b->oom) return 0; |
| 38 | want = b->n + extra + 1; |
| 39 | if (want <= b->cap) return 1; |
| 40 | { |
| 41 | size_t cap = b->cap ? b->cap : 256; |
| 42 | while (cap < want) { |
| 43 | if (cap > (size_t)1 << 30) { b->oom = 1; return 0; } |
| 44 | cap *= 2; |
| 45 | } |
| 46 | np = (char *)realloc(b->p, cap); |
| 47 | if (!np) { b->oom = 1; return 0; } |
| 48 | b->p = np; |
| 49 | b->cap = cap; |
| 50 | } |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | void wh_buf_add(wh_buf *b, const char *s, size_t n) |
| 55 | { |
| 56 | if (!n) return; |
| 57 | if (!reserve(b, n)) return; |
| 58 | memcpy(b->p + b->n, s, n); |
| 59 | b->n += n; |
| 60 | b->p[b->n] = '\0'; |
| 61 | } |
| 62 | |
| 63 | void wh_buf_str(wh_buf *b, const char *s) |
| 64 | { |
| 65 | if (!s) return; |
| 66 | wh_buf_add(b, s, strlen(s)); |
| 67 | } |
| 68 | |
| 69 | void wh_buf_printf(wh_buf *b, const char *fmt, ...) |
| 70 | { |
| 71 | va_list ap; |
| 72 | char small[512]; |
| 73 | int need; |
| 74 | |
| 75 | va_start(ap, fmt); |
| 76 | need = vsnprintf(small, sizeof small, fmt, ap); |
| 77 | va_end(ap); |
| 78 | if (need < 0) return; |
| 79 | if ((size_t)need < sizeof small) { wh_buf_add(b, small, (size_t)need); return; } |
| 80 | if (!reserve(b, (size_t)need)) return; |
| 81 | va_start(ap, fmt); |
| 82 | vsnprintf(b->p + b->n, (size_t)need + 1, fmt, ap); |
| 83 | va_end(ap); |
| 84 | b->n += (size_t)need; |
| 85 | } |
| 86 | |
| 87 | const char *wh_buf_cstr(wh_buf *b) |
| 88 | { |
| 89 | if (!b->p) { |
| 90 | if (!reserve(b, 0)) return ""; |
| 91 | b->p[0] = '\0'; |
| 92 | } |
| 93 | return b->p; |
| 94 | } |
| 95 | |
| 96 | void wh_buf_json_str(wh_buf *b, const char *s) |
| 97 | { |
| 98 | wh_buf_add(b, "\"", 1); |
| 99 | if (s) { |
| 100 | for (; *s; s++) { |
| 101 | unsigned char c = (unsigned char)*s; |
| 102 | switch (c) { |
| 103 | case '"': wh_buf_add(b, "\\\"", 2); break; |
| 104 | case '\\': wh_buf_add(b, "\\\\", 2); break; |
| 105 | case '\n': wh_buf_add(b, "\\n", 2); break; |
| 106 | case '\r': wh_buf_add(b, "\\r", 2); break; |
| 107 | case '\t': wh_buf_add(b, "\\t", 2); break; |
| 108 | case '\b': wh_buf_add(b, "\\b", 2); break; |
| 109 | case '\f': wh_buf_add(b, "\\f", 2); break; |
| 110 | default: |
| 111 | if (c < 0x20) wh_buf_printf(b, "\\u%04x", (unsigned)c); |
| 112 | else wh_buf_add(b, (const char *)&c, 1); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | wh_buf_add(b, "\"", 1); |
| 117 | } |
| 118 | |
| 119 | void wh_buf_key(wh_buf *b, const char *key, int *first) |
| 120 | { |
| 121 | /* `first` starts at 1 for a fresh object and is cleared by this call, so |
| 122 | * the separator is emitted for every key after the first one. */ |
| 123 | if (first) { |
| 124 | if (!*first) wh_buf_add(b, ",", 1); |
| 125 | *first = 0; |
| 126 | } |
| 127 | wh_buf_json_str(b, key); |
| 128 | wh_buf_add(b, ":", 1); |
| 129 | } |
| 130 | |
| 131 | char *wh_html_escape(const char *s) |
| 132 | { |
| 133 | wh_buf b; |
| 134 | wh_buf_init(&b); |
| 135 | if (s) { |
| 136 | for (; *s; s++) { |
| 137 | switch (*s) { |
| 138 | case '&': wh_buf_str(&b, "&"); break; |
| 139 | case '<': wh_buf_str(&b, "<"); break; |
| 140 | case '>': wh_buf_str(&b, ">"); break; |
| 141 | case '"': wh_buf_str(&b, """); break; |
| 142 | case '\'': wh_buf_str(&b, "'"); break; |
| 143 | default: wh_buf_add(&b, s, 1); break; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | if (!b.p) { |
| 148 | char *r = (char *)malloc(1); |
| 149 | if (r) r[0] = '\0'; |
| 150 | return r; |
| 151 | } |
| 152 | return b.p; |
| 153 | } |