/*
 * |-----------------------------------------------------------|
 * | 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: mbstowcs.c,v 1.2.2.1 92/06/24 14:01:08 marker Exp $"

/*	Copyright (c) 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.	*/


/*
 * mbstowcs.c
 * 30/6/88
 * UEL
 */

#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]

int mbstowcs(wchar, s, n)
wchar_t     *wchar;
char               *s;
size_t                 n;
{
    register int length;
    register wchar_t intcode;
    register c;
    wchar_t mask;
    int size;
        
        if(n <= 0)
                return(-1);
        if(s == (char *)0)
                return 0;

        size = 0;
        while (n-- > 0) {
                c = (unsigned char)*s++;
                intcode = 0;
                length = 0;
                mask = 0;
                if (!multibyte || c < 0200) 
                        intcode = c; 
                else
                if (c == SS2) {
                        if((length = eucw2)) 
                                mask = P01;
                } else
                if (c == SS3) {
                        if((length = eucw3)) 
                                mask = P10;
                } else
                if (iscntrl(c)) 
                        intcode = c;
                else {
                        length = eucw1-1;
                        mask = P11;
                        intcode = c & 0177;
                }
                size++;
                if(length) {
                        if ( (n-length-1) < 0) {
                                *wchar = 0;
                                return size;
                        }
                        while(length--) {
                                if((c=(unsigned char)*s++) < 0200 || iscntrl(c))
                                                        return(-1);
                                intcode = (intcode << 7) | (c & 0177);
                        }
                } 
                *wchar++ = intcode | mask;
                if (intcode == 0) {
                        size--;
                        return size;
                }
        }       
        return size;
}
