Back to index

glibc  2.9
mbsrtowcs_l.c
Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Ulrich Drepper <drepper@gnu.org>, 2002.
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 <ctype.h>
00022 #include <string.h>
00023 #include "wcsmbsload.h"
00024 #include <dlfcn.h>
00025 #include <errno.h>
00026 #include <gconv.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <wchar.h>
00030 #include <wcsmbsload.h>
00031 
00032 #include <sysdep.h>
00033 
00034 #ifndef EILSEQ
00035 # define EILSEQ EINVAL
00036 #endif
00037 
00038 
00039 size_t
00040 attribute_hidden
00041 __mbsrtowcs_l (dst, src, len, ps, l)
00042      wchar_t *dst;
00043      const char **src;
00044      size_t len;
00045      mbstate_t *ps;
00046      __locale_t l;
00047 {
00048   struct __gconv_step_data data;
00049   size_t result;
00050   int status;
00051   struct __gconv_step *towc;
00052   size_t non_reversible;
00053   const struct gconv_fcts *fcts;
00054 
00055   /* Tell where we want the result.  */
00056   data.__invocation_counter = 0;
00057   data.__internal_use = 1;
00058   data.__flags = __GCONV_IS_LAST;
00059   data.__statep = ps;
00060   data.__trans = NULL;
00061 
00062   /* Get the conversion functions.  */
00063   fcts = get_gconv_fcts (l->__locales[LC_CTYPE]);
00064 
00065   /* Get the structure with the function pointers.  */
00066   towc = fcts->towc;
00067   __gconv_fct fct = towc->__fct;
00068 #ifdef PTR_DEMANGLE
00069   if (towc->__shlib_handle != NULL)
00070     PTR_DEMANGLE (fct);
00071 #endif
00072 
00073   /* We have to handle DST == NULL special.  */
00074   if (dst == NULL)
00075     {
00076       mbstate_t temp_state;
00077       wchar_t buf[64];             /* Just an arbitrary size.  */
00078       const unsigned char *inbuf = (const unsigned char *) *src;
00079       const unsigned char *srcend = inbuf + strlen (*src) + 1;
00080 
00081       temp_state = *data.__statep;
00082       data.__statep = &temp_state;
00083 
00084       result = 0;
00085       data.__outbufend = (unsigned char *) buf + sizeof (buf);
00086       do
00087        {
00088          data.__outbuf = (unsigned char *) buf;
00089 
00090          status = DL_CALL_FCT (fct, (towc, &data, &inbuf, srcend, NULL,
00091                                   &non_reversible, 0, 1));
00092 
00093          result += (wchar_t *) data.__outbuf - buf;
00094        }
00095       while (status == __GCONV_FULL_OUTPUT);
00096 
00097       if (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
00098        {
00099          /* There better should be a NUL wide char at the end.  */
00100          assert (((wchar_t *) data.__outbuf)[-1] == L'\0');
00101          /* Don't count the NUL character in.  */
00102          --result;
00103        }
00104     }
00105   else
00106     {
00107       /* This code is based on the safe assumption that all internal
00108         multi-byte encodings use the NUL byte only to mark the end
00109         of the string.  */
00110       const unsigned char *srcp = (const unsigned char *) *src;
00111       const unsigned char *srcend;
00112 
00113       data.__outbuf = (unsigned char *) dst;
00114       data.__outbufend = data.__outbuf + len * sizeof (wchar_t);
00115 
00116       status = __GCONV_FULL_OUTPUT;
00117 
00118       while (len > 0)
00119        {
00120          /* Pessimistic guess as to how much input we can use.  In the
00121             worst case we need one input byte for one output wchar_t.  */
00122          srcend = srcp + __strnlen ((const char *) srcp, len) + 1;
00123 
00124          status = DL_CALL_FCT (fct, (towc, &data, &srcp, srcend, NULL,
00125                                   &non_reversible, 0, 1));
00126          if ((status != __GCONV_EMPTY_INPUT
00127               && status != __GCONV_INCOMPLETE_INPUT)
00128              /* Not all input read.  */
00129              || srcp != srcend
00130              /* Reached the end of the input.  */
00131              || srcend[-1] == '\0')
00132            break;
00133 
00134          len = (wchar_t *) data.__outbufend - (wchar_t *) data.__outbuf;
00135        }
00136 
00137       /* Make the end if the input known to the caller.  */
00138       *src = (const char *) srcp;
00139 
00140       result = (wchar_t *) data.__outbuf - dst;
00141 
00142       /* We have to determine whether the last character converted
00143         is the NUL character.  */
00144       if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
00145          && ((wchar_t *) dst)[result - 1] == L'\0')
00146        {
00147          assert (result > 0);
00148          assert (__mbsinit (data.__statep));
00149          *src = NULL;
00150          --result;
00151        }
00152     }
00153 
00154   /* There must not be any problems with the conversion but illegal input
00155      characters.  */
00156   assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
00157          || status == __GCONV_ILLEGAL_INPUT
00158          || status == __GCONV_INCOMPLETE_INPUT
00159          || status == __GCONV_FULL_OUTPUT);
00160 
00161   if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
00162       && status != __GCONV_EMPTY_INPUT && status != __GCONV_INCOMPLETE_INPUT)
00163     {
00164       result = (size_t) -1;
00165       __set_errno (EILSEQ);
00166     }
00167 
00168   return result;
00169 }