-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_escape.c
375 lines (316 loc) · 9.02 KB
/
c_escape.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
c_escape.c by EliteAsian123
MIT License
https://github.com/EliteAsian123/c_escape
*/
#include "c_escape.h"
#define ONLY_C \
if (type != CE_C) { \
goto _default; \
}
static const char* HEX = "0123456789ABCDEF";
static inline bool isNorm(char c) {
return c >= ' ' && c <= '~';
}
static inline bool isHex(char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
static int fromHex(const char* str) {
// If the characters aren't hex, fail
if (!isHex(str[1]) || !isHex(str[2]) || !isHex(str[3]) || !isHex(str[4])) {
return -1;
}
// Convert the 4 characters into an int
char hex[5];
hex[0] = str[1];
hex[1] = str[2];
hex[2] = str[3];
hex[3] = str[4];
hex[4] = '\0';
return strtol(hex, NULL, 16);
}
char* escape(const char* str, int type) {
// Get the length of the output string
size_t len = strlen(str);
size_t escapedLen = len;
for (size_t i = 0; i < len; i++) {
switch (str[i]) {
case '\a':
case '\e':
case '\v':
if (type != CE_C) {
goto size_default;
}
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
case '\'':
case '\"':
case '\\':
escapedLen++;
break;
size_default:
default:
if (!isNorm(str[i])) {
// Use unsigned char for bit manipulation
unsigned char val = (unsigned char) str[i];
// Get the length of the unicode escape
if (val <= 0x7F) {
// BYTES : 1
// ESCAPE: \xXX (4)
// 4 - 1 = 3
// OR
// BYTES : 1
// ESCAPE: \uXXXX (6)
// 6 - 1 = 5
if (type == CE_C) {
escapedLen += 3;
} else {
escapedLen += 5;
}
} else if ((val & 0b11100000) == 0b11000000) {
// BYTES : 2
// ESCAPE: \uXXXX (6)
// 6 - 2 = 4
escapedLen += 4;
} else if ((val & 0b11110000) == 0b11100000) {
// BYTES : 3
// ESCAPE: \uXXXX (6)
// 6 - 3 = 3
escapedLen += 3;
} else if ((val & 0b11111000) == 0b11110000) {
// BYTES : 4
// ESCAPE: \uXXXX\uXXXX (12)
// 12 - 4 = 8
escapedLen += 8;
}
// Else, we are a continuation byte, so we don't need to escape it.
}
break;
}
}
// Create output string
char* escaped = malloc(escapedLen + 1);
#define ESC(c) \
escaped[j++] = '\\'; \
escaped[j++] = c;
// Fill output string
size_t j = 0;
for (size_t i = 0; i < len; i++) {
// clang-format off
switch (str[i]) {
case '\a': ONLY_C; ESC('a'); break;
case '\e': ONLY_C; ESC('e'); break;
case '\v': ONLY_C; ESC('v'); break;
case '\b': ESC('b'); break;
case '\f': ESC('f'); break;
case '\n': ESC('n'); break;
case '\r': ESC('r'); break;
case '\t': ESC('t'); break;
case '\'':
case '\"':
case '\\': ESC(str[i]); break;
default:
_default:
if (!isNorm(str[i])) {
// Use unsigned int for bit manipulation
unsigned int val = (unsigned int) str[i];
if (val <= 0x7F) {
escaped[j++] = '\\';
if (type == CE_C) {
escaped[j++] = 'x';
escaped[j++] = HEX[(val >> 4) & 0xF];
escaped[j++] = HEX[val & 0xF];
} else {
escaped[j++] = 'u';
escaped[j++] = '0';
escaped[j++] = '0';
escaped[j++] = HEX[(val >> 4) & 0xF];
escaped[j++] = HEX[val & 0xF];
}
} else if ((val & 0b11100000) == 0b11000000) {
// Add value of next UTF-8 continuation byte to val
val = (val << 6)
| (str[i + 1] & 0b00111111);
escaped[j++] = '\\';
escaped[j++] = 'u';
escaped[j++] = '0'; // Will always be 0
escaped[j++] = HEX[(val >> 8 ) & 0xF];
escaped[j++] = HEX[(val >> 4 ) & 0xF];
escaped[j++] = HEX[val & 0xF];
} else if ((val & 0b11110000) == 0b11100000) {
// Add values of next two UTF-8 continuation bytes to val
val = (val << 12)
| ((str[i + 1] & 0b00111111) << 6)
| (str[i + 2] & 0b00111111);
escaped[j++] = '\\';
escaped[j++] = 'u';
escaped[j++] = HEX[(val >> 12) & 0xF];
escaped[j++] = HEX[(val >> 8 ) & 0xF];
escaped[j++] = HEX[(val >> 4 ) & 0xF];
escaped[j++] = HEX[(val ) & 0xF];
} else if ((val & 0b11111000) == 0b11110000) {
// Add values of next three UTF-8 continuation bytes to val
val = (val << 18)
| ((str[i + 1] & 0b00111111) << 12)
| ((str[i + 2] & 0b00111111) << 6)
| (str[i + 3] & 0b00111111);
// Split into high and low surrogates
val -= 0x10000;
unsigned int high = (val >> 10 & 0x3FF) + 0xD800;
unsigned int low = (val & 0x3FF) + 0xDC00;
// Add high surrogate
escaped[j++] = '\\';
escaped[j++] = 'u';
escaped[j++] = HEX[(high >> 12) & 0xF];
escaped[j++] = HEX[(high >> 8 ) & 0xF];
escaped[j++] = HEX[(high >> 4 ) & 0xF];
escaped[j++] = HEX[(high ) & 0xF];
// Add low surrogate
escaped[j++] = '\\';
escaped[j++] = 'u';
escaped[j++] = HEX[(low >> 12) & 0xF];
escaped[j++] = HEX[(low >> 8 ) & 0xF];
escaped[j++] = HEX[(low >> 4 ) & 0xF];
escaped[j++] = HEX[(low ) & 0xF];
}
} else {
escaped[j++] = str[i];
}
break;
}
// clang-format on
}
#undef ESC
// Add null terminator and return
escaped[j] = '\0';
return escaped;
}
char* unescape(const char* str, int type, int* error_pos) {
// Create output string
// We know that it will ALWAYS be shorter than the original
int len = strlen(str);
char* unescaped = malloc(len + 1);
// Fill output string
bool inEscape = false;
size_t j = 0;
for (size_t i = 0; i < len; i++) {
if (!inEscape) {
if (str[i] == '\\') {
inEscape = true;
} else {
unescaped[j++] = str[i];
}
} else {
// clang-format off
switch (str[i]) {
case 'a': ONLY_C; unescaped[j++] = '\a'; break;
case 'e': ONLY_C; unescaped[j++] = '\e'; break;
case 'v': ONLY_C; unescaped[j++] = '\v'; break;
case 'b': unescaped[j++] = '\b'; break;
case 'f': unescaped[j++] = '\f'; break;
case 'n': unescaped[j++] = '\n'; break;
case 'r': unescaped[j++] = '\r'; break;
case 't': unescaped[j++] = '\t'; break;
case '\'':
case '\"':
case '\\': unescaped[j++] = str[i]; break;
case 'x': {
ONLY_C;
// If there isn't 2 characters after the 'x', fail
if (i + 2 >= len) {
goto _default;
}
// If the characters after the 'x' aren't hex, fail
if (!isHex(str[i + 1]) || !isHex(str[i + 2])) {
goto _default;
}
// Convert the 2 characters into an int
char hex[3];
hex[0] = str[i + 1];
hex[1] = str[i + 2];
hex[2] = '\0';
int value = strtol(hex, NULL, 16);
// Add the value to the output string
unescaped[j++] = value;
// Skip the two characters
i += 2;
break;
}
case 'u': {
// If there isn't 4 characters after the 'u', fail
if (i + 4 >= len) {
goto _default;
}
// Convert the 4 characters into an int
int value = fromHex(str + i);
if (value == -1) {
goto _default;
}
// Check for high surrogate (for > U+FFFF)
if (value >= 0xD800 && value <= 0xDBFF) {
// Skip over the first escape code
i += 5;
// If there isn't 6 characters more characters (\uXXXX), fail
if (i + 5 >= len) {
goto _default;
}
// If there isn't an escape sequence, fail
if (str[i] != '\\' || str[i + 1] != 'u') {
goto _default;
}
// Skip over to the 'u'
i++;
// Convert the 4 characters into an int
int value2 = fromHex(str + i);
if (value2 == -1) {
goto _default;
}
// Convert high (value) and low (value2) surrogate to code point
value -= 0xD800;
value2 -= 0xDC00;
value = (value << 10) + value2 + 0x10000;
}
// How many bytes?
if (value <= 0x007F) {
// 1 byte UTF-8
unescaped[j++] = value;
} else if (value <= 0x07FF) {
// 2 byte UTF-8
unescaped[j++] = 0xC0 | ((value >> 6 ) & 0x1F);
unescaped[j++] = 0x80 | ((value ) & 0x3F);
} else if (value <= 0xFFFF) {
// 3 byte UTF-8
unescaped[j++] = 0xE0 | ((value >> 12) & 0x0F);
unescaped[j++] = 0x80 | ((value >> 6 ) & 0x3F);
unescaped[j++] = 0x80 | ((value ) & 0x3F);
} else {
// 4 byte UTF-8
unescaped[j++] = 0xF0 | ((value >> 18) & 0x07);
unescaped[j++] = 0x80 | ((value >> 12) & 0x3F);
unescaped[j++] = 0x80 | ((value >> 6 ) & 0x3F);
unescaped[j++] = 0x80 | ((value ) & 0x3F);
}
// Skip the 4 characters
i += 4;
break;
}
default:
_default:
if (error_pos != NULL) {
*error_pos = i + 1;
}
return NULL;
}
// clang-format on
inEscape = false;
}
}
// Resize to fit, add null terminator, and return
unescaped = realloc(unescaped, j + 1);
unescaped[j] = '\0';
return unescaped;
}
#undef ONLY_C