-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutf8.c
More file actions
193 lines (186 loc) · 5.77 KB
/
utf8.c
File metadata and controls
193 lines (186 loc) · 5.77 KB
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
#include "utf8.h"
#include <stdint.h>
const char unicode_box_to_cp437[128] = {
/* 0x2500 ─ */ '\xC4', // 196 single horizontal
/* 0x2501 ━ */ '\xC4', // heavy → single
/* 0x2502 │ */ '\xB3', // 179 single vertical
/* 0x2503 ┃ */ '\xB3', // heavy → single
/* 0x2504 ┄ */ '\xC4', // light dashed horizontal → solid single
/* 0x2505 ┅ */ '\xC4', // heavy dashed horizontal → solid single
/* 0x2506 ┆ */ '\xB3', // light dashed vertical → solid single
/* 0x2507 ┇ */ '\xB3', // heavy dashed vertical → solid single
/* 0x2508 ┈ */ '\xC4', // longer light dashed horiz → solid
/* 0x2509 ┉ */ '\xC4',
/* 0x250A ┊ */ '\xB3', // longer light dashed vert → solid
/* 0x250B ┋ */ '\xB3',
/* 0x250C ┌ */ '\xDA', // 218 single down-right
/* 0x250D ┍ */ '\xDA',
/* 0x250E ┎ */ '\xDA',
/* 0x250F ┏ */ '\xDA',
/* 0x2510 ┐ */ '\xBF', // 191 single down-left
/* 0x2511 ┑ */ '\xBF',
/* 0x2512 ┒ */ '\xBF',
/* 0x2513 ┓ */ '\xBF',
/* 0x2514 └ */ '\xC0', // 192 single up-right
/* 0x2515 ┕ */ '\xC0',
/* 0x2516 ┖ */ '\xC0',
/* 0x2517 ┗ */ '\xC0',
/* 0x2518 ┘ */ '\xD9', // 217 single up-left
/* 0x2519 ┙ */ '\xD9',
/* 0x251A ┚ */ '\xD9',
/* 0x251B ┛ */ '\xD9',
/* 0x251C ├ */ '\xC3', // 195 single right + vertical
/* 0x251D ┝ */ '\xC3',
/* 0x251E ┞ */ '\xC3',
/* 0x251F ┟ */ '\xC3',
/* 0x2520 ┠ */ '\xC3',
/* 0x2521 ┡ */ '\xC3',
/* 0x2522 ┢ */ '\xC3',
/* 0x2523 ┣ */ '\xC3',
/* 0x2524 ┤ */ '\xB4', // 180 single left + vertical
/* 0x2525 ┥ */ '\xB4',
/* 0x2526 ┦ */ '\xB4',
/* 0x2527 ┧ */ '\xB4',
/* 0x2528 ┨ */ '\xB4',
/* 0x2529 ┩ */ '\xB4',
/* 0x252A ┪ */ '\xB4',
/* 0x252B ┫ */ '\xB4',
/* 0x252C ┬ */ '\xC2', // 194 single horizontal + down
/* 0x252D ┭ */ '\xC2',
/* 0x252E ┮ */ '\xC2',
/* 0x252F ┯ */ '\xC2',
/* 0x2530 ┰ */ '\xC2',
/* 0x2531 ┱ */ '\xC2',
/* 0x2532 ┲ */ '\xC2',
/* 0x2533 ┳ */ '\xC2',
/* 0x2534 ┴ */ '\xC1', // 193 single horizontal + up
/* 0x2535 ┵ */ '\xC1',
/* 0x2536 ┶ */ '\xC1',
/* 0x2537 ┷ */ '\xC1',
/* 0x2538 ┸ */ '\xC1',
/* 0x2539 ┹ */ '\xC1',
/* 0x253A ┺ */ '\xC1',
/* 0x253B ┻ */ '\xC1',
/* 0x253C ┼ */ '\xC5', // 197 single cross
/* 0x253D ┽ */ '\xC5',
/* 0x253E ┾ */ '\xC5',
/* 0x253F ┿ */ '\xC5',
/* 0x2540 ╀ */ '\xC5',
/* 0x2541 ╁ */ '\xC5',
/* 0x2542 ╂ */ '\xC5',
/* 0x2543 ╃ */ '\xC5',
/* 0x2544 ╄ */ '\xC5',
/* 0x2545 ╅ */ '\xC5',
/* 0x2546 ╆ */ '\xC5',
/* 0x2547 ╇ */ '\xC5',
/* 0x2548 ╈ */ '\xC5',
/* 0x2549 ╉ */ '\xC5',
/* 0x254A ╊ */ '\xC5',
/* 0x254B ╋ */ '\xC5',
/* 0x254C ╌ */ '\xC4', // dashed double horiz → solid single horiz
/* 0x254D ╍ */ '\xC4',
/* 0x254E ╎ */ '\xB3', // dashed double vert → solid single vert
/* 0x254F ╏ */ '\xB3',
/* 0x2550 ═ */ '\xCD', // 205 double horizontal
/* 0x2551 ║ */ '\xBA', // 186 double vertical
/* 0x2552 ╒ */ '\xD5', // 213
/* 0x2553 ╓ */ '\xB4', // best approximation
/* 0x2554 ╔ */ '\xC9', // 201 double down-right
/* 0x2555 ╕ */ '\xB8', // 184
/* 0x2556 ╖ */ '\xB4',
/* 0x2557 ╗ */ '\xBB', // 187 double down-left
/* 0x2558 ╘ */ '\xBE', // 190
/* 0x2559 ╙ */ '\xC8', // approx
/* 0x255A ╚ */ '\xC8', // 200 double up-right
/* 0x255B ╛ */ '\xBC', // 188
/* 0x255C ╜ */ '\xBC',
/* 0x255D ╝ */ '\xBC', // 188 double up-left
/* 0x255E ╞ */ '\xCC', // 204
/* 0x255F ╟ */ '\xCC',
/* 0x2560 ╠ */ '\xCC', // 204 double right + vertical
/* 0x2561 ╡ */ '\xB4', // approx
/* 0x2562 ╢ */ '\xB6',
/* 0x2563 ╣ */ '\xB9', // 185 double left + vertical
/* 0x2564 ╤ */ '\xD1', // 209
/* 0x2565 ╥ */ '\xD1',
/* 0x2566 ╦ */ '\xCB', // 203 double horiz + down
/* 0x2567 ╧ */ '\xCF', // 207
/* 0x2568 ╨ */ '\xCF',
/* 0x2569 ╩ */ '\xCA', // 202 double horiz + up
/* 0x256A ╪ */ '\xCE', // 206
/* 0x256B ╫ */ '\xCE',
/* 0x256C ╬ */ '\xCE', // 206 double cross
/* 0x256D ╭ */ '\xDA', // rounded → single corner
/* 0x256E ╮ */ '\xBF',
/* 0x256F ╯ */ '\xD9',
/* 0x2570 ╰ */ '\xC0',
/* 0x2571 ╱ */ '/', // diagonal
/* 0x2572 ╲ */ '\\',
/* 0x2573 ╳ */ 'X', // closest common substitute
/* 0x2574 ╴ */ '\xC4', // horizontal stub
/* 0x2575 ╵ */ '\xB3', // vertical stub
/* 0x2576 ╶ */ '\xC4',
/* 0x2577 ╷ */ '\xB3',
/* 0x2578 ╸ */ '\xC4',
/* 0x2579 ╹ */ '\xB3',
/* 0x257A ╺ */ '\xC4',
/* 0x257B ╻ */ '\xB3',
/* 0x257C ╼ */ '\xC4',
/* 0x257D ╽ */ '\xB3',
/* 0x257E ╾ */ '\xC4',
/* 0x257F ╿ */ '\xB3'
};
int utf8_to_cp437(unsigned char *data, const int size, uint32_t *accumulator, int *pending_bytes) {
unsigned char *ascii = data;
int length = 0;
for (int i = 0; i < size; i++) {
const unsigned char c = data[i];
if (*pending_bytes == 0) {
if (c < 0x80) {
ascii[length++] = c;
}
else if ((c & 0xE0) == 0xC0) {
// 2-byte sequence (110xxxxx)
*accumulator = (uint32_t)(c & 0x1F);
*pending_bytes = 1;
}
else if ((c & 0xF0) == 0xE0) {
// 3-byte sequence (1110xxxx)
*accumulator = (uint32_t)(c & 0x0F);
*pending_bytes = 2;
}
else if ((c & 0xF8) == 0xF0) {
// 4-byte sequence (11110xxx)
*accumulator = (uint32_t)(c & 0x07);
*pending_bytes = 3;
}
else {
// invalid start byte
ascii[length++] = '?';
}
}
else {
// continuation byte expected (10xxxxxx)
if ((c & 0xC0) == 0x80) {
// valid continuation
*accumulator = (*accumulator << 6) | (c & 0x3F);
if (--*pending_bytes == 0) {
unsigned char ch = '?';
if (*accumulator >= unicode_box_to_cp437_first && *accumulator <= unicode_box_to_cp437_last) {
ch = unicode_box_to_cp437[*accumulator - unicode_box_to_cp437_first];
}
ascii[length++] = ch;
*accumulator = 0;
}
}
else {
// invalid continuation → recovery
*pending_bytes = 0;
*accumulator = 0;
ascii[length++] = '?';
i--; // re-process this byte as start of new sequence
}
}
}
return length;
}