Files
lotherk 83741ce6eb dtchat: add the conversation model and text helpers (T1.4)
conv.c: accounts, conversations, topics, idempotent add, a 2000-message ring buffer and unread counts that ignore our own/system messages. text.c: UTF-8 validation, C0 cleaning, mIRC formatting strip and iconv display with ASCII//TRANSLIT and '?' fallbacks. test_conv and test_text cover duplicate adds, ordering, the ring boundary, flags/unread, IRC codes, CR/CRLF and ISO-8859-1/ASCII output. make mcde-host exit=0; all six dtchat tests report 'all ok'; ASan/UBSan clean.
2026-11-16 17:18:12 +00:00

259 lines
4.8 KiB
C

/*
* text.c -- UTF-8 checks, IRC formatting and the display conversion
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Storage and the protocol adapters keep UTF-8. The Motif UI asks
* dc_text_to_display() for the session's charset; iconv does the work,
* with ASCII//TRANSLIT and finally '?' as fallbacks. The IRC adapter uses
* dc_text_irc_strip() before the text reaches the model.
*/
#include "dc_internal.h"
#include <ctype.h>
#include <errno.h>
#include <iconv.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
typedef struct {
char *p;
size_t len;
size_t cap;
} TextBuf;
static int
tb_append(TextBuf *buf, const char *s, size_t n)
{
size_t need, cap;
char *next;
if (n == 0)
return 0;
if (n > SIZE_MAX - buf->len - 1)
return -1;
need = buf->len + n + 1;
if (need > buf->cap) {
cap = buf->cap != 0 ? buf->cap : 64;
while (cap < need) {
if (cap > SIZE_MAX / 2)
return -1;
cap *= 2;
}
next = realloc(buf->p, cap);
if (next == NULL)
return -1;
buf->p = next;
buf->cap = cap;
}
memcpy(buf->p + buf->len, s, n);
buf->len += n;
buf->p[buf->len] = '\0';
return 0;
}
/*
* Length of the UTF-8 sequence at P. For invalid input it returns 1, so a
* fallback loop always makes progress.
*/
static size_t
utf8_seq_len(const unsigned char *p, size_t n)
{
if (n == 0)
return 0;
if (p[0] < 0x80)
return 1;
if ((p[0] & 0xE0) == 0xC0 && n >= 2)
return 2;
if ((p[0] & 0xF0) == 0xE0 && n >= 3)
return 3;
if ((p[0] & 0xF8) == 0xF0 && n >= 4)
return 4;
return 1;
}
static char *
ascii_fallback(const char *s)
{
size_t n = strlen(s);
size_t i = 0, o = 0;
char *out = malloc(n + 1);
if (out == NULL)
return NULL;
while (i < n) {
unsigned char c = (unsigned char)s[i];
if (c < 0x80) {
out[o++] = (char)c;
i++;
} else {
out[o++] = '?';
i += utf8_seq_len((const unsigned char *)s + i, n - i);
}
}
out[o] = '\0';
return out;
}
int
dc_text_valid_utf8(const char *s)
{
return s != NULL && dc_utf8_valid(s, strlen(s));
}
char *
dc_text_clean(const char *s)
{
size_t i, o = 0;
char *out;
if (s == NULL)
return NULL;
out = malloc(strlen(s) + 1);
if (out == NULL)
return NULL;
for (i = 0; s[i] != '\0'; i++) {
unsigned char c = (unsigned char)s[i];
if (c == '\r') {
if (s[i + 1] == '\n')
i++;
out[o++] = '\n';
} else if (c == '\n' || c == '\t') {
out[o++] = (char)c;
} else if (c < 0x20 || c == 0x7f) {
continue;
} else {
out[o++] = (char)c;
}
}
out[o] = '\0';
return out;
}
char *
dc_text_irc_strip(const char *s)
{
size_t i = 0, o = 0;
char *out;
if (s == NULL)
return NULL;
out = malloc(strlen(s) + 1);
if (out == NULL)
return NULL;
while (s[i] != '\0') {
unsigned char c = (unsigned char)s[i];
if (c == 0x02 || c == 0x0f || c == 0x16 || c == 0x1d ||
c == 0x1f) {
i++;
continue;
}
if (c == 0x03) {
int k;
i++;
for (k = 0; k < 2 && isdigit((unsigned char)s[i]); k++)
i++;
if (s[i] == ',' && isdigit((unsigned char)s[i + 1])) {
i++;
for (k = 0; k < 2 &&
isdigit((unsigned char)s[i]); k++)
i++;
}
continue;
}
out[o++] = (char)c;
i++;
}
out[o] = '\0';
return out;
}
static char *
iconv_to(const char *s, const char *charset)
{
iconv_t cd;
TextBuf out = { NULL, 0, 0 };
char *in = (char *)s;
size_t inleft = strlen(s);
char tmp[512];
cd = iconv_open(charset, "UTF-8");
if (cd == (iconv_t)-1)
return NULL;
while (inleft > 0) {
char *outp = tmp;
size_t outleft = sizeof(tmp);
size_t rc = iconv(cd, &in, &inleft, &outp, &outleft);
if (tb_append(&out, tmp, sizeof(tmp) - outleft) != 0)
goto fail;
if (rc != (size_t)-1)
continue;
if (errno == E2BIG)
continue;
if (errno == EILSEQ || errno == EINVAL) {
size_t skip = utf8_seq_len((const unsigned char *)in,
inleft);
if (tb_append(&out, "?", 1) != 0)
goto fail;
if (skip == 0)
skip = 1;
in += skip;
inleft -= skip;
(void)iconv(cd, NULL, NULL, NULL, NULL);
continue;
}
goto fail;
}
for (;;) {
char *outp = tmp;
size_t outleft = sizeof(tmp);
size_t rc = iconv(cd, NULL, NULL, &outp, &outleft);
if (tb_append(&out, tmp, sizeof(tmp) - outleft) != 0)
goto fail;
if (rc != (size_t)-1)
break;
if (errno != E2BIG)
break;
}
iconv_close(cd);
if (out.p == NULL)
return strdup("");
return out.p;
fail:
free(out.p);
iconv_close(cd);
return NULL;
}
char *
dc_text_to_display(const char *s, const char *charset)
{
char *out;
if (s == NULL)
return NULL;
if (charset == NULL || strcasecmp(charset, "UTF-8") == 0 ||
strcasecmp(charset, "UTF8") == 0) {
if (dc_utf8_valid(s, strlen(s)))
return strdup(s);
return ascii_fallback(s);
}
out = iconv_to(s, charset);
if (out != NULL)
return out;
out = iconv_to(s, "ASCII//TRANSLIT");
if (out != NULL)
return out;
return ascii_fallback(s);
}