xref: /titanic_41/usr/src/lib/libbc/libc/gen/common/euc.multibyte.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include "codeset.h"
317c478bd9Sstevel@tonic-gate #include "mbextern.h"
327c478bd9Sstevel@tonic-gate #include "euc.h"
337c478bd9Sstevel@tonic-gate #include <limits.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #define EUCMASK	0x8080		/* All id bits */
367c478bd9Sstevel@tonic-gate #define MASK0	0x0000		/* Code Set 0 */
377c478bd9Sstevel@tonic-gate #define MASK1	0x8080		/* Code Set 1 */
387c478bd9Sstevel@tonic-gate #define MASK2	0x0080		/* Code Set 2 */
397c478bd9Sstevel@tonic-gate #define MASK3	0x8000		/* Code Set 3 */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #define EUCWID1	eucinfo->_eucw1
427c478bd9Sstevel@tonic-gate #define EUCWID2	eucinfo->_eucw2
437c478bd9Sstevel@tonic-gate #define EUCWID3	eucinfo->_eucw3
447c478bd9Sstevel@tonic-gate 
45*5d54f3d8Smuffin int	_wctomb_euc(char *, wchar_t);
46*5d54f3d8Smuffin 
47*5d54f3d8Smuffin int
_mbtowc_euc(wchar_t * wchar,char * s,size_t n)48*5d54f3d8Smuffin _mbtowc_euc(wchar_t *wchar, char *s, size_t n)
497c478bd9Sstevel@tonic-gate {
50*5d54f3d8Smuffin 	int length;
51*5d54f3d8Smuffin 	wchar_t intcode;
52*5d54f3d8Smuffin 	int c;
537c478bd9Sstevel@tonic-gate 	char *olds = (char *)s;
547c478bd9Sstevel@tonic-gate 	wchar_t mask;
557c478bd9Sstevel@tonic-gate         eucwidth_t * eucinfo = (eucwidth_t *)_code_set_info.code_info;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	if(n <= 0)
587c478bd9Sstevel@tonic-gate 		return(-1);
597c478bd9Sstevel@tonic-gate 	if(s == (char *)0)
60*5d54f3d8Smuffin 		return (0);
617c478bd9Sstevel@tonic-gate 	c = (unsigned char)*s++;
627c478bd9Sstevel@tonic-gate 	if(c < 0200) {
637c478bd9Sstevel@tonic-gate 		if(wchar)
647c478bd9Sstevel@tonic-gate 			*wchar = c;
657c478bd9Sstevel@tonic-gate 		return (c ? 1 : 0);
667c478bd9Sstevel@tonic-gate 	}
677c478bd9Sstevel@tonic-gate 	intcode = 0;
687c478bd9Sstevel@tonic-gate 	if (c == SS2) {
697c478bd9Sstevel@tonic-gate 		if(!(length = EUCWID2)) {
707c478bd9Sstevel@tonic-gate 			if(wchar)
717c478bd9Sstevel@tonic-gate 				*wchar = c;
727c478bd9Sstevel@tonic-gate 			return (1);
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 		mask = MASK2;
757c478bd9Sstevel@tonic-gate 	} else if(c == SS3) {
767c478bd9Sstevel@tonic-gate 		if(!(length = EUCWID3)) {
777c478bd9Sstevel@tonic-gate 			if(wchar)
787c478bd9Sstevel@tonic-gate 				*wchar = c;
797c478bd9Sstevel@tonic-gate 			return (1);
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 		mask = MASK3;
827c478bd9Sstevel@tonic-gate 	} else {
837c478bd9Sstevel@tonic-gate 		if(iscntrl(c)) {
847c478bd9Sstevel@tonic-gate 			if(wchar)
857c478bd9Sstevel@tonic-gate 				*wchar = c;
867c478bd9Sstevel@tonic-gate 			return (1);
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		length = EUCWID1 - 1;
897c478bd9Sstevel@tonic-gate 		mask = MASK1;
907c478bd9Sstevel@tonic-gate 		intcode = c & 0177;
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 	if(length + 1 > n)
937c478bd9Sstevel@tonic-gate 		return (-1);
947c478bd9Sstevel@tonic-gate 	while(length--) {
957c478bd9Sstevel@tonic-gate 		if((c = (unsigned char)*s++) < 0200 || iscntrl(c))
967c478bd9Sstevel@tonic-gate 			return (-1);
977c478bd9Sstevel@tonic-gate 		intcode = (intcode << 8) | (c & 0177);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	if(wchar)
1007c478bd9Sstevel@tonic-gate 		*wchar = intcode | mask;
1017c478bd9Sstevel@tonic-gate 	return ((char *)s - olds);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate size_t
_mbstowcs_euc(wchar_t * pwcs,char * s,size_t n)106*5d54f3d8Smuffin _mbstowcs_euc(wchar_t *pwcs, char *s, size_t n)
1077c478bd9Sstevel@tonic-gate {
108*5d54f3d8Smuffin 	int		i, j;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	j=0;
1117c478bd9Sstevel@tonic-gate 	while(*s) {
1127c478bd9Sstevel@tonic-gate 		if(j>=n)
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		i=_mbtowc_euc(pwcs+j, s, MB_LEN_MAX);
1157c478bd9Sstevel@tonic-gate 		if(i==-1)
116*5d54f3d8Smuffin 			return (-1);
1177c478bd9Sstevel@tonic-gate 		s+=i;
1187c478bd9Sstevel@tonic-gate 		++j;
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 	if(j<n)
1217c478bd9Sstevel@tonic-gate 		pwcs[j]=0;
122*5d54f3d8Smuffin 	return (j);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate size_t
_wcstombs_euc(char * s,wchar_t * pwcs,size_t n)127*5d54f3d8Smuffin _wcstombs_euc(char *s, wchar_t *pwcs, size_t n)
1287c478bd9Sstevel@tonic-gate {
129*5d54f3d8Smuffin 	wchar_t	wc;
130*5d54f3d8Smuffin 	int		i;
131*5d54f3d8Smuffin 	int		r=n; /* Rest of bytes. */
132*5d54f3d8Smuffin 	char		*t;
1337c478bd9Sstevel@tonic-gate 	char			mbbuf[MB_LEN_MAX+1];
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	while(wc=(*pwcs++)) {
1367c478bd9Sstevel@tonic-gate 		i=_wctomb_euc(mbbuf, wc);
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		if (i>r)
1397c478bd9Sstevel@tonic-gate 			break;
140*5d54f3d8Smuffin 		if (i==-1) return (-1);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		r-=i;
1437c478bd9Sstevel@tonic-gate 		for (t=mbbuf;i>0;--i){
1447c478bd9Sstevel@tonic-gate 			/* Copy each byte. */
1457c478bd9Sstevel@tonic-gate 			*(s++)=*(t++);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	if (r>0)
1497c478bd9Sstevel@tonic-gate 		/* Has enough room for NUL. */
1507c478bd9Sstevel@tonic-gate 		*s=0;
151*5d54f3d8Smuffin 	return (n-r);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
154*5d54f3d8Smuffin int
_wctomb_euc(char * s,wchar_t wchar)155*5d54f3d8Smuffin _wctomb_euc(char *s, wchar_t wchar)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate         eucwidth_t * eucinfo = (eucwidth_t *)_code_set_info.code_info;
1587c478bd9Sstevel@tonic-gate 	char *olds = s;
159*5d54f3d8Smuffin 	int size, index;
1607c478bd9Sstevel@tonic-gate 	unsigned char d;
1617c478bd9Sstevel@tonic-gate 	if(!s)
1627c478bd9Sstevel@tonic-gate 		return(0);
1637c478bd9Sstevel@tonic-gate 	if( wchar <= 0177 || wchar <= 0377 && iscntrl(wchar)) {
1647c478bd9Sstevel@tonic-gate 		*s++ = wchar;
1657c478bd9Sstevel@tonic-gate 		return (wchar ? 1 : 0);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 	switch(wchar & EUCMASK) {
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		case MASK1:
1707c478bd9Sstevel@tonic-gate 			size = EUCWID1;
1717c478bd9Sstevel@tonic-gate 			break;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		case MASK2:
1747c478bd9Sstevel@tonic-gate 			*s++ = SS2;
1757c478bd9Sstevel@tonic-gate 			size = EUCWID2;
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		case MASK3:
1797c478bd9Sstevel@tonic-gate 			*s++ = SS3;
1807c478bd9Sstevel@tonic-gate 			size = EUCWID3;
1817c478bd9Sstevel@tonic-gate 			break;
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 		default:
1847c478bd9Sstevel@tonic-gate 			return (-1);
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	index = size;
1877c478bd9Sstevel@tonic-gate 	while(index--) {
1887c478bd9Sstevel@tonic-gate 		d = wchar | 0200;
1897c478bd9Sstevel@tonic-gate 		wchar >>= 8;
1907c478bd9Sstevel@tonic-gate 		if(iscntrl(d))
1917c478bd9Sstevel@tonic-gate 			return (-1);
1927c478bd9Sstevel@tonic-gate 		s[index] = d;
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 	return (s + size - olds);
1957c478bd9Sstevel@tonic-gate }
196