Update.
[glibc/history.git] / wcsmbs / mbsrtowcs.c
blobd7305d69047d929570cb1705534ff1d95b3ae806
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1996.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <wchar.h>
23 #ifndef EILSEQ
24 #define EILSEQ EINVAL
25 #endif
28 /* We don't need the state really because we don't have shift states
29 to maintain between calls to this function. */
30 static mbstate_t internal;
32 size_t
33 __mbsrtowcs (dst, src, len, ps)
34 wchar_t *dst;
35 const char **src;
36 size_t len;
37 mbstate_t *ps;
39 size_t written = 0;
40 const char *run = *src;
42 if (ps == NULL)
43 ps = &internal;
45 if (dst == NULL)
46 /* The LEN parameter has to be ignored if we don't actually write
47 anything. */
48 len = ~0;
50 /* Copy all words. */
51 while (written < len)
53 wchar_t value;
54 size_t count;
55 unsigned char byte = *run++;
57 /* We expect a start of a new multibyte character. */
58 if (byte < 0x80)
60 /* One byte sequence. */
61 count = 0;
62 value = byte;
64 else if ((byte & 0xe0) == 0xc0)
66 count = 1;
67 value = byte & 0x1f;
69 else if ((byte & 0xf0) == 0xe0)
71 /* We expect three bytes. */
72 count = 2;
73 value = byte & 0x0f;
75 else if ((byte & 0xf8) == 0xf0)
77 /* We expect four bytes. */
78 count = 3;
79 value = byte & 0x07;
81 else if ((byte & 0xfc) == 0xf8)
83 /* We expect five bytes. */
84 count = 4;
85 value = byte & 0x03;
87 else if ((byte & 0xfe) == 0xfc)
89 /* We expect six bytes. */
90 count = 5;
91 value = byte & 0x01;
93 else
95 /* This is an illegal encoding. */
96 errno = EILSEQ;
97 return (size_t) -1;
100 /* Read the possible remaining bytes. */
101 while (count-- > 0)
103 byte = *run++;
105 if ((byte & 0xc0) != 0x80)
107 /* This is an illegal encoding. */
108 errno = EILSEQ;
109 return (size_t) -1;
112 value <<= 6;
113 value |= byte & 0x3f;
116 /* Store value is required. */
117 if (dst != NULL)
118 *dst++ = value;
120 /* The whole sequence is read. Check whether end of string is
121 reached. */
122 if (value == L'\0')
124 /* Found the end of the string. */
125 *src = NULL;
126 return written;
129 /* Increment counter of produced words. */
130 ++written;
133 /* Store address of next byte to process. */
134 *src = run;
136 return written;
138 weak_alias (__mbsrtowcs, mbsrtowcs)