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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
28 static mbstate_t internal
;
31 __mbrtowc (wchar_t *pwc
, const char *s
, size_t n
, mbstate_t *ps
)
40 /* See first paragraph of description in 7.16.6.3.2. */
50 unsigned char byte
= (unsigned char) *s
++;
53 /* We must look for a possible first byte of a UTF8 sequence. */
56 /* One byte sequence. */
58 *pwc
= (wchar_t) byte
;
59 return byte
? used
: 0;
62 if ((byte
& 0xc0) == 0x80 || (byte
& 0xfe) == 0xfe)
64 /* Oh, oh. An encoding error. */
69 if ((byte
& 0xe0) == 0xc0)
71 /* We expect two bytes. */
73 ps
->value
= byte
& 0x1f;
75 else if ((byte
& 0xf0) == 0xe0)
77 /* We expect three bytes. */
79 ps
->value
= byte
& 0x0f;
81 else if ((byte
& 0xf8) == 0xf0)
83 /* We expect four bytes. */
85 ps
->value
= byte
& 0x07;
87 else if ((byte
& 0xfc) == 0xf8)
89 /* We expect five bytes. */
91 ps
->value
= byte
& 0x03;
95 /* We expect six bytes. */
97 ps
->value
= byte
& 0x01;
101 /* We know we have to handle a multibyte character and there are
102 some more bytes to read. */
105 /* The second to sixths byte must be of the form 10xxxxxx. */
106 unsigned char byte
= (unsigned char) *s
++;
109 if ((byte
& 0xc0) != 0x80)
111 /* Oh, oh. An encoding error. */
112 __set_errno (EILSEQ
);
117 ps
->value
|= byte
& 0x3f;
119 if (--ps
->count
== 0)
121 /* The character is finished. */
123 *pwc
= (wchar_t) ps
->value
;
124 return ps
->value
? used
: 0;
131 weak_alias (__mbrtowc
, mbrtowc
)