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