b230f18
diff -up cups-1.5.4/notifier/dbus.c.dbus-utf8 cups-1.5.4/notifier/dbus.c
b230f18
--- cups-1.5.4/notifier/dbus.c.dbus-utf8	2012-01-13 23:00:22.000000000 +0000
b230f18
+++ cups-1.5.4/notifier/dbus.c	2012-10-31 11:30:08.217215685 +0000
b230f18
@@ -31,6 +31,9 @@
b230f18
 #include <sys/stat.h>
b230f18
 #include <sys/types.h>
b230f18
 #include <unistd.h>
b230f18
+#include <assert.h>
b230f18
+#include <locale.h>
b230f18
+#include <wchar.h>
b230f18
 
b230f18
 #include <dbus/dbus.h>
b230f18
 #ifdef HAVE_DBUS_MESSAGE_ITER_INIT_APPEND
b230f18
@@ -156,10 +159,82 @@ enum
b230f18
  * Local functions...
b230f18
  */
b230f18
 
b230f18
-static int	acquire_lock(int *fd, char *lockfile, size_t locksize);
b230f18
+static int		acquire_lock(int *fd, char *lockfile, size_t locksize);
b230f18
+static const char	*validate_utf8(const char *str);
b230f18
 
b230f18
 
b230f18
 /*
b230f18
+ * 'validate_utf8()' - Convert to valid UTF-8
b230f18
+ */
b230f18
+
b230f18
+static const char *
b230f18
+validate_utf8 (const char *str)
b230f18
+{
b230f18
+  static char *buffer = NULL;
b230f18
+  static size_t buflen = 0;
b230f18
+  char *p;
b230f18
+  size_t str_len;
b230f18
+  unsigned int i;
b230f18
+  mbstate_t instate, outstate;
b230f18
+
b230f18
+  if (str == NULL)
b230f18
+  {
b230f18
+    free (buffer);
b230f18
+    return (NULL);
b230f18
+  }
b230f18
+
b230f18
+  /* Is it already valid? */
b230f18
+  if (mbstowcs (NULL, str, 0) != (size_t) -1)
b230f18
+    return str;
b230f18
+
b230f18
+  /* Make sure our buffer is at least as large as the input string */
b230f18
+  str_len = strlen (str);
b230f18
+  if (str_len > buflen)
b230f18
+  {
b230f18
+    if (buffer == NULL)
b230f18
+      /* Set encoding type to UTF-8 the first time we need to */
b230f18
+      setlocale (LC_CTYPE, "en_US.UTF-8");
b230f18
+
b230f18
+    buflen = str_len + 1;
b230f18
+    buffer = realloc (buffer, buflen);
b230f18
+  }
b230f18
+
b230f18
+  memset (&instate, '\0', sizeof (mbstate_t));
b230f18
+  memset (&outstate, '\0', sizeof (mbstate_t));
b230f18
+  p = buffer;
b230f18
+  i = 0;
b230f18
+  while (i < str_len)
b230f18
+  {
b230f18
+    wchar_t wc;
b230f18
+    size_t used, written;
b230f18
+    mbstate_t orig_instate = instate;
b230f18
+    used = mbrtowc (&wc, str + i, str_len - i, &instate);
b230f18
+    switch (used)
b230f18
+    {
b230f18
+    case (size_t) -2:
b230f18
+    case (size_t) -1:
b230f18
+      wc = L'?'; /* so replacement is never longer than original char */
b230f18
+      instate = orig_instate;
b230f18
+      /* fallthru */
b230f18
+    case 0:
b230f18
+      used = 1;
b230f18
+    }
b230f18
+
b230f18
+    written = wcrtomb (p, wc, &outstate);
b230f18
+    if (written != -1)
b230f18
+    {
b230f18
+      p += written;
b230f18
+      assert (p - buffer < buflen);
b230f18
+    }
b230f18
+
b230f18
+    i += used;
b230f18
+  }
b230f18
+
b230f18
+  *p = '\0';
b230f18
+  return buffer;
b230f18
+}
b230f18
+
b230f18
+/*
b230f18
  * 'main()' - Read events and send DBUS notifications.
b230f18
  */
b230f18
 
b230f18
@@ -226,6 +301,7 @@ main(int  argc,				/* I - Number of comm
b230f18
     int			no = 0;		/* Boolean "no" value */
b230f18
     int			params = PARAMS_NONE;
b230f18
 					/* What parameters to include? */
b230f18
+    const char		*val;
b230f18
 
b230f18
 
b230f18
    /*
b230f18
@@ -365,7 +441,8 @@ main(int  argc,				/* I - Number of comm
b230f18
     attr = ippFindAttribute(msg, "notify-text", IPP_TAG_TEXT);
b230f18
     if (!attr)
b230f18
       goto bail;
b230f18
-    if (!dbus_message_iter_append_string(&iter, &(attr->values[0].string.text)))
b230f18
+    val = validate_utf8 (attr->values[0].string.text);
b230f18
+    if (!dbus_message_iter_append_string(&iter, &val))
b230f18
       goto bail;
b230f18
 
b230f18
     if (params >= PARAMS_PRINTER)
b230f18
@@ -509,8 +586,8 @@ main(int  argc,				/* I - Number of comm
b230f18
       attr = ippFindAttribute(msg, "job-name", IPP_TAG_NAME);
b230f18
       if (attr)
b230f18
       {
b230f18
-        if (!dbus_message_iter_append_string(&iter,
b230f18
-                                             &(attr->values[0].string.text)))
b230f18
+        val = validate_utf8 (attr->values[0].string.text);
b230f18
+        if (!dbus_message_iter_append_string(&iter, &val))
b230f18
           goto bail;
b230f18
       }
b230f18
       else