Blame iconvdata/jis0212.h

Packit 6c4009
/* Access functions for JISX0212 conversion.
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
#ifndef _JIS0212_H
Packit 6c4009
#define _JIS0212_H	1
Packit 6c4009
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <gconv.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Struct for table with indeces in mapping table.  */
Packit 6c4009
struct jisx0212_idx
Packit 6c4009
{
Packit 6c4009
  uint16_t start;
Packit 6c4009
  uint16_t end;
Packit 6c4009
  uint16_t idx;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Conversion table.  */
Packit 6c4009
extern const struct jisx0212_idx __jisx0212_to_ucs_idx[];
Packit 6c4009
extern const uint16_t __jisx0212_to_ucs[];
Packit 6c4009
Packit 6c4009
extern const struct jisx0212_idx __jisx0212_from_ucs_idx[];
Packit 6c4009
extern const char __jisx0212_from_ucs[][2];
Packit 6c4009
Packit 6c4009
Packit 6c4009
static inline uint32_t
Packit 6c4009
__attribute ((always_inline))
Packit 6c4009
jisx0212_to_ucs4 (const unsigned char **s, size_t avail, unsigned char offset)
Packit 6c4009
{
Packit 6c4009
  const struct jisx0212_idx *rp = __jisx0212_to_ucs_idx;
Packit 6c4009
  unsigned char ch = *(*s);
Packit 6c4009
  unsigned char ch2;
Packit 6c4009
  uint32_t wch = 0;
Packit 6c4009
  int idx;
Packit 6c4009
Packit 6c4009
  if (ch < offset || (ch - offset) < 0x22 || (ch - offset) > 0x6d)
Packit 6c4009
    return __UNKNOWN_10646_CHAR;
Packit 6c4009
Packit 6c4009
  if (avail < 2)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  ch2 = (*s)[1];
Packit 6c4009
  if (ch2 < offset || (ch2 - offset) <= 0x20 || (ch2 - offset) >= 0x7f)
Packit 6c4009
    return __UNKNOWN_10646_CHAR;
Packit 6c4009
Packit 6c4009
  idx = (ch - offset - 0x21) * 94 + (ch2 - offset - 0x21);
Packit 6c4009
Packit 6c4009
  while (idx > rp->end)
Packit 6c4009
    ++rp;
Packit 6c4009
  if (idx >= rp->start)
Packit 6c4009
    wch = __jisx0212_to_ucs[rp->idx + idx - rp->start];
Packit 6c4009
Packit 6c4009
  if (wch != L'\0')
Packit 6c4009
    (*s) += 2;
Packit 6c4009
  else
Packit 6c4009
    wch = __UNKNOWN_10646_CHAR;
Packit 6c4009
Packit 6c4009
  return wch;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static inline size_t
Packit 6c4009
__attribute ((always_inline))
Packit 6c4009
ucs4_to_jisx0212 (uint32_t wch, unsigned char *s, size_t avail)
Packit 6c4009
{
Packit 6c4009
  const struct jisx0212_idx *rp = __jisx0212_from_ucs_idx;
Packit 6c4009
  unsigned int ch = (unsigned int) wch;
Packit 6c4009
  const char *cp;
Packit 6c4009
Packit 6c4009
  if (ch >= 0xffff)
Packit 6c4009
    return __UNKNOWN_10646_CHAR;
Packit 6c4009
  while (ch > rp->end)
Packit 6c4009
    ++rp;
Packit 6c4009
  if (ch >= rp->start)
Packit 6c4009
    cp = __jisx0212_from_ucs[rp->idx + ch - rp->start];
Packit 6c4009
  else
Packit 6c4009
    return __UNKNOWN_10646_CHAR;
Packit 6c4009
Packit 6c4009
  if (cp[0] == '\0')
Packit 6c4009
    return __UNKNOWN_10646_CHAR;
Packit 6c4009
Packit 6c4009
  s[0] = cp[0];
Packit 6c4009
  assert (cp[1] != '\0');
Packit 6c4009
  if (avail < 2)
Packit 6c4009
    return 0;
Packit 6c4009
Packit 6c4009
  s[1] = cp[1];
Packit 6c4009
  return 2;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#endif /* jis0212.h */