]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - iconvdata/ibm935.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / iconvdata / ibm935.c
index 1ed311b01fd4ea0e17a59c9bacc140ec4ff59dda..451b7b971b2689a1cd3c8ca687a526b9c0829dec 100644 (file)
@@ -1,5 +1,5 @@
 /* Conversion from and to IBM935
-   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/>.  */
+
+/* IBM935 is designed for the representation of Simplified Chinese
+   using a stateful EBCDIC encoding scheme.  It is also known as
+   CCSID 935 or CP935. See:
+   https://www-01.ibm.com/software/globalization/ccsid/ccsid935.html  */
 
 #include <dlfcn.h>
 #include <stdint.h>
@@ -31,6 +36,7 @@
 #define CHARSET_NAME   "IBM935//"
 #define FROM_LOOP      from_ibm935
 #define TO_LOOP                to_ibm935
+#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);                                                 \
                                                                              \
        /* Use the IBM935 table for double byte.  */                          \
-       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 = __ibm935db_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_ibm935sb_idx;                          \
     const struct gap *rp2 = __ucs4_to_ibm935db_idx;                          \
-    const char *cp;                                                          \
                                                                              \
-    if (__builtin_expect (ch >= 0xffff, 0))                                  \
+    if (__glibc_unlikely (ch >= 0xffff))                                     \
       {                                                                              \
        UNICODE_TAG_HANDLER (ch, 4);                                          \
                                                                              \
@@ -207,14 +202,16 @@ enum
       ++rp1;                                                                 \
                                                                              \
     /* Use the UCS4 table for single byte.  */                               \
+    unsigned char sbconv;                                                    \
     if (__builtin_expect (ch < rp1->start, 0)                                \
-       || (cp = __ucs4_to_ibm935sb[ch + rp1->idx],                           \
-           __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
+       || (sbconv = __ucs4_to_ibm935sb[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_ibm935db[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,21 +245,21 @@ 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;                                                            \
          }                                                                   \
-       *outptr++ = cp[0];                                                    \
-       curcs = sb;                                                           \
+       *outptr++ = sbconv;                                                   \
       }                                                                              \
                                                                              \
     /* Now that we wrote the output increment the input pointer.  */         \