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