big5.c revision 121893
138333Sphk/*- 2121893Stjr * Copyright (c) 2002, 2003 Tim J. Robbins. All rights reserved. 338333Sphk * Copyright (c) 1993 438333Sphk * The Regents of the University of California. All rights reserved. 538333Sphk * 638333Sphk * This code is derived from software contributed to Berkeley by 738333Sphk * Paul Borman at Krystal Technologies. 838333Sphk * 938333Sphk * Redistribution and use in source and binary forms, with or without 1038333Sphk * modification, are permitted provided that the following conditions 1138333Sphk * are met: 1238333Sphk * 1. Redistributions of source code must retain the above copyright 1338333Sphk * notice, this list of conditions and the following disclaimer. 1438333Sphk * 2. Redistributions in binary form must reproduce the above copyright 1538333Sphk * notice, this list of conditions and the following disclaimer in the 1638333Sphk * documentation and/or other materials provided with the distribution. 1738333Sphk * 3. All advertising materials mentioning features or use of this software 1838333Sphk * must display the following acknowledgement: 1938333Sphk * This product includes software developed by the University of 2038333Sphk * California, Berkeley and its contributors. 2138333Sphk * 4. Neither the name of the University nor the names of its contributors 2238333Sphk * may be used to endorse or promote products derived from this software 2338333Sphk * without specific prior written permission. 2438333Sphk * 2538333Sphk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2638333Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2738333Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2838333Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2938333Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3038333Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3138333Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3238333Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3338333Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3438333Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3538333Sphk * SUCH DAMAGE. 3638333Sphk */ 3738333Sphk 3838333Sphk#if defined(LIBC_SCCS) && !defined(lint) 3938333Sphkstatic char sccsid[] = "@(#)big5.c 8.1 (Berkeley) 6/4/93"; 4038333Sphk#endif /* LIBC_SCCS and not lint */ 4192986Sobrien#include <sys/cdefs.h> 4292986Sobrien__FBSDID("$FreeBSD: head/lib/libc/locale/big5.c 121893 2003-11-02 10:09:33Z tjr $"); 4338333Sphk 44121893Stjr#include <sys/types.h> 45121893Stjr#include <runetype.h> 4638333Sphk#include <stddef.h> 4738333Sphk#include <stdio.h> 4838333Sphk#include <stdlib.h> 49121893Stjr#include <wchar.h> 5038333Sphk 51121893Stjrextern size_t (*__mbrtowc)(wchar_t * __restrict, const char * __restrict, 52121893Stjr size_t, mbstate_t * __restrict); 53121893Stjrextern size_t (*__wcrtomb)(char * __restrict, wchar_t, mbstate_t * __restrict); 5438333Sphk 55121893Stjrint _BIG5_init(_RuneLocale *); 56121893Stjrsize_t _BIG5_mbrtowc(wchar_t * __restrict, const char * __restrict, size_t, 57121893Stjr mbstate_t * __restrict); 58121893Stjrsize_t _BIG5_wcrtomb(char * __restrict, wchar_t, mbstate_t * __restrict); 59121893Stjr 6038333Sphkint 61121893Stjr_BIG5_init(_RuneLocale *rl) 6238333Sphk{ 63121893Stjr 64121893Stjr __mbrtowc = _BIG5_mbrtowc; 65121893Stjr __wcrtomb = _BIG5_wcrtomb; 6638333Sphk _CurrentRuneLocale = rl; 6738333Sphk __mb_cur_max = 2; 6838333Sphk return (0); 6938333Sphk} 7038333Sphk 71121893Stjrstatic __inline int 72121893Stjr_big5_check(u_int c) 7338333Sphk{ 74121893Stjr 7538333Sphk c &= 0xff; 7638333Sphk return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1); 7738333Sphk} 7838333Sphk 79121893Stjrsize_t 80121893Stjr_BIG5_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s, size_t n, 81121893Stjr mbstate_t * __restrict ps __unused) 8238333Sphk{ 83121893Stjr wchar_t wc; 84121893Stjr int i, len; 8538333Sphk 86121893Stjr if (s == NULL) 87121893Stjr /* Reset to initial shift state (no-op) */ 88121893Stjr return (0); 89121893Stjr if (n == 0 || (size_t)(len = _big5_check(*s)) > n) 90121893Stjr /* Incomplete multibyte sequence */ 91121893Stjr return ((size_t)-2); 92121893Stjr wc = 0; 93121893Stjr i = len; 94121893Stjr while (i-- > 0) 95121893Stjr wc = (wc << 8) | (unsigned char)*s++; 96121893Stjr if (pwc != NULL) 97121893Stjr *pwc = wc; 98121893Stjr return (wc == L'\0' ? 0 : len); 9938333Sphk} 10038333Sphk 101121893Stjrsize_t 102121893Stjr_BIG5_wcrtomb(char * __restrict s, wchar_t wc, 103121893Stjr mbstate_t * __restrict ps __unused) 10438333Sphk{ 105121893Stjr 106121893Stjr if (s == NULL) 107121893Stjr /* Reset to initial shift state (no-op) */ 108121893Stjr return (1); 109121893Stjr if (wc & 0x8000) { 110121893Stjr *s++ = (wc >> 8) & 0xff; 111121893Stjr *s = wc & 0xff; 112121893Stjr return (2); 11338333Sphk } 114121893Stjr *s = wc & 0xff; 115121893Stjr return (1); 11638333Sphk} 117