]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - iconvdata/ibm939.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / iconvdata / ibm939.c
index b40c486540d771e8ced6e03871854c194717f204..e158ebe9d46f3454bd7c4c531e1f7c56f4ae9ec7 100644 (file)
@@ -1,5 +1,5 @@
 /* Conversion to and from IBM939.
-   Copyright (C) 2000-2014 Free Software Foundation, Inc.
+   Copyright (C) 2000-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Masahide Washizawa <washi@yamato.ibm.co.jp>, 2000.
 
 
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
+   <https://www.gnu.org/licenses/>.  */
+
+/* IBM939 is designed for the representation of Japanese Latin/Kanji
+   using a stateful EBCDIC encoding scheme.  It is also known as
+   CCSID 939 or CP939. See:
+   https://www-01.ibm.com/software/globalization/ccsid/ccsid939.html */
 
 #include <dlfcn.h>
 #include <stdint.h>
@@ -31,6 +36,7 @@
 #define CHARSET_NAME   "IBM939//"
 #define FROM_LOOP      from_ibm939
 #define TO_LOOP                to_ibm939
+#define ONE_DIRECTION                  0
 #define FROM_LOOP_MIN_NEEDED_FROM      1
 #define FROM_LOOP_MAX_NEEDED_FROM      2
 #define FROM_LOOP_MIN_NEEDED_TO                4
@@ -61,7 +67,7 @@
        {                                                                     \
          /* We are not in the initial state.  To switch back we have         \
             to emit `SI'.  */                                                \
-         if (__builtin_expect (outbuf >= outend, 0))                         \
+         if (__glibc_unlikely (outbuf >= outend))                            \
            /* We don't have enough room in the output buffer.  */            \
            status = __GCONV_FULL_OUTPUT;                                     \
          else                                                                \
@@ -103,24 +109,14 @@ enum
                                                                              \
     if (__builtin_expect (ch, 0) == SO)                                              \
       {                                                                              \
-       /* Shift OUT, change to DBCS converter.  */                           \
-       if (curcs == db)                                                      \
-         {                                                                   \
-           result = __GCONV_ILLEGAL_INPUT;                                   \
-           break;                                                            \
-         }                                                                   \
+       /* Shift OUT, change to DBCS converter (redundant escape okay).  */   \
        curcs = db;                                                           \
        ++inptr;                                                              \
        continue;                                                             \
       }                                                                              \
     else if (__builtin_expect (ch, 0) == SI)                                 \
       {                                                                              \
-       /* Shift IN, change to SBCS converter.  */                            \
-       if (curcs == sb)                                                      \
-         {                                                                   \
-           result = __GCONV_ILLEGAL_INPUT;                                   \
-           break;                                                            \
-         }                                                                   \
+       /* Shift IN, change to SBCS converter (redundant escape okay).  */    \
        curcs = sb;                                                           \
        ++inptr;                                                              \
        continue;                                                             \
@@ -149,7 +145,7 @@ enum
                                                                              \
        assert (curcs == db);                                                 \
                                                                              \
-       if (__builtin_expect (inptr + 1 >= inend, 0))                         \
+       if (__glibc_unlikely (inptr + 1 >= inend))                            \
          {                                                                   \
            /* The second character is not available.  Store the              \
               intermediate result. */                                        \
@@ -161,7 +157,7 @@ enum
        while (ch > rp2->end)                                                 \
          ++rp2;                                                              \
                                                                              \
-       if (__builtin_expect (rp2 == NULL, 0)                                 \
+       if (__builtin_expect (rp2->start == 0xffff, 0)                        \
            || __builtin_expect (ch < rp2->start, 0)                          \
            || (res = __ibm939db_to_ucs4[ch + rp2->idx],                      \
                __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
@@ -194,9 +190,8 @@ enum
     uint32_t ch = get32 (inptr);                                             \
     const struct gap *rp1 = __ucs4_to_ibm939sb_idx;                          \
     const struct gap *rp2 = __ucs4_to_ibm939db_idx;                          \
-    const char *cp;                                                          \
                                                                              \
-    if (__builtin_expect (ch >= 0xffff, 0))                                  \
+    if (__glibc_unlikely (ch >= 0xffff))                                     \
       {                                                                              \
        UNICODE_TAG_HANDLER (ch, 4);                                          \
        goto ibm939_invalid_char;                                             \
@@ -206,14 +201,16 @@ enum
       ++rp1;                                                                 \
                                                                              \
     /* Use the UCS4 table for single byte.  */                               \
+    unsigned char sbconv;                                                    \
     if (__builtin_expect (ch < rp1->start, 0)                                \
-       || (cp = __ucs4_to_ibm939sb[ch + rp1->idx],                           \
-           __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
+       || (sbconv = __ucs4_to_ibm939sb[ch + rp1->idx],                       \
+           __builtin_expect (sbconv, L'\1') == L'\0' && ch != '\0'))         \
       {                                                                              \
        /* Use the UCS4 table for double byte.  */                            \
        while (ch > rp2->end)                                                 \
          ++rp2;                                                              \
                                                                              \
+       const char *cp;                                                       \
        if (__builtin_expect (ch < rp2->start, 0)                             \
            || (cp = __ucs4_to_ibm939db[ch + rp2->idx],                       \
                __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0'))        \
@@ -226,7 +223,7 @@ enum
          {                                                                   \
            if (curcs == sb)                                                  \
              {                                                               \
-               if (__builtin_expect (outptr + 1 > outend, 0))                \
+               if (__glibc_unlikely (outptr + 1 > outend))                   \
                  {                                                           \
                    result = __GCONV_FULL_OUTPUT;                             \
                    break;                                                    \
@@ -235,7 +232,7 @@ enum
                curcs = db;                                                   \
              }                                                               \
                                                                              \
-           if (__builtin_expect (outptr + 2 > outend, 0))                    \
+           if (__glibc_unlikely (outptr + 2 > outend))                       \
              {                                                               \
                result = __GCONV_FULL_OUTPUT;                                 \
                break;                                                        \
@@ -248,15 +245,16 @@ enum
       {                                                                              \
        if (curcs == db)                                                      \
          {                                                                   \
-           if (__builtin_expect (outptr + 1 > outend, 0))                    \
+           if (__glibc_unlikely (outptr + 1 > outend))                       \
              {                                                               \
                result = __GCONV_FULL_OUTPUT;                                 \
                break;                                                        \
              }                                                               \
            *outptr++ = SI;                                                   \
+           curcs = sb;                                                       \
          }                                                                   \
                                                                              \
-       if (__builtin_expect (outptr + 1 > outend, 0))                        \
+       if (__glibc_unlikely (outptr + 1 > outend))                           \
          {                                                                   \
            result = __GCONV_FULL_OUTPUT;                                     \
            break;                                                            \
@@ -266,8 +264,7 @@ enum
        else if (ch == 0x5c)                                                  \
          *outptr++ = 0xb2;                                                   \
        else                                                                  \
-         *outptr++ = cp[0];                                                  \
-       curcs = sb;                                                           \
+         *outptr++ = sbconv;                                                 \
       }                                                                              \
                                                                              \
     /* Now that we wrote the output increment the input pointer.  */         \