aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet_chat_util.c')
-rw-r--r--src/gnunet_chat_util.c148
1 files changed, 146 insertions, 2 deletions
diff --git a/src/gnunet_chat_util.c b/src/gnunet_chat_util.c
index 1a75ba9..cdbd46c 100644
--- a/src/gnunet_chat_util.c
+++ b/src/gnunet_chat_util.c
@@ -26,14 +26,14 @@
26 26
27void 27void
28util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member, 28util_shorthash_from_member (const struct GNUNET_MESSENGER_Contact *member,
29 struct GNUNET_ShortHashCode* shorthash) 29 struct GNUNET_ShortHashCode *shorthash)
30{ 30{
31 memset(shorthash, 0, sizeof(*shorthash)); 31 memset(shorthash, 0, sizeof(*shorthash));
32 GNUNET_memcpy(shorthash, &member, sizeof(member)); 32 GNUNET_memcpy(shorthash, &member, sizeof(member));
33} 33}
34 34
35void 35void
36util_set_name_field (const char *name, char** field) 36util_set_name_field (const char *name, char **field)
37{ 37{
38 if (*field) 38 if (*field)
39 GNUNET_free(*field); 39 GNUNET_free(*field);
@@ -43,3 +43,147 @@ util_set_name_field (const char *name, char** field)
43 else 43 else
44 *field = NULL; 44 *field = NULL;
45} 45}
46
47int
48util_hash_file (const char *filename, struct GNUNET_HashCode *hash)
49{
50 uint64_t size;
51
52 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
53 return GNUNET_SYSERR;
54
55 struct GNUNET_DISK_FileHandle *file = GNUNET_DISK_file_open(
56 filename, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_USER_READ
57 );
58
59 if (!file)
60 return GNUNET_SYSERR;
61
62 struct GNUNET_DISK_MapHandle *mapping;
63 const void* data = GNUNET_DISK_file_map(
64 file, &mapping, GNUNET_DISK_MAP_TYPE_READ, size
65 );
66
67 if (!data)
68 {
69 GNUNET_DISK_file_close(file);
70 return GNUNET_SYSERR;
71 }
72
73 GNUNET_CRYPTO_hash(data, size, hash);
74
75 GNUNET_DISK_file_unmap(mapping);
76 GNUNET_DISK_file_close(file);
77
78 return GNUNET_OK;
79}
80
81int
82util_encrypt_file (const char *filename,
83 const struct GNUNET_CRYPTO_SymmetricSessionKey *key)
84{
85 uint64_t size;
86
87 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
88 return GNUNET_SYSERR;
89
90 struct GNUNET_DISK_FileHandle *file = GNUNET_DISK_file_open(
91 filename, GNUNET_DISK_OPEN_READWRITE,
92 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
93 );
94
95 if (!file)
96 return GNUNET_SYSERR;
97
98 struct GNUNET_DISK_MapHandle *mapping;
99 void* data = GNUNET_DISK_file_map(
100 file, &mapping, GNUNET_DISK_MAP_TYPE_READWRITE, size
101 );
102
103 if (!data)
104 {
105 GNUNET_DISK_file_close(file);
106 return GNUNET_SYSERR;
107 }
108
109 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
110 memset(&iv, 0, sizeof(iv));
111
112 ssize_t result = GNUNET_CRYPTO_symmetric_encrypt(data, size, key, &iv, data);
113
114 if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping))
115 result = -1;
116
117 if (GNUNET_OK != GNUNET_DISK_file_sync(file))
118 result = -1;
119
120 if (GNUNET_OK != GNUNET_DISK_file_close(file))
121 result = -1;
122
123 if (result < 0)
124 return GNUNET_SYSERR;
125
126 return GNUNET_OK;
127}
128
129int
130util_decrypt_file (const char *filename,
131 const struct GNUNET_CRYPTO_SymmetricSessionKey *key)
132{
133 uint64_t size;
134
135 if (GNUNET_OK != GNUNET_DISK_file_size(filename, &size, GNUNET_NO, GNUNET_YES))
136 return GNUNET_SYSERR;
137
138 struct GNUNET_DISK_FileHandle *file = GNUNET_DISK_file_open(
139 filename, GNUNET_DISK_OPEN_READWRITE,
140 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
141 );
142
143 if (!file)
144 return GNUNET_SYSERR;
145
146 struct GNUNET_DISK_MapHandle *mapping;
147 void* data = GNUNET_DISK_file_map(
148 file, &mapping, GNUNET_DISK_MAP_TYPE_READWRITE, size
149 );
150
151 if (!data)
152 {
153 GNUNET_DISK_file_close(file);
154 return GNUNET_SYSERR;
155 }
156
157 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
158 memset(&iv, 0, sizeof(iv));
159
160 ssize_t result = GNUNET_CRYPTO_symmetric_decrypt(data, size, key, &iv, data);
161
162 if (GNUNET_OK != GNUNET_DISK_file_unmap(mapping))
163 result = -1;
164
165 if (GNUNET_OK != GNUNET_DISK_file_sync(file))
166 result = -1;
167
168 if (GNUNET_OK != GNUNET_DISK_file_close(file))
169 result = -1;
170
171 if (result < 0)
172 return GNUNET_SYSERR;
173
174 return GNUNET_OK;
175}
176
177int
178util_get_filename (const char *directory, const struct GNUNET_HashCode *hash,
179 char **filename)
180{
181 return GNUNET_asprintf (
182 filename,
183 "%s%s%c%s",
184 directory,
185 "files",
186 DIR_SEPARATOR,
187 GNUNET_h2s_full(hash)
188 );
189}