ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
zalgo.h File Reference

Go to the source code of this file.

Macros

#define ZALGO_MAX_MARKS   10

Functions

asciichat_error_t zalgo_apply (const char *input, size_t length, int level, size_t max_output_bytes, char **output)
unsigned zalgo_cell_marks (uint32_t row, uint32_t col, int level, uint16_t marks[10])

Macro Definition Documentation

◆ ZALGO_MAX_MARKS

#define ZALGO_MAX_MARKS   10

Definition at line 4 of file zalgo.h.

Referenced by zalgo_apply(), zalgo_cell_marks(), and zalgo_cell_marks().

Function Documentation

◆ zalgo_apply()

asciichat_error_t zalgo_apply ( const char * input,
size_t length,
int level,
size_t max_output_bytes,
char ** output )

Decorate an ANSI frame with deterministic combining marks. Caller frees *output. REP is expanded before decoration. Only printable ASCII bases are decorated; whitespace, existing UTF-8, and escape sequences are preserved. The byte limit excludes the NUL terminator; on overflow returns an error with *output = NULL.

Decorate a frame within the caller's byte budget; errors leave no partially decorated output.

Definition at line 68 of file zalgo.c.

68 {
69 if (!output)
70 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing Zalgo output pointer");
71 *output = NULL;
72 if (!input || level < 1 || level > 10)
73 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo requires a frame and intensity 1-10");
74
75 // Bound REP expansion before allocating: a short remote frame can contain a
76 // very large repeat count. Four bytes per repeated code point is conservative.
77 const size_t expansion_limit = 1024 * 1024;
78 size_t expansion_bound = length;
79 bool has_rep = false;
80 if (length > expansion_limit)
81 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo frame exceeds expansion limit");
82 for (size_t i = 0; i + 1 < length; i++) {
83 if (input[i] != '\x1b' || input[i + 1] != '[')
84 continue;
85 size_t j = i + 2;
86 uint32_t parameter = 0;
87 while (j < length && ((input[j] >= '0' && input[j] <= '9') || input[j] == ';')) {
88 if (input[j] == ';') {
89 parameter = 0;
90 } else {
91 unsigned digit = (unsigned)(input[j] - '0');
92 if (parameter > (UINT32_MAX - digit) / 10)
93 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo frame contains an overflowing ANSI parameter");
94 parameter = parameter * 10 + digit;
95 }
96 j++;
97 }
98 if (j < length && input[j] == 'b') {
99 has_rep = true;
100 if (parameter > (expansion_limit - expansion_bound) / 4)
101 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo REP expansion exceeds frame limit");
102 expansion_bound += (size_t)parameter * 4;
103 }
104 i = j;
105 }
106
107 char *expanded = NULL;
108 defer(SAFE_FREE(expanded));
109 if (has_rep) {
110 expanded = ansi_expand_rle(input, length);
111 } else {
112 expanded = SAFE_MALLOC(length + 1, char *);
113 memcpy(expanded, input, length);
114 expanded[length] = '\0';
115 }
116 if (!expanded)
117 return SET_ERRNO(ERROR_INVALID_PARAM, "Cannot expand empty Zalgo frame");
118 size_t n = strlen(expanded);
119 if (n > max_output_bytes) {
120 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo frame exceeds output limit");
121 }
122 size_t mark_budget = max_output_bytes - n;
123 size_t factor = 1 + 2 * zalgo_maximum[level - 1];
124 // Every source byte introduces at most the selected level's mark limit.
125 if (n > (SIZE_MAX - 1) / factor) {
126 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo frame is too large");
127 }
128 size_t capacity = n * factor < max_output_bytes ? n * factor : max_output_bytes;
129 SAFE_MALLOC_DEFER_FREE(result, capacity + 1, char *);
130 char *dst = result;
131 uint32_t row = 0, col = 0;
132 for (size_t i = 0; i < n;) {
133 unsigned char c = (unsigned char)expanded[i];
134 if (c == 0x1b) {
135 size_t count = zalgo_escape_length(expanded + i, n - i);
136 memcpy(dst, expanded + i, count);
137 dst += count;
138 i += count;
139 continue;
140 }
141 *dst++ = expanded[i++];
142 if (c == '\n') {
143 row++;
144 col = 0;
145 } else if (c == '\r') {
146 col = 0;
147 } else if (c == '\t') {
148 col = (col / 8 + 1) * 8;
149 } else if (c >= 0x20 && c < 0x7f) {
151 unsigned count = c == ' ' ? 0 : zalgo_cell_marks(row, col, level, marks);
152 col++;
153 if (count * 2 > mark_budget) {
154 return SET_ERRNO(ERROR_INVALID_PARAM, "Zalgo decoration exceeds output limit");
155 }
156 mark_budget -= count * 2;
157 for (unsigned mark = 0; mark < count; mark++) {
158 uint16_t cp = marks[mark];
159 *dst++ = (char)(0xc0 | (cp >> 6));
160 *dst++ = (char)(0x80 | (cp & 0x3f));
161 }
162 } else if (c >= 0xc0) {
163 // Preserve multibyte characters; continuation bytes do not advance columns.
164 col++;
165 }
166 }
167 *dst = '\0';
168 *output = result;
169 result = NULL;
170 return ASCIICHAT_OK;
171}
#define SET_ERRNO(code,...)
unsigned short uint16_t
unsigned int uint32_t
#define SAFE_FREE(ptr)
#define SAFE_MALLOC_DEFER_FREE(name, size, cast)
#define SAFE_MALLOC(size, cast)
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
char * ansi_expand_rle(const char *input, size_t input_len)
Expand RLE escape sequences in a string.
Definition rle.c:14
unsigned zalgo_cell_marks(uint32_t row, uint32_t col, int level, uint16_t marks[ZALGO_MAX_MARKS])
Definition zalgo.c:19
#define ZALGO_MAX_MARKS
Definition zalgo.h:4

References ansi_expand_rle(), ASCIICHAT_OK, ERROR_INVALID_PARAM, SAFE_FREE, SAFE_MALLOC, SAFE_MALLOC_DEFER_FREE, SET_ERRNO, zalgo_cell_marks(), and ZALGO_MAX_MARKS.

Referenced by session_display_write_ascii_frame().

◆ zalgo_cell_marks()

unsigned zalgo_cell_marks ( uint32_t row,
uint32_t col,
int level,
uint16_t marks[10] )

Shared terminal/raster distribution. Returns 0-10 marks for one cell.

References ZALGO_MAX_MARKS.