Update.
[platform/upstream/glibc.git] / iconvdata / iso-2022-kr.c
index a0c213c..9846d92 100644 (file)
@@ -1,23 +1,24 @@
 /* Conversion module for ISO-2022-KR.
-   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
 
    The GNU C Library is free software; you can redistribute it and/or
-   modify it under the terms of the GNU Library General Public License as
-   published by the Free Software Foundation; either version 2 of the
-   License, or (at your option) any later version.
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   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,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-   Library General Public License for more details.
+   Lesser General Public License for more details.
 
-   You should have received a copy of the GNU Library General Public
-   License along with the GNU C Library; see the file COPYING.LIB.  If not,
-   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-   Boston, MA 02111-1307, USA.  */
+   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
+   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   02111-1307 USA.  */
 
+#include <dlfcn.h>
 #include <gconv.h>
 #include <stdint.h>
 #include <string.h>
 #define MAX_NEEDED_TO          4
 #define PREPARE_LOOP \
   int save_set;                                                                      \
-  int *setp = &data->statep->count;                                          \
-  if (!FROM_DIRECTION && !data->internal_use && data->invocation_counter == 0)\
+  int *setp = &data->__statep->__count;                                              \
+  if (!FROM_DIRECTION && !data->__internal_use                               \
+      && data->__invocation_counter == 0)                                    \
     {                                                                        \
       /* Emit the designator sequence.  */                                   \
       if (outbuf + 4 > outend)                                               \
-       return GCONV_FULL_OUTPUT;                                             \
+       return __GCONV_FULL_OUTPUT;                                           \
                                                                              \
       *outbuf++ = ESC;                                                       \
       *outbuf++ = '$';                                                       \
@@ -64,7 +66,7 @@
 enum
 {
   ASCII_set = 0,
-  KSC5601_set
+  KSC5601_set = 8
 };
 
 
@@ -72,27 +74,27 @@ enum
    the output state to the initial state.  This has to be done during the
    flushing.  */
 #define EMIT_SHIFT_TO_INIT \
-  if (data->statep->count != ASCII_set)                                              \
+  if (data->__statep->__count != ASCII_set)                                  \
     {                                                                        \
       if (FROM_DIRECTION)                                                    \
-       /* It's easy, we don't have to emit anything, we just reset the       \
-          state for the input.  */                                           \
-       data->statep->count = ASCII_set;                                      \
+       {                                                                     \
+         /* It's easy, we don't have to emit anything, we just reset the     \
+            state for the input.  */                                         \
+         data->__statep->__count &= 7;                                       \
+         data->__statep->__count |= ASCII_set;                               \
+       }                                                                     \
       else                                                                   \
        {                                                                     \
-         unsigned char *outbuf = data->outbuf;                               \
-                                                                             \
          /* We are not in the initial state.  To switch back we have         \
             to emit `SI'.  */                                                \
-         if (outbuf == data->outbufend)                                      \
+         if (__builtin_expect (outbuf == outend, 0))                         \
            /* We don't have enough room in the output buffer.  */            \
-           status = GCONV_FULL_OUTPUT;                                       \
+           status = __GCONV_FULL_OUTPUT;                                     \
          else                                                                \
            {                                                                 \
              /* Write out the shift sequence.  */                            \
              *outbuf++ = SI;                                                 \
-             data->outbuf = outbuf;                                          \
-             data->statep->count = ASCII_set;                                \
+             data->__statep->__count = ASCII_set;                            \
            }                                                                 \
        }                                                                     \
     }
@@ -117,26 +119,34 @@ enum
     uint32_t ch = *inptr;                                                    \
                                                                              \
     /* This is a 7bit character set, disallow all 8bit characters.  */       \
-    if (ch > 0x7f)                                                           \
+    if (__builtin_expect (ch > 0x7f, 0))                                     \
       {                                                                              \
-       result = GCONV_ILLEGAL_INPUT;                                         \
-       break;                                                                \
+       if (! ignore_errors_p ())                                             \
+         {                                                                   \
+           result = __GCONV_ILLEGAL_INPUT;                                   \
+           break;                                                            \
+         }                                                                   \
+                                                                             \
+       ++inptr;                                                              \
+       ++*irreversible;                                                      \
+       continue;                                                             \
       }                                                                              \
                                                                              \
     /* Recognize escape sequences.  */                                       \
-    if (ch == ESC)                                                           \
+    if (__builtin_expect (ch, 0) == ESC)                                     \
       {                                                                              \
        /* We don't really have to handle escape sequences since all the      \
           switching is done using the SI and SO bytes.  But we have to       \
           recognize `Esc $ ) C' since this is a kind of flag for this        \
           encoding.  We simply ignore it.  */                                \
-       if (inptr + 1 > inend                                                 \
+       if (__builtin_expect (inptr + 1 > inend, 0)                           \
            || (inptr[1] == '$'                                               \
-               && (inptr + 2 > inend                                         \
-                   || (inptr[2] == ')' && inptr + 3 > inend))))              \
+               && (__builtin_expect (inptr + 2 > inend, 0)                   \
+                   || (inptr[2] == ')'                                       \
+                       && __builtin_expect (inptr + 3 > inend, 0)))))        \
                                                                              \
          {                                                                   \
-           result = GCONV_EMPTY_INPUT;                                       \
+           result = __GCONV_INCOMPLETE_INPUT;                                \
            break;                                                            \
          }                                                                   \
        if (inptr[1] == '$' && inptr[2] == ')' && inptr[3] == 'C')            \
@@ -146,14 +156,14 @@ enum
            continue;                                                         \
          }                                                                   \
       }                                                                              \
-    else if (ch == SO)                                                       \
+    else if (__builtin_expect (ch, 0) == SO)                                 \
       {                                                                              \
        /* Switch to use KSC.  */                                             \
        ++inptr;                                                              \
        set = KSC5601_set;                                                    \
        continue;                                                             \
       }                                                                              \
-    else if (ch == SI)                                                       \
+    else if (__builtin_expect (ch, 0) == SI)                                 \
       {                                                                              \
        /* Switch to use ASCII.  */                                           \
        ++inptr;                                                              \
@@ -163,11 +173,6 @@ enum
                                                                              \
     if (set == ASCII_set)                                                    \
       {                                                                              \
-       if (ch >= 0x80)                                                       \
-         {                                                                   \
-           result = GCONV_ILLEGAL_INPUT;                                     \
-           break;                                                            \
-         }                                                                   \
        /* Almost done, just advance the input pointer.  */                   \
        ++inptr;                                                              \
       }                                                                              \
@@ -176,23 +181,32 @@ enum
        assert (set == KSC5601_set);                                          \
                                                                              \
        /* Use the KSC 5601 table.  */                                        \
-       ch = ksc5601_to_ucs4 (&inptr,                                         \
-                             NEED_LENGTH_TEST ? inend - inptr : 2, 0);       \
+       ch = ksc5601_to_ucs4 (&inptr, inend - inptr, 0);                      \
                                                                              \
-       if (NEED_LENGTH_TEST && ch == 0)                                      \
+       if (__builtin_expect (ch, 1) == 0)                                    \
          {                                                                   \
-           result = GCONV_EMPTY_INPUT;                                       \
+           result = __GCONV_INCOMPLETE_INPUT;                                \
            break;                                                            \
          }                                                                   \
-       else if (ch == UNKNOWN_10646_CHAR)                                    \
+       else if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR)            \
          {                                                                   \
-           result = GCONV_ILLEGAL_INPUT;                                     \
-           break;                                                            \
+           if (! ignore_errors_p ())                                         \
+             {                                                               \
+               /* This is an illegal character.  */                          \
+               result = __GCONV_ILLEGAL_INPUT;                               \
+               break;                                                        \
+             }                                                               \
+                                                                             \
+           ++*irreversible;                                                  \
+           ++inptr;                                                          \
+           continue;                                                         \
          }                                                                   \
       }                                                                              \
                                                                              \
-    *((uint32_t *) outptr)++ = ch;                                           \
+    put32 (outptr, ch);                                                              \
+    outptr += 4;                                                             \
   }
+#define LOOP_NEED_FLAGS
 #define EXTRA_LOOP_DECLS       , int *setp
 #define INIT_PARAMS            int set = *setp
 #define UPDATE_PARAMS          *setp = set
@@ -207,9 +221,8 @@ enum
 #define BODY \
   {                                                                          \
     uint32_t ch;                                                             \
-    size_t written = 0;                                                              \
                                                                              \
-    ch = *((uint32_t *) inptr);                                                      \
+    ch = get32 (inptr);                                                              \
                                                                              \
     /* First see whether we can write the character using the currently              \
        selected character set.  */                                           \
@@ -219,50 +232,55 @@ enum
          {                                                                   \
            *outptr++ = SI;                                                   \
            set = ASCII_set;                                                  \
-           if (NEED_LENGTH_TEST && outptr == outend)                         \
+           if (__builtin_expect (outptr == outend, 0))                       \
              {                                                               \
-               result = GCONV_FULL_OUTPUT;                                   \
+               result = __GCONV_FULL_OUTPUT;                                 \
                break;                                                        \
              }                                                               \
          }                                                                   \
                                                                              \
        *outptr++ = ch;                                                       \
-       written = 1;                                                          \
       }                                                                              \
     else                                                                     \
       {                                                                              \
        char buf[2];                                                          \
+       size_t written;                                                       \
                                                                              \
        written = ucs4_to_ksc5601 (ch, buf, 2);                               \
                                                                              \
-       if (written == UNKNOWN_10646_CHAR)                                    \
+       if (__builtin_expect (written, 0) == __UNKNOWN_10646_CHAR)            \
          {                                                                   \
+           UNICODE_TAG_HANDLER (ch, 4);                                      \
+                                                                             \
            /* Illegal character.  */                                         \
-           result = GCONV_ILLEGAL_INPUT;                                     \
-           break;                                                            \
+           STANDARD_ERR_HANDLER (4);                                         \
          }                                                                   \
-       assert (written == 2);                                                \
-                                                                             \
-       /* We use KSC 5601.  */                                               \
-       if (set != KSC5601_set)                                               \
+       else                                                                  \
          {                                                                   \
-           *outptr++ = SO;                                                   \
-           set = KSC5601_set;                                                \
-         }                                                                   \
+           assert (written == 2);                                            \
                                                                              \
-       if (NEED_LENGTH_TEST && outptr + 2 > outend)                          \
-         {                                                                   \
-           result = GCONV_FULL_OUTPUT;                                       \
-           break;                                                            \
-         }                                                                   \
+           /* We use KSC 5601.  */                                           \
+           if (set != KSC5601_set)                                           \
+             {                                                               \
+               *outptr++ = SO;                                               \
+               set = KSC5601_set;                                            \
+             }                                                               \
                                                                              \
-       *outptr++ = buf[0];                                                   \
-       *outptr++ = buf[1];                                                   \
+           if (__builtin_expect (outptr + 2 > outend, 0))                    \
+             {                                                               \
+               result = __GCONV_FULL_OUTPUT;                                 \
+               break;                                                        \
+             }                                                               \
+                                                                             \
+           *outptr++ = buf[0];                                               \
+           *outptr++ = buf[1];                                               \
+         }                                                                   \
       }                                                                              \
                                                                              \
     /* Now that we wrote the output increment the input pointer.  */         \
     inptr += 4;                                                                      \
   }
+#define LOOP_NEED_FLAGS
 #define EXTRA_LOOP_DECLS       , int *setp
 #define INIT_PARAMS            int set = *setp
 #define UPDATE_PARAMS          *setp = set