cf0e8a06761e315aaed047bd663795c00071739e
[oweals/gnunet.git] / src / fs / fs_uri.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 2, 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 ecrs identifiers (MODULE = "ecrs").
30  * <p>
31  *
32  * This module only parses URIs for the AFS module.  The ECRS 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://ecrs/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://ecrs/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://ecrs/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://ecrs/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.h"
85
86
87 /**
88  * Get a unique key from a URI.  This is for putting URIs
89  * into HashMaps.  The key may change between FS implementations.
90  *
91  * @param uri uri to convert to a unique key
92  * @param key wherer to store the unique key
93  */
94 void 
95 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
96                       GNUNET_HashCode * key)
97 {
98   switch (uri->type)
99     {
100     case chk:
101       *key = uri->data.chk.chk.query;
102       return;
103     case sks:
104       GNUNET_CRYPTO_hash (uri->data.sks.identifier,
105                           strlen (uri->data.sks.identifier), key);
106       break;
107     case ksk:
108       if (uri->data.ksk.keywordCount > 0)
109         GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
110                             strlen (uri->data.ksk.keywords[0]), key);
111       break;
112     case loc:
113       GNUNET_CRYPTO_hash (&uri->data.loc.fi,
114                           sizeof (struct FileIdentifier) +
115                           sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), key);
116       break;
117     default:
118       memset (key, 0, sizeof (GNUNET_HashCode));
119       break;
120     }
121 }
122
123
124 /**
125  * Convert keyword URI to a human readable format
126  * (i.e. the search query that was used in the first place)
127  *
128  * @param uri ksk uri to convert to a string 
129  * @return string with the keywords
130  */
131 char *
132 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
133 {
134   size_t n;
135   char *ret;
136   unsigned int i;
137   const char *keyword;
138   char **keywords;
139   unsigned int keywordCount;
140
141   if ((uri == NULL) || (uri->type != ksk))
142     {
143       GNUNET_break (0);
144       return NULL;
145     }
146   keywords = uri->data.ksk.keywords;
147   keywordCount = uri->data.ksk.keywordCount;
148   n = keywordCount + 1;
149   for (i = 0; i < keywordCount; i++)
150     {
151       keyword = keywords[i];
152       n += strlen (keyword) - 1;
153       if (NULL != strstr (&keyword[1], " "))
154         n += 2;
155       if (keyword[0] == '+')
156         n++;
157     }
158   ret = GNUNET_malloc (n);
159   strcpy (ret, "");
160   for (i = 0; i < keywordCount; i++)
161     {
162       keyword = keywords[i];
163       if (NULL != strstr (&keyword[1], " "))
164         {
165           strcat (ret, "\"");
166           if (keyword[0] == '+')
167             strcat (ret, keyword);
168           else
169             strcat (ret, &keyword[1]);
170           strcat (ret, "\"");
171         }
172       else
173         {
174           if (keyword[0] == '+')
175             strcat (ret, keyword);
176           else
177             strcat (ret, &keyword[1]);
178         }
179       strcat (ret, " ");
180     }
181   return ret;
182 }
183
184
185
186
187 /**
188  * Given a keyword with %-encoding (and possibly quotes to protect
189  * spaces), return a copy of the keyword without %-encoding and
190  * without double-quotes (%22).  Also, add a space at the beginning
191  * if there is not a '+'.
192  * 
193  * @param in string with %-encoding
194  * @param emsg where to store the parser error message (if any)
195  * @return decodded string with leading space (or preserved plus)
196  */
197 static char *
198 percent_decode_keyword (const char *in, char **emsg)
199 {
200   char *out;
201   char *ret;
202   unsigned int rpos;
203   unsigned int wpos;
204   unsigned int hx;
205
206   out = GNUNET_strdup (in);
207   rpos = 0;
208   wpos = 0;
209   while (out[rpos] != '\0')
210     {
211       if (out[rpos] == '%')
212         {
213           if (1 != sscanf (&out[rpos + 1], "%2X", &hx))
214             {
215               GNUNET_free (out);
216               return NULL;
217             }
218           rpos += 3;
219           if (hx == '"')
220             continue;           /* skip double quote */
221           out[wpos++] = (char) hx;
222         }
223       else
224         {
225           out[wpos++] = out[rpos++];
226         }
227     }
228   out[wpos] = '\0';
229   if (out[0] == '+')
230     {
231       ret = GNUNET_strdup (out);
232     }
233   else
234     {
235       /* need to prefix with space */
236       ret = GNUNET_malloc (strlen (out) + 2);
237       strcpy (ret, " ");
238       strcat (ret, out);
239     }
240   GNUNET_free (out);
241   return ret;
242 }
243
244
245 /**
246  * Parse a KSK URI.
247  *
248  * @param s an uri string
249  * @param emsg where to store the parser error message (if any)
250  * @return NULL on error, otherwise the KSK URI
251  */
252 static struct GNUNET_FS_Uri *
253 uri_ksk_parse (const char *s, char **emsg)
254 {
255   struct GNUNET_FS_Uri *ret;
256   char **keywords;
257   unsigned int pos;
258   int max;
259   int iret;
260   int i;
261   size_t slen;
262   char *dup;
263   int saw_quote;
264
265   GNUNET_assert (s != NULL);
266   slen = strlen (s);
267   pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX);
268   if ( (slen <= pos) ||
269        (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX, 
270                       pos) ) ||
271        (s[slen - 1] == '+') ||
272        (s[pos] == '+') )
273     return NULL;       /* no keywords / malformed */
274   
275   max = 1;
276   saw_quote = 0;
277   for (i = pos; i < slen; i++)
278     {
279       if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
280         {
281           saw_quote = (saw_quote + 1) % 2;
282           i += 3;
283           continue;
284         }
285       if ((s[i] == '+') && (saw_quote == 0))
286         {
287           max++;
288           if (s[i - 1] == '+')
289             return NULL;       /* "++" not allowed */
290         }
291     }
292   if (saw_quote == 1)
293     return NULL;       /* quotes not balanced */
294   iret = max;
295   dup = GNUNET_strdup (s);
296   keywords = GNUNET_malloc (max * sizeof (char *));
297   for (i = slen - 1; i >= pos; i--)
298     {
299       if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
300         {
301           saw_quote = (saw_quote + 1) % 2;
302           i += 3;
303           continue;
304         }
305       if ((dup[i] == '+') && (saw_quote == 0))
306         {
307           keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
308           if (NULL == keywords[max])
309             goto CLEANUP;
310           dup[i] = '\0';
311         }
312     }
313   keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
314   if (NULL == keywords[max])
315     goto CLEANUP;
316   GNUNET_assert (max == 0);
317   GNUNET_free (dup);
318   ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
319   ret->type = ksk;
320   ret->data.ksk.keywordCount = iret;
321   ret->data.ksk.keywords = keywords;
322   return ret;
323 CLEANUP:
324   for (i = 0; i < max; i++)
325     GNUNET_free_non_null (keywords[i]);
326   GNUNET_free (keywords);
327   GNUNET_free (dup);
328   return NULL;
329 }
330
331
332 /**
333  * Parse an SKS URI.
334  *
335  * @param s an uri string
336  * @param emsg where to store the parser error message (if any)
337  * @return NULL on error, SKS URI otherwise
338  */
339 static struct GNUNET_FS_Uri *
340 uri_sks_parse (const char *s, char **emsg)
341 {
342   struct GNUNET_FS_Uri *ret;
343   GNUNET_HashCode namespace;
344   char *identifier;
345   unsigned int pos;
346   size_t slen;
347   char enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
348
349   GNUNET_assert (s != NULL);
350   slen = strlen (s);
351   pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX);
352   if ( (slen <= pos) ||
353        (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX, 
354                       pos) ) ||
355        (slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
356        (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/') )
357     return NULL;
358   memcpy (enc, &s[pos], sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
359   enc[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
360   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (enc, &namespace))
361     return NULL;
362   identifier = GNUNET_strdup (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]);
363   ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
364   ret->type = sks;
365   ret->data.sks.namespace = namespace;
366   ret->data.sks.identifier = identifier;
367   return ret;
368 }
369
370
371 /**
372  * Parse a CHK URI.
373  *
374  * @param s an uri string
375  * @param emsg where to store the parser error message (if any)
376  * @return NULL on error, CHK URI otherwise
377  */
378 static struct GNUNET_FS_Uri *
379 uri_chk_parse (const char *s, char **emsg)
380 {
381   struct GNUNET_FS_Uri *ret;
382   struct FileIdentifier fi;
383   unsigned int pos;
384   size_t slen;
385   char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
386   char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
387
388   GNUNET_assert (s != NULL);
389
390   slen = strlen (s);
391   pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX);
392   if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
393        (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX, 
394                       pos) ) ||
395        (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
396        (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
397     return NULL;
398
399   memcpy (h1,
400           &s[pos], 
401           sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
402   h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
403   memcpy (h2,
404           &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
405           sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
406   h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
407   
408   if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
409                                                &fi.chk.key)) ||
410       (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
411                                                &fi.chk.query)) ||
412       (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
413                     "%llu", 
414                     &fi.file_length)))
415     return NULL;
416   fi.file_length = GNUNET_htonll (fi.file_length);
417
418   ret = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
419   ret->type = chk;
420   ret->data.chk = fi;
421   return ret;
422 }
423
424
425 /**
426  * Convert a character back to the binary value
427  * that it represents (given base64-encoding).
428  *
429  * @param a character to convert
430  * @return offset in the "tbl" array
431  */
432 static unsigned int
433 c2v (unsigned char a)
434 {
435   if ((a >= '0') && (a <= '9'))
436     return a - '0';
437   if ((a >= 'A') && (a <= 'Z'))
438     return (a - 'A' + 10);
439   if ((a >= 'a') && (a <= 'z'))
440     return (a - 'a' + 36);
441   if (a == '_')
442     return 62;
443   if (a == '=')
444     return 63;
445   return -1;
446 }
447
448
449 /**
450  * Convert string back to binary data.
451  *
452  * @param input '\0'-terminated string
453  * @param data where to write binary data
454  * @param size how much data should be converted
455  * @return number of characters processed from input,
456  *        -1 on error
457  */
458 static int
459 enc2bin (const char *input, void *data, size_t size)
460 {
461   size_t len;
462   size_t pos;
463   unsigned int bits;
464   unsigned int hbits;
465
466   len = size * 8 / 6;
467   if (((size * 8) % 6) != 0)
468     len++;
469   if (strlen (input) < len)
470     return -1;                  /* error! */
471   bits = 0;
472   hbits = 0;
473   len = 0;
474   pos = 0;
475   for (pos = 0; pos < size; pos++)
476     {
477       while (hbits < 8)
478         {
479           bits |= (c2v (input[len++]) << hbits);
480           hbits += 6;
481         }
482       (((unsigned char *) data)[pos]) = (unsigned char) bits;
483       bits >>= 8;
484       hbits -= 8;
485     }
486   return len;
487 }
488
489
490 /**
491  * Structure that defines how the
492  * contents of a location URI must be
493  * assembled in memory to create or
494  * verify the signature of a location
495  * URI.
496  */
497 struct LocUriAssembly 
498 {
499   struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
500
501   struct GNUNET_TIME_AbsoluteNBO exptime;
502
503   struct FileIdentifier fi;
504   
505   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
506
507 };
508
509
510 /**
511  * Parse a LOC URI.
512  * Also verifies validity of the location URI.
513  *
514  * @param s an uri string
515  * @param emsg where to store the parser error message (if any)
516  * @return NULL on error, valid LOC URI otherwise
517  */
518 static struct GNUNET_FS_Uri *
519 uri_loc_parse (const char *s, char **emsg)
520 {
521   struct GNUNET_FS_Uri *uri;
522   char h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
523   char h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)];
524   unsigned int pos;
525   unsigned int npos;
526   unsigned long long exptime;
527   struct GNUNET_TIME_Absolute et;
528   struct GNUNET_CRYPTO_RsaSignature sig;
529   struct LocUriAssembly ass;
530   int ret;
531   size_t slen;
532   char *addr;
533
534   GNUNET_assert (s != NULL);
535   slen = strlen (s);
536   pos = strlen (GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX);
537   if ( (slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
538        (0 != strncmp (s, GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX, 
539                       pos) ) ||
540        (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
541        (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.') )
542     return NULL;
543
544   memcpy (h1,
545           &s[pos], 
546           sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
547   h1[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
548   memcpy (h2,
549           &s[pos + sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)],
550           sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded));
551   h2[sizeof(struct GNUNET_CRYPTO_HashAsciiEncoded)-1] = '\0';
552   
553   if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1,
554                                                     &ass.fi.chk.key)) ||
555       (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2,
556                                                     &ass.fi.chk.query)) ||
557       (1 != SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
558                     "%llu", 
559                     &ass.fi.file_length)) )
560     return NULL;
561   ass.fi.file_length = GNUNET_htonll (ass.fi.file_length);
562
563   npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
564   while ((s[npos] != '\0') && (s[npos] != '.'))
565     npos++;
566   if (s[npos] == '\0')
567     goto ERR;
568   ret = enc2bin (&s[npos], 
569                  &ass.peer,
570                  sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
571   if (ret == -1)
572     goto ERR;
573   npos += ret;
574   if (s[npos++] != '.')
575     goto ERR;
576   ret = enc2bin (&s[npos],
577                  &sig,
578                  sizeof (struct GNUNET_CRYPTO_RsaSignature));
579   if (ret == -1)
580     goto ERR;
581   npos += ret;
582   if (s[npos++] != '.')
583     goto ERR;
584   if (1 != SSCANF (&s[npos], "%llu", &exptime))
585     goto ERR;
586   ass.purpose.size = htonl(sizeof(struct LocUriAssembly));
587   ass.purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT);
588   et.value = exptime;
589   ass.exptime = GNUNET_TIME_absolute_hton (et);
590   if (GNUNET_OK != 
591       GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_NAMESPACE_PLACEMENT,
592                                 &ass.purpose,
593                                 &sig,
594                                 &ass.peer))
595     goto ERR;
596
597   uri = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri));
598   uri->type = loc;
599   uri->data.loc.fi = ass.fi;
600   uri->data.loc.peer = ass.peer;
601   uri->data.loc.expirationTime = et;
602   uri->data.loc.contentSignature = sig;
603
604   return uri;
605 ERR:
606   GNUNET_free_non_null (addr);
607   return NULL;
608 }
609
610
611 /**
612  * Convert a UTF-8 String to a URI.
613  *
614  * @param uri string to parse
615  * @param emsg where to store the parser error message (if any)
616  * @return NULL on error
617  */
618 struct GNUNET_FS_Uri *
619 GNUNET_FS_uri_parse (const char *uri,
620                      char **emsg)
621 {
622   struct GNUNET_FS_Uri *ret;
623
624   if ( (NULL != (ret = uri_chk_parse (uri, emsg))) ||
625        (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
626        (NULL != (ret = uri_sks_parse (uri, emsg))) ||
627        (NULL != (ret = uri_loc_parse (uri, emsg))) )
628     return ret;
629   return NULL;
630 }
631
632
633 /**
634  * Free URI.
635  *
636  * @param uri uri to free
637  */
638 void 
639 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
640 {
641   unsigned int i;
642
643   GNUNET_assert (uri != NULL);
644   switch (uri->type)
645     {
646     case ksk:
647       for (i = 0; i < uri->data.ksk.keywordCount; i++)
648         GNUNET_free (uri->data.ksk.keywords[i]);
649       GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
650                          0);
651       break;
652     case sks:
653       GNUNET_free (uri->data.sks.identifier);
654       break;
655     case loc:
656       break;
657     default:
658       /* do nothing */
659       break;
660     }
661   GNUNET_free (uri);
662 }
663
664 /**
665  * How many keywords are ANDed in this keyword URI?
666  *
667  * @param uri ksk uri to get the number of keywords from
668  * @return 0 if this is not a keyword URI
669  */
670 unsigned int 
671 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
672 {
673   if (uri->type != ksk)
674     return 0;
675   return uri->data.ksk.keywordCount;
676 }
677
678
679 /**
680  * Iterate over all keywords in this keyword URI.
681  *
682  * @param uri ksk uri to get the keywords from
683  * @param iterator function to call on each keyword
684  * @param iterator_cls closure for iterator
685  * @return -1 if this is not a keyword URI, otherwise number of
686  *   keywords iterated over until iterator aborted
687  */
688 int 
689 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
690                                 GNUNET_FS_KeywordIterator iterator, 
691                                 void *iterator_cls)
692 {
693   unsigned int i;
694   char *keyword;
695
696   if (uri->type != ksk)
697     return -1;
698   if (iterator == NULL)
699     return uri->data.ksk.keywordCount;
700   for (i = 0; i < uri->data.ksk.keywordCount; i++)
701     {
702       keyword = uri->data.ksk.keywords[i];
703       /* first character of keyword indicates
704          if it is mandatory or not */
705       if (GNUNET_OK != iterator (iterator_cls,
706                                  &keyword[1],
707                                  keyword[0] == '+'))
708         return i;
709     }
710   return i;
711 }
712
713
714 /**
715  * Obtain the identity of the peer offering the data
716  *
717  * @param uri the location URI to inspect
718  * @param peer where to store the identify of the peer (presumably) offering the content
719  * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
720  */
721 int
722 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
723                                      struct GNUNET_PeerIdentity * peer)
724 {
725   if (uri->type != loc)
726     return GNUNET_SYSERR;
727   GNUNET_CRYPTO_hash (&uri->data.loc.peer,
728                       sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
729                       &peer->hashPubKey);
730   return GNUNET_OK;
731 }
732
733
734 /**
735  * Obtain the URI of the content itself.
736  *
737  * @param uri location URI to get the content URI from
738  * @return NULL if argument is not a location URI
739  */
740 struct GNUNET_FS_Uri *
741 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
742 {
743   struct GNUNET_FS_Uri *ret;
744
745   if (uri->type != loc)
746     return NULL;
747   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
748   ret->type = chk;
749   ret->data.chk = uri->data.loc.fi;
750   return ret;
751 }
752
753
754 /**
755  * Construct a location URI (this peer will be used for the location).
756  *
757  * @param baseURI content offered by the sender
758  * @param cfg configuration information (used to find our hostkey)
759  * @param expiration_time how long will the content be offered?
760  * @return the location URI, NULL on error
761  */
762 struct GNUNET_FS_Uri *
763 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
764                           struct GNUNET_CONFIGURATION_Handle *cfg,
765                           struct GNUNET_TIME_Absolute expiration_time);
766
767
768 /**
769  * Canonicalize keyword URI.  Performs operations such
770  * as decapitalization and removal of certain characters.
771  * (useful for search).
772  *
773  * @param uri the URI to canonicalize 
774  * @return canonicalized version of the URI, NULL on error
775  */
776 struct GNUNET_FS_Uri *
777 GNUNET_FS_uri_ksk_canonicalize (const struct GNUNET_FS_Uri *uri)
778 {
779   /* FIXME: not implemented */
780   return NULL;
781 }
782
783
784 /**
785  * Merge the sets of keywords from two KSK URIs.
786  * (useful for merging the canonicalized keywords with
787  * the original keywords for sharing).
788  *
789  * @param u1 first uri
790  * @param u2 second uri
791  * @return merged URI, NULL on error
792  */
793 struct GNUNET_FS_Uri *
794 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
795                          const struct GNUNET_FS_Uri *u2)
796 {
797   /* FIXME */
798   return NULL;
799 }
800
801
802 /**
803  * Duplicate URI.
804  *
805  * @param uri the URI to duplicate
806  * @return copy of the URI
807  */
808 struct GNUNET_FS_Uri *
809 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
810 {
811   struct GNUNET_FS_Uri *ret;
812   unsigned int i;
813
814   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
815   memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
816   switch (ret->type)
817     {
818     case ksk:
819       if (ret->data.ksk.keywordCount > 0)
820         {
821           ret->data.ksk.keywords
822             = GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
823           for (i = 0; i < ret->data.ksk.keywordCount; i++)
824             ret->data.ksk.keywords[i] =
825               GNUNET_strdup (uri->data.ksk.keywords[i]);
826         }
827       else
828         ret->data.ksk.keywords = NULL;  /* just to be sure */
829       break;
830     case sks:
831       ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
832       break;
833     case loc:
834       break;
835     default:
836       break;
837     }
838   return ret;
839 }
840
841
842 /**
843  * Create an FS URI from a single user-supplied string of keywords.
844  * The string is broken up at spaces into individual keywords.
845  * Keywords that start with "+" are mandatory.  Double-quotes can
846  * be used to prevent breaking up strings at spaces (and also
847  * to specify non-mandatory keywords starting with "+").
848  *
849  * Keywords must contain a balanced number of double quotes and
850  * double quotes can not be used in the actual keywords (for
851  * example, the string '""foo bar""' will be turned into two
852  * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
853  *
854  * @param keywords the keyword string
855  * @return an FS URI for the given keywords, NULL
856  *  if keywords is not legal (i.e. empty).
857  */
858 struct GNUNET_FS_Uri *
859 GNUNET_FS_uri_ksk_create (const char *keywords)
860 {
861   /* FIXME */
862   return NULL;
863 }
864
865
866 /**
867  * Create an FS URI from a user-supplied command line of keywords.
868  * Arguments should start with "+" to indicate mandatory
869  * keywords.
870  *
871  * @param argc number of keywords
872  * @param argv keywords (double quotes are not required for
873  *             keywords containing spaces; however, double
874  *             quotes are required for keywords starting with
875  *             "+"); there is no mechanism for having double
876  *             quotes in the actual keywords (if the user
877  *             did specifically specify double quotes, the
878  *             caller should convert each double quote
879  *             into two single quotes).
880  * @return an FS URI for the given keywords, NULL
881  *  if keywords is not legal (i.e. empty).
882  */
883 struct GNUNET_FS_Uri *
884 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
885                                     const char **argv)
886 {
887   /* FIXME */
888   return NULL;
889 }
890
891
892 /**
893  * Test if two URIs are equal.
894  *
895  * @param u1 one of the URIs
896  * @param u2 the other URI
897  * @return GNUNET_YES if the URIs are equal
898  */
899 int 
900 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
901                           const struct GNUNET_FS_Uri *u2)
902 {
903   int ret;
904   unsigned int i;
905   unsigned int j;
906
907   GNUNET_assert (u1 != NULL);
908   GNUNET_assert (u2 != NULL);
909   if (u1->type != u2->type)
910     return GNUNET_NO;
911   switch (u1->type)
912     {
913     case chk:
914       if (0 == memcmp (&u1->data.chk,
915                        &u2->data.chk,
916                        sizeof (struct FileIdentifier)))
917         return GNUNET_YES;
918       return GNUNET_NO;
919     case sks:
920       if ((0 == memcmp (&u1->data.sks.namespace,
921                         &u2->data.sks.namespace,
922                         sizeof (GNUNET_HashCode))) &&
923           (0 == strcmp (u1->data.sks.identifier,
924                         u2->data.sks.identifier)))
925
926         return GNUNET_YES;
927       return GNUNET_NO;
928     case ksk:
929       if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
930         return GNUNET_NO;
931       for (i = 0; i < u1->data.ksk.keywordCount; i++)
932         {
933           ret = GNUNET_NO;
934           for (j = 0; j < u2->data.ksk.keywordCount; j++)
935             {
936               if (0 == strcmp (u1->data.ksk.keywords[i],
937                                u2->data.ksk.keywords[j]))
938                 {
939                   ret = GNUNET_YES;
940                   break;
941                 }
942             }
943           if (ret == GNUNET_NO)
944             return GNUNET_NO;
945         }
946       return GNUNET_YES;
947     case loc:
948       if (memcmp (&u1->data.loc,
949                   &u2->data.loc,
950                   sizeof (struct FileIdentifier) +
951                   sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
952                   sizeof (struct GNUNET_TIME_Absolute) +
953                   sizeof (unsigned short) + sizeof (unsigned short)) != 0)
954         return GNUNET_NO;
955       return GNUNET_YES;
956     default:
957       return GNUNET_NO;
958     }
959 }
960
961
962 /**
963  * Is this a namespace URI?
964  *
965  * @param uri the uri to check
966  * @return GNUNET_YES if this is an SKS uri
967  */
968 int
969 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
970 {
971   return uri->type == sks;
972 }
973
974
975 /**
976  * Get the ID of a namespace from the given
977  * namespace URI.
978  *
979  * @param uri the uri to get the namespace ID from
980  * @param nsid where to store the ID of the namespace
981  * @return GNUNET_OK on success
982  */
983 int 
984 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
985                                  GNUNET_HashCode * nsid)
986 {
987   if (! GNUNET_FS_uri_test_sks (uri))
988     {
989       GNUNET_break (0);
990       return GNUNET_SYSERR;
991     }
992   *nsid = uri->data.sks.namespace;
993   return GNUNET_OK;
994 }
995
996
997 /**
998  * Get the content identifier of an SKS URI.
999  *
1000  * @param uri the sks uri
1001  * @return NULL on error (not a valid SKS URI)
1002  */
1003 char *
1004 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1005 {
1006   if (!GNUNET_FS_uri_test_sks (uri))
1007     {
1008       GNUNET_break (0);
1009       return NULL;
1010     }
1011   return GNUNET_strdup (uri->data.sks.identifier);
1012 }
1013
1014
1015 /**
1016  * Convert namespace URI to a human readable format
1017  * (using the namespace description, if available).
1018  *
1019  * @param cfg configuration to use
1020  * @param uri SKS uri to convert
1021  * @return NULL on error (not an SKS URI)
1022  */
1023 char *
1024 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1025                                    const struct GNUNET_FS_Uri *uri)
1026 {
1027   /* FIXME */
1028   return NULL;
1029 }
1030
1031
1032 /**
1033  * Is this a keyword URI?
1034  *
1035  * @param uri the uri
1036  * @return GNUNET_YES if this is a KSK uri
1037  */
1038 int 
1039 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1040 {
1041 #if EXTRA_CHECKS
1042   unsigned int i;
1043
1044   if (uri->type == ksk)
1045     {
1046       for (i = uri->data.ksk.keywordCount - 1; i >= 0; i--)
1047         GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1048     }
1049 #endif
1050   return uri->type == ksk;
1051 }
1052
1053
1054 /**
1055  * Is this a file (or directory) URI?
1056  *
1057  * @param uri the uri to check
1058  * @return GNUNET_YES if this is a CHK uri
1059  */
1060 int 
1061 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1062 {
1063   return uri->type == chk;
1064 }
1065
1066
1067 /**
1068  * What is the size of the file that this URI
1069  * refers to?
1070  *
1071  * @param uri the CHK URI to inspect
1072  * @return size of the file as specified in the CHK URI
1073  */
1074 uint64_t 
1075 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri *uri)
1076 {
1077   switch (uri->type)
1078     {
1079     case chk:
1080       return GNUNET_ntohll (uri->data.chk.file_length);
1081     case loc:
1082       return GNUNET_ntohll (uri->data.loc.fi.file_length);
1083     default:
1084       GNUNET_assert (0);
1085     }
1086   return 0;                     /* unreachable */
1087 }
1088
1089
1090 /**
1091  * Is this a location URI?
1092  *
1093  * @param uri the uri to check
1094  * @return GNUNET_YES if this is a LOC uri
1095  */
1096 int 
1097 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1098 {
1099   return uri->type == loc;
1100 }
1101
1102
1103 /**
1104  * Function called on each value in the meta data.
1105  * Adds it to the URI.
1106  *
1107  * @param cls URI to update
1108  * @param type type of the meta data
1109  * @param data value of the meta data
1110  * @return GNUNET_OK (always)
1111  */
1112 static int
1113 gather_uri_data (void *cls,
1114                  EXTRACTOR_KeywordType type, 
1115                  const char *data)
1116 {
1117   struct GNUNET_FS_Uri *uri = cls;
1118   char *nkword;
1119   int j;
1120   
1121   for (j = uri->data.ksk.keywordCount - 1; j >= 0; j--)
1122     if (0 == strcmp (&uri->data.ksk.keywords[j][1], data))
1123       return GNUNET_OK;
1124   nkword = GNUNET_malloc (strlen (data) + 2);
1125   strcpy (nkword, " ");         /* not mandatory */
1126   strcat (nkword, data);
1127   uri->data.ksk.keywords[uri->data.ksk.keywordCount++] = nkword;
1128   return GNUNET_OK;
1129 }
1130
1131
1132 /**
1133  * Construct a keyword-URI from meta-data (take all entries
1134  * in the meta-data and construct one large keyword URI
1135  * that lists all keywords that can be found in the meta-data).
1136  * @deprecated
1137  */
1138 struct GNUNET_FS_Uri *
1139 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1140 {
1141   struct GNUNET_FS_Uri *ret;
1142
1143   if (md == NULL)
1144     return NULL;
1145   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1146   ret->type = ksk;
1147   ret->data.ksk.keywordCount = 0;
1148   ret->data.ksk.keywords = NULL;
1149   ret->data.ksk.keywords
1150     = GNUNET_malloc (sizeof (char *) *
1151                      GNUNET_CONTAINER_meta_data_get_contents (md, NULL, NULL));
1152   GNUNET_CONTAINER_meta_data_get_contents (md, &gather_uri_data, ret);
1153   return ret;
1154
1155 }
1156
1157
1158 /**
1159  * In URI-encoding, does the given character
1160  * need to be encoded using %-encoding?
1161  */
1162 static int
1163 needs_percent (char c)
1164 {
1165   return (!((isalnum (c)) ||
1166             (c == '-') || (c == '_') || (c == '.') || (c == '~')));
1167 }
1168
1169
1170 /**
1171  * Convert a KSK URI to a string.
1172  *
1173  * @param uri the URI to convert
1174  * @return NULL on error (i.e. keywordCount == 0)
1175  */
1176 static char *
1177 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1178 {
1179   char ** keywords; 
1180   unsigned int keywordCount;
1181   size_t n;
1182   char *ret;
1183   unsigned int i;
1184   unsigned int j;
1185   unsigned int wpos;
1186   size_t slen;
1187   const char *keyword;
1188
1189   if (uri->type != ksk)
1190     return NULL;
1191   keywords = uri->data.ksk.keywords;
1192   keywordCount = uri->data.ksk.keywordCount;
1193   n =
1194     keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1195     strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1196   for (i = 0; i < keywordCount; i++)
1197     {
1198       keyword = keywords[i];
1199       slen = strlen (keyword);
1200       n += slen;
1201       for (j = 0; j < slen; j++)
1202         {
1203           if ((j == 0) && (keyword[j] == ' '))
1204             {
1205               n--;
1206               continue;         /* skip leading space */
1207             }
1208           if (needs_percent (keyword[j]))
1209             n += 2;             /* will use %-encoding */
1210         }
1211     }
1212   ret = GNUNET_malloc (n);
1213   strcpy (ret, GNUNET_FS_URI_PREFIX);
1214   strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1215   wpos = strlen (ret);
1216   for (i = 0; i < keywordCount; i++)
1217     {
1218       keyword = keywords[i];
1219       slen = strlen (keyword);
1220       for (j = 0; j < slen; j++)
1221         {
1222           if ((j == 0) && (keyword[j] == ' '))
1223             continue;           /* skip leading space */
1224           if (needs_percent (keyword[j]))
1225             {
1226               sprintf (&ret[wpos], "%%%02X", keyword[j]);
1227               wpos += 3;
1228             }
1229           else
1230             {
1231               ret[wpos++] = keyword[j];
1232             }
1233         }
1234       if (i != keywordCount - 1)
1235         ret[wpos++] = '+';
1236     }
1237   return ret;
1238 }
1239
1240
1241 /**
1242  * Convert SKS URI to a string.
1243  *
1244  * @param uri sks uri to convert
1245  * @return NULL on error
1246  */
1247 static char *
1248 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1249 {
1250   const GNUNET_HashCode * namespace;
1251   const char *identifier;
1252   char *ret;
1253   struct GNUNET_CRYPTO_HashAsciiEncoded ns;
1254   
1255   if (uri->type != sks)
1256     return NULL;
1257   namespace = &uri->data.sks.namespace;
1258   identifier = uri->data.sks.identifier;
1259   GNUNET_CRYPTO_hash_to_enc (namespace, &ns);
1260   GNUNET_asprintf (&ret,
1261                    "%s%s%s/%s",
1262                    GNUNET_FS_URI_PREFIX, 
1263                    GNUNET_FS_URI_SKS_INFIX,
1264                    (const char *) &ns, identifier);
1265   return ret;
1266 }
1267
1268
1269 /**
1270  * Convert a CHK URI to a string.
1271  *
1272  * @param uri chk uri to convert
1273  * @return NULL on error
1274  */
1275 static char *
1276 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1277 {
1278   const struct FileIdentifier * fi;
1279   char *ret;
1280   struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1281   struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1282
1283   if (uri->type != chk)
1284     return NULL;
1285   fi = &uri->data.chk;
1286   GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1287   GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1288
1289   GNUNET_asprintf (&ret,
1290                    "%s%s%s.%s.%llu",
1291                    GNUNET_FS_URI_PREFIX,
1292                    GNUNET_FS_URI_CHK_INFIX,
1293                    (const char *) &keyhash, 
1294                    (const char *) &queryhash,
1295                    GNUNET_ntohll (fi->file_length));
1296   return ret;
1297 }
1298
1299 /**
1300  * Convert binary data to a string.
1301  *
1302  * @return converted data
1303  */
1304 static char *
1305 bin2enc (const void *data, size_t size)
1306 {
1307   /**
1308    * 64 characters for encoding, 6 bits per character
1309    */
1310   static char *tbl =
1311     "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1312   
1313   size_t len;
1314   size_t pos;
1315   unsigned int bits;
1316   unsigned int hbits;
1317   char *ret;
1318
1319   GNUNET_assert (strlen (tbl) == 64);
1320   len = size * 8 / 6;
1321   if (((size * 8) % 6) != 0)
1322     len++;
1323   ret = GNUNET_malloc (len + 1);
1324   ret[len] = '\0';
1325   len = 0;
1326   bits = 0;
1327   hbits = 0;
1328   for (pos = 0; pos < size; pos++)
1329     {
1330       bits |= ((((const unsigned char *) data)[pos]) << hbits);
1331       hbits += 8;
1332       while (hbits >= 6)
1333         {
1334           ret[len++] = tbl[bits & 63];
1335           bits >>= 6;
1336           hbits -= 6;
1337         }
1338     }
1339   if (hbits > 0)
1340     ret[len++] = tbl[bits & 63];
1341   return ret;
1342 }
1343
1344
1345 /**
1346  * Convert a LOC URI to a string.
1347  *
1348  * @param uri loc uri to convert
1349  * @return NULL on error
1350  */
1351 static char *
1352 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1353 {
1354   char *ret;
1355   struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1356   struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1357   char *peerId;
1358   char *peerSig;
1359
1360   GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1361   GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1362   peerId = bin2enc (&uri->data.loc.peer,
1363                     sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
1364   peerSig = bin2enc (&uri->data.loc.contentSignature, 
1365                      sizeof (struct GNUNET_CRYPTO_RsaSignature));
1366   GNUNET_asprintf (&ret,
1367                    "%s%s%s.%s.%llu.%s.%s.%llu",
1368                    GNUNET_FS_URI_PREFIX,
1369                    GNUNET_FS_URI_LOC_INFIX,
1370                    (const char *) &keyhash,
1371                    (const char *) &queryhash,
1372                    (unsigned long long) GNUNET_ntohll (uri->data.loc.fi.file_length),
1373                    peerId,
1374                    peerSig,
1375                    (unsigned long long) uri->data.loc.expirationTime.value);
1376   GNUNET_free (peerSig);
1377   GNUNET_free (peerId);
1378   return ret;
1379 }
1380
1381
1382 /**
1383  * Convert a URI to a UTF-8 String.
1384  *
1385  * @param uri uri to convert to a string
1386  * @return the UTF-8 string
1387  */
1388 char *
1389 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1390 {
1391   if (uri == NULL)
1392     {
1393       GNUNET_break (0);
1394       return NULL;
1395     }
1396   switch (uri->type)
1397     {
1398     case ksk:
1399       return uri_ksk_to_string (uri);
1400     case sks:
1401       return uri_sks_to_string (uri);
1402     case chk:
1403       return uri_chk_to_string (uri);
1404     case loc:
1405       return uri_loc_to_string (uri);
1406     default:
1407       GNUNET_break (0);
1408       return NULL;
1409     }
1410 }
1411
1412
1413 #if 0
1414
1415 /**
1416  * Construct a location URI.
1417  *
1418  * @param baseURI content offered by the sender
1419  * @param sender identity of the peer with the content
1420  * @param expiration_time how long will the content be offered?
1421  * @param proto transport protocol to reach the peer
1422  * @param sas sender address size (for HELLO)
1423  * @param address sas bytes of address information
1424  * @param signer function to call for obtaining
1425  *        RSA signatures for "sender".
1426  * @return the location URI
1427  */
1428 struct GNUNET_ECRS_URI *
1429 GNUNET_ECRS_location_to_uri (const struct GNUNET_ECRS_URI *baseUri,
1430                              const GNUNET_RSA_PublicKey * sender,
1431                              GNUNET_Int32Time expirationTime,
1432                              GNUNET_ECRS_SignFunction signer,
1433                              void *signer_cls)
1434 {
1435   struct GNUNET_ECRS_URI *uri;
1436
1437   if (baseUri->type != chk)
1438     return NULL;
1439
1440   uri = GNUNET_malloc (sizeof (struct GNUNET_ECRS_URI));
1441   uri->type = loc;
1442   uri->data.loc.fi = baseUri->data.fi;
1443   uri->data.loc.peer = *sender;
1444   uri->data.loc.expirationTime = expirationTime;
1445   signer (signer_cls,
1446           sizeof (GNUNET_EC_FileIdentifier) +
1447           sizeof (GNUNET_PeerIdentity) +
1448           sizeof (GNUNET_Int32Time),
1449           &uri->data.loc.fi, &uri->data.loc.contentSignature);
1450   return uri;
1451 }
1452
1453 #endif
1454
1455 /* end of uri.c */