about summary refs log tree commit diff
path: root/wcsmbs
diff options
context:
space:
mode:
Diffstat (limited to 'wcsmbs')
-rw-r--r--wcsmbs/mbrtowc.c7
-rw-r--r--wcsmbs/mbsnrtowcs.c112
-rw-r--r--wcsmbs/mbsrtowcs.c7
-rw-r--r--wcsmbs/wcrtomb.c9
-rw-r--r--wcsmbs/wcsnrtombs.c7
-rw-r--r--wcsmbs/wcsrtombs.c11
-rw-r--r--wcsmbs/wmemrtombs.c7
-rw-r--r--wcsmbs/wmemrtowcs.c111
8 files changed, 172 insertions, 99 deletions
diff --git a/wcsmbs/mbrtowc.c b/wcsmbs/mbrtowc.c
index 8b4dbe2912..cf0bbd6ce2 100644
--- a/wcsmbs/mbrtowc.c
+++ b/wcsmbs/mbrtowc.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
 
@@ -38,9 +38,8 @@ __mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
   if (s == NULL)
     {
       /* See first paragraph of description in 7.16.6.3.2.  */
-      pwc = NULL;
-      s = "";
-      n = 1;
+      ps->count = 0;
+      return 0;
     }
 
   if (n > 0)
diff --git a/wcsmbs/mbsnrtowcs.c b/wcsmbs/mbsnrtowcs.c
index bb79a30ed2..db67d5c1bb 100644
--- a/wcsmbs/mbsnrtowcs.c
+++ b/wcsmbs/mbsnrtowcs.c
@@ -43,10 +43,16 @@ __mbsnrtowcs (dst, src, nmc, len, ps)
   size_t written = 0;
   const char *run = *src;
   const char *last = run + nmc;
+  wchar_t value;
+  size_t count;
 
   if (ps == NULL)
     ps = &internal;
 
+  /* Get information from last use of this state.  */
+  count = ps->count;
+  value = ps->value;
+
   if (dst == NULL)
     /* The LEN parameter has to be ignored if we don't actually write
        anything.  */
@@ -55,57 +61,66 @@ __mbsnrtowcs (dst, src, nmc, len, ps)
   /* Copy all words.  */
   while (written < len && run < last)
     {
-      wchar_t value;
-      size_t count;
-      unsigned char byte = *run++;
+      unsigned char byte;
 
-      /* We expect a start of a new multibyte character.  */
-      if (byte < 0x80)
-	{
-	  /* One byte sequence.  */
-	  count = 0;
-	  value = byte;
-	}
-      else if ((byte & 0xe0) == 0xc0)
-	{
-	  count = 1;
-	  value = byte & 0x1f;
-	}
-      else if ((byte & 0xf0) == 0xe0)
-	{
-	  /* We expect three bytes.  */
-	  count = 2;
-	  value = byte & 0x0f;
-	}
-      else if ((byte & 0xf8) == 0xf0)
-	{
-	  /* We expect four bytes.  */
-	  count = 3;
-	  value = byte & 0x07;
-	}
-      else if ((byte & 0xfc) == 0xf8)
-	{
-	  /* We expect five bytes.  */
-	  count = 4;
-	  value = byte & 0x03;
-	}
-      else if ((byte & 0xfe) == 0xfc)
-	{
-	  /* We expect six bytes.  */
-	  count = 5;
-	  value = byte & 0x01;
-	}
-      else
+      /* Store address of next byte to process.  */
+      *src = run;
+
+      /* Start reading a new character only if we are in the initial
+	 state.  */
+      if (count == 0)
 	{
-	  /* This is an illegal encoding.  */
-	  __set_errno (EILSEQ);
-	  return (size_t) -1;
+	  byte = *run++;
+
+	  /* We expect a start of a new multibyte character.  */
+	  if (byte < 0x80)
+	    {
+	      /* One byte sequence.  */
+	      count = 0;
+	      value = byte;
+	    }
+	  else if ((byte & 0xe0) == 0xc0)
+	    {
+	      count = 1;
+	      value = byte & 0x1f;
+	    }
+	  else if ((byte & 0xf0) == 0xe0)
+	    {
+	      /* We expect three bytes.  */
+	      count = 2;
+	      value = byte & 0x0f;
+	    }
+	  else if ((byte & 0xf8) == 0xf0)
+	    {
+	      /* We expect four bytes.  */
+	      count = 3;
+	      value = byte & 0x07;
+	    }
+	  else if ((byte & 0xfc) == 0xf8)
+	    {
+	      /* We expect five bytes.  */
+	      count = 4;
+	      value = byte & 0x03;
+	    }
+	  else if ((byte & 0xfe) == 0xfc)
+	    {
+	      /* We expect six bytes.  */
+	      count = 5;
+	      value = byte & 0x01;
+	    }
+	  else
+	    {
+	      /* This is an illegal encoding.  */
+	      __set_errno (EILSEQ);
+	      return (size_t) -1;
+	    }
 	}
 
       /* Read the possible remaining bytes.  */
-      while (count-- > 0)
+      while (run < last && count > 0)
 	{
 	  byte = *run++;
+	  --count;
 
 	  if ((byte & 0xc0) != 0x80)
 	    {
@@ -118,6 +133,14 @@ __mbsnrtowcs (dst, src, nmc, len, ps)
 	  value |= byte & 0x3f;
 	}
 
+      /* If this character is only partially available remember this.  */
+      if (run == last && count != 0)
+	{
+	  ps->count = count;
+	  ps->value = value;
+	  break;
+	}
+
       /* Store value is required.  */
       if (dst != NULL)
 	*dst++ = value;
@@ -128,6 +151,7 @@ __mbsnrtowcs (dst, src, nmc, len, ps)
 	{
 	  /* Found the end of the string.  */
 	  *src = NULL;
+	  ps->count = 0;
 	  return written;
 	}
 
diff --git a/wcsmbs/mbsrtowcs.c b/wcsmbs/mbsrtowcs.c
index 7ae30b4870..84d4cbf66d 100644
--- a/wcsmbs/mbsrtowcs.c
+++ b/wcsmbs/mbsrtowcs.c
@@ -52,7 +52,12 @@ __mbsrtowcs (dst, src, len, ps)
     {
       wchar_t value;
       size_t count;
-      unsigned char byte = *run++;
+      unsigned char byte;
+
+      /* Store address of next byte to process.  */
+      *src = run;
+
+      byte = *run++;
 
       /* We expect a start of a new multibyte character.  */
       if (byte < 0x80)
diff --git a/wcsmbs/wcrtomb.c b/wcsmbs/wcrtomb.c
index c5b887dce9..69c67705c3 100644
--- a/wcsmbs/wcrtomb.c
+++ b/wcsmbs/wcrtomb.c
@@ -1,4 +1,4 @@
-/* Copyright (C) 1996 Free Software Foundation, Inc.
+/* Copyright (C) 1996, 1997 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
 
@@ -48,8 +48,10 @@ __wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
 
   if (s == NULL)
     {
-      s = fake;
-      wc = L'\0';
+      /* This is equivalent to wcrtomb (<<internal>, L'\0', ps).  We
+	 only have to reset the state.  */
+      ps->count = 0;
+      return 1;
     }
 
   /* Store the UTF8 representation of WC.  */
@@ -65,6 +67,7 @@ __wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
       /* It's a one byte sequence.  */
       if (s != NULL)
 	*s = (char) wc;
+      ps->count = 0;
       return 1;
     }
 
diff --git a/wcsmbs/wcsnrtombs.c b/wcsmbs/wcsnrtombs.c
index ddd4e95057..f6c8048295 100644
--- a/wcsmbs/wcsnrtombs.c
+++ b/wcsmbs/wcsnrtombs.c
@@ -63,7 +63,12 @@ __wcsnrtombs (dst, src, nwc, len, ps)
 
   while (written < len && nwc-- > 0)
     {
-      wchar_t wc = *run++;
+      wchar_t wc;
+
+      /* Store position of first unprocessed word.  */
+      *src = run;
+
+      wc = *run++;
 
       if (wc < 0 || wc > 0x7fffffff)
 	{
diff --git a/wcsmbs/wcsrtombs.c b/wcsmbs/wcsrtombs.c
index b2c0c73778..cc21a51eb3 100644
--- a/wcsmbs/wcsrtombs.c
+++ b/wcsmbs/wcsrtombs.c
@@ -59,7 +59,12 @@ __wcsrtombs (dst, src, len, ps)
 
   while (written < len)
     {
-      wchar_t wc = *run++;
+      wchar_t wc;
+
+      /* Store position of first unprocessed word.  */
+      *src = run;
+
+      wc = *run++;
 
       if (wc < 0 || wc > 0x7fffffff)
 	{
@@ -73,6 +78,7 @@ __wcsrtombs (dst, src, len, ps)
 	  /* Found the end.  */
 	  if (dst != NULL)
 	    *dst = '\0';
+	  ps->count = 0;
 	  *src = NULL;
 	  return written;
 	}
@@ -120,6 +126,9 @@ __wcsrtombs (dst, src, len, ps)
   /* Store position of first unprocessed word.  */
   *src = run;
 
+  /* Signal that we finished correctly.  */
+  ps->count = 0;
+
   return written;
 }
 weak_alias (__wcsrtombs, wcsrtombs)
diff --git a/wcsmbs/wmemrtombs.c b/wcsmbs/wmemrtombs.c
index 2bbd66788e..5b8e39cbe3 100644
--- a/wcsmbs/wmemrtombs.c
+++ b/wcsmbs/wmemrtombs.c
@@ -63,7 +63,12 @@ __wmemrtombs (dst, src, nwc, len, ps)
 
   while (written < len && nwc-- > 0)
     {
-      wchar_t wc = *run++;
+      wchar_t wc;
+
+      /* Store position of first unprocessed word.  */
+      *src = run;
+
+      wc = *run++;
 
       if (wc < 0 || wc > 0x7fffffff)
 	{
diff --git a/wcsmbs/wmemrtowcs.c b/wcsmbs/wmemrtowcs.c
index 1686229b8f..4efdd72d2e 100644
--- a/wcsmbs/wmemrtowcs.c
+++ b/wcsmbs/wmemrtowcs.c
@@ -43,10 +43,16 @@ __wmemrtowcs (dst, src, nmc, len, ps)
   size_t written = 0;
   const char *run = *src;
   const char *last = run + nmc;
+  wchar_t value;
+  size_t count;
 
   if (ps == NULL)
     ps = &internal;
 
+  /* Get information from last use of this state.  */
+  count = ps->count;
+  value = ps->value;
+
   if (dst == NULL)
     /* The LEN parameter has to be ignored if we don't actually write
        anything.  */
@@ -55,57 +61,66 @@ __wmemrtowcs (dst, src, nmc, len, ps)
   /* Copy all words.  */
   while (written < len && run < last)
     {
-      wchar_t value;
-      size_t count;
-      unsigned char byte = *run++;
+      unsigned char byte;
 
-      /* We expect a start of a new multibyte character.  */
-      if (byte < 0x80)
-	{
-	  /* One byte sequence.  */
-	  count = 0;
-	  value = byte;
-	}
-      else if ((byte & 0xe0) == 0xc0)
-	{
-	  count = 1;
-	  value = byte & 0x1f;
-	}
-      else if ((byte & 0xf0) == 0xe0)
-	{
-	  /* We expect three bytes.  */
-	  count = 2;
-	  value = byte & 0x0f;
-	}
-      else if ((byte & 0xf8) == 0xf0)
-	{
-	  /* We expect four bytes.  */
-	  count = 3;
-	  value = byte & 0x07;
-	}
-      else if ((byte & 0xfc) == 0xf8)
-	{
-	  /* We expect five bytes.  */
-	  count = 4;
-	  value = byte & 0x03;
-	}
-      else if ((byte & 0xfe) == 0xfc)
-	{
-	  /* We expect six bytes.  */
-	  count = 5;
-	  value = byte & 0x01;
-	}
-      else
+      /* Store address of next byte to process.  */
+      *src = run;
+
+      /* Start reading a new character only if we are in the initial
+	 state.  */
+      if (count == 0)
 	{
-	  /* This is an illegal encoding.  */
-	  __set_errno (EILSEQ);
-	  return (size_t) -1;
+	  byte = *run++;
+
+	  /* We expect a start of a new multibyte character.  */
+	  if (byte < 0x80)
+	    {
+	      /* One byte sequence.  */
+	      count = 0;
+	      value = byte;
+	    }
+	  else if ((byte & 0xe0) == 0xc0)
+	    {
+	      count = 1;
+	      value = byte & 0x1f;
+	    }
+	  else if ((byte & 0xf0) == 0xe0)
+	    {
+	      /* We expect three bytes.  */
+	      count = 2;
+	      value = byte & 0x0f;
+	    }
+	  else if ((byte & 0xf8) == 0xf0)
+	    {
+	      /* We expect four bytes.  */
+	      count = 3;
+	      value = byte & 0x07;
+	    }
+	  else if ((byte & 0xfc) == 0xf8)
+	    {
+	      /* We expect five bytes.  */
+	      count = 4;
+	      value = byte & 0x03;
+	    }
+	  else if ((byte & 0xfe) == 0xfc)
+	    {
+	      /* We expect six bytes.  */
+	      count = 5;
+	      value = byte & 0x01;
+	    }
+	  else
+	    {
+	      /* This is an illegal encoding.  */
+	      __set_errno (EILSEQ);
+	      return (size_t) -1;
+	    }
 	}
 
       /* Read the possible remaining bytes.  */
-      while (count-- > 0)
+      while (run < last && count > 0)
 	{
 	  byte = *run++;
+	  --count;
 
 	  if ((byte & 0xc0) != 0x80)
 	    {
@@ -118,6 +133,14 @@ __wmemrtowcs (dst, src, nmc, len, ps)
 	  value |= byte & 0x3f;
 	}
 
+      /* If this character is only partially available remember this.  */
+      if (run == last && count != 0)
+	{
+	  ps->count = count;
+	  ps->value = value;
+	  break;
+	}
+
       /* Store value is required.  */
       if (dst != NULL)
 	*dst++ = value;