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_validator.c File Reference

Go to the source code of this file.

Functions

bool frame_validate_integrity (const char *frame_data, size_t frame_size)
 Validate ASCII frame integrity.
size_t frame_get_valid_end (const char *frame_data, size_t frame_size)
 Get the actual valid frame end position.

Function Documentation

◆ frame_get_valid_end()

size_t frame_get_valid_end ( const char * frame_data,
size_t frame_size )

Get the actual valid frame end position.

Finds where the frame SHOULD end (right after ESC[0m reset sequence). Returns the position after the final reset, or frame_size if no reset found.

Parameters
frame_dataFrame buffer
frame_sizeBuffer size
Returns
Position where frame should end

Definition at line 70 of file video/ascii/frame_validator.c.

70 {
71 if (!frame_data || frame_size < 4) {
72 return frame_size;
73 }
74
75 size_t reset_pos = frame_find_final_reset(frame_data, frame_size);
76 if (reset_pos == SIZE_MAX) {
77 return frame_size; // No reset found, assume all data is valid
78 }
79
80 return reset_pos + 4; // Return position after the reset sequence
81}

◆ frame_validate_integrity()

bool frame_validate_integrity ( const char * frame_data,
size_t frame_size )

Validate ASCII frame integrity.

Checks for common corruption patterns:

  • Frame ends with proper reset sequence ESC[0m
  • No garbage bytes after reset sequence
  • Frame is properly null-terminated
Parameters
frame_dataFrame buffer (may not be null-terminated)
frame_sizeActual frame size in bytes
Returns
true if frame appears valid, false if corruption detected

Definition at line 34 of file video/ascii/frame_validator.c.

34 {
35 if (!frame_data || frame_size == 0) {
36 return false;
37 }
38
39 size_t reset_pos = frame_find_final_reset(frame_data, frame_size);
40 if (reset_pos == SIZE_MAX) {
41 // No reset sequence found - this is suspicious
42 log_warn("FRAME_VALIDATION: No ESC[0m reset sequence found in %zu byte frame", frame_size);
43 return false;
44 }
45
46 size_t expected_end = reset_pos + 4; // 4 bytes for ESC[0m
47
48 // Check if there's garbage after the reset
49 if (expected_end < frame_size) {
50 // Calculate how much garbage we have
51 size_t garbage_size = frame_size - expected_end;
52
53 // Check what the garbage looks like
54 const uint8_t *garbage = (const uint8_t *)&frame_data[expected_end];
55
56 // Log the garbage for debugging
57 log_warn("FRAME_VALIDATION: Found %zu garbage bytes after reset at offset %zu (frame size %zu): "
58 "[%02x %02x %02x %02x ...]",
59 garbage_size, expected_end, frame_size, garbage[0], garbage_size > 1 ? garbage[1] : 0,
60 garbage_size > 2 ? garbage[2] : 0, garbage_size > 3 ? garbage[3] : 0);
61
62 return false;
63 }
64
65 // Frame looks valid
66 log_debug("FRAME_VALIDATION: Frame OK (%zu bytes, ends at offset %zu)", frame_size, expected_end);
67 return true;
68}
unsigned char uint8_t
Definition common.h:60
#define log_warn(...)
Log a WARN message.
Definition log/log.h:621
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:595

References log_debug, and log_warn.