]> mj.ucw.cz Git - libucw.git/blobdiff - ucw/ff-unicode.c
Build: Added support for custom PKG_CONFIG_PATH in UCW::Configure::PkgConfig().
[libucw.git] / ucw / ff-unicode.c
index 6de5a107e083a5aaf1e63e4342d085c98ee7db19..e0faa0c85ae4aeb31a917bc87f34b24844522761 100644 (file)
@@ -1,23 +1,23 @@
 /*
  *     UCW Library: Reading and writing of UTF-8 on Fastbuf Streams
  *
- *     (c) 2001--2004 Martin Mares <mj@ucw.cz>
+ *     (c) 2001--2015 Martin Mares <mj@ucw.cz>
  *     (c) 2004 Robert Spalek <robert@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
  */
 
-#include "ucw/lib.h"
-#include "ucw/fastbuf.h"
-#include "ucw/unicode.h"
-#include "ucw/ff-unicode.h"
-#include "ucw/ff-binary.h"
+#include <ucw/lib.h>
+#include <ucw/fastbuf.h>
+#include <ucw/unicode.h>
+#include <ucw/ff-unicode.h>
+#include <ucw/ff-binary.h>
 
 /*** UTF-8 ***/
 
 int
-bget_utf8_slow(struct fastbuf *b, uns repl)
+bget_utf8_slow(struct fastbuf *b, uint repl)
 {
   int c = bgetc(b);
   int code;
@@ -41,6 +41,8 @@ bget_utf8_slow(struct fastbuf *b, uns repl)
       if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
        goto wrong;
       code = (code << 6) | (c & 0x3f);
+      if (code < 0x800)
+       goto wrong2;
     }
   else                                 /* 2 bytes */
     {
@@ -48,21 +50,25 @@ bget_utf8_slow(struct fastbuf *b, uns repl)
       if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
        goto wrong;
       code = (code << 6) | (c & 0x3f);
+      if (code < 0x80)
+       goto wrong2;
     }
   return code;
 
- wrong:
+wrong:
   if (c >= 0)
     bungetc(b);
+wrong2:
   return repl;
 }
 
 int
-bget_utf8_32_slow(struct fastbuf *b, uns repl)
+bget_utf8_32_slow(struct fastbuf *b, uint repl)
 {
   int c = bgetc(b);
   int code;
   int nr;
+  int limit;
 
   if (c < 0x80)                                /* Includes EOF */
     return c;
@@ -72,49 +78,53 @@ bget_utf8_32_slow(struct fastbuf *b, uns repl)
     {
       code = c & 0x1f;
       nr = 1;
+      limit = 0x80;
     }
   else if (c < 0xf0)
     {
       code = c & 0x0f;
       nr = 2;
+      limit = 0x800;
     }
   else if (c < 0xf8)
     {
       code = c & 0x07;
       nr = 3;
+      limit = 1 << 16;
     }
   else if (c < 0xfc)
     {
       code = c & 0x03;
       nr = 4;
+      limit = 1 << 21;
     }
   else if (c < 0xfe)
     {
       code = c & 0x01;
       nr = 5;
+      limit = 1 << 26;
     }
-  else                                 /* Too large, skip it */
-    {
-      while ((c = bgetc(b)) >= 0x80 && c < 0xc0)
-       ;
-      goto wrong;
-    }
+  else                                 /* Too large */
+    goto wrong2;
   while (nr-- > 0)
     {
       if ((c = bgetc(b)) < 0x80 || c >= 0xc0)
        goto wrong;
       code = (code << 6) | (c & 0x3f);
     }
+  if (code < limit)
+    goto wrong2;
   return code;
 
- wrong:
+wrong:
   if (c >= 0)
     bungetc(b);
+wrong2:
   return repl;
 }
 
 void
-bput_utf8_slow(struct fastbuf *b, uns u)
+bput_utf8_slow(struct fastbuf *b, uint u)
 {
   ASSERT(u < 65536);
   if (u < 0x80)
@@ -133,7 +143,7 @@ bput_utf8_slow(struct fastbuf *b, uns u)
 }
 
 void
-bput_utf8_32_slow(struct fastbuf *b, uns u)
+bput_utf8_32_slow(struct fastbuf *b, uint u)
 {
   ASSERT(u < (1U<<31));
   if (u < 0x80)
@@ -172,11 +182,11 @@ bput_utf8_32_slow(struct fastbuf *b, uns u)
 /*** UTF-16 ***/
 
 int
-bget_utf16_be_slow(struct fastbuf *b, uns repl)
+bget_utf16_be_slow(struct fastbuf *b, uint repl)
 {
   if (bpeekc(b) < 0)
     return -1;
-  uns u = bgetw_be(b), x, y;
+  uint u = bgetw_be(b), x, y;
   if ((int)u < 0)
     return repl;
   if ((x = u - 0xd800) >= 0x800)
@@ -187,11 +197,11 @@ bget_utf16_be_slow(struct fastbuf *b, uns repl)
 }
 
 int
-bget_utf16_le_slow(struct fastbuf *b, uns repl)
+bget_utf16_le_slow(struct fastbuf *b, uint repl)
 {
   if (bpeekc(b) < 0)
     return -1;
-  uns u = bgetw_le(b), x, y;
+  uint u = bgetw_le(b), x, y;
   if ((int)u < 0)
     return repl;
   if ((x = u - 0xd800) >= 0x800)
@@ -202,7 +212,7 @@ bget_utf16_le_slow(struct fastbuf *b, uns repl)
 }
 
 void
-bput_utf16_be_slow(struct fastbuf *b, uns u)
+bput_utf16_be_slow(struct fastbuf *b, uint u)
 {
   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
     {
@@ -221,7 +231,7 @@ bput_utf16_be_slow(struct fastbuf *b, uns u)
 }
 
 void
-bput_utf16_le_slow(struct fastbuf *b, uns u)
+bput_utf16_le_slow(struct fastbuf *b, uint u)
 {
   if (u < 0xd800 || (u < 0x10000 && u >= 0xe000))
     {
@@ -261,9 +271,9 @@ int main(int argc, char **argv)
 #undef F
   };
 
-  uns func = ~0U;
+  uint func = ~0U;
   if (argc > 1)
-    for (uns i = 0; i < ARRAY_SIZE(names); i++)
+    for (uint i = 0; i < ARRAY_SIZE(names); i++)
       if (!strcasecmp(names[i], argv[1]))
        func = i;
   if (!~func)
@@ -275,7 +285,7 @@ int main(int argc, char **argv)
   struct fastbuf *b = fbgrow_create(8);
   if (func < FUNC_BPUT_UTF8)
     {
-      uns u;
+      uint u;
       while (scanf("%x", &u) == 1)
        bputc(b, u);
       fbgrow_rewind(b);
@@ -306,7 +316,7 @@ int main(int argc, char **argv)
     }
   else
     {
-      uns u, i = 0;
+      uint u, i = 0;
       while (scanf("%x", &u) == 1)
         {
          switch (func)