Back to index

glibc  2.9
gconv.c
Go to the documentation of this file.
00001 /* Convert characters in input buffer using conversion descriptor to
00002    output buffer.
00003    Copyright (C) 1997-2001, 2005, 2007 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <assert.h>
00023 #include <dlfcn.h>
00024 #include <stddef.h>
00025 #include <sys/param.h>
00026 
00027 #include <gconv_int.h>
00028 #include <sysdep.h>
00029 
00030 
00031 int
00032 internal_function
00033 __gconv (__gconv_t cd, const unsigned char **inbuf,
00034         const unsigned char *inbufend, unsigned char **outbuf,
00035         unsigned char *outbufend, size_t *irreversible)
00036 {
00037   size_t last_step;
00038   int result;
00039 
00040   if (cd == (__gconv_t) -1L)
00041     return __GCONV_ILLEGAL_DESCRIPTOR;
00042 
00043   last_step = cd->__nsteps - 1;
00044 
00045   assert (irreversible != NULL);
00046   *irreversible = 0;
00047 
00048   cd->__data[last_step].__outbuf = outbuf != NULL ? *outbuf : NULL;
00049   cd->__data[last_step].__outbufend = outbufend;
00050 
00051   __gconv_fct fct = cd->__steps->__fct;
00052 #ifdef PTR_DEMANGLE
00053   if (cd->__steps->__shlib_handle != NULL)
00054     PTR_DEMANGLE (fct);
00055 #endif
00056 
00057   if (inbuf == NULL || *inbuf == NULL)
00058     {
00059       /* We just flush.  */
00060       result = DL_CALL_FCT (fct,
00061                          (cd->__steps, cd->__data, NULL, NULL, NULL,
00062                           irreversible,
00063                           cd->__data[last_step].__outbuf == NULL ? 2 : 1,
00064                           0));
00065 
00066       /* If the flush was successful clear the rest of the state.  */
00067       if (result == __GCONV_OK)
00068        for (size_t cnt = 0; cnt <= last_step; ++cnt)
00069          cd->__data[cnt].__invocation_counter = 0;
00070     }
00071   else
00072     {
00073       const unsigned char *last_start;
00074 
00075       assert (outbuf != NULL && *outbuf != NULL);
00076 
00077       do
00078        {
00079          last_start = *inbuf;
00080          result = DL_CALL_FCT (fct,
00081                             (cd->__steps, cd->__data, inbuf, inbufend,
00082                              NULL, irreversible, 0, 0));
00083        }
00084       while (result == __GCONV_EMPTY_INPUT && last_start != *inbuf
00085             && *inbuf + cd->__steps->__min_needed_from <= inbufend);
00086     }
00087 
00088   if (outbuf != NULL && *outbuf != NULL)
00089     *outbuf = cd->__data[last_step].__outbuf;
00090 
00091   return result;
00092 }