]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - iconvdata/ibm933.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / iconvdata / ibm933.c
index 389028ebc4e700f06d27dc6d1785c5c30ee1d37d..839e6b85200c39da216eeedcc43612c2f7559453 100644 (file)
@@ -1,5 +1,5 @@
 /* Conversion from and to IBM933.
-   Copyright (C) 2000-2002 Free Software Foundation, Inc.
+   Copyright (C) 2000-2020 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Masahide Washizawa <washi@yamato.ibm.co.jp>, 2000.
 
    Lesser General Public License for more details.
 
    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.  */
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* IBM933 is designed for the representation of Korean using a stateful
+   EBCDIC encoding scheme.  It is also known as CCSID 933 or CP933. See:
+   https://www-01.ibm.com/software/globalization/ccsid/ccsid933.html */
 
 #include <dlfcn.h>
 #include <stdint.h>
@@ -32,6 +35,7 @@
 #define CHARSET_NAME   "IBM933//"
 #define FROM_LOOP      from_ibm933
 #define TO_LOOP                to_ibm933
+#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
@@ -62,7 +66,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                                                                \
@@ -104,24 +108,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;                                                             \
@@ -150,7 +144,7 @@ enum
        assert (curcs == db);                                                 \
                                                                              \
        /* Use the IBM933 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. */                                        \
@@ -162,7 +156,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 = __ibm933db_to_ucs4[ch + rp2->idx],                      \
                __builtin_expect (res, L'\1') == L'\0' && ch != '\0'))        \
@@ -195,9 +189,8 @@ enum
     uint32_t ch = get32 (inptr);                                             \
     const struct gap *rp1 = __ucs4_to_ibm933sb_idx;                          \
     const struct gap *rp2 = __ucs4_to_ibm933db_idx;                          \
-    const char *cp;                                                          \
                                                                              \
-    if (__builtin_expect (ch >= 0xffff, 0))                                  \
+    if (__glibc_unlikely (ch >= 0xffff))                                     \
       {                                                                              \
        UNICODE_TAG_HANDLER (ch, 4);                                          \
                                                                              \
@@ -208,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_ibm933sb[ch + rp1->idx],                           \
-           __builtin_expect (cp[0], L'\1') == L'\0' && ch != '\0'))          \
+       || (sbconv = __ucs4_to_ibm933sb[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_ibm933db[ch + rp2->idx],                       \
                __builtin_expect (cp[0], L'\1')==L'\0' && ch != '\0'))        \
@@ -227,7 +222,7 @@ enum
          {                                                                   \
            if (curcs == sb)                                                  \
              {                                                               \
-               if (__builtin_expect (outptr + 1 > outend, 0))                \
+               if (__glibc_unlikely (outptr + 1 > outend))                   \
                  {                                                           \
                    result = __GCONV_FULL_OUTPUT;                             \
                    break;                                                    \
@@ -236,7 +231,7 @@ enum
                curcs = db;                                                   \
              }                                                               \
                                                                              \
-           if (__builtin_expect (outptr + 2 > outend, 0))                    \
+           if (__glibc_unlikely (outptr + 2 > outend))                       \
              {                                                               \
                result = __GCONV_FULL_OUTPUT;                                 \
                break;                                                        \
@@ -249,21 +244,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.  */         \
@@ -272,6 +267,7 @@ enum
 #define LOOP_NEED_FLAGS
 #define EXTRA_LOOP_DECLS       , int *curcsp
 #define INIT_PARAMS            int curcs = *curcsp & ~7
+#define REINIT_PARAMS          curcs = *curcsp & ~7
 #define UPDATE_PARAMS          *curcsp = curcs
 #include <iconv/loop.c>