]> git.ipfire.org Git - thirdparty/glibc.git/blob - wcsmbs/mbsnrtowcs.c
update from main archive 970125
[thirdparty/glibc.git] / wcsmbs / mbsnrtowcs.c
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.
4
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.
9
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.
14
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. */
19
20 #include <errno.h>
21 #include <wchar.h>
22
23 #ifndef EILSEQ
24 #define EILSEQ EINVAL
25 #endif
26
27
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;
31
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;
42 {
43 size_t written = 0;
44 const char *run = *src;
45 const char *last = run + nmc;
46
47 if (ps == NULL)
48 ps = &internal;
49
50 if (dst == NULL)
51 /* The LEN parameter has to be ignored if we don't actually write
52 anything. */
53 len = ~0;
54
55 /* Copy all words. */
56 while (written < len && run < last)
57 {
58 wchar_t value;
59 size_t count;
60 unsigned char byte = *run++;
61
62 /* We expect a start of a new multibyte character. */
63 if (byte < 0x80)
64 {
65 /* One byte sequence. */
66 count = 0;
67 value = byte;
68 }
69 else if ((byte & 0xe0) == 0xc0)
70 {
71 count = 1;
72 value = byte & 0x1f;
73 }
74 else if ((byte & 0xf0) == 0xe0)
75 {
76 /* We expect three bytes. */
77 count = 2;
78 value = byte & 0x0f;
79 }
80 else if ((byte & 0xf8) == 0xf0)
81 {
82 /* We expect four bytes. */
83 count = 3;
84 value = byte & 0x07;
85 }
86 else if ((byte & 0xfc) == 0xf8)
87 {
88 /* We expect five bytes. */
89 count = 4;
90 value = byte & 0x03;
91 }
92 else if ((byte & 0xfe) == 0xfc)
93 {
94 /* We expect six bytes. */
95 count = 5;
96 value = byte & 0x01;
97 }
98 else
99 {
100 /* This is an illegal encoding. */
101 __set_errno (EILSEQ);
102 return (size_t) -1;
103 }
104
105 /* Read the possible remaining bytes. */
106 while (count-- > 0)
107 {
108 byte = *run++;
109
110 if ((byte & 0xc0) != 0x80)
111 {
112 /* This is an illegal encoding. */
113 __set_errno (EILSEQ);
114 return (size_t) -1;
115 }
116
117 value <<= 6;
118 value |= byte & 0x3f;
119 }
120
121 /* Store value is required. */
122 if (dst != NULL)
123 *dst++ = value;
124
125 /* The whole sequence is read. Check whether end of string is
126 reached. */
127 if (value == L'\0')
128 {
129 /* Found the end of the string. */
130 *src = NULL;
131 return written;
132 }
133
134 /* Increment counter of produced words. */
135 ++written;
136 }
137
138 /* Store address of next byte to process. */
139 *src = run;
140
141 return written;
142 }
143 weak_alias (__mbsnrtowcs, mbsnrtowcs)