about summary refs log tree commit diff
path: root/wcsmbs/mbrtowc.c
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1996-07-09 23:17:59 +0000
committerRoland McGrath <roland@gnu.org>1996-07-09 23:17:59 +0000
commit07a4742f9ec2b0587c0d488bb65da2a6faa50fed (patch)
tree2b10e82cad4053bcbe27447bc3d444f6d2aef5e7 /wcsmbs/mbrtowc.c
parentc150923988933b5db75a974d4cc08cd7f7aaf3dc (diff)
downloadglibc-07a4742f9ec2b0587c0d488bb65da2a6faa50fed.tar.gz
glibc-07a4742f9ec2b0587c0d488bb65da2a6faa50fed.tar.xz
glibc-07a4742f9ec2b0587c0d488bb65da2a6faa50fed.zip
* posix/glob.h (__glob_opendir_hook, __glob_readdir_hook, cvs/libc-960710
	__glob_closedir_hook): Remove decls.

	* sysdeps/generic/machine-gmon.h: Declare mcount_internal.

	* sysdeps/unix/inet/syscalls.list: Define __ names with weak aliases
 	for send and connect syscalls.

	* socket/sys/socket.h: New file, taken from non-sysdep parts of
	linux/sys/socket.h; break sysdeps parts out into socketbits.h.
	Declare __ names for send and connect.
	* sysdeps/generic/socketbits.h: New file.
	* sysdeps/unix/sysv/linux/socketbits.h: New file.
	* sysdeps/unix/sysv/linux/sys/socket.h: File removed.
	* sysdeps/generic/sys/socket.h: File removed.

	* sysdeps/mach/hurd/connect.c: Define __ name and weak alias.
	* sysdeps/mach/hurd/send.c: Likewise.
Diffstat (limited to 'wcsmbs/mbrtowc.c')
-rw-r--r--wcsmbs/mbrtowc.c111
1 files changed, 88 insertions, 23 deletions
diff --git a/wcsmbs/mbrtowc.c b/wcsmbs/mbrtowc.c
index 2c4b0779da..9e70a0b2c9 100644
--- a/wcsmbs/mbrtowc.c
+++ b/wcsmbs/mbrtowc.c
@@ -1,6 +1,6 @@
 /* Copyright (C) 1996 Free Software Foundation, Inc.
 This file is part of the GNU C Library.
-Contributed by Ulrich Drepper, <drepper@gnu.ai.mit.edu>
+Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
 
 The GNU C Library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Library General Public License as
@@ -17,50 +17,115 @@ License along with the GNU C Library; see the file COPYING.LIB.  If
 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  */
 
+#include <errno.h>
 #include <wchar.h>
 
+#ifndef EILSEQ
+#define EILSEQ EINVAL
+#endif
+
 
 static mbstate_t internal;
 
 size_t
-mbrtowc (pwc, s, n, ps)
-     wchar_t *pwc;
-     const char *s;
-     size_t n;
-     mbstate_t *ps;
+mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
 {
   wchar_t to_wide;
+  size_t used = 0;
 
   if (ps == NULL)
     ps = &internal;
 
-  /*************************************************************\
-  |* This is no complete implementation.  While the multi-byte *|
-  |* character handling is not finished this will do.	       *|
-  \*************************************************************/
-
   if (s == NULL)
     {
+      /* See first paragraph of description in 7.16.6.3.2.  */
       pwc = NULL;
       s = "";
       n = 1;
     }
 
-  if (n == 0)
-    return (size_t) -2;
+  if (n > 0)
+    {
+      if (ps->count == 0)
+	{
+	  unsigned char byte = (unsigned char) *s++;
+	  ++used;
 
-  /* For now.  */
-  to_wide = (wchar_t) *s;
+	  /* We must look for a possible first byte of a UTF8 sequence.  */
+	  if (byte < 0x80)
+	    {
+	      /* One byte sequence.  */
+	      if (pwc != NULL)
+		*pwc = (wchar_t) byte;
+	      return byte ? used : 0;
+	    }
 
-  if (pwc != NULL)
-    *pwc = to_wide;
+	  if ((byte & 0xc0) == 0x80 || (byte & 0xfe) == 0xfe)
+	    {
+	      /* Oh, oh.  An encoding error.  */
+	      errno = EILSEQ;
+	      return (size_t) -1;
+	    }
 
-  if (pwc == L'\0')
-    {
-      *ps = 0;		/* This is required.  */
-      return 0;
+	  if ((byte & 0xe0) == 0xc0)
+	    {
+	      /* We expect two bytes.  */
+	      ps->count = 1;
+	      ps->value = byte & 0x1f;
+	    }
+	  else if ((byte & 0xf0) == 0xe0)
+	    {
+	      /* We expect three bytes.  */
+	      ps->count = 2;
+	      ps->value = byte & 0x0f;
+	    }
+	  else if ((byte & 0xf8) == 0xf0)
+	    {
+	      /* We expect four bytes.  */
+	      ps->count = 3;
+	      ps->value = byte & 0x07;
+	    }
+	  else if ((byte & 0xfc) == 0xf8)
+	    {
+	      /* We expect five bytes.  */
+	      ps->count = 4;
+	      ps->value = byte & 0x03;
+	    }
+	  else
+	    {
+	      /* We expect six bytes.  */
+	      ps->count = 5;
+	      ps->value = byte & 0x01;
+	    }
+	}
+
+      /* We know we have to handle a multibyte character and there are
+	 some more bytes to read.  */
+      while (used < n)
+	{
+	  /* The second to sixths byte must be of the form 10xxxxxx.  */
+	  unsigned char byte = (unsigned char) *s++;
+	  ++used;
+
+	  if ((byte & 0xc0) != 0x80)
+	    {
+	      /* Oh, oh.  An encoding error.  */
+	      errno = EILSEQ;
+	      return (size_t) -1;
+	    }
+
+	  ps->value <<= 6;
+	  ps->value |= byte & 0x3f;
+
+	  if (--ps->count == 0)
+	    {
+	      /* The character is finished.  */
+	      if (pwc != NULL)
+		*pwc = (wchar_t) ps->value;
+	      return ps->value ? used : 0;
+	    }
+	}
     }
 
-  /* Return code (size_t)-1 cannot happend for now.  */
-  return 1;
+  return (size_t) -2;
 }