Blame iconv/gconv.c

Packit 6c4009
/* Convert characters in input buffer using conversion descriptor to
Packit 6c4009
   output buffer.
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
Packit 6c4009
#include <gconv_int.h>
Packit 6c4009
#include <sysdep.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
__gconv (__gconv_t cd, const unsigned char **inbuf,
Packit 6c4009
	 const unsigned char *inbufend, unsigned char **outbuf,
Packit 6c4009
	 unsigned char *outbufend, size_t *irreversible)
Packit 6c4009
{
Packit 6c4009
  size_t last_step;
Packit 6c4009
  int result;
Packit 6c4009
Packit 6c4009
  if (cd == (__gconv_t) -1L)
Packit 6c4009
    return __GCONV_ILLEGAL_DESCRIPTOR;
Packit 6c4009
Packit 6c4009
  last_step = cd->__nsteps - 1;
Packit 6c4009
Packit 6c4009
  assert (irreversible != NULL);
Packit 6c4009
  *irreversible = 0;
Packit 6c4009
Packit 6c4009
  cd->__data[last_step].__outbuf = outbuf != NULL ? *outbuf : NULL;
Packit 6c4009
  cd->__data[last_step].__outbufend = outbufend;
Packit 6c4009
Packit 6c4009
  __gconv_fct fct = cd->__steps->__fct;
Packit 6c4009
#ifdef PTR_DEMANGLE
Packit 6c4009
  if (cd->__steps->__shlib_handle != NULL)
Packit 6c4009
    PTR_DEMANGLE (fct);
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
  if (inbuf == NULL || *inbuf == NULL)
Packit 6c4009
    {
Packit 6c4009
      /* We just flush.  */
Packit 6c4009
      result = DL_CALL_FCT (fct,
Packit 6c4009
			    (cd->__steps, cd->__data, NULL, NULL, NULL,
Packit 6c4009
			     irreversible,
Packit 6c4009
			     cd->__data[last_step].__outbuf == NULL ? 2 : 1,
Packit 6c4009
			     0));
Packit 6c4009
Packit 6c4009
      /* If the flush was successful clear the rest of the state.  */
Packit 6c4009
      if (result == __GCONV_OK)
Packit 6c4009
	for (size_t cnt = 0; cnt <= last_step; ++cnt)
Packit 6c4009
	  cd->__data[cnt].__invocation_counter = 0;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      const unsigned char *last_start;
Packit 6c4009
Packit 6c4009
      assert (outbuf != NULL && *outbuf != NULL);
Packit 6c4009
Packit 6c4009
      do
Packit 6c4009
	{
Packit 6c4009
	  last_start = *inbuf;
Packit 6c4009
	  result = DL_CALL_FCT (fct,
Packit 6c4009
				(cd->__steps, cd->__data, inbuf, inbufend,
Packit 6c4009
				 NULL, irreversible, 0, 0));
Packit 6c4009
	}
Packit 6c4009
      while (result == __GCONV_EMPTY_INPUT && last_start != *inbuf
Packit 6c4009
	     && *inbuf + cd->__steps->__min_needed_from <= inbufend);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (outbuf != NULL && *outbuf != NULL)
Packit 6c4009
    *outbuf = cd->__data[last_step].__outbuf;
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}