aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/gnunet-fs-gtk-common.c')
-rw-r--r--src/fs/gnunet-fs-gtk-common.c87
1 files changed, 53 insertions, 34 deletions
diff --git a/src/fs/gnunet-fs-gtk-common.c b/src/fs/gnunet-fs-gtk-common.c
index b2bf9b26..bd33d17a 100644
--- a/src/fs/gnunet-fs-gtk-common.c
+++ b/src/fs/gnunet-fs-gtk-common.c
@@ -26,6 +26,53 @@
26#include "gnunet-fs-gtk-common.h" 26#include "gnunet-fs-gtk-common.h"
27 27
28/** 28/**
29 * Converts metadata specified by @data of size @data_len
30 * and saved in format @format to UTF-8 encoded string.
31 * Works only for C-string and UTF8 metadata formats
32 * (returns NULL for everything else).
33 * Verifies UTF-8 strings.
34 *
35 * @param format format of the @data
36 * @param data data to convert
37 * @param data_len length of the data buffer (in bytes)
38 * @return NULL if can't be converted, allocated string otherwise,
39 * freeable with GNUNET_free* ().
40 */
41char *
42GNUNET_FS_GTK_dubious_meta_to_utf8 (enum EXTRACTOR_MetaFormat format, const char *data, size_t data_len)
43{
44 gchar *result = NULL;
45
46 if (format == EXTRACTOR_METAFORMAT_UTF8)
47 {
48 /* data must not contain NULLs (hence the -1) */
49 if (g_utf8_validate (data, data_len - 1, NULL))
50 result = GNUNET_strdup (data);
51 else
52 {
53 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
54 "Failed to validate supposedly utf-8 string `%s' of length %u, assuming it to be a C string\n",
55 data, data_len);
56 format = EXTRACTOR_METAFORMAT_C_STRING;
57 }
58 }
59 if (format == EXTRACTOR_METAFORMAT_C_STRING)
60 {
61 if (data_len > 0)
62 { /* There are no guarantees that data is NULL-terminated, AFAIU,
63 * so let's play it safe, shall we?
64 */
65 char *data_copy = GNUNET_malloc (data_len + 1);
66 memcpy (data_copy, data, data_len);
67 data_copy[data_len] = '\0';
68 result = GNUNET_GTK_from_loc_to_utf8 (data_copy);
69 GNUNET_free (data_copy);
70 }
71 }
72 return result;
73}
74
75/**
29 * Add meta data to list store. 76 * Add meta data to list store.
30 * 77 *
31 * @param cls closure (the GtkListStore) 78 * @param cls closure (the GtkListStore)
@@ -49,44 +96,17 @@ GNUNET_FS_GTK_add_meta_data_to_list_store (void *cls, const char *plugin_name,
49 const char *data, size_t data_len) 96 const char *data, size_t data_len)
50{ 97{
51 GtkListStore *ls = GTK_LIST_STORE (cls); 98 GtkListStore *ls = GTK_LIST_STORE (cls);
52 const gchar *data_to_insert = NULL; 99 gchar *data_to_insert = NULL;
53 gchar *free_data = NULL;
54 gsize rd;
55 gsize wr;
56 100
57 if (format == EXTRACTOR_METAFORMAT_UTF8) 101 data_to_insert = GNUNET_FS_GTK_dubious_meta_to_utf8 (format, data, data_len);
58 {
59 if (g_utf8_validate (data, data_len, NULL))
60 data_to_insert = data;
61 else
62 {
63 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
64 "Failed to validate supposedly utf-8 string `%s' of length %u, assuming it to be a C string\n",
65 data, data_len);
66 format = EXTRACTOR_METAFORMAT_C_STRING;
67 }
68 }
69 if (format == EXTRACTOR_METAFORMAT_C_STRING)
70 {
71 GError *gerr = NULL;
72 rd = wr = 0;
73 free_data = g_locale_to_utf8 (data, data_len, &rd, &wr, &gerr);
74 if (gerr != NULL)
75 {
76 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
77 "Error when converting a C string `%s' of length %u to utf-8 (converted %u to %u bytes): %s\n",
78 data, data_len, rd, wr, gerr->message);
79 g_error_free (gerr);
80 }
81 data_to_insert = free_data;
82 }
83 102
84 if (NULL != data_to_insert) 103 if (NULL != data_to_insert)
104 {
85 gtk_list_store_insert_with_values (ls, NULL, G_MAXINT, 0, type, 1, format, 105 gtk_list_store_insert_with_values (ls, NULL, G_MAXINT, 0, type, 1, format,
86 2, EXTRACTOR_metatype_to_string (type), 106 2, EXTRACTOR_metatype_to_string (type),
87 3, data_to_insert, -1); 107 3, data_to_insert, -1);
88 if (NULL != free_data) 108 g_free (data_to_insert);
89 g_free (free_data); 109 }
90 110
91 return 0; 111 return 0;
92} 112}
@@ -194,5 +214,4 @@ GNUNET_FS_GTK_mmap_and_scan (const char *filename,
194 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh)); 214 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
195} 215}
196 216
197
198/* end of gnunet-fs-gtk-common.c */ 217/* end of gnunet-fs-gtk-common.c */