2 This file is part of GNUnet.
3 (C) 2003--2013 Christian Grothoff (and other contributing authors)
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.
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.
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.
23 * @brief Parses and produces uri strings.
24 * @author Igor Wronsky, Christian Grothoff
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").
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:
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).
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.
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.
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.
77 * The encoding for hexadecimal values is defined in the hashing.c
78 * module in the gnunetutil library and discussed there.
82 #include "gnunet_fs_service.h"
83 #include "gnunet_signatures.h"
94 * Get a unique key from a URI. This is for putting URIs
95 * into HashMaps. The key may change between FS implementations.
97 * @param uri uri to convert to a unique key
98 * @param key where to store the unique key
101 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
102 struct GNUNET_HashCode *key)
106 case GNUNET_FS_URI_CHK:
107 *key = uri->data.chk.chk.query;
109 case GNUNET_FS_URI_SKS:
110 GNUNET_CRYPTO_hash (uri->data.sks.identifier,
111 strlen (uri->data.sks.identifier), key);
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);
118 case GNUNET_FS_URI_LOC:
119 GNUNET_CRYPTO_hash (&uri->data.loc.fi,
120 sizeof (struct FileIdentifier) +
121 sizeof (struct GNUNET_PeerIdentity),
125 memset (key, 0, sizeof (struct GNUNET_HashCode));
132 * Convert keyword URI to a human readable format
133 * (i.e. the search query that was used in the first place)
135 * @param uri ksk uri to convert to a string
136 * @return string with the keywords
139 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
146 unsigned int keywordCount;
148 if ((NULL == uri) || (GNUNET_FS_URI_KSK != uri->type))
153 keywords = uri->data.ksk.keywords;
154 keywordCount = uri->data.ksk.keywordCount;
155 n = keywordCount + 1;
156 for (i = 0; i < keywordCount; i++)
158 keyword = keywords[i];
159 n += strlen (keyword) - 1;
160 if (NULL != strstr (&keyword[1], " "))
162 if (keyword[0] == '+')
165 ret = GNUNET_malloc (n);
167 for (i = 0; i < keywordCount; i++)
169 keyword = keywords[i];
170 if (NULL != strstr (&keyword[1], " "))
173 if (keyword[0] == '+')
174 strcat (ret, keyword);
176 strcat (ret, &keyword[1]);
181 if (keyword[0] == '+')
182 strcat (ret, keyword);
184 strcat (ret, &keyword[1]);
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 '+'.
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)
203 percent_decode_keyword (const char *in, char **emsg)
211 out = GNUNET_strdup (in);
214 while (out[rpos] != '\0')
216 if (out[rpos] == '%')
218 if (1 != SSCANF (&out[rpos + 1], "%2X", &hx))
221 *emsg = GNUNET_strdup (_(/* xgettext:no-c-format */
222 "`%' must be followed by HEX number"));
227 continue; /* skip double quote */
228 out[wpos++] = (char) hx;
232 out[wpos++] = out[rpos++];
238 ret = GNUNET_strdup (out);
242 /* need to prefix with space */
243 ret = GNUNET_malloc (strlen (out) + 2);
251 #define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
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
260 static struct GNUNET_FS_Uri *
261 uri_ksk_parse (const char *s, char **emsg)
263 struct GNUNET_FS_Uri *ret;
273 GNUNET_assert (NULL != 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] == '+'))
281 GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
286 for (i = pos; i < slen; i++)
288 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
290 saw_quote = (saw_quote + 1) % 2;
294 if ((s[i] == '+') && (saw_quote == 0))
299 *emsg = GNUNET_strdup (_("`++' not allowed in KSK URI"));
306 *emsg = GNUNET_strdup (_("Quotes not balanced in KSK URI"));
310 dup = GNUNET_strdup (s);
311 keywords = GNUNET_malloc (max * sizeof (char *));
312 for (i = slen - 1; i >= (int) pos; i--)
314 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
316 saw_quote = (saw_quote + 1) % 2;
320 if ((dup[i] == '+') && (saw_quote == 0))
322 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
323 if (NULL == keywords[max])
328 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
329 if (NULL == keywords[max])
331 GNUNET_assert (max == 0);
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;
339 for (i = 0; i < max; i++)
340 GNUNET_free_non_null (keywords[i]);
341 GNUNET_free (keywords);
347 #define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
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
356 static struct GNUNET_FS_Uri *
357 uri_sks_parse (const char *s, char **emsg)
359 struct GNUNET_FS_Uri *ret;
360 struct GNUNET_CRYPTO_EcdsaPublicKey ns;
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) ||
371 GNUNET_STRINGS_string_to_data (&s[pos],
376 *emsg = GNUNET_strdup (_("Malformed SKS URI"));
377 return NULL; /* malformed */
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);
387 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
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
397 static struct GNUNET_FS_Uri *
398 uri_chk_parse (const char *s, char **emsg)
400 struct GNUNET_FS_Uri *ret;
401 struct FileIdentifier fi;
403 unsigned long long flen;
405 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
406 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
410 GNUNET_assert (s != NULL);
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] != '.'))
419 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
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';
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)) ||
431 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
434 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
437 fi.file_length = GNUNET_htonll (flen);
438 ret = GNUNET_new (struct GNUNET_FS_Uri);
439 ret->type = GNUNET_FS_URI_CHK;
445 GNUNET_NETWORK_STRUCT_BEGIN
447 * Structure that defines how the contents of a location URI must be
448 * assembled in memory to create or verify the signature of a location
451 struct LocUriAssembly
454 * What is being signed (rest of this struct).
456 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
459 * Expiration time of the offer.
461 struct GNUNET_TIME_AbsoluteNBO exptime;
464 * File being offered.
466 struct FileIdentifier fi;
469 * Peer offering the file.
471 struct GNUNET_PeerIdentity peer;
474 GNUNET_NETWORK_STRUCT_END
477 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
479 #define SIGNATURE_ASCII_LENGTH 103
483 * Also verifies validity of the location URI.
485 * @param s an uri string
486 * @param emsg where to store the parser error message (if any)
487 * @return NULL on error, valid LOC URI otherwise
489 static struct GNUNET_FS_Uri *
490 uri_loc_parse (const char *s, char **emsg)
492 struct GNUNET_FS_Uri *uri;
493 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
494 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
497 unsigned long long exptime;
498 unsigned long long flen;
499 struct GNUNET_TIME_Absolute et;
500 struct GNUNET_CRYPTO_EddsaSignature sig;
501 struct LocUriAssembly ass;
505 pos = strlen (GNUNET_FS_URI_LOC_PREFIX);
506 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
507 (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX, pos)))
508 return NULL; /* not an SKS URI */
509 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
510 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
512 *emsg = GNUNET_strdup (_("SKS URI malformed"));
515 memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
516 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
517 memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
518 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
519 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
521 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &ass.fi.chk.key)) ||
522 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &ass.fi.chk.query)) ||
524 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
527 *emsg = GNUNET_strdup (_("LOC URI malformed"));
530 ass.fi.file_length = GNUNET_htonll (flen);
532 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
533 while ((s[npos] != '\0') && (s[npos] != '.'))
537 *emsg = GNUNET_strdup (_("LOC URI malformed"));
541 if ( (strlen (&s[npos]) <= GNUNET_CRYPTO_PKEY_ASCII_LENGTH + 1) ||
542 ('.' != s[npos+GNUNET_CRYPTO_PKEY_ASCII_LENGTH]) )
545 GNUNET_strdup (_("LOC URI malformed (could not decode public key)"));
548 GNUNET_CRYPTO_eddsa_public_key_from_string (&s[npos],
549 GNUNET_CRYPTO_PKEY_ASCII_LENGTH,
550 &ass.peer.public_key))
553 GNUNET_strdup (_("LOC URI malformed (could not decode public key)"));
556 npos += GNUNET_CRYPTO_PKEY_ASCII_LENGTH;
557 if (s[npos++] != '.')
559 *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
562 if ( (strlen (&s[npos]) <= SIGNATURE_ASCII_LENGTH + 1) ||
563 ('.' != s[npos + SIGNATURE_ASCII_LENGTH]) )
565 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
569 GNUNET_STRINGS_string_to_data (&s[npos],
570 SIGNATURE_ASCII_LENGTH,
572 sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
574 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
577 npos += SIGNATURE_ASCII_LENGTH;
578 if (s[npos++] != '.')
580 *emsg = GNUNET_strdup (_("SKS URI malformed"));
583 if (1 != SSCANF (&s[npos], "%llu", &exptime))
586 GNUNET_strdup (_("SKS URI malformed (could not parse expiration time)"));
589 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
590 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
591 et.abs_value_us = exptime * 1000LL * 1000LL;
592 ass.exptime = GNUNET_TIME_absolute_hton (et);
594 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
595 &ass.purpose, &sig, &ass.peer.public_key))
598 GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
601 uri = GNUNET_new (struct GNUNET_FS_Uri);
602 uri->type = GNUNET_FS_URI_LOC;
603 uri->data.loc.fi = ass.fi;
604 uri->data.loc.peer = ass.peer;
605 uri->data.loc.expirationTime = et;
606 uri->data.loc.contentSignature = sig;
615 * Convert a UTF-8 String to a URI.
617 * @param uri string to parse
618 * @param emsg where to store the parser error message (if any)
619 * @return NULL on error
621 struct GNUNET_FS_Uri *
622 GNUNET_FS_uri_parse (const char *uri, char **emsg)
624 struct GNUNET_FS_Uri *ret;
630 if ((NULL != (ret = uri_chk_parse (uri, emsg))) ||
631 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
632 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
633 (NULL != (ret = uri_loc_parse (uri, emsg))))
636 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
646 * @param uri uri to free
649 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
655 case GNUNET_FS_URI_KSK:
656 for (i = 0; i < uri->data.ksk.keywordCount; i++)
657 GNUNET_free (uri->data.ksk.keywords[i]);
658 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0);
660 case GNUNET_FS_URI_SKS:
661 GNUNET_free (uri->data.sks.identifier);
663 case GNUNET_FS_URI_LOC:
674 * How many keywords are ANDed in this keyword URI?
676 * @param uri ksk uri to get the number of keywords from
677 * @return 0 if this is not a keyword URI
680 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
682 if (uri->type != GNUNET_FS_URI_KSK)
684 return uri->data.ksk.keywordCount;
689 * Iterate over all keywords in this keyword URI.
691 * @param uri ksk uri to get the keywords from
692 * @param iterator function to call on each keyword
693 * @param iterator_cls closure for iterator
694 * @return -1 if this is not a keyword URI, otherwise number of
695 * keywords iterated over until iterator aborted
698 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
699 GNUNET_FS_KeywordIterator iterator,
705 if (uri->type != GNUNET_FS_URI_KSK)
707 if (NULL == iterator)
708 return uri->data.ksk.keywordCount;
709 for (i = 0; i < uri->data.ksk.keywordCount; i++)
711 keyword = uri->data.ksk.keywords[i];
712 /* first character of keyword indicates
713 * if it is mandatory or not */
714 if (GNUNET_OK != iterator (iterator_cls, &keyword[1], keyword[0] == '+'))
722 * Add the given keyword to the set of keywords represented by the URI.
723 * Does nothing if the keyword is already present.
725 * @param uri ksk uri to modify
726 * @param keyword keyword to add
727 * @param is_mandatory is this keyword mandatory?
730 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
738 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
739 for (i = 0; i < uri->data.ksk.keywordCount; i++)
741 old = uri->data.ksk.keywords[i];
742 if (0 == strcmp (&old[1], keyword))
745 GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword);
746 GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
751 * Remove the given keyword from the set of keywords represented by the URI.
752 * Does nothing if the keyword is not present.
754 * @param uri ksk uri to modify
755 * @param keyword keyword to add
758 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
764 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
765 for (i = 0; i < uri->data.ksk.keywordCount; i++)
767 old = uri->data.ksk.keywords[i];
768 if (0 == strcmp (&old[1], keyword))
770 uri->data.ksk.keywords[i] =
771 uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1];
772 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
773 uri->data.ksk.keywordCount - 1);
782 * Obtain the identity of the peer offering the data
784 * @param uri the location URI to inspect
785 * @param peer where to store the identify of the peer (presumably) offering the content
786 * @return #GNUNET_SYSERR if this is not a location URI, otherwise #GNUNET_OK
789 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
790 struct GNUNET_PeerIdentity *peer)
792 if (uri->type != GNUNET_FS_URI_LOC)
793 return GNUNET_SYSERR;
794 *peer = uri->data.loc.peer;
800 * Obtain the expiration of the LOC URI.
802 * @param uri location URI to get the expiration from
803 * @return expiration time of the URI
805 struct GNUNET_TIME_Absolute
806 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
808 GNUNET_assert (uri->type == GNUNET_FS_URI_LOC);
809 return uri->data.loc.expirationTime;
814 * Obtain the URI of the content itself.
816 * @param uri location URI to get the content URI from
817 * @return NULL if argument is not a location URI
819 struct GNUNET_FS_Uri *
820 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
822 struct GNUNET_FS_Uri *ret;
824 if (uri->type != GNUNET_FS_URI_LOC)
826 ret = GNUNET_new (struct GNUNET_FS_Uri);
827 ret->type = GNUNET_FS_URI_CHK;
828 ret->data.chk = uri->data.loc.fi;
834 * Construct a location URI (this peer will be used for the location).
836 * @param baseUri content offered by the sender
837 * @param cfg configuration information (used to find our hostkey)
838 * @param expiration_time how long will the content be offered?
839 * @return the location URI, NULL on error
841 struct GNUNET_FS_Uri *
842 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
843 const struct GNUNET_CONFIGURATION_Handle *cfg,
844 struct GNUNET_TIME_Absolute expiration_time)
846 struct GNUNET_FS_Uri *uri;
847 struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
848 struct GNUNET_CRYPTO_EddsaPublicKey my_public_key;
850 struct LocUriAssembly ass;
851 struct GNUNET_TIME_Absolute et;
853 if (baseUri->type != GNUNET_FS_URI_CHK)
856 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
859 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
860 _("Lacking key configuration settings.\n"));
863 if (NULL == (my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_file (keyfile)))
865 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
866 _("Could not access hostkey file `%s'.\n"), keyfile);
867 GNUNET_free (keyfile);
870 GNUNET_free (keyfile);
871 /* we round expiration time to full seconds for SKS URIs */
872 et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL;
873 GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_public_key);
874 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
875 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
876 ass.exptime = GNUNET_TIME_absolute_hton (et);
877 ass.fi = baseUri->data.chk;
878 ass.peer.public_key = my_public_key;
879 uri = GNUNET_new (struct GNUNET_FS_Uri);
880 uri->type = GNUNET_FS_URI_LOC;
881 uri->data.loc.fi = baseUri->data.chk;
882 uri->data.loc.expirationTime = et;
883 uri->data.loc.peer.public_key = my_public_key;
884 GNUNET_assert (GNUNET_OK ==
885 GNUNET_CRYPTO_eddsa_sign (my_private_key, &ass.purpose,
886 &uri->data.loc.contentSignature));
887 GNUNET_free (my_private_key);
893 * Create an SKS URI from a namespace ID and an identifier.
895 * @param ns namespace ID
896 * @param id identifier
897 * @return an FS URI for the given namespace and identifier
899 struct GNUNET_FS_Uri *
900 GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
903 struct GNUNET_FS_Uri *ns_uri;
905 ns_uri = GNUNET_new (struct GNUNET_FS_Uri);
906 ns_uri->type = GNUNET_FS_URI_SKS;
907 ns_uri->data.sks.ns = *ns;
908 ns_uri->data.sks.identifier = GNUNET_strdup (id);
914 * Merge the sets of keywords from two KSK URIs.
915 * (useful for merging the canonicalized keywords with
916 * the original keywords for sharing).
918 * @param u1 first uri
919 * @param u2 second uri
920 * @return merged URI, NULL on error
922 struct GNUNET_FS_Uri *
923 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
924 const struct GNUNET_FS_Uri *u2)
926 struct GNUNET_FS_Uri *ret;
934 if ((u1 == NULL) && (u2 == NULL))
937 return GNUNET_FS_uri_dup (u2);
939 return GNUNET_FS_uri_dup (u1);
940 if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK))
945 kc = u1->data.ksk.keywordCount;
946 kl = GNUNET_malloc ((kc + u2->data.ksk.keywordCount) * sizeof (char *));
947 for (i = 0; i < u1->data.ksk.keywordCount; i++)
948 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
949 for (i = 0; i < u2->data.ksk.keywordCount; i++)
951 kp = u2->data.ksk.keywords[i];
953 for (j = 0; j < u1->data.ksk.keywordCount; j++)
954 if (0 == strcmp (kp + 1, kl[j] + 1))
962 kl[kc++] = GNUNET_strdup (kp);
964 ret = GNUNET_new (struct GNUNET_FS_Uri);
965 ret->type = GNUNET_FS_URI_KSK;
966 ret->data.ksk.keywordCount = kc;
967 ret->data.ksk.keywords = kl;
975 * @param uri the URI to duplicate
976 * @return copy of the URI
978 struct GNUNET_FS_Uri *
979 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
981 struct GNUNET_FS_Uri *ret;
986 ret = GNUNET_new (struct GNUNET_FS_Uri);
987 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
990 case GNUNET_FS_URI_KSK:
991 if (ret->data.ksk.keywordCount >=
992 GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
998 if (ret->data.ksk.keywordCount > 0)
1000 ret->data.ksk.keywords =
1001 GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1002 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1003 ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]);
1006 ret->data.ksk.keywords = NULL; /* just to be sure */
1008 case GNUNET_FS_URI_SKS:
1009 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1011 case GNUNET_FS_URI_LOC:
1021 * Create an FS URI from a single user-supplied string of keywords.
1022 * The string is broken up at spaces into individual keywords.
1023 * Keywords that start with "+" are mandatory. Double-quotes can
1024 * be used to prevent breaking up strings at spaces (and also
1025 * to specify non-mandatory keywords starting with "+").
1027 * Keywords must contain a balanced number of double quotes and
1028 * double quotes can not be used in the actual keywords (for
1029 * example, the string '""foo bar""' will be turned into two
1030 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1032 * @param keywords the keyword string
1033 * @param emsg where to store an error message
1034 * @return an FS URI for the given keywords, NULL
1035 * if keywords is not legal (i.e. empty).
1037 struct GNUNET_FS_Uri *
1038 GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg)
1041 unsigned int num_Words;
1044 struct GNUNET_FS_Uri *uri;
1048 if (keywords == NULL)
1050 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1054 searchString = GNUNET_strdup (keywords);
1059 while ('\0' != *pos)
1061 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1065 else if (0 == inWord)
1071 saw_quote = (saw_quote + 1) % 2;
1076 GNUNET_free (searchString);
1077 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1082 GNUNET_free (searchString);
1083 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1086 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1090 while ('\0' != *pos)
1092 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1097 else if (0 == inWord)
1099 keywordarr[num_Words] = pos;
1104 saw_quote = (saw_quote + 1) % 2;
1108 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1109 (const char **) keywordarr);
1110 GNUNET_free (keywordarr);
1111 GNUNET_free (searchString);
1117 * Create an FS URI from a user-supplied command line of keywords.
1118 * Arguments should start with "+" to indicate mandatory
1121 * @param argc number of keywords
1122 * @param argv keywords (double quotes are not required for
1123 * keywords containing spaces; however, double
1124 * quotes are required for keywords starting with
1125 * "+"); there is no mechanism for having double
1126 * quotes in the actual keywords (if the user
1127 * did specifically specify double quotes, the
1128 * caller should convert each double quote
1129 * into two single quotes).
1130 * @return an FS URI for the given keywords, NULL
1131 * if keywords is not legal (i.e. empty).
1133 struct GNUNET_FS_Uri *
1134 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv)
1137 struct GNUNET_FS_Uri *uri;
1138 const char *keyword;
1146 /* allow URI to be given as one and only keyword and
1147 * handle accordingly */
1149 if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) &&
1151 strncmp (argv[0], GNUNET_FS_URI_PREFIX, strlen (GNUNET_FS_URI_PREFIX)))
1152 && (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg))))
1154 GNUNET_free_non_null (emsg);
1155 uri = GNUNET_new (struct GNUNET_FS_Uri);
1156 uri->type = GNUNET_FS_URI_KSK;
1157 uri->data.ksk.keywordCount = argc;
1158 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1159 for (i = 0; i < argc; i++)
1162 if (keyword[0] == '+')
1163 val = GNUNET_strdup (keyword);
1165 GNUNET_asprintf (&val, " %s", keyword);
1176 uri->data.ksk.keywords[i] = val;
1183 * Test if two URIs are equal.
1185 * @param u1 one of the URIs
1186 * @param u2 the other URI
1187 * @return GNUNET_YES if the URIs are equal
1190 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1191 const struct GNUNET_FS_Uri *u2)
1197 GNUNET_assert (u1 != NULL);
1198 GNUNET_assert (u2 != NULL);
1199 if (u1->type != u2->type)
1203 case GNUNET_FS_URI_CHK:
1205 memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier)))
1208 case GNUNET_FS_URI_SKS:
1210 memcmp (&u1->data.sks.ns, &u2->data.sks.ns,
1211 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) &&
1212 (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
1216 case GNUNET_FS_URI_KSK:
1217 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1219 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1222 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1224 if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1230 if (ret == GNUNET_NO)
1234 case GNUNET_FS_URI_LOC:
1236 (&u1->data.loc, &u2->data.loc,
1237 sizeof (struct FileIdentifier) +
1238 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1239 sizeof (struct GNUNET_TIME_Absolute) + sizeof (unsigned short) +
1240 sizeof (unsigned short)) != 0)
1250 * Is this a namespace URI?
1252 * @param uri the uri to check
1253 * @return GNUNET_YES if this is an SKS uri
1256 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1258 return uri->type == GNUNET_FS_URI_SKS;
1263 * Get the ID of a namespace from the given
1266 * @param uri the uri to get the namespace ID from
1267 * @param pseudonym where to store the ID of the namespace
1268 * @return GNUNET_OK on success
1271 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1272 struct GNUNET_CRYPTO_EcdsaPublicKey *pseudonym)
1274 if (!GNUNET_FS_uri_test_sks (uri))
1277 return GNUNET_SYSERR;
1279 *pseudonym = uri->data.sks.ns;
1285 * Get the content identifier of an SKS URI.
1287 * @param uri the sks uri
1288 * @return NULL on error (not a valid SKS URI)
1291 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1293 if (!GNUNET_FS_uri_test_sks (uri))
1298 return GNUNET_strdup (uri->data.sks.identifier);
1303 * Is this a keyword URI?
1305 * @param uri the uri
1306 * @return #GNUNET_YES if this is a KSK uri
1309 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1314 if (uri->type == GNUNET_FS_URI_KSK)
1316 for (i=0;i < uri->data.ksk.keywordCount; i++)
1317 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1320 return uri->type == GNUNET_FS_URI_KSK;
1325 * Is this a file (or directory) URI?
1327 * @param uri the uri to check
1328 * @return GNUNET_YES if this is a CHK uri
1331 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1333 return uri->type == GNUNET_FS_URI_CHK;
1338 * What is the size of the file that this URI
1341 * @param uri the CHK URI to inspect
1342 * @return size of the file as specified in the CHK URI
1345 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri * uri)
1349 case GNUNET_FS_URI_CHK:
1350 return GNUNET_ntohll (uri->data.chk.file_length);
1351 case GNUNET_FS_URI_LOC:
1352 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1356 return 0; /* unreachable */
1361 * Is this a location URI?
1363 * @param uri the uri to check
1364 * @return GNUNET_YES if this is a LOC uri
1367 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1369 return uri->type == GNUNET_FS_URI_LOC;
1374 * Add a keyword as non-mandatory (with ' '-prefix) to the
1375 * given keyword list at offset 'index'. The array is
1376 * guaranteed to be long enough.
1378 * @param s keyword to add
1379 * @param array array to add the keyword to
1380 * @param index offset where to add the keyword
1383 insert_non_mandatory_keyword (const char *s, char **array, int index)
1387 GNUNET_asprintf (&nkword,
1388 " %s", /* space to mark as 'non mandatory' */
1390 array[index] = nkword;
1395 * Test if the given keyword @a s is already present in the
1396 * given array, ignoring the '+'-mandatory prefix in the array.
1398 * @param s keyword to test
1399 * @param array keywords to test against, with ' ' or '+' prefix to ignore
1400 * @param array_length length of the @a array
1401 * @return #GNUNET_YES if the keyword exists, #GNUNET_NO if not
1404 find_duplicate (const char *s, const char **array, int array_length)
1408 for (j = array_length - 1; j >= 0; j--)
1409 if (0 == strcmp (&array[j][1], s))
1419 normalize_metadata (enum EXTRACTOR_MetaFormat format,
1423 uint8_t *free_str = NULL;
1424 uint8_t *str_to_normalize = (uint8_t *) data;
1425 uint8_t *normalized;
1427 if (str_to_normalize == NULL)
1429 /* Don't trust libextractor */
1430 if (format == EXTRACTOR_METAFORMAT_UTF8)
1432 free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len);
1433 if (free_str == NULL)
1436 format = EXTRACTOR_METAFORMAT_C_STRING;
1438 if (format == EXTRACTOR_METAFORMAT_C_STRING)
1440 free_str = u8_strconv_from_encoding (data, locale_charset (), iconveh_escape_sequence);
1441 if (free_str == NULL)
1445 normalized = u8_tolower (str_to_normalize, strlen ((char *) str_to_normalize), NULL, UNINORM_NFD, NULL, &r_len);
1446 /* free_str is allocated by libunistring internally, use free() */
1447 if (free_str != NULL)
1449 if (normalized != NULL)
1451 /* u8_tolower allocates a non-NULL-terminated string! */
1452 free_str = GNUNET_malloc (r_len + 1);
1453 memcpy (free_str, normalized, r_len);
1454 free_str[r_len] = '\0';
1456 normalized = free_str;
1458 return (char *) normalized;
1462 * Counts the number of UTF-8 characters (not bytes) in the string,
1463 * returns that count.
1466 u8_strcount (const uint8_t *s)
1470 GNUNET_assert (s != NULL);
1473 for (count = 0; s != NULL; count++)
1474 s = u8_next (&c, s);
1480 * Break the filename up by matching [], () and {} pairs to make
1481 * keywords. In case of nesting parentheses only the inner pair counts.
1482 * You can't escape parentheses to scan something like "[blah\{foo]" to
1483 * make a "blah{foo" keyword, this function is only a heuristic!
1485 * @param s string to break down.
1486 * @param array array to fill with enclosed tokens. If NULL, then tokens
1488 * @param index index at which to start filling the array (entries prior
1489 * to it are used to check for duplicates). ignored if array == NULL.
1490 * @return number of tokens counted (including duplicates), or number of
1491 * tokens extracted (excluding duplicates). 0 if there are no
1492 * matching parens in the string (when counting), or when all tokens
1493 * were duplicates (when extracting).
1496 get_keywords_from_parens (const char *s, char **array, int index)
1506 ss = GNUNET_strdup (s);
1507 open_paren = ss - 1;
1508 while (NULL != (open_paren = strpbrk (open_paren + 1, "[{(")))
1512 close_paren = strpbrk (open_paren + 1, "]})");
1513 if (NULL == close_paren)
1515 switch (open_paren[0])
1518 if (']' == close_paren[0])
1522 if ('}' == close_paren[0])
1526 if (')' == close_paren[0])
1532 if (match && (close_paren - open_paren > 1))
1534 tmp = close_paren[0];
1535 close_paren[0] = '\0';
1536 /* Keywords must be at least 3 characters long */
1537 if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2)
1539 close_paren[0] = tmp;
1545 if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
1546 (const char **) array, index + count))
1548 insert_non_mandatory_keyword ((const char *) &open_paren[1], array,
1552 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1553 &open_paren[1], close_paren - &open_paren[1]);
1554 if (normalized != NULL)
1556 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1557 (const char **) array, index + count))
1559 insert_non_mandatory_keyword ((const char *) normalized, array,
1563 GNUNET_free (normalized);
1568 close_paren[0] = tmp;
1577 * Where to break up keywords
1579 #define TOKENS "_. /-!?#&+@\"\'\\;:,()[]{}$<>|"
1582 * Break the filename up by TOKENS to make
1585 * @param s string to break down.
1586 * @param array array to fill with tokens. If NULL, then tokens are only
1588 * @param index index at which to start filling the array (entries prior
1589 * to it are used to check for duplicates). ignored if array == NULL.
1590 * @return number of tokens (>1) counted (including duplicates), or number of
1591 * tokens extracted (excluding duplicates). 0 if there are no
1592 * separators in the string (when counting), or when all tokens were
1593 * duplicates (when extracting).
1596 get_keywords_from_tokens (const char *s, char **array, int index)
1602 ss = GNUNET_strdup (s);
1603 for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS))
1605 /* Keywords must be at least 3 characters long */
1606 if (u8_strcount ((const uint8_t *) p) <= 2)
1611 if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps))
1613 insert_non_mandatory_keyword (p, array,
1617 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1619 if (normalized != NULL)
1621 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1622 (const char **) array, index + seps))
1624 insert_non_mandatory_keyword ((const char *) normalized, array,
1628 GNUNET_free (normalized);
1641 * Function called on each value in the meta data.
1642 * Adds it to the URI.
1644 * @param cls URI to update
1645 * @param plugin_name name of the plugin that produced this value;
1646 * special values can be used (i.e. '<zlib>' for zlib being
1647 * used in the main libextractor library and yielding
1649 * @param type libextractor-type describing the meta data
1650 * @param format basic format information about data
1651 * @param data_mime_type mime-type of data (not of the original file);
1652 * can be NULL (if mime-type is not known)
1653 * @param data actual meta-data found
1654 * @param data_len number of bytes in @a data
1655 * @return 0 (always)
1658 gather_uri_data (void *cls, const char *plugin_name,
1659 enum EXTRACTOR_MetaType type,
1660 enum EXTRACTOR_MetaFormat format,
1661 const char *data_mime_type,
1665 struct GNUNET_FS_Uri *uri = cls;
1666 char *normalized_data;
1669 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1670 (format != EXTRACTOR_METAFORMAT_C_STRING))
1672 /* Keywords must be at least 3 characters long
1673 * If given non-utf8 string it will, most likely, find it to be invalid,
1674 * and will return the length of its valid part, skipping the keyword.
1675 * If it does - fix the extractor, not this check!
1677 if (u8_strcount ((const uint8_t *) data) <= 2)
1679 if ( (EXTRACTOR_METATYPE_MIMETYPE == type) &&
1680 (NULL != (sep = memchr (data, '/', data_len))) &&
1685 GNUNET_asprintf (&xtra,
1689 if (! find_duplicate (xtra,
1690 (const char **) uri->data.ksk.keywords,
1691 uri->data.ksk.keywordCount))
1693 insert_non_mandatory_keyword (xtra,
1694 uri->data.ksk.keywords,
1695 uri->data.ksk.keywordCount);
1696 uri->data.ksk.keywordCount++;
1701 normalized_data = normalize_metadata (format, data, data_len);
1702 if (! find_duplicate (data,
1703 (const char **) uri->data.ksk.keywords,
1704 uri->data.ksk.keywordCount))
1706 insert_non_mandatory_keyword (data,
1707 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1708 uri->data.ksk.keywordCount++;
1710 if (NULL != normalized_data)
1712 if (! find_duplicate (normalized_data,
1713 (const char **) uri->data.ksk.keywords,
1714 uri->data.ksk.keywordCount))
1716 insert_non_mandatory_keyword (normalized_data,
1717 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1718 uri->data.ksk.keywordCount++;
1720 GNUNET_free (normalized_data);
1727 * Construct a keyword-URI from meta-data (take all entries
1728 * in the meta-data and construct one large keyword URI
1729 * that lists all keywords that can be found in the meta-data).
1731 * @param md metadata to use
1732 * @return NULL on error, otherwise a KSK URI
1734 struct GNUNET_FS_Uri *
1735 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1737 struct GNUNET_FS_Uri *ret;
1739 char *full_name = NULL;
1742 int tok_keywords = 0;
1743 int paren_keywords = 0;
1747 ret = GNUNET_new (struct GNUNET_FS_Uri);
1748 ret->type = GNUNET_FS_URI_KSK;
1749 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1752 full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
1753 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, -1);
1754 if (NULL != full_name)
1756 filename = full_name;
1757 while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR)))
1759 tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
1760 paren_keywords = get_keywords_from_parens (filename, NULL, 0);
1762 /* x3 because there might be a normalized variant of every keyword,
1763 plus theoretically one more for mime... */
1764 ret->data.ksk.keywords = GNUNET_malloc
1765 (sizeof (char *) * (ent + tok_keywords + paren_keywords) * 3);
1766 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1768 if (tok_keywords > 0)
1769 ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
1770 ret->data.ksk.keywords,
1771 ret->data.ksk.keywordCount);
1772 if (paren_keywords > 0)
1773 ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
1774 ret->data.ksk.keywords,
1775 ret->data.ksk.keywordCount);
1777 GNUNET_free_non_null (full_name);
1783 * In URI-encoding, does the given character
1784 * need to be encoded using %-encoding?
1787 needs_percent (char c)
1790 ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') ||
1791 (c == '.') || (c == '~')));
1796 * Convert a KSK URI to a string.
1798 * @param uri the URI to convert
1799 * @return NULL on error (i.e. keywordCount == 0)
1802 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1805 unsigned int keywordCount;
1812 const char *keyword;
1814 if (uri->type != GNUNET_FS_URI_KSK)
1816 keywords = uri->data.ksk.keywords;
1817 keywordCount = uri->data.ksk.keywordCount;
1818 n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1819 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1820 for (i = 0; i < keywordCount; i++)
1822 keyword = keywords[i];
1823 slen = strlen (keyword);
1825 for (j = 0; j < slen; j++)
1827 if ((j == 0) && (keyword[j] == ' '))
1830 continue; /* skip leading space */
1832 if (needs_percent (keyword[j]))
1833 n += 2; /* will use %-encoding */
1836 ret = GNUNET_malloc (n);
1837 strcpy (ret, GNUNET_FS_URI_PREFIX);
1838 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1839 wpos = strlen (ret);
1840 for (i = 0; i < keywordCount; i++)
1842 keyword = keywords[i];
1843 slen = strlen (keyword);
1844 for (j = 0; j < slen; j++)
1846 if ((j == 0) && (keyword[j] == ' '))
1847 continue; /* skip leading space */
1848 if (needs_percent (keyword[j]))
1850 sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]);
1855 ret[wpos++] = keyword[j];
1858 if (i != keywordCount - 1)
1866 * Convert SKS URI to a string.
1868 * @param uri sks uri to convert
1869 * @return NULL on error
1872 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1877 if (GNUNET_FS_URI_SKS != uri->type)
1879 ret = GNUNET_STRINGS_data_to_string (&uri->data.sks.ns,
1880 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
1883 GNUNET_assert (NULL != ret);
1885 GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
1886 GNUNET_FS_URI_SKS_INFIX, buf,
1887 uri->data.sks.identifier);
1893 * Convert a CHK URI to a string.
1895 * @param uri chk uri to convert
1896 * @return NULL on error
1899 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1901 const struct FileIdentifier *fi;
1903 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1904 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1906 if (uri->type != GNUNET_FS_URI_CHK)
1908 fi = &uri->data.chk;
1909 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1910 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1912 GNUNET_asprintf (&ret, "%s%s%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1913 GNUNET_FS_URI_CHK_INFIX, (const char *) &keyhash,
1914 (const char *) &queryhash, GNUNET_ntohll (fi->file_length));
1920 * Convert a LOC URI to a string.
1922 * @param uri loc uri to convert
1923 * @return NULL on error
1926 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1929 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1930 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1932 char peer_sig[SIGNATURE_ASCII_LENGTH + 1];
1934 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1935 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1937 GNUNET_CRYPTO_eddsa_public_key_to_string (&uri->data.loc.peer.public_key);
1938 GNUNET_assert (NULL !=
1939 GNUNET_STRINGS_data_to_string (&uri->data.loc.contentSignature,
1940 sizeof (struct GNUNET_CRYPTO_EddsaSignature),
1942 sizeof (peer_sig)));
1943 GNUNET_asprintf (&ret,
1944 "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1945 GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
1946 (const char *) &queryhash,
1947 (unsigned long long) GNUNET_ntohll (uri->data.loc.
1951 (unsigned long long) uri->data.loc.expirationTime.abs_value_us / 1000000LL);
1952 GNUNET_free (peer_id);
1958 * Convert a URI to a UTF-8 String.
1960 * @param uri uri to convert to a string
1961 * @return the UTF-8 string
1964 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1973 case GNUNET_FS_URI_KSK:
1974 return uri_ksk_to_string (uri);
1975 case GNUNET_FS_URI_SKS:
1976 return uri_sks_to_string (uri);
1977 case GNUNET_FS_URI_CHK:
1978 return uri_chk_to_string (uri);
1979 case GNUNET_FS_URI_LOC:
1980 return uri_loc_to_string (uri);
1987 /* end of fs_uri.c */