/*
 * |-----------------------------------------------------------|
 * | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
 * | All Rights Reserved                                       |
 * |-----------------------------------------------------------|
 * |          Restricted Rights Legend                         |
 * | Use, duplication, or disclosure by the Government is      |
 * | subject to restrictions as set forth in                   |
 * | subparagraph (c)(1)(ii) of the Rights in Technical        |
 * | Data and Computer Software Clause of DFARS 252.227-7013.  |
 * |         MIPS Computer Systems, Inc.                       |
 * |         950 DeGuigne Avenue                               |
 * |         Sunnyvale, California 94088-3650, USA             |
 * |-----------------------------------------------------------|
 */
#ident	"$Header: mbtowc.c,v 1.3.2.1 92/06/24 14:01:09 marker Exp $"

/*	Copyright (c) 1984, 1986, 1987, 1988 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/


#include <ctype.h>
#include <stdlib.h>
#include <sys/types.h>
#define SS2     0x8e
#define SS3     0x8f
#define EUCMASK 0x30000000
#define P00     0x00000000      /* Code Set 0 */
#define P11     0x30000000      /* Code Set 1 */
#define P01     0x10000000      /* Code Set 2 */
#define P10     0x20000000      /* Code Set 3 */
#define multibyte       (_ctype[520]>1)
#define eucw1   _ctype[514]
#define eucw2   _ctype[515]
#define eucw3   _ctype[516]
/* start for MNLS 3.2 by AT&T UP  Nov.16,1988 */
/*
 *	The followings are for 2 bytes of multi-byte code definition.
 */
#define H_EUCMASK 0x8080	/* mask bit for 16 bits */
#define H_P00     0x0000	/* Code Set 0 for 2 bytes code */
#define H_P11     0x8080	/* Code Set 1 for 2 bytes code */
#define H_P01     0x0080	/* Code Set 2 for 2 bytes code */
#define H_P10     0x8000	/* Code Set 3 for 2 bytes code */
/* end for MNLS 3.2 */


int mbtowc(wchar, s, n)
wchar_t *wchar;
char *s;
size_t n;
{
        register int length;
        register wchar_t intcode;
        register c;
        char *olds = (char *)s;
        wchar_t mask;
/* start for MNLS 3.2 by AT&T UP  Nov.16,1988 */
	short	pcw;	/* process code width */
/* end for MNLS 3.2 */
        
        if(n == 0)
                return(-1);
        if(s == (char *)0)
                return 0;
        c = (unsigned char)*s++;
        if(!multibyte || c < 0200) {
                if(wchar)
                        *wchar = c;
                return(c ? 1 : 0);
        }
/* start for MNLS 3.2 by AT&T UP  Nov.16,1988 */
	if ( eucw1 > sizeof(unsigned short) ||
	     eucw2 > sizeof(unsigned short) ||
	     eucw3 > sizeof(unsigned short))
		pcw = sizeof(unsigned long);	/* 32 bits process code */
	else
		pcw = sizeof(unsigned short);	/* 16 bits process code */
/* end for MNLS 3.2 */
        intcode = 0;
        if (c == SS2) {
                if(!(length = eucw2)) 
                        goto lab1;
/* start for MNLS 3.2 by AT&T UP  Nov.16,1988 */
/*              mask = P01; */
		if ( pcw > sizeof(unsigned short))
                	mask = P01;
		else
			mask = H_P01;
/* end for MNLS 3.2 */
                goto lab2;
        } else if(c == SS3) {
                if(!(length = eucw3)) 
                        goto lab1;
/* start for MNLS 3.2 by AT&T UP  Nov.16,1988 */
/*              mask = P10; */
		if ( pcw > sizeof(unsigned short))
                	mask = P10;
		else
			mask = H_P10;
/* end for MNLS 3.2 */
                goto lab2;
        } 
lab1:
        if(iscntrl(c)) {
                if(wchar)
                        *wchar = c;
                return(1);
        }
        length = eucw1 - 1;
/* start for MNLS 3.2 by AT&T UP  Nov.16,1988 */
/*      mask = P11; */
	if ( pcw > sizeof(unsigned short))
               	mask = P11;
	else
		mask = H_P11;
/* end for MNLS 3.2 */
        intcode = c & 0177;
lab2:
        if(length + 1 > n || length < 0)
                return(-1);
        while(length--) {
                if((c = (unsigned char)*s++) < 0200 || iscntrl(c))
                        return(-1);
/* start for MNLS 3.2 by AT&T UP  Nov.16,1988 */
/*              intcode = (intcode << 7) | (c & 0177); */
		if ( pcw > sizeof(unsigned short))
	                intcode = (intcode << 7) | (c & 0177);
		else
                	intcode = (intcode << 8) | (c & 0177);
/* end for MNLS 3.2 */
        }
        if(wchar)
                *wchar = intcode | mask;
        return((char *)s - olds);
}       

#undef mblen

int
mblen(s, n)
const char *s;
size_t n;
{
	return(mbtowc((wchar_t *)0, s, n));
}
