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