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

Go to the source code of this file.

Functions

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

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 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[ZALGO_MAX_MARKS] )

Definition at line 19 of file zalgo.c.

19 {
20 if (level < 1 || level > 10 || !marks)
21 return 0;
22 static const unsigned coverage[] = {3, 6, 10, 18, 28, 40, 55, 70, 85, 100};
23 static const unsigned minimum[] = {1, 1, 1, 1, 1, 1, 2, 3, 4, 6};
24 static const uint16_t short_marks[] = {0x0334, 0x0335, 0x0336, 0x0337, 0x0338};
25 static const uint16_t stack_marks[] = {0x0300, 0x0301, 0x0302, 0x0303, 0x0308, 0x030a, 0x0310, 0x0311, 0x0324, 0x0325, 0x0329, 0x0330};
26 unsigned index = (unsigned)level - 1;
27 uint32_t hash = zalgo_hash((uint32_t)((uint64_t)row * UINT64_C(0x9e3779b9)) ^ col ^ UINT32_C(0x5a17));
28 if (hash % 100 >= coverage[index])
29 return 0;
30 unsigned count = minimum[index] + ((hash >> 8) % 256) * (zalgo_maximum[index] - minimum[index] + 1) / 256;
31 for (unsigned mark = 0; mark < count; mark++) {
32 uint32_t choice = zalgo_hash((uint32_t)((uint64_t)hash + mark));
33 marks[mark] = mark == 0 ? short_marks[choice % 5] : stack_marks[choice % 12];
34 }
35 return count;
36}
unsigned long long uint64_t
static bool size_t index
Definition queries.h:10

References index, and ZALGO_MAX_MARKS.

Referenced by zalgo_apply().