forked from WHC/Hermit
Vendored sashidown, OpenAI-compatible env (OPENAI_*), and bootstrap scripts. Co-authored-by: Cursor <cursoragent@cursor.com>
45 lines
1.0 KiB
C
45 lines
1.0 KiB
C
#include "sashidown.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
|
|
char *cmark_markdown_to_html(const char *text, int len)
|
|
{
|
|
if (!text)
|
|
return NULL;
|
|
if (len < 0)
|
|
len = (int)strlen(text);
|
|
|
|
size_t in_len = (size_t)len;
|
|
/* HTML typically expands; grow until parse fits (thread-safe, no static buf). */
|
|
size_t cap = in_len * 4 + 256;
|
|
if (cap < 1024)
|
|
cap = 1024;
|
|
|
|
for (;;) {
|
|
char *buf = (char *)malloc(cap);
|
|
if (!buf)
|
|
return NULL;
|
|
|
|
size_t out_len = 0;
|
|
if (sashidown_parse(text, in_len, buf, cap, &out_len) == 0) {
|
|
char *fit = (char *)realloc(buf, out_len + 1);
|
|
return fit ? fit : buf;
|
|
}
|
|
|
|
free(buf);
|
|
if (cap > SIZE_MAX / 2)
|
|
return NULL;
|
|
size_t next = cap * 2;
|
|
/* Bail if expansion is absurd relative to input. */
|
|
if (next > in_len * 64 + 4096)
|
|
return NULL;
|
|
cap = next;
|
|
}
|
|
}
|
|
|
|
void sashidown_free(void *ptr)
|
|
{
|
|
free(ptr);
|
|
}
|