OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [newlib/] [libc/] [sys/] [linux/] [iconv/] [iconv_charmap.c] - Diff between revs 148 and 158

Only display areas with differences | Details | Blame | View Log

Rev 148 Rev 158
/* Convert using charmaps and possibly iconv().
/* Convert using charmaps and possibly iconv().
   Copyright (C) 2001 Free Software Foundation, Inc.
   Copyright (C) 2001 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   This file is part of the GNU C Library.
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
 
 
   The GNU C Library is free software; you can redistribute it and/or
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   version 2.1 of the License, or (at your option) any later version.
 
 
   The GNU C Library is distributed in the hope that it will be useful,
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   Lesser General Public License for more details.
 
 
   You should have received a copy of the GNU Lesser General Public
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */
   02111-1307 USA.  */
 
 
#include <assert.h>
#include <assert.h>
#include <errno.h>
#include <errno.h>
#include <error.h>
#include <error.h>
#include <fcntl.h>
#include <fcntl.h>
#include <iconv.h>
#include <iconv.h>
#include <libintl.h>
#include <libintl.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <unistd.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/stat.h>
 
 
#include "iconv_prog.h"
#include "iconv_prog.h"
 
 
 
 
/* Prototypes for a few program-wide used functions.  */
/* Prototypes for a few program-wide used functions.  */
extern void *xmalloc (size_t __n);
extern void *xmalloc (size_t __n);
extern void *xcalloc (size_t __n, size_t __s);
extern void *xcalloc (size_t __n, size_t __s);
 
 
 
 
struct convtable
struct convtable
{
{
  int term[256 / 8];
  int term[256 / 8];
  union
  union
  {
  {
    struct convtable *sub;
    struct convtable *sub;
    struct charseq *out;
    struct charseq *out;
  } val[256];
  } val[256];
};
};
 
 
 
 
static inline struct convtable *
static inline struct convtable *
allocate_table (void)
allocate_table (void)
{
{
  return (struct convtable *) xcalloc (1, sizeof (struct convtable));
  return (struct convtable *) xcalloc (1, sizeof (struct convtable));
}
}
 
 
 
 
static inline int
static inline int
is_term (struct convtable *tbl, unsigned int idx)
is_term (struct convtable *tbl, unsigned int idx)
{
{
  return tbl->term[idx / 8] & (1 << (idx % 8));
  return tbl->term[idx / 8] & (1 << (idx % 8));
}
}
 
 
 
 
static inline void
static inline void
clear_term (struct convtable *tbl, unsigned int idx)
clear_term (struct convtable *tbl, unsigned int idx)
{
{
  tbl->term[idx / 8] &= ~(1 << (idx % 8));
  tbl->term[idx / 8] &= ~(1 << (idx % 8));
}
}
 
 
 
 
static inline void
static inline void
set_term (struct convtable *tbl, unsigned int idx)
set_term (struct convtable *tbl, unsigned int idx)
{
{
  tbl->term[idx / 8] |= 1 << (idx % 8);
  tbl->term[idx / 8] |= 1 << (idx % 8);
}
}
 
 
 
 
/* Generate the conversion table.  */
/* Generate the conversion table.  */
static struct convtable *use_from_charmap (struct charmap_t *from_charmap,
static struct convtable *use_from_charmap (struct charmap_t *from_charmap,
                                           const char *to_code);
                                           const char *to_code);
static struct convtable *use_to_charmap (const char *from_code,
static struct convtable *use_to_charmap (const char *from_code,
                                         struct charmap_t *to_charmap);
                                         struct charmap_t *to_charmap);
static struct convtable *use_both_charmaps (struct charmap_t *from_charmap,
static struct convtable *use_both_charmaps (struct charmap_t *from_charmap,
                                            struct charmap_t *to_charmap);
                                            struct charmap_t *to_charmap);
 
 
/* Prototypes for the functions doing the actual work.  */
/* Prototypes for the functions doing the actual work.  */
static int process_block (struct convtable *tbl, char *addr, size_t len,
static int process_block (struct convtable *tbl, char *addr, size_t len,
                          FILE *output);
                          FILE *output);
static int process_fd (struct convtable *tbl, int fd, FILE *output);
static int process_fd (struct convtable *tbl, int fd, FILE *output);
static int process_file (struct convtable *tbl, FILE *input, FILE *output);
static int process_file (struct convtable *tbl, FILE *input, FILE *output);
 
 
 
 
int
int
charmap_conversion (const char *from_code, struct charmap_t *from_charmap,
charmap_conversion (const char *from_code, struct charmap_t *from_charmap,
                    const char *to_code, struct charmap_t *to_charmap,
                    const char *to_code, struct charmap_t *to_charmap,
                    int argc, int remaining, char *argv[], FILE *output)
                    int argc, int remaining, char *argv[], FILE *output)
{
{
  struct convtable *cvtbl;
  struct convtable *cvtbl;
  int status = EXIT_SUCCESS;
  int status = EXIT_SUCCESS;
 
 
  /* We have three different cases to handle:
  /* We have three different cases to handle:
 
 
     - both, from_charmap and to_charmap, are available.  This means we
     - both, from_charmap and to_charmap, are available.  This means we
       can assume that the symbolic names match and use them to create
       can assume that the symbolic names match and use them to create
       the mapping.
       the mapping.
 
 
     - only from_charmap is available.  In this case we can only hope that
     - only from_charmap is available.  In this case we can only hope that
       the symbolic names used are of the <Uxxxx> form in which case we
       the symbolic names used are of the <Uxxxx> form in which case we
       can use a UCS4->"to_code" iconv() conversion for the second step.
       can use a UCS4->"to_code" iconv() conversion for the second step.
 
 
     - only to_charmap is available.  This is similar, only that we would
     - only to_charmap is available.  This is similar, only that we would
       use iconv() for the "to_code"->UCS4 conversion.
       use iconv() for the "to_code"->UCS4 conversion.
 
 
       We first create a table which maps input bytes into output bytes.
       We first create a table which maps input bytes into output bytes.
       Once this is done we can handle all three of the cases above
       Once this is done we can handle all three of the cases above
       equally.  */
       equally.  */
  if (from_charmap != NULL)
  if (from_charmap != NULL)
    {
    {
      if (to_charmap == NULL)
      if (to_charmap == NULL)
        cvtbl = use_from_charmap (from_charmap, to_code);
        cvtbl = use_from_charmap (from_charmap, to_code);
      else
      else
        cvtbl = use_both_charmaps (from_charmap, to_charmap);
        cvtbl = use_both_charmaps (from_charmap, to_charmap);
    }
    }
  else
  else
    {
    {
      assert (to_charmap != NULL);
      assert (to_charmap != NULL);
      cvtbl = use_to_charmap (from_code, to_charmap);
      cvtbl = use_to_charmap (from_code, to_charmap);
    }
    }
 
 
  /* If we couldn't generate a table stop now.  */
  /* If we couldn't generate a table stop now.  */
  if (cvtbl == NULL)
  if (cvtbl == NULL)
    return EXIT_FAILURE;
    return EXIT_FAILURE;
 
 
  /* We can now start the conversion.  */
  /* We can now start the conversion.  */
  if (remaining == argc)
  if (remaining == argc)
    {
    {
      if (process_file (cvtbl, stdin, output) != 0)
      if (process_file (cvtbl, stdin, output) != 0)
        status = EXIT_FAILURE;
        status = EXIT_FAILURE;
    }
    }
  else
  else
    do
    do
      {
      {
        struct stat st;
        struct stat st;
        char *addr;
        char *addr;
        int fd;
        int fd;
 
 
        if (verbose)
        if (verbose)
          printf ("%s:\n", argv[remaining]);
          printf ("%s:\n", argv[remaining]);
        if (strcmp (argv[remaining], "-") == 0)
        if (strcmp (argv[remaining], "-") == 0)
          fd = 0;
          fd = 0;
        else
        else
          {
          {
            fd = open (argv[remaining], O_RDONLY);
            fd = open (argv[remaining], O_RDONLY);
 
 
            if (fd == -1)
            if (fd == -1)
              {
              {
                error (0, errno, _("cannot open input file `%s'"),
                error (0, errno, _("cannot open input file `%s'"),
                       argv[remaining]);
                       argv[remaining]);
                status = EXIT_FAILURE;
                status = EXIT_FAILURE;
                continue;
                continue;
              }
              }
          }
          }
 
 
#ifdef _POSIX_MAPPED_FILES
#ifdef _POSIX_MAPPED_FILES
        /* We have possibilities for reading the input file.  First try
        /* We have possibilities for reading the input file.  First try
           to mmap() it since this will provide the fastest solution.  */
           to mmap() it since this will provide the fastest solution.  */
        if (fstat (fd, &st) == 0
        if (fstat (fd, &st) == 0
            && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
            && ((addr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE,
                              fd, 0)) != MAP_FAILED))
                              fd, 0)) != MAP_FAILED))
          {
          {
            /* Yes, we can use mmap().  The descriptor is not needed
            /* Yes, we can use mmap().  The descriptor is not needed
               anymore.  */
               anymore.  */
            if (close (fd) != 0)
            if (close (fd) != 0)
              error (EXIT_FAILURE, errno,
              error (EXIT_FAILURE, errno,
                     _("error while closing input `%s'"), argv[remaining]);
                     _("error while closing input `%s'"), argv[remaining]);
 
 
            if (process_block (cvtbl, addr, st.st_size, output) < 0)
            if (process_block (cvtbl, addr, st.st_size, output) < 0)
              {
              {
                /* Something went wrong.  */
                /* Something went wrong.  */
                status = EXIT_FAILURE;
                status = EXIT_FAILURE;
 
 
                /* We don't need the input data anymore.  */
                /* We don't need the input data anymore.  */
                munmap ((void *) addr, st.st_size);
                munmap ((void *) addr, st.st_size);
 
 
                /* We cannot go on with producing output since it might
                /* We cannot go on with producing output since it might
                   lead to problem because the last output might leave
                   lead to problem because the last output might leave
                   the output stream in an undefined state.  */
                   the output stream in an undefined state.  */
                break;
                break;
              }
              }
 
 
            /* We don't need the input data anymore.  */
            /* We don't need the input data anymore.  */
            munmap ((void *) addr, st.st_size);
            munmap ((void *) addr, st.st_size);
          }
          }
        else
        else
#endif  /* _POSIX_MAPPED_FILES */
#endif  /* _POSIX_MAPPED_FILES */
          {
          {
            /* Read the file in pieces.  */
            /* Read the file in pieces.  */
            if (process_fd (cvtbl, fd, output) != 0)
            if (process_fd (cvtbl, fd, output) != 0)
              {
              {
                /* Something went wrong.  */
                /* Something went wrong.  */
                status = EXIT_FAILURE;
                status = EXIT_FAILURE;
 
 
                /* We don't need the input file anymore.  */
                /* We don't need the input file anymore.  */
                close (fd);
                close (fd);
 
 
                /* We cannot go on with producing output since it might
                /* We cannot go on with producing output since it might
                   lead to problem because the last output might leave
                   lead to problem because the last output might leave
                   the output stream in an undefined state.  */
                   the output stream in an undefined state.  */
                break;
                break;
              }
              }
 
 
            /* Now close the file.  */
            /* Now close the file.  */
            close (fd);
            close (fd);
          }
          }
      }
      }
    while (++remaining < argc);
    while (++remaining < argc);
 
 
  /* All done.  */
  /* All done.  */
  return status;
  return status;
}
}
 
 
 
 
static void
static void
add_bytes (struct convtable *tbl, struct charseq *in, struct charseq *out)
add_bytes (struct convtable *tbl, struct charseq *in, struct charseq *out)
{
{
  int n = 0;
  int n = 0;
  unsigned int byte;
  unsigned int byte;
 
 
  assert (in->nbytes > 0);
  assert (in->nbytes > 0);
 
 
  byte = ((unsigned char *) in->bytes)[n];
  byte = ((unsigned char *) in->bytes)[n];
  while (n + 1 < in->nbytes)
  while (n + 1 < in->nbytes)
    {
    {
      if (is_term (tbl, byte) || tbl->val[byte].sub == NULL)
      if (is_term (tbl, byte) || tbl->val[byte].sub == NULL)
        {
        {
          /* Note that we simply ignore a definition for a byte sequence
          /* Note that we simply ignore a definition for a byte sequence
             which is also the prefix for a longer one.  */
             which is also the prefix for a longer one.  */
          clear_term (tbl, byte);
          clear_term (tbl, byte);
          tbl->val[byte].sub =
          tbl->val[byte].sub =
            (struct convtable *) xcalloc (1, sizeof (struct convtable));
            (struct convtable *) xcalloc (1, sizeof (struct convtable));
        }
        }
 
 
      tbl = tbl->val[byte].sub;
      tbl = tbl->val[byte].sub;
 
 
      byte = ((unsigned char *) in->bytes)[++n];
      byte = ((unsigned char *) in->bytes)[++n];
    }
    }
 
 
  /* Only add the new sequence if there is none yet and the byte sequence
  /* Only add the new sequence if there is none yet and the byte sequence
     is not part of an even longer one.  */
     is not part of an even longer one.  */
  if (! is_term (tbl, byte) && tbl->val[byte].sub == NULL)
  if (! is_term (tbl, byte) && tbl->val[byte].sub == NULL)
    {
    {
      set_term (tbl, byte);
      set_term (tbl, byte);
      tbl->val[byte].out = out;
      tbl->val[byte].out = out;
    }
    }
}
}
 
 
 
 
static struct convtable *
static struct convtable *
use_from_charmap (struct charmap_t *from_charmap, const char *to_code)
use_from_charmap (struct charmap_t *from_charmap, const char *to_code)
{
{
  /* We iterate over all entries in the from_charmap and for those which
  /* We iterate over all entries in the from_charmap and for those which
     have a known UCS4 representation we use an iconv() call to determine
     have a known UCS4 representation we use an iconv() call to determine
     the mapping to the to_code charset.  */
     the mapping to the to_code charset.  */
  struct convtable *rettbl;
  struct convtable *rettbl;
  iconv_t cd;
  iconv_t cd;
  void *ptr = NULL;
  void *ptr = NULL;
  const void *key;
  const void *key;
  size_t keylen;
  size_t keylen;
  void *data;
  void *data;
 
 
  cd = iconv_open (to_code, "WCHAR_T");
  cd = iconv_open (to_code, "WCHAR_T");
  if (cd == (iconv_t) -1)
  if (cd == (iconv_t) -1)
    /* We cannot do anything.  */
    /* We cannot do anything.  */
    return NULL;
    return NULL;
 
 
  rettbl = allocate_table ();
  rettbl = allocate_table ();
 
 
  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
         >= 0)
         >= 0)
    {
    {
      struct charseq *in = (struct charseq *) data;
      struct charseq *in = (struct charseq *) data;
 
 
      if (in->ucs4 != UNINITIALIZED_CHAR_VALUE)
      if (in->ucs4 != UNINITIALIZED_CHAR_VALUE)
        {
        {
          /* There is a chance.  Try the iconv module.  */
          /* There is a chance.  Try the iconv module.  */
          wchar_t inbuf[1] = { in->ucs4 };
          wchar_t inbuf[1] = { in->ucs4 };
          unsigned char outbuf[64];
          unsigned char outbuf[64];
          char *inptr = (char *) inbuf;
          char *inptr = (char *) inbuf;
          size_t inlen = sizeof (inbuf);
          size_t inlen = sizeof (inbuf);
          char *outptr = (char *) outbuf;
          char *outptr = (char *) outbuf;
          size_t outlen = sizeof (outbuf);
          size_t outlen = sizeof (outbuf);
 
 
          (void) iconv (cd, &inptr, &inlen, &outptr, &outlen);
          (void) iconv (cd, &inptr, &inlen, &outptr, &outlen);
 
 
          if (outptr != (char *) outbuf)
          if (outptr != (char *) outbuf)
            {
            {
              /* We got some output.  Good, use it.  */
              /* We got some output.  Good, use it.  */
              struct charseq *newp;
              struct charseq *newp;
 
 
              outlen = sizeof (outbuf) - outlen;
              outlen = sizeof (outbuf) - outlen;
              assert ((char *) outbuf + outlen == outptr);
              assert ((char *) outbuf + outlen == outptr);
 
 
              newp = (struct charseq *) xmalloc (sizeof (struct charseq)
              newp = (struct charseq *) xmalloc (sizeof (struct charseq)
                                                 + outlen);
                                                 + outlen);
              newp->name = in->name;
              newp->name = in->name;
              newp->ucs4 = in->ucs4;
              newp->ucs4 = in->ucs4;
              newp->nbytes = outlen;
              newp->nbytes = outlen;
              memcpy (newp->bytes, outbuf, outlen);
              memcpy (newp->bytes, outbuf, outlen);
 
 
              add_bytes (rettbl, in, newp);
              add_bytes (rettbl, in, newp);
            }
            }
 
 
          /* Clear any possible state left behind.  */
          /* Clear any possible state left behind.  */
          (void) iconv (cd, NULL, NULL, NULL, NULL);
          (void) iconv (cd, NULL, NULL, NULL, NULL);
        }
        }
    }
    }
 
 
  iconv_close (cd);
  iconv_close (cd);
 
 
  return rettbl;
  return rettbl;
}
}
 
 
 
 
static struct convtable *
static struct convtable *
use_to_charmap (const char *from_code, struct charmap_t *to_charmap)
use_to_charmap (const char *from_code, struct charmap_t *to_charmap)
{
{
  /* We iterate over all entries in the to_charmap and for those which
  /* We iterate over all entries in the to_charmap and for those which
     have a known UCS4 representation we use an iconv() call to determine
     have a known UCS4 representation we use an iconv() call to determine
     the mapping to the from_code charset.  */
     the mapping to the from_code charset.  */
  struct convtable *rettbl;
  struct convtable *rettbl;
  iconv_t cd;
  iconv_t cd;
  void *ptr = NULL;
  void *ptr = NULL;
  const void *key;
  const void *key;
  size_t keylen;
  size_t keylen;
  void *data;
  void *data;
 
 
  /* Note that the conversion we use here is the reverse direction.  Without
  /* Note that the conversion we use here is the reverse direction.  Without
     exhaustive search we cannot figure out which input yields the UCS4
     exhaustive search we cannot figure out which input yields the UCS4
     character we are looking for.  Therefore we determine it the other
     character we are looking for.  Therefore we determine it the other
     way round.  */
     way round.  */
  cd = iconv_open (from_code, "WCHAR_T");
  cd = iconv_open (from_code, "WCHAR_T");
  if (cd == (iconv_t) -1)
  if (cd == (iconv_t) -1)
    /* We cannot do anything.  */
    /* We cannot do anything.  */
    return NULL;
    return NULL;
 
 
  rettbl = allocate_table ();
  rettbl = allocate_table ();
 
 
  while (iterate_table (&to_charmap->char_table, &ptr, &key, &keylen, &data)
  while (iterate_table (&to_charmap->char_table, &ptr, &key, &keylen, &data)
         >= 0)
         >= 0)
    {
    {
      struct charseq *out = (struct charseq *) data;
      struct charseq *out = (struct charseq *) data;
 
 
      if (out->ucs4 != UNINITIALIZED_CHAR_VALUE)
      if (out->ucs4 != UNINITIALIZED_CHAR_VALUE)
        {
        {
          /* There is a chance.  Try the iconv module.  */
          /* There is a chance.  Try the iconv module.  */
          wchar_t inbuf[1] = { out->ucs4 };
          wchar_t inbuf[1] = { out->ucs4 };
          unsigned char outbuf[64];
          unsigned char outbuf[64];
          char *inptr = (char *) inbuf;
          char *inptr = (char *) inbuf;
          size_t inlen = sizeof (inbuf);
          size_t inlen = sizeof (inbuf);
          char *outptr = (char *) outbuf;
          char *outptr = (char *) outbuf;
          size_t outlen = sizeof (outbuf);
          size_t outlen = sizeof (outbuf);
 
 
          (void) iconv (cd, &inptr, &inlen, &outptr, &outlen);
          (void) iconv (cd, &inptr, &inlen, &outptr, &outlen);
 
 
          if (outptr != (char *) outbuf)
          if (outptr != (char *) outbuf)
            {
            {
              /* We got some output.  Good, use it.  */
              /* We got some output.  Good, use it.  */
              struct charseq *newp;
              struct charseq *newp;
 
 
              outlen = sizeof (outbuf) - outlen;
              outlen = sizeof (outbuf) - outlen;
              assert ((char *) outbuf + outlen == outptr);
              assert ((char *) outbuf + outlen == outptr);
 
 
              newp = (struct charseq *) xmalloc (sizeof (struct charseq)
              newp = (struct charseq *) xmalloc (sizeof (struct charseq)
                                                 + outlen);
                                                 + outlen);
              newp->name = out->name;
              newp->name = out->name;
              newp->ucs4 = out->ucs4;
              newp->ucs4 = out->ucs4;
              newp->nbytes = outlen;
              newp->nbytes = outlen;
              memcpy (newp->bytes, outbuf, outlen);
              memcpy (newp->bytes, outbuf, outlen);
 
 
              add_bytes (rettbl, newp, out);
              add_bytes (rettbl, newp, out);
            }
            }
 
 
          /* Clear any possible state left behind.  */
          /* Clear any possible state left behind.  */
          (void) iconv (cd, NULL, NULL, NULL, NULL);
          (void) iconv (cd, NULL, NULL, NULL, NULL);
        }
        }
    }
    }
 
 
  iconv_close (cd);
  iconv_close (cd);
 
 
  return rettbl;
  return rettbl;
}
}
 
 
 
 
static struct convtable *
static struct convtable *
use_both_charmaps (struct charmap_t *from_charmap,
use_both_charmaps (struct charmap_t *from_charmap,
                   struct charmap_t *to_charmap)
                   struct charmap_t *to_charmap)
{
{
  /* In this case we iterate over all the entries in the from_charmap,
  /* In this case we iterate over all the entries in the from_charmap,
     determine the internal name, and find an appropriate entry in the
     determine the internal name, and find an appropriate entry in the
     to_charmap (if it exists).  */
     to_charmap (if it exists).  */
  struct convtable *rettbl = allocate_table ();
  struct convtable *rettbl = allocate_table ();
  void *ptr = NULL;
  void *ptr = NULL;
  const void *key;
  const void *key;
  size_t keylen;
  size_t keylen;
  void *data;
  void *data;
 
 
  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
  while (iterate_table (&from_charmap->char_table, &ptr, &key, &keylen, &data)
         >= 0)
         >= 0)
    {
    {
      struct charseq *in = (struct charseq *) data;
      struct charseq *in = (struct charseq *) data;
      struct charseq *out = charmap_find_value (to_charmap, key, keylen);
      struct charseq *out = charmap_find_value (to_charmap, key, keylen);
 
 
      if (out != NULL)
      if (out != NULL)
        add_bytes (rettbl, in, out);
        add_bytes (rettbl, in, out);
    }
    }
 
 
  return rettbl;
  return rettbl;
}
}
 
 
 
 
static int
static int
process_block (struct convtable *tbl, char *addr, size_t len, FILE *output)
process_block (struct convtable *tbl, char *addr, size_t len, FILE *output)
{
{
  size_t n = 0;
  size_t n = 0;
 
 
  while (n < len)
  while (n < len)
    {
    {
      struct convtable *cur = tbl;
      struct convtable *cur = tbl;
      unsigned char *curp = (unsigned char *) addr;
      unsigned char *curp = (unsigned char *) addr;
      unsigned int byte = *curp;
      unsigned int byte = *curp;
      int cnt;
      int cnt;
      struct charseq *out;
      struct charseq *out;
 
 
      while (! is_term (cur, byte))
      while (! is_term (cur, byte))
        if (cur->val[byte].sub == NULL)
        if (cur->val[byte].sub == NULL)
          {
          {
            /* This is a invalid sequence.  Skip the first byte if we are
            /* This is a invalid sequence.  Skip the first byte if we are
               ignoring errors.  Otherwise punt.  */
               ignoring errors.  Otherwise punt.  */
            if (! omit_invalid)
            if (! omit_invalid)
              {
              {
                error (0, 0, _("illegal input sequence at position %Zd"), n);
                error (0, 0, _("illegal input sequence at position %Zd"), n);
                return -1;
                return -1;
              }
              }
 
 
            n -= curp - (unsigned char *) addr;
            n -= curp - (unsigned char *) addr;
 
 
            byte = *(curp = (unsigned char *) ++addr);
            byte = *(curp = (unsigned char *) ++addr);
            if (++n >= len)
            if (++n >= len)
              /* All converted.  */
              /* All converted.  */
              return 0;
              return 0;
 
 
            cur = tbl;
            cur = tbl;
          }
          }
        else
        else
          {
          {
            cur = cur->val[byte].sub;
            cur = cur->val[byte].sub;
 
 
            if (++n >= len)
            if (++n >= len)
              {
              {
                error (0, 0, _("\
                error (0, 0, _("\
incomplete character or shift sequence at end of buffer"));
incomplete character or shift sequence at end of buffer"));
                return -1;
                return -1;
              }
              }
 
 
            byte = *++curp;
            byte = *++curp;
          }
          }
 
 
      /* We found a final byte.  Write the output bytes.  */
      /* We found a final byte.  Write the output bytes.  */
      out = cur->val[byte].out;
      out = cur->val[byte].out;
      for (cnt = 0; cnt < out->nbytes; ++cnt)
      for (cnt = 0; cnt < out->nbytes; ++cnt)
        fputc_unlocked (out->bytes[cnt], output);
        fputc_unlocked (out->bytes[cnt], output);
 
 
      addr = (char *) curp + 1;
      addr = (char *) curp + 1;
      ++n;
      ++n;
    }
    }
 
 
  return 0;
  return 0;
}
}
 
 
 
 
static int
static int
process_fd (struct convtable *tbl, int fd, FILE *output)
process_fd (struct convtable *tbl, int fd, FILE *output)
{
{
  /* we have a problem with reading from a desriptor since we must not
  /* we have a problem with reading from a desriptor since we must not
     provide the iconv() function an incomplete character or shift
     provide the iconv() function an incomplete character or shift
     sequence at the end of the buffer.  Since we have to deal with
     sequence at the end of the buffer.  Since we have to deal with
     arbitrary encodings we must read the whole text in a buffer and
     arbitrary encodings we must read the whole text in a buffer and
     process it in one step.  */
     process it in one step.  */
  static char *inbuf = NULL;
  static char *inbuf = NULL;
  static size_t maxlen = 0;
  static size_t maxlen = 0;
  char *inptr = NULL;
  char *inptr = NULL;
  size_t actlen = 0;
  size_t actlen = 0;
 
 
  while (actlen < maxlen)
  while (actlen < maxlen)
    {
    {
      ssize_t n = read (fd, inptr, maxlen - actlen);
      ssize_t n = read (fd, inptr, maxlen - actlen);
 
 
      if (n == 0)
      if (n == 0)
        /* No more text to read.  */
        /* No more text to read.  */
        break;
        break;
 
 
      if (n == -1)
      if (n == -1)
        {
        {
          /* Error while reading.  */
          /* Error while reading.  */
          error (0, errno, _("error while reading the input"));
          error (0, errno, _("error while reading the input"));
          return -1;
          return -1;
        }
        }
 
 
      inptr += n;
      inptr += n;
      actlen += n;
      actlen += n;
    }
    }
 
 
  if (actlen == maxlen)
  if (actlen == maxlen)
    while (1)
    while (1)
      {
      {
        ssize_t n;
        ssize_t n;
 
 
        /* Increase the buffer.  */
        /* Increase the buffer.  */
        maxlen += 32768;
        maxlen += 32768;
        inbuf = realloc (inbuf, maxlen);
        inbuf = realloc (inbuf, maxlen);
        if (inbuf == NULL)
        if (inbuf == NULL)
          error (0, errno, _("unable to allocate buffer for input"));
          error (0, errno, _("unable to allocate buffer for input"));
        inptr = inbuf + actlen;
        inptr = inbuf + actlen;
 
 
        do
        do
          {
          {
            n = read (fd, inptr, maxlen - actlen);
            n = read (fd, inptr, maxlen - actlen);
 
 
            if (n == 0)
            if (n == 0)
              /* No more text to read.  */
              /* No more text to read.  */
              break;
              break;
 
 
            if (n == -1)
            if (n == -1)
              {
              {
                /* Error while reading.  */
                /* Error while reading.  */
                error (0, errno, _("error while reading the input"));
                error (0, errno, _("error while reading the input"));
                return -1;
                return -1;
              }
              }
 
 
            inptr += n;
            inptr += n;
            actlen += n;
            actlen += n;
          }
          }
        while (actlen < maxlen);
        while (actlen < maxlen);
 
 
        if (n == 0)
        if (n == 0)
          /* Break again so we leave both loops.  */
          /* Break again so we leave both loops.  */
          break;
          break;
      }
      }
 
 
  /* Now we have all the input in the buffer.  Process it in one run.  */
  /* Now we have all the input in the buffer.  Process it in one run.  */
  return process_block (tbl, inbuf, actlen, output);
  return process_block (tbl, inbuf, actlen, output);
}
}
 
 
 
 
static int
static int
process_file (struct convtable *tbl, FILE *input, FILE *output)
process_file (struct convtable *tbl, FILE *input, FILE *output)
{
{
  /* This should be safe since we use this function only for `stdin' and
  /* This should be safe since we use this function only for `stdin' and
     we haven't read anything so far.  */
     we haven't read anything so far.  */
  return process_fd (tbl, fileno (input), output);
  return process_fd (tbl, fileno (input), output);
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.