| 1 | /* |
| 2 | * ws_buf.c - the small string toolkit plugin_websearch runs on: a growable |
| 3 | * byte buffer with real error returns, JSON escaping, an HTML entity decoder, |
| 4 | * a tag stripper, and URL encode/decode. |
| 5 | * |
| 6 | * Why hand-written instead of sweeping the page with regexes: a search result |
| 7 | * page is attacker-influenced text that ends up inside a JSON document the |
| 8 | * model reads. Every conversion here is bounded (no unbounded stack copy), and |
| 9 | * the JSON escaper is the only path by which page text becomes output, so a |
| 10 | * quote or a control character in a title cannot break the document. |
| 11 | * |
| 12 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 13 | */ |
| 14 | |
| 15 | #if defined(_WIN32) && !defined(_WIN32_WINNT) |
| 16 | # define _WIN32_WINNT 0x0601 |
| 17 | #endif |
| 18 | #if !defined(_WIN32) |
| 19 | # if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L |
| 20 | # undef _POSIX_C_SOURCE |
| 21 | # define _POSIX_C_SOURCE 200809L |
| 22 | # endif |
| 23 | # ifndef _DEFAULT_SOURCE |
| 24 | # define _DEFAULT_SOURCE 1 |
| 25 | # endif |
| 26 | #endif |
| 27 | |
| 28 | #include "ws.h" |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | /* ------------------------------------------------------------------ buffer */ |
| 37 | |
| 38 | void ws_buf_init(ws_buf *b) |
| 39 | { |
| 40 | b->p = NULL; |
| 41 | b->n = 0; |
| 42 | b->cap = 0; |
| 43 | } |
| 44 | |
| 45 | int ws_buf_need(ws_buf *b, size_t extra) |
| 46 | { |
| 47 | size_t want; |
| 48 | char *np; |
| 49 | |
| 50 | if (b->n + extra + 1 <= b->cap) return 0; |
| 51 | want = b->cap ? b->cap : 256; |
| 52 | while (want < b->n + extra + 1) { |
| 53 | if (want > (size_t)1 << 30) { |
| 54 | want = b->n + extra + 1; |
| 55 | break; |
| 56 | } |
| 57 | want *= 2; |
| 58 | } |
| 59 | np = (char *)realloc(b->p, want); |
| 60 | if (!np) return -1; |
| 61 | b->p = np; |
| 62 | b->cap = want; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | int ws_buf_add(ws_buf *b, const char *s, size_t n) |
| 67 | { |
| 68 | if (!s || n == 0) { |
| 69 | if (ws_buf_need(b, 0) != 0) return -1; |
| 70 | b->p[b->n] = '\0'; |
| 71 | return 0; |
| 72 | } |
| 73 | if (ws_buf_need(b, n) != 0) return -1; |
| 74 | memcpy(b->p + b->n, s, n); |
| 75 | b->n += n; |
| 76 | b->p[b->n] = '\0'; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int ws_buf_puts(ws_buf *b, const char *s) |
| 81 | { |
| 82 | return s ? ws_buf_add(b, s, strlen(s)) : ws_buf_add(b, NULL, 0); |
| 83 | } |
| 84 | |
| 85 | int ws_buf_putc(ws_buf *b, int c) |
| 86 | { |
| 87 | char ch = (char)c; |
| 88 | if (ws_buf_need(b, 1) != 0) return -1; |
| 89 | b->p[b->n++] = ch; |
| 90 | b->p[b->n] = '\0'; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int ws_buf_printf(ws_buf *b, const char *fmt, ...) |
| 95 | { |
| 96 | va_list ap, ap2; |
| 97 | int need; |
| 98 | |
| 99 | va_start(ap, fmt); |
| 100 | va_copy(ap2, ap); |
| 101 | need = vsnprintf(NULL, 0, fmt, ap); |
| 102 | va_end(ap); |
| 103 | if (need < 0) { va_end(ap2); return -1; } |
| 104 | if (ws_buf_need(b, (size_t)need) != 0) { va_end(ap2); return -1; } |
| 105 | vsnprintf(b->p + b->n, b->cap - b->n, fmt, ap2); |
| 106 | va_end(ap2); |
| 107 | b->n += (size_t)need; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | const char *ws_buf_str(const ws_buf *b) |
| 112 | { |
| 113 | return b->p ? b->p : ""; |
| 114 | } |
| 115 | |
| 116 | void ws_buf_free(ws_buf *b) |
| 117 | { |
| 118 | free(b->p); |
| 119 | b->p = NULL; |
| 120 | b->n = b->cap = 0; |
| 121 | } |
| 122 | |
| 123 | /* ------------------------------------------------------------- JSON escape */ |
| 124 | |
| 125 | /* Char by char, so no intermediate buffer can truncate a long snippet and no |
| 126 | * control byte can survive into the document. */ |
| 127 | int ws_json_str(ws_buf *b, const char *s) |
| 128 | { |
| 129 | size_t i; |
| 130 | |
| 131 | if (ws_buf_putc(b, '"') != 0) return -1; |
| 132 | if (!s) return ws_buf_putc(b, '"'); |
| 133 | for (i = 0; s[i]; i++) { |
| 134 | unsigned char ch = (unsigned char)s[i]; |
| 135 | if (ch == '"' || ch == '\\') { |
| 136 | if (ws_buf_putc(b, '\\') != 0 || ws_buf_putc(b, ch) != 0) return -1; |
| 137 | } else if (ch == '\n') { |
| 138 | if (ws_buf_add(b, "\\n", 2) != 0) return -1; |
| 139 | } else if (ch == '\r') { |
| 140 | if (ws_buf_add(b, "\\r", 2) != 0) return -1; |
| 141 | } else if (ch == '\t') { |
| 142 | if (ws_buf_add(b, "\\t", 2) != 0) return -1; |
| 143 | } else if (ch < 0x20) { |
| 144 | if (ws_buf_printf(b, "\\u%04x", ch) != 0) return -1; |
| 145 | } else { |
| 146 | if (ws_buf_putc(b, ch) != 0) return -1; |
| 147 | } |
| 148 | } |
| 149 | return ws_buf_putc(b, '"'); |
| 150 | } |
| 151 | |
| 152 | /* --------------------------------------------------------------- html text */ |
| 153 | |
| 154 | void ws_copy(char *dst, size_t capsz, const char *src) |
| 155 | { |
| 156 | size_t n; |
| 157 | if (!dst || capsz == 0) return; |
| 158 | if (!src) { dst[0] = '\0'; return; } |
| 159 | n = strlen(src); |
| 160 | if (n >= capsz) n = capsz - 1; |
| 161 | memcpy(dst, src, n); |
| 162 | dst[n] = '\0'; |
| 163 | } |
| 164 | |
| 165 | int ws_ieq(const char *a, const char *b) |
| 166 | { |
| 167 | if (!a || !b) return 0; |
| 168 | while (*a && *b) { |
| 169 | if (tolower((unsigned char)*a) != tolower((unsigned char)*b)) return 0; |
| 170 | a++; b++; |
| 171 | } |
| 172 | return *a == '\0' && *b == '\0'; |
| 173 | } |
| 174 | |
| 175 | int ws_iprefix(const char *s, const char *prefix) |
| 176 | { |
| 177 | if (!s || !prefix) return 0; |
| 178 | while (*prefix) { |
| 179 | if (!*s) return 0; |
| 180 | if (tolower((unsigned char)*s) != tolower((unsigned char)*prefix)) return 0; |
| 181 | s++; prefix++; |
| 182 | } |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | void ws_collapse(char *s) |
| 187 | { |
| 188 | size_t r = 0, w = 0; |
| 189 | int pending_space = 0; |
| 190 | |
| 191 | if (!s) return; |
| 192 | while (s[r]) { |
| 193 | unsigned char ch = (unsigned char)s[r++]; |
| 194 | if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\f' || |
| 195 | ch == '\v') { |
| 196 | if (w > 0) pending_space = 1; |
| 197 | continue; |
| 198 | } |
| 199 | if (pending_space) { s[w++] = ' '; pending_space = 0; } |
| 200 | s[w++] = (char)ch; |
| 201 | } |
| 202 | s[w] = '\0'; |
| 203 | } |
| 204 | |
| 205 | const char *ws_memmem(const char *hay, size_t hlen, const char *needle) |
| 206 | { |
| 207 | size_t nl = strlen(needle); |
| 208 | size_t i; |
| 209 | |
| 210 | if (!hay || !needle || nl == 0 || nl > hlen) return NULL; |
| 211 | for (i = 0; i + nl <= hlen; i++) |
| 212 | if (hay[i] == needle[0] && memcmp(hay + i, needle, nl) == 0) |
| 213 | return hay + i; |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | static int hexval(int c) |
| 218 | { |
| 219 | if (c >= '0' && c <= '9') return c - '0'; |
| 220 | if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
| 221 | if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | /* Decodes one entity starting at s (which points at '&'). Returns the number |
| 226 | * of input bytes consumed, or 0 when this is not an entity we know. */ |
| 227 | static size_t entity_at(const char *s, const char **utf8, char *one, size_t onecap) |
| 228 | { |
| 229 | const char *semi = strchr(s, ';'); |
| 230 | size_t len; |
| 231 | char name[12]; |
| 232 | size_t i; |
| 233 | |
| 234 | (void)onecap; |
| 235 | if (!semi) return 0; |
| 236 | len = (size_t)(semi - s - 1); |
| 237 | if (len == 0 || len >= sizeof name) return 0; |
| 238 | for (i = 0; i < len; i++) name[i] = (char)tolower((unsigned char)s[1 + i]); |
| 239 | name[len] = '\0'; |
| 240 | |
| 241 | *utf8 = NULL; |
| 242 | if (strcmp(name, "amp") == 0) *utf8 = "&"; |
| 243 | else if (strcmp(name, "lt") == 0) *utf8 = "<"; |
| 244 | else if (strcmp(name, "gt") == 0) *utf8 = ">"; |
| 245 | else if (strcmp(name, "quot") == 0) *utf8 = "\""; |
| 246 | else if (strcmp(name, "apos") == 0) *utf8 = "'"; |
| 247 | else if (strcmp(name, "nbsp") == 0) *utf8 = " "; |
| 248 | else if (strcmp(name, "ndash") == 0) *utf8 = "-"; |
| 249 | else if (strcmp(name, "mdash") == 0) *utf8 = "--"; |
| 250 | else if (strcmp(name, "hellip") == 0) *utf8 = "..."; |
| 251 | else if (strcmp(name, "lsquo") == 0 || strcmp(name, "rsquo") == 0) |
| 252 | *utf8 = "'"; |
| 253 | else if (strcmp(name, "ldquo") == 0 || strcmp(name, "rdquo") == 0) |
| 254 | *utf8 = "\""; |
| 255 | else if (name[0] == '#') { |
| 256 | /* Numeric: ' ' — encode back to UTF-8 rather than guessing |
| 257 | * a single byte, so a CJK page does not turn into latin-1 garbage. */ |
| 258 | static char enc[5]; |
| 259 | unsigned long cp = 0; |
| 260 | int base = 10; |
| 261 | const char *p = name + 1; |
| 262 | if (*p == 'x' || *p == 'X') { base = 16; p++; } |
| 263 | if (!*p) return 0; |
| 264 | for (; *p; p++) { |
| 265 | int v = hexval((unsigned char)*p); |
| 266 | if (v < 0 || (base == 10 && v > 9)) return 0; |
| 267 | cp = cp * (unsigned long)base + (unsigned long)v; |
| 268 | if (cp > 0x10FFFFul) return 0; |
| 269 | } |
| 270 | if (cp == 0) return 0; |
| 271 | if (cp < 0x80) { |
| 272 | enc[0] = (char)cp; enc[1] = '\0'; |
| 273 | } else if (cp < 0x800) { |
| 274 | enc[0] = (char)(0xC0 | (cp >> 6)); |
| 275 | enc[1] = (char)(0x80 | (cp & 0x3F)); |
| 276 | enc[2] = '\0'; |
| 277 | } else if (cp < 0x10000) { |
| 278 | enc[0] = (char)(0xE0 | (cp >> 12)); |
| 279 | enc[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 280 | enc[2] = (char)(0x80 | (cp & 0x3F)); |
| 281 | enc[3] = '\0'; |
| 282 | } else { |
| 283 | enc[0] = (char)(0xF0 | (cp >> 18)); |
| 284 | enc[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); |
| 285 | enc[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 286 | enc[3] = (char)(0x80 | (cp & 0x3F)); |
| 287 | enc[4] = '\0'; |
| 288 | } |
| 289 | *utf8 = enc; |
| 290 | } else { |
| 291 | one[0] = '\0'; |
| 292 | return 0; |
| 293 | } |
| 294 | return len + 2; |
| 295 | } |
| 296 | |
| 297 | void ws_html_unescape(char *out, size_t outsz, const char *s) |
| 298 | { |
| 299 | size_t o = 0; |
| 300 | |
| 301 | if (!out || outsz == 0) return; |
| 302 | out[0] = '\0'; |
| 303 | if (!s) return; |
| 304 | while (*s && o + 1 < outsz) { |
| 305 | if (*s == '&') { |
| 306 | char one[1]; |
| 307 | const char *rep = NULL; |
| 308 | size_t used = entity_at(s, &rep, one, sizeof one); |
| 309 | if (used && rep) { |
| 310 | size_t rl = strlen(rep); |
| 311 | if (o + rl + 1 > outsz) break; |
| 312 | memcpy(out + o, rep, rl); |
| 313 | o += rl; |
| 314 | s += used; |
| 315 | continue; |
| 316 | } |
| 317 | } |
| 318 | out[o++] = *s++; |
| 319 | } |
| 320 | out[o] = '\0'; |
| 321 | } |
| 322 | |
| 323 | /* Tag stripper. Removes <...> and decodes entities, then collapses space. |
| 324 | * <script>/<style> content is dropped wholesale — a page's inline JS would |
| 325 | * otherwise arrive as "result text" in the model's context. */ |
| 326 | void ws_html_text(char *out, size_t outsz, const char *html) |
| 327 | { |
| 328 | ws_buf tmp; |
| 329 | const char *p; |
| 330 | int in_tag = 0; |
| 331 | |
| 332 | if (!out || outsz == 0) return; |
| 333 | out[0] = '\0'; |
| 334 | if (!html) return; |
| 335 | |
| 336 | ws_buf_init(&tmp); |
| 337 | for (p = html; *p; p++) { |
| 338 | /* Order matters: a character inside a tag must never reach the output, |
| 339 | * so the in_tag test comes BEFORE the plain-text copy. (Checking the |
| 340 | * other way round emits "b>rust/b>" for "<b>rust</b>" — the tag name |
| 341 | * and the closing bracket survive while the angle brackets vanish.) |
| 342 | * A tag boundary always becomes one space, so "a<b>c" reads "a c". */ |
| 343 | if (in_tag) { |
| 344 | if (*p == '>') { in_tag = 0; ws_buf_putc(&tmp, ' '); } |
| 345 | continue; |
| 346 | } |
| 347 | if (*p != '<') { ws_buf_putc(&tmp, *p); continue; } |
| 348 | if (ws_iprefix(p, "<script") || ws_iprefix(p, "<style")) { |
| 349 | const char *close = ws_iprefix(p, "<script") ? "</script" : "</style"; |
| 350 | const char *e = strstr(p, close); |
| 351 | p = e ? e + strlen(close) - 1 : p + strlen(p) - 1; |
| 352 | ws_buf_putc(&tmp, ' '); |
| 353 | continue; |
| 354 | } |
| 355 | in_tag = 1; |
| 356 | } |
| 357 | ws_html_unescape(out, outsz, ws_buf_str(&tmp)); |
| 358 | ws_collapse(out); |
| 359 | ws_buf_free(&tmp); |
| 360 | } |
| 361 | |
| 362 | /* ---------------------------------------------------------------- url code */ |
| 363 | |
| 364 | void ws_url_decode(char *out, size_t outsz, const char *s) |
| 365 | { |
| 366 | size_t o = 0; |
| 367 | |
| 368 | if (!out || outsz == 0) return; |
| 369 | out[0] = '\0'; |
| 370 | if (!s) return; |
| 371 | while (*s && o + 1 < outsz) { |
| 372 | if (*s == '%' && hexval((unsigned char)s[1]) >= 0 && |
| 373 | hexval((unsigned char)s[2]) >= 0) { |
| 374 | out[o++] = (char)((hexval((unsigned char)s[1]) << 4) | |
| 375 | hexval((unsigned char)s[2])); |
| 376 | s += 3; |
| 377 | } else if (*s == '+') { |
| 378 | out[o++] = ' '; |
| 379 | s++; |
| 380 | } else { |
| 381 | out[o++] = *s++; |
| 382 | } |
| 383 | } |
| 384 | out[o] = '\0'; |
| 385 | } |
| 386 | |
| 387 | void ws_url_encode(const char *in, ws_buf *out) |
| 388 | { |
| 389 | static const char hex[] = "0123456789ABCDEF"; |
| 390 | size_t i; |
| 391 | |
| 392 | if (!in) return; |
| 393 | for (i = 0; in[i]; i++) { |
| 394 | unsigned char ch = (unsigned char)in[i]; |
| 395 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || |
| 396 | (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' || ch == '.' || |
| 397 | ch == '~') { |
| 398 | ws_buf_putc(out, ch); |
| 399 | } else { |
| 400 | char esc[3]; |
| 401 | esc[0] = '%'; |
| 402 | esc[1] = hex[ch >> 4]; |
| 403 | esc[2] = hex[ch & 0x0F]; |
| 404 | ws_buf_add(out, esc, 3); |
| 405 | } |
| 406 | } |
| 407 | } |