Update.
[glibc/history.git] / wcsmbs / mbsnrtowcs.c
blobdb67d5c1bb20405a8fa943606b1889baafa56e00
1 /* Copyright (C) 1996, 1997 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 not,
17 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 /* This is a non-standard function but it is very useful in the
33 implementation of stdio because we have to deal with unterminated
34 buffers. At most NMC bytes will be converted. */
35 size_t
36 __mbsnrtowcs (dst, src, nmc, len, ps)
37 wchar_t *dst;
38 const char **src;
39 size_t nmc;
40 size_t len;
41 mbstate_t *ps;
43 size_t written = 0;
44 const char *run = *src;
45 const char *last = run + nmc;
46 wchar_t value;
47 size_t count;
49 if (ps == NULL)
50 ps = &internal;
52 /* Get information from last use of this state. */
53 count = ps->count;
54 value = ps->value;
56 if (dst == NULL)
57 /* The LEN parameter has to be ignored if we don't actually write
58 anything. */
59 len = ~0;
61 /* Copy all words. */
62 while (written < len && run < last)
64 unsigned char byte;
66 /* Store address of next byte to process. */
67 *src = run;
69 /* Start reading a new character only if we are in the initial
70 state. */
71 if (count == 0)
73 byte = *run++;
75 /* We expect a start of a new multibyte character. */
76 if (byte < 0x80)
78 /* One byte sequence. */
79 count = 0;
80 value = byte;
82 else if ((byte & 0xe0) == 0xc0)
84 count = 1;
85 value = byte & 0x1f;
87 else if ((byte & 0xf0) == 0xe0)
89 /* We expect three bytes. */
90 count = 2;
91 value = byte & 0x0f;
93 else if ((byte & 0xf8) == 0xf0)
95 /* We expect four bytes. */
96 count = 3;
97 value = byte & 0x07;
99 else if ((byte & 0xfc) == 0xf8)
101 /* We expect five bytes. */
102 count = 4;
103 value = byte & 0x03;
105 else if ((byte & 0xfe) == 0xfc)
107 /* We expect six bytes. */
108 count = 5;
109 value = byte & 0x01;
111 else
113 /* This is an illegal encoding. */
114 __set_errno (EILSEQ);
115 return (size_t) -1;
119 /* Read the possible remaining bytes. */
120 while (run < last && count > 0)
122 byte = *run++;
123 --count;
125 if ((byte & 0xc0) != 0x80)
127 /* This is an illegal encoding. */
128 __set_errno (EILSEQ);
129 return (size_t) -1;
132 value <<= 6;
133 value |= byte & 0x3f;
136 /* If this character is only partially available remember this. */
137 if (run == last && count != 0)
139 ps->count = count;
140 ps->value = value;
141 break;
144 /* Store value is required. */
145 if (dst != NULL)
146 *dst++ = value;
148 /* The whole sequence is read. Check whether end of string is
149 reached. */
150 if (value == L'\0')
152 /* Found the end of the string. */
153 *src = NULL;
154 ps->count = 0;
155 return written;
158 /* Increment counter of produced words. */
159 ++written;
162 /* Store address of next byte to process. */
163 *src = run;
165 return written;
167 weak_alias (__mbsnrtowcs, mbsnrtowcs)