2 This file is part of GNUnet.
3 (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 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 wherer to store the unique key
101 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri, struct GNUNET_HashCode * key)
106 *key = uri->data.chk.chk.query;
109 GNUNET_CRYPTO_hash (uri->data.sks.identifier,
110 strlen (uri->data.sks.identifier), key);
113 if (uri->data.ksk.keywordCount > 0)
114 GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
115 strlen (uri->data.ksk.keywords[0]), key);
118 GNUNET_CRYPTO_hash (&uri->data.loc.fi,
119 sizeof (struct FileIdentifier) +
120 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
124 memset (key, 0, sizeof (struct GNUNET_HashCode));
131 * Convert keyword URI to a human readable format
132 * (i.e. the search query that was used in the first place)
134 * @param uri ksk uri to convert to a string
135 * @return string with the keywords
138 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
145 unsigned int keywordCount;
147 if ((uri == NULL) || (uri->type != ksk))
152 keywords = uri->data.ksk.keywords;
153 keywordCount = uri->data.ksk.keywordCount;
154 n = keywordCount + 1;
155 for (i = 0; i < keywordCount; i++)
157 keyword = keywords[i];
158 n += strlen (keyword) - 1;
159 if (NULL != strstr (&keyword[1], " "))
161 if (keyword[0] == '+')
164 ret = GNUNET_malloc (n);
166 for (i = 0; i < keywordCount; i++)
168 keyword = keywords[i];
169 if (NULL != strstr (&keyword[1], " "))
172 if (keyword[0] == '+')
173 strcat (ret, keyword);
175 strcat (ret, &keyword[1]);
180 if (keyword[0] == '+')
181 strcat (ret, keyword);
183 strcat (ret, &keyword[1]);
192 * Given a keyword with %-encoding (and possibly quotes to protect
193 * spaces), return a copy of the keyword without %-encoding and
194 * without double-quotes (%22). Also, add a space at the beginning
195 * if there is not a '+'.
197 * @param in string with %-encoding
198 * @param emsg where to store the parser error message (if any)
199 * @return decodded string with leading space (or preserved plus)
202 percent_decode_keyword (const char *in, char **emsg)
210 out = GNUNET_strdup (in);
213 while (out[rpos] != '\0')
215 if (out[rpos] == '%')
217 if (1 != SSCANF (&out[rpos + 1], "%2X", &hx))
220 *emsg = GNUNET_strdup (_(/* xgettext:no-c-format */
221 "`%' must be followed by HEX number"));
226 continue; /* skip double quote */
227 out[wpos++] = (char) hx;
231 out[wpos++] = out[rpos++];
237 ret = GNUNET_strdup (out);
241 /* need to prefix with space */
242 ret = GNUNET_malloc (strlen (out) + 2);
250 #define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
255 * @param s an uri string
256 * @param emsg where to store the parser error message (if any)
257 * @return NULL on error, otherwise the KSK URI
259 static struct GNUNET_FS_Uri *
260 uri_ksk_parse (const char *s, char **emsg)
262 struct GNUNET_FS_Uri *ret;
272 GNUNET_assert (s != NULL);
274 pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
275 if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX, pos)))
276 return NULL; /* not KSK URI */
277 if ((s[slen - 1] == '+') || (s[pos] == '+'))
280 GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
285 for (i = pos; i < slen; i++)
287 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
289 saw_quote = (saw_quote + 1) % 2;
293 if ((s[i] == '+') && (saw_quote == 0))
298 *emsg = GNUNET_strdup (_("`++' not allowed in KSK URI"));
305 *emsg = GNUNET_strdup (_("Quotes not balanced in KSK URI"));
309 dup = GNUNET_strdup (s);
310 keywords = GNUNET_malloc (max * sizeof (char *));
311 for (i = slen - 1; i >= pos; i--)
313 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
315 saw_quote = (saw_quote + 1) % 2;
319 if ((dup[i] == '+') && (saw_quote == 0))
321 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
322 if (NULL == keywords[max])
327 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
328 if (NULL == keywords[max])
330 GNUNET_assert (max == 0);
332 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
334 ret->data.ksk.keywordCount = iret;
335 ret->data.ksk.keywords = keywords;
338 for (i = 0; i < max; i++)
339 GNUNET_free_non_null (keywords[i]);
340 GNUNET_free (keywords);
346 #define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
351 * @param s an uri string
352 * @param emsg where to store the parser error message (if any)
353 * @return NULL on error, SKS URI otherwise
355 static struct GNUNET_FS_Uri *
356 uri_sks_parse (const char *s, char **emsg)
358 struct GNUNET_FS_Uri *ret;
359 struct GNUNET_HashCode ns;
363 char enc[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
365 GNUNET_assert (s != NULL);
367 pos = strlen (GNUNET_FS_URI_SKS_PREFIX);
368 if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX, pos)))
369 return NULL; /* not an SKS URI */
370 if ((slen < pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) ||
371 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '/'))
373 *emsg = GNUNET_strdup (_("Malformed SKS URI"));
376 memcpy (enc, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
377 enc[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
378 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (enc, &ns))
380 *emsg = GNUNET_strdup (_("Malformed SKS URI"));
384 GNUNET_strdup (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)]);
385 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
387 ret->data.sks.ns = ns;
388 ret->data.sks.identifier = identifier;
392 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
398 * @param s an uri string
399 * @param emsg where to store the parser error message (if any)
400 * @return NULL on error, CHK URI otherwise
402 static struct GNUNET_FS_Uri *
403 uri_chk_parse (const char *s, char **emsg)
405 struct GNUNET_FS_Uri *ret;
406 struct FileIdentifier fi;
408 unsigned long long flen;
410 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
411 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
415 GNUNET_assert (s != NULL);
417 pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
418 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
419 (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX, pos)))
420 return NULL; /* not a CHK URI */
421 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
422 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
424 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
427 memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
428 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
429 memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
430 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
431 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
433 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &fi.chk.key)) ||
434 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &fi.chk.query)) ||
436 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
439 *emsg = GNUNET_strdup (_("Malformed CHK URI"));
442 fi.file_length = GNUNET_htonll (flen);
443 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
451 * Convert a character back to the binary value
452 * that it represents (given base64-encoding).
454 * @param a character to convert
455 * @return offset in the "tbl" array
458 c2v (unsigned char a)
460 if ((a >= '0') && (a <= '9'))
462 if ((a >= 'A') && (a <= 'Z'))
463 return (a - 'A' + 10);
464 if ((a >= 'a') && (a <= 'z'))
465 return (a - 'a' + 36);
475 * Convert string back to binary data.
477 * @param input '\\0'-terminated string
478 * @param data where to write binary data
479 * @param size how much data should be converted
480 * @return number of characters processed from input,
484 enc2bin (const char *input, void *data, size_t size)
492 if (((size * 8) % 6) != 0)
494 if (strlen (input) < len)
495 return -1; /* error! */
499 for (pos = 0; pos < size; pos++)
503 bits |= (c2v (input[len++]) << hbits);
506 (((unsigned char *) data)[pos]) = (unsigned char) bits;
515 * Structure that defines how the
516 * contents of a location URI must be
517 * assembled in memory to create or
518 * verify the signature of a location
521 struct LocUriAssembly
523 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
525 struct GNUNET_TIME_AbsoluteNBO exptime;
527 struct FileIdentifier fi;
529 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded peer;
534 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
538 * Also verifies validity of the location URI.
540 * @param s an uri string
541 * @param emsg where to store the parser error message (if any)
542 * @return NULL on error, valid LOC URI otherwise
544 static struct GNUNET_FS_Uri *
545 uri_loc_parse (const char *s, char **emsg)
547 struct GNUNET_FS_Uri *uri;
548 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
549 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
552 unsigned long long exptime;
553 unsigned long long flen;
554 struct GNUNET_TIME_Absolute et;
555 struct GNUNET_CRYPTO_RsaSignature sig;
556 struct LocUriAssembly ass;
560 GNUNET_assert (s != NULL);
562 pos = strlen (GNUNET_FS_URI_LOC_PREFIX);
563 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
564 (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX, pos)))
565 return NULL; /* not an SKS URI */
566 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
567 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
569 *emsg = GNUNET_strdup (_("SKS URI malformed"));
572 memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
573 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
574 memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
575 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
576 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
578 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &ass.fi.chk.key)) ||
579 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &ass.fi.chk.query)) ||
581 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
584 *emsg = GNUNET_strdup (_("SKS URI malformed"));
587 ass.fi.file_length = GNUNET_htonll (flen);
589 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
590 while ((s[npos] != '\0') && (s[npos] != '.'))
594 *emsg = GNUNET_strdup (_("SKS URI malformed"));
599 enc2bin (&s[npos], &ass.peer,
600 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
604 GNUNET_strdup (_("SKS URI malformed (could not decode public key)"));
608 if (s[npos++] != '.')
610 *emsg = GNUNET_strdup (_("SKS URI malformed (could not find signature)"));
613 ret = enc2bin (&s[npos], &sig, sizeof (struct GNUNET_CRYPTO_RsaSignature));
616 *emsg = GNUNET_strdup (_("SKS URI malformed (could not decode signature)"));
620 if (s[npos++] != '.')
622 *emsg = GNUNET_strdup (_("SKS URI malformed"));
625 if (1 != SSCANF (&s[npos], "%llu", &exptime))
629 ("SKS URI malformed (could not parse expiration time)"));
632 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
633 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
634 et.abs_value = exptime;
635 ass.exptime = GNUNET_TIME_absolute_hton (et);
637 GNUNET_CRYPTO_rsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
638 &ass.purpose, &sig, &ass.peer))
641 GNUNET_strdup (_("SKS URI malformed (signature failed validation)"));
644 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
646 uri->data.loc.fi = ass.fi;
647 uri->data.loc.peer = ass.peer;
648 uri->data.loc.expirationTime = et;
649 uri->data.loc.contentSignature = sig;
658 * Convert a UTF-8 String to a URI.
660 * @param uri string to parse
661 * @param emsg where to store the parser error message (if any)
662 * @return NULL on error
664 struct GNUNET_FS_Uri *
665 GNUNET_FS_uri_parse (const char *uri, char **emsg)
667 struct GNUNET_FS_Uri *ret;
673 if ((NULL != (ret = uri_chk_parse (uri, emsg))) ||
674 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
675 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
676 (NULL != (ret = uri_loc_parse (uri, emsg))))
679 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
689 * @param uri uri to free
692 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
696 GNUNET_assert (uri != NULL);
700 for (i = 0; i < uri->data.ksk.keywordCount; i++)
701 GNUNET_free (uri->data.ksk.keywords[i]);
702 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0);
705 GNUNET_free (uri->data.sks.identifier);
717 * How many keywords are ANDed in this keyword URI?
719 * @param uri ksk uri to get the number of keywords from
720 * @return 0 if this is not a keyword URI
723 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
725 if (uri->type != ksk)
727 return uri->data.ksk.keywordCount;
732 * Iterate over all keywords in this keyword URI.
734 * @param uri ksk uri to get the keywords from
735 * @param iterator function to call on each keyword
736 * @param iterator_cls closure for iterator
737 * @return -1 if this is not a keyword URI, otherwise number of
738 * keywords iterated over until iterator aborted
741 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
742 GNUNET_FS_KeywordIterator iterator,
748 if (uri->type != ksk)
750 if (iterator == NULL)
751 return uri->data.ksk.keywordCount;
752 for (i = 0; i < uri->data.ksk.keywordCount; i++)
754 keyword = uri->data.ksk.keywords[i];
755 /* first character of keyword indicates
756 * if it is mandatory or not */
757 if (GNUNET_OK != iterator (iterator_cls, &keyword[1], keyword[0] == '+'))
765 * Add the given keyword to the set of keywords represented by the URI.
766 * Does nothing if the keyword is already present.
768 * @param uri ksk uri to modify
769 * @param keyword keyword to add
770 * @param is_mandatory is this keyword mandatory?
773 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri, const char *keyword,
780 GNUNET_assert (uri->type == ksk);
781 for (i = 0; i < uri->data.ksk.keywordCount; i++)
783 old = uri->data.ksk.keywords[i];
784 if (0 == strcmp (&old[1], keyword))
787 GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword);
788 GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
793 * Remove the given keyword from the set of keywords represented by the URI.
794 * Does nothing if the keyword is not present.
796 * @param uri ksk uri to modify
797 * @param keyword keyword to add
800 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
806 GNUNET_assert (uri->type == ksk);
807 for (i = 0; i < uri->data.ksk.keywordCount; i++)
809 old = uri->data.ksk.keywords[i];
810 if (0 == strcmp (&old[1], keyword))
812 uri->data.ksk.keywords[i] =
813 uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1];
814 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
815 uri->data.ksk.keywordCount - 1);
824 * Obtain the identity of the peer offering the data
826 * @param uri the location URI to inspect
827 * @param peer where to store the identify of the peer (presumably) offering the content
828 * @return GNUNET_SYSERR if this is not a location URI, otherwise GNUNET_OK
831 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
832 struct GNUNET_PeerIdentity *peer)
834 if (uri->type != loc)
835 return GNUNET_SYSERR;
836 GNUNET_CRYPTO_hash (&uri->data.loc.peer,
837 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
844 * Obtain the expiration of the LOC URI.
846 * @param uri location URI to get the expiration from
847 * @return expiration time of the URI
849 struct GNUNET_TIME_Absolute
850 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
852 GNUNET_assert (uri->type == loc);
853 return uri->data.loc.expirationTime;
859 * Obtain the URI of the content itself.
861 * @param uri location URI to get the content URI from
862 * @return NULL if argument is not a location URI
864 struct GNUNET_FS_Uri *
865 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
867 struct GNUNET_FS_Uri *ret;
869 if (uri->type != loc)
871 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
873 ret->data.chk = uri->data.loc.fi;
879 * Construct a location URI (this peer will be used for the location).
881 * @param baseUri content offered by the sender
882 * @param cfg configuration information (used to find our hostkey)
883 * @param expiration_time how long will the content be offered?
884 * @return the location URI, NULL on error
886 struct GNUNET_FS_Uri *
887 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *baseUri,
888 const struct GNUNET_CONFIGURATION_Handle *cfg,
889 struct GNUNET_TIME_Absolute expiration_time)
891 struct GNUNET_FS_Uri *uri;
892 struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
893 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
895 struct LocUriAssembly ass;
897 if (baseUri->type != chk)
900 GNUNET_CONFIGURATION_get_value_filename (cfg, "GNUNETD", "HOSTKEY",
903 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
904 _("Lacking key configuration settings.\n"));
907 if (NULL == (my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile)))
909 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
910 _("Could not access hostkey file `%s'.\n"), keyfile);
911 GNUNET_free (keyfile);
914 GNUNET_free (keyfile);
915 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
916 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
917 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
918 ass.exptime = GNUNET_TIME_absolute_hton (expiration_time);
919 ass.fi = baseUri->data.chk;
920 ass.peer = my_public_key;
921 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
923 uri->data.loc.fi = baseUri->data.chk;
924 uri->data.loc.expirationTime = expiration_time;
925 uri->data.loc.peer = my_public_key;
926 GNUNET_assert (GNUNET_OK ==
927 GNUNET_CRYPTO_rsa_sign (my_private_key, &ass.purpose,
928 &uri->data.loc.contentSignature));
929 GNUNET_CRYPTO_rsa_key_free (my_private_key);
935 * Create an SKS URI from a namespace and an identifier.
937 * @param ns namespace
938 * @param id identifier
939 * @param emsg where to store an error message
940 * @return an FS URI for the given namespace and identifier
942 struct GNUNET_FS_Uri *
943 GNUNET_FS_uri_sks_create (struct GNUNET_FS_Namespace *ns, const char *id,
946 struct GNUNET_FS_Uri *ns_uri;
947 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pk;
949 ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
951 GNUNET_CRYPTO_rsa_key_get_public (ns->key, &pk);
952 GNUNET_CRYPTO_hash (&pk, sizeof (pk), &ns_uri->data.sks.ns);
953 ns_uri->data.sks.identifier = GNUNET_strdup (id);
959 * Create an SKS URI from a namespace ID and an identifier.
961 * @param nsid namespace ID
962 * @param id identifier
963 * @return an FS URI for the given namespace and identifier
965 struct GNUNET_FS_Uri *
966 GNUNET_FS_uri_sks_create_from_nsid (struct GNUNET_HashCode * nsid, const char *id)
968 struct GNUNET_FS_Uri *ns_uri;
970 ns_uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
972 ns_uri->data.sks.ns = *nsid;
973 ns_uri->data.sks.identifier = GNUNET_strdup (id);
979 * Merge the sets of keywords from two KSK URIs.
980 * (useful for merging the canonicalized keywords with
981 * the original keywords for sharing).
983 * @param u1 first uri
984 * @param u2 second uri
985 * @return merged URI, NULL on error
987 struct GNUNET_FS_Uri *
988 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
989 const struct GNUNET_FS_Uri *u2)
991 struct GNUNET_FS_Uri *ret;
999 if ((u1 == NULL) && (u2 == NULL))
1002 return GNUNET_FS_uri_dup (u2);
1004 return GNUNET_FS_uri_dup (u1);
1005 if ((u1->type != ksk) || (u2->type != ksk))
1010 kc = u1->data.ksk.keywordCount;
1011 kl = GNUNET_malloc ((kc + u2->data.ksk.keywordCount) * sizeof (char *));
1012 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1013 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
1014 for (i = 0; i < u2->data.ksk.keywordCount; i++)
1016 kp = u2->data.ksk.keywords[i];
1018 for (j = 0; j < u1->data.ksk.keywordCount; j++)
1019 if (0 == strcmp (kp + 1, kl[j] + 1))
1027 kl[kc++] = GNUNET_strdup (kp);
1029 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1031 ret->data.ksk.keywordCount = kc;
1032 ret->data.ksk.keywords = kl;
1040 * @param uri the URI to duplicate
1041 * @return copy of the URI
1043 struct GNUNET_FS_Uri *
1044 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
1046 struct GNUNET_FS_Uri *ret;
1051 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1052 memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
1056 if (ret->data.ksk.keywordCount >=
1057 GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
1063 if (ret->data.ksk.keywordCount > 0)
1065 ret->data.ksk.keywords =
1066 GNUNET_malloc (ret->data.ksk.keywordCount * sizeof (char *));
1067 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1068 ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]);
1071 ret->data.ksk.keywords = NULL; /* just to be sure */
1074 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1086 * Create an FS URI from a single user-supplied string of keywords.
1087 * The string is broken up at spaces into individual keywords.
1088 * Keywords that start with "+" are mandatory. Double-quotes can
1089 * be used to prevent breaking up strings at spaces (and also
1090 * to specify non-mandatory keywords starting with "+").
1092 * Keywords must contain a balanced number of double quotes and
1093 * double quotes can not be used in the actual keywords (for
1094 * example, the string '""foo bar""' will be turned into two
1095 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1097 * @param keywords the keyword string
1098 * @param emsg where to store an error message
1099 * @return an FS URI for the given keywords, NULL
1100 * if keywords is not legal (i.e. empty).
1102 struct GNUNET_FS_Uri *
1103 GNUNET_FS_uri_ksk_create (const char *keywords, char **emsg)
1106 unsigned int num_Words;
1109 struct GNUNET_FS_Uri *uri;
1113 if (keywords == NULL)
1115 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1119 searchString = GNUNET_strdup (keywords);
1124 while ('\0' != *pos)
1126 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1130 else if (0 == inWord)
1136 saw_quote = (saw_quote + 1) % 2;
1141 GNUNET_free (searchString);
1142 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1147 GNUNET_free (searchString);
1148 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1151 keywordarr = GNUNET_malloc (num_Words * sizeof (char *));
1155 while ('\0' != *pos)
1157 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1162 else if (0 == inWord)
1164 keywordarr[num_Words] = pos;
1169 saw_quote = (saw_quote + 1) % 2;
1173 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1174 (const char **) keywordarr);
1175 GNUNET_free (keywordarr);
1176 GNUNET_free (searchString);
1182 * Create an FS URI from a user-supplied command line of keywords.
1183 * Arguments should start with "+" to indicate mandatory
1186 * @param argc number of keywords
1187 * @param argv keywords (double quotes are not required for
1188 * keywords containing spaces; however, double
1189 * quotes are required for keywords starting with
1190 * "+"); there is no mechanism for having double
1191 * quotes in the actual keywords (if the user
1192 * did specifically specify double quotes, the
1193 * caller should convert each double quote
1194 * into two single quotes).
1195 * @return an FS URI for the given keywords, NULL
1196 * if keywords is not legal (i.e. empty).
1198 struct GNUNET_FS_Uri *
1199 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc, const char **argv)
1202 struct GNUNET_FS_Uri *uri;
1203 const char *keyword;
1211 /* allow URI to be given as one and only keyword and
1212 * handle accordingly */
1214 if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) &&
1216 strncmp (argv[0], GNUNET_FS_URI_PREFIX, strlen (GNUNET_FS_URI_PREFIX)))
1217 && (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg))))
1219 GNUNET_free_non_null (emsg);
1220 uri = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1222 uri->data.ksk.keywordCount = argc;
1223 uri->data.ksk.keywords = GNUNET_malloc (argc * sizeof (char *));
1224 for (i = 0; i < argc; i++)
1227 if (keyword[0] == '+')
1228 val = GNUNET_strdup (keyword);
1230 GNUNET_asprintf (&val, " %s", keyword);
1241 uri->data.ksk.keywords[i] = val;
1248 * Test if two URIs are equal.
1250 * @param u1 one of the URIs
1251 * @param u2 the other URI
1252 * @return GNUNET_YES if the URIs are equal
1255 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1256 const struct GNUNET_FS_Uri *u2)
1262 GNUNET_assert (u1 != NULL);
1263 GNUNET_assert (u2 != NULL);
1264 if (u1->type != u2->type)
1270 memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier)))
1275 memcmp (&u1->data.sks.ns, &u2->data.sks.ns,
1276 sizeof (struct GNUNET_HashCode))) &&
1277 (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
1282 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1284 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1287 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1289 if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1295 if (ret == GNUNET_NO)
1301 (&u1->data.loc, &u2->data.loc,
1302 sizeof (struct FileIdentifier) +
1303 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1304 sizeof (struct GNUNET_TIME_Absolute) + sizeof (unsigned short) +
1305 sizeof (unsigned short)) != 0)
1315 * Is this a namespace URI?
1317 * @param uri the uri to check
1318 * @return GNUNET_YES if this is an SKS uri
1321 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1323 return uri->type == sks;
1328 * Get the ID of a namespace from the given
1331 * @param uri the uri to get the namespace ID from
1332 * @param nsid where to store the ID of the namespace
1333 * @return GNUNET_OK on success
1336 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1337 struct GNUNET_HashCode * nsid)
1339 if (!GNUNET_FS_uri_test_sks (uri))
1342 return GNUNET_SYSERR;
1344 *nsid = uri->data.sks.ns;
1350 * Get the content identifier of an SKS URI.
1352 * @param uri the sks uri
1353 * @return NULL on error (not a valid SKS URI)
1356 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1358 if (!GNUNET_FS_uri_test_sks (uri))
1363 return GNUNET_strdup (uri->data.sks.identifier);
1368 * Convert namespace URI to a human readable format
1369 * (using the namespace description, if available).
1371 * @param cfg configuration to use
1372 * @param uri SKS uri to convert
1373 * @return NULL on error (not an SKS URI)
1376 GNUNET_FS_uri_sks_to_string_fancy (struct GNUNET_CONFIGURATION_Handle *cfg,
1377 const struct GNUNET_FS_Uri *uri)
1383 if (uri->type != sks)
1385 (void) GNUNET_PSEUDONYM_get_info (cfg, &uri->data.sks.ns,
1386 NULL, NULL, &name, NULL);
1387 unique_name = GNUNET_PSEUDONYM_name_uniquify (cfg, &uri->data.sks.ns, name, NULL);
1389 GNUNET_asprintf (&ret, "%s: %s", unique_name, uri->data.sks.identifier);
1390 GNUNET_free (unique_name);
1396 * Is this a keyword URI?
1398 * @param uri the uri
1399 * @return GNUNET_YES if this is a KSK uri
1402 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1407 if (uri->type == ksk)
1409 for (i=0;i < uri->data.ksk.keywordCount; i++)
1410 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1413 return uri->type == ksk;
1418 * Is this a file (or directory) URI?
1420 * @param uri the uri to check
1421 * @return GNUNET_YES if this is a CHK uri
1424 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1426 return uri->type == chk;
1431 * What is the size of the file that this URI
1434 * @param uri the CHK URI to inspect
1435 * @return size of the file as specified in the CHK URI
1438 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri * uri)
1443 return GNUNET_ntohll (uri->data.chk.file_length);
1445 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1449 return 0; /* unreachable */
1454 * Is this a location URI?
1456 * @param uri the uri to check
1457 * @return GNUNET_YES if this is a LOC uri
1460 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1462 return uri->type == loc;
1467 * Add a keyword as non-mandatory (with ' '-prefix) to the
1468 * given keyword list at offset 'index'. The array is
1469 * guaranteed to be long enough.
1471 * @param s keyword to add
1472 * @param array array to add the keyword to
1473 * @param index offset where to add the keyword
1476 insert_non_mandatory_keyword (const char *s, char **array, int index)
1479 GNUNET_asprintf (&nkword, " %s", /* space to mark as 'non mandatory' */ s);
1480 array[index] = nkword;
1485 * Test if the given keyword 's' is already present in the
1486 * given array, ignoring the '+'-mandatory prefix in the array.
1488 * @param s keyword to test
1489 * @param array keywords to test against, with ' ' or '+' prefix to ignore
1490 * @param array_length length of the array
1491 * @return GNUNET_YES if the keyword exists, GNUNET_NO if not
1494 find_duplicate (const char *s, const char **array, int array_length)
1498 for (j = array_length - 1; j >= 0; j--)
1499 if (0 == strcmp (&array[j][1], s))
1509 normalize_metadata (enum EXTRACTOR_MetaFormat format, const char *data,
1512 uint8_t *free_str = NULL;
1513 uint8_t *str_to_normalize = (uint8_t *) data;
1514 uint8_t *normalized;
1516 if (str_to_normalize == NULL)
1518 /* Don't trust libextractor */
1519 if (format == EXTRACTOR_METAFORMAT_UTF8)
1521 free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len);
1522 if (free_str == NULL)
1525 format = EXTRACTOR_METAFORMAT_C_STRING;
1527 if (format == EXTRACTOR_METAFORMAT_C_STRING)
1529 free_str = u8_strconv_from_encoding (data, locale_charset (), iconveh_escape_sequence);
1530 if (free_str == NULL)
1534 normalized = u8_tolower (str_to_normalize, strlen ((char *) str_to_normalize), NULL, UNINORM_NFD, NULL, &r_len);
1535 /* free_str is allocated by libunistring internally, use free() */
1536 if (free_str != NULL)
1538 if (normalized != NULL)
1540 /* u8_tolower allocates a non-NULL-terminated string! */
1541 free_str = GNUNET_malloc (r_len + 1);
1542 memcpy (free_str, normalized, r_len);
1543 free_str[r_len] = '\0';
1545 normalized = free_str;
1547 return (char *) normalized;
1551 * Counts the number of UTF-8 characters (not bytes) in the string,
1552 * returns that count.
1555 u8_strcount (const uint8_t *s)
1559 GNUNET_assert (s != NULL);
1562 for (count = 0; s != NULL; count++)
1563 s = u8_next (&c, s);
1569 * Break the filename up by matching [], () and {} pairs to make
1570 * keywords. In case of nesting parentheses only the inner pair counts.
1571 * You can't escape parentheses to scan something like "[blah\{foo]" to
1572 * make a "blah{foo" keyword, this function is only a heuristic!
1574 * @param s string to break down.
1575 * @param array array to fill with enclosed tokens. If NULL, then tokens
1577 * @param index index at which to start filling the array (entries prior
1578 * to it are used to check for duplicates). ignored if array == NULL.
1579 * @return number of tokens counted (including duplicates), or number of
1580 * tokens extracted (excluding duplicates). 0 if there are no
1581 * matching parens in the string (when counting), or when all tokens
1582 * were duplicates (when extracting).
1585 get_keywords_from_parens (const char *s, char **array, int index)
1595 ss = GNUNET_strdup (s);
1596 open_paren = ss - 1;
1597 while (NULL != (open_paren = strpbrk (open_paren + 1, "[{(")))
1601 close_paren = strpbrk (open_paren + 1, "]})");
1602 if (NULL == close_paren)
1604 switch (open_paren[0])
1607 if (']' == close_paren[0])
1611 if ('}' == close_paren[0])
1615 if (')' == close_paren[0])
1621 if (match && (close_paren - open_paren > 1))
1623 tmp = close_paren[0];
1624 close_paren[0] = '\0';
1625 /* Keywords must be at least 3 characters long */
1626 if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2)
1628 close_paren[0] = tmp;
1634 if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
1635 (const char **) array, index + count))
1637 insert_non_mandatory_keyword ((const char *) &open_paren[1], array,
1641 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1642 &open_paren[1], close_paren - &open_paren[1]);
1643 if (normalized != NULL)
1645 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1646 (const char **) array, index + count))
1648 insert_non_mandatory_keyword ((const char *) normalized, array,
1652 GNUNET_free (normalized);
1657 close_paren[0] = tmp;
1666 * Where to break up keywords
1668 #define TOKENS "_. /-!?#&+@\"\'\\;:,()[]{}$<>|"
1671 * Break the filename up by TOKENS to make
1674 * @param s string to break down.
1675 * @param array array to fill with tokens. If NULL, then tokens are only
1677 * @param index index at which to start filling the array (entries prior
1678 * to it are used to check for duplicates). ignored if array == NULL.
1679 * @return number of tokens (>1) counted (including duplicates), or number of
1680 * tokens extracted (excluding duplicates). 0 if there are no
1681 * separators in the string (when counting), or when all tokens were
1682 * duplicates (when extracting).
1685 get_keywords_from_tokens (const char *s, char **array, int index)
1691 ss = GNUNET_strdup (s);
1692 for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS))
1694 /* Keywords must be at least 3 characters long */
1695 if (u8_strcount ((const uint8_t *) p) <= 2)
1700 if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps))
1702 insert_non_mandatory_keyword (p, array,
1706 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1708 if (normalized != NULL)
1710 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1711 (const char **) array, index + seps))
1713 insert_non_mandatory_keyword ((const char *) normalized, array,
1717 GNUNET_free (normalized);
1729 * Function called on each value in the meta data.
1730 * Adds it to the URI.
1732 * @param cls URI to update
1733 * @param plugin_name name of the plugin that produced this value;
1734 * special values can be used (i.e. '<zlib>' for zlib being
1735 * used in the main libextractor library and yielding
1737 * @param type libextractor-type describing the meta data
1738 * @param format basic format information about data
1739 * @param data_mime_type mime-type of data (not of the original file);
1740 * can be NULL (if mime-type is not known)
1741 * @param data actual meta-data found
1742 * @param data_len number of bytes in data
1743 * @return 0 (always)
1746 gather_uri_data (void *cls, const char *plugin_name,
1747 enum EXTRACTOR_MetaType type, enum EXTRACTOR_MetaFormat format,
1748 const char *data_mime_type, const char *data, size_t data_len)
1750 struct GNUNET_FS_Uri *uri = cls;
1751 char *normalized_data;
1753 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1754 (format != EXTRACTOR_METAFORMAT_C_STRING))
1756 /* Keywords must be at least 3 characters long
1757 * If given non-utf8 string it will, most likely, find it to be invalid,
1758 * and will return the length of its valid part, skipping the keyword.
1759 * If it does - fix the extractor, not this check!
1761 if (u8_strcount ((const uint8_t *) data) <= 2)
1765 normalized_data = normalize_metadata (format, data, data_len);
1766 if (!find_duplicate (data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
1768 insert_non_mandatory_keyword (data,
1769 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1770 uri->data.ksk.keywordCount++;
1772 if (normalized_data != NULL)
1774 if (!find_duplicate (normalized_data, (const char **) uri->data.ksk.keywords, uri->data.ksk.keywordCount))
1776 insert_non_mandatory_keyword (normalized_data,
1777 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1778 uri->data.ksk.keywordCount++;
1780 GNUNET_free (normalized_data);
1787 * Construct a keyword-URI from meta-data (take all entries
1788 * in the meta-data and construct one large keyword URI
1789 * that lists all keywords that can be found in the meta-data).
1791 * @param md metadata to use
1792 * @return NULL on error, otherwise a KSK URI
1794 struct GNUNET_FS_Uri *
1795 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData
1798 struct GNUNET_FS_Uri *ret;
1800 char *full_name = NULL;
1803 int tok_keywords = 0;
1804 int paren_keywords = 0;
1808 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri));
1810 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1813 full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
1814 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, -1);
1815 if (NULL != full_name)
1817 filename = full_name;
1818 while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR)))
1820 tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
1821 paren_keywords = get_keywords_from_parens (filename, NULL, 0);
1823 /* x2 because there might be a normalized variant of every keyword */
1824 ret->data.ksk.keywords = GNUNET_malloc (sizeof (char *) * (ent
1825 + tok_keywords + paren_keywords) * 2);
1826 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1828 if (tok_keywords > 0)
1829 ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
1830 ret->data.ksk.keywords,
1831 ret->data.ksk.keywordCount);
1832 if (paren_keywords > 0)
1833 ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
1834 ret->data.ksk.keywords,
1835 ret->data.ksk.keywordCount);
1837 GNUNET_free_non_null (full_name);
1843 * In URI-encoding, does the given character
1844 * need to be encoded using %-encoding?
1847 needs_percent (char c)
1850 ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') ||
1851 (c == '.') || (c == '~')));
1856 * Convert a KSK URI to a string.
1858 * @param uri the URI to convert
1859 * @return NULL on error (i.e. keywordCount == 0)
1862 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1865 unsigned int keywordCount;
1872 const char *keyword;
1874 if (uri->type != ksk)
1876 keywords = uri->data.ksk.keywords;
1877 keywordCount = uri->data.ksk.keywordCount;
1878 n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1879 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1880 for (i = 0; i < keywordCount; i++)
1882 keyword = keywords[i];
1883 slen = strlen (keyword);
1885 for (j = 0; j < slen; j++)
1887 if ((j == 0) && (keyword[j] == ' '))
1890 continue; /* skip leading space */
1892 if (needs_percent (keyword[j]))
1893 n += 2; /* will use %-encoding */
1896 ret = GNUNET_malloc (n);
1897 strcpy (ret, GNUNET_FS_URI_PREFIX);
1898 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1899 wpos = strlen (ret);
1900 for (i = 0; i < keywordCount; i++)
1902 keyword = keywords[i];
1903 slen = strlen (keyword);
1904 for (j = 0; j < slen; j++)
1906 if ((j == 0) && (keyword[j] == ' '))
1907 continue; /* skip leading space */
1908 if (needs_percent (keyword[j]))
1910 sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]);
1915 ret[wpos++] = keyword[j];
1918 if (i != keywordCount - 1)
1926 * Convert SKS URI to a string.
1928 * @param uri sks uri to convert
1929 * @return NULL on error
1932 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1934 const struct GNUNET_HashCode *ns;
1935 const char *identifier;
1937 struct GNUNET_CRYPTO_HashAsciiEncoded nsasc;
1939 if (uri->type != sks)
1941 ns = &uri->data.sks.ns;
1942 identifier = uri->data.sks.identifier;
1943 GNUNET_CRYPTO_hash_to_enc (ns, &nsasc);
1944 GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
1945 GNUNET_FS_URI_SKS_INFIX, (const char *) &nsasc, identifier);
1951 * Convert a CHK URI to a string.
1953 * @param uri chk uri to convert
1954 * @return NULL on error
1957 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1959 const struct FileIdentifier *fi;
1961 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1962 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1964 if (uri->type != chk)
1966 fi = &uri->data.chk;
1967 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1968 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1970 GNUNET_asprintf (&ret, "%s%s%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1971 GNUNET_FS_URI_CHK_INFIX, (const char *) &keyhash,
1972 (const char *) &queryhash, GNUNET_ntohll (fi->file_length));
1977 * Convert binary data to a string.
1979 * @param data binary data to convert
1980 * @param size number of bytes in data
1981 * @return converted data
1984 bin2enc (const void *data, size_t size)
1987 * 64 characters for encoding, 6 bits per character
1990 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_=";
1998 GNUNET_assert (strlen (tbl) == 64);
2000 if (((size * 8) % 6) != 0)
2002 ret = GNUNET_malloc (len + 1);
2007 for (pos = 0; pos < size; pos++)
2009 bits |= ((((const unsigned char *) data)[pos]) << hbits);
2013 ret[len++] = tbl[bits & 63];
2019 ret[len] = tbl[bits & 63];
2025 * Convert a LOC URI to a string.
2027 * @param uri loc uri to convert
2028 * @return NULL on error
2031 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
2034 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
2035 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
2039 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
2040 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
2042 bin2enc (&uri->data.loc.peer,
2043 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded));
2045 bin2enc (&uri->data.loc.contentSignature,
2046 sizeof (struct GNUNET_CRYPTO_RsaSignature));
2047 GNUNET_asprintf (&ret, "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
2048 GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
2049 (const char *) &queryhash,
2050 (unsigned long long) GNUNET_ntohll (uri->data.loc.
2051 fi.file_length), peerId,
2053 (unsigned long long) uri->data.loc.expirationTime.abs_value);
2054 GNUNET_free (peerSig);
2055 GNUNET_free (peerId);
2061 * Convert a URI to a UTF-8 String.
2063 * @param uri uri to convert to a string
2064 * @return the UTF-8 string
2067 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
2077 return uri_ksk_to_string (uri);
2079 return uri_sks_to_string (uri);
2081 return uri_chk_to_string (uri);
2083 return uri_loc_to_string (uri);
2090 /* end of fs_uri.c */