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 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 204 of file frame_validation.c.

204 {
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}
#define SET_ERRNO(code,...)
#define NET_TO_HOST_U32(val)
Definition endian.h:85
unsigned int uint32_t
unsigned char uint8_t
@ ASCIICHAT_OK
Definition error_codes.h:50
@ 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 browser_client_validate_frame(), and 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 226 of file frame_validation.c.

226 {
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 HOST_TO_NET_U32(val)
Definition endian.h:70
@ 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 200 of file frame_validation.c.

200 {
201 return session_frame_validate_contract(text, length, width, height, NULL, 0);
202}
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
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 189 of file frame_validation.c.

189 {
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}
#define HAS_ERRNO(context)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
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

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

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