ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
frame_validation.c
Go to the documentation of this file.
1
2/* Existing buffer budgets in bytes, including NUL for text storage. */
3#define FRAME_SGR_PARAMETER_CAPACITY 16
4
6#include <ascii-chat-deps/utf8proc/utf8proc.h>
7#include <stdbool.h>
8#include <ascii-chat/otel.h>
9#include <string.h>
12
13/* Current network renderer emits SGR, REP and newline-separated complete rows.
14 * A leading cursor home is accepted for older renderers; arbitrary cursor
15 * movement, OSC/DCS/APC, queries and private modes are not frame content. */
16static bool valid_sgr(const uint32_t *p, size_t n, bool *neutral) {
17 for (size_t i = 0; i < n; ++i) {
18 uint32_t v = p[i];
19 if (v == 38 || v == 48) {
20 *neutral = false;
21 if (++i >= n)
22 return false;
23 unsigned count = p[i] == 2 ? 3 : p[i] == 5 ? 1 : 0;
24 if (!count || count > n - i - 1)
25 return false;
26 while (count--)
27 if (p[++i] > 255)
28 return false;
29 } else if (!(v <= 9 || (v >= 22 && v <= 29) || (v >= 30 && v <= 37) || v == 39 || (v >= 40 && v <= 47) || v == 49 || (v >= 90 && v <= 97) || (v >= 100 && v <= 107))) {
30 return false;
31 } else if (v == 0) {
32 *neutral = true;
33 } else if (!((v >= 22 && v <= 29) || v == 39 || v == 49)) {
34 *neutral = false;
35 }
36 }
37 return true;
38}
39
40static asciichat_error_t validate_contract(const char *text, size_t length, uint32_t width, uint32_t height, const acip_render_settings_t *settings, uint32_t effects) {
41 if (!text || !length || !width || !height || width > 16384 || height > 16384 || (uint64_t)width * height > FRAME_RENDER_MAX_BYTES || length > FRAME_RENDER_MAX_BYTES)
42 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid rendered frame bounds");
43 uint32_t palette[256];
44 size_t palette_count = 0;
45 bool ascii_allowed[128] /* One entry for each 7-bit ASCII code. */ = {0};
46 unsigned rain_groups = 0;
47 if (settings) {
48 size_t n = strnlen(settings->palette, sizeof(settings->palette));
49 if (!n || n == sizeof(settings->palette))
50 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid frame contract palette length");
51 for (size_t i = 0; i < n;) {
52 utf8proc_int32_t cp;
53 utf8proc_ssize_t bytes = utf8proc_iterate((const uint8_t *)settings->palette + i, (utf8proc_ssize_t)(n - i), &cp);
54 if (bytes <= 0 || cp < 32 || (cp >= 127 && cp <= 159) || utf8proc_charwidth(cp) < 0)
55 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid frame contract palette UTF-8/control");
56 if (cp < 128) {
57 ascii_allowed[cp] = true;
58 int group = digital_rain_glyph_group((uint32_t)cp);
59 if (group >= 0)
60 rain_groups |= 1u << (unsigned)group;
61 } else {
62 size_t at = palette_count++;
63 while (at && palette[at - 1] > (uint32_t)cp) {
64 palette[at] = palette[at - 1];
65 --at;
66 }
67 palette[at] = (uint32_t)cp;
68 }
69 i += (size_t)bytes;
70 }
71 }
72 uint32_t row = 0, col = 0, previous_width = 0, marks = 0;
73 bool ended_row = false, can_repeat = false, neutral_sgr = true;
74 for (size_t i = 0; i < length;) {
75 unsigned char c = (unsigned char)text[i];
76 if (c == 0x1b) {
77 size_t start = i;
78 if (length - i < 3 || text[++i] != '[')
79 return SET_ERRNO(ERROR_INVALID_FRAME, "Unsupported frame escape at byte %zu", start);
80 ++i;
82 size_t count = 1;
83 while (i < length && ((text[i] >= '0' && text[i] <= '9') || text[i] == ';')) {
84 if (i - start >= 64)
85 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame escape exceeds byte bound");
86 if (text[i] == ';') {
87 if (++count > 16)
88 return SET_ERRNO(ERROR_INVALID_FRAME, "Too many frame escape parameters");
89 } else {
90 uint32_t digit = (uint32_t)(text[i] - '0');
91 if (params[count - 1] > (16384 - digit) / 10)
92 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame escape parameter exceeds bound");
93 params[count - 1] = params[count - 1] * 10 + digit;
94 }
95 ++i;
96 }
97 if (i == length)
98 return SET_ERRNO(ERROR_INVALID_FRAME, "Truncated frame escape at byte %zu", start);
99 if (i - start >= 64)
100 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame escape exceeds byte bound");
101 char command = text[i++];
102 if (command == 'm' && valid_sgr(params, count, &neutral_sgr))
103 continue;
104 if (command == 'H' && start == 0 && count <= 2 && params[0] <= 1 && params[count - 1] <= 1)
105 continue;
106 if (command == 'b' && count == 1 && previous_width && can_repeat && !ended_row) {
107 uint32_t repeats = params[0] ? params[0] : 1;
108 if (repeats > (width - col) / previous_width)
109 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame REP exceeds row width at row %u", row + 1);
110 col += repeats * previous_width;
111 continue;
112 }
113 return SET_ERRNO(ERROR_INVALID_FRAME, "Unsupported frame CSI at byte %zu", start);
114 }
115 if (c == '\r' || c == '\n') {
116 if (c == '\r' && (i + 1 == length || text[++i] != '\n'))
117 return SET_ERRNO(ERROR_INVALID_FRAME, "Bare carriage return in frame");
118 if (ended_row || col != width || row >= height)
119 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame row %u has %u cells, expected %u", row + 1, col, width);
120 ++row;
121 col = previous_width = marks = 0;
122 can_repeat = false;
123 ended_row = row == height;
124 ++i;
125 continue;
126 }
127 if (c < 0x20 || c == 0x7f || ended_row || row >= height)
128 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid frame control or row count at byte %zu", i);
129 utf8proc_int32_t cp = c;
130 utf8proc_ssize_t bytes = 1;
131 if (c >= 0x80)
132 bytes = utf8proc_iterate((const utf8proc_uint8_t *)text + i, (utf8proc_ssize_t)(length - i), &cp);
133 if (bytes <= 0 || (cp >= 0x80 && cp <= 0x9f))
134 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid frame UTF-8/control at byte %zu", i);
135 if (settings && cp != ' ') {
136 bool allowed = false;
137 if (settings->caps.render_mode == RENDER_MODE_HALF_BLOCK)
138 allowed = cp == 0x2580 || cp == 0x2584 || cp == 0x2588 || (cp >= 0x2591 && cp <= 0x2593);
139 else {
140 if (cp < 128)
141 allowed = ascii_allowed[cp];
142 else {
143 size_t lo = 0, hi = palette_count;
144 while (lo < hi) {
145 size_t mid = lo + (hi - lo) / 2;
146 if (palette[mid] < (uint32_t)cp)
147 lo = mid + 1;
148 else
149 hi = mid;
150 }
151 allowed = lo < palette_count && palette[lo] == (uint32_t)cp;
152 }
153 if (!allowed && (effects & FRAME_RENDER_CONTRACT_RAIN)) {
154 int group = digital_rain_glyph_group((uint32_t)cp);
155 allowed = group >= 0 && (rain_groups & (1u << (unsigned)group));
156 }
157 }
158 if ((effects & FRAME_RENDER_CONTRACT_GRID) && (cp == '_' || cp == '+' || cp == '|'))
159 allowed = true;
160 if (!allowed)
161 return SET_ERRNO(ERROR_INVALID_FRAME, "Off-palette frame glyph U+%04x at byte %zu", (unsigned)cp, i);
162 }
163 int cells = cp < 128 ? 1 : utf8proc_charwidth(cp);
164 if (cells < 0 || (cells == 0 && (!previous_width || ++marks > 32)))
165 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid frame combining sequence at byte %zu", i);
166 if ((uint32_t)cells > width - col)
167 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame row %u exceeds %u cells", row + 1, width);
168 col += (uint32_t)cells;
169 if (cells) {
170 previous_width = (uint32_t)cells;
171 marks = 0;
172 can_repeat = true;
173 } else {
174 // REP repeats the final codepoint, not a combining cluster. Do not accept
175 // a REP after marks, where terminal implementations disagree.
176 can_repeat = false;
177 }
178 i += (size_t)bytes;
179 }
180 if (!neutral_sgr)
181 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame leaves terminal attributes active");
182 if (!ended_row && col == width)
183 ++row;
184 if (row != height || (!ended_row && col != width))
185 return SET_ERRNO(ERROR_INVALID_FRAME, "Frame geometry does not match %ux%u (%u rows, %u final cells)", width, height, row, col);
186 return ASCIICHAT_OK;
187}
188
190 asciichat_error_t result = validate_contract(text, length, width, height, settings, effects);
191 ascii_otel_metric(ASCII_OTEL_METRIC_FRAME_VALIDATION_CHECKED, 1);
192 if (result != ASCIICHAT_OK) {
193 asciichat_error_context_t error = {0};
194 const char *reason = HAS_ERRNO(&error) ? error.context_message : asciichat_error_string(result);
195 ascii_otel_frame_rejected("rendered_frame", reason, length, width, height, result);
196 }
197 return result;
198}
199
201 return session_frame_validate_contract(text, length, width, height, NULL, 0);
202}
203
204asciichat_error_t frame_render_contract_decode(const void *wire, size_t available, acip_render_settings_t *settings, uint32_t *effects, size_t *length) {
205 if (!wire || !settings || !effects || !length || available < FRAME_RENDER_CONTRACT_HEADER)
206 return SET_ERRNO(ERROR_INVALID_FRAME, "Truncated rendered frame contract");
207 uint32_t fields[5];
208 memcpy(fields, wire, sizeof(fields));
209 for (size_t i = 0; i < 5; ++i)
210 fields[i] = NET_TO_HOST_U32(fields[i]);
211 if (fields[0] != FRAME_RENDER_CONTRACT_SCHEMA || fields[2] > RENDER_MODE_HALF_BLOCK || (fields[3] & ~(FRAME_RENDER_CONTRACT_RAIN | FRAME_RENDER_CONTRACT_GRID)) || !fields[4] || fields[4] > 255 ||
212 fields[4] > available - FRAME_RENDER_CONTRACT_HEADER)
213 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid rendered frame contract schema/mode/effects/palette length");
214 memset(settings, 0, sizeof(*settings));
215 settings->revision = fields[1];
216 settings->caps.render_mode = (terminal_render_mode_t)fields[2];
217 settings->palette_length = fields[4];
218 memcpy(settings->palette, (const uint8_t *)wire + FRAME_RENDER_CONTRACT_HEADER, fields[4]);
219 if (memchr(settings->palette, 0, fields[4]))
220 return SET_ERRNO(ERROR_INVALID_FRAME, "Embedded NUL in rendered frame contract palette");
221 *effects = fields[3];
222 *length = FRAME_RENDER_CONTRACT_HEADER + fields[4];
223 return ASCIICHAT_OK;
224}
225
227 if (!settings || !wire || !length)
228 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing rendered frame contract");
229 size_t n = strnlen(settings->palette, sizeof(settings->palette));
230 if (!n || n > 255)
231 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rendered frame contract palette");
232 uint32_t fields[] = {HOST_TO_NET_U32(FRAME_RENDER_CONTRACT_SCHEMA), HOST_TO_NET_U32(settings->revision), HOST_TO_NET_U32(settings->caps.render_mode), HOST_TO_NET_U32(effects),
234 memcpy(wire, fields, sizeof(fields));
235 memcpy(wire + sizeof(fields), settings->palette, n);
236 *length = sizeof(fields) + n;
237 return ASCIICHAT_OK;
238}
#define SET_ERRNO(code,...)
#define HAS_ERRNO(context)
Matrix-style digital rain effect for ASCII frames.
🔄 Network byte order conversion helpers
#define HOST_TO_NET_U32(val)
Definition endian.h:70
#define NET_TO_HOST_U32(val)
Definition endian.h:85
char cells[(512 *256 *2)]
Definition fft.c:22
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
asciichat_error_t frame_render_contract_decode(const void *wire, size_t available, acip_render_settings_t *settings, uint32_t *effects, size_t *length)
asciichat_error_t session_frame_validate(const char *text, size_t length, uint32_t width, uint32_t height)
asciichat_error_t frame_render_contract_encode(const acip_render_settings_t *settings, uint32_t effects, uint8_t *wire, size_t *length)
#define FRAME_SGR_PARAMETER_CAPACITY
asciichat_error_t session_frame_validate_contract(const char *text, size_t length, uint32_t width, uint32_t height, const acip_render_settings_t *settings, uint32_t effects)
unsigned int uint32_t
unsigned long long uint64_t
unsigned char uint8_t
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_FRAME
@ ERROR_INVALID_PARAM
int digital_rain_glyph_group(uint32_t codepoint)
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:175
void ascii_otel_frame_rejected(const char *validator, const char *reason, size_t bytes, uint32_t width, uint32_t height, asciichat_error_t code)
Definition otel.c:893
#define FRAME_RENDER_CONTRACT_RAIN
#define FRAME_RENDER_CONTRACT_HEADER
#define FRAME_RENDER_MAX_BYTES
#define FRAME_RENDER_CONTRACT_GRID
#define FRAME_RENDER_CONTRACT_SCHEMA
🔤 String Manipulation and Shell Escaping Utilities
acip render settings keeps the values that move through the subsystem: caps, width,...