Add function to validate binary data
authorDan Vrátil <dvratil@redhat.com>
Thu, 23 Aug 2012 09:09:24 +0000 (11:09 +0200)
committerDan Vrátil <dvratil@redhat.com>
Thu, 23 Aug 2012 09:09:24 +0000 (11:09 +0200)
bug #680786

libedataserver/e-data-server-util.c
libedataserver/e-data-server-util.h

index ff3c261..7ffe27f 100644 (file)
@@ -469,18 +469,35 @@ e_util_utf8_remove_accents (const gchar *str)
 gchar *
 e_util_utf8_make_valid (const gchar *str)
 {
+       return e_util_utf8_data_make_valid (str, strlen (str));
+}
+
+/**
+ * e_util_utf8_data_make_valid:
+ * @data: UTF-8 binary data
+ * @data_bytes: length of the binary data
+ *
+ * Returns a newly-allocated NULL-terminated string with invalid characters
+ * replaced by Unicode replacement characters (U+FFFD).
+ *
+ * Returns: a newly-allocated string
+ *
+ * Since: 3.6
+ */
+gchar *
+e_util_utf8_data_make_valid (const gchar *data,
+                            gsize data_bytes)
+{
        /* almost identical copy of glib's _g_utf8_make_valid() */
        GString *string;
        const gchar *remainder, *invalid;
-       gint remaining_bytes, valid_bytes, total_bytes;
+       gint remaining_bytes, valid_bytes;
 
-       g_return_val_if_fail (str != NULL, NULL);
+       g_return_val_if_fail (data != NULL, NULL);
 
        string = NULL;
-       remainder = str;
-       remaining_bytes = strlen (str);
-
-       total_bytes = remaining_bytes;
+       remainder = (gchar *) data,
+       remaining_bytes = data_bytes;
 
        while (remaining_bytes != 0) {
                if (g_utf8_validate (remainder, remaining_bytes, &invalid))
@@ -499,11 +516,11 @@ e_util_utf8_make_valid (const gchar *str)
        }
 
        if (string == NULL)
-               return g_strndup (str, total_bytes);
+               return g_strndup ((gchar *) data, data_bytes);
 
        g_string_append (string, remainder);
 
-       g_assert (g_utf8_validate (string->str, -1, NULL));
+       g_warn_if_fail (g_utf8_validate (string->str, -1, NULL));
 
        return g_string_free (string, FALSE);
 }
index 24e26a1..bc3dee0 100644 (file)
@@ -50,6 +50,8 @@ gint          e_util_utf8_strcasecmp          (const gchar *s1,
                                                 const gchar *s2);
 gchar *                e_util_utf8_remove_accents      (const gchar *str);
 gchar *                e_util_utf8_make_valid          (const gchar *str);
+gchar *                e_util_utf8_data_make_valid     (const gchar *data,
+                                                gsize bytes);
 const gchar *   e_util_ensure_gdbus_string     (const gchar *str,
                                                 gchar **gdbus_str);
 guint64                e_util_gthread_id               (GThread *thread);