Back to index

glibc  2.9
wcsrtombs.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2000, 2002, 2003, 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 <stdlib.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 
00035 /* This is the private state used if PS is NULL.  */
00036 static mbstate_t state;
00037 
00038 size_t
00039 __wcsrtombs (dst, src, len, ps)
00040      char *dst;
00041      const wchar_t **src;
00042      size_t len;
00043      mbstate_t *ps;
00044 {
00045   struct __gconv_step_data data;
00046   int status;
00047   size_t result;
00048   struct __gconv_step *tomb;
00049   const struct gconv_fcts *fcts;
00050 
00051   /* Tell where we want the result.  */
00052   data.__invocation_counter = 0;
00053   data.__internal_use = 1;
00054   data.__flags = __GCONV_IS_LAST;
00055   data.__statep = ps ?: &state;
00056   data.__trans = NULL;
00057 
00058   /* Get the conversion functions.  */
00059   fcts = get_gconv_fcts (_NL_CURRENT_DATA (LC_CTYPE));
00060 
00061   /* Get the structure with the function pointers.  */
00062   tomb = fcts->tomb;
00063   __gconv_fct fct = tomb->__fct;
00064 #ifdef PTR_DEMANGLE
00065   if (tomb->__shlib_handle != NULL)
00066     PTR_DEMANGLE (fct);
00067 #endif
00068 
00069   /* We have to handle DST == NULL special.  */
00070   if (dst == NULL)
00071     {
00072       mbstate_t temp_state;
00073       unsigned char buf[256];             /* Just an arbitrary value.  */
00074       const wchar_t *srcend = *src + __wcslen (*src) + 1;
00075       const unsigned char *inbuf = (const unsigned char *) *src;
00076       size_t dummy;
00077 
00078       temp_state = *data.__statep;
00079       data.__statep = &temp_state;
00080 
00081       result = 0;
00082       data.__outbufend = buf + sizeof (buf);
00083 
00084       do
00085        {
00086          data.__outbuf = buf;
00087 
00088          status = DL_CALL_FCT (fct, (tomb, &data, &inbuf,
00089                                   (const unsigned char *) srcend, NULL,
00090                                   &dummy, 0, 1));
00091 
00092          /* Count the number of bytes.  */
00093          result += 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 byte at the end.  */
00100          assert (data.__outbuf[-1] == '\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 wchar_t *srcend = *src + __wcsnlen (*src, len) + 1;
00111       size_t dummy;
00112 
00113       data.__outbuf = (unsigned char *) dst;
00114       data.__outbufend = (unsigned char *) dst + len;
00115 
00116       status = DL_CALL_FCT (fct, (tomb, &data, (const unsigned char **) src,
00117                               (const unsigned char *) srcend, NULL,
00118                               &dummy, 0, 1));
00119 
00120       /* Count the number of bytes.  */
00121       result = data.__outbuf - (unsigned char *) dst;
00122 
00123       /* We have to determine whether the last character converted
00124         is the NUL character.  */
00125       if ((status == __GCONV_OK || status == __GCONV_EMPTY_INPUT)
00126          && data.__outbuf[-1] == '\0')
00127        {
00128          assert (data.__outbuf != (unsigned char *) dst);
00129          assert (__mbsinit (data.__statep));
00130          *src = NULL;
00131          --result;
00132        }
00133     }
00134 
00135   /* There must not be any problems with the conversion but illegal input
00136      characters.  */
00137   assert (status == __GCONV_OK || status == __GCONV_EMPTY_INPUT
00138          || status == __GCONV_ILLEGAL_INPUT
00139          || status == __GCONV_INCOMPLETE_INPUT
00140          || status == __GCONV_FULL_OUTPUT);
00141 
00142   if (status != __GCONV_OK && status != __GCONV_FULL_OUTPUT
00143       && status != __GCONV_EMPTY_INPUT)
00144     {
00145       result = (size_t) -1;
00146       __set_errno (EILSEQ);
00147     }
00148 
00149   return result;
00150 }
00151 weak_alias (__wcsrtombs, wcsrtombs)