ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
frame_validation.c File Reference

Go to the source code of this file.

Macros

#define FRAME_SGR_PARAMETER_CAPACITY   16

Functions

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)
asciichat_error_t session_frame_validate (const char *text, size_t length, uint32_t width, uint32_t height)
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 frame_render_contract_encode (const acip_render_settings_t *settings, uint32_t effects, uint8_t *wire, size_t *length)

Macro Definition Documentation

◆ FRAME_SGR_PARAMETER_CAPACITY

#define FRAME_SGR_PARAMETER_CAPACITY   16

Definition at line 3 of file frame_validation.c.

Function Documentation

◆ frame_render_contract_decode()

asciichat_error_t frame_render_contract_decode ( const void * wire,
size_t available,
acip_render_settings_t * settings,
uint32_t * effects,
size_t * length )

Definition at line 209 of file frame_validation.c.

210 {
211 if (!wire || !settings || !effects || !length || available < FRAME_RENDER_CONTRACT_HEADER)
212 return SET_ERRNO(ERROR_INVALID_FRAME, "Truncated rendered frame contract");
213 uint32_t fields[5];
214 memcpy(fields, wire, sizeof(fields));
215 for (size_t i = 0; i < 5; ++i)
216 fields[i] = NET_TO_HOST_U32(fields[i]);
217 if (fields[0] != FRAME_RENDER_CONTRACT_SCHEMA || fields[2] > RENDER_MODE_HALF_BLOCK ||
218 (fields[3] & ~(FRAME_RENDER_CONTRACT_RAIN | FRAME_RENDER_CONTRACT_GRID)) || !fields[4] || fields[4] > 255 ||
219 fields[4] > available - FRAME_RENDER_CONTRACT_HEADER)
220 return SET_ERRNO(ERROR_INVALID_FRAME, "Invalid rendered frame contract schema/mode/effects/palette length");
221 memset(settings, 0, sizeof(*settings));
222 settings->revision = fields[1];
223 settings->caps.render_mode = (terminal_render_mode_t)fields[2];
224 settings->palette_length = fields[4];
225 memcpy(settings->palette, (const uint8_t *)wire + FRAME_RENDER_CONTRACT_HEADER, fields[4]);
226 if (memchr(settings->palette, 0, fields[4]))
227 return SET_ERRNO(ERROR_INVALID_FRAME, "Embedded NUL in rendered frame contract palette");
228 *effects = fields[3];
229 *length = FRAME_RENDER_CONTRACT_HEADER + fields[4];
230 return ASCIICHAT_OK;
231}
#define SET_ERRNO(code,...)
#define NET_TO_HOST_U32(val)
Definition endian.h:81
unsigned int uint32_t
Definition common.h:62
unsigned char uint8_t
Definition common.h:60
@ ASCIICHAT_OK
Definition error_codes.h:46
@ ERROR_INVALID_FRAME
#define FRAME_RENDER_CONTRACT_RAIN
#define FRAME_RENDER_CONTRACT_HEADER
#define FRAME_RENDER_CONTRACT_GRID
#define FRAME_RENDER_CONTRACT_SCHEMA

References ASCIICHAT_OK, acip_render_settings_t::caps, ERROR_INVALID_FRAME, FRAME_RENDER_CONTRACT_GRID, FRAME_RENDER_CONTRACT_HEADER, FRAME_RENDER_CONTRACT_RAIN, FRAME_RENDER_CONTRACT_SCHEMA, NET_TO_HOST_U32, acip_render_settings_t::palette, acip_render_settings_t::palette_length, acip_render_settings_t::revision, and SET_ERRNO.

Referenced by client_validate_frame().

◆ frame_render_contract_encode()

asciichat_error_t frame_render_contract_encode ( const acip_render_settings_t * settings,
uint32_t effects,
uint8_t * wire,
size_t * length )

Definition at line 233 of file frame_validation.c.

234 {
235 if (!settings || !wire || !length)
236 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing rendered frame contract");
237 size_t n = strnlen(settings->palette, sizeof(settings->palette));
238 if (!n || n > 255)
239 return SET_ERRNO(ERROR_INVALID_PARAM, "Invalid rendered frame contract palette");
241 HOST_TO_NET_U32(settings->caps.render_mode), HOST_TO_NET_U32(effects), HOST_TO_NET_U32(n)};
242 memcpy(wire, fields, sizeof(fields));
243 memcpy(wire + sizeof(fields), settings->palette, n);
244 *length = sizeof(fields) + n;
245 return ASCIICHAT_OK;
246}
#define HOST_TO_NET_U32(val)
Definition endian.h:66
@ ERROR_INVALID_PARAM

References ASCIICHAT_OK, acip_render_settings_t::caps, ERROR_INVALID_PARAM, FRAME_RENDER_CONTRACT_SCHEMA, HOST_TO_NET_U32, acip_render_settings_t::palette, acip_render_settings_t::revision, and SET_ERRNO.

Referenced by acip_send_ascii_frame_contract().

◆ session_frame_validate()

asciichat_error_t session_frame_validate ( const char * text,
size_t length,
uint32_t width,
uint32_t height )

Definition at line 205 of file frame_validation.c.

205 {
206 return session_frame_validate_contract(text, length, width, height, NULL, 0);
207}
unsigned int height
Definition fft.c:23
unsigned int width
Definition fft.c:23
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)

References height, session_frame_validate_contract(), and width.

Referenced by mirror_render_audio_visualization().

◆ session_frame_validate_contract()

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 )

Definition at line 193 of file frame_validation.c.

194 {
195 asciichat_error_t result = validate_contract(text, length, width, height, settings, effects);
196 ascii_otel_metric(ASCII_OTEL_METRIC_FRAME_VALIDATION_CHECKED, 1);
197 if (result != ASCIICHAT_OK) {
198 asciichat_error_context_t error = {0};
199 const char *reason = HAS_ERRNO(&error) ? error.context_message : asciichat_error_string(result);
200 ascii_otel_frame_rejected("rendered_frame", reason, length, width, height, result);
201 }
202 return result;
203}
#define HAS_ERRNO(context)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
void ascii_otel_metric(ascii_otel_metric_t metric, double value)
Definition otel.c:161
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:746

References ascii_otel_frame_rejected(), ascii_otel_metric(), ASCIICHAT_OK, asciichat_error_context_t::context_message, HAS_ERRNO, height, and width.

Referenced by client_validate_frame(), mirror_convert_frame(), and session_frame_validate().