big5.c revision 71579
11722Sjkh/*- 21722Sjkh * Copyright (c) 1993 31722Sjkh * The Regents of the University of California. All rights reserved. 41722Sjkh * 51722Sjkh * This code is derived from software contributed to Berkeley by 61722Sjkh * Paul Borman at Krystal Technologies. 71722Sjkh * 81722Sjkh * Redistribution and use in source and binary forms, with or without 91722Sjkh * modification, are permitted provided that the following conditions 101722Sjkh * are met: 111722Sjkh * 1. Redistributions of source code must retain the above copyright 121722Sjkh * notice, this list of conditions and the following disclaimer. 131722Sjkh * 2. Redistributions in binary form must reproduce the above copyright 141722Sjkh * notice, this list of conditions and the following disclaimer in the 151722Sjkh * documentation and/or other materials provided with the distribution. 161722Sjkh * 3. All advertising materials mentioning features or use of this software 171722Sjkh * must display the following acknowledgement: 181722Sjkh * This product includes software developed by the University of 191722Sjkh * California, Berkeley and its contributors. 201722Sjkh * 4. Neither the name of the University nor the names of its contributors 211722Sjkh * may be used to endorse or promote products derived from this software 221722Sjkh * without specific prior written permission. 231722Sjkh * 241722Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 251722Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261722Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271722Sjkh * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 281722Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291722Sjkh * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301722Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311722Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321722Sjkh * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331722Sjkh * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341722Sjkh * SUCH DAMAGE. 351722Sjkh * 361722Sjkh * $FreeBSD: head/lib/libc/locale/big5.c 71579 2001-01-24 13:01:12Z deischen $ 371722Sjkh */ 381722Sjkh 391722Sjkh#if defined(LIBC_SCCS) && !defined(lint) 401722Sjkhstatic char sccsid[] = "@(#)big5.c 8.1 (Berkeley) 6/4/93"; 411722Sjkh#endif /* LIBC_SCCS and not lint */ 421722Sjkh 431722Sjkh#include <rune.h> 441722Sjkh#include <stddef.h> 451722Sjkh#include <stdio.h> 461722Sjkh#include <stdlib.h> 471722Sjkh#include <sys/types.h> 481722Sjkh 491722Sjkhrune_t _BIG5_sgetrune __P((const char *, size_t, char const **)); 501722Sjkhint _BIG5_sputrune __P((rune_t, char *, size_t, char **)); 511722Sjkh 521722Sjkhint 531722Sjkh_BIG5_init(rl) 541722Sjkh _RuneLocale *rl; 551722Sjkh{ 561722Sjkh rl->sgetrune = _BIG5_sgetrune; 571722Sjkh rl->sputrune = _BIG5_sputrune; 588857Srgrimes _CurrentRuneLocale = rl; 591722Sjkh __mb_cur_max = 2; 601722Sjkh return (0); 611722Sjkh} 621722Sjkh 631722Sjkhstatic inline int 641722Sjkh_big5_check(c) 651722Sjkh u_int c; 661722Sjkh{ 671722Sjkh c &= 0xff; 681722Sjkh return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1); 691722Sjkh} 701722Sjkh 711722Sjkhrune_t 721722Sjkh_BIG5_sgetrune(string, n, result) 731722Sjkh const char *string; 741722Sjkh size_t n; 751722Sjkh char const **result; 761722Sjkh{ 771722Sjkh rune_t rune = 0; 781722Sjkh int len; 791722Sjkh 801722Sjkh if (n < 1 || (len = _big5_check(*string)) > n) { 811722Sjkh if (result) 821722Sjkh *result = string; 831722Sjkh return (_INVALID_RUNE); 841722Sjkh } 851722Sjkh while (--len >= 0) 861722Sjkh rune = (rune << 8) | ((u_int)(*string++) & 0xff); 871722Sjkh if (result) 881722Sjkh *result = string; 891722Sjkh return rune; 901722Sjkh} 911722Sjkh 921722Sjkhint 931722Sjkh_BIG5_sputrune(c, string, n, result) 941722Sjkh rune_t c; 951722Sjkh char *string, **result; 961722Sjkh size_t n; 971722Sjkh{ 981722Sjkh if (c & 0x8000) { 991722Sjkh if (n >= 2) { 1001722Sjkh string[0] = (c >> 8) & 0xff; 1011722Sjkh string[1] = c & 0xff; 1021722Sjkh if (result) 1031722Sjkh *result = string + 2; 1041722Sjkh return (2); 1051722Sjkh } 1061722Sjkh } 1071722Sjkh else { 1081722Sjkh if (n >= 1) { 1091722Sjkh *string = c & 0xff; 1101722Sjkh if (result) 1111722Sjkh *result = string + 1; 1121722Sjkh return (1); 1131722Sjkh } 1141722Sjkh } 1151722Sjkh if (result) 1161722Sjkh *result = string; 1171722Sjkh return (0); 1181722Sjkh 1191722Sjkh} 1201722Sjkh