unicode.h revision 1.1 1 /* $NetBSD: unicode.h,v 1.1 2004/11/21 16:28:40 jdolecek 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 * Routines for handling Unicode encoded in UTF-8 form, code derived from
38 * src/lib/libc/locale/utf2.c.
39 */
40
41 /*
42 * Read one wide character off the string, shift the string pointer
43 * and return the character.
44 */
45 static u_int16_t
46 wget_utf8(const char **str)
47 {
48 int c;
49 u_int16_t rune = 0;
50 const char *s = *str;
51 static const int _utf_count[16] = {
52 1, 1, 1, 1, 1, 1, 1, 1,
53 0, 0, 0, 0, 2, 2, 3, 0,
54 };
55
56
57 c = _utf_count[(s[0] & 0xf0) >> 4];
58 if (c == 0) {
59 decoding_error:
60 /*
61 * The first character is in range 128-255 and doesn't
62 * mark valid a valid UTF-8 sequence. There is not much
63 * we can do with this, so handle by returning
64 * the first character as if it would be a correctly
65 * encoded ISO-8859-1 character.
66 */
67 c = 1;
68 }
69
70 switch (c) {
71 case 1:
72 rune = s[0] & 0xff;
73 break;
74 case 2:
75 if (!s[0] || !s[1])
76 goto decoding_error;
77 if ((s[1] & 0xc0) != 0x80)
78 goto decoding_error;
79 rune = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
80 break;
81 case 3:
82 if (!s[0] || !s[1] || !s[2])
83 goto decoding_error;
84 if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80)
85 goto decoding_error;
86 rune = ((s[0] & 0x1F) << 12) | ((s[1] & 0x3F) << 6)
87 | (s[2] & 0x3F);
88 break;
89 }
90
91 *str = *str + c;
92 return rune;
93 }
94
95 /*
96 * Encode wide character and write it to the string. 'n' specifies
97 * how much buffer space remains in 's'. Returns number of bytes written
98 * to the target string 's'.
99 */
100 static int
101 wput_utf8(char *s, size_t n, u_int16_t wc)
102 {
103 if (wc & 0xf800) {
104 if (n < 3) {
105 /* bound check failure */
106 return 0;
107 }
108
109 s[0] = 0xE0 | ((wc >> 12) & 0x0F);
110 s[1] = 0x80 | ((wc >> 6) & 0x3F);
111 s[2] = 0x80 | ((wc) & 0x3F);
112 return 3;
113 } else if (wc & 0x0780) {
114 if (n < 2) {
115 /* bound check failure */
116 return 0;
117 }
118
119 s[0] = 0xC0 | ((wc >> 6) & 0x1F);
120 s[1] = 0x80 | ((wc) & 0x3F);
121 return 2;
122 } else {
123 if (n < 1) {
124 /* bound check failure */
125 return 0;
126 }
127
128 s[0] = wc;
129 return 1;
130 }
131 }
132