dump2: Tolerate UTF-8, not just ASCII, in strings.
[pspp] / dump2.c
diff --git a/dump2.c b/dump2.c
index af01b408cdc5f77c1ae6021458d1001e1e6473cd..d7f8ad3d68abbcff43539cae1f56ef4515aa2e06 100644 (file)
--- a/dump2.c
+++ b/dump2.c
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include "u8-mbtouc.h"
 
 static uint8_t *data;
 static size_t n, pos;
@@ -86,10 +87,25 @@ all_ascii(const uint8_t *p)
   return true;
 }
 
+static bool
+all_utf8(const uint8_t *p)
+{
+  size_t len = strlen ((char *) p);
+  for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
+    {
+      ucs4_t uc;
+
+      mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
+      if (uc < 32 || uc == 127 || uc == 0xfffd)
+        return false;
+    }
+  return true;
+}
+
 static char *
 get_fixed_string(int len, const char *where)
 {
-  if (pos + len > n || !memchr(&data[pos], 0, len) || !all_ascii(&data[pos]))
+  if (pos + len > n || !memchr(&data[pos], 0, len) || !all_utf8(&data[pos]))
     {
       fprintf(stderr, "%s: 0x%x: bad fixed-width string\n", where, pos);
       exit(1);