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_CRYPTO_EccPublicKey),
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 >= 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_EccPublicKey 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;
446 * Convert a character back to the binary value
447 * that it represents (given base64-encoding).
449 * @param a character to convert
450 * @return offset in the "tbl" array
453 c2v (unsigned char a)
455 if ((a >= '0') && (a <= '9'))
457 if ((a >= 'A') && (a <= 'Z'))
458 return (a - 'A' + 10);
459 if ((a >= 'a') && (a <= 'z'))
460 return (a - 'a' + 36);
470 * Convert string back to binary data.
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,
479 enc2bin (const char *input, void *data, size_t size)
487 if (((size * 8) % 6) != 0)
489 if (strlen (input) < len)
490 return -1; /* error! */
494 for (pos = 0; pos < size; pos++)
498 bits |= (c2v (input[len++]) << hbits);
501 (((unsigned char *) data)[pos]) = (unsigned char) bits;
509 GNUNET_NETWORK_STRUCT_BEGIN
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
515 struct LocUriAssembly
518 * What is being signed (rest of this struct).
520 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
523 * Expiration time of the offer.
525 struct GNUNET_TIME_AbsoluteNBO exptime;
528 * File being offered.
530 struct FileIdentifier fi;
533 * Peer offering the file.
535 struct GNUNET_CRYPTO_EccPublicKey peer;
538 GNUNET_NETWORK_STRUCT_END
541 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
545 * Also verifies validity of the location URI.
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
551 static struct GNUNET_FS_Uri *
552 uri_loc_parse (const char *s, char **emsg)
554 struct GNUNET_FS_Uri *uri;
555 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
556 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
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;
567 GNUNET_assert (s != NULL);
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] != '.'))
576 *emsg = GNUNET_strdup (_("SKS URI malformed"));
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';
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)) ||
588 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
591 *emsg = GNUNET_strdup (_("SKS URI malformed"));
594 ass.fi.file_length = GNUNET_htonll (flen);
596 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
597 while ((s[npos] != '\0') && (s[npos] != '.'))
601 *emsg = GNUNET_strdup (_("SKS URI malformed"));
606 enc2bin (&s[npos], &ass.peer,
607 sizeof (struct GNUNET_CRYPTO_EccPublicKey));
611 GNUNET_strdup (_("SKS URI malformed (could not decode public key)"));
615 if (s[npos++] != '.')
617 *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
620 ret = enc2bin (&s[npos], &sig, sizeof (struct GNUNET_CRYPTO_EccSignature));
623 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
627 if (s[npos++] != '.')
629 *emsg = GNUNET_strdup (_("SKS URI malformed"));
632 if (1 != SSCANF (&s[npos], "%llu", &exptime))
636 ("SKS URI malformed (could not parse expiration time)"));
639 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
640 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
641 et.abs_value = exptime;
642 ass.exptime = GNUNET_TIME_absolute_hton (et);
644 GNUNET_CRYPTO_ecc_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
645 &ass.purpose, &sig, &ass.peer))
648 GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
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;
665 * Convert a UTF-8 String to a URI.
667 * @param uri string to parse
668 * @param emsg where to store the parser error message (if any)
669 * @return NULL on error
671 struct GNUNET_FS_Uri *
672 GNUNET_FS_uri_parse (const char *uri, char **emsg)
674 struct GNUNET_FS_Uri *ret;
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))))
686 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
696 * @param uri uri to free
699 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
703 GNUNET_assert (uri != NULL);
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);
711 case GNUNET_FS_URI_SKS:
712 GNUNET_free (uri->data.sks.identifier);
714 case GNUNET_FS_URI_LOC:
724 * How many keywords are ANDed in this keyword URI?
726 * @param uri ksk uri to get the number of keywords from
727 * @return 0 if this is not a keyword URI
730 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
732 if (uri->type != GNUNET_FS_URI_KSK)
734 return uri->data.ksk.keywordCount;
739 * Iterate over all keywords in this keyword URI.
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
748 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
749 GNUNET_FS_KeywordIterator iterator,
755 if (uri->type != GNUNET_FS_URI_KSK)
757 if (iterator == NULL)
758 return uri->data.ksk.keywordCount;
759 for (i = 0; i < uri->data.ksk.keywordCount; i++)
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] == '+'))
772 * Add the given keyword to the set of keywords represented by the URI.
773 * Does nothing if the keyword is already present.
775 * @param uri ksk uri to modify
776 * @param keyword keyword to add
777 * @param is_mandatory is this keyword mandatory?
780 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri, const char *keyword,
787 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
788 for (i = 0; i < uri->data.ksk.keywordCount; i++)
790 old = uri->data.ksk.keywords[i];
791 if (0 == strcmp (&old[1], keyword))
794 GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword);
795 GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
800 * Remove the given keyword from the set of keywords represented by the URI.
801 * Does nothing if the keyword is not present.
803 * @param uri ksk uri to modify
804 * @param keyword keyword to add
807 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
813 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
814 for (i = 0; i < uri->data.ksk.keywordCount; i++)
816 old = uri->data.ksk.keywords[i];
817 if (0 == strcmp (&old[1], keyword))
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);
831 * Obtain the identity of the peer offering the data
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
838 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
839 struct GNUNET_PeerIdentity *peer)
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),
851 * Obtain the expiration of the LOC URI.
853 * @param uri location URI to get the expiration from
854 * @return expiration time of the URI
856 struct GNUNET_TIME_Absolute
857 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
859 GNUNET_assert (uri->type == GNUNET_FS_URI_LOC);
860 return uri->data.loc.expirationTime;
866 * Obtain the URI of the content itself.
868 * @param uri location URI to get the content URI from
869 * @return NULL if argument is not a location URI
871 struct GNUNET_FS_Uri *
872 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
874 struct GNUNET_FS_Uri *ret;
876 if (uri->type != GNUNET_FS_URI_LOC)
878 ret = GNUNET_new (struct GNUNET_FS_Uri);
879 ret->type = GNUNET_FS_URI_CHK;
880 ret->data.chk = uri->data.loc.fi;
886 * Construct a location URI (this peer will be used for the location).
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
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)
898 struct GNUNET_FS_Uri *uri;
899 struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
900 struct GNUNET_CRYPTO_EccPublicKey my_public_key;
902 struct LocUriAssembly ass;
904 if (baseUri->type != GNUNET_FS_URI_CHK)
907 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
910 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
911 _("Lacking key configuration settings.\n"));
914 if (NULL == (my_private_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile)))
916 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
917 _("Could not access hostkey file `%s'.\n"), keyfile);
918 GNUNET_free (keyfile);
921 GNUNET_free (keyfile);
922 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
923 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
924 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
925 ass.exptime = GNUNET_TIME_absolute_hton (expiration_time);
926 ass.fi = baseUri->data.chk;
927 ass.peer = my_public_key;
928 uri = GNUNET_new (struct GNUNET_FS_Uri);
929 uri->type = GNUNET_FS_URI_LOC;
930 uri->data.loc.fi = baseUri->data.chk;
931 uri->data.loc.expirationTime = expiration_time;
932 uri->data.loc.peer = my_public_key;
933 GNUNET_assert (GNUNET_OK ==
934 GNUNET_CRYPTO_ecc_sign (my_private_key, &ass.purpose,
935 &uri->data.loc.contentSignature));
936 GNUNET_CRYPTO_ecc_key_free (my_private_key);
942 * Create an SKS URI from a namespace ID and an identifier.
944 * @param ns namespace ID
945 * @param id identifier
946 * @return an FS URI for the given namespace and identifier
948 struct GNUNET_FS_Uri *
949 GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EccPublicKey *ns,
952 struct GNUNET_FS_Uri *ns_uri;
954 ns_uri = GNUNET_new (struct GNUNET_FS_Uri);
955 ns_uri->type = GNUNET_FS_URI_SKS;
956 ns_uri->data.sks.ns = *ns;
957 ns_uri->data.sks.identifier = GNUNET_strdup (id);
963 * Merge the sets of keywords from two KSK URIs.
964 * (useful for merging the canonicalized keywords with
965 * the original keywords for sharing).
967 * @param u1 first uri
968 * @param u2 second uri
969 * @return merged URI, NULL on error
971 struct GNUNET_FS_Uri *
972 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
973 const struct GNUNET_FS_Uri *u2)
975 struct GNUNET_FS_Uri *ret;
983 if ((u1 == NULL) && (u2 == NULL))
986 return GNUNET_FS_uri_dup (u2);
988 return GNUNET_FS_uri_dup (u1);
989 if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK))
994 kc = u1->data.ksk.keywordCount;
995 kl = GNUNET_malloc ((kc + u2->data.ksk.keywordCount) * sizeof (char *));
996 for (i = 0; i < u1->data.ksk.keywordCount; i++)
997 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
998 for (i = 0; i < u2->data.ksk.keywordCount; i++)
1000 kp = u2->data.ksk.keywords[i];
1002 for (j = 0; j < u1->data.ksk.keywordCount; j++)
1003 if (0 == strcmp (kp + 1, kl[j] + 1))
1011 kl[kc++] = GNUNET_strdup (kp);
1013 ret = GNUNET_new (struct GNUNET_FS_Uri);
1014 ret->type = GNUNET_FS_URI_KSK;
1015 ret->data.ksk.keywordCount = kc;
1016 ret->data.ksk.keywords = kl;
1024 * @param uri the URI to duplicate
1025 * @return copy of the URI
1027 struct GNUNET_FS_Uri *
1028 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1030 struct GNUNET_FS_Uri *ret;
1035 ret = GNUNET_new (struct GNUNET_FS_Uri);
1036 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
1039 case GNUNET_FS_URI_KSK:
1040 if (ret->data.ksk.keywordCount >=
1041 GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
1047 if (ret->data.ksk.keywordCount > 0)
1049 ret->data.ksk.keywords =
1050 GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1051 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1052 ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]);
1055 ret->data.ksk.keywords = NULL; /* just to be sure */
1057 case GNUNET_FS_URI_SKS:
1058 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1060 case GNUNET_FS_URI_LOC:
1070 * Create an FS URI from a single user-supplied string of keywords.
1071 * The string is broken up at spaces into individual keywords.
1072 * Keywords that start with "+" are mandatory. Double-quotes can
1073 * be used to prevent breaking up strings at spaces (and also
1074 * to specify non-mandatory keywords starting with "+").
1076 * Keywords must contain a balanced number of double quotes and
1077 * double quotes can not be used in the actual keywords (for
1078 * example, the string '""foo bar""' will be turned into two
1079 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1081 * @param keywords the keyword string
1082 * @param emsg where to store an error message
1083 * @return an FS URI for the given keywords, NULL
1084 * if keywords is not legal (i.e. empty).
1086 struct GNUNET_FS_Uri *
1087 GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg)
1090 unsigned int num_Words;
1093 struct GNUNET_FS_Uri *uri;
1097 if (keywords == NULL)
1099 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1103 searchString = GNUNET_strdup (keywords);
1108 while ('\0' != *pos)
1110 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1114 else if (0 == inWord)
1120 saw_quote = (saw_quote + 1) % 2;
1125 GNUNET_free (searchString);
1126 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1131 GNUNET_free (searchString);
1132 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1135 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1139 while ('\0' != *pos)
1141 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1146 else if (0 == inWord)
1148 keywordarr[num_Words] = pos;
1153 saw_quote = (saw_quote + 1) % 2;
1157 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1158 (const char **) keywordarr);
1159 GNUNET_free (keywordarr);
1160 GNUNET_free (searchString);
1166 * Create an FS URI from a user-supplied command line of keywords.
1167 * Arguments should start with "+" to indicate mandatory
1170 * @param argc number of keywords
1171 * @param argv keywords (double quotes are not required for
1172 * keywords containing spaces; however, double
1173 * quotes are required for keywords starting with
1174 * "+"); there is no mechanism for having double
1175 * quotes in the actual keywords (if the user
1176 * did specifically specify double quotes, the
1177 * caller should convert each double quote
1178 * into two single quotes).
1179 * @return an FS URI for the given keywords, NULL
1180 * if keywords is not legal (i.e. empty).
1182 struct GNUNET_FS_Uri *
1183 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv)
1186 struct GNUNET_FS_Uri *uri;
1187 const char *keyword;
1195 /* allow URI to be given as one and only keyword and
1196 * handle accordingly */
1198 if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) &&
1200 strncmp (argv[0], GNUNET_FS_URI_PREFIX, strlen (GNUNET_FS_URI_PREFIX)))
1201 && (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg))))
1203 GNUNET_free_non_null (emsg);
1204 uri = GNUNET_new (struct GNUNET_FS_Uri);
1205 uri->type = GNUNET_FS_URI_KSK;
1206 uri->data.ksk.keywordCount = argc;
1207 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1208 for (i = 0; i < argc; i++)
1211 if (keyword[0] == '+')
1212 val = GNUNET_strdup (keyword);
1214 GNUNET_asprintf (&val, " %s", keyword);
1225 uri->data.ksk.keywords[i] = val;
1232 * Test if two URIs are equal.
1234 * @param u1 one of the URIs
1235 * @param u2 the other URI
1236 * @return GNUNET_YES if the URIs are equal
1239 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1240 const struct GNUNET_FS_Uri *u2)
1246 GNUNET_assert (u1 != NULL);
1247 GNUNET_assert (u2 != NULL);
1248 if (u1->type != u2->type)
1252 case GNUNET_FS_URI_CHK:
1254 memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier)))
1257 case GNUNET_FS_URI_SKS:
1259 memcmp (&u1->data.sks.ns, &u2->data.sks.ns,
1260 sizeof (struct GNUNET_CRYPTO_EccPublicKey))) &&
1261 (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
1265 case GNUNET_FS_URI_KSK:
1266 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1268 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1271 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1273 if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1279 if (ret == GNUNET_NO)
1283 case GNUNET_FS_URI_LOC:
1285 (&u1->data.loc, &u2->data.loc,
1286 sizeof (struct FileIdentifier) +
1287 sizeof (struct GNUNET_CRYPTO_EccPublicKey) +
1288 sizeof (struct GNUNET_TIME_Absolute) + sizeof (unsigned short) +
1289 sizeof (unsigned short)) != 0)
1299 * Is this a namespace URI?
1301 * @param uri the uri to check
1302 * @return GNUNET_YES if this is an SKS uri
1305 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1307 return uri->type == GNUNET_FS_URI_SKS;
1312 * Get the ID of a namespace from the given
1315 * @param uri the uri to get the namespace ID from
1316 * @param pseudonym where to store the ID of the namespace
1317 * @return GNUNET_OK on success
1320 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1321 struct GNUNET_CRYPTO_EccPublicKey *pseudonym)
1323 if (!GNUNET_FS_uri_test_sks (uri))
1326 return GNUNET_SYSERR;
1328 *pseudonym = uri->data.sks.ns;
1334 * Get the content identifier of an SKS URI.
1336 * @param uri the sks uri
1337 * @return NULL on error (not a valid SKS URI)
1340 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1342 if (!GNUNET_FS_uri_test_sks (uri))
1347 return GNUNET_strdup (uri->data.sks.identifier);
1352 * Convert namespace URI to a human readable format
1353 * (using the namespace description, if available).
1355 * @param cfg configuration to use
1356 * @param uri SKS uri to convert
1357 * @return NULL on error (not an SKS URI)
1360 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1361 const struct GNUNET_FS_Uri *uri)
1367 if (uri->type != GNUNET_FS_URI_SKS)
1369 (void) GNUNET_FS_pseudonym_get_info (cfg, &uri->data.sks.ns,
1370 NULL, NULL, &name, NULL);
1371 unique_name = GNUNET_FS_pseudonym_name_uniquify (cfg, &uri->data.sks.ns, name, NULL);
1373 GNUNET_asprintf (&ret, "%s: %s", unique_name, uri->data.sks.identifier);
1374 GNUNET_free (unique_name);
1380 * Is this a keyword URI?
1382 * @param uri the uri
1383 * @return GNUNET_YES if this is a KSK uri
1386 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1391 if (uri->type == GNUNET_FS_URI_KSK)
1393 for (i=0;i < uri->data.ksk.keywordCount; i++)
1394 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1397 return uri->type == GNUNET_FS_URI_KSK;
1402 * Is this a file (or directory) URI?
1404 * @param uri the uri to check
1405 * @return GNUNET_YES if this is a CHK uri
1408 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1410 return uri->type == GNUNET_FS_URI_CHK;
1415 * What is the size of the file that this URI
1418 * @param uri the CHK URI to inspect
1419 * @return size of the file as specified in the CHK URI
1422 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri * uri)
1426 case GNUNET_FS_URI_CHK:
1427 return GNUNET_ntohll (uri->data.chk.file_length);
1428 case GNUNET_FS_URI_LOC:
1429 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1433 return 0; /* unreachable */
1438 * Is this a location URI?
1440 * @param uri the uri to check
1441 * @return GNUNET_YES if this is a LOC uri
1444 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1446 return uri->type == GNUNET_FS_URI_LOC;
1451 * Add a keyword as non-mandatory (with ' '-prefix) to the
1452 * given keyword list at offset 'index'. The array is
1453 * guaranteed to be long enough.
1455 * @param s keyword to add
1456 * @param array array to add the keyword to
1457 * @param index offset where to add the keyword
1460 insert_non_mandatory_keyword (const char *s, char **array, int index)
1463 GNUNET_asprintf (&nkword, " %s", /* space to mark as 'non mandatory' */ s);
1464 array[index] = nkword;
1469 * Test if the given keyword 's' is already present in the
1470 * given array, ignoring the '+'-mandatory prefix in the array.
1472 * @param s keyword to test
1473 * @param array keywords to test against, with ' ' or '+' prefix to ignore
1474 * @param array_length length of the array
1475 * @return GNUNET_YES if the keyword exists, GNUNET_NO if not
1478 find_duplicate (const char *s, const char **array, int array_length)
1482 for (j = array_length - 1; j >= 0; j--)
1483 if (0 == strcmp (&array[j][1], s))
1493 normalize_metadata (enum EXTRACTOR_MetaFormat format, const char *data,
1496 uint8_t *free_str = NULL;
1497 uint8_t *str_to_normalize = (uint8_t *) data;
1498 uint8_t *normalized;
1500 if (str_to_normalize == NULL)
1502 /* Don't trust libextractor */
1503 if (format == EXTRACTOR_METAFORMAT_UTF8)
1505 free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len);
1506 if (free_str == NULL)
1509 format = EXTRACTOR_METAFORMAT_C_STRING;
1511 if (format == EXTRACTOR_METAFORMAT_C_STRING)
1513 free_str = u8_strconv_from_encoding (data, locale_charset (), iconveh_escape_sequence);
1514 if (free_str == NULL)
1518 normalized = u8_tolower (str_to_normalize, strlen ((char *) str_to_normalize), NULL, UNINORM_NFD, NULL, &r_len);
1519 /* free_str is allocated by libunistring internally, use free() */
1520 if (free_str != NULL)
1522 if (normalized != NULL)
1524 /* u8_tolower allocates a non-NULL-terminated string! */
1525 free_str = GNUNET_malloc (r_len + 1);
1526 memcpy (free_str, normalized, r_len);
1527 free_str[r_len] = '\0';
1529 normalized = free_str;
1531 return (char *) normalized;
1535 * Counts the number of UTF-8 characters (not bytes) in the string,
1536 * returns that count.
1539 u8_strcount (const uint8_t *s)
1543 GNUNET_assert (s != NULL);
1546 for (count = 0; s != NULL; count++)
1547 s = u8_next (&c, s);
1553 * Break the filename up by matching [], () and {} pairs to make
1554 * keywords. In case of nesting parentheses only the inner pair counts.
1555 * You can't escape parentheses to scan something like "[blah\{foo]" to
1556 * make a "blah{foo" keyword, this function is only a heuristic!
1558 * @param s string to break down.
1559 * @param array array to fill with enclosed tokens. If NULL, then tokens
1561 * @param index index at which to start filling the array (entries prior
1562 * to it are used to check for duplicates). ignored if array == NULL.
1563 * @return number of tokens counted (including duplicates), or number of
1564 * tokens extracted (excluding duplicates). 0 if there are no
1565 * matching parens in the string (when counting), or when all tokens
1566 * were duplicates (when extracting).
1569 get_keywords_from_parens (const char *s, char **array, int index)
1579 ss = GNUNET_strdup (s);
1580 open_paren = ss - 1;
1581 while (NULL != (open_paren = strpbrk (open_paren + 1, "[{(")))
1585 close_paren = strpbrk (open_paren + 1, "]})");
1586 if (NULL == close_paren)
1588 switch (open_paren[0])
1591 if (']' == close_paren[0])
1595 if ('}' == close_paren[0])
1599 if (')' == close_paren[0])
1605 if (match && (close_paren - open_paren > 1))
1607 tmp = close_paren[0];
1608 close_paren[0] = '\0';
1609 /* Keywords must be at least 3 characters long */
1610 if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2)
1612 close_paren[0] = tmp;
1618 if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
1619 (const char **) array, index + count))
1621 insert_non_mandatory_keyword ((const char *) &open_paren[1], array,
1625 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1626 &open_paren[1], close_paren - &open_paren[1]);
1627 if (normalized != NULL)
1629 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1630 (const char **) array, index + count))
1632 insert_non_mandatory_keyword ((const char *) normalized, array,
1636 GNUNET_free (normalized);
1641 close_paren[0] = tmp;
1650 * Where to break up keywords
1652 #define TOKENS "_. /-!?#&+@\"\'\\;:,()[]{}$<>|"
1655 * Break the filename up by TOKENS to make
1658 * @param s string to break down.
1659 * @param array array to fill with tokens. If NULL, then tokens are only
1661 * @param index index at which to start filling the array (entries prior
1662 * to it are used to check for duplicates). ignored if array == NULL.
1663 * @return number of tokens (>1) counted (including duplicates), or number of
1664 * tokens extracted (excluding duplicates). 0 if there are no
1665 * separators in the string (when counting), or when all tokens were
1666 * duplicates (when extracting).
1669 get_keywords_from_tokens (const char *s, char **array, int index)
1675 ss = GNUNET_strdup (s);
1676 for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS))
1678 /* Keywords must be at least 3 characters long */
1679 if (u8_strcount ((const uint8_t *) p) <= 2)
1684 if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps))
1686 insert_non_mandatory_keyword (p, array,
1690 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1692 if (normalized != NULL)
1694 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1695 (const char **) array, index + seps))
1697 insert_non_mandatory_keyword ((const char *) normalized, array,
1701 GNUNET_free (normalized);
1713 * Function called on each value in the meta data.
1714 * Adds it to the URI.
1716 * @param cls URI to update
1717 * @param plugin_name name of the plugin that produced this value;
1718 * special values can be used (i.e. '<zlib>' for zlib being
1719 * used in the main libextractor library and yielding
1721 * @param type libextractor-type describing the meta data
1722 * @param format basic format information about data
1723 * @param data_mime_type mime-type of data (not of the original file);
1724 * can be NULL (if mime-type is not known)
1725 * @param data actual meta-data found
1726 * @param data_len number of bytes in data
1727 * @return 0 (always)
1730 gather_uri_data (void *cls, const char *plugin_name,
1731 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
1732 const char *data_mime_type, const char *data, size_t data_len)
1734 struct GNUNET_FS_Uri *uri = cls;
1735 char *normalized_data;
1737 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1738 (format != EXTRACTOR_METAFORMAT_C_STRING))
1740 /* Keywords must be at least 3 characters long
1741 * If given non-utf8 string it will, most likely, find it to be invalid,
1742 * and will return the length of its valid part, skipping the keyword.
1743 * If it does - fix the extractor, not this check!
1745 if (u8_strcount ((const uint8_t *) data) <= 2)
1749 normalized_data = normalize_metadata (format, data, data_len);
1750 if (!find_duplicate (data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
1752 insert_non_mandatory_keyword (data,
1753 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1754 uri->data.ksk.keywordCount++;
1756 if (normalized_data != NULL)
1758 if (!find_duplicate (normalized_data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
1760 insert_non_mandatory_keyword (normalized_data,
1761 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1762 uri->data.ksk.keywordCount++;
1764 GNUNET_free (normalized_data);
1771 * Construct a keyword-URI from meta-data (take all entries
1772 * in the meta-data and construct one large keyword URI
1773 * that lists all keywords that can be found in the meta-data).
1775 * @param md metadata to use
1776 * @return NULL on error, otherwise a KSK URI
1778 struct GNUNET_FS_Uri *
1779 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
1782 struct GNUNET_FS_Uri *ret;
1784 char *full_name = NULL;
1787 int tok_keywords = 0;
1788 int paren_keywords = 0;
1792 ret = GNUNET_new (struct GNUNET_FS_Uri);
1793 ret->type = GNUNET_FS_URI_KSK;
1794 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1797 full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
1798 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, -1);
1799 if (NULL != full_name)
1801 filename = full_name;
1802 while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR)))
1804 tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
1805 paren_keywords = get_keywords_from_parens (filename, NULL, 0);
1807 /* x2 because there might be a normalized variant of every keyword */
1808 ret->data.ksk.keywords = GNUNET_malloc (sizeof (char *) * (ent
1809 + tok_keywords + paren_keywords) * 2);
1810 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1812 if (tok_keywords > 0)
1813 ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
1814 ret->data.ksk.keywords,
1815 ret->data.ksk.keywordCount);
1816 if (paren_keywords > 0)
1817 ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
1818 ret->data.ksk.keywords,
1819 ret->data.ksk.keywordCount);
1821 GNUNET_free_non_null (full_name);
1827 * In URI-encoding, does the given character
1828 * need to be encoded using %-encoding?
1831 needs_percent (char c)
1834 ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') ||
1835 (c == '.') || (c == '~')));
1840 * Convert a KSK URI to a string.
1842 * @param uri the URI to convert
1843 * @return NULL on error (i.e. keywordCount == 0)
1846 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1849 unsigned int keywordCount;
1856 const char *keyword;
1858 if (uri->type != GNUNET_FS_URI_KSK)
1860 keywords = uri->data.ksk.keywords;
1861 keywordCount = uri->data.ksk.keywordCount;
1862 n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1863 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1864 for (i = 0; i < keywordCount; i++)
1866 keyword = keywords[i];
1867 slen = strlen (keyword);
1869 for (j = 0; j < slen; j++)
1871 if ((j == 0) && (keyword[j] == ' '))
1874 continue; /* skip leading space */
1876 if (needs_percent (keyword[j]))
1877 n += 2; /* will use %-encoding */
1880 ret = GNUNET_malloc (n);
1881 strcpy (ret, GNUNET_FS_URI_PREFIX);
1882 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1883 wpos = strlen (ret);
1884 for (i = 0; i < keywordCount; i++)
1886 keyword = keywords[i];
1887 slen = strlen (keyword);
1888 for (j = 0; j < slen; j++)
1890 if ((j == 0) && (keyword[j] == ' '))
1891 continue; /* skip leading space */
1892 if (needs_percent (keyword[j]))
1894 sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]);
1899 ret[wpos++] = keyword[j];
1902 if (i != keywordCount - 1)
1910 * Convert SKS URI to a string.
1912 * @param uri sks uri to convert
1913 * @return NULL on error
1916 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1921 if (GNUNET_FS_URI_SKS != uri->type)
1923 ret = GNUNET_STRINGS_data_to_string (&uri->data.sks.ns,
1924 sizeof (struct GNUNET_CRYPTO_EccPublicKey),
1927 GNUNET_assert (NULL != ret);
1929 GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
1930 GNUNET_FS_URI_SKS_INFIX, buf,
1931 uri->data.sks.identifier);
1937 * Convert a CHK URI to a string.
1939 * @param uri chk uri to convert
1940 * @return NULL on error
1943 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1945 const struct FileIdentifier *fi;
1947 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1948 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1950 if (uri->type != GNUNET_FS_URI_CHK)
1952 fi = &uri->data.chk;
1953 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1954 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1956 GNUNET_asprintf (&ret, "%s%s%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1957 GNUNET_FS_URI_CHK_INFIX, (const char *) &keyhash,
1958 (const char *) &queryhash, GNUNET_ntohll (fi->file_length));
1963 * Convert binary data to a string.
1965 * @param data binary data to convert
1966 * @param size number of bytes in data
1967 * @return converted data
1970 bin2enc (const void *data, size_t size)
1973 * 64 characters for encoding, 6 bits per character
1976 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1984 GNUNET_assert (strlen (tbl) == 64);
1986 if (((size * 8) % 6) != 0)
1988 ret = GNUNET_malloc (len + 1);
1993 for (pos = 0; pos < size; pos++)
1995 bits |= ((((const unsigned char *) data)[pos]) << hbits);
1999 ret[len++] = tbl[bits & 63];
2005 ret[len] = tbl[bits & 63];
2011 * Convert a LOC URI to a string.
2013 * @param uri loc uri to convert
2014 * @return NULL on error
2017 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
2020 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
2021 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
2025 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
2026 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
2028 bin2enc (&uri->data.loc.peer,
2029 sizeof (struct GNUNET_CRYPTO_EccPublicKey));
2031 bin2enc (&uri->data.loc.contentSignature,
2032 sizeof (struct GNUNET_CRYPTO_EccSignature));
2033 GNUNET_asprintf (&ret, "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
2034 GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
2035 (const char *) &queryhash,
2036 (unsigned long long) GNUNET_ntohll (uri->data.loc.
2037 fi.file_length), peerId,
2039 (unsigned long long) uri->data.loc.expirationTime.abs_value);
2040 GNUNET_free (peerSig);
2041 GNUNET_free (peerId);
2047 * Convert a URI to a UTF-8 String.
2049 * @param uri uri to convert to a string
2050 * @return the UTF-8 string
2053 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
2062 case GNUNET_FS_URI_KSK:
2063 return uri_ksk_to_string (uri);
2064 case GNUNET_FS_URI_SKS:
2065 return uri_sks_to_string (uri);
2066 case GNUNET_FS_URI_CHK:
2067 return uri_chk_to_string (uri);
2068 case GNUNET_FS_URI_LOC:
2069 return uri_loc_to_string (uri);
2076 /* end of fs_uri.c */