Blame iconvdata/unicode.c

Packit 6c4009
/* Conversion module for Unicode
Packit 6c4009
   Copyright (C) 1999-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>, 1999.
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 <byteswap.h>
Packit 6c4009
#include <dlfcn.h>
Packit 6c4009
#include <gconv.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
/* This is the Byte Order Mark character (BOM).  */
Packit 6c4009
#define BOM	0xfeff
Packit 6c4009
/* And in the other endian format.  */
Packit 6c4009
#define BOM_OE	0xfffe
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Definitions used in the body of the `gconv' function.  */
Packit 6c4009
#define FROM_LOOP		from_unicode_loop
Packit 6c4009
#define TO_LOOP			to_unicode_loop
Packit 6c4009
#define DEFINE_INIT		0
Packit 6c4009
#define DEFINE_FINI		0
Packit 6c4009
#define MIN_NEEDED_FROM		2
Packit 6c4009
#define MIN_NEEDED_TO		4
Packit 6c4009
#define ONE_DIRECTION		0
Packit 6c4009
#define FROM_DIRECTION		(dir == from_unicode)
Packit 6c4009
#define PREPARE_LOOP \
Packit 6c4009
  enum direction dir = ((struct unicode_data *) step->__data)->dir;	      \
Packit 6c4009
  int swap;								      \
Packit 6c4009
  if (FROM_DIRECTION)							      \
Packit 6c4009
    {									      \
Packit 6c4009
      if (data->__invocation_counter == 0)				      \
Packit 6c4009
	{								      \
Packit 6c4009
	  /* We have to find out which byte order the file is encoded in.  */ \
Packit 6c4009
	  if (inptr + 2 > inend)					      \
Packit 6c4009
	    return (inptr == inend					      \
Packit 6c4009
		    ? __GCONV_EMPTY_INPUT : __GCONV_INCOMPLETE_INPUT);	      \
Packit 6c4009
									      \
Packit 6c4009
	  if (get16u (inptr) == BOM)					      \
Packit 6c4009
	    /* Simply ignore the BOM character.  */			      \
Packit 6c4009
	    *inptrp = inptr += 2;					      \
Packit 6c4009
	  else if (get16u (inptr) == BOM_OE)				      \
Packit 6c4009
	    {								      \
Packit 6c4009
	      data->__flags |= __GCONV_SWAP;				      \
Packit 6c4009
	      *inptrp = inptr += 2;					      \
Packit 6c4009
	    }								      \
Packit 6c4009
	}								      \
Packit 6c4009
    }									      \
Packit 6c4009
  else if (!data->__internal_use && data->__invocation_counter == 0)	      \
Packit 6c4009
    {									      \
Packit 6c4009
      /* Emit the Byte Order Mark.  */					      \
Packit 6c4009
      if (__glibc_unlikely (outbuf + 2 > outend))			      \
Packit 6c4009
	return __GCONV_FULL_OUTPUT;					      \
Packit 6c4009
									      \
Packit 6c4009
      put16u (outbuf, BOM);						      \
Packit 6c4009
      outbuf += 2;							      \
Packit 6c4009
    }									      \
Packit 6c4009
  swap = data->__flags & __GCONV_SWAP;
Packit 6c4009
#define EXTRA_LOOP_ARGS		, swap
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Direction of the transformation.  */
Packit 6c4009
enum direction
Packit 6c4009
{
Packit 6c4009
  illegal_dir,
Packit 6c4009
  to_unicode,
Packit 6c4009
  from_unicode
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
struct unicode_data
Packit 6c4009
{
Packit 6c4009
  enum direction dir;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
Packit 6c4009
extern int gconv_init (struct __gconv_step *step);
Packit 6c4009
int
Packit 6c4009
gconv_init (struct __gconv_step *step)
Packit 6c4009
{
Packit 6c4009
  /* Determine which direction.  */
Packit 6c4009
  struct unicode_data *new_data;
Packit 6c4009
  enum direction dir = illegal_dir;
Packit 6c4009
  int result;
Packit 6c4009
Packit 6c4009
  if (strcmp (step->__from_name, "UNICODE//") == 0)
Packit 6c4009
    dir = from_unicode;
Packit 6c4009
  else
Packit 6c4009
    dir = to_unicode;
Packit 6c4009
Packit 6c4009
  new_data = (struct unicode_data *) malloc (sizeof (struct unicode_data));
Packit 6c4009
Packit 6c4009
  result = __GCONV_NOMEM;
Packit 6c4009
  if (new_data != NULL)
Packit 6c4009
    {
Packit 6c4009
      new_data->dir = dir;
Packit 6c4009
      step->__data = new_data;
Packit 6c4009
Packit 6c4009
      if (dir == from_unicode)
Packit 6c4009
	{
Packit 6c4009
	  step->__min_needed_from = MIN_NEEDED_FROM;
Packit 6c4009
	  step->__max_needed_from = MIN_NEEDED_FROM;
Packit 6c4009
	  step->__min_needed_to = MIN_NEEDED_TO;
Packit 6c4009
	  step->__max_needed_to = MIN_NEEDED_TO;
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  step->__min_needed_from = MIN_NEEDED_TO;
Packit 6c4009
	  step->__max_needed_from = MIN_NEEDED_TO;
Packit 6c4009
	  step->__min_needed_to = MIN_NEEDED_FROM;
Packit 6c4009
	  step->__max_needed_to = MIN_NEEDED_FROM;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      step->__stateful = 0;
Packit 6c4009
Packit 6c4009
      result = __GCONV_OK;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
extern void gconv_end (struct __gconv_step *data);
Packit 6c4009
void
Packit 6c4009
gconv_end (struct __gconv_step *data)
Packit 6c4009
{
Packit 6c4009
  free (data->__data);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Convert from the internal (UCS4-like) format to UCS2.  */
Packit 6c4009
#define MIN_NEEDED_INPUT	MIN_NEEDED_TO
Packit 6c4009
#define MIN_NEEDED_OUTPUT	MIN_NEEDED_FROM
Packit 6c4009
#define LOOPFCT			TO_LOOP
Packit 6c4009
#define BODY \
Packit 6c4009
  {									      \
Packit 6c4009
    uint32_t c = get32 (inptr);						      \
Packit 6c4009
									      \
Packit 6c4009
    if (__glibc_unlikely (c >= 0x10000))				      \
Packit 6c4009
      {									      \
Packit 6c4009
	UNICODE_TAG_HANDLER (c, 4);					      \
Packit 6c4009
	STANDARD_TO_LOOP_ERR_HANDLER (4);				      \
Packit 6c4009
      }									      \
Packit 6c4009
    else if (__glibc_unlikely (c >= 0xd800 && c < 0xe000))		      \
Packit 6c4009
      {									      \
Packit 6c4009
	/* Surrogate characters in UCS-4 input are not valid.		      \
Packit 6c4009
	   We must catch this, because the UCS-2 output might be	      \
Packit 6c4009
	   interpreted as UTF-16 by other programs.  If we let		      \
Packit 6c4009
	   surrogates pass through, attackers could make a security	      \
Packit 6c4009
	   hole exploit by synthesizing any desired plane 1-16		      \
Packit 6c4009
	   character.  */						      \
Packit 6c4009
	result = __GCONV_ILLEGAL_INPUT;					      \
Packit 6c4009
	if (! ignore_errors_p ())					      \
Packit 6c4009
	  break;							      \
Packit 6c4009
	inptr += 4;							      \
Packit 6c4009
	++*irreversible;						      \
Packit 6c4009
	continue;							      \
Packit 6c4009
      }									      \
Packit 6c4009
    else								      \
Packit 6c4009
      {									      \
Packit 6c4009
	put16 (outptr, c);						      \
Packit 6c4009
	outptr += 2;							      \
Packit 6c4009
      }									      \
Packit 6c4009
									      \
Packit 6c4009
    inptr += 4;								      \
Packit 6c4009
  }
Packit 6c4009
#define LOOP_NEED_FLAGS
Packit 6c4009
#define EXTRA_LOOP_DECLS \
Packit 6c4009
	, int swap
Packit 6c4009
#include <iconv/loop.c>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Convert from UCS2 to the internal (UCS4-like) format.  */
Packit 6c4009
#define MIN_NEEDED_INPUT	MIN_NEEDED_FROM
Packit 6c4009
#define MIN_NEEDED_OUTPUT	MIN_NEEDED_TO
Packit 6c4009
#define LOOPFCT			FROM_LOOP
Packit 6c4009
#define BODY \
Packit 6c4009
  {									      \
Packit 6c4009
    uint16_t u1 = get16 (inptr);					      \
Packit 6c4009
									      \
Packit 6c4009
    if (swap)								      \
Packit 6c4009
      u1 = bswap_16 (u1);						      \
Packit 6c4009
									      \
Packit 6c4009
    if (__glibc_unlikely (u1 >= 0xd800 && u1 < 0xe000))			      \
Packit 6c4009
      {									      \
Packit 6c4009
	/* Surrogate characters in UCS-2 input are not valid.  Reject	      \
Packit 6c4009
	   them.  (Catching this here is not security relevant.)  */	      \
Packit 6c4009
	STANDARD_FROM_LOOP_ERR_HANDLER (2);				      \
Packit 6c4009
      }									      \
Packit 6c4009
									      \
Packit 6c4009
    put32 (outptr, u1);							      \
Packit 6c4009
									      \
Packit 6c4009
    inptr += 2;								      \
Packit 6c4009
    outptr += 4;							      \
Packit 6c4009
  }
Packit 6c4009
#define LOOP_NEED_FLAGS
Packit 6c4009
#define EXTRA_LOOP_DECLS \
Packit 6c4009
	, int swap
Packit 6c4009
#include <iconv/loop.c>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Now define the toplevel functions.  */
Packit 6c4009
#include <iconv/skeleton.c>