big5.c revision 38333
1/*-
2 * Copyright (c) 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Paul Borman at Krystal Technologies.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifdef XPG4
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
42#include <errno.h>
43#include <rune.h>
44#include <stddef.h>
45#include <stdio.h>
46#include <stdlib.h>
47
48rune_t	_BIG5_sgetrune __P((const char *, size_t, char const **));
49int	_BIG5_sputrune __P((rune_t, char *, size_t, char **));
50
51int
52_BIG5_init(rl)
53	_RuneLocale *rl;
54{
55	rl->sgetrune = _BIG5_sgetrune;
56	rl->sputrune = _BIG5_sputrune;
57	_CurrentRuneLocale = rl;
58	__mb_cur_max = 2;
59	return (0);
60}
61
62static inline int
63_big5_check(c)
64	u_int c;
65{
66	c &= 0xff;
67	return ((c >= 0xa1 && c <= 0xfe) ? 2 : 1);
68}
69
70rune_t
71_BIG5_sgetrune(string, n, result)
72	const char *string;
73	size_t n;
74	char const **result;
75{
76	rune_t rune = 0;
77	int len;
78
79	if (n < 1 || (len = _big5_check(*string)) > n) {
80		if (result)
81			*result = string;
82		return (_INVALID_RUNE);
83	}
84	while (len-- >= 0)
85		rune = (rune << 8) | ((u_int)(*string++) & 0xff);
86	if (result)
87		*result = string + len;
88	return rune;
89}
90
91int
92_BIG5_sputrune(c, string, n, result)
93	rune_t c;
94	char *string, **result;
95	size_t n;
96{
97	if (c & 0x8000) {
98		if (n >= 2) {
99			string[0] = (c >> 8) & 0xff;
100			string[1] = c & 0xff;
101			if (result)
102				*result = string + 2;
103			return (2);
104		}
105	}
106	else {
107		if (n >= 1) {
108			*string = c & 0xff;
109			if (result)
110				*result = string + 1;
111			return (1);
112		}
113	}
114	if (result)
115		*result = string;
116	return (0);
117
118}
119#endif /* XPG4 */
120