big5.c revision 122283
1/*- 2 * Copyright (c) 2002, 2003 Tim J. Robbins. All rights reserved. 3 * Copyright (c) 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Paul Borman at Krystal Technologies. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38#if defined(LIBC_SCCS) && !defined(lint) 39static char sccsid[] = "@(#)big5.c 8.1 (Berkeley) 6/4/93"; 40#endif /* LIBC_SCCS and not lint */ 41#include <sys/cdefs.h> 42__FBSDID("$FreeBSD: head/lib/libc/locale/big5.c 122283 2003-11-08 02:58:37Z tjr $"); 43 44#include <sys/types.h> 45#include <runetype.h> 46#include <stdlib.h> 47#include <wchar.h> 48 49extern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict, 50 size_t, mbstate_t * __restrict); 51extern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict); 52 53int _BIG5_init(_RuneLocale *); 54size_t _BIG5_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t, 55 mbstate_t * __restrict); 56size_t _BIG5_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict); 57 58int 59_BIG5_init(_RuneLocale *rl) 60{ 61 62 __mbrtowc = _BIG5_mbrtowc; 63 __wcrtomb = _BIG5_wcrtomb; 64 _CurrentRuneLocale = rl; 65 __mb_cur_max = 2; 66 return (0); 67} 68 69static __inline int 70_big5_check(u_int c) 71{ 72 73 c &= 0xff; 74 return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1); 75} 76 77size_t 78_BIG5_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, 79 mbstate_t * __restrict ps __unused) 80{ 81 wchar_t wc; 82 int i, len; 83 84 if (s == NULL) 85 /* Reset to initial shift state (no-op) */ 86 return (0); 87 if (n == 0 || (size_t)(len = _big5_check(*s)) > n) 88 /* Incomplete multibyte sequence */ 89 return ((size_t)-2); 90 wc = 0; 91 i = len; 92 while (i-- > 0) 93 wc = (wc << 8) | (unsigned char)*s++; 94 if (pwc != NULL) 95 *pwc = wc; 96 return (wc == L'\0' ? 0 : len); 97} 98 99size_t 100_BIG5_wcrtomb(char * __restrict s, wchar_t wc, 101 mbstate_t * __restrict ps __unused) 102{ 103 104 if (s == NULL) 105 /* Reset to initial shift state (no-op) */ 106 return (1); 107 if (wc & 0x8000) { 108 *s++ = (wc >> 8) & 0xff; 109 *s = wc & 0xff; 110 return (2); 111 } 112 *s = wc & 0xff; 113 return (1); 114} 115