-use GPLv3+ consistently
[oweals/gnunet.git] / src / fs / fs_uri.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003--2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file fs/fs_uri.c
23  * @brief Parses and produces uri strings.
24  * @author Igor Wronsky, Christian Grothoff
25  *
26  * GNUnet URIs are of the general form "gnunet://MODULE/IDENTIFIER".
27  * The specific structure of "IDENTIFIER" depends on the module and
28  * maybe differenciated into additional subcategories if applicable.
29  * This module only deals with fs identifiers (MODULE = "fs").
30  * <p>
31  *
32  * This module only parses URIs for the AFS module.  The FS URIs fall
33  * into four categories, "chk", "sks", "ksk" and "loc".  The first three
34  * categories were named in analogy (!) to Freenet, but they do NOT
35  * work in exactly the same way.  They are very similar from the user's
36  * point of view (unique file identifier, subspace, keyword), but the
37  * implementation is rather different in pretty much every detail.
38  * The concrete URI formats are:
39  *
40  * <ul><li>
41  *
42  * First, there are URIs that identify a file.  They have the format
43  * "gnunet://fs/chk/HEX1.HEX2.SIZE".  These URIs can be used to
44  * download the file.  The description, filename, mime-type and other
45  * meta-data is NOT part of the file-URI since a URI uniquely
46  * identifies a resource (and the contents of the file would be the
47  * same even if it had a different description).
48  *
49  * </li><li>
50  *
51  * The second category identifies entries in a namespace.  The format
52  * is "gnunet://fs/sks/NAMESPACE/IDENTIFIER" where the namespace
53  * should be given in HEX.  Applications may allow using a nickname
54  * for the namespace if the nickname is not ambiguous.  The identifier
55  * can be either an ASCII sequence or a HEX-encoding.  If the
56  * identifier is in ASCII but the format is ambiguous and could denote
57  * a HEX-string a "/" is appended to indicate ASCII encoding.
58  *
59  * </li> <li>
60  *
61  * The third category identifies ordinary searches.  The format is
62  * "gnunet://fs/ksk/KEYWORD[+KEYWORD]*".  Using the "+" syntax
63  * it is possible to encode searches with the boolean "AND" operator.
64  * "+" is used since it indicates a commutative 'and' operation and
65  * is unlikely to be used in a keyword by itself.
66  *
67  * </li><li>
68  *
69  * The last category identifies a datum on a specific machine.  The
70  * format is "gnunet://fs/loc/HEX1.HEX2.SIZE.PEER.SIG.EXPTIME".  PEER is
71  * the BinName of the public key of the peer storing the datum.  The
72  * signature (SIG) certifies that this peer has this content.
73  * HEX1, HEX2 and SIZE correspond to a 'chk' URI.
74  *
75  * </li></ul>
76  *
77  * The encoding for hexadecimal values is defined in the hashing.c
78  * module in the gnunetutil library and discussed there.
79  * <p>
80  */
81 #include "platform.h"
82 #include "gnunet_fs_service.h"
83 #include "gnunet_signatures.h"
84 #include "fs_api.h"
85 #include <unitypes.h>
86 #include <unicase.h>
87 #include <uniconv.h>
88 #include <unistr.h>
89 #include <unistdio.h>
90
91
92
93 /**
94  * Get a unique key from a URI.  This is for putting URIs
95  * into HashMaps.  The key may change between FS implementations.
96  *
97  * @param uri uri to convert to a unique key
98  * @param key where to store the unique key
99  */
100 void
101 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri, 
102                       struct GNUNET_HashCode *key)
103 {
104   switch (uri->type)
105   {
106   case GNUNET_FS_URI_CHK:
107     *key = uri->data.chk.chk.query;
108     return;
109   case GNUNET_FS_URI_SKS:
110     GNUNET_CRYPTO_hash (uri->data.sks.identifier,
111                         strlen (uri->data.sks.identifier), key);
112     break;
113   case GNUNET_FS_URI_KSK:
114     if (uri->data.ksk.keywordCount > 0)
115       GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
116                           strlen (uri->data.ksk.keywords[0]), key);
117     break;
118   case GNUNET_FS_URI_LOC:
119     GNUNET_CRYPTO_hash (&uri->data.loc.fi,
120                         sizeof (struct FileIdentifier) +
121                         sizeof (struct GNUNET_CRYPTO_EccPublicKey),
122                         key);
123     break;
124   default:
125     memset (key, 0, sizeof (struct GNUNET_HashCode));
126     break;
127   }
128 }
129
130
131 /**
132  * Convert keyword URI to a human readable format
133  * (i.e. the search query that was used in the first place)
134  *
135  * @param uri ksk uri to convert to a string
136  * @return string with the keywords
137  */
138 char *
139 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
140 {
141   size_t n;
142   char *ret;
143   unsigned int i;
144   const char *keyword;
145   char **keywords;
146   unsigned int keywordCount;
147
148   if ((NULL == uri) || (GNUNET_FS_URI_KSK != uri->type))
149   {
150     GNUNET_break (0);
151     return NULL;
152   }
153   keywords = uri->data.ksk.keywords;
154   keywordCount = uri->data.ksk.keywordCount;
155   n = keywordCount + 1;
156   for (i = 0; i < keywordCount; i++)
157   {
158     keyword = keywords[i];
159     n += strlen (keyword) - 1;
160     if (NULL != strstr (&keyword[1], " "))
161       n += 2;
162     if (keyword[0] == '+')
163       n++;
164   }
165   ret = GNUNET_malloc (n);
166   strcpy (ret, "");
167   for (i = 0; i < keywordCount; i++)
168   {
169     keyword = keywords[i];
170     if (NULL != strstr (&keyword[1], " "))
171     {
172       strcat (ret, "\"");
173       if (keyword[0] == '+')
174         strcat (ret, keyword);
175       else
176         strcat (ret, &keyword[1]);
177       strcat (ret, "\"");
178     }
179     else
180     {
181       if (keyword[0] == '+')
182         strcat (ret, keyword);
183       else
184         strcat (ret, &keyword[1]);
185     }
186     strcat (ret, " ");
187   }
188   return ret;
189 }
190
191
192 /**
193  * Given a keyword with %-encoding (and possibly quotes to protect
194  * spaces), return a copy of the keyword without %-encoding and
195  * without double-quotes (%22).  Also, add a space at the beginning
196  * if there is not a '+'.
197  *
198  * @param in string with %-encoding
199  * @param emsg where to store the parser error message (if any)
200  * @return decodded string with leading space (or preserved plus)
201  */
202 static char *
203 percent_decode_keyword (const char *in, char **emsg)
204 {
205   char *out;
206   char *ret;
207   unsigned int rpos;
208   unsigned int wpos;
209   unsigned int hx;
210
211   out = GNUNET_strdup (in);
212   rpos = 0;
213   wpos = 0;
214   while (out[rpos] != '\0')
215   {
216     if (out[rpos] == '%')
217     {
218       if (1 != SSCANF (&out[rpos + 1], "%2X", &hx))
219       {
220         GNUNET_free (out);
221         *emsg = GNUNET_strdup (_(/* xgettext:no-c-format */
222                                  "`%' must be followed by HEX number"));
223         return NULL;
224       }
225       rpos += 3;
226       if (hx == '"')
227         continue;               /* skip double quote */
228       out[wpos++] = (char) hx;
229     }
230     else
231     {
232       out[wpos++] = out[rpos++];
233     }
234   }
235   out[wpos] = '\0';
236   if (out[0] == '+')
237   {
238     ret = GNUNET_strdup (out);
239   }
240   else
241   {
242     /* need to prefix with space */
243     ret = GNUNET_malloc (strlen (out) + 2);
244     strcpy (ret, " ");
245     strcat (ret, out);
246   }
247   GNUNET_free (out);
248   return ret;
249 }
250
251 #define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
252
253 /**
254  * Parse a KSK URI.
255  *
256  * @param s an uri string
257  * @param emsg where to store the parser error message (if any)
258  * @return NULL on error, otherwise the KSK URI
259  */
260 static struct GNUNET_FS_Uri *
261 uri_ksk_parse (const char *s, char **emsg)
262 {
263   struct GNUNET_FS_Uri *ret;
264   char **keywords;
265   unsigned int pos;
266   int max;
267   int iret;
268   int i;
269   size_t slen;
270   char *dup;
271   int saw_quote;
272
273   GNUNET_assert (NULL != s);
274   slen = strlen (s);
275   pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
276   if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX, pos)))
277     return NULL;                /* not KSK URI */
278   if ((s[slen - 1] == '+') || (s[pos] == '+'))
279   {
280     *emsg =
281         GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
282     return NULL;
283   }
284   max = 1;
285   saw_quote = 0;
286   for (i = pos; i < slen; i++)
287   {
288     if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
289     {
290       saw_quote = (saw_quote + 1) % 2;
291       i += 3;
292       continue;
293     }
294     if ((s[i] == '+') && (saw_quote == 0))
295     {
296       max++;
297       if (s[i - 1] == '+')
298       {
299         *emsg = GNUNET_strdup (_("`++' not allowed in KSK URI"));
300         return NULL;
301       }
302     }
303   }
304   if (saw_quote == 1)
305   {
306     *emsg = GNUNET_strdup (_("Quotes not balanced in KSK URI"));
307     return NULL;
308   }
309   iret = max;
310   dup = GNUNET_strdup (s);
311   keywords = GNUNET_malloc (max * sizeof (char *));
312   for (i = slen - 1; i >= pos; i--)
313   {
314     if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
315     {
316       saw_quote = (saw_quote + 1) % 2;
317       i += 3;
318       continue;
319     }
320     if ((dup[i] == '+') && (saw_quote == 0))
321     {
322       keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
323       if (NULL == keywords[max])
324         goto CLEANUP;
325       dup[i] = '\0';
326     }
327   }
328   keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
329   if (NULL == keywords[max])
330     goto CLEANUP;
331   GNUNET_assert (max == 0);
332   GNUNET_free (dup);
333   ret = GNUNET_new (struct GNUNET_FS_Uri);
334   ret->type = GNUNET_FS_URI_KSK;
335   ret->data.ksk.keywordCount = iret;
336   ret->data.ksk.keywords = keywords;
337   return ret;
338 CLEANUP:
339   for (i = 0; i < max; i++)
340     GNUNET_free_non_null (keywords[i]);
341   GNUNET_free (keywords);
342   GNUNET_free (dup);
343   return NULL;
344 }
345
346
347 #define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
348
349 /**
350  * Parse an SKS URI.
351  *
352  * @param s an uri string
353  * @param emsg where to store the parser error message (if any)
354  * @return NULL on error, SKS URI otherwise
355  */
356 static struct GNUNET_FS_Uri *
357 uri_sks_parse (const char *s, char **emsg)
358 {
359   struct GNUNET_FS_Uri *ret;
360   struct GNUNET_CRYPTO_EccPublicKey ns;
361   size_t pos;
362   char *end;
363
364   GNUNET_assert (s != NULL);
365   pos = strlen (GNUNET_FS_URI_SKS_PREFIX);
366   if ((strlen (s) <= pos) || (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX, pos)))
367     return NULL;                /* not an SKS URI */
368   end = strchr (&s[pos], '/');
369   if ( (NULL == end) ||
370        (GNUNET_OK !=
371         GNUNET_STRINGS_string_to_data (&s[pos], 
372                                        end - &s[pos],
373                                        &ns,
374                                        sizeof (ns))) )
375   {
376     *emsg = GNUNET_strdup (_("Malformed SKS URI"));
377     return NULL; /* malformed */
378   }
379   end++; /* skip over '/' */
380   ret = GNUNET_new (struct GNUNET_FS_Uri);
381   ret->type = GNUNET_FS_URI_SKS;
382   ret->data.sks.ns = ns;
383   ret->data.sks.identifier = GNUNET_strdup (end);
384   return ret;
385 }
386
387 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
388
389
390 /**
391  * Parse a CHK URI.
392  *
393  * @param s an uri string
394  * @param emsg where to store the parser error message (if any)
395  * @return NULL on error, CHK URI otherwise
396  */
397 static struct GNUNET_FS_Uri *
398 uri_chk_parse (const char *s, char **emsg)
399 {
400   struct GNUNET_FS_Uri *ret;
401   struct FileIdentifier fi;
402   unsigned int pos;
403   unsigned long long flen;
404   size_t slen;
405   char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
406   char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
407
408   if (NULL == s)
409     return NULL;
410   GNUNET_assert (s != NULL);
411   slen = strlen (s);
412   pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
413   if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
414       (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX, pos)))
415     return NULL;                /* not a CHK URI */
416   if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
417       (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
418   {
419     *emsg = GNUNET_strdup (_("Malformed CHK URI"));
420     return NULL;
421   }
422   memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
423   h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
424   memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
425           sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
426   h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
427
428   if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &fi.chk.key)) ||
429       (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &fi.chk.query)) ||
430       (1 !=
431        SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
432                "%llu", &flen)))
433   {
434     *emsg = GNUNET_strdup (_("Malformed CHK URI"));
435     return NULL;
436   }
437   fi.file_length = GNUNET_htonll (flen);
438   ret = GNUNET_new (struct GNUNET_FS_Uri);
439   ret->type = GNUNET_FS_URI_CHK;
440   ret->data.chk = fi;
441   return ret;
442 }
443
444
445 /**
446  * Convert a character back to the binary value
447  * that it represents (given base64-encoding).
448  *
449  * @param a character to convert
450  * @return offset in the "tbl" array
451  */
452 static unsigned int
453 c2v (unsigned char a)
454 {
455   if ((a >= '0') && (a <= '9'))
456     return a - '0';
457   if ((a >= 'A') && (a <= 'Z'))
458     return (a - 'A' + 10);
459   if ((a >= 'a') && (a <= 'z'))
460     return (a - 'a' + 36);
461   if (a == '_')
462     return 62;
463   if (a == '=')
464     return 63;
465   return -1;
466 }
467
468
469 /**
470  * Convert string back to binary data.
471  *
472  * @param input '\\0'-terminated string
473  * @param data where to write binary data
474  * @param size how much data should be converted
475  * @return number of characters processed from input,
476  *        -1 on error
477  */
478 static int
479 enc2bin (const char *input, void *data, size_t size)
480 {
481   size_t len;
482   size_t pos;
483   unsigned int bits;
484   unsigned int hbits;
485
486   len = size * 8 / 6;
487   if (((size * 8) % 6) != 0)
488     len++;
489   if (strlen (input) < len)
490     return -1;                  /* error! */
491   bits = 0;
492   hbits = 0;
493   len = 0;
494   for (pos = 0; pos < size; pos++)
495   {
496     while (hbits < 8)
497     {
498       bits |= (c2v (input[len++]) << hbits);
499       hbits += 6;
500     }
501     (((unsigned char *) data)[pos]) = (unsigned char) bits;
502     bits >>= 8;
503     hbits -= 8;
504   }
505   return len;
506 }
507
508
509 GNUNET_NETWORK_STRUCT_BEGIN
510 /**
511  * Structure that defines how the contents of a location URI must be
512  * assembled in memory to create or verify the signature of a location
513  * URI.
514  */
515 struct LocUriAssembly
516 {
517   /**
518    * What is being signed (rest of this struct).
519    */
520   struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
521
522   /**
523    * Expiration time of the offer.
524    */
525   struct GNUNET_TIME_AbsoluteNBO exptime;
526
527   /**
528    * File being offered.
529    */ 
530   struct FileIdentifier fi;
531
532   /**
533    * Peer offering the file.
534    */ 
535   struct GNUNET_CRYPTO_EccPublicKey peer;
536
537 };
538 GNUNET_NETWORK_STRUCT_END
539
540
541 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
542
543 /**
544  * Parse a LOC URI.
545  * Also verifies validity of the location URI.
546  *
547  * @param s an uri string
548  * @param emsg where to store the parser error message (if any)
549  * @return NULL on error, valid LOC URI otherwise
550  */
551 static struct GNUNET_FS_Uri *
552 uri_loc_parse (const char *s, char **emsg)
553 {
554   struct GNUNET_FS_Uri *uri;
555   char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
556   char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
557   unsigned int pos;
558   unsigned int npos;
559   unsigned long long exptime;
560   unsigned long long flen;
561   struct GNUNET_TIME_Absolute et;
562   struct GNUNET_CRYPTO_EccSignature sig;
563   struct LocUriAssembly ass;
564   int ret;
565   size_t slen;
566
567   GNUNET_assert (s != NULL);
568   slen = strlen (s);
569   pos = strlen (GNUNET_FS_URI_LOC_PREFIX);
570   if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
571       (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX, pos)))
572     return NULL;                /* not an SKS URI */
573   if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
574       (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
575   {
576     *emsg = GNUNET_strdup (_("SKS URI malformed"));
577     return NULL;
578   }
579   memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
580   h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
581   memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
582           sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
583   h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
584
585   if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &ass.fi.chk.key)) ||
586       (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &ass.fi.chk.query)) ||
587       (1 !=
588        SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
589                "%llu", &flen)))
590   {
591     *emsg = GNUNET_strdup (_("LOC URI malformed"));
592     return NULL;
593   }
594   ass.fi.file_length = GNUNET_htonll (flen);
595
596   npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
597   while ((s[npos] != '\0') && (s[npos] != '.'))
598     npos++;
599   if (s[npos] == '\0')
600   {
601     *emsg = GNUNET_strdup (_("LOC URI malformed"));
602     goto ERR;
603   }
604   npos++;
605   ret =
606       enc2bin (&s[npos], &ass.peer,
607                sizeof (struct GNUNET_CRYPTO_EccPublicKey));
608   if (ret == -1)
609   {
610     *emsg =
611         GNUNET_strdup (_("LOC URI malformed (could not decode public key)"));
612     goto ERR;
613   }
614   npos += ret;
615   if (s[npos++] != '.')
616   {
617     *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
618     goto ERR;
619   }
620   ret = enc2bin (&s[npos], &sig, sizeof (struct GNUNET_CRYPTO_EccSignature));
621   if (ret == -1)
622   {
623     *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
624     goto ERR;
625   }
626   npos += ret;
627   if (s[npos++] != '.')
628   {
629     *emsg = GNUNET_strdup (_("SKS URI malformed"));
630     goto ERR;
631   }
632   if (1 != SSCANF (&s[npos], "%llu", &exptime))
633   {
634     *emsg =
635         GNUNET_strdup (_
636                        ("SKS URI malformed (could not parse expiration time)"));
637     goto ERR;
638   }
639   ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
640   ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
641   et.abs_value_us = exptime * 1000LL * 1000LL;
642   ass.exptime = GNUNET_TIME_absolute_hton (et);
643   if (GNUNET_OK !=
644       GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
645                                 &ass.purpose, &sig, &ass.peer))
646   {
647     *emsg =
648         GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
649     goto ERR;
650   }
651   uri = GNUNET_new (struct GNUNET_FS_Uri);
652   uri->type = GNUNET_FS_URI_LOC;
653   uri->data.loc.fi = ass.fi;
654   uri->data.loc.peer = ass.peer;
655   uri->data.loc.expirationTime = et;
656   uri->data.loc.contentSignature = sig;
657
658   return uri;
659 ERR:
660   return NULL;
661 }
662
663
664 /**
665  * Convert a UTF-8 String to a URI.
666  *
667  * @param uri string to parse
668  * @param emsg where to store the parser error message (if any)
669  * @return NULL on error
670  */
671 struct GNUNET_FS_Uri *
672 GNUNET_FS_uri_parse (const char *uri, char **emsg)
673 {
674   struct GNUNET_FS_Uri *ret;
675   char *msg;
676
677   if (NULL == emsg)
678     emsg = &msg;
679   *emsg = NULL;
680   if ((NULL != (ret = uri_chk_parse (uri, emsg))) ||
681       (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
682       (NULL != (ret = uri_sks_parse (uri, emsg))) ||
683       (NULL != (ret = uri_loc_parse (uri, emsg))))
684     return ret;
685   if (NULL == *emsg)
686     *emsg = GNUNET_strdup (_("Unrecognized URI type"));
687   if (emsg == &msg)
688     GNUNET_free (msg);
689   return NULL;
690 }
691
692
693 /**
694  * Free URI.
695  *
696  * @param uri uri to free
697  */
698 void
699 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
700 {
701   unsigned int i;
702
703   GNUNET_assert (uri != NULL);
704   switch (uri->type)
705   {
706   case GNUNET_FS_URI_KSK:
707     for (i = 0; i < uri->data.ksk.keywordCount; i++)
708       GNUNET_free (uri->data.ksk.keywords[i]);
709     GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0);
710     break;
711   case GNUNET_FS_URI_SKS:
712     GNUNET_free (uri->data.sks.identifier);
713     break;
714   case GNUNET_FS_URI_LOC:
715     break;
716   default:
717     /* do nothing */
718     break;
719   }
720   GNUNET_free (uri);
721 }
722
723 /**
724  * How many keywords are ANDed in this keyword URI?
725  *
726  * @param uri ksk uri to get the number of keywords from
727  * @return 0 if this is not a keyword URI
728  */
729 unsigned int
730 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
731 {
732   if (uri->type != GNUNET_FS_URI_KSK)
733     return 0;
734   return uri->data.ksk.keywordCount;
735 }
736
737
738 /**
739  * Iterate over all keywords in this keyword URI.
740  *
741  * @param uri ksk uri to get the keywords from
742  * @param iterator function to call on each keyword
743  * @param iterator_cls closure for iterator
744  * @return -1 if this is not a keyword URI, otherwise number of
745  *   keywords iterated over until iterator aborted
746  */
747 int
748 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
749                                 GNUNET_FS_KeywordIterator iterator,
750                                 void *iterator_cls)
751 {
752   unsigned int i;
753   char *keyword;
754
755   if (uri->type != GNUNET_FS_URI_KSK)
756     return -1;
757   if (iterator == NULL)
758     return uri->data.ksk.keywordCount;
759   for (i = 0; i < uri->data.ksk.keywordCount; i++)
760   {
761     keyword = uri->data.ksk.keywords[i];
762     /* first character of keyword indicates
763      * if it is mandatory or not */
764     if (GNUNET_OK != iterator (iterator_cls, &keyword[1], keyword[0] == '+'))
765       return i;
766   }
767   return i;
768 }
769
770
771 /**
772  * Add the given keyword to the set of keywords represented by the URI.
773  * Does nothing if the keyword is already present.
774  *
775  * @param uri ksk uri to modify
776  * @param keyword keyword to add
777  * @param is_mandatory is this keyword mandatory?
778  */
779 void
780 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri, const char *keyword,
781                                int is_mandatory)
782 {
783   unsigned int i;
784   const char *old;
785   char *n;
786
787   GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
788   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       return;
793   }
794   GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword);
795   GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
796 }
797
798
799 /**
800  * Remove the given keyword from the set of keywords represented by the URI.
801  * Does nothing if the keyword is not present.
802  *
803  * @param uri ksk uri to modify
804  * @param keyword keyword to add
805  */
806 void
807 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
808                                   const char *keyword)
809 {
810   unsigned int i;
811   char *old;
812
813   GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
814   for (i = 0; i < uri->data.ksk.keywordCount; i++)
815   {
816     old = uri->data.ksk.keywords[i];
817     if (0 == strcmp (&old[1], keyword))
818     {
819       uri->data.ksk.keywords[i] =
820           uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1];
821       GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
822                          uri->data.ksk.keywordCount - 1);
823       GNUNET_free (old);
824       return;
825     }
826   }
827 }
828
829
830 /**
831  * Obtain the identity of the peer offering the data
832  *
833  * @param uri the location URI to inspect
834  * @param peer where to store the identify of the peer (presumably) offering the content
835  * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
836  */
837 int
838 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
839                                      struct GNUNET_PeerIdentity *peer)
840 {
841   if (uri->type != GNUNET_FS_URI_LOC)
842     return GNUNET_SYSERR;
843   GNUNET_CRYPTO_hash (&uri->data.loc.peer,
844                       sizeof (struct GNUNET_CRYPTO_EccPublicKey),
845                       &peer->hashPubKey);
846   return GNUNET_OK;
847 }
848
849
850 /**
851  * Obtain the expiration of the LOC URI.
852  *
853  * @param uri location URI to get the expiration from
854  * @return expiration time of the URI
855  */
856 struct GNUNET_TIME_Absolute
857 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
858 {
859   GNUNET_assert (uri->type == GNUNET_FS_URI_LOC);
860   return uri->data.loc.expirationTime;
861 }
862
863
864
865 /**
866  * Obtain the URI of the content itself.
867  *
868  * @param uri location URI to get the content URI from
869  * @return NULL if argument is not a location URI
870  */
871 struct GNUNET_FS_Uri *
872 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
873 {
874   struct GNUNET_FS_Uri *ret;
875
876   if (uri->type != GNUNET_FS_URI_LOC)
877     return NULL;
878   ret = GNUNET_new (struct GNUNET_FS_Uri);
879   ret->type = GNUNET_FS_URI_CHK;
880   ret->data.chk = uri->data.loc.fi;
881   return ret;
882 }
883
884
885 /**
886  * Construct a location URI (this peer will be used for the location).
887  *
888  * @param baseUri content offered by the sender
889  * @param cfg configuration information (used to find our hostkey)
890  * @param expiration_time how long will the content be offered?
891  * @return the location URI, NULL on error
892  */
893 struct GNUNET_FS_Uri *
894 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
895                           const struct GNUNET_CONFIGURATION_Handle *cfg,
896                           struct GNUNET_TIME_Absolute expiration_time)
897 {
898   struct GNUNET_FS_Uri *uri;
899   struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
900   struct GNUNET_CRYPTO_EccPublicKey my_public_key;
901   char *keyfile;
902   struct LocUriAssembly ass;
903   struct GNUNET_TIME_Absolute et;
904
905   if (baseUri->type != GNUNET_FS_URI_CHK)
906     return NULL;
907   if (GNUNET_OK !=
908       GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
909                                                &keyfile))
910   {
911     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
912                 _("Lacking key configuration settings.\n"));
913     return NULL;
914   }
915   if (NULL == (my_private_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile)))
916   {
917     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
918                 _("Could not access hostkey file `%s'.\n"), keyfile);
919     GNUNET_free (keyfile);
920     return NULL;
921   }
922   GNUNET_free (keyfile);
923   /* we round expiration time to full seconds for SKS URIs */
924   et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL;
925   GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
926   ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
927   ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
928   ass.exptime = GNUNET_TIME_absolute_hton (et);
929   ass.fi = baseUri->data.chk;
930   ass.peer = my_public_key;
931   uri = GNUNET_new (struct GNUNET_FS_Uri);
932   uri->type = GNUNET_FS_URI_LOC;
933   uri->data.loc.fi = baseUri->data.chk;
934   uri->data.loc.expirationTime = et;
935   uri->data.loc.peer = my_public_key;
936   GNUNET_assert (GNUNET_OK ==
937                  GNUNET_CRYPTO_ecc_sign (my_private_key, &ass.purpose,
938                                          &uri->data.loc.contentSignature));
939   GNUNET_free (my_private_key);
940   return uri;
941 }
942
943
944 /**
945  * Create an SKS URI from a namespace ID and an identifier.
946  *
947  * @param ns namespace ID
948  * @param id identifier
949  * @return an FS URI for the given namespace and identifier
950  */
951 struct GNUNET_FS_Uri *
952 GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EccPublicKey *ns, 
953                           const char *id)
954 {
955   struct GNUNET_FS_Uri *ns_uri;
956
957   ns_uri = GNUNET_new (struct GNUNET_FS_Uri);
958   ns_uri->type = GNUNET_FS_URI_SKS;
959   ns_uri->data.sks.ns = *ns;
960   ns_uri->data.sks.identifier = GNUNET_strdup (id);
961   return ns_uri;
962 }
963
964
965 /**
966  * Merge the sets of keywords from two KSK URIs.
967  * (useful for merging the canonicalized keywords with
968  * the original keywords for sharing).
969  *
970  * @param u1 first uri
971  * @param u2 second uri
972  * @return merged URI, NULL on error
973  */
974 struct GNUNET_FS_Uri *
975 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
976                          const struct GNUNET_FS_Uri *u2)
977 {
978   struct GNUNET_FS_Uri *ret;
979   unsigned int kc;
980   unsigned int i;
981   unsigned int j;
982   int found;
983   const char *kp;
984   char **kl;
985
986   if ((u1 == NULL) && (u2 == NULL))
987     return NULL;
988   if (u1 == NULL)
989     return GNUNET_FS_uri_dup (u2);
990   if (u2 == NULL)
991     return GNUNET_FS_uri_dup (u1);
992   if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK))
993   {
994     GNUNET_break (0);
995     return NULL;
996   }
997   kc = u1->data.ksk.keywordCount;
998   kl = GNUNET_malloc ((kc + u2->data.ksk.keywordCount) * sizeof (char *));
999   for (i = 0; i < u1->data.ksk.keywordCount; i++)
1000     kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
1001   for (i = 0; i < u2->data.ksk.keywordCount; i++)
1002   {
1003     kp = u2->data.ksk.keywords[i];
1004     found = 0;
1005     for (j = 0; j < u1->data.ksk.keywordCount; j++)
1006       if (0 == strcmp (kp + 1, kl[j] + 1))
1007       {
1008         found = 1;
1009         if (kp[0] == '+')
1010           kl[j][0] = '+';
1011         break;
1012       }
1013     if (0 == found)
1014       kl[kc++] = GNUNET_strdup (kp);
1015   }
1016   ret = GNUNET_new (struct GNUNET_FS_Uri);
1017   ret->type = GNUNET_FS_URI_KSK;
1018   ret->data.ksk.keywordCount = kc;
1019   ret->data.ksk.keywords = kl;
1020   return ret;
1021 }
1022
1023
1024 /**
1025  * Duplicate URI.
1026  *
1027  * @param uri the URI to duplicate
1028  * @return copy of the URI
1029  */
1030 struct GNUNET_FS_Uri *
1031 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1032 {
1033   struct GNUNET_FS_Uri *ret;
1034   unsigned int i;
1035
1036   if (uri == NULL)
1037     return NULL;
1038   ret = GNUNET_new (struct GNUNET_FS_Uri);
1039   memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
1040   switch (ret->type)
1041   {
1042   case GNUNET_FS_URI_KSK:
1043     if (ret->data.ksk.keywordCount >=
1044         GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
1045     {
1046       GNUNET_break (0);
1047       GNUNET_free (ret);
1048       return NULL;
1049     }
1050     if (ret->data.ksk.keywordCount > 0)
1051     {
1052       ret->data.ksk.keywords =
1053           GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1054       for (i = 0; i < ret->data.ksk.keywordCount; i++)
1055         ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]);
1056     }
1057     else
1058       ret->data.ksk.keywords = NULL;    /* just to be sure */
1059     break;
1060   case GNUNET_FS_URI_SKS:
1061     ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1062     break;
1063   case GNUNET_FS_URI_LOC:
1064     break;
1065   default:
1066     break;
1067   }
1068   return ret;
1069 }
1070
1071
1072 /**
1073  * Create an FS URI from a single user-supplied string of keywords.
1074  * The string is broken up at spaces into individual keywords.
1075  * Keywords that start with "+" are mandatory.  Double-quotes can
1076  * be used to prevent breaking up strings at spaces (and also
1077  * to specify non-mandatory keywords starting with "+").
1078  *
1079  * Keywords must contain a balanced number of double quotes and
1080  * double quotes can not be used in the actual keywords (for
1081  * example, the string '""foo bar""' will be turned into two
1082  * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1083  *
1084  * @param keywords the keyword string
1085  * @param emsg where to store an error message
1086  * @return an FS URI for the given keywords, NULL
1087  *  if keywords is not legal (i.e. empty).
1088  */
1089 struct GNUNET_FS_Uri *
1090 GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg)
1091 {
1092   char **keywordarr;
1093   unsigned int num_Words;
1094   int inWord;
1095   char *pos;
1096   struct GNUNET_FS_Uri *uri;
1097   char *searchString;
1098   int saw_quote;
1099
1100   if (keywords == NULL)
1101   {
1102     *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1103     GNUNET_break (0);
1104     return NULL;
1105   }
1106   searchString = GNUNET_strdup (keywords);
1107   num_Words = 0;
1108   inWord = 0;
1109   saw_quote = 0;
1110   pos = searchString;
1111   while ('\0' != *pos)
1112   {
1113     if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1114     {
1115       inWord = 0;
1116     }
1117     else if (0 == inWord)
1118     {
1119       inWord = 1;
1120       ++num_Words;
1121     }
1122     if ('"' == *pos)
1123       saw_quote = (saw_quote + 1) % 2;
1124     pos++;
1125   }
1126   if (num_Words == 0)
1127   {
1128     GNUNET_free (searchString);
1129     *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1130     return NULL;
1131   }
1132   if (saw_quote != 0)
1133   {
1134     GNUNET_free (searchString);
1135     *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1136     return NULL;
1137   }
1138   keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1139   num_Words = 0;
1140   inWord = 0;
1141   pos = searchString;
1142   while ('\0' != *pos)
1143   {
1144     if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1145     {
1146       inWord = 0;
1147       *pos = '\0';
1148     }
1149     else if (0 == inWord)
1150     {
1151       keywordarr[num_Words] = pos;
1152       inWord = 1;
1153       ++num_Words;
1154     }
1155     if ('"' == *pos)
1156       saw_quote = (saw_quote + 1) % 2;
1157     pos++;
1158   }
1159   uri =
1160       GNUNET_FS_uri_ksk_create_from_args (num_Words,
1161                                           (const char **) keywordarr);
1162   GNUNET_free (keywordarr);
1163   GNUNET_free (searchString);
1164   return uri;
1165 }
1166
1167
1168 /**
1169  * Create an FS URI from a user-supplied command line of keywords.
1170  * Arguments should start with "+" to indicate mandatory
1171  * keywords.
1172  *
1173  * @param argc number of keywords
1174  * @param argv keywords (double quotes are not required for
1175  *             keywords containing spaces; however, double
1176  *             quotes are required for keywords starting with
1177  *             "+"); there is no mechanism for having double
1178  *             quotes in the actual keywords (if the user
1179  *             did specifically specify double quotes, the
1180  *             caller should convert each double quote
1181  *             into two single quotes).
1182  * @return an FS URI for the given keywords, NULL
1183  *  if keywords is not legal (i.e. empty).
1184  */
1185 struct GNUNET_FS_Uri *
1186 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv)
1187 {
1188   unsigned int i;
1189   struct GNUNET_FS_Uri *uri;
1190   const char *keyword;
1191   char *val;
1192   const char *r;
1193   char *w;
1194   char *emsg;
1195
1196   if (argc == 0)
1197     return NULL;
1198   /* allow URI to be given as one and only keyword and
1199    * handle accordingly */
1200   emsg = NULL;
1201   if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) &&
1202       (0 ==
1203        strncmp (argv[0], GNUNET_FS_URI_PREFIX, strlen (GNUNET_FS_URI_PREFIX)))
1204       && (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg))))
1205     return uri;
1206   GNUNET_free_non_null (emsg);
1207   uri = GNUNET_new (struct GNUNET_FS_Uri);
1208   uri->type = GNUNET_FS_URI_KSK;
1209   uri->data.ksk.keywordCount = argc;
1210   uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1211   for (i = 0; i < argc; i++)
1212   {
1213     keyword = argv[i];
1214     if (keyword[0] == '+')
1215       val = GNUNET_strdup (keyword);
1216     else
1217       GNUNET_asprintf (&val, " %s", keyword);
1218     r = val;
1219     w = val;
1220     while ('\0' != *r)
1221     {
1222       if ('"' == *r)
1223         r++;
1224       else
1225         *(w++) = *(r++);
1226     }
1227     *w = '\0';
1228     uri->data.ksk.keywords[i] = val;
1229   }
1230   return uri;
1231 }
1232
1233
1234 /**
1235  * Test if two URIs are equal.
1236  *
1237  * @param u1 one of the URIs
1238  * @param u2 the other URI
1239  * @return GNUNET_YES if the URIs are equal
1240  */
1241 int
1242 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1243                           const struct GNUNET_FS_Uri *u2)
1244 {
1245   int ret;
1246   unsigned int i;
1247   unsigned int j;
1248
1249   GNUNET_assert (u1 != NULL);
1250   GNUNET_assert (u2 != NULL);
1251   if (u1->type != u2->type)
1252     return GNUNET_NO;
1253   switch (u1->type)
1254   {
1255   case GNUNET_FS_URI_CHK:
1256     if (0 ==
1257         memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier)))
1258       return GNUNET_YES;
1259     return GNUNET_NO;
1260   case GNUNET_FS_URI_SKS:
1261     if ((0 ==
1262          memcmp (&u1->data.sks.ns, &u2->data.sks.ns,
1263                  sizeof (struct GNUNET_CRYPTO_EccPublicKey))) &&
1264         (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
1265
1266       return GNUNET_YES;
1267     return GNUNET_NO;
1268   case GNUNET_FS_URI_KSK:
1269     if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1270       return GNUNET_NO;
1271     for (i = 0; i < u1->data.ksk.keywordCount; i++)
1272     {
1273       ret = GNUNET_NO;
1274       for (j = 0; j < u2->data.ksk.keywordCount; j++)
1275       {
1276         if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1277         {
1278           ret = GNUNET_YES;
1279           break;
1280         }
1281       }
1282       if (ret == GNUNET_NO)
1283         return GNUNET_NO;
1284     }
1285     return GNUNET_YES;
1286   case GNUNET_FS_URI_LOC:
1287     if (memcmp
1288         (&u1->data.loc, &u2->data.loc,
1289          sizeof (struct FileIdentifier) +
1290          sizeof (struct GNUNET_CRYPTO_EccPublicKey) +
1291          sizeof (struct GNUNET_TIME_Absolute) + sizeof (unsigned short) +
1292          sizeof (unsigned short)) != 0)
1293       return GNUNET_NO;
1294     return GNUNET_YES;
1295   default:
1296     return GNUNET_NO;
1297   }
1298 }
1299
1300
1301 /**
1302  * Is this a namespace URI?
1303  *
1304  * @param uri the uri to check
1305  * @return GNUNET_YES if this is an SKS uri
1306  */
1307 int
1308 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1309 {
1310   return uri->type == GNUNET_FS_URI_SKS;
1311 }
1312
1313
1314 /**
1315  * Get the ID of a namespace from the given
1316  * namespace URI.
1317  *
1318  * @param uri the uri to get the namespace ID from
1319  * @param pseudonym where to store the ID of the namespace
1320  * @return GNUNET_OK on success
1321  */
1322 int
1323 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1324                                  struct GNUNET_CRYPTO_EccPublicKey *pseudonym)
1325 {
1326   if (!GNUNET_FS_uri_test_sks (uri))
1327   {
1328     GNUNET_break (0);
1329     return GNUNET_SYSERR;
1330   }
1331   *pseudonym = uri->data.sks.ns;
1332   return GNUNET_OK;
1333 }
1334
1335
1336 /**
1337  * Get the content identifier of an SKS URI.
1338  *
1339  * @param uri the sks uri
1340  * @return NULL on error (not a valid SKS URI)
1341  */
1342 char *
1343 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1344 {
1345   if (!GNUNET_FS_uri_test_sks (uri))
1346   {
1347     GNUNET_break (0);
1348     return NULL;
1349   }
1350   return GNUNET_strdup (uri->data.sks.identifier);
1351 }
1352
1353
1354 /**
1355  * Convert namespace URI to a human readable format
1356  * (using the namespace description, if available).
1357  *
1358  * @param cfg configuration to use
1359  * @param uri SKS uri to convert
1360  * @return NULL on error (not an SKS URI)
1361  */
1362 char *
1363 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1364                                    const struct GNUNET_FS_Uri *uri)
1365 {
1366   char *ret;
1367   char *name;
1368   char *unique_name;
1369
1370   if (uri->type != GNUNET_FS_URI_SKS)
1371     return NULL;
1372   (void) GNUNET_FS_pseudonym_get_info (cfg, &uri->data.sks.ns,
1373                                     NULL, NULL, &name, NULL);
1374   unique_name = GNUNET_FS_pseudonym_name_uniquify (cfg, &uri->data.sks.ns, name, NULL);
1375   GNUNET_free (name);
1376   GNUNET_asprintf (&ret, "%s: %s", unique_name, uri->data.sks.identifier);
1377   GNUNET_free (unique_name);
1378   return ret;
1379 }
1380
1381
1382 /**
1383  * Is this a keyword URI?
1384  *
1385  * @param uri the uri
1386  * @return GNUNET_YES if this is a KSK uri
1387  */
1388 int
1389 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1390 {
1391 #if EXTRA_CHECKS
1392   unsigned int i;
1393
1394   if (uri->type == GNUNET_FS_URI_KSK)
1395   {
1396     for (i=0;i < uri->data.ksk.keywordCount; i++)
1397       GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1398   }
1399 #endif
1400   return uri->type == GNUNET_FS_URI_KSK;
1401 }
1402
1403
1404 /**
1405  * Is this a file (or directory) URI?
1406  *
1407  * @param uri the uri to check
1408  * @return GNUNET_YES if this is a CHK uri
1409  */
1410 int
1411 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1412 {
1413   return uri->type == GNUNET_FS_URI_CHK;
1414 }
1415
1416
1417 /**
1418  * What is the size of the file that this URI
1419  * refers to?
1420  *
1421  * @param uri the CHK URI to inspect
1422  * @return size of the file as specified in the CHK URI
1423  */
1424 uint64_t
1425 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri * uri)
1426 {
1427   switch (uri->type)
1428   {
1429   case GNUNET_FS_URI_CHK:
1430     return GNUNET_ntohll (uri->data.chk.file_length);
1431   case GNUNET_FS_URI_LOC:
1432     return GNUNET_ntohll (uri->data.loc.fi.file_length);
1433   default:
1434     GNUNET_assert (0);
1435   }
1436   return 0;                     /* unreachable */
1437 }
1438
1439
1440 /**
1441  * Is this a location URI?
1442  *
1443  * @param uri the uri to check
1444  * @return GNUNET_YES if this is a LOC uri
1445  */
1446 int
1447 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1448 {
1449   return uri->type == GNUNET_FS_URI_LOC;
1450 }
1451
1452
1453 /**
1454  * Add a keyword as non-mandatory (with ' '-prefix) to the
1455  * given keyword list at offset 'index'.  The array is
1456  * guaranteed to be long enough.
1457  * 
1458  * @param s keyword to add
1459  * @param array array to add the keyword to
1460  * @param index offset where to add the keyword
1461  */
1462 static void
1463 insert_non_mandatory_keyword (const char *s, char **array, int index)
1464 {
1465   char *nkword;
1466   GNUNET_asprintf (&nkword, " %s", /* space to mark as 'non mandatory' */ s);
1467   array[index] = nkword;
1468 }
1469
1470
1471 /**
1472  * Test if the given keyword 's' is already present in the 
1473  * given array, ignoring the '+'-mandatory prefix in the array.
1474  *
1475  * @param s keyword to test
1476  * @param array keywords to test against, with ' ' or '+' prefix to ignore
1477  * @param array_length length of the array
1478  * @return GNUNET_YES if the keyword exists, GNUNET_NO if not
1479  */ 
1480 static int
1481 find_duplicate (const char *s, const char **array, int array_length)
1482 {
1483   int j;
1484
1485   for (j = array_length - 1; j >= 0; j--)
1486     if (0 == strcmp (&array[j][1], s))
1487       return GNUNET_YES;
1488   return GNUNET_NO;
1489 }
1490
1491
1492 /**
1493  * FIXME: comment
1494  */
1495 static char *
1496 normalize_metadata (enum EXTRACTOR_MetaFormat format, const char *data,
1497     size_t data_len)
1498 {
1499   uint8_t *free_str = NULL;
1500   uint8_t *str_to_normalize = (uint8_t *) data;
1501   uint8_t *normalized;
1502   size_t r_len;
1503   if (str_to_normalize == NULL)
1504     return NULL;
1505   /* Don't trust libextractor */
1506   if (format == EXTRACTOR_METAFORMAT_UTF8)
1507   {
1508     free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len);
1509     if (free_str == NULL)
1510       free_str = NULL;
1511     else
1512       format = EXTRACTOR_METAFORMAT_C_STRING;
1513   }
1514   if (format == EXTRACTOR_METAFORMAT_C_STRING)
1515   {
1516     free_str = u8_strconv_from_encoding (data, locale_charset (), iconveh_escape_sequence);
1517     if (free_str == NULL)
1518       return NULL;
1519   }
1520
1521   normalized = u8_tolower (str_to_normalize, strlen ((char *) str_to_normalize), NULL, UNINORM_NFD, NULL, &r_len);
1522   /* free_str is allocated by libunistring internally, use free() */
1523   if (free_str != NULL)
1524     free (free_str);
1525   if (normalized != NULL)
1526   {
1527     /* u8_tolower allocates a non-NULL-terminated string! */
1528     free_str = GNUNET_malloc (r_len + 1);
1529     memcpy (free_str, normalized, r_len);
1530     free_str[r_len] = '\0';
1531     free (normalized);
1532     normalized = free_str;
1533   }
1534   return (char *) normalized;
1535 }
1536
1537 /**
1538  * Counts the number of UTF-8 characters (not bytes) in the string,
1539  * returns that count.
1540  */
1541 static size_t
1542 u8_strcount (const uint8_t *s)
1543 {
1544   size_t count;
1545   ucs4_t c;
1546   GNUNET_assert (s != NULL);
1547   if (s[0] == 0)
1548     return 0;
1549   for (count = 0; s != NULL; count++)
1550     s = u8_next (&c, s);
1551   return count - 1;
1552 }
1553
1554
1555 /**
1556  * Break the filename up by matching [], () and {} pairs to make
1557  * keywords. In case of nesting parentheses only the inner pair counts.
1558  * You can't escape parentheses to scan something like "[blah\{foo]" to
1559  * make a "blah{foo" keyword, this function is only a heuristic!
1560  *
1561  * @param s string to break down.
1562  * @param array array to fill with enclosed tokens. If NULL, then tokens
1563  *        are only counted.
1564  * @param index index at which to start filling the array (entries prior
1565  *        to it are used to check for duplicates). ignored if array == NULL.
1566  * @return number of tokens counted (including duplicates), or number of
1567  *         tokens extracted (excluding duplicates). 0 if there are no
1568  *         matching parens in the string (when counting), or when all tokens 
1569  *         were duplicates (when extracting).
1570  */
1571 static int
1572 get_keywords_from_parens (const char *s, char **array, int index)
1573 {
1574   int count = 0;
1575   char *open_paren;
1576   char *close_paren;
1577   char *ss;
1578   char tmp;
1579
1580   if (NULL == s)
1581     return 0;
1582   ss = GNUNET_strdup (s);
1583   open_paren = ss - 1;
1584   while (NULL != (open_paren = strpbrk (open_paren + 1, "[{(")))
1585   {
1586     int match = 0;
1587
1588     close_paren = strpbrk (open_paren + 1, "]})");
1589     if (NULL == close_paren)
1590       continue;
1591     switch (open_paren[0])
1592     {
1593     case '[':
1594       if (']' == close_paren[0])
1595         match = 1;
1596       break;
1597     case '{':
1598       if ('}' == close_paren[0])
1599         match = 1;
1600       break;
1601     case '(':
1602       if (')' == close_paren[0])
1603         match = 1;
1604       break;
1605     default:
1606       break;
1607     }
1608     if (match && (close_paren - open_paren > 1))
1609     {
1610       tmp = close_paren[0];
1611       close_paren[0] = '\0';
1612       /* Keywords must be at least 3 characters long */
1613       if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2)
1614       {
1615         close_paren[0] = tmp;
1616         continue;
1617       }
1618       if (NULL != array)
1619       {
1620         char *normalized;
1621         if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
1622             (const char **) array, index + count))
1623         {
1624           insert_non_mandatory_keyword ((const char *) &open_paren[1], array,
1625                                         index + count);
1626           count++;
1627         }
1628         normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1629             &open_paren[1], close_paren - &open_paren[1]);
1630         if (normalized != NULL)
1631         {
1632           if (GNUNET_NO == find_duplicate ((const char *) normalized,
1633               (const char **) array, index + count))
1634           {
1635             insert_non_mandatory_keyword ((const char *) normalized, array,
1636                                           index + count);
1637             count++;
1638           }
1639           GNUNET_free (normalized);
1640         }
1641       }
1642       else
1643         count++;
1644       close_paren[0] = tmp;
1645     }   
1646   }
1647   GNUNET_free (ss);
1648   return count;
1649 }
1650
1651
1652 /**
1653  * Where to break up keywords
1654  */
1655 #define TOKENS "_. /-!?#&+@\"\'\\;:,()[]{}$<>|"
1656
1657 /**
1658  * Break the filename up by TOKENS to make
1659  * keywords.
1660  *
1661  * @param s string to break down.
1662  * @param array array to fill with tokens. If NULL, then tokens are only
1663  *        counted.
1664  * @param index index at which to start filling the array (entries prior
1665  *        to it are used to check for duplicates). ignored if array == NULL.
1666  * @return number of tokens (>1) counted (including duplicates), or number of
1667  *         tokens extracted (excluding duplicates). 0 if there are no
1668  *         separators in the string (when counting), or when all tokens were
1669  *         duplicates (when extracting).
1670  */
1671 static int
1672 get_keywords_from_tokens (const char *s, char **array, int index)
1673 {
1674   char *p;
1675   char *ss;
1676   int seps = 0;
1677
1678   ss = GNUNET_strdup (s);
1679   for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS))
1680   {
1681     /* Keywords must be at least 3 characters long */
1682     if (u8_strcount ((const uint8_t *) p) <= 2)
1683       continue;
1684     if (NULL != array)
1685     {
1686       char *normalized;
1687       if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps))
1688       {
1689         insert_non_mandatory_keyword (p, array,
1690                                       index + seps);
1691         seps++;
1692       }
1693       normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1694           p, strlen (p));
1695       if (normalized != NULL)
1696       {
1697         if (GNUNET_NO == find_duplicate ((const char *) normalized,
1698             (const char **) array, index + seps))
1699         {
1700           insert_non_mandatory_keyword ((const char *) normalized, array,
1701                                   index + seps);
1702           seps++;
1703         }
1704         GNUNET_free (normalized);
1705       }
1706     }
1707     else
1708       seps++;
1709   }
1710   GNUNET_free (ss);
1711   return seps;
1712 }
1713 #undef TOKENS
1714
1715 /**
1716  * Function called on each value in the meta data.
1717  * Adds it to the URI.
1718  *
1719  * @param cls URI to update
1720  * @param plugin_name name of the plugin that produced this value;
1721  *        special values can be used (i.e. '&lt;zlib&gt;' for zlib being
1722  *        used in the main libextractor library and yielding
1723  *        meta data).
1724  * @param type libextractor-type describing the meta data
1725  * @param format basic format information about data
1726  * @param data_mime_type mime-type of data (not of the original file);
1727  *        can be NULL (if mime-type is not known)
1728  * @param data actual meta-data found
1729  * @param data_len number of bytes in data
1730  * @return 0 (always)
1731  */
1732 static int
1733 gather_uri_data (void *cls, const char *plugin_name,
1734                  enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
1735                  const char *data_mime_type, const char *data, size_t data_len)
1736 {
1737   struct GNUNET_FS_Uri *uri = cls;
1738   char *normalized_data;
1739
1740   if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1741       (format != EXTRACTOR_METAFORMAT_C_STRING))
1742     return 0;
1743   /* Keywords must be at least 3 characters long
1744    * If given non-utf8 string it will, most likely, find it to be invalid,
1745    * and will return the length of its valid part, skipping the keyword.
1746    * If it does - fix the extractor, not this check!
1747    */
1748   if (u8_strcount ((const uint8_t *) data) <= 2)
1749   {
1750     return 0;
1751   }
1752   normalized_data = normalize_metadata (format, data, data_len);
1753   if (!find_duplicate (data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
1754   {
1755     insert_non_mandatory_keyword (data,
1756                                   uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1757     uri->data.ksk.keywordCount++;
1758   }
1759   if (normalized_data != NULL)
1760   {
1761     if (!find_duplicate (normalized_data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
1762     {
1763       insert_non_mandatory_keyword (normalized_data,
1764                                     uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1765       uri->data.ksk.keywordCount++;
1766     }
1767     GNUNET_free (normalized_data);
1768   }
1769   return 0;
1770 }
1771
1772
1773 /**
1774  * Construct a keyword-URI from meta-data (take all entries
1775  * in the meta-data and construct one large keyword URI
1776  * that lists all keywords that can be found in the meta-data).
1777  *
1778  * @param md metadata to use
1779  * @return NULL on error, otherwise a KSK URI
1780  */
1781 struct GNUNET_FS_Uri *
1782 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
1783                                          *md)
1784 {
1785   struct GNUNET_FS_Uri *ret;
1786   char *filename;
1787   char *full_name = NULL;
1788   char *ss;
1789   int ent;
1790   int tok_keywords = 0;
1791   int paren_keywords = 0;
1792
1793   if (md == NULL)
1794     return NULL;
1795   ret = GNUNET_new (struct GNUNET_FS_Uri);
1796   ret->type = GNUNET_FS_URI_KSK;
1797   ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1798   if (ent > 0)
1799   {
1800     full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
1801         EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, -1);
1802     if (NULL != full_name)
1803     {
1804       filename = full_name;
1805       while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR)))
1806         filename = ss + 1;
1807       tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
1808       paren_keywords = get_keywords_from_parens (filename, NULL, 0);
1809     }
1810     /* x2 because there might be a normalized variant of every keyword */
1811     ret->data.ksk.keywords = GNUNET_malloc (sizeof (char *) * (ent
1812         + tok_keywords + paren_keywords) * 2);
1813     GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1814   }
1815   if (tok_keywords > 0)
1816     ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
1817         ret->data.ksk.keywords,
1818         ret->data.ksk.keywordCount);
1819   if (paren_keywords > 0)
1820     ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
1821         ret->data.ksk.keywords,
1822         ret->data.ksk.keywordCount);
1823   if (ent > 0)
1824     GNUNET_free_non_null (full_name);
1825   return ret;
1826 }
1827
1828
1829 /**
1830  * In URI-encoding, does the given character
1831  * need to be encoded using %-encoding?
1832  */
1833 static int
1834 needs_percent (char c)
1835 {
1836   return (!
1837           ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') ||
1838            (c == '.') || (c == '~')));
1839 }
1840
1841
1842 /**
1843  * Convert a KSK URI to a string.
1844  *
1845  * @param uri the URI to convert
1846  * @return NULL on error (i.e. keywordCount == 0)
1847  */
1848 static char *
1849 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1850 {
1851   char **keywords;
1852   unsigned int keywordCount;
1853   size_t n;
1854   char *ret;
1855   unsigned int i;
1856   unsigned int j;
1857   unsigned int wpos;
1858   size_t slen;
1859   const char *keyword;
1860
1861   if (uri->type != GNUNET_FS_URI_KSK)
1862     return NULL;
1863   keywords = uri->data.ksk.keywords;
1864   keywordCount = uri->data.ksk.keywordCount;
1865   n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1866       strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1867   for (i = 0; i < keywordCount; i++)
1868   {
1869     keyword = keywords[i];
1870     slen = strlen (keyword);
1871     n += slen;
1872     for (j = 0; j < slen; j++)
1873     {
1874       if ((j == 0) && (keyword[j] == ' '))
1875       {
1876         n--;
1877         continue;               /* skip leading space */
1878       }
1879       if (needs_percent (keyword[j]))
1880         n += 2;                 /* will use %-encoding */
1881     }
1882   }
1883   ret = GNUNET_malloc (n);
1884   strcpy (ret, GNUNET_FS_URI_PREFIX);
1885   strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1886   wpos = strlen (ret);
1887   for (i = 0; i < keywordCount; i++)
1888   {
1889     keyword = keywords[i];
1890     slen = strlen (keyword);
1891     for (j = 0; j < slen; j++)
1892     {
1893       if ((j == 0) && (keyword[j] == ' '))
1894         continue;               /* skip leading space */
1895       if (needs_percent (keyword[j]))
1896       {
1897         sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]);
1898         wpos += 3;
1899       }
1900       else
1901       {
1902         ret[wpos++] = keyword[j];
1903       }
1904     }
1905     if (i != keywordCount - 1)
1906       ret[wpos++] = '+';
1907   }
1908   return ret;
1909 }
1910
1911
1912 /**
1913  * Convert SKS URI to a string.
1914  *
1915  * @param uri sks uri to convert
1916  * @return NULL on error
1917  */
1918 static char *
1919 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1920 {
1921   char *ret;
1922   char buf[1024];
1923
1924   if (GNUNET_FS_URI_SKS != uri->type)
1925     return NULL;
1926   ret = GNUNET_STRINGS_data_to_string (&uri->data.sks.ns,
1927                                        sizeof (struct GNUNET_CRYPTO_EccPublicKey),
1928                                        buf,
1929                                        sizeof (buf));
1930   GNUNET_assert (NULL != ret);
1931   ret[0] = '\0';
1932   GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
1933                    GNUNET_FS_URI_SKS_INFIX, buf, 
1934                    uri->data.sks.identifier);
1935   return ret;
1936 }
1937
1938
1939 /**
1940  * Convert a CHK URI to a string.
1941  *
1942  * @param uri chk uri to convert
1943  * @return NULL on error
1944  */
1945 static char *
1946 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1947 {
1948   const struct FileIdentifier *fi;
1949   char *ret;
1950   struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1951   struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1952
1953   if (uri->type != GNUNET_FS_URI_CHK)
1954     return NULL;
1955   fi = &uri->data.chk;
1956   GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1957   GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1958
1959   GNUNET_asprintf (&ret, "%s%s%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1960                    GNUNET_FS_URI_CHK_INFIX, (const char *) &keyhash,
1961                    (const char *) &queryhash, GNUNET_ntohll (fi->file_length));
1962   return ret;
1963 }
1964
1965 /**
1966  * Convert binary data to a string.
1967  *
1968  * @param data binary data to convert
1969  * @param size number of bytes in data
1970  * @return converted data
1971  */
1972 static char *
1973 bin2enc (const void *data, size_t size)
1974 {
1975   /**
1976    * 64 characters for encoding, 6 bits per character
1977    */
1978   static char *tbl =
1979       "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1980
1981   size_t len;
1982   size_t pos;
1983   unsigned int bits;
1984   unsigned int hbits;
1985   char *ret;
1986
1987   GNUNET_assert (strlen (tbl) == 64);
1988   len = size * 8 / 6;
1989   if (((size * 8) % 6) != 0)
1990     len++;
1991   ret = GNUNET_malloc (len + 1);
1992   ret[len] = '\0';
1993   len = 0;
1994   bits = 0;
1995   hbits = 0;
1996   for (pos = 0; pos < size; pos++)
1997   {
1998     bits |= ((((const unsigned char *) data)[pos]) << hbits);
1999     hbits += 8;
2000     while (hbits >= 6)
2001     {
2002       ret[len++] = tbl[bits & 63];
2003       bits >>= 6;
2004       hbits -= 6;
2005     }
2006   }
2007   if (hbits > 0)
2008     ret[len] = tbl[bits & 63];
2009   return ret;
2010 }
2011
2012
2013 /**
2014  * Convert a LOC URI to a string.
2015  *
2016  * @param uri loc uri to convert
2017  * @return NULL on error
2018  */
2019 static char *
2020 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
2021 {
2022   char *ret;
2023   struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
2024   struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
2025   char *peerId;
2026   char *peerSig;
2027
2028   GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
2029   GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
2030   peerId =
2031       bin2enc (&uri->data.loc.peer,
2032                sizeof (struct GNUNET_CRYPTO_EccPublicKey));
2033   peerSig =
2034       bin2enc (&uri->data.loc.contentSignature,
2035                sizeof (struct GNUNET_CRYPTO_EccSignature));
2036   GNUNET_asprintf (&ret, 
2037                    "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
2038                    GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
2039                    (const char *) &queryhash,
2040                    (unsigned long long) GNUNET_ntohll (uri->data.loc.
2041                                                        fi.file_length), peerId,
2042                    peerSig,
2043                    (unsigned long long) uri->data.loc.expirationTime.abs_value_us / 1000000LL);
2044   GNUNET_free (peerSig);
2045   GNUNET_free (peerId);
2046   return ret;
2047 }
2048
2049
2050 /**
2051  * Convert a URI to a UTF-8 String.
2052  *
2053  * @param uri uri to convert to a string
2054  * @return the UTF-8 string
2055  */
2056 char *
2057 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
2058 {
2059   if (uri == NULL)
2060   {
2061     GNUNET_break (0);
2062     return NULL;
2063   }
2064   switch (uri->type)
2065   {
2066   case GNUNET_FS_URI_KSK:
2067     return uri_ksk_to_string (uri);
2068   case GNUNET_FS_URI_SKS:
2069     return uri_sks_to_string (uri);
2070   case GNUNET_FS_URI_CHK:
2071     return uri_chk_to_string (uri);
2072   case GNUNET_FS_URI_LOC:
2073     return uri_loc_to_string (uri);
2074   default:
2075     GNUNET_break (0);
2076     return NULL;
2077   }
2078 }
2079
2080 /* end of fs_uri.c */