Blame SOURCES/cups-dbus-utf8.patch

ea9550
diff -up cups-1.6.1/notifier/dbus.c.dbus-utf8 cups-1.6.1/notifier/dbus.c
ea9550
--- cups-1.6.1/notifier/dbus.c.dbus-utf8	2012-01-20 19:00:32.000000000 +0000
ea9550
+++ cups-1.6.1/notifier/dbus.c	2012-10-31 11:04:47.686973616 +0000
ea9550
@@ -31,6 +31,9 @@
ea9550
 #include <sys/stat.h>
ea9550
 #include <sys/types.h>
ea9550
 #include <unistd.h>
ea9550
+#include <assert.h>
ea9550
+#include <locale.h>
ea9550
+#include <wchar.h>
ea9550
 
ea9550
 #ifdef HAVE_DBUS
ea9550
 #  include <dbus/dbus.h>
ea9550
@@ -157,10 +160,82 @@ enum
ea9550
  * Local functions...
ea9550
  */
ea9550
 
ea9550
-static int	acquire_lock(int *fd, char *lockfile, size_t locksize);
ea9550
+static int		acquire_lock(int *fd, char *lockfile, size_t locksize);
ea9550
+static const char	*validate_utf8(const char *str);
ea9550
 
ea9550
 
ea9550
 /*
ea9550
+ * 'validate_utf8()' - Convert to valid UTF-8
ea9550
+ */
ea9550
+
ea9550
+static const char *
ea9550
+validate_utf8 (const char *str)
ea9550
+{
ea9550
+  static char *buffer = NULL;
ea9550
+  static size_t buflen = 0;
ea9550
+  char *p;
ea9550
+  size_t str_len;
ea9550
+  unsigned int i;
ea9550
+  mbstate_t instate, outstate;
ea9550
+
ea9550
+  if (str == NULL)
ea9550
+  {
ea9550
+    free (buffer);
ea9550
+    return (NULL);
ea9550
+  }
ea9550
+
ea9550
+  /* Is it already valid? */
ea9550
+  if (mbstowcs (NULL, str, 0) != (size_t) -1)
ea9550
+    return str;
ea9550
+
ea9550
+  /* Make sure our buffer is at least as large as the input string */
ea9550
+  str_len = strlen (str);
ea9550
+  if (str_len > buflen)
ea9550
+  {
ea9550
+    if (buffer == NULL)
ea9550
+      /* Set encoding type to UTF-8 the first time we need to */
ea9550
+      setlocale (LC_CTYPE, "en_US.UTF-8");
ea9550
+
ea9550
+    buflen = str_len + 1;
ea9550
+    buffer = realloc (buffer, buflen);
ea9550
+  }
ea9550
+
ea9550
+  memset (&instate, '\0', sizeof (mbstate_t));
ea9550
+  memset (&outstate, '\0', sizeof (mbstate_t));
ea9550
+  p = buffer;
ea9550
+  i = 0;
ea9550
+  while (i < str_len)
ea9550
+  {
ea9550
+    wchar_t wc;
ea9550
+    size_t used, written;
ea9550
+    mbstate_t orig_instate = instate;
ea9550
+    used = mbrtowc (&wc, str + i, str_len - i, &instate);
ea9550
+    switch (used)
ea9550
+    {
ea9550
+    case (size_t) -2:
ea9550
+    case (size_t) -1:
ea9550
+      wc = L'?'; /* so replacement is never longer than original char */
ea9550
+      instate = orig_instate;
ea9550
+      /* fallthru */
ea9550
+    case 0:
ea9550
+      used = 1;
ea9550
+    }
ea9550
+
ea9550
+    written = wcrtomb (p, wc, &outstate);
ea9550
+    if (written != -1)
ea9550
+    {
ea9550
+      p += written;
ea9550
+      assert (p - buffer < buflen);
ea9550
+    }
ea9550
+
ea9550
+    i += used;
ea9550
+  }
ea9550
+
ea9550
+  *p = '\0';
ea9550
+  return buffer;
ea9550
+}
ea9550
+
ea9550
+/*
ea9550
  * 'main()' - Read events and send DBUS notifications.
ea9550
  */
ea9550
 
ea9550
@@ -366,7 +441,7 @@ main(int  argc,				/* I - Number of comm
ea9550
     attr = ippFindAttribute(msg, "notify-text", IPP_TAG_TEXT);
ea9550
     if (attr)
ea9550
     {
ea9550
-      const char *val = ippGetString(attr, 0, NULL);
ea9550
+      const char *val = validate_utf8 (ippGetString(attr, 0, NULL));
ea9550
       if (!dbus_message_iter_append_string(&iter, &val))
ea9550
         goto bail;
ea9550
     }