]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - iconvdata/utf-16.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / iconvdata / utf-16.c
index b4ddfeb2380d645046a1c09073ca47253e576f78..aa8d25f98cc76838d7df894ca2f949fa66e20cf9 100644 (file)
@@ -1,5 +1,5 @@
 /* Conversion module for UTF-16.
-   Copyright (C) 1999, 2000-2002, 2003, 2005, 2011 Free Software Foundation, Inc.
+   Copyright (C) 1999-2019 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1999.
 
@@ -15,7 +15,7 @@
 
    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/>.  */
 
 #include <byteswap.h>
 #include <dlfcn.h>
 #define MIN_NEEDED_FROM                2
 #define MAX_NEEDED_FROM                4
 #define MIN_NEEDED_TO          4
+#define ONE_DIRECTION          0
 #define FROM_DIRECTION         (dir == from_utf16)
 #define PREPARE_LOOP \
   enum direction dir = ((struct utf16_data *) step->__data)->dir;            \
   enum variant var = ((struct utf16_data *) step->__data)->var;                      \
-  if (__builtin_expect (data->__invocation_counter == 0, 0))                 \
+  if (__glibc_unlikely (data->__invocation_counter == 0))                    \
     {                                                                        \
       if (var == UTF_16)                                                     \
        {                                                                     \
@@ -67,7 +68,7 @@
          else if (!FROM_DIRECTION && !data->__internal_use)                  \
            {                                                                 \
              /* Emit the Byte Order Mark.  */                                \
-             if (__builtin_expect (outbuf + 2 > outend, 0))                  \
+             if (__glibc_unlikely (outbuf + 2 > outend))                     \
                return __GCONV_FULL_OUTPUT;                                   \
                                                                              \
              put16u (outbuf, BOM);                                           \
@@ -200,7 +201,7 @@ gconv_end (struct __gconv_step *data)
   {                                                                          \
     uint32_t c = get32 (inptr);                                                      \
                                                                              \
-    if (__builtin_expect (c >= 0xd800 && c < 0xe000, 0))                     \
+    if (__glibc_unlikely (c >= 0xd800 && c < 0xe000))                        \
       {                                                                              \
        /* Surrogate characters in UCS-4 input are not valid.                 \
           We must catch this.  If we let surrogates pass through,            \
@@ -216,15 +217,15 @@ gconv_end (struct __gconv_step *data)
                                                                              \
     if (swap)                                                                \
       {                                                                              \
-       if (__builtin_expect (c >= 0x10000, 0))                               \
+       if (__glibc_unlikely (c >= 0x10000))                                  \
          {                                                                   \
-           if (__builtin_expect (c >= 0x110000, 0))                          \
+           if (__glibc_unlikely (c >= 0x110000))                             \
              {                                                               \
                STANDARD_TO_LOOP_ERR_HANDLER (4);                             \
              }                                                               \
                                                                              \
            /* Generate a surrogate character.  */                            \
-           if (__builtin_expect (outptr + 4 > outend, 0))                    \
+           if (__glibc_unlikely (outptr + 4 > outend))                       \
              {                                                               \
                /* Overflow in the output buffer.  */                         \
                result = __GCONV_FULL_OUTPUT;                                 \
@@ -240,15 +241,15 @@ gconv_end (struct __gconv_step *data)
       }                                                                              \
     else                                                                     \
       {                                                                              \
-       if (__builtin_expect (c >= 0x10000, 0))                               \
+       if (__glibc_unlikely (c >= 0x10000))                                  \
          {                                                                   \
-           if (__builtin_expect (c >= 0x110000, 0))                          \
+           if (__glibc_unlikely (c >= 0x110000))                             \
              {                                                               \
                STANDARD_TO_LOOP_ERR_HANDLER (4);                             \
              }                                                               \
                                                                              \
            /* Generate a surrogate character.  */                            \
-           if (__builtin_expect (outptr + 4 > outend, 0))                    \
+           if (__glibc_unlikely (outptr + 4 > outend))                       \
              {                                                               \
                /* Overflow in the output buffer.  */                         \
                result = __GCONV_FULL_OUTPUT;                                 \
@@ -294,9 +295,15 @@ gconv_end (struct __gconv_step *data)
          {                                                                   \
            uint16_t u2;                                                      \
                                                                              \
+           if (__glibc_unlikely (u1 >= 0xdc00))                              \
+             {                                                               \
+               /* This is no valid first word for a surrogate.  */           \
+               STANDARD_FROM_LOOP_ERR_HANDLER (2);                           \
+             }                                                               \
+                                                                             \
            /* It's a surrogate character.  At least the first word says      \
               it is.  */                                                     \
-           if (__builtin_expect (inptr + 4 > inend, 0))                      \
+           if (__glibc_unlikely (inptr + 4 > inend))                         \
              {                                                               \
                /* We don't have enough input for another complete input      \
                   character.  */                                             \
@@ -328,9 +335,15 @@ gconv_end (struct __gconv_step *data)
          }                                                                   \
        else                                                                  \
          {                                                                   \
+           if (__glibc_unlikely (u1 >= 0xdc00))                              \
+             {                                                               \
+               /* This is no valid first word for a surrogate.  */           \
+               STANDARD_FROM_LOOP_ERR_HANDLER (2);                           \
+             }                                                               \
+                                                                             \
            /* It's a surrogate character.  At least the first word says      \
               it is.  */                                                     \
-           if (__builtin_expect (inptr + 4 > inend, 0))                      \
+           if (__glibc_unlikely (inptr + 4 > inend))                         \
              {                                                               \
                /* We don't have enough input for another complete input      \
                   character.  */                                             \