summaryrefslogtreecommitdiff
path: root/src/fs/fs_uri.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs/fs_uri.c')
-rw-r--r--src/fs/fs_uri.c1774
1 files changed, 899 insertions, 875 deletions
diff --git a/src/fs/fs_uri.c b/src/fs/fs_uri.c
index 5a67afbc0..c9ce19114 100644
--- a/src/fs/fs_uri.c
+++ b/src/fs/fs_uri.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file fs/fs_uri.c 22 * @file fs/fs_uri.c
@@ -100,43 +100,47 @@
100 * @return #GNUNET_OK on success 100 * @return #GNUNET_OK on success
101 */ 101 */
102int 102int
103GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri, 103GNUNET_FS_uri_to_key(const struct GNUNET_FS_Uri *uri,
104 struct GNUNET_HashCode *key) 104 struct GNUNET_HashCode *key)
105{ 105{
106 switch (uri->type) 106 switch (uri->type)
107 {
108 case GNUNET_FS_URI_CHK:
109 *key = uri->data.chk.chk.query;
110 return GNUNET_OK;
111 case GNUNET_FS_URI_SKS:
112 GNUNET_CRYPTO_hash (uri->data.sks.identifier,
113 strlen (uri->data.sks.identifier),
114 key);
115 return GNUNET_OK;
116 case GNUNET_FS_URI_KSK:
117 if (uri->data.ksk.keywordCount > 0)
118 { 107 {
119 GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0], 108 case GNUNET_FS_URI_CHK:
120 strlen (uri->data.ksk.keywords[0]), 109 *key = uri->data.chk.chk.query;
121 key);
122 return GNUNET_OK; 110 return GNUNET_OK;
123 } 111
124 else 112 case GNUNET_FS_URI_SKS:
125 { 113 GNUNET_CRYPTO_hash(uri->data.sks.identifier,
126 memset (key, 0, sizeof (struct GNUNET_HashCode)); 114 strlen(uri->data.sks.identifier),
115 key);
116 return GNUNET_OK;
117
118 case GNUNET_FS_URI_KSK:
119 if (uri->data.ksk.keywordCount > 0)
120 {
121 GNUNET_CRYPTO_hash(uri->data.ksk.keywords[0],
122 strlen(uri->data.ksk.keywords[0]),
123 key);
124 return GNUNET_OK;
125 }
126 else
127 {
128 memset(key, 0, sizeof(struct GNUNET_HashCode));
129 return GNUNET_SYSERR;
130 }
131 break;
132
133 case GNUNET_FS_URI_LOC:
134 GNUNET_CRYPTO_hash(&uri->data.loc.fi,
135 sizeof(struct FileIdentifier) +
136 sizeof(struct GNUNET_PeerIdentity),
137 key);
138 return GNUNET_OK;
139
140 default:
141 memset(key, 0, sizeof(struct GNUNET_HashCode));
127 return GNUNET_SYSERR; 142 return GNUNET_SYSERR;
128 } 143 }
129 break;
130 case GNUNET_FS_URI_LOC:
131 GNUNET_CRYPTO_hash (&uri->data.loc.fi,
132 sizeof (struct FileIdentifier) +
133 sizeof (struct GNUNET_PeerIdentity),
134 key);
135 return GNUNET_OK;
136 default:
137 memset (key, 0, sizeof (struct GNUNET_HashCode));
138 return GNUNET_SYSERR;
139 }
140} 144}
141 145
142 146
@@ -148,7 +152,7 @@ GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
148 * @return string with the keywords 152 * @return string with the keywords
149 */ 153 */
150char * 154char *
151GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri) 155GNUNET_FS_uri_ksk_to_string_fancy(const struct GNUNET_FS_Uri *uri)
152{ 156{
153 size_t n; 157 size_t n;
154 char *ret; 158 char *ret;
@@ -158,45 +162,45 @@ GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
158 unsigned int keywordCount; 162 unsigned int keywordCount;
159 163
160 if ((NULL == uri) || (GNUNET_FS_URI_KSK != uri->type)) 164 if ((NULL == uri) || (GNUNET_FS_URI_KSK != uri->type))
161 { 165 {
162 GNUNET_break (0); 166 GNUNET_break(0);
163 return NULL; 167 return NULL;
164 } 168 }
165 keywords = uri->data.ksk.keywords; 169 keywords = uri->data.ksk.keywords;
166 keywordCount = uri->data.ksk.keywordCount; 170 keywordCount = uri->data.ksk.keywordCount;
167 n = keywordCount + 1; 171 n = keywordCount + 1;
168 for (i = 0; i < keywordCount; i++) 172 for (i = 0; i < keywordCount; i++)
169 {
170 keyword = keywords[i];
171 n += strlen (keyword) - 1;
172 if (NULL != strstr (&keyword[1], " "))
173 n += 2;
174 if (keyword[0] == '+')
175 n++;
176 }
177 ret = GNUNET_malloc (n);
178 strcpy (ret, "");
179 for (i = 0; i < keywordCount; i++)
180 {
181 keyword = keywords[i];
182 if (NULL != strstr (&keyword[1], " "))
183 { 173 {
184 strcat (ret, "\""); 174 keyword = keywords[i];
175 n += strlen(keyword) - 1;
176 if (NULL != strstr(&keyword[1], " "))
177 n += 2;
185 if (keyword[0] == '+') 178 if (keyword[0] == '+')
186 strcat (ret, keyword); 179 n++;
187 else
188 strcat (ret, &keyword[1]);
189 strcat (ret, "\"");
190 } 180 }
191 else 181 ret = GNUNET_malloc(n);
182 strcpy(ret, "");
183 for (i = 0; i < keywordCount; i++)
192 { 184 {
193 if (keyword[0] == '+') 185 keyword = keywords[i];
194 strcat (ret, keyword); 186 if (NULL != strstr(&keyword[1], " "))
187 {
188 strcat(ret, "\"");
189 if (keyword[0] == '+')
190 strcat(ret, keyword);
191 else
192 strcat(ret, &keyword[1]);
193 strcat(ret, "\"");
194 }
195 else 195 else
196 strcat (ret, &keyword[1]); 196 {
197 if (keyword[0] == '+')
198 strcat(ret, keyword);
199 else
200 strcat(ret, &keyword[1]);
201 }
202 strcat(ret, " ");
197 } 203 }
198 strcat (ret, " ");
199 }
200 return ret; 204 return ret;
201} 205}
202 206
@@ -212,7 +216,7 @@ GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
212 * @return decodded string with leading space (or preserved plus) 216 * @return decodded string with leading space (or preserved plus)
213 */ 217 */
214static char * 218static char *
215percent_decode_keyword (const char *in, char **emsg) 219percent_decode_keyword(const char *in, char **emsg)
216{ 220{
217 char *out; 221 char *out;
218 char *ret; 222 char *ret;
@@ -220,44 +224,44 @@ percent_decode_keyword (const char *in, char **emsg)
220 unsigned int wpos; 224 unsigned int wpos;
221 unsigned int hx; 225 unsigned int hx;
222 226
223 out = GNUNET_strdup (in); 227 out = GNUNET_strdup(in);
224 rpos = 0; 228 rpos = 0;
225 wpos = 0; 229 wpos = 0;
226 while (out[rpos] != '\0') 230 while (out[rpos] != '\0')
227 {
228 if (out[rpos] == '%')
229 {
230 if (1 != sscanf (&out[rpos + 1], "%2X", &hx))
231 {
232 GNUNET_free (out);
233 *emsg = GNUNET_strdup (
234 _ (/* xgettext:no-c-format */
235 "Malformed KSK URI (`%' must be followed by HEX number)"));
236 return NULL;
237 }
238 rpos += 3;
239 if (hx == '"')
240 continue; /* skip double quote */
241 out[wpos++] = (char) hx;
242 }
243 else
244 { 231 {
245 out[wpos++] = out[rpos++]; 232 if (out[rpos] == '%')
233 {
234 if (1 != sscanf(&out[rpos + 1], "%2X", &hx))
235 {
236 GNUNET_free(out);
237 *emsg = GNUNET_strdup(
238 _(/* xgettext:no-c-format */
239 "Malformed KSK URI (`%' must be followed by HEX number)"));
240 return NULL;
241 }
242 rpos += 3;
243 if (hx == '"')
244 continue; /* skip double quote */
245 out[wpos++] = (char)hx;
246 }
247 else
248 {
249 out[wpos++] = out[rpos++];
250 }
246 } 251 }
247 }
248 out[wpos] = '\0'; 252 out[wpos] = '\0';
249 if (out[0] == '+') 253 if (out[0] == '+')
250 { 254 {
251 ret = GNUNET_strdup (out); 255 ret = GNUNET_strdup(out);
252 } 256 }
253 else 257 else
254 { 258 {
255 /* need to prefix with space */ 259 /* need to prefix with space */
256 ret = GNUNET_malloc (strlen (out) + 2); 260 ret = GNUNET_malloc(strlen(out) + 2);
257 strcpy (ret, " "); 261 strcpy(ret, " ");
258 strcat (ret, out); 262 strcat(ret, out);
259 } 263 }
260 GNUNET_free (out); 264 GNUNET_free(out);
261 return ret; 265 return ret;
262} 266}
263 267
@@ -271,7 +275,7 @@ percent_decode_keyword (const char *in, char **emsg)
271 * @return NULL on error, otherwise the KSK URI 275 * @return NULL on error, otherwise the KSK URI
272 */ 276 */
273static struct GNUNET_FS_Uri * 277static struct GNUNET_FS_Uri *
274uri_ksk_parse (const char *s, char **emsg) 278uri_ksk_parse(const char *s, char **emsg)
275{ 279{
276 struct GNUNET_FS_Uri *ret; 280 struct GNUNET_FS_Uri *ret;
277 char **keywords; 281 char **keywords;
@@ -283,74 +287,74 @@ uri_ksk_parse (const char *s, char **emsg)
283 char *dup; 287 char *dup;
284 int saw_quote; 288 int saw_quote;
285 289
286 slen = strlen (s); 290 slen = strlen(s);
287 pos = strlen (GNUNET_FS_URI_KSK_PREFIX); 291 pos = strlen(GNUNET_FS_URI_KSK_PREFIX);
288 if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX, pos))) 292 if ((slen <= pos) || (0 != strncmp(s, GNUNET_FS_URI_KSK_PREFIX, pos)))
289 return NULL; /* not KSK URI */ 293 return NULL; /* not KSK URI */
290 if ((s[slen - 1] == '+') || (s[pos] == '+')) 294 if ((s[slen - 1] == '+') || (s[pos] == '+'))
291 { 295 {
292 *emsg = 296 *emsg =
293 GNUNET_strdup (_ ("Malformed KSK URI (must not begin or end with `+')")); 297 GNUNET_strdup(_("Malformed KSK URI (must not begin or end with `+')"));
294 return NULL; 298 return NULL;
295 } 299 }
296 max = 1; 300 max = 1;
297 saw_quote = 0; 301 saw_quote = 0;
298 for (i = pos; i < slen; i++) 302 for (i = pos; i < slen; i++)
299 {
300 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
301 {
302 saw_quote = (saw_quote + 1) % 2;
303 i += 3;
304 continue;
305 }
306 if ((s[i] == '+') && (saw_quote == 0))
307 { 303 {
308 max++; 304 if ((s[i] == '%') && (&s[i] == strstr(&s[i], "%22")))
309 if (s[i - 1] == '+') 305 {
310 { 306 saw_quote = (saw_quote + 1) % 2;
311 *emsg = GNUNET_strdup (_ ("Malformed KSK URI (`++' not allowed)")); 307 i += 3;
312 return NULL; 308 continue;
313 } 309 }
310 if ((s[i] == '+') && (saw_quote == 0))
311 {
312 max++;
313 if (s[i - 1] == '+')
314 {
315 *emsg = GNUNET_strdup(_("Malformed KSK URI (`++' not allowed)"));
316 return NULL;
317 }
318 }
314 } 319 }
315 }
316 if (saw_quote == 1) 320 if (saw_quote == 1)
317 {
318 *emsg = GNUNET_strdup (_ ("Malformed KSK URI (quotes not balanced)"));
319 return NULL;
320 }
321 iret = max;
322 dup = GNUNET_strdup (s);
323 keywords = GNUNET_new_array (max, char *);
324 for (i = slen - 1; i >= (int) pos; i--)
325 {
326 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
327 { 321 {
328 saw_quote = (saw_quote + 1) % 2; 322 *emsg = GNUNET_strdup(_("Malformed KSK URI (quotes not balanced)"));
329 continue; 323 return NULL;
330 } 324 }
331 if ((dup[i] == '+') && (saw_quote == 0)) 325 iret = max;
326 dup = GNUNET_strdup(s);
327 keywords = GNUNET_new_array(max, char *);
328 for (i = slen - 1; i >= (int)pos; i--)
332 { 329 {
333 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg); 330 if ((s[i] == '%') && (&s[i] == strstr(&s[i], "%22")))
334 if (NULL == keywords[max]) 331 {
335 goto CLEANUP; 332 saw_quote = (saw_quote + 1) % 2;
336 dup[i] = '\0'; 333 continue;
334 }
335 if ((dup[i] == '+') && (saw_quote == 0))
336 {
337 keywords[--max] = percent_decode_keyword(&dup[i + 1], emsg);
338 if (NULL == keywords[max])
339 goto CLEANUP;
340 dup[i] = '\0';
341 }
337 } 342 }
338 } 343 keywords[--max] = percent_decode_keyword(&dup[pos], emsg);
339 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
340 if (NULL == keywords[max]) 344 if (NULL == keywords[max])
341 goto CLEANUP; 345 goto CLEANUP;
342 GNUNET_assert (0 == max); 346 GNUNET_assert(0 == max);
343 GNUNET_free (dup); 347 GNUNET_free(dup);
344 ret = GNUNET_new (struct GNUNET_FS_Uri); 348 ret = GNUNET_new(struct GNUNET_FS_Uri);
345 ret->type = GNUNET_FS_URI_KSK; 349 ret->type = GNUNET_FS_URI_KSK;
346 ret->data.ksk.keywordCount = iret; 350 ret->data.ksk.keywordCount = iret;
347 ret->data.ksk.keywords = keywords; 351 ret->data.ksk.keywords = keywords;
348 return ret; 352 return ret;
349CLEANUP: 353CLEANUP:
350 for (i = 0; i < max; i++) 354 for (i = 0; i < max; i++)
351 GNUNET_free_non_null (keywords[i]); 355 GNUNET_free_non_null(keywords[i]);
352 GNUNET_free (keywords); 356 GNUNET_free(keywords);
353 GNUNET_free (dup); 357 GNUNET_free(dup);
354 return NULL; 358 return NULL;
355} 359}
356 360
@@ -365,31 +369,31 @@ CLEANUP:
365 * @return NULL on error, SKS URI otherwise 369 * @return NULL on error, SKS URI otherwise
366 */ 370 */
367static struct GNUNET_FS_Uri * 371static struct GNUNET_FS_Uri *
368uri_sks_parse (const char *s, char **emsg) 372uri_sks_parse(const char *s, char **emsg)
369{ 373{
370 struct GNUNET_FS_Uri *ret; 374 struct GNUNET_FS_Uri *ret;
371 struct GNUNET_CRYPTO_EcdsaPublicKey ns; 375 struct GNUNET_CRYPTO_EcdsaPublicKey ns;
372 size_t pos; 376 size_t pos;
373 char *end; 377 char *end;
374 378
375 pos = strlen (GNUNET_FS_URI_SKS_PREFIX); 379 pos = strlen(GNUNET_FS_URI_SKS_PREFIX);
376 if ((strlen (s) <= pos) || (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX, pos))) 380 if ((strlen(s) <= pos) || (0 != strncmp(s, GNUNET_FS_URI_SKS_PREFIX, pos)))
377 return NULL; /* not an SKS URI */ 381 return NULL; /* not an SKS URI */
378 end = strchr (&s[pos], '/'); 382 end = strchr(&s[pos], '/');
379 if ((NULL == end) || 383 if ((NULL == end) ||
380 (GNUNET_OK != GNUNET_STRINGS_string_to_data (&s[pos], 384 (GNUNET_OK != GNUNET_STRINGS_string_to_data(&s[pos],
381 end - &s[pos], 385 end - &s[pos],
382 &ns, 386 &ns,
383 sizeof (ns)))) 387 sizeof(ns))))
384 { 388 {
385 *emsg = GNUNET_strdup (_ ("Malformed SKS URI (wrong syntax)")); 389 *emsg = GNUNET_strdup(_("Malformed SKS URI (wrong syntax)"));
386 return NULL; /* malformed */ 390 return NULL; /* malformed */
387 } 391 }
388 end++; /* skip over '/' */ 392 end++; /* skip over '/' */
389 ret = GNUNET_new (struct GNUNET_FS_Uri); 393 ret = GNUNET_new(struct GNUNET_FS_Uri);
390 ret->type = GNUNET_FS_URI_SKS; 394 ret->type = GNUNET_FS_URI_SKS;
391 ret->data.sks.ns = ns; 395 ret->data.sks.ns = ns;
392 ret->data.sks.identifier = GNUNET_strdup (end); 396 ret->data.sks.identifier = GNUNET_strdup(end);
393 return ret; 397 return ret;
394} 398}
395 399
@@ -404,46 +408,46 @@ uri_sks_parse (const char *s, char **emsg)
404 * @return NULL on error, CHK URI otherwise 408 * @return NULL on error, CHK URI otherwise
405 */ 409 */
406static struct GNUNET_FS_Uri * 410static struct GNUNET_FS_Uri *
407uri_chk_parse (const char *s, char **emsg) 411uri_chk_parse(const char *s, char **emsg)
408{ 412{
409 struct GNUNET_FS_Uri *ret; 413 struct GNUNET_FS_Uri *ret;
410 struct FileIdentifier fi; 414 struct FileIdentifier fi;
411 unsigned int pos; 415 unsigned int pos;
412 unsigned long long flen; 416 unsigned long long flen;
413 size_t slen; 417 size_t slen;
414 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; 418 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
415 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; 419 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
416 420
417 slen = strlen (s); 421 slen = strlen(s);
418 pos = strlen (GNUNET_FS_URI_CHK_PREFIX); 422 pos = strlen(GNUNET_FS_URI_CHK_PREFIX);
419 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) || 423 if ((slen < pos + 2 * sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
420 (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX, pos))) 424 (0 != strncmp(s, GNUNET_FS_URI_CHK_PREFIX, pos)))
421 return NULL; /* not a CHK URI */ 425 return NULL; /* not a CHK URI */
422 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') || 426 if ((s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
423 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.')) 427 (s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
424 { 428 {
425 *emsg = GNUNET_strdup (_ ("Malformed CHK URI (wrong syntax)")); 429 *emsg = GNUNET_strdup(_("Malformed CHK URI (wrong syntax)"));
426 return NULL; 430 return NULL;
427 } 431 }
428 GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)); 432 GNUNET_memcpy(h1, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
429 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0'; 433 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
430 GNUNET_memcpy (h2, 434 GNUNET_memcpy(h2,
431 &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)], 435 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
432 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)); 436 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
433 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0'; 437 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
434 438
435 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &fi.chk.key)) || 439 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string(h1, &fi.chk.key)) ||
436 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &fi.chk.query)) || 440 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string(h2, &fi.chk.query)) ||
437 (1 != 441 (1 !=
438 sscanf (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2], 442 sscanf(&s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
439 "%llu", 443 "%llu",
440 &flen))) 444 &flen)))
441 { 445 {
442 *emsg = GNUNET_strdup (_ ("Malformed CHK URI (failed to decode CHK)")); 446 *emsg = GNUNET_strdup(_("Malformed CHK URI (failed to decode CHK)"));
443 return NULL; 447 return NULL;
444 } 448 }
445 fi.file_length = GNUNET_htonll (flen); 449 fi.file_length = GNUNET_htonll(flen);
446 ret = GNUNET_new (struct GNUNET_FS_Uri); 450 ret = GNUNET_new(struct GNUNET_FS_Uri);
447 ret->type = GNUNET_FS_URI_CHK; 451 ret->type = GNUNET_FS_URI_CHK;
448 ret->data.chk = fi; 452 ret->data.chk = fi;
449 return ret; 453 return ret;
@@ -456,8 +460,7 @@ GNUNET_NETWORK_STRUCT_BEGIN
456 * assembled in memory to create or verify the signature of a location 460 * assembled in memory to create or verify the signature of a location
457 * URI. 461 * URI.
458 */ 462 */
459struct LocUriAssembly 463struct LocUriAssembly {
460{
461 /** 464 /**
462 * What is being signed (rest of this struct). 465 * What is being signed (rest of this struct).
463 */ 466 */
@@ -494,11 +497,11 @@ GNUNET_NETWORK_STRUCT_END
494 * @return NULL on error, valid LOC URI otherwise 497 * @return NULL on error, valid LOC URI otherwise
495 */ 498 */
496static struct GNUNET_FS_Uri * 499static struct GNUNET_FS_Uri *
497uri_loc_parse (const char *s, char **emsg) 500uri_loc_parse(const char *s, char **emsg)
498{ 501{
499 struct GNUNET_FS_Uri *uri; 502 struct GNUNET_FS_Uri *uri;
500 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; 503 char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
501 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]; 504 char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
502 unsigned int pos; 505 unsigned int pos;
503 unsigned int npos; 506 unsigned int npos;
504 unsigned long long exptime; 507 unsigned long long exptime;
@@ -508,113 +511,113 @@ uri_loc_parse (const char *s, char **emsg)
508 struct LocUriAssembly ass; 511 struct LocUriAssembly ass;
509 size_t slen; 512 size_t slen;
510 513
511 slen = strlen (s); 514 slen = strlen(s);
512 pos = strlen (GNUNET_FS_URI_LOC_PREFIX); 515 pos = strlen(GNUNET_FS_URI_LOC_PREFIX);
513 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) || 516 if ((slen < pos + 2 * sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
514 (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX, pos))) 517 (0 != strncmp(s, GNUNET_FS_URI_LOC_PREFIX, pos)))
515 return NULL; /* not a LOC URI */ 518 return NULL; /* not a LOC URI */
516 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') || 519 if ((s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
517 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.')) 520 (s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
518 { 521 {
519 *emsg = GNUNET_strdup (_ ("LOC URI malformed (wrong syntax)")); 522 *emsg = GNUNET_strdup(_("LOC URI malformed (wrong syntax)"));
520 return NULL; 523 return NULL;
521 } 524 }
522 GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)); 525 GNUNET_memcpy(h1, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
523 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0'; 526 h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
524 GNUNET_memcpy (h2, 527 GNUNET_memcpy(h2,
525 &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)], 528 &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
526 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)); 529 sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
527 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0'; 530 h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
528 531
529 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &ass.fi.chk.key)) || 532 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string(h1, &ass.fi.chk.key)) ||
530 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &ass.fi.chk.query)) || 533 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string(h2, &ass.fi.chk.query)) ||
531 (1 != 534 (1 !=
532 sscanf (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2], 535 sscanf(&s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
533 "%llu", 536 "%llu",
534 &flen))) 537 &flen)))
535 { 538 {
536 *emsg = GNUNET_strdup (_ ("LOC URI malformed (no CHK)")); 539 *emsg = GNUNET_strdup(_("LOC URI malformed (no CHK)"));
537 return NULL; 540 return NULL;
538 } 541 }
539 ass.fi.file_length = GNUNET_htonll (flen); 542 ass.fi.file_length = GNUNET_htonll(flen);
540 543
541 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2; 544 npos = pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
542 while ((s[npos] != '\0') && (s[npos] != '.')) 545 while ((s[npos] != '\0') && (s[npos] != '.'))
543 npos++; 546 npos++;
544 if (s[npos] == '\0') 547 if (s[npos] == '\0')
545 { 548 {
546 *emsg = GNUNET_strdup (_ ("LOC URI malformed (missing LOC)")); 549 *emsg = GNUNET_strdup(_("LOC URI malformed (missing LOC)"));
547 goto ERR; 550 goto ERR;
548 } 551 }
549 npos++; 552 npos++;
550 if ((strlen (&s[npos]) <= GNUNET_CRYPTO_PKEY_ASCII_LENGTH + 1) || 553 if ((strlen(&s[npos]) <= GNUNET_CRYPTO_PKEY_ASCII_LENGTH + 1) ||
551 ('.' != s[npos + GNUNET_CRYPTO_PKEY_ASCII_LENGTH])) 554 ('.' != s[npos + GNUNET_CRYPTO_PKEY_ASCII_LENGTH]))
552 { 555 {
553 *emsg = 556 *emsg =
554 GNUNET_strdup (_ ("LOC URI malformed (wrong syntax for public key)")); 557 GNUNET_strdup(_("LOC URI malformed (wrong syntax for public key)"));
555 } 558 }
556 if ( 559 if (
557 GNUNET_OK != 560 GNUNET_OK !=
558 GNUNET_CRYPTO_eddsa_public_key_from_string (&s[npos], 561 GNUNET_CRYPTO_eddsa_public_key_from_string(&s[npos],
559 GNUNET_CRYPTO_PKEY_ASCII_LENGTH, 562 GNUNET_CRYPTO_PKEY_ASCII_LENGTH,
560 &ass.peer.public_key)) 563 &ass.peer.public_key))
561 { 564 {
562 *emsg = 565 *emsg =
563 GNUNET_strdup (_ ("LOC URI malformed (could not decode public key)")); 566 GNUNET_strdup(_("LOC URI malformed (could not decode public key)"));
564 goto ERR; 567 goto ERR;
565 } 568 }
566 npos += GNUNET_CRYPTO_PKEY_ASCII_LENGTH; 569 npos += GNUNET_CRYPTO_PKEY_ASCII_LENGTH;
567 if (s[npos++] != '.') 570 if (s[npos++] != '.')
568 { 571 {
569 *emsg = GNUNET_strdup (_ ("LOC URI malformed (could not find signature)")); 572 *emsg = GNUNET_strdup(_("LOC URI malformed (could not find signature)"));
570 goto ERR; 573 goto ERR;
571 } 574 }
572 if ((strlen (&s[npos]) <= SIGNATURE_ASCII_LENGTH + 1) || 575 if ((strlen(&s[npos]) <= SIGNATURE_ASCII_LENGTH + 1) ||
573 ('.' != s[npos + SIGNATURE_ASCII_LENGTH])) 576 ('.' != s[npos + SIGNATURE_ASCII_LENGTH]))
574 { 577 {
575 *emsg = 578 *emsg =
576 GNUNET_strdup (_ ("LOC URI malformed (wrong syntax for signature)")); 579 GNUNET_strdup(_("LOC URI malformed (wrong syntax for signature)"));
577 goto ERR; 580 goto ERR;
578 } 581 }
579 if (GNUNET_OK != 582 if (GNUNET_OK !=
580 GNUNET_STRINGS_string_to_data (&s[npos], 583 GNUNET_STRINGS_string_to_data(&s[npos],
581 SIGNATURE_ASCII_LENGTH, 584 SIGNATURE_ASCII_LENGTH,
582 &sig, 585 &sig,
583 sizeof ( 586 sizeof(
584 struct GNUNET_CRYPTO_EddsaSignature))) 587 struct GNUNET_CRYPTO_EddsaSignature)))
585 { 588 {
586 *emsg = 589 *emsg =
587 GNUNET_strdup (_ ("LOC URI malformed (could not decode signature)")); 590 GNUNET_strdup(_("LOC URI malformed (could not decode signature)"));
588 goto ERR; 591 goto ERR;
589 } 592 }
590 npos += SIGNATURE_ASCII_LENGTH; 593 npos += SIGNATURE_ASCII_LENGTH;
591 if (s[npos++] != '.') 594 if (s[npos++] != '.')
592 { 595 {
593 *emsg = GNUNET_strdup ( 596 *emsg = GNUNET_strdup(
594 _ ("LOC URI malformed (wrong syntax for expiration time)")); 597 _("LOC URI malformed (wrong syntax for expiration time)"));
595 goto ERR; 598 goto ERR;
596 } 599 }
597 if (1 != sscanf (&s[npos], "%llu", &exptime)) 600 if (1 != sscanf(&s[npos], "%llu", &exptime))
598 { 601 {
599 *emsg = 602 *emsg =
600 GNUNET_strdup (_ ("LOC URI malformed (could not parse expiration time)")); 603 GNUNET_strdup(_("LOC URI malformed (could not parse expiration time)"));
601 goto ERR; 604 goto ERR;
602 } 605 }
603 ass.purpose.size = htonl (sizeof (struct LocUriAssembly)); 606 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
604 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT); 607 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
605 et.abs_value_us = exptime * 1000LL * 1000LL; 608 et.abs_value_us = exptime * 1000LL * 1000LL;
606 ass.exptime = GNUNET_TIME_absolute_hton (et); 609 ass.exptime = GNUNET_TIME_absolute_hton(et);
607 if (GNUNET_OK != 610 if (GNUNET_OK !=
608 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT, 611 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
609 &ass.purpose, 612 &ass.purpose,
610 &sig, 613 &sig,
611 &ass.peer.public_key)) 614 &ass.peer.public_key))
612 { 615 {
613 *emsg = 616 *emsg =
614 GNUNET_strdup (_ ("LOC URI malformed (signature failed validation)")); 617 GNUNET_strdup(_("LOC URI malformed (signature failed validation)"));
615 goto ERR; 618 goto ERR;
616 } 619 }
617 uri = GNUNET_new (struct GNUNET_FS_Uri); 620 uri = GNUNET_new(struct GNUNET_FS_Uri);
618 uri->type = GNUNET_FS_URI_LOC; 621 uri->type = GNUNET_FS_URI_LOC;
619 uri->data.loc.fi = ass.fi; 622 uri->data.loc.fi = ass.fi;
620 uri->data.loc.peer = ass.peer; 623 uri->data.loc.peer = ass.peer;
@@ -635,30 +638,30 @@ ERR:
635 * @return NULL on error 638 * @return NULL on error
636 */ 639 */
637struct GNUNET_FS_Uri * 640struct GNUNET_FS_Uri *
638GNUNET_FS_uri_parse (const char *uri, char **emsg) 641GNUNET_FS_uri_parse(const char *uri, char **emsg)
639{ 642{
640 struct GNUNET_FS_Uri *ret; 643 struct GNUNET_FS_Uri *ret;
641 char *msg; 644 char *msg;
642 645
643 if (NULL == uri) 646 if (NULL == uri)
644 { 647 {
645 GNUNET_break (0); 648 GNUNET_break(0);
646 if (NULL != emsg) 649 if (NULL != emsg)
647 *emsg = GNUNET_strdup (_ ("invalid argument")); 650 *emsg = GNUNET_strdup(_("invalid argument"));
648 return NULL; 651 return NULL;
649 } 652 }
650 if (NULL == emsg) 653 if (NULL == emsg)
651 emsg = &msg; 654 emsg = &msg;
652 *emsg = NULL; 655 *emsg = NULL;
653 if ((NULL != (ret = uri_chk_parse (uri, emsg))) || 656 if ((NULL != (ret = uri_chk_parse(uri, emsg))) ||
654 (NULL != (ret = uri_ksk_parse (uri, emsg))) || 657 (NULL != (ret = uri_ksk_parse(uri, emsg))) ||
655 (NULL != (ret = uri_sks_parse (uri, emsg))) || 658 (NULL != (ret = uri_sks_parse(uri, emsg))) ||
656 (NULL != (ret = uri_loc_parse (uri, emsg)))) 659 (NULL != (ret = uri_loc_parse(uri, emsg))))
657 return ret; 660 return ret;
658 if (NULL == *emsg) 661 if (NULL == *emsg)
659 *emsg = GNUNET_strdup (_ ("Unrecognized URI type")); 662 *emsg = GNUNET_strdup(_("Unrecognized URI type"));
660 if (emsg == &msg) 663 if (emsg == &msg)
661 GNUNET_free (msg); 664 GNUNET_free(msg);
662 return NULL; 665 return NULL;
663} 666}
664 667
@@ -669,27 +672,30 @@ GNUNET_FS_uri_parse (const char *uri, char **emsg)
669 * @param uri uri to free 672 * @param uri uri to free
670 */ 673 */
671void 674void
672GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri) 675GNUNET_FS_uri_destroy(struct GNUNET_FS_Uri *uri)
673{ 676{
674 unsigned int i; 677 unsigned int i;
675 678
676 switch (uri->type) 679 switch (uri->type)
677 { 680 {
678 case GNUNET_FS_URI_KSK: 681 case GNUNET_FS_URI_KSK:
679 for (i = 0; i < uri->data.ksk.keywordCount; i++) 682 for (i = 0; i < uri->data.ksk.keywordCount; i++)
680 GNUNET_free (uri->data.ksk.keywords[i]); 683 GNUNET_free(uri->data.ksk.keywords[i]);
681 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0); 684 GNUNET_array_grow(uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0);
682 break; 685 break;
683 case GNUNET_FS_URI_SKS: 686
684 GNUNET_free (uri->data.sks.identifier); 687 case GNUNET_FS_URI_SKS:
685 break; 688 GNUNET_free(uri->data.sks.identifier);
686 case GNUNET_FS_URI_LOC: 689 break;
687 break; 690
688 default: 691 case GNUNET_FS_URI_LOC:
689 /* do nothing */ 692 break;
690 break; 693
691 } 694 default:
692 GNUNET_free (uri); 695 /* do nothing */
696 break;
697 }
698 GNUNET_free(uri);
693} 699}
694 700
695 701
@@ -700,7 +706,7 @@ GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
700 * @return 0 if this is not a keyword URI 706 * @return 0 if this is not a keyword URI
701 */ 707 */
702unsigned int 708unsigned int
703GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri) 709GNUNET_FS_uri_ksk_get_keyword_count(const struct GNUNET_FS_Uri *uri)
704{ 710{
705 if (uri->type != GNUNET_FS_URI_KSK) 711 if (uri->type != GNUNET_FS_URI_KSK)
706 return 0; 712 return 0;
@@ -718,9 +724,9 @@ GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
718 * keywords iterated over until iterator aborted 724 * keywords iterated over until iterator aborted
719 */ 725 */
720int 726int
721GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri, 727GNUNET_FS_uri_ksk_get_keywords(const struct GNUNET_FS_Uri *uri,
722 GNUNET_FS_KeywordIterator iterator, 728 GNUNET_FS_KeywordIterator iterator,
723 void *iterator_cls) 729 void *iterator_cls)
724{ 730{
725 unsigned int i; 731 unsigned int i;
726 char *keyword; 732 char *keyword;
@@ -730,13 +736,13 @@ GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
730 if (NULL == iterator) 736 if (NULL == iterator)
731 return uri->data.ksk.keywordCount; 737 return uri->data.ksk.keywordCount;
732 for (i = 0; i < uri->data.ksk.keywordCount; i++) 738 for (i = 0; i < uri->data.ksk.keywordCount; i++)
733 { 739 {
734 keyword = uri->data.ksk.keywords[i]; 740 keyword = uri->data.ksk.keywords[i];
735 /* first character of keyword indicates 741 /* first character of keyword indicates
736 * if it is mandatory or not */ 742 * if it is mandatory or not */
737 if (GNUNET_OK != iterator (iterator_cls, &keyword[1], keyword[0] == '+')) 743 if (GNUNET_OK != iterator(iterator_cls, &keyword[1], keyword[0] == '+'))
738 return i; 744 return i;
739 } 745 }
740 return i; 746 return i;
741} 747}
742 748
@@ -750,23 +756,23 @@ GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
750 * @param is_mandatory is this keyword mandatory? 756 * @param is_mandatory is this keyword mandatory?
751 */ 757 */
752void 758void
753GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri, 759GNUNET_FS_uri_ksk_add_keyword(struct GNUNET_FS_Uri *uri,
754 const char *keyword, 760 const char *keyword,
755 int is_mandatory) 761 int is_mandatory)
756{ 762{
757 unsigned int i; 763 unsigned int i;
758 const char *old; 764 const char *old;
759 char *n; 765 char *n;
760 766
761 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK); 767 GNUNET_assert(uri->type == GNUNET_FS_URI_KSK);
762 for (i = 0; i < uri->data.ksk.keywordCount; i++) 768 for (i = 0; i < uri->data.ksk.keywordCount; i++)
763 { 769 {
764 old = uri->data.ksk.keywords[i]; 770 old = uri->data.ksk.keywords[i];
765 if (0 == strcmp (&old[1], keyword)) 771 if (0 == strcmp(&old[1], keyword))
766 return; 772 return;
767 } 773 }
768 GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword); 774 GNUNET_asprintf(&n, is_mandatory ? "+%s" : " %s", keyword);
769 GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n); 775 GNUNET_array_append(uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
770} 776}
771 777
772 778
@@ -778,27 +784,27 @@ GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
778 * @param keyword keyword to add 784 * @param keyword keyword to add
779 */ 785 */
780void 786void
781GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri, 787GNUNET_FS_uri_ksk_remove_keyword(struct GNUNET_FS_Uri *uri,
782 const char *keyword) 788 const char *keyword)
783{ 789{
784 unsigned int i; 790 unsigned int i;
785 char *old; 791 char *old;
786 792
787 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK); 793 GNUNET_assert(uri->type == GNUNET_FS_URI_KSK);
788 for (i = 0; i < uri->data.ksk.keywordCount; i++) 794 for (i = 0; i < uri->data.ksk.keywordCount; i++)
789 {
790 old = uri->data.ksk.keywords[i];
791 if (0 == strcmp (&old[1], keyword))
792 { 795 {
793 uri->data.ksk.keywords[i] = 796 old = uri->data.ksk.keywords[i];
794 uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1]; 797 if (0 == strcmp(&old[1], keyword))
795 GNUNET_array_grow (uri->data.ksk.keywords, 798 {
796 uri->data.ksk.keywordCount, 799 uri->data.ksk.keywords[i] =
797 uri->data.ksk.keywordCount - 1); 800 uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1];
798 GNUNET_free (old); 801 GNUNET_array_grow(uri->data.ksk.keywords,
799 return; 802 uri->data.ksk.keywordCount,
803 uri->data.ksk.keywordCount - 1);
804 GNUNET_free(old);
805 return;
806 }
800 } 807 }
801 }
802} 808}
803 809
804 810
@@ -810,8 +816,8 @@ GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
810 * @return #GNUNET_SYSERR if this is not a location URI, otherwise #GNUNET_OK 816 * @return #GNUNET_SYSERR if this is not a location URI, otherwise #GNUNET_OK
811 */ 817 */
812int 818int
813GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri, 819GNUNET_FS_uri_loc_get_peer_identity(const struct GNUNET_FS_Uri *uri,
814 struct GNUNET_PeerIdentity *peer) 820 struct GNUNET_PeerIdentity *peer)
815{ 821{
816 if (uri->type != GNUNET_FS_URI_LOC) 822 if (uri->type != GNUNET_FS_URI_LOC)
817 return GNUNET_SYSERR; 823 return GNUNET_SYSERR;
@@ -827,9 +833,9 @@ GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
827 * @return expiration time of the URI 833 * @return expiration time of the URI
828 */ 834 */
829struct GNUNET_TIME_Absolute 835struct GNUNET_TIME_Absolute
830GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri) 836GNUNET_FS_uri_loc_get_expiration(const struct GNUNET_FS_Uri *uri)
831{ 837{
832 GNUNET_assert (uri->type == GNUNET_FS_URI_LOC); 838 GNUNET_assert(uri->type == GNUNET_FS_URI_LOC);
833 return uri->data.loc.expirationTime; 839 return uri->data.loc.expirationTime;
834} 840}
835 841
@@ -841,13 +847,13 @@ GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
841 * @return NULL if argument is not a location URI 847 * @return NULL if argument is not a location URI
842 */ 848 */
843struct GNUNET_FS_Uri * 849struct GNUNET_FS_Uri *
844GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri) 850GNUNET_FS_uri_loc_get_uri(const struct GNUNET_FS_Uri *uri)
845{ 851{
846 struct GNUNET_FS_Uri *ret; 852 struct GNUNET_FS_Uri *ret;
847 853
848 if (uri->type != GNUNET_FS_URI_LOC) 854 if (uri->type != GNUNET_FS_URI_LOC)
849 return NULL; 855 return NULL;
850 ret = GNUNET_new (struct GNUNET_FS_Uri); 856 ret = GNUNET_new(struct GNUNET_FS_Uri);
851 ret->type = GNUNET_FS_URI_CHK; 857 ret->type = GNUNET_FS_URI_CHK;
852 ret->data.chk = uri->data.loc.fi; 858 ret->data.chk = uri->data.loc.fi;
853 return ret; 859 return ret;
@@ -867,9 +873,9 @@ GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
867 * @return the location URI, NULL on error 873 * @return the location URI, NULL on error
868 */ 874 */
869struct GNUNET_FS_Uri * 875struct GNUNET_FS_Uri *
870GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *base_uri, 876GNUNET_FS_uri_loc_create(const struct GNUNET_FS_Uri *base_uri,
871 const struct GNUNET_CRYPTO_EddsaPrivateKey *sign_key, 877 const struct GNUNET_CRYPTO_EddsaPrivateKey *sign_key,
872 struct GNUNET_TIME_Absolute expiration_time) 878 struct GNUNET_TIME_Absolute expiration_time)
873{ 879{
874 struct GNUNET_FS_Uri *uri; 880 struct GNUNET_FS_Uri *uri;
875 struct GNUNET_CRYPTO_EddsaPublicKey my_public_key; 881 struct GNUNET_CRYPTO_EddsaPublicKey my_public_key;
@@ -880,21 +886,21 @@ GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *base_uri,
880 return NULL; 886 return NULL;
881 /* we round expiration time to full seconds for SKS URIs */ 887 /* we round expiration time to full seconds for SKS URIs */
882 et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL; 888 et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL;
883 GNUNET_CRYPTO_eddsa_key_get_public (sign_key, &my_public_key); 889 GNUNET_CRYPTO_eddsa_key_get_public(sign_key, &my_public_key);
884 ass.purpose.size = htonl (sizeof (struct LocUriAssembly)); 890 ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
885 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT); 891 ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
886 ass.exptime = GNUNET_TIME_absolute_hton (et); 892 ass.exptime = GNUNET_TIME_absolute_hton(et);
887 ass.fi = base_uri->data.chk; 893 ass.fi = base_uri->data.chk;
888 ass.peer.public_key = my_public_key; 894 ass.peer.public_key = my_public_key;
889 uri = GNUNET_new (struct GNUNET_FS_Uri); 895 uri = GNUNET_new(struct GNUNET_FS_Uri);
890 uri->type = GNUNET_FS_URI_LOC; 896 uri->type = GNUNET_FS_URI_LOC;
891 uri->data.loc.fi = base_uri->data.chk; 897 uri->data.loc.fi = base_uri->data.chk;
892 uri->data.loc.expirationTime = et; 898 uri->data.loc.expirationTime = et;
893 uri->data.loc.peer.public_key = my_public_key; 899 uri->data.loc.peer.public_key = my_public_key;
894 GNUNET_assert (GNUNET_OK == 900 GNUNET_assert(GNUNET_OK ==
895 GNUNET_CRYPTO_eddsa_sign (sign_key, 901 GNUNET_CRYPTO_eddsa_sign(sign_key,
896 &ass.purpose, 902 &ass.purpose,
897 &uri->data.loc.contentSignature)); 903 &uri->data.loc.contentSignature));
898 return uri; 904 return uri;
899} 905}
900 906
@@ -907,15 +913,15 @@ GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *base_uri,
907 * @return an FS URI for the given namespace and identifier 913 * @return an FS URI for the given namespace and identifier
908 */ 914 */
909struct GNUNET_FS_Uri * 915struct GNUNET_FS_Uri *
910GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *ns, 916GNUNET_FS_uri_sks_create(const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
911 const char *id) 917 const char *id)
912{ 918{
913 struct GNUNET_FS_Uri *ns_uri; 919 struct GNUNET_FS_Uri *ns_uri;
914 920
915 ns_uri = GNUNET_new (struct GNUNET_FS_Uri); 921 ns_uri = GNUNET_new(struct GNUNET_FS_Uri);
916 ns_uri->type = GNUNET_FS_URI_SKS; 922 ns_uri->type = GNUNET_FS_URI_SKS;
917 ns_uri->data.sks.ns = *ns; 923 ns_uri->data.sks.ns = *ns;
918 ns_uri->data.sks.identifier = GNUNET_strdup (id); 924 ns_uri->data.sks.identifier = GNUNET_strdup(id);
919 return ns_uri; 925 return ns_uri;
920} 926}
921 927
@@ -930,8 +936,8 @@ GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
930 * @return merged URI, NULL on error 936 * @return merged URI, NULL on error
931 */ 937 */
932struct GNUNET_FS_Uri * 938struct GNUNET_FS_Uri *
933GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1, 939GNUNET_FS_uri_ksk_merge(const struct GNUNET_FS_Uri *u1,
934 const struct GNUNET_FS_Uri *u2) 940 const struct GNUNET_FS_Uri *u2)
935{ 941{
936 struct GNUNET_FS_Uri *ret; 942 struct GNUNET_FS_Uri *ret;
937 unsigned int kc; 943 unsigned int kc;
@@ -944,34 +950,34 @@ GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
944 if ((u1 == NULL) && (u2 == NULL)) 950 if ((u1 == NULL) && (u2 == NULL))
945 return NULL; 951 return NULL;
946 if (u1 == NULL) 952 if (u1 == NULL)
947 return GNUNET_FS_uri_dup (u2); 953 return GNUNET_FS_uri_dup(u2);
948 if (u2 == NULL) 954 if (u2 == NULL)
949 return GNUNET_FS_uri_dup (u1); 955 return GNUNET_FS_uri_dup(u1);
950 if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK)) 956 if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK))
951 { 957 {
952 GNUNET_break (0); 958 GNUNET_break(0);
953 return NULL; 959 return NULL;
954 } 960 }
955 kc = u1->data.ksk.keywordCount; 961 kc = u1->data.ksk.keywordCount;
956 kl = GNUNET_new_array (kc + u2->data.ksk.keywordCount, char *); 962 kl = GNUNET_new_array(kc + u2->data.ksk.keywordCount, char *);
957 for (i = 0; i < u1->data.ksk.keywordCount; i++) 963 for (i = 0; i < u1->data.ksk.keywordCount; i++)
958 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]); 964 kl[i] = GNUNET_strdup(u1->data.ksk.keywords[i]);
959 for (i = 0; i < u2->data.ksk.keywordCount; i++) 965 for (i = 0; i < u2->data.ksk.keywordCount; i++)
960 { 966 {
961 kp = u2->data.ksk.keywords[i]; 967 kp = u2->data.ksk.keywords[i];
962 found = 0; 968 found = 0;
963 for (j = 0; j < u1->data.ksk.keywordCount; j++) 969 for (j = 0; j < u1->data.ksk.keywordCount; j++)
964 if (0 == strcmp (kp + 1, kl[j] + 1)) 970 if (0 == strcmp(kp + 1, kl[j] + 1))
965 { 971 {
966 found = 1; 972 found = 1;
967 if (kp[0] == '+') 973 if (kp[0] == '+')
968 kl[j][0] = '+'; 974 kl[j][0] = '+';
969 break; 975 break;
970 } 976 }
971 if (0 == found) 977 if (0 == found)
972 kl[kc++] = GNUNET_strdup (kp); 978 kl[kc++] = GNUNET_strdup(kp);
973 } 979 }
974 ret = GNUNET_new (struct GNUNET_FS_Uri); 980 ret = GNUNET_new(struct GNUNET_FS_Uri);
975 ret->type = GNUNET_FS_URI_KSK; 981 ret->type = GNUNET_FS_URI_KSK;
976 ret->data.ksk.keywordCount = kc; 982 ret->data.ksk.keywordCount = kc;
977 ret->data.ksk.keywords = kl; 983 ret->data.ksk.keywords = kl;
@@ -986,43 +992,46 @@ GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
986 * @return copy of the URI 992 * @return copy of the URI
987 */ 993 */
988struct GNUNET_FS_Uri * 994struct GNUNET_FS_Uri *
989GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri) 995GNUNET_FS_uri_dup(const struct GNUNET_FS_Uri *uri)
990{ 996{
991 struct GNUNET_FS_Uri *ret; 997 struct GNUNET_FS_Uri *ret;
992 unsigned int i; 998 unsigned int i;
993 999
994 if (uri == NULL) 1000 if (uri == NULL)
995 return NULL; 1001 return NULL;
996 ret = GNUNET_new (struct GNUNET_FS_Uri); 1002 ret = GNUNET_new(struct GNUNET_FS_Uri);
997 GNUNET_memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri)); 1003 GNUNET_memcpy(ret, uri, sizeof(struct GNUNET_FS_Uri));
998 switch (ret->type) 1004 switch (ret->type)
999 {
1000 case GNUNET_FS_URI_KSK:
1001 if (ret->data.ksk.keywordCount >=
1002 GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
1003 { 1005 {
1004 GNUNET_break (0); 1006 case GNUNET_FS_URI_KSK:
1005 GNUNET_free (ret); 1007 if (ret->data.ksk.keywordCount >=
1006 return NULL; 1008 GNUNET_MAX_MALLOC_CHECKED / sizeof(char *))
1007 } 1009 {
1008 if (ret->data.ksk.keywordCount > 0) 1010 GNUNET_break(0);
1009 { 1011 GNUNET_free(ret);
1010 ret->data.ksk.keywords = 1012 return NULL;
1011 GNUNET_new_array (ret->data.ksk.keywordCount, char *); 1013 }
1012 for (i = 0; i < ret->data.ksk.keywordCount; i++) 1014 if (ret->data.ksk.keywordCount > 0)
1013 ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]); 1015 {
1016 ret->data.ksk.keywords =
1017 GNUNET_new_array(ret->data.ksk.keywordCount, char *);
1018 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1019 ret->data.ksk.keywords[i] = GNUNET_strdup(uri->data.ksk.keywords[i]);
1020 }
1021 else
1022 ret->data.ksk.keywords = NULL; /* just to be sure */
1023 break;
1024
1025 case GNUNET_FS_URI_SKS:
1026 ret->data.sks.identifier = GNUNET_strdup(uri->data.sks.identifier);
1027 break;
1028
1029 case GNUNET_FS_URI_LOC:
1030 break;
1031
1032 default:
1033 break;
1014 } 1034 }
1015 else
1016 ret->data.ksk.keywords = NULL; /* just to be sure */
1017 break;
1018 case GNUNET_FS_URI_SKS:
1019 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1020 break;
1021 case GNUNET_FS_URI_LOC:
1022 break;
1023 default:
1024 break;
1025 }
1026 return ret; 1035 return ret;
1027} 1036}
1028 1037
@@ -1045,7 +1054,7 @@ GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1045 * if keywords is not legal (i.e. empty). 1054 * if keywords is not legal (i.e. empty).
1046 */ 1055 */
1047struct GNUNET_FS_Uri * 1056struct GNUNET_FS_Uri *
1048GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg) 1057GNUNET_FS_uri_ksk_create(const char *keywords, char **emsg)
1049{ 1058{
1050 char **keywordarr; 1059 char **keywordarr;
1051 unsigned int num_Words; 1060 unsigned int num_Words;
@@ -1056,68 +1065,68 @@ GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg)
1056 int saw_quote; 1065 int saw_quote;
1057 1066
1058 if (keywords == NULL) 1067 if (keywords == NULL)
1059 { 1068 {
1060 *emsg = GNUNET_strdup (_ ("No keywords specified!\n")); 1069 *emsg = GNUNET_strdup(_("No keywords specified!\n"));
1061 GNUNET_break (0); 1070 GNUNET_break(0);
1062 return NULL; 1071 return NULL;
1063 } 1072 }
1064 searchString = GNUNET_strdup (keywords); 1073 searchString = GNUNET_strdup(keywords);
1065 num_Words = 0; 1074 num_Words = 0;
1066 inWord = 0; 1075 inWord = 0;
1067 saw_quote = 0; 1076 saw_quote = 0;
1068 pos = searchString; 1077 pos = searchString;
1069 while ('\0' != *pos) 1078 while ('\0' != *pos)
1070 {
1071 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1072 { 1079 {
1073 inWord = 0; 1080 if ((saw_quote == 0) && (isspace((unsigned char)*pos)))
1081 {
1082 inWord = 0;
1083 }
1084 else if (0 == inWord)
1085 {
1086 inWord = 1;
1087 ++num_Words;
1088 }
1089 if ('"' == *pos)
1090 saw_quote = (saw_quote + 1) % 2;
1091 pos++;
1074 } 1092 }
1075 else if (0 == inWord) 1093 if (num_Words == 0)
1076 { 1094 {
1077 inWord = 1; 1095 GNUNET_free(searchString);
1078 ++num_Words; 1096 *emsg = GNUNET_strdup(_("No keywords specified!\n"));
1097 return NULL;
1079 } 1098 }
1080 if ('"' == *pos)
1081 saw_quote = (saw_quote + 1) % 2;
1082 pos++;
1083 }
1084 if (num_Words == 0)
1085 {
1086 GNUNET_free (searchString);
1087 *emsg = GNUNET_strdup (_ ("No keywords specified!\n"));
1088 return NULL;
1089 }
1090 if (saw_quote != 0) 1099 if (saw_quote != 0)
1091 { 1100 {
1092 GNUNET_free (searchString); 1101 GNUNET_free(searchString);
1093 *emsg = GNUNET_strdup (_ ("Number of double-quotes not balanced!\n")); 1102 *emsg = GNUNET_strdup(_("Number of double-quotes not balanced!\n"));
1094 return NULL; 1103 return NULL;
1095 } 1104 }
1096 keywordarr = GNUNET_new_array (num_Words, char *); 1105 keywordarr = GNUNET_new_array(num_Words, char *);
1097 num_Words = 0; 1106 num_Words = 0;
1098 inWord = 0; 1107 inWord = 0;
1099 pos = searchString; 1108 pos = searchString;
1100 while ('\0' != *pos) 1109 while ('\0' != *pos)
1101 {
1102 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1103 {
1104 inWord = 0;
1105 *pos = '\0';
1106 }
1107 else if (0 == inWord)
1108 { 1110 {
1109 keywordarr[num_Words] = pos; 1111 if ((saw_quote == 0) && (isspace((unsigned char)*pos)))
1110 inWord = 1; 1112 {
1111 ++num_Words; 1113 inWord = 0;
1114 *pos = '\0';
1115 }
1116 else if (0 == inWord)
1117 {
1118 keywordarr[num_Words] = pos;
1119 inWord = 1;
1120 ++num_Words;
1121 }
1122 if ('"' == *pos)
1123 saw_quote = (saw_quote + 1) % 2;
1124 pos++;
1112 } 1125 }
1113 if ('"' == *pos)
1114 saw_quote = (saw_quote + 1) % 2;
1115 pos++;
1116 }
1117 uri = 1126 uri =
1118 GNUNET_FS_uri_ksk_create_from_args (num_Words, (const char **) keywordarr); 1127 GNUNET_FS_uri_ksk_create_from_args(num_Words, (const char **)keywordarr);
1119 GNUNET_free (keywordarr); 1128 GNUNET_free(keywordarr);
1120 GNUNET_free (searchString); 1129 GNUNET_free(searchString);
1121 return uri; 1130 return uri;
1122} 1131}
1123 1132
@@ -1140,7 +1149,7 @@ GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg)
1140 * if keywords is not legal (i.e. empty). 1149 * if keywords is not legal (i.e. empty).
1141 */ 1150 */
1142struct GNUNET_FS_Uri * 1151struct GNUNET_FS_Uri *
1143GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv) 1152GNUNET_FS_uri_ksk_create_from_args(unsigned int argc, const char **argv)
1144{ 1153{
1145 unsigned int i; 1154 unsigned int i;
1146 struct GNUNET_FS_Uri *uri; 1155 struct GNUNET_FS_Uri *uri;
@@ -1155,36 +1164,36 @@ GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv)
1155 /* allow URI to be given as one and only keyword and 1164 /* allow URI to be given as one and only keyword and
1156 * handle accordingly */ 1165 * handle accordingly */
1157 emsg = NULL; 1166 emsg = NULL;
1158 if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) && 1167 if ((argc == 1) && (strlen(argv[0]) > strlen(GNUNET_FS_URI_PREFIX)) &&
1159 (0 == strncmp (argv[0], 1168 (0 == strncmp(argv[0],
1160 GNUNET_FS_URI_PREFIX, 1169 GNUNET_FS_URI_PREFIX,
1161 strlen (GNUNET_FS_URI_PREFIX))) && 1170 strlen(GNUNET_FS_URI_PREFIX))) &&
1162 (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg)))) 1171 (NULL != (uri = GNUNET_FS_uri_parse(argv[0], &emsg))))
1163 return uri; 1172 return uri;
1164 GNUNET_free_non_null (emsg); 1173 GNUNET_free_non_null(emsg);
1165 uri = GNUNET_new (struct GNUNET_FS_Uri); 1174 uri = GNUNET_new(struct GNUNET_FS_Uri);
1166 uri->type = GNUNET_FS_URI_KSK; 1175 uri->type = GNUNET_FS_URI_KSK;
1167 uri->data.ksk.keywordCount = argc; 1176 uri->data.ksk.keywordCount = argc;
1168 uri->data.ksk.keywords = GNUNET_new_array (argc, char *); 1177 uri->data.ksk.keywords = GNUNET_new_array(argc, char *);
1169 for (i = 0; i < argc; i++) 1178 for (i = 0; i < argc; i++)
1170 {
1171 keyword = argv[i];
1172 if (keyword[0] == '+')
1173 val = GNUNET_strdup (keyword);
1174 else
1175 GNUNET_asprintf (&val, " %s", keyword);
1176 r = val;
1177 w = val;
1178 while ('\0' != *r)
1179 { 1179 {
1180 if ('"' == *r) 1180 keyword = argv[i];
1181 r++; 1181 if (keyword[0] == '+')
1182 val = GNUNET_strdup(keyword);
1182 else 1183 else
1183 *(w++) = *(r++); 1184 GNUNET_asprintf(&val, " %s", keyword);
1185 r = val;
1186 w = val;
1187 while ('\0' != *r)
1188 {
1189 if ('"' == *r)
1190 r++;
1191 else
1192 *(w++) = *(r++);
1193 }
1194 *w = '\0';
1195 uri->data.ksk.keywords[i] = val;
1184 } 1196 }
1185 *w = '\0';
1186 uri->data.ksk.keywords[i] = val;
1187 }
1188 return uri; 1197 return uri;
1189} 1198}
1190 1199
@@ -1197,62 +1206,66 @@ GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv)
1197 * @return #GNUNET_YES if the URIs are equal 1206 * @return #GNUNET_YES if the URIs are equal
1198 */ 1207 */
1199int 1208int
1200GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1, 1209GNUNET_FS_uri_test_equal(const struct GNUNET_FS_Uri *u1,
1201 const struct GNUNET_FS_Uri *u2) 1210 const struct GNUNET_FS_Uri *u2)
1202{ 1211{
1203 int ret; 1212 int ret;
1204 unsigned int i; 1213 unsigned int i;
1205 unsigned int j; 1214 unsigned int j;
1206 1215
1207 GNUNET_assert (u1 != NULL); 1216 GNUNET_assert(u1 != NULL);
1208 GNUNET_assert (u2 != NULL); 1217 GNUNET_assert(u2 != NULL);
1209 if (u1->type != u2->type) 1218 if (u1->type != u2->type)
1210 return GNUNET_NO; 1219 return GNUNET_NO;
1211 switch (u1->type) 1220 switch (u1->type)
1212 { 1221 {
1213 case GNUNET_FS_URI_CHK: 1222 case GNUNET_FS_URI_CHK:
1214 if (0 == 1223 if (0 ==
1215 memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier))) 1224 memcmp(&u1->data.chk, &u2->data.chk, sizeof(struct FileIdentifier)))
1216 return GNUNET_YES; 1225 return GNUNET_YES;
1217 return GNUNET_NO; 1226 return GNUNET_NO;
1218 case GNUNET_FS_URI_SKS:
1219 if ((0 == memcmp (&u1->data.sks.ns,
1220 &u2->data.sks.ns,
1221 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) &&
1222 (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
1223 1227
1224 return GNUNET_YES; 1228 case GNUNET_FS_URI_SKS:
1225 return GNUNET_NO; 1229 if ((0 == memcmp(&u1->data.sks.ns,
1226 case GNUNET_FS_URI_KSK: 1230 &u2->data.sks.ns,
1227 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount) 1231 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey))) &&
1232 (0 == strcmp(u1->data.sks.identifier, u2->data.sks.identifier)))
1233
1234 return GNUNET_YES;
1228 return GNUNET_NO; 1235 return GNUNET_NO;
1229 for (i = 0; i < u1->data.ksk.keywordCount; i++) 1236
1230 { 1237 case GNUNET_FS_URI_KSK:
1231 ret = GNUNET_NO; 1238 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1232 for (j = 0; j < u2->data.ksk.keywordCount; j++) 1239 return GNUNET_NO;
1233 { 1240 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1234 if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1235 { 1241 {
1236 ret = GNUNET_YES; 1242 ret = GNUNET_NO;
1237 break; 1243 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1244 {
1245 if (0 == strcmp(u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1246 {
1247 ret = GNUNET_YES;
1248 break;
1249 }
1250 }
1251 if (ret == GNUNET_NO)
1252 return GNUNET_NO;
1238 } 1253 }
1239 } 1254 return GNUNET_YES;
1240 if (ret == GNUNET_NO) 1255
1256 case GNUNET_FS_URI_LOC:
1257 if (memcmp(&u1->data.loc,
1258 &u2->data.loc,
1259 sizeof(struct FileIdentifier) +
1260 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
1261 sizeof(struct GNUNET_TIME_Absolute) +
1262 sizeof(unsigned short) + sizeof(unsigned short)) != 0)
1241 return GNUNET_NO; 1263 return GNUNET_NO;
1242 } 1264 return GNUNET_YES;
1243 return GNUNET_YES; 1265
1244 case GNUNET_FS_URI_LOC: 1266 default:
1245 if (memcmp (&u1->data.loc,
1246 &u2->data.loc,
1247 sizeof (struct FileIdentifier) +
1248 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1249 sizeof (struct GNUNET_TIME_Absolute) +
1250 sizeof (unsigned short) + sizeof (unsigned short)) != 0)
1251 return GNUNET_NO; 1267 return GNUNET_NO;
1252 return GNUNET_YES; 1268 }
1253 default:
1254 return GNUNET_NO;
1255 }
1256} 1269}
1257 1270
1258 1271
@@ -1263,7 +1276,7 @@ GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1263 * @return #GNUNET_YES if this is an SKS uri 1276 * @return #GNUNET_YES if this is an SKS uri
1264 */ 1277 */
1265int 1278int
1266GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri) 1279GNUNET_FS_uri_test_sks(const struct GNUNET_FS_Uri *uri)
1267{ 1280{
1268 return uri->type == GNUNET_FS_URI_SKS; 1281 return uri->type == GNUNET_FS_URI_SKS;
1269} 1282}
@@ -1278,14 +1291,14 @@ GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1278 * @return #GNUNET_OK on success 1291 * @return #GNUNET_OK on success
1279 */ 1292 */
1280int 1293int
1281GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri, 1294GNUNET_FS_uri_sks_get_namespace(const struct GNUNET_FS_Uri *uri,
1282 struct GNUNET_CRYPTO_EcdsaPublicKey *pseudonym) 1295 struct GNUNET_CRYPTO_EcdsaPublicKey *pseudonym)
1283{ 1296{
1284 if (! GNUNET_FS_uri_test_sks (uri)) 1297 if (!GNUNET_FS_uri_test_sks(uri))
1285 { 1298 {
1286 GNUNET_break (0); 1299 GNUNET_break(0);
1287 return GNUNET_SYSERR; 1300 return GNUNET_SYSERR;
1288 } 1301 }
1289 *pseudonym = uri->data.sks.ns; 1302 *pseudonym = uri->data.sks.ns;
1290 return GNUNET_OK; 1303 return GNUNET_OK;
1291} 1304}
@@ -1298,14 +1311,14 @@ GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1298 * @return NULL on error (not a valid SKS URI) 1311 * @return NULL on error (not a valid SKS URI)
1299 */ 1312 */
1300char * 1313char *
1301GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri) 1314GNUNET_FS_uri_sks_get_content_id(const struct GNUNET_FS_Uri *uri)
1302{ 1315{
1303 if (! GNUNET_FS_uri_test_sks (uri)) 1316 if (!GNUNET_FS_uri_test_sks(uri))
1304 { 1317 {
1305 GNUNET_break (0); 1318 GNUNET_break(0);
1306 return NULL; 1319 return NULL;
1307 } 1320 }
1308 return GNUNET_strdup (uri->data.sks.identifier); 1321 return GNUNET_strdup(uri->data.sks.identifier);
1309} 1322}
1310 1323
1311 1324
@@ -1316,16 +1329,16 @@ GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1316 * @return #GNUNET_YES if this is a KSK uri 1329 * @return #GNUNET_YES if this is a KSK uri
1317 */ 1330 */
1318int 1331int
1319GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri) 1332GNUNET_FS_uri_test_ksk(const struct GNUNET_FS_Uri *uri)
1320{ 1333{
1321#if EXTRA_CHECKS 1334#if EXTRA_CHECKS
1322 unsigned int i; 1335 unsigned int i;
1323 1336
1324 if (uri->type == GNUNET_FS_URI_KSK) 1337 if (uri->type == GNUNET_FS_URI_KSK)
1325 { 1338 {
1326 for (i = 0; i < uri->data.ksk.keywordCount; i++) 1339 for (i = 0; i < uri->data.ksk.keywordCount; i++)
1327 GNUNET_assert (uri->data.ksk.keywords[i] != NULL); 1340 GNUNET_assert(uri->data.ksk.keywords[i] != NULL);
1328 } 1341 }
1329#endif 1342#endif
1330 return uri->type == GNUNET_FS_URI_KSK; 1343 return uri->type == GNUNET_FS_URI_KSK;
1331} 1344}
@@ -1338,7 +1351,7 @@ GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1338 * @return #GNUNET_YES if this is a CHK uri 1351 * @return #GNUNET_YES if this is a CHK uri
1339 */ 1352 */
1340int 1353int
1341GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri) 1354GNUNET_FS_uri_test_chk(const struct GNUNET_FS_Uri *uri)
1342{ 1355{
1343 return uri->type == GNUNET_FS_URI_CHK; 1356 return uri->type == GNUNET_FS_URI_CHK;
1344} 1357}
@@ -1352,17 +1365,19 @@ GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1352 * @return size of the file as specified in the CHK URI 1365 * @return size of the file as specified in the CHK URI
1353 */ 1366 */
1354uint64_t 1367uint64_t
1355GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri) 1368GNUNET_FS_uri_chk_get_file_size(const struct GNUNET_FS_Uri *uri)
1356{ 1369{
1357 switch (uri->type) 1370 switch (uri->type)
1358 { 1371 {
1359 case GNUNET_FS_URI_CHK: 1372 case GNUNET_FS_URI_CHK:
1360 return GNUNET_ntohll (uri->data.chk.file_length); 1373 return GNUNET_ntohll(uri->data.chk.file_length);
1361 case GNUNET_FS_URI_LOC: 1374
1362 return GNUNET_ntohll (uri->data.loc.fi.file_length); 1375 case GNUNET_FS_URI_LOC:
1363 default: 1376 return GNUNET_ntohll(uri->data.loc.fi.file_length);
1364 GNUNET_assert (0); 1377
1365 } 1378 default:
1379 GNUNET_assert(0);
1380 }
1366 return 0; /* unreachable */ 1381 return 0; /* unreachable */
1367} 1382}
1368 1383
@@ -1374,7 +1389,7 @@ GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1374 * @return #GNUNET_YES if this is a LOC uri 1389 * @return #GNUNET_YES if this is a LOC uri
1375 */ 1390 */
1376int 1391int
1377GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri) 1392GNUNET_FS_uri_test_loc(const struct GNUNET_FS_Uri *uri)
1378{ 1393{
1379 return uri->type == GNUNET_FS_URI_LOC; 1394 return uri->type == GNUNET_FS_URI_LOC;
1380} 1395}
@@ -1390,13 +1405,13 @@ GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1390 * @param index offset where to add the keyword 1405 * @param index offset where to add the keyword
1391 */ 1406 */
1392static void 1407static void
1393insert_non_mandatory_keyword (const char *s, char **array, int index) 1408insert_non_mandatory_keyword(const char *s, char **array, int index)
1394{ 1409{
1395 char *nkword; 1410 char *nkword;
1396 1411
1397 GNUNET_asprintf (&nkword, 1412 GNUNET_asprintf(&nkword,
1398 " %s", /* space to mark as 'non mandatory' */ 1413 " %s", /* space to mark as 'non mandatory' */
1399 s); 1414 s);
1400 array[index] = nkword; 1415 array[index] = nkword;
1401} 1416}
1402 1417
@@ -1411,12 +1426,12 @@ insert_non_mandatory_keyword (const char *s, char **array, int index)
1411 * @return #GNUNET_YES if the keyword exists, #GNUNET_NO if not 1426 * @return #GNUNET_YES if the keyword exists, #GNUNET_NO if not
1412 */ 1427 */
1413static int 1428static int
1414find_duplicate (const char *s, const char **array, int array_length) 1429find_duplicate(const char *s, const char **array, int array_length)
1415{ 1430{
1416 int j; 1431 int j;
1417 1432
1418 for (j = array_length - 1; j >= 0; j--) 1433 for (j = array_length - 1; j >= 0; j--)
1419 if (0 == strcmp (&array[j][1], s)) 1434 if (0 == strcmp(&array[j][1], s))
1420 return GNUNET_YES; 1435 return GNUNET_YES;
1421 return GNUNET_NO; 1436 return GNUNET_NO;
1422} 1437}
@@ -1426,53 +1441,54 @@ find_duplicate (const char *s, const char **array, int array_length)
1426 * FIXME: comment 1441 * FIXME: comment
1427 */ 1442 */
1428static char * 1443static char *
1429normalize_metadata (enum EXTRACTOR_MetaFormat format, 1444normalize_metadata(enum EXTRACTOR_MetaFormat format,
1430 const char *data, 1445 const char *data,
1431 size_t data_len) 1446 size_t data_len)
1432{ 1447{
1433 uint8_t *free_str = NULL; 1448 uint8_t *free_str = NULL;
1434 uint8_t *str_to_normalize = (uint8_t *) data; 1449 uint8_t *str_to_normalize = (uint8_t *)data;
1435 uint8_t *normalized; 1450 uint8_t *normalized;
1436 size_t r_len; 1451 size_t r_len;
1452
1437 if (str_to_normalize == NULL) 1453 if (str_to_normalize == NULL)
1438 return NULL; 1454 return NULL;
1439 /* Don't trust libextractor */ 1455 /* Don't trust libextractor */
1440 if (format == EXTRACTOR_METAFORMAT_UTF8) 1456 if (format == EXTRACTOR_METAFORMAT_UTF8)
1441 { 1457 {
1442 free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len); 1458 free_str = (uint8_t *)u8_check((const uint8_t *)data, data_len);
1443 if (free_str == NULL) 1459 if (free_str == NULL)
1444 free_str = NULL; 1460 free_str = NULL;
1445 else 1461 else
1446 format = EXTRACTOR_METAFORMAT_C_STRING; 1462 format = EXTRACTOR_METAFORMAT_C_STRING;
1447 } 1463 }
1448 if (format == EXTRACTOR_METAFORMAT_C_STRING) 1464 if (format == EXTRACTOR_METAFORMAT_C_STRING)
1449 { 1465 {
1450 free_str = u8_strconv_from_encoding (data, 1466 free_str = u8_strconv_from_encoding(data,
1451 locale_charset (), 1467 locale_charset(),
1452 iconveh_escape_sequence); 1468 iconveh_escape_sequence);
1453 if (free_str == NULL) 1469 if (free_str == NULL)
1454 return NULL; 1470 return NULL;
1455 } 1471 }
1456 1472
1457 normalized = u8_tolower (str_to_normalize, 1473 normalized = u8_tolower(str_to_normalize,
1458 strlen ((char *) str_to_normalize), 1474 strlen((char *)str_to_normalize),
1459 NULL, 1475 NULL,
1460 UNINORM_NFD, 1476 UNINORM_NFD,
1461 NULL, 1477 NULL,
1462 &r_len); 1478 &r_len);
1463 /* free_str is allocated by libunistring internally, use free() */ 1479 /* free_str is allocated by libunistring internally, use free() */
1464 if (free_str != NULL) 1480 if (free_str != NULL)
1465 free (free_str); 1481 free(free_str);
1466 if (normalized != NULL) 1482 if (normalized != NULL)
1467 { 1483 {
1468 /* u8_tolower allocates a non-NULL-terminated string! */ 1484 /* u8_tolower allocates a non-NULL-terminated string! */
1469 free_str = GNUNET_malloc (r_len + 1); 1485 free_str = GNUNET_malloc(r_len + 1);
1470 GNUNET_memcpy (free_str, normalized, r_len); 1486 GNUNET_memcpy(free_str, normalized, r_len);
1471 free_str[r_len] = '\0'; 1487 free_str[r_len] = '\0';
1472 free (normalized); 1488 free(normalized);
1473 normalized = free_str; 1489 normalized = free_str;
1474 } 1490 }
1475 return (char *) normalized; 1491 return (char *)normalized;
1476} 1492}
1477 1493
1478 1494
@@ -1481,15 +1497,16 @@ normalize_metadata (enum EXTRACTOR_MetaFormat format,
1481 * returns that count. 1497 * returns that count.
1482 */ 1498 */
1483static size_t 1499static size_t
1484u8_strcount (const uint8_t *s) 1500u8_strcount(const uint8_t *s)
1485{ 1501{
1486 size_t count; 1502 size_t count;
1487 ucs4_t c; 1503 ucs4_t c;
1488 GNUNET_assert (s != NULL); 1504
1505 GNUNET_assert(s != NULL);
1489 if (s[0] == 0) 1506 if (s[0] == 0)
1490 return 0; 1507 return 0;
1491 for (count = 0; s != NULL; count++) 1508 for (count = 0; s != NULL; count++)
1492 s = u8_next (&c, s); 1509 s = u8_next(&c, s);
1493 return count - 1; 1510 return count - 1;
1494} 1511}
1495 1512
@@ -1511,7 +1528,7 @@ u8_strcount (const uint8_t *s)
1511 * were duplicates (when extracting). 1528 * were duplicates (when extracting).
1512 */ 1529 */
1513static int 1530static int
1514get_keywords_from_parens (const char *s, char **array, int index) 1531get_keywords_from_parens(const char *s, char **array, int index)
1515{ 1532{
1516 int count = 0; 1533 int count = 0;
1517 char *open_paren; 1534 char *open_paren;
@@ -1521,77 +1538,80 @@ get_keywords_from_parens (const char *s, char **array, int index)
1521 1538
1522 if (NULL == s) 1539 if (NULL == s)
1523 return 0; 1540 return 0;
1524 ss = GNUNET_strdup (s); 1541 ss = GNUNET_strdup(s);
1525 open_paren = ss - 1; 1542 open_paren = ss - 1;
1526 while (NULL != (open_paren = strpbrk (open_paren + 1, "[{("))) 1543 while (NULL != (open_paren = strpbrk(open_paren + 1, "[{(")))
1527 {
1528 int match = 0;
1529
1530 close_paren = strpbrk (open_paren + 1, "]})");
1531 if (NULL == close_paren)
1532 continue;
1533 switch (open_paren[0])
1534 {
1535 case '[':
1536 if (']' == close_paren[0])
1537 match = 1;
1538 break;
1539 case '{':
1540 if ('}' == close_paren[0])
1541 match = 1;
1542 break;
1543 case '(':
1544 if (')' == close_paren[0])
1545 match = 1;
1546 break;
1547 default:
1548 break;
1549 }
1550 if (match && (close_paren - open_paren > 1))
1551 { 1544 {
1552 tmp = close_paren[0]; 1545 int match = 0;
1553 close_paren[0] = '\0'; 1546
1554 /* Keywords must be at least 3 characters long */ 1547 close_paren = strpbrk(open_paren + 1, "]})");
1555 if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2) 1548 if (NULL == close_paren)
1556 {
1557 close_paren[0] = tmp;
1558 continue; 1549 continue;
1559 } 1550 switch (open_paren[0])
1560 if (NULL != array)
1561 {
1562 char *normalized;
1563 if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
1564 (const char **) array,
1565 index + count))
1566 { 1551 {
1567 insert_non_mandatory_keyword ((const char *) &open_paren[1], 1552 case '[':
1568 array, 1553 if (']' == close_paren[0])
1569 index + count); 1554 match = 1;
1570 count++; 1555 break;
1556
1557 case '{':
1558 if ('}' == close_paren[0])
1559 match = 1;
1560 break;
1561
1562 case '(':
1563 if (')' == close_paren[0])
1564 match = 1;
1565 break;
1566
1567 default:
1568 break;
1571 } 1569 }
1572 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8, 1570 if (match && (close_paren - open_paren > 1))
1573 &open_paren[1],
1574 close_paren - &open_paren[1]);
1575 if (normalized != NULL)
1576 { 1571 {
1577 if (GNUNET_NO == find_duplicate ((const char *) normalized, 1572 tmp = close_paren[0];
1578 (const char **) array, 1573 close_paren[0] = '\0';
1579 index + count)) 1574 /* Keywords must be at least 3 characters long */
1580 { 1575 if (u8_strcount((const uint8_t *)&open_paren[1]) <= 2)
1581 insert_non_mandatory_keyword ((const char *) normalized, 1576 {
1582 array, 1577 close_paren[0] = tmp;
1583 index + count); 1578 continue;
1579 }
1580 if (NULL != array)
1581 {
1582 char *normalized;
1583 if (GNUNET_NO == find_duplicate((const char *)&open_paren[1],
1584 (const char **)array,
1585 index + count))
1586 {
1587 insert_non_mandatory_keyword((const char *)&open_paren[1],
1588 array,
1589 index + count);
1590 count++;
1591 }
1592 normalized = normalize_metadata(EXTRACTOR_METAFORMAT_UTF8,
1593 &open_paren[1],
1594 close_paren - &open_paren[1]);
1595 if (normalized != NULL)
1596 {
1597 if (GNUNET_NO == find_duplicate((const char *)normalized,
1598 (const char **)array,
1599 index + count))
1600 {
1601 insert_non_mandatory_keyword((const char *)normalized,
1602 array,
1603 index + count);
1604 count++;
1605 }
1606 GNUNET_free(normalized);
1607 }
1608 }
1609 else
1584 count++; 1610 count++;
1585 } 1611 close_paren[0] = tmp;
1586 GNUNET_free (normalized);
1587 } 1612 }
1588 }
1589 else
1590 count++;
1591 close_paren[0] = tmp;
1592 } 1613 }
1593 } 1614 GNUNET_free(ss);
1594 GNUNET_free (ss);
1595 return count; 1615 return count;
1596} 1616}
1597 1617
@@ -1616,46 +1636,46 @@ get_keywords_from_parens (const char *s, char **array, int index)
1616 * duplicates (when extracting). 1636 * duplicates (when extracting).
1617 */ 1637 */
1618static int 1638static int
1619get_keywords_from_tokens (const char *s, char **array, int index) 1639get_keywords_from_tokens(const char *s, char **array, int index)
1620{ 1640{
1621 char *p; 1641 char *p;
1622 char *ss; 1642 char *ss;
1623 int seps = 0; 1643 int seps = 0;
1624 1644
1625 ss = GNUNET_strdup (s); 1645 ss = GNUNET_strdup(s);
1626 for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS)) 1646 for (p = strtok(ss, TOKENS); p != NULL; p = strtok(NULL, TOKENS))
1627 {
1628 /* Keywords must be at least 3 characters long */
1629 if (u8_strcount ((const uint8_t *) p) <= 2)
1630 continue;
1631 if (NULL != array)
1632 { 1647 {
1633 char *normalized; 1648 /* Keywords must be at least 3 characters long */
1634 if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps)) 1649 if (u8_strcount((const uint8_t *)p) <= 2)
1635 { 1650 continue;
1636 insert_non_mandatory_keyword (p, array, index + seps); 1651 if (NULL != array)
1637 seps++;
1638 }
1639 normalized =
1640 normalize_metadata (EXTRACTOR_METAFORMAT_UTF8, p, strlen (p));
1641 if (normalized != NULL)
1642 {
1643 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1644 (const char **) array,
1645 index + seps))
1646 { 1652 {
1647 insert_non_mandatory_keyword ((const char *) normalized, 1653 char *normalized;
1648 array, 1654 if (GNUNET_NO == find_duplicate(p, (const char **)array, index + seps))
1649 index + seps); 1655 {
1650 seps++; 1656 insert_non_mandatory_keyword(p, array, index + seps);
1657 seps++;
1658 }
1659 normalized =
1660 normalize_metadata(EXTRACTOR_METAFORMAT_UTF8, p, strlen(p));
1661 if (normalized != NULL)
1662 {
1663 if (GNUNET_NO == find_duplicate((const char *)normalized,
1664 (const char **)array,
1665 index + seps))
1666 {
1667 insert_non_mandatory_keyword((const char *)normalized,
1668 array,
1669 index + seps);
1670 seps++;
1671 }
1672 GNUNET_free(normalized);
1673 }
1651 } 1674 }
1652 GNUNET_free (normalized); 1675 else
1653 } 1676 seps++;
1654 } 1677 }
1655 else 1678 GNUNET_free(ss);
1656 seps++;
1657 }
1658 GNUNET_free (ss);
1659 return seps; 1679 return seps;
1660} 1680}
1661#undef TOKENS 1681#undef TOKENS
@@ -1679,13 +1699,13 @@ get_keywords_from_tokens (const char *s, char **array, int index)
1679 * @return 0 (always) 1699 * @return 0 (always)
1680 */ 1700 */
1681static int 1701static int
1682gather_uri_data (void *cls, 1702gather_uri_data(void *cls,
1683 const char *plugin_name, 1703 const char *plugin_name,
1684 enum EXTRACTOR_MetaType type, 1704 enum EXTRACTOR_MetaType type,
1685 enum EXTRACTOR_MetaFormat format, 1705 enum EXTRACTOR_MetaFormat format,
1686 const char *data_mime_type, 1706 const char *data_mime_type,
1687 const char *data, 1707 const char *data,
1688 size_t data_len) 1708 size_t data_len)
1689{ 1709{
1690 struct GNUNET_FS_Uri *uri = cls; 1710 struct GNUNET_FS_Uri *uri = cls;
1691 char *normalized_data; 1711 char *normalized_data;
@@ -1699,49 +1719,49 @@ gather_uri_data (void *cls,
1699 * and will return the length of its valid part, skipping the keyword. 1719 * and will return the length of its valid part, skipping the keyword.
1700 * If it does - fix the extractor, not this check! 1720 * If it does - fix the extractor, not this check!
1701 */ 1721 */
1702 if (u8_strcount ((const uint8_t *) data) <= 2) 1722 if (u8_strcount((const uint8_t *)data) <= 2)
1703 return 0; 1723 return 0;
1704 if ((EXTRACTOR_METATYPE_MIMETYPE == type) && 1724 if ((EXTRACTOR_METATYPE_MIMETYPE == type) &&
1705 (NULL != (sep = memchr (data, '/', data_len))) && (sep != data)) 1725 (NULL != (sep = memchr(data, '/', data_len))) && (sep != data))
1706 { 1726 {
1707 char *xtra; 1727 char *xtra;
1708 1728
1709 GNUNET_asprintf (&xtra, "mimetype:%.*s", (int) (sep - data), data); 1729 GNUNET_asprintf(&xtra, "mimetype:%.*s", (int)(sep - data), data);
1710 if (! find_duplicate (xtra, 1730 if (!find_duplicate(xtra,
1711 (const char **) uri->data.ksk.keywords, 1731 (const char **)uri->data.ksk.keywords,
1712 uri->data.ksk.keywordCount)) 1732 uri->data.ksk.keywordCount))
1733 {
1734 insert_non_mandatory_keyword(xtra,
1735 uri->data.ksk.keywords,
1736 uri->data.ksk.keywordCount);
1737 uri->data.ksk.keywordCount++;
1738 }
1739 GNUNET_free(xtra);
1740 }
1741
1742 normalized_data = normalize_metadata(format, data, data_len);
1743 if (!find_duplicate(data,
1744 (const char **)uri->data.ksk.keywords,
1745 uri->data.ksk.keywordCount))
1713 { 1746 {
1714 insert_non_mandatory_keyword (xtra, 1747 insert_non_mandatory_keyword(data,
1715 uri->data.ksk.keywords, 1748 uri->data.ksk.keywords,
1716 uri->data.ksk.keywordCount); 1749 uri->data.ksk.keywordCount);
1717 uri->data.ksk.keywordCount++; 1750 uri->data.ksk.keywordCount++;
1718 } 1751 }
1719 GNUNET_free (xtra);
1720 }
1721
1722 normalized_data = normalize_metadata (format, data, data_len);
1723 if (! find_duplicate (data,
1724 (const char **) uri->data.ksk.keywords,
1725 uri->data.ksk.keywordCount))
1726 {
1727 insert_non_mandatory_keyword (data,
1728 uri->data.ksk.keywords,
1729 uri->data.ksk.keywordCount);
1730 uri->data.ksk.keywordCount++;
1731 }
1732 if (NULL != normalized_data) 1752 if (NULL != normalized_data)
1733 {
1734 if (! find_duplicate (normalized_data,
1735 (const char **) uri->data.ksk.keywords,
1736 uri->data.ksk.keywordCount))
1737 { 1753 {
1738 insert_non_mandatory_keyword (normalized_data, 1754 if (!find_duplicate(normalized_data,
1739 uri->data.ksk.keywords, 1755 (const char **)uri->data.ksk.keywords,
1740 uri->data.ksk.keywordCount); 1756 uri->data.ksk.keywordCount))
1741 uri->data.ksk.keywordCount++; 1757 {
1758 insert_non_mandatory_keyword(normalized_data,
1759 uri->data.ksk.keywords,
1760 uri->data.ksk.keywordCount);
1761 uri->data.ksk.keywordCount++;
1762 }
1763 GNUNET_free(normalized_data);
1742 } 1764 }
1743 GNUNET_free (normalized_data);
1744 }
1745 return 0; 1765 return 0;
1746} 1766}
1747 1767
@@ -1755,7 +1775,7 @@ gather_uri_data (void *cls,
1755 * @return NULL on error, otherwise a KSK URI 1775 * @return NULL on error, otherwise a KSK URI
1756 */ 1776 */
1757struct GNUNET_FS_Uri * 1777struct GNUNET_FS_Uri *
1758GNUNET_FS_uri_ksk_create_from_meta_data ( 1778GNUNET_FS_uri_ksk_create_from_meta_data(
1759 const struct GNUNET_CONTAINER_MetaData *md) 1779 const struct GNUNET_CONTAINER_MetaData *md)
1760{ 1780{
1761 struct GNUNET_FS_Uri *ret; 1781 struct GNUNET_FS_Uri *ret;
@@ -1768,41 +1788,41 @@ GNUNET_FS_uri_ksk_create_from_meta_data (
1768 1788
1769 if (NULL == md) 1789 if (NULL == md)
1770 return NULL; 1790 return NULL;
1771 ret = GNUNET_new (struct GNUNET_FS_Uri); 1791 ret = GNUNET_new(struct GNUNET_FS_Uri);
1772 ret->type = GNUNET_FS_URI_KSK; 1792 ret->type = GNUNET_FS_URI_KSK;
1773 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL); 1793 ent = GNUNET_CONTAINER_meta_data_iterate(md, NULL, NULL);
1774 if (ent > 0) 1794 if (ent > 0)
1775 {
1776 full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (
1777 md,
1778 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
1779 -1);
1780 if (NULL != full_name)
1781 { 1795 {
1782 filename = full_name; 1796 full_name = GNUNET_CONTAINER_meta_data_get_first_by_types(
1783 while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR))) 1797 md,
1784 filename = ss + 1; 1798 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME,
1785 tok_keywords = get_keywords_from_tokens (filename, NULL, 0); 1799 -1);
1786 paren_keywords = get_keywords_from_parens (filename, NULL, 0); 1800 if (NULL != full_name)
1801 {
1802 filename = full_name;
1803 while (NULL != (ss = strstr(filename, DIR_SEPARATOR_STR)))
1804 filename = ss + 1;
1805 tok_keywords = get_keywords_from_tokens(filename, NULL, 0);
1806 paren_keywords = get_keywords_from_parens(filename, NULL, 0);
1807 }
1808 /* x3 because there might be a normalized variant of every keyword,
1809 plus theoretically one more for mime... */
1810 ret->data.ksk.keywords =
1811 GNUNET_new_array((ent + tok_keywords + paren_keywords) * 3, char *);
1812 GNUNET_CONTAINER_meta_data_iterate(md, &gather_uri_data, ret);
1787 } 1813 }
1788 /* x3 because there might be a normalized variant of every keyword,
1789 plus theoretically one more for mime... */
1790 ret->data.ksk.keywords =
1791 GNUNET_new_array ((ent + tok_keywords + paren_keywords) * 3, char *);
1792 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1793 }
1794 if (tok_keywords > 0) 1814 if (tok_keywords > 0)
1795 ret->data.ksk.keywordCount += 1815 ret->data.ksk.keywordCount +=
1796 get_keywords_from_tokens (filename, 1816 get_keywords_from_tokens(filename,
1797 ret->data.ksk.keywords, 1817 ret->data.ksk.keywords,
1798 ret->data.ksk.keywordCount); 1818 ret->data.ksk.keywordCount);
1799 if (paren_keywords > 0) 1819 if (paren_keywords > 0)
1800 ret->data.ksk.keywordCount += 1820 ret->data.ksk.keywordCount +=
1801 get_keywords_from_parens (filename, 1821 get_keywords_from_parens(filename,
1802 ret->data.ksk.keywords, 1822 ret->data.ksk.keywords,
1803 ret->data.ksk.keywordCount); 1823 ret->data.ksk.keywordCount);
1804 if (ent > 0) 1824 if (ent > 0)
1805 GNUNET_free_non_null (full_name); 1825 GNUNET_free_non_null(full_name);
1806 return ret; 1826 return ret;
1807} 1827}
1808 1828
@@ -1812,10 +1832,10 @@ GNUNET_FS_uri_ksk_create_from_meta_data (
1812 * need to be encoded using %-encoding? 1832 * need to be encoded using %-encoding?
1813 */ 1833 */
1814static int 1834static int
1815needs_percent (char c) 1835needs_percent(char c)
1816{ 1836{
1817 return (! ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') || 1837 return(!((isalnum((unsigned char)c)) || (c == '-') || (c == '_') ||
1818 (c == '.') || (c == '~'))); 1838 (c == '.') || (c == '~')));
1819} 1839}
1820 1840
1821 1841
@@ -1826,7 +1846,7 @@ needs_percent (char c)
1826 * @return NULL on error (i.e. keywordCount == 0) 1846 * @return NULL on error (i.e. keywordCount == 0)
1827 */ 1847 */
1828static char * 1848static char *
1829uri_ksk_to_string (const struct GNUNET_FS_Uri *uri) 1849uri_ksk_to_string(const struct GNUNET_FS_Uri *uri)
1830{ 1850{
1831 char **keywords; 1851 char **keywords;
1832 unsigned int keywordCount; 1852 unsigned int keywordCount;
@@ -1842,49 +1862,49 @@ uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1842 return NULL; 1862 return NULL;
1843 keywords = uri->data.ksk.keywords; 1863 keywords = uri->data.ksk.keywords;
1844 keywordCount = uri->data.ksk.keywordCount; 1864 keywordCount = uri->data.ksk.keywordCount;
1845 n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) + 1865 n = keywordCount + strlen(GNUNET_FS_URI_PREFIX) +
1846 strlen (GNUNET_FS_URI_KSK_INFIX) + 1; 1866 strlen(GNUNET_FS_URI_KSK_INFIX) + 1;
1847 for (i = 0; i < keywordCount; i++) 1867 for (i = 0; i < keywordCount; i++)
1848 {
1849 keyword = keywords[i];
1850 slen = strlen (keyword);
1851 n += slen;
1852 for (j = 0; j < slen; j++)
1853 { 1868 {
1854 if ((j == 0) && (keyword[j] == ' ')) 1869 keyword = keywords[i];
1855 { 1870 slen = strlen(keyword);
1856 n--; 1871 n += slen;
1857 continue; /* skip leading space */ 1872 for (j = 0; j < slen; j++)
1858 } 1873 {
1859 if (needs_percent (keyword[j])) 1874 if ((j == 0) && (keyword[j] == ' '))
1860 n += 2; /* will use %-encoding */ 1875 {
1876 n--;
1877 continue; /* skip leading space */
1878 }
1879 if (needs_percent(keyword[j]))
1880 n += 2; /* will use %-encoding */
1881 }
1861 } 1882 }
1862 } 1883 ret = GNUNET_malloc(n);
1863 ret = GNUNET_malloc (n); 1884 strcpy(ret, GNUNET_FS_URI_PREFIX);
1864 strcpy (ret, GNUNET_FS_URI_PREFIX); 1885 strcat(ret, GNUNET_FS_URI_KSK_INFIX);
1865 strcat (ret, GNUNET_FS_URI_KSK_INFIX); 1886 wpos = strlen(ret);
1866 wpos = strlen (ret);
1867 for (i = 0; i < keywordCount; i++) 1887 for (i = 0; i < keywordCount; i++)
1868 {
1869 keyword = keywords[i];
1870 slen = strlen (keyword);
1871 for (j = 0; j < slen; j++)
1872 { 1888 {
1873 if ((j == 0) && (keyword[j] == ' ')) 1889 keyword = keywords[i];
1874 continue; /* skip leading space */ 1890 slen = strlen(keyword);
1875 if (needs_percent (keyword[j])) 1891 for (j = 0; j < slen; j++)
1876 { 1892 {
1877 sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]); 1893 if ((j == 0) && (keyword[j] == ' '))
1878 wpos += 3; 1894 continue; /* skip leading space */
1879 } 1895 if (needs_percent(keyword[j]))
1880 else 1896 {
1881 { 1897 sprintf(&ret[wpos], "%%%02X", (unsigned char)keyword[j]);
1882 ret[wpos++] = keyword[j]; 1898 wpos += 3;
1883 } 1899 }
1900 else
1901 {
1902 ret[wpos++] = keyword[j];
1903 }
1904 }
1905 if (i != keywordCount - 1)
1906 ret[wpos++] = '+';
1884 } 1907 }
1885 if (i != keywordCount - 1)
1886 ret[wpos++] = '+';
1887 }
1888 return ret; 1908 return ret;
1889} 1909}
1890 1910
@@ -1896,7 +1916,7 @@ uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1896 * @return NULL on error 1916 * @return NULL on error
1897 */ 1917 */
1898static char * 1918static char *
1899uri_sks_to_string (const struct GNUNET_FS_Uri *uri) 1919uri_sks_to_string(const struct GNUNET_FS_Uri *uri)
1900{ 1920{
1901 char *ret; 1921 char *ret;
1902 char buf[1024]; 1922 char buf[1024];
@@ -1904,18 +1924,18 @@ uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1904 if (GNUNET_FS_URI_SKS != uri->type) 1924 if (GNUNET_FS_URI_SKS != uri->type)
1905 return NULL; 1925 return NULL;
1906 ret = 1926 ret =
1907 GNUNET_STRINGS_data_to_string (&uri->data.sks.ns, 1927 GNUNET_STRINGS_data_to_string(&uri->data.sks.ns,
1908 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey), 1928 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
1909 buf, 1929 buf,
1910 sizeof (buf)); 1930 sizeof(buf));
1911 GNUNET_assert (NULL != ret); 1931 GNUNET_assert(NULL != ret);
1912 ret[0] = '\0'; 1932 ret[0] = '\0';
1913 GNUNET_asprintf (&ret, 1933 GNUNET_asprintf(&ret,
1914 "%s%s%s/%s", 1934 "%s%s%s/%s",
1915 GNUNET_FS_URI_PREFIX, 1935 GNUNET_FS_URI_PREFIX,
1916 GNUNET_FS_URI_SKS_INFIX, 1936 GNUNET_FS_URI_SKS_INFIX,
1917 buf, 1937 buf,
1918 uri->data.sks.identifier); 1938 uri->data.sks.identifier);
1919 return ret; 1939 return ret;
1920} 1940}
1921 1941
@@ -1927,7 +1947,7 @@ uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1927 * @return NULL on error 1947 * @return NULL on error
1928 */ 1948 */
1929static char * 1949static char *
1930uri_chk_to_string (const struct GNUNET_FS_Uri *uri) 1950uri_chk_to_string(const struct GNUNET_FS_Uri *uri)
1931{ 1951{
1932 const struct FileIdentifier *fi; 1952 const struct FileIdentifier *fi;
1933 char *ret; 1953 char *ret;
@@ -1937,16 +1957,16 @@ uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1937 if (uri->type != GNUNET_FS_URI_CHK) 1957 if (uri->type != GNUNET_FS_URI_CHK)
1938 return NULL; 1958 return NULL;
1939 fi = &uri->data.chk; 1959 fi = &uri->data.chk;
1940 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash); 1960 GNUNET_CRYPTO_hash_to_enc(&fi->chk.key, &keyhash);
1941 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash); 1961 GNUNET_CRYPTO_hash_to_enc(&fi->chk.query, &queryhash);
1942 1962
1943 GNUNET_asprintf (&ret, 1963 GNUNET_asprintf(&ret,
1944 "%s%s%s.%s.%llu", 1964 "%s%s%s.%s.%llu",
1945 GNUNET_FS_URI_PREFIX, 1965 GNUNET_FS_URI_PREFIX,
1946 GNUNET_FS_URI_CHK_INFIX, 1966 GNUNET_FS_URI_CHK_INFIX,
1947 (const char *) &keyhash, 1967 (const char *)&keyhash,
1948 (const char *) &queryhash, 1968 (const char *)&queryhash,
1949 GNUNET_ntohll (fi->file_length)); 1969 GNUNET_ntohll(fi->file_length));
1950 return ret; 1970 return ret;
1951} 1971}
1952 1972
@@ -1958,7 +1978,7 @@ uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1958 * @return NULL on error 1978 * @return NULL on error
1959 */ 1979 */
1960static char * 1980static char *
1961uri_loc_to_string (const struct GNUNET_FS_Uri *uri) 1981uri_loc_to_string(const struct GNUNET_FS_Uri *uri)
1962{ 1982{
1963 char *ret; 1983 char *ret;
1964 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash; 1984 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
@@ -1966,30 +1986,30 @@ uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1966 char *peer_id; 1986 char *peer_id;
1967 char peer_sig[SIGNATURE_ASCII_LENGTH + 1]; 1987 char peer_sig[SIGNATURE_ASCII_LENGTH + 1];
1968 1988
1969 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash); 1989 GNUNET_CRYPTO_hash_to_enc(&uri->data.loc.fi.chk.key, &keyhash);
1970 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash); 1990 GNUNET_CRYPTO_hash_to_enc(&uri->data.loc.fi.chk.query, &queryhash);
1971 peer_id = 1991 peer_id =
1972 GNUNET_CRYPTO_eddsa_public_key_to_string (&uri->data.loc.peer.public_key); 1992 GNUNET_CRYPTO_eddsa_public_key_to_string(&uri->data.loc.peer.public_key);
1973 GNUNET_assert ( 1993 GNUNET_assert(
1974 NULL != 1994 NULL !=
1975 GNUNET_STRINGS_data_to_string (&uri->data.loc.contentSignature, 1995 GNUNET_STRINGS_data_to_string(&uri->data.loc.contentSignature,
1976 sizeof (struct GNUNET_CRYPTO_EddsaSignature), 1996 sizeof(struct GNUNET_CRYPTO_EddsaSignature),
1977 peer_sig, 1997 peer_sig,
1978 sizeof (peer_sig))); 1998 sizeof(peer_sig)));
1979 GNUNET_asprintf (&ret, 1999 GNUNET_asprintf(&ret,
1980 "%s%s%s.%s.%llu.%s.%s.%llu", 2000 "%s%s%s.%s.%llu.%s.%s.%llu",
1981 GNUNET_FS_URI_PREFIX, 2001 GNUNET_FS_URI_PREFIX,
1982 GNUNET_FS_URI_LOC_INFIX, 2002 GNUNET_FS_URI_LOC_INFIX,
1983 (const char *) &keyhash, 2003 (const char *)&keyhash,
1984 (const char *) &queryhash, 2004 (const char *)&queryhash,
1985 (unsigned long long) GNUNET_ntohll ( 2005 (unsigned long long)GNUNET_ntohll(
1986 uri->data.loc.fi.file_length), 2006 uri->data.loc.fi.file_length),
1987 peer_id, 2007 peer_id,
1988 peer_sig, 2008 peer_sig,
1989 (unsigned long long) 2009 (unsigned long long)
1990 uri->data.loc.expirationTime.abs_value_us / 2010 uri->data.loc.expirationTime.abs_value_us /
1991 1000000LL); 2011 1000000LL);
1992 GNUNET_free (peer_id); 2012 GNUNET_free(peer_id);
1993 return ret; 2013 return ret;
1994} 2014}
1995 2015
@@ -2001,27 +2021,31 @@ uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
2001 * @return the UTF-8 string 2021 * @return the UTF-8 string
2002 */ 2022 */
2003char * 2023char *
2004GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri) 2024GNUNET_FS_uri_to_string(const struct GNUNET_FS_Uri *uri)
2005{ 2025{
2006 if (uri == NULL) 2026 if (uri == NULL)
2007 { 2027 {
2008 GNUNET_break (0); 2028 GNUNET_break(0);
2009 return NULL; 2029 return NULL;
2010 } 2030 }
2011 switch (uri->type) 2031 switch (uri->type)
2012 { 2032 {
2013 case GNUNET_FS_URI_KSK: 2033 case GNUNET_FS_URI_KSK:
2014 return uri_ksk_to_string (uri); 2034 return uri_ksk_to_string(uri);
2015 case GNUNET_FS_URI_SKS: 2035
2016 return uri_sks_to_string (uri); 2036 case GNUNET_FS_URI_SKS:
2017 case GNUNET_FS_URI_CHK: 2037 return uri_sks_to_string(uri);
2018 return uri_chk_to_string (uri); 2038
2019 case GNUNET_FS_URI_LOC: 2039 case GNUNET_FS_URI_CHK:
2020 return uri_loc_to_string (uri); 2040 return uri_chk_to_string(uri);
2021 default: 2041
2022 GNUNET_break (0); 2042 case GNUNET_FS_URI_LOC:
2023 return NULL; 2043 return uri_loc_to_string(uri);
2024 } 2044
2045 default:
2046 GNUNET_break(0);
2047 return NULL;
2048 }
2025} 2049}
2026 2050
2027/* end of fs_uri.c */ 2051/* end of fs_uri.c */