xref: /netbsd-src/sys/fs/unicode.h (revision 6d9f89fe00dbe0479e2657b534b06218779c7d22)
1*6d9f89feSjakllsch /* $NetBSD: unicode.h,v 1.7 2014/04/06 19:25:22 jakllsch Exp $ */
254678300Sjdolecek 
354678300Sjdolecek /*-
454678300Sjdolecek  * Copyright (c) 2001, 2004 The NetBSD Foundation, Inc.
554678300Sjdolecek  * All rights reserved.
654678300Sjdolecek  *
754678300Sjdolecek  * Redistribution and use in source and binary forms, with or without
854678300Sjdolecek  * modification, are permitted provided that the following conditions
954678300Sjdolecek  * are met:
1054678300Sjdolecek  * 1. Redistributions of source code must retain the above copyright
1154678300Sjdolecek  *    notice, this list of conditions and the following disclaimer.
1254678300Sjdolecek  * 2. Redistributions in binary form must reproduce the above copyright
1354678300Sjdolecek  *    notice, this list of conditions and the following disclaimer in the
1454678300Sjdolecek  *    documentation and/or other materials provided with the distribution.
1554678300Sjdolecek  *
1654678300Sjdolecek  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1754678300Sjdolecek  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1854678300Sjdolecek  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1954678300Sjdolecek  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2054678300Sjdolecek  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2154678300Sjdolecek  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2254678300Sjdolecek  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2354678300Sjdolecek  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2454678300Sjdolecek  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2554678300Sjdolecek  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2654678300Sjdolecek  * POSSIBILITY OF SUCH DAMAGE.
2754678300Sjdolecek  */
2854678300Sjdolecek 
29fa971123Syamt /*-
30fa971123Syamt  * Copyright (c) 1993
31fa971123Syamt  *	The Regents of the University of California.  All rights reserved.
32fa971123Syamt  *
33fa971123Syamt  * This code is derived from software contributed to Berkeley by
34fa971123Syamt  * Paul Borman at Krystal Technologies.
35fa971123Syamt  *
36fa971123Syamt  * Redistribution and use in source and binary forms, with or without
37fa971123Syamt  * modification, are permitted provided that the following conditions
38fa971123Syamt  * are met:
39fa971123Syamt  * 1. Redistributions of source code must retain the above copyright
40fa971123Syamt  *    notice, this list of conditions and the following disclaimer.
41fa971123Syamt  * 2. Redistributions in binary form must reproduce the above copyright
42fa971123Syamt  *    notice, this list of conditions and the following disclaimer in the
43fa971123Syamt  *    documentation and/or other materials provided with the distribution.
44fa971123Syamt  * 3. All advertising materials mentioning features or use of this software
45fa971123Syamt  *    must display the following acknowledgement:
46fa971123Syamt  *	This product includes software developed by the University of
47fa971123Syamt  *	California, Berkeley and its contributors.
48fa971123Syamt  * 4. Neither the name of the University nor the names of its contributors
49fa971123Syamt  *    may be used to endorse or promote products derived from this software
50fa971123Syamt  *    without specific prior written permission.
51fa971123Syamt  *
52fa971123Syamt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53fa971123Syamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54fa971123Syamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55fa971123Syamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56fa971123Syamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57fa971123Syamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58fa971123Syamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59fa971123Syamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60fa971123Syamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61fa971123Syamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62fa971123Syamt  * SUCH DAMAGE.
63fa971123Syamt  */
64fa971123Syamt 
6554678300Sjdolecek /*
6654678300Sjdolecek  * Routines for handling Unicode encoded in UTF-8 form, code derived from
6754678300Sjdolecek  * src/lib/libc/locale/utf2.c.
6854678300Sjdolecek  */
6933ac0de1Sdrochner static u_int16_t wget_utf8(const char **, size_t *) __unused;
7033ac0de1Sdrochner static int wput_utf8(char *, size_t, u_int16_t) __unused;
7154678300Sjdolecek 
7254678300Sjdolecek /*
732ce9f451Sjdolecek  * Read one UTF8-encoded character off the string, shift the string pointer
7454678300Sjdolecek  * and return the character.
7554678300Sjdolecek  */
7654678300Sjdolecek static u_int16_t
wget_utf8(const char ** str,size_t * sz)772ce9f451Sjdolecek wget_utf8(const char **str, size_t *sz)
7854678300Sjdolecek {
79*6d9f89feSjakllsch 	size_t c;
8054678300Sjdolecek 	u_int16_t rune = 0;
8154678300Sjdolecek 	const char *s = *str;
8254678300Sjdolecek 	static const int _utf_count[16] = {
8354678300Sjdolecek 		1, 1, 1, 1, 1, 1, 1, 1,
8454678300Sjdolecek 		0, 0, 0, 0, 2, 2, 3, 0,
8554678300Sjdolecek 	};
8654678300Sjdolecek 
872ce9f451Sjdolecek 	/* must be called with at least one byte remaining */
882ce9f451Sjdolecek 	KASSERT(*sz > 0);
8954678300Sjdolecek 
9054678300Sjdolecek 	c = _utf_count[(s[0] & 0xf0) >> 4];
912ce9f451Sjdolecek 	if (c == 0 || c > *sz) {
9254678300Sjdolecek     decoding_error:
9354678300Sjdolecek 		/*
9454678300Sjdolecek 		 * The first character is in range 128-255 and doesn't
9554678300Sjdolecek 		 * mark valid a valid UTF-8 sequence. There is not much
9654678300Sjdolecek 		 * we can do with this, so handle by returning
9754678300Sjdolecek 		 * the first character as if it would be a correctly
9854678300Sjdolecek 		 * encoded ISO-8859-1 character.
9954678300Sjdolecek 		 */
10054678300Sjdolecek 		c = 1;
10154678300Sjdolecek 	}
10254678300Sjdolecek 
10354678300Sjdolecek 	switch (c) {
10454678300Sjdolecek 	case 1:
10554678300Sjdolecek 		rune = s[0] & 0xff;
10654678300Sjdolecek 		break;
10754678300Sjdolecek 	case 2:
10854678300Sjdolecek 		if ((s[1] & 0xc0) != 0x80)
10954678300Sjdolecek 			goto decoding_error;
11054678300Sjdolecek 		rune = ((s[0] & 0x1F) << 6) | (s[1] & 0x3F);
11154678300Sjdolecek 		break;
11254678300Sjdolecek 	case 3:
11354678300Sjdolecek 		if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80)
11454678300Sjdolecek 			goto decoding_error;
11533ac0de1Sdrochner 		rune = ((s[0] & 0x0F) << 12) | ((s[1] & 0x3F) << 6)
11654678300Sjdolecek 		    | (s[2] & 0x3F);
11754678300Sjdolecek 		break;
11854678300Sjdolecek 	}
11954678300Sjdolecek 
1202ce9f451Sjdolecek 	*str += c;
1212ce9f451Sjdolecek 	*sz -= c;
12254678300Sjdolecek 	return rune;
12354678300Sjdolecek }
12454678300Sjdolecek 
12554678300Sjdolecek /*
12654678300Sjdolecek  * Encode wide character and write it to the string. 'n' specifies
12754678300Sjdolecek  * how much buffer space remains in 's'. Returns number of bytes written
12854678300Sjdolecek  * to the target string 's'.
12954678300Sjdolecek  */
13054678300Sjdolecek static int
wput_utf8(char * s,size_t n,u_int16_t wc)13154678300Sjdolecek wput_utf8(char *s, size_t n, u_int16_t wc)
13254678300Sjdolecek {
13354678300Sjdolecek 	if (wc & 0xf800) {
13454678300Sjdolecek 		if (n < 3) {
13554678300Sjdolecek 			/* bound check failure */
13654678300Sjdolecek 			return 0;
13754678300Sjdolecek 		}
13854678300Sjdolecek 
13933ac0de1Sdrochner 		s[0] = 0xE0 | (wc >> 12);
14054678300Sjdolecek 		s[1] = 0x80 | ((wc >> 6) & 0x3F);
14154678300Sjdolecek 		s[2] = 0x80 | ((wc) & 0x3F);
14254678300Sjdolecek 		return 3;
14354678300Sjdolecek 	} else if (wc & 0x0780) {
14454678300Sjdolecek 		if (n < 2) {
14554678300Sjdolecek 			/* bound check failure */
14654678300Sjdolecek 			return 0;
14754678300Sjdolecek 		}
14854678300Sjdolecek 
14933ac0de1Sdrochner 		s[0] = 0xC0 | (wc >> 6);
15054678300Sjdolecek 		s[1] = 0x80 | ((wc) & 0x3F);
15154678300Sjdolecek 		return 2;
15254678300Sjdolecek 	} else {
15354678300Sjdolecek 		if (n < 1) {
15454678300Sjdolecek 			/* bound check failure */
15554678300Sjdolecek 			return 0;
15654678300Sjdolecek 		}
15754678300Sjdolecek 
15854678300Sjdolecek 		s[0] = wc;
15954678300Sjdolecek 		return 1;
16054678300Sjdolecek 	}
16154678300Sjdolecek }
162