23
23
* @brief A bunch of miscellaneous functions, mostly file conversions.
24
24
*/
25
25
26
+ #include "../misc/scanneraux.h" /* for struct scan_globals */
27
+
26
28
#include <errno.h> /* for errno() */
27
29
#include <gvm/base/prefs.h> /* for prefs_get() */
28
30
#include <stdlib.h> /* for atoi() */
@@ -39,6 +41,101 @@ extern int global_max_checks;
39
41
*/
40
42
#define G_LOG_DOMAIN "sd main"
41
43
44
+ /**
45
+ * @brief Adds a 'translation' entry for a file sent by the client.
46
+ *
47
+ * Files sent by the client are stored in memory on the server side.
48
+ * In order to access these files, their original name ('local' to the client)
49
+ * can be 'translated' into the file contents of the in-memory copy of the
50
+ * file on the server side.
51
+ *
52
+ * @param globals Global struct.
53
+ * @param file_hash hash to reference the file.
54
+ * @param contents Contents of the file.
55
+ */
56
+ static void
57
+ files_add_translation (struct scan_globals * globals , const char * file_hash ,
58
+ char * contents )
59
+ {
60
+ GHashTable * trans = globals -> files_translation ;
61
+ // Register the mapping table if none there yet
62
+ if (trans == NULL )
63
+ {
64
+ trans = g_hash_table_new_full (g_str_hash , g_str_equal , g_free , g_free );
65
+ globals -> files_translation = trans ;
66
+ }
67
+
68
+ g_hash_table_insert (trans , g_strdup (file_hash ), contents );
69
+ }
70
+
71
+ /**
72
+ * @brief Adds a 'content size' entry for a file sent by the client.
73
+ *
74
+ * Files sent by the client are stored in memory on the server side.
75
+ * Because they may be binary we need to store the size of the uploaded file as
76
+ * well. This function sets up a mapping from the original name sent by the
77
+ * client to the file size.
78
+ *
79
+ * @param globals Global struct.
80
+ * @param file_hash hash to reference the file.
81
+ * @param filesize Size of the file in bytes.
82
+ */
83
+ static void
84
+ files_add_size_translation (struct scan_globals * globals , const char * file_hash ,
85
+ const long filesize )
86
+ {
87
+ GHashTable * trans = globals -> files_size_translation ;
88
+ gchar * filesize_str = g_strdup_printf ("%ld" , filesize );
89
+
90
+ // Register the mapping table if none there yet
91
+ if (trans == NULL )
92
+ {
93
+ trans = g_hash_table_new_full (g_str_hash , g_str_equal , g_free , NULL );
94
+ globals -> files_size_translation = trans ;
95
+ }
96
+
97
+ g_hash_table_insert (trans , g_strdup (file_hash ), g_strdup (filesize_str ));
98
+ }
99
+
100
+ /**
101
+ * @brief Stores a file type preference in a hash table.
102
+ *
103
+ * @param globals Global struct.
104
+ * @param file File content.
105
+ * @param file_hash hash to reference the file.
106
+ *
107
+ * @return 0 if successful, -1 in case of errors.
108
+ */
109
+ int
110
+ store_file (struct scan_globals * globals , const char * file ,
111
+ const char * file_hash )
112
+ {
113
+ char * origname ;
114
+ gchar * contents = NULL ;
115
+
116
+ size_t bytes = 0 ;
117
+
118
+ if (!file_hash && * file_hash == '\0' )
119
+ return -1 ;
120
+
121
+ origname = g_strdup (file_hash );
122
+
123
+ contents = (gchar * ) g_base64_decode (file , & bytes );
124
+
125
+ if (contents == NULL )
126
+ {
127
+ g_debug ("store_file: Failed to allocate memory for uploaded file." );
128
+ g_free (origname );
129
+ return -1 ;
130
+ }
131
+
132
+ files_add_translation (globals , origname , contents );
133
+ files_add_size_translation (globals , origname , bytes );
134
+
135
+ g_free (origname );
136
+ return 0 ;
137
+ }
138
+
42
139
/**
43
140
* Get the max number of hosts to test at the same time.
44
141
*/
0 commit comments