unicode.h revision 1.4.12.1 1 /* $NetBSD: unicode.h,v 1.4.12.1 2007/02/27 16:54:11 yamt Exp $ */
2
3 /*-
4 * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 /*-
37 * Copyright (c) 1993
38 * The Regents of the University of California. All rights reserved.
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Paul Borman at Krystal Technologies.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 */
71
72 /*
73 * Routines for handling Unicode encoded in UTF-8 form, code derived from
74 * src/lib/libc/locale/utf2.c.
75 */
76 static u_int16_t wget_utf8(const char **, size_t *) __unused;
77 static int wput_utf8(char *, size_t, u_int16_t) __unused;
78
79 /*
80 * Read one UTF8-encoded character off the string, shift the string pointer
81 * and return the character.
82 */
83 static u_int16_t
84 wget_utf8(const char **str, size_t *sz)
85 {
86 int c;
87 u_int16_t rune = 0;
88 const char *s = *str;
89 static const int _utf_count[16] = {
90 1, 1, 1, 1, 1, 1, 1, 1,
91 0, 0, 0, 0, 2, 2, 3, 0,
92 };
93
94 /* must be called with at least one byte remaining */
95 KASSERT(*sz > 0);
96
97 c = _utf_count[(s[0] & 0xf0) >> 4];
98 if (c == 0 || c > *sz) {
99 decoding_error:
100 /*
101 * The first character is in range 128-255 and doesn't
102 * mark valid a valid UTF-8 sequence. There is not much
103 * we can do with this, so handle by returning
104 * the first character as if it would be a correctly
105 * encoded ISO-8859-1 character.
106 */
107 c = 1;
108 }
109
110 switch (c) {
111 case 1:
112 rune = s[0] & 0xff;
113 break;
114 case 2:
115 if ((s[1] & 0xc0) != 0x80)
116 goto decoding_error;
117 rune = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
118 break;
119 case 3:
120 if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80)
121 goto decoding_error;
122 rune = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6)
123 | (s[2] & 0x3F);
124 break;
125 }
126
127 *str += c;
128 *sz -= c;
129 return rune;
130 }
131
132 /*
133 * Encode wide character and write it to the string. 'n' specifies
134 * how much buffer space remains in 's'. Returns number of bytes written
135 * to the target string 's'.
136 */
137 static int
138 wput_utf8(char *s, size_t n, u_int16_t wc)
139 {
140 if (wc & 0xf800) {
141 if (n < 3) {
142 /* bound check failure */
143 return 0;
144 }
145
146 s[0] = 0xE0 | (wc >> 12);
147 s[1] = 0x80 | ((wc >> 6) & 0x3F);
148 s[2] = 0x80 | ((wc) & 0x3F);
149 return 3;
150 } else if (wc & 0x0780) {
151 if (n < 2) {
152 /* bound check failure */
153 return 0;
154 }
155
156 s[0] = 0xC0 | (wc >> 6);
157 s[1] = 0x80 | ((wc) & 0x3F);
158 return 2;
159 } else {
160 if (n < 1) {
161 /* bound check failure */
162 return 0;
163 }
164
165 s[0] = wc;
166 return 1;
167 }
168 }
169