xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/xccs.multibyte.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #if !defined(lint) && defined(SCCSIDS)
30*0Sstevel@tonic-gate static  char *sccsid = "%Z%%M% %I%     %E% SMI";
31*0Sstevel@tonic-gate #endif
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #define CS377	0377
37*0Sstevel@tonic-gate #define MASK	0x0000ffff
38*0Sstevel@tonic-gate #define TOP1	0x80000000
39*0Sstevel@tonic-gate #define TOP2	0x08000000
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*
43*0Sstevel@tonic-gate  * mbtowc routines for the Xerox XCCS codeset standard
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate int
_mbtowc_xccs(pwc,s,n)46*0Sstevel@tonic-gate _mbtowc_xccs(pwc, s, n)
47*0Sstevel@tonic-gate 	wchar_t *pwc;
48*0Sstevel@tonic-gate 	char *s;
49*0Sstevel@tonic-gate 	int n;
50*0Sstevel@tonic-gate {
51*0Sstevel@tonic-gate 	static unsigned int CSselect = 0;
52*0Sstevel@tonic-gate 	static int CSlength = 1;
53*0Sstevel@tonic-gate 	wchar_t twchar = 0;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate 	/*
56*0Sstevel@tonic-gate 	 * If length is negative, return error
57*0Sstevel@tonic-gate 	 */
58*0Sstevel@tonic-gate 	if (n <= 0)
59*0Sstevel@tonic-gate 		return (-1);
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 	/*
62*0Sstevel@tonic-gate 	 * End of string ?
63*0Sstevel@tonic-gate 	 */
64*0Sstevel@tonic-gate 	if (*s == 0 && CSlength == 1)
65*0Sstevel@tonic-gate 		return (0);
66*0Sstevel@tonic-gate 	if (*s == 0 && *(s + 1) == 0 && CSlength == 2)
67*0Sstevel@tonic-gate 		return (0);
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate 	/*
70*0Sstevel@tonic-gate 	 * Get a character
71*0Sstevel@tonic-gate 	 */
72*0Sstevel@tonic-gate 	if ((unsigned char)*s == CS377) {
73*0Sstevel@tonic-gate 		/*
74*0Sstevel@tonic-gate 		 * Switching code set
75*0Sstevel@tonic-gate 		 */
76*0Sstevel@tonic-gate 		++s;
77*0Sstevel@tonic-gate 		/*
78*0Sstevel@tonic-gate 		 * Change characteristics
79*0Sstevel@tonic-gate 		 */
80*0Sstevel@tonic-gate 		 if ((unsigned char)*s == CS377) {
81*0Sstevel@tonic-gate 			++s;
82*0Sstevel@tonic-gate 			/*
83*0Sstevel@tonic-gate 			 * two byte sequence
84*0Sstevel@tonic-gate 			 */
85*0Sstevel@tonic-gate 			 if (*s++ != 0)
86*0Sstevel@tonic-gate 				return (-1);
87*0Sstevel@tonic-gate 			 CSselect = 0;
88*0Sstevel@tonic-gate 			 CSlength = 2;
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 		 }
91*0Sstevel@tonic-gate 		 else {
92*0Sstevel@tonic-gate 			/*
93*0Sstevel@tonic-gate 			 * Change CSselect
94*0Sstevel@tonic-gate 			 */
95*0Sstevel@tonic-gate 			 CSselect = (unsigned int)*s++;
96*0Sstevel@tonic-gate 			 CSlength = 1;
97*0Sstevel@tonic-gate 		}
98*0Sstevel@tonic-gate 	}
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	/*
101*0Sstevel@tonic-gate 	 * Get a character and return
102*0Sstevel@tonic-gate 	 */
103*0Sstevel@tonic-gate 	 if (CSlength == 1) {
104*0Sstevel@tonic-gate 		twchar = CSselect;
105*0Sstevel@tonic-gate 	 }
106*0Sstevel@tonic-gate 	 else {
107*0Sstevel@tonic-gate 		twchar = *s++;
108*0Sstevel@tonic-gate 	 }
109*0Sstevel@tonic-gate 	 twchar = twchar << 8;
110*0Sstevel@tonic-gate 	 twchar = twchar | *s;
111*0Sstevel@tonic-gate 	 if (pwc)
112*0Sstevel@tonic-gate 		 *pwc = twchar & MASK;
113*0Sstevel@tonic-gate 	 /*
114*0Sstevel@tonic-gate 	  * Encode additional information
115*0Sstevel@tonic-gate 	  */
116*0Sstevel@tonic-gate 	 if (CSlength == 2)
117*0Sstevel@tonic-gate 		if (pwc)
118*0Sstevel@tonic-gate 			*pwc |= TOP1;
119*0Sstevel@tonic-gate 	 return (CSlength);
120*0Sstevel@tonic-gate }
121*0Sstevel@tonic-gate 
122*0Sstevel@tonic-gate /*
123*0Sstevel@tonic-gate  * wctomb routines
124*0Sstevel@tonic-gate  */
125*0Sstevel@tonic-gate int
_wctomb_xccs(s,pwc)126*0Sstevel@tonic-gate _wctomb_xccs(s, pwc)
127*0Sstevel@tonic-gate 	char *s;
128*0Sstevel@tonic-gate 	wchar_t pwc;
129*0Sstevel@tonic-gate {
130*0Sstevel@tonic-gate 	unsigned char upper, lower;
131*0Sstevel@tonic-gate 	char *old = s;
132*0Sstevel@tonic-gate #ifdef DEBUG
133*0Sstevel@tonic-gate 	printf ("XCCS- xctomb\n");
134*0Sstevel@tonic-gate #endif
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (!s)
137*0Sstevel@tonic-gate 		return (0);
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	/*
140*0Sstevel@tonic-gate 	 * Get lower and upper anyway
141*0Sstevel@tonic-gate 	 */
142*0Sstevel@tonic-gate 	lower = pwc & 0x00ff;
143*0Sstevel@tonic-gate 	upper = (pwc >> 8) & 0x00ff;
144*0Sstevel@tonic-gate 	if (lower == CS377 || upper == CS377)
145*0Sstevel@tonic-gate 		return (-1);
146*0Sstevel@tonic-gate 	if (pwc & TOP1) {	/* length == 2 */
147*0Sstevel@tonic-gate 		/*
148*0Sstevel@tonic-gate 		 * This was the marker.
149*0Sstevel@tonic-gate 		 * Emitt 3 additional characters.
150*0Sstevel@tonic-gate 		 */
151*0Sstevel@tonic-gate 		*s++ = CS377;
152*0Sstevel@tonic-gate 		*s++ = CS377;
153*0Sstevel@tonic-gate 		*s++ = 0;
154*0Sstevel@tonic-gate 		*s++ = upper;
155*0Sstevel@tonic-gate 		*s++ = lower;
156*0Sstevel@tonic-gate 	}
157*0Sstevel@tonic-gate 	else {
158*0Sstevel@tonic-gate 		/*
159*0Sstevel@tonic-gate 		 * This was the marker.
160*0Sstevel@tonic-gate 		 * Emitt 2 additional characters.
161*0Sstevel@tonic-gate 		 */
162*0Sstevel@tonic-gate 		*s++ = CS377;
163*0Sstevel@tonic-gate 		*s++ = upper;
164*0Sstevel@tonic-gate 		*s++ = lower;
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 	return (s - old);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate /*
171*0Sstevel@tonic-gate  * mbstowcs routines
172*0Sstevel@tonic-gate  */
173*0Sstevel@tonic-gate size_t
_mbstowcs_xccs(pwc,s,n)174*0Sstevel@tonic-gate _mbstowcs_xccs(pwc, s, n)
175*0Sstevel@tonic-gate 	wchar_t *pwc;
176*0Sstevel@tonic-gate 	char *s;
177*0Sstevel@tonic-gate 	int n;
178*0Sstevel@tonic-gate {
179*0Sstevel@tonic-gate 	static unsigned int CSselect = 0;
180*0Sstevel@tonic-gate 	static int CSlength = 1;
181*0Sstevel@tonic-gate 	wchar_t twchar = 0;
182*0Sstevel@tonic-gate 	int cnt = 0;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 	/*
185*0Sstevel@tonic-gate 	 * If length is negative, return error
186*0Sstevel@tonic-gate 	 */
187*0Sstevel@tonic-gate 	if (n <= 0)
188*0Sstevel@tonic-gate 		return (-1);
189*0Sstevel@tonic-gate 
190*0Sstevel@tonic-gate 	/*
191*0Sstevel@tonic-gate 	 * End of string ?
192*0Sstevel@tonic-gate 	 */
193*0Sstevel@tonic-gate 	if (*s == 0 && CSlength == 1)
194*0Sstevel@tonic-gate 		return (0);
195*0Sstevel@tonic-gate 	if (*s == 0 && *(s + 1) == 0 && CSlength == 2)
196*0Sstevel@tonic-gate 		return (0);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate 	do {
199*0Sstevel@tonic-gate 		/*
200*0Sstevel@tonic-gate 		 * Check for an end of the string
201*0Sstevel@tonic-gate 		 */
202*0Sstevel@tonic-gate 		if (((*s == 0 && CSlength == 1)) ||
203*0Sstevel@tonic-gate 		   ((*s == 0 && *(s + 1) == 0 && CSlength == 2))) {
204*0Sstevel@tonic-gate 			*pwc = 0;
205*0Sstevel@tonic-gate 			++cnt;
206*0Sstevel@tonic-gate 			--n;
207*0Sstevel@tonic-gate 			break;
208*0Sstevel@tonic-gate 		}
209*0Sstevel@tonic-gate 		/*
210*0Sstevel@tonic-gate 		 * Get a character
211*0Sstevel@tonic-gate 		 */
212*0Sstevel@tonic-gate 		if ((unsigned char)*s == CS377) {
213*0Sstevel@tonic-gate 			++s;
214*0Sstevel@tonic-gate 			/*
215*0Sstevel@tonic-gate 			 * Change characterristics
216*0Sstevel@tonic-gate 			 */
217*0Sstevel@tonic-gate 			 if ((unsigned char)*s == CS377) {
218*0Sstevel@tonic-gate 				++s;
219*0Sstevel@tonic-gate 				/*
220*0Sstevel@tonic-gate 				 * two byte sequence
221*0Sstevel@tonic-gate 				 */
222*0Sstevel@tonic-gate 				 if (*s++ != 0)
223*0Sstevel@tonic-gate 					return (-1);
224*0Sstevel@tonic-gate 				 CSselect = 0;
225*0Sstevel@tonic-gate 				 CSlength = 2;
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 			 }
228*0Sstevel@tonic-gate 			 else {
229*0Sstevel@tonic-gate 				/*
230*0Sstevel@tonic-gate 				 * Change CSselect
231*0Sstevel@tonic-gate 				 */
232*0Sstevel@tonic-gate 				 CSselect = (unsigned int)*s++;
233*0Sstevel@tonic-gate 				 CSlength = 1;
234*0Sstevel@tonic-gate 			}
235*0Sstevel@tonic-gate 		}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 		/*
238*0Sstevel@tonic-gate 		 * Get a character and return
239*0Sstevel@tonic-gate 		 */
240*0Sstevel@tonic-gate 		 if (CSlength == 1) {
241*0Sstevel@tonic-gate 			twchar = CSselect;
242*0Sstevel@tonic-gate 		 }
243*0Sstevel@tonic-gate 		 else {
244*0Sstevel@tonic-gate 			twchar = *s++;
245*0Sstevel@tonic-gate 		 }
246*0Sstevel@tonic-gate 		 twchar = twchar << 8;
247*0Sstevel@tonic-gate 		 twchar = twchar | *s++;
248*0Sstevel@tonic-gate 		 *pwc = twchar & MASK;
249*0Sstevel@tonic-gate 		 if (CSlength == 2)
250*0Sstevel@tonic-gate 			*pwc |= TOP1;
251*0Sstevel@tonic-gate 		 ++pwc;
252*0Sstevel@tonic-gate 		 ++cnt;
253*0Sstevel@tonic-gate 		 --n;
254*0Sstevel@tonic-gate 	 } while (n >= 0);
255*0Sstevel@tonic-gate 	 return (cnt);
256*0Sstevel@tonic-gate }
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate /*
260*0Sstevel@tonic-gate  * wcstombs routines
261*0Sstevel@tonic-gate  */
262*0Sstevel@tonic-gate size_t
_wcstombs_xccs(s,pwc,n)263*0Sstevel@tonic-gate _wcstombs_xccs(s, pwc, n)
264*0Sstevel@tonic-gate 	char *s;
265*0Sstevel@tonic-gate 	wchar_t *pwc;
266*0Sstevel@tonic-gate 	int n;
267*0Sstevel@tonic-gate {
268*0Sstevel@tonic-gate 	int cnt = 0;
269*0Sstevel@tonic-gate 	unsigned char lower, upper;
270*0Sstevel@tonic-gate 	int in_2byte = 0;
271*0Sstevel@tonic-gate 	int in_1byte = 0;
272*0Sstevel@tonic-gate 	int current = 0;
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	if (n <= 0)
275*0Sstevel@tonic-gate 		return (-1);
276*0Sstevel@tonic-gate 
277*0Sstevel@tonic-gate 	if (*pwc == 0)
278*0Sstevel@tonic-gate 		return (0);
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	do {
281*0Sstevel@tonic-gate 		lower = *pwc & 0x00ff;
282*0Sstevel@tonic-gate 		upper = (*pwc >> 8) & 0x00ff;
283*0Sstevel@tonic-gate 		/*
284*0Sstevel@tonic-gate 		 * End of string ?
285*0Sstevel@tonic-gate 		 */
286*0Sstevel@tonic-gate 		if (lower == 0) {
287*0Sstevel@tonic-gate 			*s++ = 0;
288*0Sstevel@tonic-gate 			++cnt;
289*0Sstevel@tonic-gate 			--n;
290*0Sstevel@tonic-gate 			if (n == 0)
291*0Sstevel@tonic-gate 				break;
292*0Sstevel@tonic-gate 			*s++ = 0;
293*0Sstevel@tonic-gate 			++cnt;
294*0Sstevel@tonic-gate 			break;
295*0Sstevel@tonic-gate 		}
296*0Sstevel@tonic-gate 		if (lower == CS377 || upper == CS377)
297*0Sstevel@tonic-gate 			return (-1);
298*0Sstevel@tonic-gate 		if (*pwc & TOP1) {	/* length == 2 */
299*0Sstevel@tonic-gate 			if (in_2byte == 0) {
300*0Sstevel@tonic-gate 				/*
301*0Sstevel@tonic-gate 				 * This was the marker.
302*0Sstevel@tonic-gate 				 * Emitt 3 additional characters.
303*0Sstevel@tonic-gate 				 */
304*0Sstevel@tonic-gate 				*s++ = CS377; ++cnt; --n;
305*0Sstevel@tonic-gate 				*s++ = CS377; ++cnt; --n;
306*0Sstevel@tonic-gate 				*s++ = 0; ++cnt; --n;
307*0Sstevel@tonic-gate 				in_2byte = 1;
308*0Sstevel@tonic-gate 				in_1byte = 0;
309*0Sstevel@tonic-gate 			}
310*0Sstevel@tonic-gate 			*s++ = upper; ++cnt; --n;
311*0Sstevel@tonic-gate 			if (n == 0)
312*0Sstevel@tonic-gate 				break;
313*0Sstevel@tonic-gate 			*s++ = lower; ++cnt; --n;
314*0Sstevel@tonic-gate 			if (n == 0)
315*0Sstevel@tonic-gate 				break;
316*0Sstevel@tonic-gate 		}
317*0Sstevel@tonic-gate 		else {
318*0Sstevel@tonic-gate 			if ((in_1byte == 0 && in_2byte == 1) ||
319*0Sstevel@tonic-gate 			    (in_1byte == 1 && upper != current) ||
320*0Sstevel@tonic-gate 			    (in_1byte == 0 && in_2byte == 0 && upper != 0)) {
321*0Sstevel@tonic-gate 				/*
322*0Sstevel@tonic-gate 				 * This was the marker.
323*0Sstevel@tonic-gate 				 * Emitt 2 additional characters.
324*0Sstevel@tonic-gate 				 */
325*0Sstevel@tonic-gate 				*s++ = CS377; ++cnt; --n;
326*0Sstevel@tonic-gate 				if (n == 0)
327*0Sstevel@tonic-gate 					break;
328*0Sstevel@tonic-gate 				*s++ = upper; ++cnt; --n;
329*0Sstevel@tonic-gate 				if (n == 0)
330*0Sstevel@tonic-gate 					break;
331*0Sstevel@tonic-gate 				in_2byte = 0;
332*0Sstevel@tonic-gate 				in_1byte = 1;
333*0Sstevel@tonic-gate 				current = upper;
334*0Sstevel@tonic-gate 			}
335*0Sstevel@tonic-gate 			*s++ = lower; ++cnt; --n;
336*0Sstevel@tonic-gate 			if (n == 0)
337*0Sstevel@tonic-gate 				break;
338*0Sstevel@tonic-gate 		}
339*0Sstevel@tonic-gate 		++pwc;
340*0Sstevel@tonic-gate 	} while (n >= 0);
341*0Sstevel@tonic-gate 	return (cnt);
342*0Sstevel@tonic-gate }
343