|
Packit |
6c4009 |
/* Copyright (C) 1999-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
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 |
As a special exception, if you link the code in this file with
|
|
Packit |
6c4009 |
files compiled with a GNU compiler to produce an executable,
|
|
Packit |
6c4009 |
that does not cause the resulting executable to be covered by
|
|
Packit |
6c4009 |
the GNU Lesser General Public License. This exception does not
|
|
Packit |
6c4009 |
however invalidate any other reasons why the executable file
|
|
Packit |
6c4009 |
might be covered by the GNU Lesser General Public License.
|
|
Packit |
6c4009 |
This exception applies to code released by its copyright holders
|
|
Packit |
6c4009 |
in files containing the exception. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <libioP.h>
|
|
Packit |
6c4009 |
#include <dlfcn.h>
|
|
Packit |
6c4009 |
#include <wchar.h>
|
|
Packit |
6c4009 |
#include <assert.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <langinfo.h>
|
|
Packit |
6c4009 |
#include <locale/localeinfo.h>
|
|
Packit |
6c4009 |
#include <wcsmbs/wcsmbsload.h>
|
|
Packit |
6c4009 |
#include <iconv/gconv_int.h>
|
|
Packit |
6c4009 |
#include <shlib-compat.h>
|
|
Packit |
6c4009 |
#include <sysdep.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Return orientation of stream. If mode is nonzero try to change
|
|
Packit |
6c4009 |
the orientation first. */
|
|
Packit |
6c4009 |
#undef _IO_fwide
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
_IO_fwide (FILE *fp, int mode)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Normalize the value. */
|
|
Packit |
6c4009 |
mode = mode < 0 ? -1 : (mode == 0 ? 0 : 1);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1)
|
|
Packit |
6c4009 |
if (__builtin_expect (&_IO_stdin_used == NULL, 0)
|
|
Packit |
6c4009 |
&& (fp == _IO_stdin || fp == _IO_stdout || fp == _IO_stderr))
|
|
Packit |
6c4009 |
/* This is for a stream in the glibc 2.0 format. */
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The orientation already has been determined. */
|
|
Packit |
6c4009 |
if (fp->_mode != 0
|
|
Packit |
6c4009 |
/* Or the caller simply wants to know about the current orientation. */
|
|
Packit |
6c4009 |
|| mode == 0)
|
|
Packit |
6c4009 |
return fp->_mode;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Set the orientation appropriately. */
|
|
Packit |
6c4009 |
if (mode > 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct _IO_codecvt *cc = fp->_codecvt = &fp->_wide_data->_codecvt;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fp->_wide_data->_IO_read_ptr = fp->_wide_data->_IO_read_end;
|
|
Packit |
6c4009 |
fp->_wide_data->_IO_write_ptr = fp->_wide_data->_IO_write_base;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Get the character conversion functions based on the currently
|
|
Packit |
6c4009 |
selected locale for LC_CTYPE. */
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Clear the state. We start all over again. */
|
|
Packit |
6c4009 |
memset (&fp->_wide_data->_IO_state, '\0', sizeof (__mbstate_t));
|
|
Packit |
6c4009 |
memset (&fp->_wide_data->_IO_last_state, '\0', sizeof (__mbstate_t));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct gconv_fcts fcts;
|
|
Packit |
6c4009 |
__wcsmbs_clone_conv (&fcts);
|
|
Packit |
6c4009 |
assert (fcts.towc_nsteps == 1);
|
|
Packit |
6c4009 |
assert (fcts.tomb_nsteps == 1);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
cc->__cd_in.__cd.__nsteps = fcts.towc_nsteps;
|
|
Packit |
6c4009 |
cc->__cd_in.__cd.__steps = fcts.towc;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
cc->__cd_in.__cd.__data[0].__invocation_counter = 0;
|
|
Packit |
6c4009 |
cc->__cd_in.__cd.__data[0].__internal_use = 1;
|
|
Packit |
6c4009 |
cc->__cd_in.__cd.__data[0].__flags = __GCONV_IS_LAST;
|
|
Packit |
6c4009 |
cc->__cd_in.__cd.__data[0].__statep = &fp->_wide_data->_IO_state;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
cc->__cd_out.__cd.__nsteps = fcts.tomb_nsteps;
|
|
Packit |
6c4009 |
cc->__cd_out.__cd.__steps = fcts.tomb;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
cc->__cd_out.__cd.__data[0].__invocation_counter = 0;
|
|
Packit |
6c4009 |
cc->__cd_out.__cd.__data[0].__internal_use = 1;
|
|
Packit |
6c4009 |
cc->__cd_out.__cd.__data[0].__flags
|
|
Packit |
6c4009 |
= __GCONV_IS_LAST | __GCONV_TRANSLIT;
|
|
Packit |
6c4009 |
cc->__cd_out.__cd.__data[0].__statep = &fp->_wide_data->_IO_state;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* From now on use the wide character callback functions. */
|
|
Packit |
6c4009 |
_IO_JUMPS_FILE_plus (fp) = fp->_wide_data->_wide_vtable;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Set the mode now. */
|
|
Packit |
6c4009 |
fp->_mode = mode;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return mode;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit Service |
357fe4 |
enum __codecvt_result
|
|
Packit Service |
357fe4 |
__libio_codecvt_out (struct _IO_codecvt *codecvt, __mbstate_t *statep,
|
|
Packit Service |
357fe4 |
const wchar_t *from_start, const wchar_t *from_end,
|
|
Packit Service |
357fe4 |
const wchar_t **from_stop, char *to_start, char *to_end,
|
|
Packit Service |
357fe4 |
char **to_stop)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
enum __codecvt_result result;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct __gconv_step *gs = codecvt->__cd_out.__cd.__steps;
|
|
Packit |
6c4009 |
int status;
|
|
Packit |
6c4009 |
size_t dummy;
|
|
Packit |
6c4009 |
const unsigned char *from_start_copy = (unsigned char *) from_start;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
codecvt->__cd_out.__cd.__data[0].__outbuf = (unsigned char *) to_start;
|
|
Packit |
6c4009 |
codecvt->__cd_out.__cd.__data[0].__outbufend = (unsigned char *) to_end;
|
|
Packit |
6c4009 |
codecvt->__cd_out.__cd.__data[0].__statep = statep;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__gconv_fct fct = gs->__fct;
|
|
Packit |
6c4009 |
#ifdef PTR_DEMANGLE
|
|
Packit |
6c4009 |
if (gs->__shlib_handle != NULL)
|
|
Packit |
6c4009 |
PTR_DEMANGLE (fct);
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
status = DL_CALL_FCT (fct,
|
|
Packit |
6c4009 |
(gs, codecvt->__cd_out.__cd.__data, &from_start_copy,
|
|
Packit |
6c4009 |
(const unsigned char *) from_end, NULL,
|
|
Packit |
6c4009 |
&dummy, 0, 0));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
*from_stop = (wchar_t *) from_start_copy;
|
|
Packit |
6c4009 |
*to_stop = (char *) codecvt->__cd_out.__cd.__data[0].__outbuf;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
switch (status)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case __GCONV_OK:
|
|
Packit |
6c4009 |
case __GCONV_EMPTY_INPUT:
|
|
Packit |
6c4009 |
result = __codecvt_ok;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case __GCONV_FULL_OUTPUT:
|
|
Packit |
6c4009 |
case __GCONV_INCOMPLETE_INPUT:
|
|
Packit |
6c4009 |
result = __codecvt_partial;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
result = __codecvt_error;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit Service |
357fe4 |
enum __codecvt_result
|
|
Packit Service |
357fe4 |
__libio_codecvt_in (struct _IO_codecvt *codecvt, __mbstate_t *statep,
|
|
Packit Service |
357fe4 |
const char *from_start, const char *from_end,
|
|
Packit Service |
357fe4 |
const char **from_stop,
|
|
Packit Service |
357fe4 |
wchar_t *to_start, wchar_t *to_end, wchar_t **to_stop)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
enum __codecvt_result result;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct __gconv_step *gs = codecvt->__cd_in.__cd.__steps;
|
|
Packit |
6c4009 |
int status;
|
|
Packit |
6c4009 |
size_t dummy;
|
|
Packit |
6c4009 |
const unsigned char *from_start_copy = (unsigned char *) from_start;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
codecvt->__cd_in.__cd.__data[0].__outbuf = (unsigned char *) to_start;
|
|
Packit |
6c4009 |
codecvt->__cd_in.__cd.__data[0].__outbufend = (unsigned char *) to_end;
|
|
Packit |
6c4009 |
codecvt->__cd_in.__cd.__data[0].__statep = statep;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__gconv_fct fct = gs->__fct;
|
|
Packit |
6c4009 |
#ifdef PTR_DEMANGLE
|
|
Packit |
6c4009 |
if (gs->__shlib_handle != NULL)
|
|
Packit |
6c4009 |
PTR_DEMANGLE (fct);
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
status = DL_CALL_FCT (fct,
|
|
Packit |
6c4009 |
(gs, codecvt->__cd_in.__cd.__data, &from_start_copy,
|
|
Packit |
6c4009 |
(const unsigned char *) from_end, NULL,
|
|
Packit |
6c4009 |
&dummy, 0, 0));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
*from_stop = (const char *) from_start_copy;
|
|
Packit |
6c4009 |
*to_stop = (wchar_t *) codecvt->__cd_in.__cd.__data[0].__outbuf;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
switch (status)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case __GCONV_OK:
|
|
Packit |
6c4009 |
case __GCONV_EMPTY_INPUT:
|
|
Packit |
6c4009 |
result = __codecvt_ok;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
case __GCONV_FULL_OUTPUT:
|
|
Packit |
6c4009 |
case __GCONV_INCOMPLETE_INPUT:
|
|
Packit |
6c4009 |
result = __codecvt_partial;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
result = __codecvt_error;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit Service |
357fe4 |
int
|
|
Packit Service |
357fe4 |
__libio_codecvt_encoding (struct _IO_codecvt *codecvt)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* See whether the encoding is stateful. */
|
|
Packit |
6c4009 |
if (codecvt->__cd_in.__cd.__steps[0].__stateful)
|
|
Packit |
6c4009 |
return -1;
|
|
Packit |
6c4009 |
/* Fortunately not. Now determine the input bytes for the conversion
|
|
Packit |
6c4009 |
necessary for each wide character. */
|
|
Packit |
6c4009 |
if (codecvt->__cd_in.__cd.__steps[0].__min_needed_from
|
|
Packit |
6c4009 |
!= codecvt->__cd_in.__cd.__steps[0].__max_needed_from)
|
|
Packit |
6c4009 |
/* Not a constant value. */
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return codecvt->__cd_in.__cd.__steps[0].__min_needed_from;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit Service |
357fe4 |
int
|
|
Packit Service |
357fe4 |
__libio_codecvt_length (struct _IO_codecvt *codecvt, __mbstate_t *statep,
|
|
Packit Service |
357fe4 |
const char *from_start, const char *from_end,
|
|
Packit Service |
357fe4 |
size_t max)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int result;
|
|
Packit |
6c4009 |
const unsigned char *cp = (const unsigned char *) from_start;
|
|
Packit |
6c4009 |
wchar_t to_buf[max];
|
|
Packit |
6c4009 |
struct __gconv_step *gs = codecvt->__cd_in.__cd.__steps;
|
|
Packit |
6c4009 |
size_t dummy;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
codecvt->__cd_in.__cd.__data[0].__outbuf = (unsigned char *) to_buf;
|
|
Packit |
6c4009 |
codecvt->__cd_in.__cd.__data[0].__outbufend = (unsigned char *) &to_buf[max];
|
|
Packit |
6c4009 |
codecvt->__cd_in.__cd.__data[0].__statep = statep;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
__gconv_fct fct = gs->__fct;
|
|
Packit |
6c4009 |
#ifdef PTR_DEMANGLE
|
|
Packit |
6c4009 |
if (gs->__shlib_handle != NULL)
|
|
Packit |
6c4009 |
PTR_DEMANGLE (fct);
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
DL_CALL_FCT (fct,
|
|
Packit |
6c4009 |
(gs, codecvt->__cd_in.__cd.__data, &cp,
|
|
Packit |
6c4009 |
(const unsigned char *) from_end, NULL,
|
|
Packit |
6c4009 |
&dummy, 0, 0));
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
result = cp - (const unsigned char *) from_start;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return result;
|
|
Packit |
6c4009 |
}
|