From: Dan Vrátil Date: Thu, 23 Aug 2012 09:09:24 +0000 (+0200) Subject: Add function to validate binary data X-Git-Tag: upstream/3.7.4~516 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ac9664207d8d030a667e88ef0174c2ceffc1d4bd;p=platform%2Fupstream%2Fevolution-data-server.git Add function to validate binary data bug #680786 --- diff --git a/libedataserver/e-data-server-util.c b/libedataserver/e-data-server-util.c index ff3c261..7ffe27f 100644 --- a/libedataserver/e-data-server-util.c +++ b/libedataserver/e-data-server-util.c @@ -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); } diff --git a/libedataserver/e-data-server-util.h b/libedataserver/e-data-server-util.h index 24e26a1..bc3dee0 100644 --- a/libedataserver/e-data-server-util.h +++ b/libedataserver/e-data-server-util.h @@ -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);