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
|
#ifndef lint
static char *rcsid = "$Id: utf8.c,v 1.1 2000/12/07 00:52:26 tale Exp $";
#endif
/*
* Copyright (c) 2000 Japan Network Information Center. All rights reserved.
*
* By using this file, you agree to the terms and conditions set forth bellow.
*
* LICENSE TERMS AND CONDITIONS
*
* The following License Terms and Conditions apply, unless a different
* license is obtained from Japan Network Information Center ("JPNIC"),
* a Japanese association, Fuundo Bldg., 1-2 Kanda Ogawamachi, Chiyoda-ku,
* Tokyo, Japan.
*
* 1. Use, Modification and Redistribution (including distribution of any
* modified or derived work) in source and/or binary forms is permitted
* under this License Terms and Conditions.
*
* 2. Redistribution of source code must retain the copyright notices as they
* appear in each source code file, this License Terms and Conditions.
*
* 3. Redistribution in binary form must reproduce the Copyright Notice,
* this License Terms and Conditions, in the documentation and/or other
* materials provided with the distribution. For the purposes of binary
* distribution the "Copyright Notice" refers to the following language:
* "Copyright (c) Japan Network Information Center. All rights reserved."
*
* 4. Neither the name of JPNIC may be used to endorse or promote products
* derived from this Software without specific prior written approval of
* JPNIC.
*
* 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
* "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 JPNIC 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 DAMAGES.
*
* 6. Indemnification by Licensee
* Any person or entities using and/or redistributing this Software under
* this License Terms and Conditions shall defend indemnify and hold
* harmless JPNIC from and against any and all judgements damages,
* expenses, settlement liabilities, cost and other liabilities of any
* kind as a result of use and redistribution of this Software or any
* claim, suite, action, litigation or proceeding by any third party
* arising out of or relates to this License Terms and Conditions.
*
* 7. Governing Law, Jurisdiction and Venue
* This License Terms and Conditions shall be governed by and and
* construed in accordance with the law of Japan. Any person or entities
* using and/or redistributing this Software under this License Terms and
* Conditions hereby agrees and consent to the personal and exclusive
* jurisdiction and venue of Tokyo District Court of Japan.
*/
#include <config.h>
#include <stddef.h>
#include <mdn/assert.h>
#include <mdn/logmacro.h>
#include <mdn/utf8.h>
#include <mdn/debug.h>
#define UTF8_WIDTH(c) \
(((c) < 0x80) ? 1 : \
((c) < 0xc0) ? 0 : \
((c) < 0xe0) ? 2 : \
((c) < 0xf0) ? 3 : \
((c) < 0xf8) ? 4 : \
((c) < 0xfc) ? 5 : \
((c) < 0xfe) ? 6 : 0)
#define VALID_CONT_BYTE(c) (0x80 <= (c) && (c) < 0xc0)
int
mdn_utf8_mblen(const char *s) {
int c = *(unsigned char *)s;
assert(s != NULL);
#if 0
TRACE(("mdn_utf8_mblen(s=<%s>)\n", mdn_debug_hexstring(s, 6)));
#endif
return UTF8_WIDTH(c);
}
int
mdn_utf8_getmb(const char *s, size_t len, char *buf) {
/* buf must be at least 7-bytes long */
const unsigned char *p = (const unsigned char *)s;
unsigned char *q = (unsigned char *)buf;
int width = UTF8_WIDTH(*p);
int w;
assert(s != NULL);
#if 0
TRACE(("mdn_utf8_getmb(s=<%s>,len=%d)\n",
mdn_debug_hexstring(s, 6), len));
#endif
if (width == 0 || len < width)
return (0);
/* Copy the first byte. */
*q++ = *p++;
/* .. and the rest. */
w = width;
while (--w > 0) {
if (!VALID_CONT_BYTE(*p))
return (0);
*q++ = *p++;
}
return (width);
}
extern int
mdn_utf8_getwc(const char *s, size_t len, unsigned long *vp) {
unsigned long v;
unsigned long min;
const unsigned char *p = (const unsigned char *)s;
int c;
int width;
int rest;
assert(s != NULL);
#if 0
TRACE(("mdn_utf8_getwc(s=<%s>,len=%d)\n",
mdn_debug_hexstring(s, 10), len));
#endif
c = *p++;
width = UTF8_WIDTH(c);
switch (width) {
case 0:
return (0);
case 1:
v = c;
min = 0;
break;
case 2:
v = c & 0x1f;
min = 0x80;
break;
case 3:
v = c & 0xf;
min = 0x800;
break;
case 4:
v = c & 0x7;
min = 0x10000;
break;
case 5:
v = c & 3;
min = 0x200000;
break;
case 6:
v = c & 1;
min = 0x4000000;
break;
default:
FATAL(("mdn_utf8_getint: internal error\n"));
return (0);
}
if (len < width)
return (0);
rest = width - 1;
while (rest-- > 0) {
if (!VALID_CONT_BYTE(*p))
return (0);
v = (v << 6) | (*p & 0x3f);
p++;
}
if (v < min)
return (0);
*vp = v;
return (width);
}
extern int
mdn_utf8_putwc(char *s, size_t len, unsigned long v) {
unsigned char *p = (unsigned char *)s;
int mask;
int off;
int l;
assert(s != NULL);
#if 0
TRACE(("mdn_utf8_putwc(v=%lx)\n", v));
#endif
if (v < 0x80) {
mask = 0;
l = 1;
} else if (v < 0x800) {
mask = 0xc0;
l = 2;
} else if (v < 0x10000) {
mask = 0xe0;
l = 3;
} else if (v < 0x200000) {
mask = 0xf0;
l = 4;
} else if (v < 0x4000000) {
mask = 0xf8;
l = 5;
} else if (v < 0x80000000) {
mask = 0xfc;
l = 6;
} else {
return (0);
}
if (len < l)
return (0);
off = 6 * (l - 1);
*p++ = (v >> off) | mask;
mask = 0x80;
while (off > 0) {
off -= 6;
*p++ = ((v >> off) & 0x3f) | mask;
}
return l;
}
int
mdn_utf8_isvalidchar(const char *s) {
unsigned long dummy;
TRACE(("mdn_utf8_isvalidchar(s=<%s>)\n",
mdn_debug_hexstring(s, 6)));
return (mdn_utf8_getwc(s, 6, &dummy) > 0);
}
int
mdn_utf8_isvalidstring(const char *s) {
unsigned long dummy;
int width;
assert(s != NULL);
TRACE(("mdn_utf8_isvalidstring(s=<%s>)\n",
mdn_debug_hexstring(s, 20)));
while (*s != '\0') {
width = mdn_utf8_getwc(s, 6, &dummy);
if (width == 0)
return (0);
s += width;
}
return (1);
}
char *
mdn_utf8_findfirstbyte(const char *s, const char *known_top) {
const unsigned char *p = (const unsigned char *)s;
const unsigned char *t = (const unsigned char *)known_top;
assert(s != NULL && known_top != NULL && known_top <= s);
TRACE(("mdn_utf8_findfirstbyte(s=<%s>)\n",
mdn_debug_hexstring(s, 8)));
while (p >= t) {
if (!VALID_CONT_BYTE(*p))
break;
p--;
}
if (p < t || UTF8_WIDTH(*p) == 0)
return (NULL);
return ((char *)p);
}
|