2 This file is part of GNUnet.
3 Copyright (C) 2003--2014 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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:
44 * First, there are URIs that identify a file. They have the format
45 * "gnunet://fs/chk/HEX1.HEX2.SIZE". These URIs can be used to
46 * download the file. The description, filename, mime-type and other
47 * meta-data is NOT part of the file-URI since a URI uniquely
48 * identifies a resource (and the contents of the file would be the
49 * same even if it had a different description).
53 * The second category identifies entries in a namespace. The format
54 * is "gnunet://fs/sks/NAMESPACE/IDENTIFIER" where the namespace
55 * should be given in HEX. Applications may allow using a nickname
56 * for the namespace if the nickname is not ambiguous. The identifier
57 * can be either an ASCII sequence or a HEX-encoding. If the
58 * identifier is in ASCII but the format is ambiguous and could denote
59 * a HEX-string a "/" is appended to indicate ASCII encoding.
63 * The third category identifies ordinary searches. The format is
64 * "gnunet://fs/ksk/KEYWORD[+KEYWORD]*". Using the "+" syntax
65 * it is possible to encode searches with the boolean "AND" operator.
66 * "+" is used since it indicates a commutative 'and' operation and
67 * is unlikely to be used in a keyword by itself.
71 * The last category identifies a datum on a specific machine. The
72 * format is "gnunet://fs/loc/HEX1.HEX2.SIZE.PEER.SIG.EXPTIME". PEER is
73 * the BinName of the public key of the peer storing the datum. The
74 * signature (SIG) certifies that this peer has this content.
75 * HEX1, HEX2 and SIZE correspond to a 'chk' URI.
79 * The encoding for hexadecimal values is defined in the hashing.c
80 * module in the gnunetutil library and discussed there.
84 #include "gnunet_fs_service.h"
85 #include "gnunet_signatures.h"
96 * Get a unique key from a URI. This is for putting URIs
97 * into HashMaps. The key may change between FS implementations.
99 * @param uri uri to convert to a unique key
100 * @param key where to store the unique key
101 * @return #GNUNET_OK on success
104 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
105 struct GNUNET_HashCode *key)
109 case GNUNET_FS_URI_CHK:
110 *key = uri->data.chk.chk.query;
112 case GNUNET_FS_URI_SKS:
113 GNUNET_CRYPTO_hash (uri->data.sks.identifier,
114 strlen (uri->data.sks.identifier),
117 case GNUNET_FS_URI_KSK:
118 if (uri->data.ksk.keywordCount > 0)
120 GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
121 strlen (uri->data.ksk.keywords[0]),
127 memset (key, 0, sizeof (struct GNUNET_HashCode));
128 return GNUNET_SYSERR;
131 case GNUNET_FS_URI_LOC:
132 GNUNET_CRYPTO_hash (&uri->data.loc.fi,
133 sizeof (struct FileIdentifier) +
134 sizeof (struct GNUNET_PeerIdentity),
138 memset (key, 0, sizeof (struct GNUNET_HashCode));
139 return GNUNET_SYSERR;
145 * Convert keyword URI to a human readable format
146 * (i.e. the search query that was used in the first place)
148 * @param uri ksk uri to convert to a string
149 * @return string with the keywords
152 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
159 unsigned int keywordCount;
161 if ((NULL == uri) || (GNUNET_FS_URI_KSK != uri->type))
166 keywords = uri->data.ksk.keywords;
167 keywordCount = uri->data.ksk.keywordCount;
168 n = keywordCount + 1;
169 for (i = 0; i < keywordCount; i++)
171 keyword = keywords[i];
172 n += strlen (keyword) - 1;
173 if (NULL != strstr (&keyword[1], " "))
175 if (keyword[0] == '+')
178 ret = GNUNET_malloc (n);
180 for (i = 0; i < keywordCount; i++)
182 keyword = keywords[i];
183 if (NULL != strstr (&keyword[1], " "))
186 if (keyword[0] == '+')
187 strcat (ret, keyword);
189 strcat (ret, &keyword[1]);
194 if (keyword[0] == '+')
195 strcat (ret, keyword);
197 strcat (ret, &keyword[1]);
206 * Given a keyword with %-encoding (and possibly quotes to protect
207 * spaces), return a copy of the keyword without %-encoding and
208 * without double-quotes (%22). Also, add a space at the beginning
209 * if there is not a '+'.
211 * @param in string with %-encoding
212 * @param emsg where to store the parser error message (if any)
213 * @return decodded string with leading space (or preserved plus)
216 percent_decode_keyword (const char *in,
225 out = GNUNET_strdup (in);
228 while (out[rpos] != '\0')
230 if (out[rpos] == '%')
232 if (1 != SSCANF (&out[rpos + 1], "%2X", &hx))
235 *emsg = GNUNET_strdup (_(/* xgettext:no-c-format */
236 "Malformed KSK URI (`%' must be followed by HEX number)"));
241 continue; /* skip double quote */
242 out[wpos++] = (char) hx;
246 out[wpos++] = out[rpos++];
252 ret = GNUNET_strdup (out);
256 /* need to prefix with space */
257 ret = GNUNET_malloc (strlen (out) + 2);
265 #define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
270 * @param s an uri string
271 * @param emsg where to store the parser error message (if any)
272 * @return NULL on error, otherwise the KSK URI
274 static struct GNUNET_FS_Uri *
275 uri_ksk_parse (const char *s,
278 struct GNUNET_FS_Uri *ret;
289 pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
290 if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX, pos)))
291 return NULL; /* not KSK URI */
292 if ((s[slen - 1] == '+') || (s[pos] == '+'))
295 GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
300 for (i = pos; i < slen; i++)
302 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
304 saw_quote = (saw_quote + 1) % 2;
308 if ((s[i] == '+') && (saw_quote == 0))
313 *emsg = GNUNET_strdup (_("Malformed KSK URI (`++' not allowed)"));
320 *emsg = GNUNET_strdup (_("Malformed KSK URI (quotes not balanced)"));
324 dup = GNUNET_strdup (s);
325 keywords = GNUNET_new_array (max,
327 for (i = slen - 1; i >= (int) pos; i--)
329 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
331 saw_quote = (saw_quote + 1) % 2;
334 if ((dup[i] == '+') && (saw_quote == 0))
336 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
337 if (NULL == keywords[max])
342 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
343 if (NULL == keywords[max])
345 GNUNET_assert (0 == max);
347 ret = GNUNET_new (struct GNUNET_FS_Uri);
348 ret->type = GNUNET_FS_URI_KSK;
349 ret->data.ksk.keywordCount = iret;
350 ret->data.ksk.keywords = keywords;
353 for (i = 0; i < max; i++)
354 GNUNET_free_non_null (keywords[i]);
355 GNUNET_free (keywords);
361 #define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
366 * @param s an uri string
367 * @param emsg where to store the parser error message (if any)
368 * @return NULL on error, SKS URI otherwise
370 static struct GNUNET_FS_Uri *
371 uri_sks_parse (const char *s,
374 struct GNUNET_FS_Uri *ret;
375 struct GNUNET_CRYPTO_EcdsaPublicKey ns;
379 pos = strlen (GNUNET_FS_URI_SKS_PREFIX);
380 if ((strlen (s) <= pos) || (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX, pos)))
381 return NULL; /* not an SKS URI */
382 end = strchr (&s[pos], '/');
383 if ( (NULL == end) ||
385 GNUNET_STRINGS_string_to_data (&s[pos],
390 *emsg = GNUNET_strdup (_("Malformed SKS URI (wrong syntax)"));
391 return NULL; /* malformed */
393 end++; /* skip over '/' */
394 ret = GNUNET_new (struct GNUNET_FS_Uri);
395 ret->type = GNUNET_FS_URI_SKS;
396 ret->data.sks.ns = ns;
397 ret->data.sks.identifier = GNUNET_strdup (end);
401 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
407 * @param s an uri string
408 * @param emsg where to store the parser error message (if any)
409 * @return NULL on error, CHK URI otherwise
411 static struct GNUNET_FS_Uri *
412 uri_chk_parse (const char *s,
415 struct GNUNET_FS_Uri *ret;
416 struct FileIdentifier fi;
418 unsigned long long flen;
420 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
421 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
424 pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
425 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
426 (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX, pos)))
427 return NULL; /* not a CHK URI */
428 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
429 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
431 *emsg = GNUNET_strdup (_("Malformed CHK URI (wrong syntax)"));
434 GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
435 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
436 GNUNET_memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
437 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
438 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
440 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &fi.chk.key)) ||
441 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &fi.chk.query)) ||
443 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
446 *emsg = GNUNET_strdup (_("Malformed CHK URI (failed to decode CHK)"));
449 fi.file_length = GNUNET_htonll (flen);
450 ret = GNUNET_new (struct GNUNET_FS_Uri);
451 ret->type = GNUNET_FS_URI_CHK;
457 GNUNET_NETWORK_STRUCT_BEGIN
459 * Structure that defines how the contents of a location URI must be
460 * assembled in memory to create or verify the signature of a location
463 struct LocUriAssembly
466 * What is being signed (rest of this struct).
468 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
471 * Expiration time of the offer.
473 struct GNUNET_TIME_AbsoluteNBO exptime;
476 * File being offered.
478 struct FileIdentifier fi;
481 * Peer offering the file.
483 struct GNUNET_PeerIdentity peer;
486 GNUNET_NETWORK_STRUCT_END
489 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
491 #define SIGNATURE_ASCII_LENGTH 103
495 * Also verifies validity of the location URI.
497 * @param s an uri string
498 * @param emsg where to store the parser error message (if any)
499 * @return NULL on error, valid LOC URI otherwise
501 static struct GNUNET_FS_Uri *
502 uri_loc_parse (const char *s,
505 struct GNUNET_FS_Uri *uri;
506 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
507 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
510 unsigned long long exptime;
511 unsigned long long flen;
512 struct GNUNET_TIME_Absolute et;
513 struct GNUNET_CRYPTO_EddsaSignature sig;
514 struct LocUriAssembly ass;
518 pos = strlen (GNUNET_FS_URI_LOC_PREFIX);
519 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
520 (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX, pos)))
521 return NULL; /* not a LOC URI */
522 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
523 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
525 *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax)"));
528 GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
529 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
530 GNUNET_memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
531 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
532 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
534 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &ass.fi.chk.key)) ||
535 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &ass.fi.chk.query)) ||
537 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
540 *emsg = GNUNET_strdup (_("LOC URI malformed (no CHK)"));
543 ass.fi.file_length = GNUNET_htonll (flen);
545 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
546 while ((s[npos] != '\0') && (s[npos] != '.'))
550 *emsg = GNUNET_strdup (_("LOC URI malformed (missing LOC)"));
554 if ( (strlen (&s[npos]) <= GNUNET_CRYPTO_PKEY_ASCII_LENGTH + 1) ||
555 ('.' != s[npos+GNUNET_CRYPTO_PKEY_ASCII_LENGTH]) )
558 GNUNET_strdup (_("LOC URI malformed (wrong syntax for public key)"));
561 GNUNET_CRYPTO_eddsa_public_key_from_string (&s[npos],
562 GNUNET_CRYPTO_PKEY_ASCII_LENGTH,
563 &ass.peer.public_key))
566 GNUNET_strdup (_("LOC URI malformed (could not decode public key)"));
569 npos += GNUNET_CRYPTO_PKEY_ASCII_LENGTH;
570 if (s[npos++] != '.')
572 *emsg = GNUNET_strdup (_("LOC URI malformed (could not find signature)"));
575 if ( (strlen (&s[npos]) <= SIGNATURE_ASCII_LENGTH + 1) ||
576 ('.' != s[npos + SIGNATURE_ASCII_LENGTH]) )
578 *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax for signature)"));
582 GNUNET_STRINGS_string_to_data (&s[npos],
583 SIGNATURE_ASCII_LENGTH,
585 sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
587 *emsg = GNUNET_strdup (_("LOC URI malformed (could not decode signature)"));
590 npos += SIGNATURE_ASCII_LENGTH;
591 if (s[npos++] != '.')
593 *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax for expiration time)"));
596 if (1 != SSCANF (&s[npos], "%llu", &exptime))
599 GNUNET_strdup (_("LOC URI malformed (could not parse expiration time)"));
602 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
603 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
604 et.abs_value_us = exptime * 1000LL * 1000LL;
605 ass.exptime = GNUNET_TIME_absolute_hton (et);
607 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
608 &ass.purpose, &sig, &ass.peer.public_key))
611 GNUNET_strdup (_("LOC URI malformed (signature failed validation)"));
614 uri = GNUNET_new (struct GNUNET_FS_Uri);
615 uri->type = GNUNET_FS_URI_LOC;
616 uri->data.loc.fi = ass.fi;
617 uri->data.loc.peer = ass.peer;
618 uri->data.loc.expirationTime = et;
619 uri->data.loc.contentSignature = sig;
628 * Convert a UTF-8 String to a URI.
630 * @param uri string to parse
631 * @param emsg where to store the parser error message (if any)
632 * @return NULL on error
634 struct GNUNET_FS_Uri *
635 GNUNET_FS_uri_parse (const char *uri,
638 struct GNUNET_FS_Uri *ret;
645 *emsg = GNUNET_strdup (_("invalid argument"));
651 if ((NULL != (ret = uri_chk_parse (uri, emsg))) ||
652 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
653 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
654 (NULL != (ret = uri_loc_parse (uri, emsg))))
657 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
667 * @param uri uri to free
670 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
676 case GNUNET_FS_URI_KSK:
677 for (i = 0; i < uri->data.ksk.keywordCount; i++)
678 GNUNET_free (uri->data.ksk.keywords[i]);
679 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0);
681 case GNUNET_FS_URI_SKS:
682 GNUNET_free (uri->data.sks.identifier);
684 case GNUNET_FS_URI_LOC:
695 * How many keywords are ANDed in this keyword URI?
697 * @param uri ksk uri to get the number of keywords from
698 * @return 0 if this is not a keyword URI
701 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
703 if (uri->type != GNUNET_FS_URI_KSK)
705 return uri->data.ksk.keywordCount;
710 * Iterate over all keywords in this keyword URI.
712 * @param uri ksk uri to get the keywords from
713 * @param iterator function to call on each keyword
714 * @param iterator_cls closure for iterator
715 * @return -1 if this is not a keyword URI, otherwise number of
716 * keywords iterated over until iterator aborted
719 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
720 GNUNET_FS_KeywordIterator iterator,
726 if (uri->type != GNUNET_FS_URI_KSK)
728 if (NULL == iterator)
729 return uri->data.ksk.keywordCount;
730 for (i = 0; i < uri->data.ksk.keywordCount; i++)
732 keyword = uri->data.ksk.keywords[i];
733 /* first character of keyword indicates
734 * if it is mandatory or not */
735 if (GNUNET_OK != iterator (iterator_cls, &keyword[1], keyword[0] == '+'))
743 * Add the given keyword to the set of keywords represented by the URI.
744 * Does nothing if the keyword is already present.
746 * @param uri ksk uri to modify
747 * @param keyword keyword to add
748 * @param is_mandatory is this keyword mandatory?
751 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
759 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
760 for (i = 0; i < uri->data.ksk.keywordCount; i++)
762 old = uri->data.ksk.keywords[i];
763 if (0 == strcmp (&old[1], keyword))
766 GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword);
767 GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
772 * Remove the given keyword from the set of keywords represented by the URI.
773 * Does nothing if the keyword is not present.
775 * @param uri ksk uri to modify
776 * @param keyword keyword to add
779 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
785 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
786 for (i = 0; i < uri->data.ksk.keywordCount; i++)
788 old = uri->data.ksk.keywords[i];
789 if (0 == strcmp (&old[1], keyword))
791 uri->data.ksk.keywords[i] =
792 uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1];
793 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
794 uri->data.ksk.keywordCount - 1);
803 * Obtain the identity of the peer offering the data
805 * @param uri the location URI to inspect
806 * @param peer where to store the identify of the peer (presumably) offering the content
807 * @return #GNUNET_SYSERR if this is not a location URI, otherwise #GNUNET_OK
810 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
811 struct GNUNET_PeerIdentity *peer)
813 if (uri->type != GNUNET_FS_URI_LOC)
814 return GNUNET_SYSERR;
815 *peer = uri->data.loc.peer;
821 * Obtain the expiration of the LOC URI.
823 * @param uri location URI to get the expiration from
824 * @return expiration time of the URI
826 struct GNUNET_TIME_Absolute
827 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
829 GNUNET_assert (uri->type == GNUNET_FS_URI_LOC);
830 return uri->data.loc.expirationTime;
835 * Obtain the URI of the content itself.
837 * @param uri location URI to get the content URI from
838 * @return NULL if argument is not a location URI
840 struct GNUNET_FS_Uri *
841 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
843 struct GNUNET_FS_Uri *ret;
845 if (uri->type != GNUNET_FS_URI_LOC)
847 ret = GNUNET_new (struct GNUNET_FS_Uri);
848 ret->type = GNUNET_FS_URI_CHK;
849 ret->data.chk = uri->data.loc.fi;
855 * Construct a location URI (this peer will be used for the location).
856 * This function should only be called from within gnunet-service-fs,
857 * as it requires the peer's private key which is generally unavailable
858 * to processes directly under the user's control. However, for
859 * testing and as it logically fits under URIs, it is in this API.
861 * @param base_uri content offered by the sender
862 * @param sign_key private key of the peer
863 * @param expiration_time how long will the content be offered?
864 * @return the location URI, NULL on error
866 struct GNUNET_FS_Uri *
867 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *base_uri,
868 const struct GNUNET_CRYPTO_EddsaPrivateKey *sign_key,
869 struct GNUNET_TIME_Absolute expiration_time)
871 struct GNUNET_FS_Uri *uri;
872 struct GNUNET_CRYPTO_EddsaPublicKey my_public_key;
873 struct LocUriAssembly ass;
874 struct GNUNET_TIME_Absolute et;
876 if (GNUNET_FS_URI_CHK != base_uri->type)
878 /* we round expiration time to full seconds for SKS URIs */
879 et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL;
880 GNUNET_CRYPTO_eddsa_key_get_public (sign_key,
882 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
883 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
884 ass.exptime = GNUNET_TIME_absolute_hton (et);
885 ass.fi = base_uri->data.chk;
886 ass.peer.public_key = my_public_key;
887 uri = GNUNET_new (struct GNUNET_FS_Uri);
888 uri->type = GNUNET_FS_URI_LOC;
889 uri->data.loc.fi = base_uri->data.chk;
890 uri->data.loc.expirationTime = et;
891 uri->data.loc.peer.public_key = my_public_key;
892 GNUNET_assert (GNUNET_OK ==
893 GNUNET_CRYPTO_eddsa_sign (sign_key,
895 &uri->data.loc.contentSignature));
901 * Create an SKS URI from a namespace ID and an identifier.
903 * @param ns namespace ID
904 * @param id identifier
905 * @return an FS URI for the given namespace and identifier
907 struct GNUNET_FS_Uri *
908 GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
911 struct GNUNET_FS_Uri *ns_uri;
913 ns_uri = GNUNET_new (struct GNUNET_FS_Uri);
914 ns_uri->type = GNUNET_FS_URI_SKS;
915 ns_uri->data.sks.ns = *ns;
916 ns_uri->data.sks.identifier = GNUNET_strdup (id);
922 * Merge the sets of keywords from two KSK URIs.
923 * (useful for merging the canonicalized keywords with
924 * the original keywords for sharing).
926 * @param u1 first uri
927 * @param u2 second uri
928 * @return merged URI, NULL on error
930 struct GNUNET_FS_Uri *
931 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
932 const struct GNUNET_FS_Uri *u2)
934 struct GNUNET_FS_Uri *ret;
942 if ((u1 == NULL) && (u2 == NULL))
945 return GNUNET_FS_uri_dup (u2);
947 return GNUNET_FS_uri_dup (u1);
948 if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK))
953 kc = u1->data.ksk.keywordCount;
954 kl = GNUNET_new_array (kc + u2->data.ksk.keywordCount,
956 for (i = 0; i < u1->data.ksk.keywordCount; i++)
957 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
958 for (i = 0; i < u2->data.ksk.keywordCount; i++)
960 kp = u2->data.ksk.keywords[i];
962 for (j = 0; j < u1->data.ksk.keywordCount; j++)
963 if (0 == strcmp (kp + 1, kl[j] + 1))
971 kl[kc++] = GNUNET_strdup (kp);
973 ret = GNUNET_new (struct GNUNET_FS_Uri);
974 ret->type = GNUNET_FS_URI_KSK;
975 ret->data.ksk.keywordCount = kc;
976 ret->data.ksk.keywords = kl;
984 * @param uri the URI to duplicate
985 * @return copy of the URI
987 struct GNUNET_FS_Uri *
988 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
990 struct GNUNET_FS_Uri *ret;
995 ret = GNUNET_new (struct GNUNET_FS_Uri);
996 GNUNET_memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
999 case GNUNET_FS_URI_KSK:
1000 if (ret->data.ksk.keywordCount >=
1001 GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
1007 if (ret->data.ksk.keywordCount > 0)
1009 ret->data.ksk.keywords
1010 = GNUNET_new_array (ret->data.ksk.keywordCount,
1012 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1013 ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]);
1016 ret->data.ksk.keywords = NULL; /* just to be sure */
1018 case GNUNET_FS_URI_SKS:
1019 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1021 case GNUNET_FS_URI_LOC:
1031 * Create an FS URI from a single user-supplied string of keywords.
1032 * The string is broken up at spaces into individual keywords.
1033 * Keywords that start with "+" are mandatory. Double-quotes can
1034 * be used to prevent breaking up strings at spaces (and also
1035 * to specify non-mandatory keywords starting with "+").
1037 * Keywords must contain a balanced number of double quotes and
1038 * double quotes can not be used in the actual keywords (for
1039 * example, the string '""foo bar""' will be turned into two
1040 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1042 * @param keywords the keyword string
1043 * @param emsg where to store an error message
1044 * @return an FS URI for the given keywords, NULL
1045 * if keywords is not legal (i.e. empty).
1047 struct GNUNET_FS_Uri *
1048 GNUNET_FS_uri_ksk_create (const char *keywords,
1052 unsigned int num_Words;
1055 struct GNUNET_FS_Uri *uri;
1059 if (keywords == NULL)
1061 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1065 searchString = GNUNET_strdup (keywords);
1070 while ('\0' != *pos)
1072 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1076 else if (0 == inWord)
1082 saw_quote = (saw_quote + 1) % 2;
1087 GNUNET_free (searchString);
1088 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1093 GNUNET_free (searchString);
1094 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1097 keywordarr = GNUNET_new_array (num_Words,
1102 while ('\0' != *pos)
1104 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1109 else if (0 == inWord)
1111 keywordarr[num_Words] = pos;
1116 saw_quote = (saw_quote + 1) % 2;
1120 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1121 (const char **) keywordarr);
1122 GNUNET_free (keywordarr);
1123 GNUNET_free (searchString);
1129 * Create an FS URI from a user-supplied command line of keywords.
1130 * Arguments should start with "+" to indicate mandatory
1133 * @param argc number of keywords
1134 * @param argv keywords (double quotes are not required for
1135 * keywords containing spaces; however, double
1136 * quotes are required for keywords starting with
1137 * "+"); there is no mechanism for having double
1138 * quotes in the actual keywords (if the user
1139 * did specifically specify double quotes, the
1140 * caller should convert each double quote
1141 * into two single quotes).
1142 * @return an FS URI for the given keywords, NULL
1143 * if keywords is not legal (i.e. empty).
1145 struct GNUNET_FS_Uri *
1146 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
1150 struct GNUNET_FS_Uri *uri;
1151 const char *keyword;
1159 /* allow URI to be given as one and only keyword and
1160 * handle accordingly */
1162 if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) &&
1164 strncmp (argv[0], GNUNET_FS_URI_PREFIX, strlen (GNUNET_FS_URI_PREFIX)))
1165 && (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg))))
1167 GNUNET_free_non_null (emsg);
1168 uri = GNUNET_new (struct GNUNET_FS_Uri);
1169 uri->type = GNUNET_FS_URI_KSK;
1170 uri->data.ksk.keywordCount = argc;
1171 uri->data.ksk.keywords = GNUNET_new_array (argc,
1173 for (i = 0; i < argc; i++)
1176 if (keyword[0] == '+')
1177 val = GNUNET_strdup (keyword);
1179 GNUNET_asprintf (&val, " %s", keyword);
1190 uri->data.ksk.keywords[i] = val;
1197 * Test if two URIs are equal.
1199 * @param u1 one of the URIs
1200 * @param u2 the other URI
1201 * @return #GNUNET_YES if the URIs are equal
1204 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1205 const struct GNUNET_FS_Uri *u2)
1211 GNUNET_assert (u1 != NULL);
1212 GNUNET_assert (u2 != NULL);
1213 if (u1->type != u2->type)
1217 case GNUNET_FS_URI_CHK:
1219 memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier)))
1222 case GNUNET_FS_URI_SKS:
1224 memcmp (&u1->data.sks.ns, &u2->data.sks.ns,
1225 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) &&
1226 (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
1230 case GNUNET_FS_URI_KSK:
1231 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1233 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1236 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1238 if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1244 if (ret == GNUNET_NO)
1248 case GNUNET_FS_URI_LOC:
1250 (&u1->data.loc, &u2->data.loc,
1251 sizeof (struct FileIdentifier) +
1252 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1253 sizeof (struct GNUNET_TIME_Absolute) + sizeof (unsigned short) +
1254 sizeof (unsigned short)) != 0)
1264 * Is this a namespace URI?
1266 * @param uri the uri to check
1267 * @return #GNUNET_YES if this is an SKS uri
1270 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1272 return uri->type == GNUNET_FS_URI_SKS;
1277 * Get the ID of a namespace from the given
1280 * @param uri the uri to get the namespace ID from
1281 * @param pseudonym where to store the ID of the namespace
1282 * @return #GNUNET_OK on success
1285 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1286 struct GNUNET_CRYPTO_EcdsaPublicKey *pseudonym)
1288 if (!GNUNET_FS_uri_test_sks (uri))
1291 return GNUNET_SYSERR;
1293 *pseudonym = uri->data.sks.ns;
1299 * Get the content identifier of an SKS URI.
1301 * @param uri the sks uri
1302 * @return NULL on error (not a valid SKS URI)
1305 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1307 if (!GNUNET_FS_uri_test_sks (uri))
1312 return GNUNET_strdup (uri->data.sks.identifier);
1317 * Is this a keyword URI?
1319 * @param uri the uri
1320 * @return #GNUNET_YES if this is a KSK uri
1323 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1328 if (uri->type == GNUNET_FS_URI_KSK)
1330 for (i=0;i < uri->data.ksk.keywordCount; i++)
1331 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1334 return uri->type == GNUNET_FS_URI_KSK;
1339 * Is this a file (or directory) URI?
1341 * @param uri the uri to check
1342 * @return #GNUNET_YES if this is a CHK uri
1345 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1347 return uri->type == GNUNET_FS_URI_CHK;
1352 * What is the size of the file that this URI
1355 * @param uri the CHK URI to inspect
1356 * @return size of the file as specified in the CHK URI
1359 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri * uri)
1363 case GNUNET_FS_URI_CHK:
1364 return GNUNET_ntohll (uri->data.chk.file_length);
1365 case GNUNET_FS_URI_LOC:
1366 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1370 return 0; /* unreachable */
1375 * Is this a location URI?
1377 * @param uri the uri to check
1378 * @return #GNUNET_YES if this is a LOC uri
1381 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1383 return uri->type == GNUNET_FS_URI_LOC;
1388 * Add a keyword as non-mandatory (with ' '-prefix) to the
1389 * given keyword list at offset 'index'. The array is
1390 * guaranteed to be long enough.
1392 * @param s keyword to add
1393 * @param array array to add the keyword to
1394 * @param index offset where to add the keyword
1397 insert_non_mandatory_keyword (const char *s,
1403 GNUNET_asprintf (&nkword,
1404 " %s", /* space to mark as 'non mandatory' */
1406 array[index] = nkword;
1411 * Test if the given keyword @a s is already present in the
1412 * given array, ignoring the '+'-mandatory prefix in the array.
1414 * @param s keyword to test
1415 * @param array keywords to test against, with ' ' or '+' prefix to ignore
1416 * @param array_length length of the @a array
1417 * @return #GNUNET_YES if the keyword exists, #GNUNET_NO if not
1420 find_duplicate (const char *s,
1426 for (j = array_length - 1; j >= 0; j--)
1427 if (0 == strcmp (&array[j][1], s))
1437 normalize_metadata (enum EXTRACTOR_MetaFormat format,
1441 uint8_t *free_str = NULL;
1442 uint8_t *str_to_normalize = (uint8_t *) data;
1443 uint8_t *normalized;
1445 if (str_to_normalize == NULL)
1447 /* Don't trust libextractor */
1448 if (format == EXTRACTOR_METAFORMAT_UTF8)
1450 free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len);
1451 if (free_str == NULL)
1454 format = EXTRACTOR_METAFORMAT_C_STRING;
1456 if (format == EXTRACTOR_METAFORMAT_C_STRING)
1458 free_str = u8_strconv_from_encoding (data, locale_charset (), iconveh_escape_sequence);
1459 if (free_str == NULL)
1463 normalized = u8_tolower (str_to_normalize, strlen ((char *) str_to_normalize), NULL, UNINORM_NFD, NULL, &r_len);
1464 /* free_str is allocated by libunistring internally, use free() */
1465 if (free_str != NULL)
1467 if (normalized != NULL)
1469 /* u8_tolower allocates a non-NULL-terminated string! */
1470 free_str = GNUNET_malloc (r_len + 1);
1471 GNUNET_memcpy (free_str, normalized, r_len);
1472 free_str[r_len] = '\0';
1474 normalized = free_str;
1476 return (char *) normalized;
1481 * Counts the number of UTF-8 characters (not bytes) in the string,
1482 * returns that count.
1485 u8_strcount (const uint8_t *s)
1489 GNUNET_assert (s != NULL);
1492 for (count = 0; s != NULL; count++)
1493 s = u8_next (&c, s);
1499 * Break the filename up by matching [], () and {} pairs to make
1500 * keywords. In case of nesting parentheses only the inner pair counts.
1501 * You can't escape parentheses to scan something like "[blah\{foo]" to
1502 * make a "blah{foo" keyword, this function is only a heuristic!
1504 * @param s string to break down.
1505 * @param array array to fill with enclosed tokens. If NULL, then tokens
1507 * @param index index at which to start filling the array (entries prior
1508 * to it are used to check for duplicates). ignored if @a array == NULL.
1509 * @return number of tokens counted (including duplicates), or number of
1510 * tokens extracted (excluding duplicates). 0 if there are no
1511 * matching parens in the string (when counting), or when all tokens
1512 * were duplicates (when extracting).
1515 get_keywords_from_parens (const char *s,
1527 ss = GNUNET_strdup (s);
1528 open_paren = ss - 1;
1529 while (NULL != (open_paren = strpbrk (open_paren + 1, "[{(")))
1533 close_paren = strpbrk (open_paren + 1, "]})");
1534 if (NULL == close_paren)
1536 switch (open_paren[0])
1539 if (']' == close_paren[0])
1543 if ('}' == close_paren[0])
1547 if (')' == close_paren[0])
1553 if (match && (close_paren - open_paren > 1))
1555 tmp = close_paren[0];
1556 close_paren[0] = '\0';
1557 /* Keywords must be at least 3 characters long */
1558 if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2)
1560 close_paren[0] = tmp;
1566 if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
1567 (const char **) array, index + count))
1569 insert_non_mandatory_keyword ((const char *) &open_paren[1], array,
1573 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1574 &open_paren[1], close_paren - &open_paren[1]);
1575 if (normalized != NULL)
1577 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1578 (const char **) array, index + count))
1580 insert_non_mandatory_keyword ((const char *) normalized, array,
1584 GNUNET_free (normalized);
1589 close_paren[0] = tmp;
1598 * Where to break up keywords
1600 #define TOKENS "_. /-!?#&+@\"\'\\;:,()[]{}$<>|"
1603 * Break the filename up by TOKENS to make
1606 * @param s string to break down.
1607 * @param array array to fill with tokens. If NULL, then tokens are only
1609 * @param index index at which to start filling the array (entries prior
1610 * to it are used to check for duplicates). ignored if @a array == NULL.
1611 * @return number of tokens (>1) counted (including duplicates), or number of
1612 * tokens extracted (excluding duplicates). 0 if there are no
1613 * separators in the string (when counting), or when all tokens were
1614 * duplicates (when extracting).
1617 get_keywords_from_tokens (const char *s,
1625 ss = GNUNET_strdup (s);
1626 for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS))
1628 /* Keywords must be at least 3 characters long */
1629 if (u8_strcount ((const uint8_t *) p) <= 2)
1634 if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps))
1636 insert_non_mandatory_keyword (p, array,
1640 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1642 if (normalized != NULL)
1644 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1645 (const char **) array, index + seps))
1647 insert_non_mandatory_keyword ((const char *) normalized, array,
1651 GNUNET_free (normalized);
1664 * Function called on each value in the meta data.
1665 * Adds it to the URI.
1667 * @param cls URI to update
1668 * @param plugin_name name of the plugin that produced this value;
1669 * special values can be used (i.e. '<zlib>' for zlib being
1670 * used in the main libextractor library and yielding
1672 * @param type libextractor-type describing the meta data
1673 * @param format basic format information about data
1674 * @param data_mime_type mime-type of data (not of the original file);
1675 * can be NULL (if mime-type is not known)
1676 * @param data actual meta-data found
1677 * @param data_len number of bytes in @a data
1678 * @return 0 (always)
1681 gather_uri_data (void *cls, const char *plugin_name,
1682 enum EXTRACTOR_MetaType type,
1683 enum EXTRACTOR_MetaFormat format,
1684 const char *data_mime_type,
1688 struct GNUNET_FS_Uri *uri = cls;
1689 char *normalized_data;
1692 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1693 (format != EXTRACTOR_METAFORMAT_C_STRING))
1695 /* Keywords must be at least 3 characters long
1696 * If given non-utf8 string it will, most likely, find it to be invalid,
1697 * and will return the length of its valid part, skipping the keyword.
1698 * If it does - fix the extractor, not this check!
1700 if (u8_strcount ((const uint8_t *) data) <= 2)
1702 if ( (EXTRACTOR_METATYPE_MIMETYPE == type) &&
1703 (NULL != (sep = memchr (data, '/', data_len))) &&
1708 GNUNET_asprintf (&xtra,
1712 if (! find_duplicate (xtra,
1713 (const char **) uri->data.ksk.keywords,
1714 uri->data.ksk.keywordCount))
1716 insert_non_mandatory_keyword (xtra,
1717 uri->data.ksk.keywords,
1718 uri->data.ksk.keywordCount);
1719 uri->data.ksk.keywordCount++;
1724 normalized_data = normalize_metadata (format, data, data_len);
1725 if (! find_duplicate (data,
1726 (const char **) uri->data.ksk.keywords,
1727 uri->data.ksk.keywordCount))
1729 insert_non_mandatory_keyword (data,
1730 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1731 uri->data.ksk.keywordCount++;
1733 if (NULL != normalized_data)
1735 if (! find_duplicate (normalized_data,
1736 (const char **) uri->data.ksk.keywords,
1737 uri->data.ksk.keywordCount))
1739 insert_non_mandatory_keyword (normalized_data,
1740 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1741 uri->data.ksk.keywordCount++;
1743 GNUNET_free (normalized_data);
1750 * Construct a keyword-URI from meta-data (take all entries
1751 * in the meta-data and construct one large keyword URI
1752 * that lists all keywords that can be found in the meta-data).
1754 * @param md metadata to use
1755 * @return NULL on error, otherwise a KSK URI
1757 struct GNUNET_FS_Uri *
1758 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1760 struct GNUNET_FS_Uri *ret;
1762 char *full_name = NULL;
1765 int tok_keywords = 0;
1766 int paren_keywords = 0;
1770 ret = GNUNET_new (struct GNUNET_FS_Uri);
1771 ret->type = GNUNET_FS_URI_KSK;
1772 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1775 full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
1776 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, -1);
1777 if (NULL != full_name)
1779 filename = full_name;
1780 while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR)))
1782 tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
1783 paren_keywords = get_keywords_from_parens (filename, NULL, 0);
1785 /* x3 because there might be a normalized variant of every keyword,
1786 plus theoretically one more for mime... */
1787 ret->data.ksk.keywords
1788 = GNUNET_new_array ((ent + tok_keywords + paren_keywords) * 3,
1790 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1792 if (tok_keywords > 0)
1793 ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
1794 ret->data.ksk.keywords,
1795 ret->data.ksk.keywordCount);
1796 if (paren_keywords > 0)
1797 ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
1798 ret->data.ksk.keywords,
1799 ret->data.ksk.keywordCount);
1801 GNUNET_free_non_null (full_name);
1807 * In URI-encoding, does the given character
1808 * need to be encoded using %-encoding?
1811 needs_percent (char c)
1814 ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') ||
1815 (c == '.') || (c == '~')));
1820 * Convert a KSK URI to a string.
1822 * @param uri the URI to convert
1823 * @return NULL on error (i.e. keywordCount == 0)
1826 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1829 unsigned int keywordCount;
1836 const char *keyword;
1838 if (uri->type != GNUNET_FS_URI_KSK)
1840 keywords = uri->data.ksk.keywords;
1841 keywordCount = uri->data.ksk.keywordCount;
1842 n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1843 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1844 for (i = 0; i < keywordCount; i++)
1846 keyword = keywords[i];
1847 slen = strlen (keyword);
1849 for (j = 0; j < slen; j++)
1851 if ((j == 0) && (keyword[j] == ' '))
1854 continue; /* skip leading space */
1856 if (needs_percent (keyword[j]))
1857 n += 2; /* will use %-encoding */
1860 ret = GNUNET_malloc (n);
1861 strcpy (ret, GNUNET_FS_URI_PREFIX);
1862 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1863 wpos = strlen (ret);
1864 for (i = 0; i < keywordCount; i++)
1866 keyword = keywords[i];
1867 slen = strlen (keyword);
1868 for (j = 0; j < slen; j++)
1870 if ((j == 0) && (keyword[j] == ' '))
1871 continue; /* skip leading space */
1872 if (needs_percent (keyword[j]))
1874 sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]);
1879 ret[wpos++] = keyword[j];
1882 if (i != keywordCount - 1)
1890 * Convert SKS URI to a string.
1892 * @param uri sks uri to convert
1893 * @return NULL on error
1896 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1901 if (GNUNET_FS_URI_SKS != uri->type)
1903 ret = GNUNET_STRINGS_data_to_string (&uri->data.sks.ns,
1904 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
1907 GNUNET_assert (NULL != ret);
1909 GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
1910 GNUNET_FS_URI_SKS_INFIX, buf,
1911 uri->data.sks.identifier);
1917 * Convert a CHK URI to a string.
1919 * @param uri chk uri to convert
1920 * @return NULL on error
1923 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1925 const struct FileIdentifier *fi;
1927 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1928 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1930 if (uri->type != GNUNET_FS_URI_CHK)
1932 fi = &uri->data.chk;
1933 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1934 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1936 GNUNET_asprintf (&ret, "%s%s%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1937 GNUNET_FS_URI_CHK_INFIX, (const char *) &keyhash,
1938 (const char *) &queryhash, GNUNET_ntohll (fi->file_length));
1944 * Convert a LOC URI to a string.
1946 * @param uri loc uri to convert
1947 * @return NULL on error
1950 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1953 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1954 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1956 char peer_sig[SIGNATURE_ASCII_LENGTH + 1];
1958 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1959 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1961 GNUNET_CRYPTO_eddsa_public_key_to_string (&uri->data.loc.peer.public_key);
1962 GNUNET_assert (NULL !=
1963 GNUNET_STRINGS_data_to_string (&uri->data.loc.contentSignature,
1964 sizeof (struct GNUNET_CRYPTO_EddsaSignature),
1966 sizeof (peer_sig)));
1967 GNUNET_asprintf (&ret,
1968 "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1969 GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
1970 (const char *) &queryhash,
1971 (unsigned long long) GNUNET_ntohll (uri->data.loc.
1975 (unsigned long long) uri->data.loc.expirationTime.abs_value_us / 1000000LL);
1976 GNUNET_free (peer_id);
1982 * Convert a URI to a UTF-8 String.
1984 * @param uri uri to convert to a string
1985 * @return the UTF-8 string
1988 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1997 case GNUNET_FS_URI_KSK:
1998 return uri_ksk_to_string (uri);
1999 case GNUNET_FS_URI_SKS:
2000 return uri_sks_to_string (uri);
2001 case GNUNET_FS_URI_CHK:
2002 return uri_chk_to_string (uri);
2003 case GNUNET_FS_URI_LOC:
2004 return uri_loc_to_string (uri);
2011 /* end of fs_uri.c */