Back to index

glibc  2.9
mbrtowc.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2002, 2004, 2005
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <assert.h>
00022 #include <dlfcn.h>
00023 #include <errno.h>
00024 #include <gconv.h>
00025 #include <wchar.h>
00026 #include <wcsmbsload.h>
00027 
00028 #include <sysdep.h>
00029 
00030 #ifndef EILSEQ
00031 # define EILSEQ EINVAL
00032 #endif
00033 
00034 /* This is the private state used if PS is NULL.  */
00035 static mbstate_t state;
00036 
00037 size_t
00038 __mbrtowc (wchar_t *pwc, const char *s, size_t n, mbstate_t *ps)
00039 {
00040   wchar_t buf[1];
00041   struct __gconv_step_data data;
00042   int status;
00043   size_t result;
00044   size_t dummy;
00045   const unsigned char *inbuf, *endbuf;
00046   unsigned char *outbuf = (unsigned char *) (pwc ?: buf);
00047   const struct gconv_fcts *fcts;
00048 
00049   /* Set information for this step.  */
00050   data.__invocation_counter = 0;
00051   data.__internal_use = 1;
00052   data.__flags = __GCONV_IS_LAST;
00053   data.__statep = ps ?: &state;
00054   data.__trans = NULL;
00055 
00056   /* A first special case is if S is NULL.  This means put PS in the
00057      initial state.  */
00058   if (s == NULL)
00059     {
00060       outbuf = (unsigned char *) buf;
00061       s = "";
00062       n = 1;
00063     }
00064 
00065   /* Tell where we want the result.  */
00066   data.__outbuf = outbuf;
00067   data.__outbufend = outbuf + sizeof (wchar_t);
00068 
00069   /* Get the conversion functions.  */
00070   fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
00071 
00072   /* Do a normal conversion.  */
00073   inbuf = (const unsigned char *) s;
00074   endbuf = inbuf + n;
00075   if (__builtin_expect (endbuf < inbuf, 0))
00076     endbuf = (const unsigned char *) ~(uintptr_t) 0;
00077   __gconv_fct fct = fcts->towc->__fct;
00078 #ifdef PTR_DEMANGLE
00079   if (fcts->towc->__shlib_handle != NULL)
00080     PTR_DEMANGLE (fct);
00081 #endif
00082   status = DL_CALL_FCT (fct, (fcts->towc, &data, &inbuf, endbuf,
00083                            NULL, &dummy, 0, 1));
00084 
00085   /* There must not be any problems with the conversion but illegal input
00086      characters.  The output buffer must be large enough, otherwise the
00087      definition of MB_CUR_MAX is not correct.  All the other possible
00088      errors also must not happen.  */
00089   assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
00090          || status == __GCONV_ILLEGAL_INPUT
00091          || status == __GCONV_INCOMPLETE_INPUT
00092          || status == __GCONV_FULL_OUTPUT);
00093 
00094   if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
00095       || status == __GCONV_FULL_OUTPUT)
00096     {
00097       if (data.__outbuf != (unsigned char *) outbuf
00098          && *(wchar_t *) outbuf == L'\0')
00099        {
00100          /* The converted character is the NUL character.  */
00101          assert (__mbsinit (data.__statep));
00102          result = 0;
00103        }
00104       else
00105        result = inbuf - (const unsigned char *) s;
00106     }
00107   else if (status == __GCONV_INCOMPLETE_INPUT)
00108     result = (size_t) -2;
00109   else
00110     {
00111       result = (size_t) -1;
00112       __set_errno (EILSEQ);
00113     }
00114 
00115   return result;
00116 }
00117 libc_hidden_def (__mbrtowc)
00118 weak_alias (__mbrtowc, mbrtowc)
00119 libc_hidden_weak (mbrtowc)