Blame SOURCES/coreutils-i18n-un-expand-BOM.patch

e63663
From 7a7c776a4e228d180e74614fd8c8afcad5d4bdf7 Mon Sep 17 00:00:00 2001
e63663
From: Jakub Martisko <jamartis@redhat.com>
e63663
Date: Thu, 7 Jul 2016 12:53:26 +0200
e63663
Subject: [PATCH] coreutils-i18n-un-expand-BOM.patch
e63663
e63663
---
e63663
 src/expand-common.c  | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++
e63663
 src/expand-common.h  |  12 ++++++
e63663
 src/expand.c         |  45 +++++++++++++++++++-
e63663
 src/unexpand.c       |  43 ++++++++++++++++++-
e63663
 tests/expand/mb.sh   |  71 ++++++++++++++++++++++++++++++++
e63663
 tests/unexpand/mb.sh |  59 ++++++++++++++++++++++++++
e63663
 6 files changed, 342 insertions(+), 2 deletions(-)
e63663
e63663
diff --git a/src/expand-common.c b/src/expand-common.c
e63663
index 4657e46..97cbb09 100644
e63663
--- a/src/expand-common.c
e63663
+++ b/src/expand-common.c
e63663
@@ -19,6 +19,7 @@
e63663
 #include <assert.h>
e63663
 #include <stdio.h>
e63663
 #include <sys/types.h>
e63663
+#include <mbfile.h>
e63663
 #include "system.h"
e63663
 #include "die.h"
e63663
 #include "error.h"
e63663
@@ -126,6 +127,119 @@ set_increment_size (uintmax_t tabval)
e63663
   return ok;
e63663
 }
e63663
 
e63663
+extern int
e63663
+set_utf_locale (void)
e63663
+{
e63663
+      /*try using some predefined locale */
e63663
+      const char* predef_locales[] = {"C.UTF8","en_US.UTF8","en_GB.UTF8"};
e63663
+
e63663
+      const int predef_locales_count=3;
e63663
+      for (int i=0;i
e63663
+        {
e63663
+          if (setlocale(LC_ALL,predef_locales[i])!=NULL)
e63663
+          {
e63663
+            break;
e63663
+          }
e63663
+          else if (i==predef_locales_count-1)
e63663
+          {
e63663
+            return 1;
e63663
+            error (EXIT_FAILURE, errno, _("cannot set UTF-8 locale"));
e63663
+          }
e63663
+        }
e63663
+        return 0;
e63663
+}
e63663
+
e63663
+extern bool
e63663
+check_utf_locale(void)
e63663
+{
e63663
+  char* locale = setlocale (LC_CTYPE , NULL);
e63663
+  if (locale == NULL)
e63663
+  {
e63663
+    return false;
e63663
+  }
e63663
+  else if (strcasestr(locale, "utf8") == NULL && strcasestr(locale, "utf-8") == NULL)
e63663
+  {
e63663
+    return false;
e63663
+  }
e63663
+  return true;
e63663
+}
e63663
+
e63663
+extern bool
e63663
+check_bom(FILE* fp, mb_file_t *mbf)
e63663
+{
e63663
+  int c;
e63663
+
e63663
+
e63663
+  c=fgetc(fp);
e63663
+
e63663
+  /*test BOM header of the first file */
e63663
+  mbf->bufcount=0;
e63663
+  if (c == 0xEF)
e63663
+  {
e63663
+    c=fgetc(fp);
e63663
+  }
e63663
+  else
e63663
+  {
e63663
+    if (c != EOF)
e63663
+    {
e63663
+      ungetc(c,fp);
e63663
+    }
e63663
+    return false;
e63663
+  }
e63663
+
e63663
+  if (c == 0xBB)
e63663
+  {
e63663
+    c=fgetc(fp);
e63663
+  }
e63663
+  else
e63663
+  {
e63663
+    if ( c!= EOF )
e63663
+    {
e63663
+      mbf->buf[0]=(unsigned char) 0xEF;
e63663
+      mbf->bufcount=1;
e63663
+      ungetc(c,fp);
e63663
+      return false;
e63663
+    }
e63663
+    else
e63663
+    {
e63663
+      ungetc(0xEF,fp);
e63663
+      return false;
e63663
+    }
e63663
+  }
e63663
+  if (c == 0xBF)
e63663
+  {
e63663
+    mbf->bufcount=0;
e63663
+    return true;
e63663
+  }
e63663
+  else
e63663
+  {
e63663
+    if (c != EOF)
e63663
+    {
e63663
+      mbf->buf[0]=(unsigned char) 0xEF;
e63663
+      mbf->buf[1]=(unsigned char) 0xBB;
e63663
+      mbf->bufcount=2;
e63663
+      ungetc(c,fp);
e63663
+      return false;
e63663
+    }
e63663
+    else
e63663
+    {
e63663
+      mbf->buf[0]=(unsigned char) 0xEF;
e63663
+      mbf->bufcount=1;
e63663
+      ungetc(0xBB,fp);
e63663
+      return false;
e63663
+    }
e63663
+  }
e63663
+  return false;
e63663
+}
e63663
+
e63663
+extern void
e63663
+print_bom(void)
e63663
+{
e63663
+  putc (0xEF, stdout);
e63663
+  putc (0xBB, stdout);
e63663
+  putc (0xBF, stdout);
e63663
+}
e63663
+
e63663
 /* Add the comma or blank separated list of tab stops STOPS
e63663
    to the list of tab stops.  */
e63663
 extern void
e63663
diff --git a/src/expand-common.h b/src/expand-common.h
e63663
index 8cb2079..763bfda 100644
e63663
--- a/src/expand-common.h
e63663
+++ b/src/expand-common.h
e63663
@@ -34,6 +34,18 @@ extern size_t max_column_width;
e63663
 /* The desired exit status.  */
e63663
 extern int exit_status;
e63663
 
e63663
+extern int
e63663
+set_utf_locale (void);
e63663
+
e63663
+extern bool
e63663
+check_utf_locale(void);
e63663
+
e63663
+extern bool
e63663
+check_bom(FILE* fp, mb_file_t *mbf);
e63663
+
e63663
+extern void
e63663
+print_bom(void);
e63663
+
e63663
 /* Add tab stop TABVAL to the end of 'tab_list'.  */
e63663
 extern void
e63663
 add_tab_stop (uintmax_t tabval);
e63663
diff --git a/src/expand.c b/src/expand.c
e63663
index 310b349..4136824 100644
e63663
--- a/src/expand.c
e63663
+++ b/src/expand.c
e63663
@@ -103,11 +103,33 @@ expand (void)
e63663
   FILE *fp = next_file (NULL);
e63663
   mb_file_t mbf;
e63663
   mbf_char_t c;
e63663
+  /* True if the starting locale is utf8.  */
e63663
+  bool using_utf_locale;
e63663
+
e63663
+  /* True if the first file contains BOM header.  */
e63663
+  bool found_bom;
e63663
+  using_utf_locale=check_utf_locale();
e63663
 
e63663
   if (!fp)
e63663
     return;
e63663
-
e63663
   mbf_init (mbf, fp);
e63663
+  found_bom=check_bom(fp,&mbf);
e63663
+
e63663
+  if (using_utf_locale == false && found_bom == true)
e63663
+  {
e63663
+    /*try using some predefined locale */
e63663
+
e63663
+    if (set_utf_locale () != 0)
e63663
+    {
e63663
+      error (EXIT_FAILURE, errno, _("cannot set UTF-8 locale"));
e63663
+    }
e63663
+  }
e63663
+
e63663
+
e63663
+  if (found_bom == true)
e63663
+  {
e63663
+    print_bom();
e63663
+  }
e63663
 
e63663
   while (true)
e63663
     {
e63663
@@ -132,6 +154,27 @@ expand (void)
e63663
             if ((mb_iseof (c)) && (fp = next_file (fp)))
e63663
               {
e63663
                 mbf_init (mbf, fp);
e63663
+                if (fp!=NULL)
e63663
+                {
e63663
+                  if (check_bom(fp,&mbf)==true)
e63663
+                  {
e63663
+                    /*Not the first file - check BOM header*/
e63663
+                    if (using_utf_locale==false && found_bom==false)
e63663
+                    {
e63663
+                      /*BOM header in subsequent file but not in the first one. */
e63663
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
e63663
+                    }
e63663
+                  }
e63663
+                  else
e63663
+                  {
e63663
+                    if(using_utf_locale==false && found_bom==true)
e63663
+                    {
e63663
+                      /*First file conatined BOM header - locale was switched to UTF
e63663
+                       *all subsequent files should contain BOM. */
e63663
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
e63663
+                    }
e63663
+                  }
e63663
+                }
e63663
                 continue;
e63663
               }
e63663
             else
e63663
diff --git a/src/unexpand.c b/src/unexpand.c
e63663
index 863a90a..5681b58 100644
e63663
--- a/src/unexpand.c
e63663
+++ b/src/unexpand.c
e63663
@@ -116,16 +116,36 @@ unexpand (void)
e63663
      include characters other than spaces, so the blanks must be
e63663
      stored, not merely counted.  */
e63663
   mbf_char_t *pending_blank;
e63663
+  /* True if the starting locale is utf8.  */
e63663
+  bool using_utf_locale;
e63663
+
e63663
+  /* True if the first file contains BOM header.  */
e63663
+  bool found_bom;
e63663
+  using_utf_locale=check_utf_locale();
e63663
 
e63663
   if (!fp)
e63663
     return;
e63663
+  mbf_init (mbf, fp);
e63663
+  found_bom=check_bom(fp,&mbf);
e63663
+
e63663
+  if (using_utf_locale == false && found_bom == true)
e63663
+  {
e63663
+    /*try using some predefined locale */
e63663
 
e63663
+    if (set_utf_locale () != 0)
e63663
+    {
e63663
+      error (EXIT_FAILURE, errno, _("cannot set UTF-8 locale"));
e63663
+    }
e63663
+  }
e63663
   /* The worst case is a non-blank character, then one blank, then a
e63663
      tab stop, then MAX_COLUMN_WIDTH - 1 blanks, then a non-blank; so
e63663
      allocate MAX_COLUMN_WIDTH bytes to store the blanks.  */
e63663
   pending_blank = xmalloc (max_column_width * sizeof (mbf_char_t));
e63663
 
e63663
-  mbf_init (mbf, fp);
e63663
+  if (found_bom == true)
e63663
+  {
e63663
+    print_bom();
e63663
+  }
e63663
 
e63663
   while (true)
e63663
     {
e63663
@@ -169,6 +189,27 @@ unexpand (void)
e63663
             if ((mb_iseof (c)) && (fp = next_file (fp)))
e63663
               {
e63663
                 mbf_init (mbf, fp);
e63663
+                if (fp!=NULL)
e63663
+                {
e63663
+                  if (check_bom(fp,&mbf)==true)
e63663
+                  {
e63663
+                    /*Not the first file - check BOM header*/
e63663
+                    if (using_utf_locale==false && found_bom==false)
e63663
+                    {
e63663
+                      /*BOM header in subsequent file but not in the first one. */
e63663
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
e63663
+                    }
e63663
+                  }
e63663
+                  else
e63663
+                  {
e63663
+                    if(using_utf_locale==false && found_bom==true)
e63663
+                    {
e63663
+                      /*First file conatined BOM header - locale was switched to UTF
e63663
+                       *all subsequent files should contain BOM. */
e63663
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
e63663
+                    }
e63663
+                  }
e63663
+                }
e63663
                 continue;
e63663
               }
e63663
             else
e63663
diff --git a/tests/expand/mb.sh b/tests/expand/mb.sh
e63663
index 031be7a..1621c84 100755
e63663
--- a/tests/expand/mb.sh
e63663
+++ b/tests/expand/mb.sh
e63663
@@ -109,4 +109,75 @@ env printf '12345678
e63663
 expand < in > out || fail=1
e63663
 compare exp out > /dev/null 2>&1 || fail=1
e63663
 
e63663
+
e63663
+
e63663
+#BOM header test 1
e63663
+printf "\xEF\xBB\xBF" > in; cat <<\EOF >> in || framework_failure_
e63663
+1234567812345678123456781
e63663
+.       .       .       .
e63663
+a	b	c	d
e63663
+.       .       .       .
e63663
+ä	ö	ü	ß
e63663
+.       .       .       .
e63663
+EOF
e63663
+env printf '   äöü\t.    öüä.   \tä xx\n' >> in || framework_failure_
e63663
+
e63663
+printf "\xEF\xBB\xBF" > exp; cat <<\EOF >> exp || framework_failure_
e63663
+1234567812345678123456781
e63663
+.       .       .       .
e63663
+a       b       c       d
e63663
+.       .       .       .
e63663
+ä       ö       ü       ß
e63663
+.       .       .       .
e63663
+   äöü  .    öüä.       ä xx
e63663
+EOF
e63663
+
e63663
+
e63663
+expand < in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LANG=C expand < in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LC_ALL=C expand < in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+
e63663
+printf '\xEF\xBB\xBF' > in1; cat <<\EOF >> in1 || framework_failure_
e63663
+1234567812345678123456781
e63663
+.       .       .       .
e63663
+a	b	c	d
e63663
+.       .       .       .
e63663
+ä	ö	ü	ß
e63663
+.       .       .       .
e63663
+EOF
e63663
+env printf '   äöü\t.    öüä.   \tä xx\n' >> in1 || framework_failure_
e63663
+
e63663
+
e63663
+printf '\xEF\xBB\xBF' > exp; cat <<\EOF >> exp || framework_failure_
e63663
+1234567812345678123456781
e63663
+.       .       .       .
e63663
+a       b       c       d
e63663
+.       .       .       .
e63663
+ä       ö       ü       ß
e63663
+.       .       .       .
e63663
+   äöü  .    öüä.       ä xx
e63663
+1234567812345678123456781
e63663
+.       .       .       .
e63663
+a       b       c       d
e63663
+.       .       .       .
e63663
+ä       ö       ü       ß
e63663
+.       .       .       .
e63663
+   äöü  .    öüä.       ä xx
e63663
+EOF
e63663
+
e63663
+expand in1 in1 > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LANG=C expand in1 in1  > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LC_ALL=C expand in1 in1 > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
 exit $fail
e63663
diff --git a/tests/unexpand/mb.sh b/tests/unexpand/mb.sh
e63663
index 8d75652..9d4ee3e 100755
e63663
--- a/tests/unexpand/mb.sh
e63663
+++ b/tests/unexpand/mb.sh
e63663
@@ -111,3 +111,62 @@ env printf '12345678
e63663
 
e63663
 unexpand -a < in > out || fail=1
e63663
 compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+#BOM header test 1
e63663
+printf "\xEF\xBB\xBF" > in; cat <<\EOF >> in || framework_failure_
e63663
+1234567812345678123456781
e63663
+.       .       .       .
e63663
+a       b       c       d
e63663
+.       .       .       .
e63663
+ä       ö       ü       ß
e63663
+.       .       .       .
e63663
+   äöü  .    öüä.       ä xx
e63663
+EOF
e63663
+env printf '   äöü\t.    öüä.   \tä xx\n' >> in || framework_failure_
e63663
+
e63663
+printf "\xEF\xBB\xBF" > exp; cat <<\EOF >> exp || framework_failure_
e63663
+1234567812345678123456781
e63663
+.	.	.	.
e63663
+a	b	c	d
e63663
+.	.	.	.
e63663
+ä	ö	ü	ß
e63663
+.	.	.	.
e63663
+   äöü	.    öüä.	ä xx
e63663
+EOF
e63663
+
e63663
+unexpand < in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LANG=C unexpand < in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LC_ALL=C unexpand < in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+
e63663
+printf "\xEF\xBB\xBF" > exp; cat <<\EOF >> exp || framework_failure_
e63663
+1234567812345678123456781
e63663
+.	.	.	.
e63663
+a	b	c	d
e63663
+.	.	.	.
e63663
+ä	ö	ü	ß
e63663
+.	.	.	.
e63663
+   äöü	.    öüä.	ä xx
e63663
+1234567812345678123456781
e63663
+.	.	.	.
e63663
+a	b	c	d
e63663
+.	.	.	.
e63663
+ä	ö	ü	ß
e63663
+.	.	.	.
e63663
+   äöü	.    öüä.	ä xx
e63663
+EOF
e63663
+
e63663
+
e63663
+unexpand in in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LANG=C unexpand in in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
+
e63663
+LC_ALL=C unexpand in in > out || fail=1
e63663
+compare exp out > /dev/null 2>&1 || fail=1
e63663
-- 
e63663
2.9.3
e63663