Back to index

glibc  2.9
mbsnrtowcs.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2000, 2002, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <dlfcn.h>
00022 #include <errno.h>
00023 #include <gconv.h>
00024 #include <string.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 
00035 /* This is the private state used if PS is NULL.  */
00036 static mbstate_t state;
00037 
00038 /* This is a non-standard function but it is very useful in the
00039    implementation of stdio because we have to deal with unterminated
00040    buffers.  At most NMC bytes will be converted.  */
00041 size_t
00042 __mbsnrtowcs (dst, src, nmc, len, ps)
00043      wchar_t *dst;
00044      const char **src;
00045      size_t nmc;
00046      size_t len;
00047      mbstate_t *ps;
00048 {
00049   const unsigned char *srcend;
00050   struct __gconv_step_data data;
00051   size_t result;
00052   int status;
00053   struct __gconv_step *towc;
00054   size_t dummy;
00055   const struct gconv_fcts *fcts;
00056 
00057   /* Tell where we want the result.  */
00058   data.__invocation_counter = 0;
00059   data.__internal_use = 1;
00060   data.__flags = __GCONV_IS_LAST;
00061   data.__statep = ps ?: &state;
00062   data.__trans = NULL;
00063 
00064   if (nmc == 0)
00065     return 0;
00066   srcend = (const unsigned char *) *src + __strnlen (*src, nmc - 1) + 1;
00067 
00068   /* Get the conversion functions.  */
00069   fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
00070 
00071   /* Get the structure with the function pointers.  */
00072   towc = fcts->towc;
00073   __gconv_fct fct = towc->__fct;
00074 #ifdef PTR_DEMANGLE
00075   if (towc->__shlib_handle != NULL)
00076     PTR_DEMANGLE (fct);
00077 #endif
00078 
00079   /* We have to handle DST == NULL special.  */
00080   if (dst == NULL)
00081     {
00082       wchar_t buf[64];             /* Just an arbitrary size.  */
00083       const unsigned char *inbuf = (const unsigned char *) *src;
00084 
00085       result = 0;
00086       data.__outbufend = (unsigned char *) buf + sizeof (buf);
00087       do
00088        {
00089          data.__outbuf = (unsigned char *) buf;
00090 
00091          status = DL_CALL_FCT (fct, (towc, &data, &inbuf, srcend, NULL,
00092                                   &dummy, 0, 1));
00093 
00094          result += (wchar_t *) data.__outbuf - buf;
00095        }
00096       while (status == __GCONV_FULL_OUTPUT);
00097 
00098       if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
00099          && ((wchar_t *) data.__outbuf)[-1] == L'\0')
00100        /* Don't count the NUL character in.  */
00101        --result;
00102     }
00103   else
00104     {
00105       /* This code is based on the safe assumption that all internal
00106         multi-byte encodings use the NUL byte only to mark the end
00107         of the string.  */
00108       data.__outbuf = (unsigned char *) dst;
00109       data.__outbufend = data.__outbuf + len * sizeof (wchar_t);
00110 
00111       status = DL_CALL_FCT (fct,
00112                          (towc, &data, (const unsigned char **) src, srcend,
00113                           NULL, &dummy, 0, 1));
00114 
00115       result = (wchar_t *) data.__outbuf - dst;
00116 
00117       /* We have to determine whether the last character converted
00118         is the NUL character.  */
00119       if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
00120          && (assert (result > 0),
00121              ((wchar_t *) dst)[result - 1] == L'\0'))
00122        {
00123          assert (__mbsinit (data.__statep));
00124          *src = NULL;
00125          --result;
00126        }
00127     }
00128 
00129   /* There must not be any problems with the conversion but illegal input
00130      characters.  */
00131   assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
00132          || status == __GCONV_ILLEGAL_INPUT
00133          || status == __GCONV_INCOMPLETE_INPUT
00134          || status == __GCONV_FULL_OUTPUT);
00135 
00136   if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
00137       && status != __GCONV_EMPTY_INPUT && status != __GCONV_INCOMPLETE_INPUT)
00138     {
00139       result = (size_t) -1;
00140       __set_errno (EILSEQ);
00141     }
00142 
00143   return result;
00144 }
00145 weak_alias (__mbsnrtowcs, mbsnrtowcs)