1*0a6a1f1dSLionel Sambuc /* $NetBSD: unicode.h,v 1.7 2014/04/06 19:25:22 jakllsch Exp $ */
284d9c625SLionel Sambuc
384d9c625SLionel Sambuc /*-
484d9c625SLionel Sambuc * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
584d9c625SLionel Sambuc * All rights reserved.
684d9c625SLionel Sambuc *
784d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
884d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
984d9c625SLionel Sambuc * are met:
1084d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1184d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1284d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1384d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1484d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
1584d9c625SLionel Sambuc *
1684d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1784d9c625SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1884d9c625SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1984d9c625SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2084d9c625SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2184d9c625SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2284d9c625SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2384d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2484d9c625SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2584d9c625SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2684d9c625SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
2784d9c625SLionel Sambuc */
2884d9c625SLionel Sambuc
2984d9c625SLionel Sambuc /*-
3084d9c625SLionel Sambuc * Copyright (c) 1993
3184d9c625SLionel Sambuc * The Regents of the University of California. All rights reserved.
3284d9c625SLionel Sambuc *
3384d9c625SLionel Sambuc * This code is derived from software contributed to Berkeley by
3484d9c625SLionel Sambuc * Paul Borman at Krystal Technologies.
3584d9c625SLionel Sambuc *
3684d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
3784d9c625SLionel Sambuc * modification, are permitted provided that the following conditions
3884d9c625SLionel Sambuc * are met:
3984d9c625SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
4084d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
4184d9c625SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
4284d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
4384d9c625SLionel Sambuc * documentation and/or other materials provided with the distribution.
4484d9c625SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
4584d9c625SLionel Sambuc * must display the following acknowledgement:
4684d9c625SLionel Sambuc * This product includes software developed by the University of
4784d9c625SLionel Sambuc * California, Berkeley and its contributors.
4884d9c625SLionel Sambuc * 4. Neither the name of the University nor the names of its contributors
4984d9c625SLionel Sambuc * may be used to endorse or promote products derived from this software
5084d9c625SLionel Sambuc * without specific prior written permission.
5184d9c625SLionel Sambuc *
5284d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5384d9c625SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5484d9c625SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5584d9c625SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5684d9c625SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5784d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5884d9c625SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5984d9c625SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6084d9c625SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6184d9c625SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6284d9c625SLionel Sambuc * SUCH DAMAGE.
6384d9c625SLionel Sambuc */
6484d9c625SLionel Sambuc
6584d9c625SLionel Sambuc /*
6684d9c625SLionel Sambuc * Routines for handling Unicode encoded in UTF-8 form, code derived from
6784d9c625SLionel Sambuc * src/lib/libc/locale/utf2.c.
6884d9c625SLionel Sambuc */
6984d9c625SLionel Sambuc static u_int16_t wget_utf8(const char **, size_t *) __unused;
7084d9c625SLionel Sambuc static int wput_utf8(char *, size_t, u_int16_t) __unused;
7184d9c625SLionel Sambuc
7284d9c625SLionel Sambuc /*
7384d9c625SLionel Sambuc * Read one UTF8-encoded character off the string, shift the string pointer
7484d9c625SLionel Sambuc * and return the character.
7584d9c625SLionel Sambuc */
7684d9c625SLionel Sambuc static u_int16_t
wget_utf8(const char ** str,size_t * sz)7784d9c625SLionel Sambuc wget_utf8(const char **str, size_t *sz)
7884d9c625SLionel Sambuc {
79*0a6a1f1dSLionel Sambuc size_t c;
8084d9c625SLionel Sambuc u_int16_t rune = 0;
8184d9c625SLionel Sambuc const char *s = *str;
8284d9c625SLionel Sambuc static const int _utf_count[16] = {
8384d9c625SLionel Sambuc 1, 1, 1, 1, 1, 1, 1, 1,
8484d9c625SLionel Sambuc 0, 0, 0, 0, 2, 2, 3, 0,
8584d9c625SLionel Sambuc };
8684d9c625SLionel Sambuc
8784d9c625SLionel Sambuc /* must be called with at least one byte remaining */
8884d9c625SLionel Sambuc KASSERT(*sz > 0);
8984d9c625SLionel Sambuc
9084d9c625SLionel Sambuc c = _utf_count[(s[0] & 0xf0) >> 4];
9184d9c625SLionel Sambuc if (c == 0 || c > *sz) {
9284d9c625SLionel Sambuc decoding_error:
9384d9c625SLionel Sambuc /*
9484d9c625SLionel Sambuc * The first character is in range 128-255 and doesn't
9584d9c625SLionel Sambuc * mark valid a valid UTF-8 sequence. There is not much
9684d9c625SLionel Sambuc * we can do with this, so handle by returning
9784d9c625SLionel Sambuc * the first character as if it would be a correctly
9884d9c625SLionel Sambuc * encoded ISO-8859-1 character.
9984d9c625SLionel Sambuc */
10084d9c625SLionel Sambuc c = 1;
10184d9c625SLionel Sambuc }
10284d9c625SLionel Sambuc
10384d9c625SLionel Sambuc switch (c) {
10484d9c625SLionel Sambuc case 1:
10584d9c625SLionel Sambuc rune = s[0] & 0xff;
10684d9c625SLionel Sambuc break;
10784d9c625SLionel Sambuc case 2:
10884d9c625SLionel Sambuc if ((s[1] & 0xc0) != 0x80)
10984d9c625SLionel Sambuc goto decoding_error;
11084d9c625SLionel Sambuc rune = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
11184d9c625SLionel Sambuc break;
11284d9c625SLionel Sambuc case 3:
11384d9c625SLionel Sambuc if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80)
11484d9c625SLionel Sambuc goto decoding_error;
11584d9c625SLionel Sambuc rune = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6)
11684d9c625SLionel Sambuc | (s[2] & 0x3F);
11784d9c625SLionel Sambuc break;
11884d9c625SLionel Sambuc }
11984d9c625SLionel Sambuc
12084d9c625SLionel Sambuc *str += c;
12184d9c625SLionel Sambuc *sz -= c;
12284d9c625SLionel Sambuc return rune;
12384d9c625SLionel Sambuc }
12484d9c625SLionel Sambuc
12584d9c625SLionel Sambuc /*
12684d9c625SLionel Sambuc * Encode wide character and write it to the string. 'n' specifies
12784d9c625SLionel Sambuc * how much buffer space remains in 's'. Returns number of bytes written
12884d9c625SLionel Sambuc * to the target string 's'.
12984d9c625SLionel Sambuc */
13084d9c625SLionel Sambuc static int
wput_utf8(char * s,size_t n,u_int16_t wc)13184d9c625SLionel Sambuc wput_utf8(char *s, size_t n, u_int16_t wc)
13284d9c625SLionel Sambuc {
13384d9c625SLionel Sambuc if (wc & 0xf800) {
13484d9c625SLionel Sambuc if (n < 3) {
13584d9c625SLionel Sambuc /* bound check failure */
13684d9c625SLionel Sambuc return 0;
13784d9c625SLionel Sambuc }
13884d9c625SLionel Sambuc
13984d9c625SLionel Sambuc s[0] = 0xE0 | (wc >> 12);
14084d9c625SLionel Sambuc s[1] = 0x80 | ((wc >> 6) & 0x3F);
14184d9c625SLionel Sambuc s[2] = 0x80 | ((wc) & 0x3F);
14284d9c625SLionel Sambuc return 3;
14384d9c625SLionel Sambuc } else if (wc & 0x0780) {
14484d9c625SLionel Sambuc if (n < 2) {
14584d9c625SLionel Sambuc /* bound check failure */
14684d9c625SLionel Sambuc return 0;
14784d9c625SLionel Sambuc }
14884d9c625SLionel Sambuc
14984d9c625SLionel Sambuc s[0] = 0xC0 | (wc >> 6);
15084d9c625SLionel Sambuc s[1] = 0x80 | ((wc) & 0x3F);
15184d9c625SLionel Sambuc return 2;
15284d9c625SLionel Sambuc } else {
15384d9c625SLionel Sambuc if (n < 1) {
15484d9c625SLionel Sambuc /* bound check failure */
15584d9c625SLionel Sambuc return 0;
15684d9c625SLionel Sambuc }
15784d9c625SLionel Sambuc
15884d9c625SLionel Sambuc s[0] = wc;
15984d9c625SLionel Sambuc return 1;
16084d9c625SLionel Sambuc }
16184d9c625SLionel Sambuc }
162