-
Notifications
You must be signed in to change notification settings - Fork 29
/
mbchar.c
232 lines (204 loc) · 9.14 KB
/
mbchar.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
/*-
* Copyright (c) 1998, 2002-2008 Kiyoshi Matsui <[email protected]>
* All rights reserved.
*
* Some parts of this code are derived from the public domain software
* DECUS cpp (1984,1985) written by Martin Minow.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* M B C H A R . C
* C h a r a c t e r h a n d l i n g R o u t i n e s
*
* Character handling and multi-byte character handling routines are
* placed here.
*/
#include "system.H"
#include "internal.H"
/*
* Tables of character types and multi-byte character types.
*/
/* Non-ASCII characters are always checked by mb_read(). */
#define NA 0x4000 /* Non-ASCII characters */
/* Horizontal spaces (' ', '\t' and TOK_SEP) */
#define HSPA (SPA | HSP)
short * char_type; /* Pointer to one of the following type_*[]. */
/*
* For UTF8 multi-byte character encoding.
*/
#define U2_1 0x100 /* 1st byte of 2-byte encoding of UTF8 */
#define U3_1 0x200 /* 1st byte of 3-byte encoding of UTF8 */
#define U4_1 0x400 /* 1st byte of 4-byte encoding of UTF8 */
#define UCONT 0x800 /* Continuation of a 2, 3, or 4 byte UTF8 sequence */
#define U2_1N (NA | U2_1)
#define U3_1N (NA | U3_1)
#define U4_1N (NA | U4_1)
#define UCONTN (NA | UCONT)
static short type_utf8[ UCHARMAX + 1] = {
/* Character type codes */
/* 0, 1, 2, 3, 4, 5, 6, 7, */
/* 8, 9, A, B, C, D, E, F, Hex */
000, 000, 000, 000, 000, 000, 000, 000, /* 00 */
000, HSPA, SPA, SPA, SPA, SPA, 000, 000, /* 08 */
000, 000, 000, 000, 000, 000, 000, 000, /* 10 */
/* 0x17-0x1A and 0x1F will be cleared in some modes by chk_opts() */
000, LET, LET, 000, 000, 000, 000, HSPA, /* 18 */
HSPA, PUNC, QUO, PUNC, 000, PUNC, PUNC, QUO, /* 20 !"#$%&' */
PUNC, PUNC, PUNC, PUNC, PUNC, PUNC, DOT, PUNC, /* 28 ()*+,-./ */
DIG, DIG, DIG, DIG, DIG, DIG, DIG, DIG, /* 30 01234567 */
DIG, DIG, PUNC, PUNC, PUNC, PUNC, PUNC, PUNC, /* 38 89:;<=>? */
000, LET, LET, LET, LET, LET, LET, LET, /* 40 @ABCDEFG */
LET, LET, LET, LET, LET, LET, LET, LET, /* 48 HIJKLMNO */
LET, LET, LET, LET, LET, LET, LET, LET, /* 50 PQRSTUVW */
LET, LET, LET, PUNC, 000, PUNC, PUNC, LET, /* 58 XYZ[\]^_ */
000, LET, LET, LET, LET, LET, LET, LET, /* 60 `abcdefg */
LET, LET, LET, LET, LET, LET, LET, LET, /* 68 hijklmno */
LET, LET, LET, LET, LET, LET, LET, LET, /* 70 pqrstuvw */
LET, LET, LET, PUNC, PUNC, PUNC, PUNC, 000, /* 78 xyz{|}~ */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* 80 .. 87 */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* 88 .. 8F */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* 90 .. 97 */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* 98 .. 9F */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* A0 .. A7 */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* A8 .. AF */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* B0 .. B7 */
UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN,UCONTN, /* B8 .. BF */
NA, NA, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, /* C0 .. C7 */
U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, /* C8 .. CF */
U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, /* D0 .. D7 */
U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, U2_1N, /* D8 .. DF */
U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, /* E0 .. E7 */
U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, U3_1N, /* E8 .. EF */
U4_1N, U4_1N, U4_1N, U4_1N, U4_1N, NA, NA, NA, /* F0 .. F7 */
NA, NA, NA, NA, NA, NA, NA, NA, /* F8 .. FF */
};
static int mbstart;
static size_t mb_read_utf8( int c1, char ** in_pp, char ** out_pp);
/* For UTF8 mbchar encoding */
void mb_init()
/*
* Initialize multi-byte character settings.
* First called prior to setting the 'mcpp_mode'.
* Will be called again each time the multibyte character encoding is changed.
*/
{
/*
* Select the character classification table, select the multi-byte
* character reading routine and decide whether multi-byte character
* may contain the byte of value 0x5c.
*/
char_type = type_utf8;
mb_read = mb_read_utf8;
mbstart = (U2_1 | U3_1 | U4_1);
mbchk = NA;
/*
* Modify magic characters in character type table.
* char_type[] table should be rewritten in accordance with the 'mcpp_mode'
* whenever the encoding is changed.
*/
char_type[ DEF_MAGIC] = LET;
char_type[ IN_SRC] = LET;
char_type[ TOK_SEP] = HSPA; /* TOK_SEP equals to COM_SEP */
}
static size_t mb_read_utf8(
int c1,
char ** in_pp,
char ** out_pp
)
/*
* Multi-byte character reading routine for UTF8.
*/
{
int error = FALSE;
size_t len = 0;
char * in_p = *in_pp;
char * out_p = *out_pp;
if (! (char_type[ c1 & UCHARMAX] & mbstart))
return MB_ERROR;
do {
unsigned int codepoint;
int i, bytes;
if ((char_type[ c1 & UCHARMAX] & U4_1) == U4_1)
bytes = 4; /* 4-byte character */
else if ((char_type[ c1 & UCHARMAX] & U3_1) == U3_1)
bytes = 3; /* 3-byte character */
else //if ((char_type[ c1 & UCHARMAX] & U2_1) == U2_1)
bytes = 2; /* 2-byte character */
/* Must ensure that the sequence is not reserved as a surrogate */
codepoint = ((2 << (6-bytes)) - 1) & c1; /* mask off top bits */
/* All bytes left in the sequence must be in 0x80 - 0xBF */
for (i = bytes - 1; i && !error; i--) {
codepoint = (codepoint << 6) + ((*in_p) & 0x3fU);
if (! (char_type[ (*out_p++ = *in_p++) & UCHARMAX] & UCONT))
error = TRUE;
}
/* Check for overlong/underlong sequences */
if ((bytes == 2 && (codepoint < 0x80 || codepoint > 0x7FF))
|| (bytes == 3 && (codepoint < 0x800 || codepoint > 0xFFFF))
|| (bytes == 4 && (codepoint < 0x10000 || codepoint > 0x10FFFF)))
error = TRUE;
if ((codepoint >= 0xD800 && codepoint <= 0xDFFF)
/* Check for reserved surrogate codepoints */
|| (codepoint >= 0xFFFE && codepoint <= 0xFFFF))
/* Illegal */
error = TRUE;
#if 0
printf( "codepoint:0x%x\n", codepoint);
#endif
if (error)
break;
len++;
} while (char_type[ (*out_p++ = c1 = *in_p++) & UCHARMAX] & mbstart);
/* Start of the next multi-byte character */
*in_pp = --in_p;
*(--out_p) = EOS;
*out_pp = out_p;
return error ? (len | MB_ERROR) : len;
}
uexpr_t mb_eval(
char ** seq_pp
)
/*
* Evaluate the value of a multi-byte character.
* This routine does not check the legality of the sequence.
* This routine is called from eval_char().
* This routine is never called in POST_STD mode.
*/
{
char * seq = *seq_pp;
uexpr_t val = 0;
int c;
if (! (char_type[ c = *seq++ & UCHARMAX] & mbstart)) {
*seq_pp = seq;
return c; /* Not a multi-byte character */
}
val = (c << 8) + (*seq++ & UCHARMAX);
if (char_type[ c & UCHARMAX] & U3_1) {
val = (val << 8) + (*seq++ & UCHARMAX);
} else if (char_type[ c & UCHARMAX] & U4_1) {
val = (val << 8) + (*seq++ & UCHARMAX);
val = (val << 8) + (*seq++ & UCHARMAX);
}
*seq_pp = seq;
return val;
}