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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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
99 * @return #GNUNET_OK on success
102 GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
103 struct GNUNET_HashCode *key)
107 case GNUNET_FS_URI_CHK:
108 *key = uri->data.chk.chk.query;
110 case GNUNET_FS_URI_SKS:
111 GNUNET_CRYPTO_hash (uri->data.sks.identifier,
112 strlen (uri->data.sks.identifier),
115 case GNUNET_FS_URI_KSK:
116 if (uri->data.ksk.keywordCount > 0)
118 GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
119 strlen (uri->data.ksk.keywords[0]),
125 memset (key, 0, sizeof (struct GNUNET_HashCode));
126 return GNUNET_SYSERR;
129 case GNUNET_FS_URI_LOC:
130 GNUNET_CRYPTO_hash (&uri->data.loc.fi,
131 sizeof (struct FileIdentifier) +
132 sizeof (struct GNUNET_PeerIdentity),
136 memset (key, 0, sizeof (struct GNUNET_HashCode));
137 return GNUNET_SYSERR;
143 * Convert keyword URI to a human readable format
144 * (i.e. the search query that was used in the first place)
146 * @param uri ksk uri to convert to a string
147 * @return string with the keywords
150 GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
157 unsigned int keywordCount;
159 if ((NULL == uri) || (GNUNET_FS_URI_KSK != uri->type))
164 keywords = uri->data.ksk.keywords;
165 keywordCount = uri->data.ksk.keywordCount;
166 n = keywordCount + 1;
167 for (i = 0; i < keywordCount; i++)
169 keyword = keywords[i];
170 n += strlen (keyword) - 1;
171 if (NULL != strstr (&keyword[1], " "))
173 if (keyword[0] == '+')
176 ret = GNUNET_malloc (n);
178 for (i = 0; i < keywordCount; i++)
180 keyword = keywords[i];
181 if (NULL != strstr (&keyword[1], " "))
184 if (keyword[0] == '+')
185 strcat (ret, keyword);
187 strcat (ret, &keyword[1]);
192 if (keyword[0] == '+')
193 strcat (ret, keyword);
195 strcat (ret, &keyword[1]);
204 * Given a keyword with %-encoding (and possibly quotes to protect
205 * spaces), return a copy of the keyword without %-encoding and
206 * without double-quotes (%22). Also, add a space at the beginning
207 * if there is not a '+'.
209 * @param in string with %-encoding
210 * @param emsg where to store the parser error message (if any)
211 * @return decodded string with leading space (or preserved plus)
214 percent_decode_keyword (const char *in,
223 out = GNUNET_strdup (in);
226 while (out[rpos] != '\0')
228 if (out[rpos] == '%')
230 if (1 != SSCANF (&out[rpos + 1], "%2X", &hx))
233 *emsg = GNUNET_strdup (_(/* xgettext:no-c-format */
234 "Malformed KSK URI (`%' must be followed by HEX number)"));
239 continue; /* skip double quote */
240 out[wpos++] = (char) hx;
244 out[wpos++] = out[rpos++];
250 ret = GNUNET_strdup (out);
254 /* need to prefix with space */
255 ret = GNUNET_malloc (strlen (out) + 2);
263 #define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
268 * @param s an uri string
269 * @param emsg where to store the parser error message (if any)
270 * @return NULL on error, otherwise the KSK URI
272 static struct GNUNET_FS_Uri *
273 uri_ksk_parse (const char *s,
276 struct GNUNET_FS_Uri *ret;
287 pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
288 if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX, pos)))
289 return NULL; /* not KSK URI */
290 if ((s[slen - 1] == '+') || (s[pos] == '+'))
293 GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
298 for (i = pos; i < slen; i++)
300 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
302 saw_quote = (saw_quote + 1) % 2;
306 if ((s[i] == '+') && (saw_quote == 0))
311 *emsg = GNUNET_strdup (_("Malformed KSK URI (`++' not allowed)"));
318 *emsg = GNUNET_strdup (_("Malformed KSK URI (quotes not balanced)"));
322 dup = GNUNET_strdup (s);
323 keywords = GNUNET_new_array (max,
325 for (i = slen - 1; i >= (int) pos; i--)
327 if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
329 saw_quote = (saw_quote + 1) % 2;
332 if ((dup[i] == '+') && (saw_quote == 0))
334 keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
335 if (NULL == keywords[max])
340 keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
341 if (NULL == keywords[max])
343 GNUNET_assert (0 == max);
345 ret = GNUNET_new (struct GNUNET_FS_Uri);
346 ret->type = GNUNET_FS_URI_KSK;
347 ret->data.ksk.keywordCount = iret;
348 ret->data.ksk.keywords = keywords;
351 for (i = 0; i < max; i++)
352 GNUNET_free_non_null (keywords[i]);
353 GNUNET_free (keywords);
359 #define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
364 * @param s an uri string
365 * @param emsg where to store the parser error message (if any)
366 * @return NULL on error, SKS URI otherwise
368 static struct GNUNET_FS_Uri *
369 uri_sks_parse (const char *s,
372 struct GNUNET_FS_Uri *ret;
373 struct GNUNET_CRYPTO_EcdsaPublicKey ns;
377 pos = strlen (GNUNET_FS_URI_SKS_PREFIX);
378 if ((strlen (s) <= pos) || (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX, pos)))
379 return NULL; /* not an SKS URI */
380 end = strchr (&s[pos], '/');
381 if ( (NULL == end) ||
383 GNUNET_STRINGS_string_to_data (&s[pos],
388 *emsg = GNUNET_strdup (_("Malformed SKS URI (wrong syntax)"));
389 return NULL; /* malformed */
391 end++; /* skip over '/' */
392 ret = GNUNET_new (struct GNUNET_FS_Uri);
393 ret->type = GNUNET_FS_URI_SKS;
394 ret->data.sks.ns = ns;
395 ret->data.sks.identifier = GNUNET_strdup (end);
399 #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
405 * @param s an uri string
406 * @param emsg where to store the parser error message (if any)
407 * @return NULL on error, CHK URI otherwise
409 static struct GNUNET_FS_Uri *
410 uri_chk_parse (const char *s,
413 struct GNUNET_FS_Uri *ret;
414 struct FileIdentifier fi;
416 unsigned long long flen;
418 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
419 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
422 pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
423 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
424 (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX, pos)))
425 return NULL; /* not a CHK URI */
426 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
427 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
429 *emsg = GNUNET_strdup (_("Malformed CHK URI (wrong syntax)"));
432 GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
433 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
434 GNUNET_memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
435 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
436 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
438 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &fi.chk.key)) ||
439 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &fi.chk.query)) ||
441 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
444 *emsg = GNUNET_strdup (_("Malformed CHK URI (failed to decode CHK)"));
447 fi.file_length = GNUNET_htonll (flen);
448 ret = GNUNET_new (struct GNUNET_FS_Uri);
449 ret->type = GNUNET_FS_URI_CHK;
455 GNUNET_NETWORK_STRUCT_BEGIN
457 * Structure that defines how the contents of a location URI must be
458 * assembled in memory to create or verify the signature of a location
461 struct LocUriAssembly
464 * What is being signed (rest of this struct).
466 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
469 * Expiration time of the offer.
471 struct GNUNET_TIME_AbsoluteNBO exptime;
474 * File being offered.
476 struct FileIdentifier fi;
479 * Peer offering the file.
481 struct GNUNET_PeerIdentity peer;
484 GNUNET_NETWORK_STRUCT_END
487 #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
489 #define SIGNATURE_ASCII_LENGTH 103
493 * Also verifies validity of the location URI.
495 * @param s an uri string
496 * @param emsg where to store the parser error message (if any)
497 * @return NULL on error, valid LOC URI otherwise
499 static struct GNUNET_FS_Uri *
500 uri_loc_parse (const char *s,
503 struct GNUNET_FS_Uri *uri;
504 char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
505 char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
508 unsigned long long exptime;
509 unsigned long long flen;
510 struct GNUNET_TIME_Absolute et;
511 struct GNUNET_CRYPTO_EddsaSignature sig;
512 struct LocUriAssembly ass;
516 pos = strlen (GNUNET_FS_URI_LOC_PREFIX);
517 if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
518 (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX, pos)))
519 return NULL; /* not a LOC URI */
520 if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
521 (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
523 *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax)"));
526 GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
527 h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
528 GNUNET_memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
529 sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
530 h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
532 if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &ass.fi.chk.key)) ||
533 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &ass.fi.chk.query)) ||
535 SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
538 *emsg = GNUNET_strdup (_("LOC URI malformed (no CHK)"));
541 ass.fi.file_length = GNUNET_htonll (flen);
543 npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
544 while ((s[npos] != '\0') && (s[npos] != '.'))
548 *emsg = GNUNET_strdup (_("LOC URI malformed (missing LOC)"));
552 if ( (strlen (&s[npos]) <= GNUNET_CRYPTO_PKEY_ASCII_LENGTH + 1) ||
553 ('.' != s[npos+GNUNET_CRYPTO_PKEY_ASCII_LENGTH]) )
556 GNUNET_strdup (_("LOC URI malformed (wrong syntax for public key)"));
559 GNUNET_CRYPTO_eddsa_public_key_from_string (&s[npos],
560 GNUNET_CRYPTO_PKEY_ASCII_LENGTH,
561 &ass.peer.public_key))
564 GNUNET_strdup (_("LOC URI malformed (could not decode public key)"));
567 npos += GNUNET_CRYPTO_PKEY_ASCII_LENGTH;
568 if (s[npos++] != '.')
570 *emsg = GNUNET_strdup (_("LOC URI malformed (could not find signature)"));
573 if ( (strlen (&s[npos]) <= SIGNATURE_ASCII_LENGTH + 1) ||
574 ('.' != s[npos + SIGNATURE_ASCII_LENGTH]) )
576 *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax for signature)"));
580 GNUNET_STRINGS_string_to_data (&s[npos],
581 SIGNATURE_ASCII_LENGTH,
583 sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
585 *emsg = GNUNET_strdup (_("LOC URI malformed (could not decode signature)"));
588 npos += SIGNATURE_ASCII_LENGTH;
589 if (s[npos++] != '.')
591 *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax for expiration time)"));
594 if (1 != SSCANF (&s[npos], "%llu", &exptime))
597 GNUNET_strdup (_("LOC URI malformed (could not parse expiration time)"));
600 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
601 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
602 et.abs_value_us = exptime * 1000LL * 1000LL;
603 ass.exptime = GNUNET_TIME_absolute_hton (et);
605 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
606 &ass.purpose, &sig, &ass.peer.public_key))
609 GNUNET_strdup (_("LOC URI malformed (signature failed validation)"));
612 uri = GNUNET_new (struct GNUNET_FS_Uri);
613 uri->type = GNUNET_FS_URI_LOC;
614 uri->data.loc.fi = ass.fi;
615 uri->data.loc.peer = ass.peer;
616 uri->data.loc.expirationTime = et;
617 uri->data.loc.contentSignature = sig;
626 * Convert a UTF-8 String to a URI.
628 * @param uri string to parse
629 * @param emsg where to store the parser error message (if any)
630 * @return NULL on error
632 struct GNUNET_FS_Uri *
633 GNUNET_FS_uri_parse (const char *uri,
636 struct GNUNET_FS_Uri *ret;
643 *emsg = GNUNET_strdup (_("invalid argument"));
649 if ((NULL != (ret = uri_chk_parse (uri, emsg))) ||
650 (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
651 (NULL != (ret = uri_sks_parse (uri, emsg))) ||
652 (NULL != (ret = uri_loc_parse (uri, emsg))))
655 *emsg = GNUNET_strdup (_("Unrecognized URI type"));
665 * @param uri uri to free
668 GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
674 case GNUNET_FS_URI_KSK:
675 for (i = 0; i < uri->data.ksk.keywordCount; i++)
676 GNUNET_free (uri->data.ksk.keywords[i]);
677 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0);
679 case GNUNET_FS_URI_SKS:
680 GNUNET_free (uri->data.sks.identifier);
682 case GNUNET_FS_URI_LOC:
693 * How many keywords are ANDed in this keyword URI?
695 * @param uri ksk uri to get the number of keywords from
696 * @return 0 if this is not a keyword URI
699 GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
701 if (uri->type != GNUNET_FS_URI_KSK)
703 return uri->data.ksk.keywordCount;
708 * Iterate over all keywords in this keyword URI.
710 * @param uri ksk uri to get the keywords from
711 * @param iterator function to call on each keyword
712 * @param iterator_cls closure for iterator
713 * @return -1 if this is not a keyword URI, otherwise number of
714 * keywords iterated over until iterator aborted
717 GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
718 GNUNET_FS_KeywordIterator iterator,
724 if (uri->type != GNUNET_FS_URI_KSK)
726 if (NULL == iterator)
727 return uri->data.ksk.keywordCount;
728 for (i = 0; i < uri->data.ksk.keywordCount; i++)
730 keyword = uri->data.ksk.keywords[i];
731 /* first character of keyword indicates
732 * if it is mandatory or not */
733 if (GNUNET_OK != iterator (iterator_cls, &keyword[1], keyword[0] == '+'))
741 * Add the given keyword to the set of keywords represented by the URI.
742 * Does nothing if the keyword is already present.
744 * @param uri ksk uri to modify
745 * @param keyword keyword to add
746 * @param is_mandatory is this keyword mandatory?
749 GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
757 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
758 for (i = 0; i < uri->data.ksk.keywordCount; i++)
760 old = uri->data.ksk.keywords[i];
761 if (0 == strcmp (&old[1], keyword))
764 GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword);
765 GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
770 * Remove the given keyword from the set of keywords represented by the URI.
771 * Does nothing if the keyword is not present.
773 * @param uri ksk uri to modify
774 * @param keyword keyword to add
777 GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
783 GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
784 for (i = 0; i < uri->data.ksk.keywordCount; i++)
786 old = uri->data.ksk.keywords[i];
787 if (0 == strcmp (&old[1], keyword))
789 uri->data.ksk.keywords[i] =
790 uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1];
791 GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
792 uri->data.ksk.keywordCount - 1);
801 * Obtain the identity of the peer offering the data
803 * @param uri the location URI to inspect
804 * @param peer where to store the identify of the peer (presumably) offering the content
805 * @return #GNUNET_SYSERR if this is not a location URI, otherwise #GNUNET_OK
808 GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
809 struct GNUNET_PeerIdentity *peer)
811 if (uri->type != GNUNET_FS_URI_LOC)
812 return GNUNET_SYSERR;
813 *peer = uri->data.loc.peer;
819 * Obtain the expiration of the LOC URI.
821 * @param uri location URI to get the expiration from
822 * @return expiration time of the URI
824 struct GNUNET_TIME_Absolute
825 GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
827 GNUNET_assert (uri->type == GNUNET_FS_URI_LOC);
828 return uri->data.loc.expirationTime;
833 * Obtain the URI of the content itself.
835 * @param uri location URI to get the content URI from
836 * @return NULL if argument is not a location URI
838 struct GNUNET_FS_Uri *
839 GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
841 struct GNUNET_FS_Uri *ret;
843 if (uri->type != GNUNET_FS_URI_LOC)
845 ret = GNUNET_new (struct GNUNET_FS_Uri);
846 ret->type = GNUNET_FS_URI_CHK;
847 ret->data.chk = uri->data.loc.fi;
853 * Construct a location URI (this peer will be used for the location).
854 * This function should only be called from within gnunet-service-fs,
855 * as it requires the peer's private key which is generally unavailable
856 * to processes directly under the user's control. However, for
857 * testing and as it logically fits under URIs, it is in this API.
859 * @param base_uri content offered by the sender
860 * @param sign_key private key of the peer
861 * @param expiration_time how long will the content be offered?
862 * @return the location URI, NULL on error
864 struct GNUNET_FS_Uri *
865 GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *base_uri,
866 const struct GNUNET_CRYPTO_EddsaPrivateKey *sign_key,
867 struct GNUNET_TIME_Absolute expiration_time)
869 struct GNUNET_FS_Uri *uri;
870 struct GNUNET_CRYPTO_EddsaPublicKey my_public_key;
871 struct LocUriAssembly ass;
872 struct GNUNET_TIME_Absolute et;
874 if (GNUNET_FS_URI_CHK != base_uri->type)
876 /* we round expiration time to full seconds for SKS URIs */
877 et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL;
878 GNUNET_CRYPTO_eddsa_key_get_public (sign_key,
880 ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
881 ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
882 ass.exptime = GNUNET_TIME_absolute_hton (et);
883 ass.fi = base_uri->data.chk;
884 ass.peer.public_key = my_public_key;
885 uri = GNUNET_new (struct GNUNET_FS_Uri);
886 uri->type = GNUNET_FS_URI_LOC;
887 uri->data.loc.fi = base_uri->data.chk;
888 uri->data.loc.expirationTime = et;
889 uri->data.loc.peer.public_key = my_public_key;
890 GNUNET_assert (GNUNET_OK ==
891 GNUNET_CRYPTO_eddsa_sign (sign_key,
893 &uri->data.loc.contentSignature));
899 * Create an SKS URI from a namespace ID and an identifier.
901 * @param ns namespace ID
902 * @param id identifier
903 * @return an FS URI for the given namespace and identifier
905 struct GNUNET_FS_Uri *
906 GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
909 struct GNUNET_FS_Uri *ns_uri;
911 ns_uri = GNUNET_new (struct GNUNET_FS_Uri);
912 ns_uri->type = GNUNET_FS_URI_SKS;
913 ns_uri->data.sks.ns = *ns;
914 ns_uri->data.sks.identifier = GNUNET_strdup (id);
920 * Merge the sets of keywords from two KSK URIs.
921 * (useful for merging the canonicalized keywords with
922 * the original keywords for sharing).
924 * @param u1 first uri
925 * @param u2 second uri
926 * @return merged URI, NULL on error
928 struct GNUNET_FS_Uri *
929 GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
930 const struct GNUNET_FS_Uri *u2)
932 struct GNUNET_FS_Uri *ret;
940 if ((u1 == NULL) && (u2 == NULL))
943 return GNUNET_FS_uri_dup (u2);
945 return GNUNET_FS_uri_dup (u1);
946 if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK))
951 kc = u1->data.ksk.keywordCount;
952 kl = GNUNET_new_array (kc + u2->data.ksk.keywordCount,
954 for (i = 0; i < u1->data.ksk.keywordCount; i++)
955 kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
956 for (i = 0; i < u2->data.ksk.keywordCount; i++)
958 kp = u2->data.ksk.keywords[i];
960 for (j = 0; j < u1->data.ksk.keywordCount; j++)
961 if (0 == strcmp (kp + 1, kl[j] + 1))
969 kl[kc++] = GNUNET_strdup (kp);
971 ret = GNUNET_new (struct GNUNET_FS_Uri);
972 ret->type = GNUNET_FS_URI_KSK;
973 ret->data.ksk.keywordCount = kc;
974 ret->data.ksk.keywords = kl;
982 * @param uri the URI to duplicate
983 * @return copy of the URI
985 struct GNUNET_FS_Uri *
986 GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
988 struct GNUNET_FS_Uri *ret;
993 ret = GNUNET_new (struct GNUNET_FS_Uri);
994 GNUNET_memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
997 case GNUNET_FS_URI_KSK:
998 if (ret->data.ksk.keywordCount >=
999 GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
1005 if (ret->data.ksk.keywordCount > 0)
1007 ret->data.ksk.keywords
1008 = GNUNET_new_array (ret->data.ksk.keywordCount,
1010 for (i = 0; i < ret->data.ksk.keywordCount; i++)
1011 ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]);
1014 ret->data.ksk.keywords = NULL; /* just to be sure */
1016 case GNUNET_FS_URI_SKS:
1017 ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
1019 case GNUNET_FS_URI_LOC:
1029 * Create an FS URI from a single user-supplied string of keywords.
1030 * The string is broken up at spaces into individual keywords.
1031 * Keywords that start with "+" are mandatory. Double-quotes can
1032 * be used to prevent breaking up strings at spaces (and also
1033 * to specify non-mandatory keywords starting with "+").
1035 * Keywords must contain a balanced number of double quotes and
1036 * double quotes can not be used in the actual keywords (for
1037 * example, the string '""foo bar""' will be turned into two
1038 * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
1040 * @param keywords the keyword string
1041 * @param emsg where to store an error message
1042 * @return an FS URI for the given keywords, NULL
1043 * if keywords is not legal (i.e. empty).
1045 struct GNUNET_FS_Uri *
1046 GNUNET_FS_uri_ksk_create (const char *keywords,
1050 unsigned int num_Words;
1053 struct GNUNET_FS_Uri *uri;
1057 if (keywords == NULL)
1059 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1063 searchString = GNUNET_strdup (keywords);
1068 while ('\0' != *pos)
1070 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1074 else if (0 == inWord)
1080 saw_quote = (saw_quote + 1) % 2;
1085 GNUNET_free (searchString);
1086 *emsg = GNUNET_strdup (_("No keywords specified!\n"));
1091 GNUNET_free (searchString);
1092 *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
1095 keywordarr = GNUNET_new_array (num_Words,
1100 while ('\0' != *pos)
1102 if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
1107 else if (0 == inWord)
1109 keywordarr[num_Words] = pos;
1114 saw_quote = (saw_quote + 1) % 2;
1118 GNUNET_FS_uri_ksk_create_from_args (num_Words,
1119 (const char **) keywordarr);
1120 GNUNET_free (keywordarr);
1121 GNUNET_free (searchString);
1127 * Create an FS URI from a user-supplied command line of keywords.
1128 * Arguments should start with "+" to indicate mandatory
1131 * @param argc number of keywords
1132 * @param argv keywords (double quotes are not required for
1133 * keywords containing spaces; however, double
1134 * quotes are required for keywords starting with
1135 * "+"); there is no mechanism for having double
1136 * quotes in the actual keywords (if the user
1137 * did specifically specify double quotes, the
1138 * caller should convert each double quote
1139 * into two single quotes).
1140 * @return an FS URI for the given keywords, NULL
1141 * if keywords is not legal (i.e. empty).
1143 struct GNUNET_FS_Uri *
1144 GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
1148 struct GNUNET_FS_Uri *uri;
1149 const char *keyword;
1157 /* allow URI to be given as one and only keyword and
1158 * handle accordingly */
1160 if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) &&
1162 strncmp (argv[0], GNUNET_FS_URI_PREFIX, strlen (GNUNET_FS_URI_PREFIX)))
1163 && (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg))))
1165 GNUNET_free_non_null (emsg);
1166 uri = GNUNET_new (struct GNUNET_FS_Uri);
1167 uri->type = GNUNET_FS_URI_KSK;
1168 uri->data.ksk.keywordCount = argc;
1169 uri->data.ksk.keywords = GNUNET_new_array (argc,
1171 for (i = 0; i < argc; i++)
1174 if (keyword[0] == '+')
1175 val = GNUNET_strdup (keyword);
1177 GNUNET_asprintf (&val, " %s", keyword);
1188 uri->data.ksk.keywords[i] = val;
1195 * Test if two URIs are equal.
1197 * @param u1 one of the URIs
1198 * @param u2 the other URI
1199 * @return #GNUNET_YES if the URIs are equal
1202 GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
1203 const struct GNUNET_FS_Uri *u2)
1209 GNUNET_assert (u1 != NULL);
1210 GNUNET_assert (u2 != NULL);
1211 if (u1->type != u2->type)
1215 case GNUNET_FS_URI_CHK:
1217 memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier)))
1220 case GNUNET_FS_URI_SKS:
1222 memcmp (&u1->data.sks.ns, &u2->data.sks.ns,
1223 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) &&
1224 (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
1228 case GNUNET_FS_URI_KSK:
1229 if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
1231 for (i = 0; i < u1->data.ksk.keywordCount; i++)
1234 for (j = 0; j < u2->data.ksk.keywordCount; j++)
1236 if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
1242 if (ret == GNUNET_NO)
1246 case GNUNET_FS_URI_LOC:
1248 (&u1->data.loc, &u2->data.loc,
1249 sizeof (struct FileIdentifier) +
1250 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
1251 sizeof (struct GNUNET_TIME_Absolute) + sizeof (unsigned short) +
1252 sizeof (unsigned short)) != 0)
1262 * Is this a namespace URI?
1264 * @param uri the uri to check
1265 * @return #GNUNET_YES if this is an SKS uri
1268 GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
1270 return uri->type == GNUNET_FS_URI_SKS;
1275 * Get the ID of a namespace from the given
1278 * @param uri the uri to get the namespace ID from
1279 * @param pseudonym where to store the ID of the namespace
1280 * @return #GNUNET_OK on success
1283 GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
1284 struct GNUNET_CRYPTO_EcdsaPublicKey *pseudonym)
1286 if (!GNUNET_FS_uri_test_sks (uri))
1289 return GNUNET_SYSERR;
1291 *pseudonym = uri->data.sks.ns;
1297 * Get the content identifier of an SKS URI.
1299 * @param uri the sks uri
1300 * @return NULL on error (not a valid SKS URI)
1303 GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
1305 if (!GNUNET_FS_uri_test_sks (uri))
1310 return GNUNET_strdup (uri->data.sks.identifier);
1315 * Is this a keyword URI?
1317 * @param uri the uri
1318 * @return #GNUNET_YES if this is a KSK uri
1321 GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
1326 if (uri->type == GNUNET_FS_URI_KSK)
1328 for (i=0;i < uri->data.ksk.keywordCount; i++)
1329 GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
1332 return uri->type == GNUNET_FS_URI_KSK;
1337 * Is this a file (or directory) URI?
1339 * @param uri the uri to check
1340 * @return #GNUNET_YES if this is a CHK uri
1343 GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
1345 return uri->type == GNUNET_FS_URI_CHK;
1350 * What is the size of the file that this URI
1353 * @param uri the CHK URI to inspect
1354 * @return size of the file as specified in the CHK URI
1357 GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri * uri)
1361 case GNUNET_FS_URI_CHK:
1362 return GNUNET_ntohll (uri->data.chk.file_length);
1363 case GNUNET_FS_URI_LOC:
1364 return GNUNET_ntohll (uri->data.loc.fi.file_length);
1368 return 0; /* unreachable */
1373 * Is this a location URI?
1375 * @param uri the uri to check
1376 * @return #GNUNET_YES if this is a LOC uri
1379 GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
1381 return uri->type == GNUNET_FS_URI_LOC;
1386 * Add a keyword as non-mandatory (with ' '-prefix) to the
1387 * given keyword list at offset 'index'. The array is
1388 * guaranteed to be long enough.
1390 * @param s keyword to add
1391 * @param array array to add the keyword to
1392 * @param index offset where to add the keyword
1395 insert_non_mandatory_keyword (const char *s,
1401 GNUNET_asprintf (&nkword,
1402 " %s", /* space to mark as 'non mandatory' */
1404 array[index] = nkword;
1409 * Test if the given keyword @a s is already present in the
1410 * given array, ignoring the '+'-mandatory prefix in the array.
1412 * @param s keyword to test
1413 * @param array keywords to test against, with ' ' or '+' prefix to ignore
1414 * @param array_length length of the @a array
1415 * @return #GNUNET_YES if the keyword exists, #GNUNET_NO if not
1418 find_duplicate (const char *s,
1424 for (j = array_length - 1; j >= 0; j--)
1425 if (0 == strcmp (&array[j][1], s))
1435 normalize_metadata (enum EXTRACTOR_MetaFormat format,
1439 uint8_t *free_str = NULL;
1440 uint8_t *str_to_normalize = (uint8_t *) data;
1441 uint8_t *normalized;
1443 if (str_to_normalize == NULL)
1445 /* Don't trust libextractor */
1446 if (format == EXTRACTOR_METAFORMAT_UTF8)
1448 free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len);
1449 if (free_str == NULL)
1452 format = EXTRACTOR_METAFORMAT_C_STRING;
1454 if (format == EXTRACTOR_METAFORMAT_C_STRING)
1456 free_str = u8_strconv_from_encoding (data, locale_charset (), iconveh_escape_sequence);
1457 if (free_str == NULL)
1461 normalized = u8_tolower (str_to_normalize, strlen ((char *) str_to_normalize), NULL, UNINORM_NFD, NULL, &r_len);
1462 /* free_str is allocated by libunistring internally, use free() */
1463 if (free_str != NULL)
1465 if (normalized != NULL)
1467 /* u8_tolower allocates a non-NULL-terminated string! */
1468 free_str = GNUNET_malloc (r_len + 1);
1469 GNUNET_memcpy (free_str, normalized, r_len);
1470 free_str[r_len] = '\0';
1472 normalized = free_str;
1474 return (char *) normalized;
1479 * Counts the number of UTF-8 characters (not bytes) in the string,
1480 * returns that count.
1483 u8_strcount (const uint8_t *s)
1487 GNUNET_assert (s != NULL);
1490 for (count = 0; s != NULL; count++)
1491 s = u8_next (&c, s);
1497 * Break the filename up by matching [], () and {} pairs to make
1498 * keywords. In case of nesting parentheses only the inner pair counts.
1499 * You can't escape parentheses to scan something like "[blah\{foo]" to
1500 * make a "blah{foo" keyword, this function is only a heuristic!
1502 * @param s string to break down.
1503 * @param array array to fill with enclosed tokens. If NULL, then tokens
1505 * @param index index at which to start filling the array (entries prior
1506 * to it are used to check for duplicates). ignored if @a array == NULL.
1507 * @return number of tokens counted (including duplicates), or number of
1508 * tokens extracted (excluding duplicates). 0 if there are no
1509 * matching parens in the string (when counting), or when all tokens
1510 * were duplicates (when extracting).
1513 get_keywords_from_parens (const char *s,
1525 ss = GNUNET_strdup (s);
1526 open_paren = ss - 1;
1527 while (NULL != (open_paren = strpbrk (open_paren + 1, "[{(")))
1531 close_paren = strpbrk (open_paren + 1, "]})");
1532 if (NULL == close_paren)
1534 switch (open_paren[0])
1537 if (']' == close_paren[0])
1541 if ('}' == close_paren[0])
1545 if (')' == close_paren[0])
1551 if (match && (close_paren - open_paren > 1))
1553 tmp = close_paren[0];
1554 close_paren[0] = '\0';
1555 /* Keywords must be at least 3 characters long */
1556 if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2)
1558 close_paren[0] = tmp;
1564 if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
1565 (const char **) array, index + count))
1567 insert_non_mandatory_keyword ((const char *) &open_paren[1], array,
1571 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1572 &open_paren[1], close_paren - &open_paren[1]);
1573 if (normalized != NULL)
1575 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1576 (const char **) array, index + count))
1578 insert_non_mandatory_keyword ((const char *) normalized, array,
1582 GNUNET_free (normalized);
1587 close_paren[0] = tmp;
1596 * Where to break up keywords
1598 #define TOKENS "_. /-!?#&+@\"\'\\;:,()[]{}$<>|"
1601 * Break the filename up by TOKENS to make
1604 * @param s string to break down.
1605 * @param array array to fill with tokens. If NULL, then tokens are only
1607 * @param index index at which to start filling the array (entries prior
1608 * to it are used to check for duplicates). ignored if @a array == NULL.
1609 * @return number of tokens (>1) counted (including duplicates), or number of
1610 * tokens extracted (excluding duplicates). 0 if there are no
1611 * separators in the string (when counting), or when all tokens were
1612 * duplicates (when extracting).
1615 get_keywords_from_tokens (const char *s,
1623 ss = GNUNET_strdup (s);
1624 for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS))
1626 /* Keywords must be at least 3 characters long */
1627 if (u8_strcount ((const uint8_t *) p) <= 2)
1632 if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps))
1634 insert_non_mandatory_keyword (p, array,
1638 normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
1640 if (normalized != NULL)
1642 if (GNUNET_NO == find_duplicate ((const char *) normalized,
1643 (const char **) array, index + seps))
1645 insert_non_mandatory_keyword ((const char *) normalized, array,
1649 GNUNET_free (normalized);
1662 * Function called on each value in the meta data.
1663 * Adds it to the URI.
1665 * @param cls URI to update
1666 * @param plugin_name name of the plugin that produced this value;
1667 * special values can be used (i.e. '<zlib>' for zlib being
1668 * used in the main libextractor library and yielding
1670 * @param type libextractor-type describing the meta data
1671 * @param format basic format information about data
1672 * @param data_mime_type mime-type of data (not of the original file);
1673 * can be NULL (if mime-type is not known)
1674 * @param data actual meta-data found
1675 * @param data_len number of bytes in @a data
1676 * @return 0 (always)
1679 gather_uri_data (void *cls, const char *plugin_name,
1680 enum EXTRACTOR_MetaType type,
1681 enum EXTRACTOR_MetaFormat format,
1682 const char *data_mime_type,
1686 struct GNUNET_FS_Uri *uri = cls;
1687 char *normalized_data;
1690 if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
1691 (format != EXTRACTOR_METAFORMAT_C_STRING))
1693 /* Keywords must be at least 3 characters long
1694 * If given non-utf8 string it will, most likely, find it to be invalid,
1695 * and will return the length of its valid part, skipping the keyword.
1696 * If it does - fix the extractor, not this check!
1698 if (u8_strcount ((const uint8_t *) data) <= 2)
1700 if ( (EXTRACTOR_METATYPE_MIMETYPE == type) &&
1701 (NULL != (sep = memchr (data, '/', data_len))) &&
1706 GNUNET_asprintf (&xtra,
1710 if (! find_duplicate (xtra,
1711 (const char **) uri->data.ksk.keywords,
1712 uri->data.ksk.keywordCount))
1714 insert_non_mandatory_keyword (xtra,
1715 uri->data.ksk.keywords,
1716 uri->data.ksk.keywordCount);
1717 uri->data.ksk.keywordCount++;
1722 normalized_data = normalize_metadata (format, data, data_len);
1723 if (! find_duplicate (data,
1724 (const char **) uri->data.ksk.keywords,
1725 uri->data.ksk.keywordCount))
1727 insert_non_mandatory_keyword (data,
1728 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1729 uri->data.ksk.keywordCount++;
1731 if (NULL != normalized_data)
1733 if (! find_duplicate (normalized_data,
1734 (const char **) uri->data.ksk.keywords,
1735 uri->data.ksk.keywordCount))
1737 insert_non_mandatory_keyword (normalized_data,
1738 uri->data.ksk.keywords, uri->data.ksk.keywordCount);
1739 uri->data.ksk.keywordCount++;
1741 GNUNET_free (normalized_data);
1748 * Construct a keyword-URI from meta-data (take all entries
1749 * in the meta-data and construct one large keyword URI
1750 * that lists all keywords that can be found in the meta-data).
1752 * @param md metadata to use
1753 * @return NULL on error, otherwise a KSK URI
1755 struct GNUNET_FS_Uri *
1756 GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
1758 struct GNUNET_FS_Uri *ret;
1760 char *full_name = NULL;
1763 int tok_keywords = 0;
1764 int paren_keywords = 0;
1768 ret = GNUNET_new (struct GNUNET_FS_Uri);
1769 ret->type = GNUNET_FS_URI_KSK;
1770 ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
1773 full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
1774 EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, -1);
1775 if (NULL != full_name)
1777 filename = full_name;
1778 while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR)))
1780 tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
1781 paren_keywords = get_keywords_from_parens (filename, NULL, 0);
1783 /* x3 because there might be a normalized variant of every keyword,
1784 plus theoretically one more for mime... */
1785 ret->data.ksk.keywords
1786 = GNUNET_new_array ((ent + tok_keywords + paren_keywords) * 3,
1788 GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
1790 if (tok_keywords > 0)
1791 ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
1792 ret->data.ksk.keywords,
1793 ret->data.ksk.keywordCount);
1794 if (paren_keywords > 0)
1795 ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
1796 ret->data.ksk.keywords,
1797 ret->data.ksk.keywordCount);
1799 GNUNET_free_non_null (full_name);
1805 * In URI-encoding, does the given character
1806 * need to be encoded using %-encoding?
1809 needs_percent (char c)
1812 ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') ||
1813 (c == '.') || (c == '~')));
1818 * Convert a KSK URI to a string.
1820 * @param uri the URI to convert
1821 * @return NULL on error (i.e. keywordCount == 0)
1824 uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
1827 unsigned int keywordCount;
1834 const char *keyword;
1836 if (uri->type != GNUNET_FS_URI_KSK)
1838 keywords = uri->data.ksk.keywords;
1839 keywordCount = uri->data.ksk.keywordCount;
1840 n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
1841 strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
1842 for (i = 0; i < keywordCount; i++)
1844 keyword = keywords[i];
1845 slen = strlen (keyword);
1847 for (j = 0; j < slen; j++)
1849 if ((j == 0) && (keyword[j] == ' '))
1852 continue; /* skip leading space */
1854 if (needs_percent (keyword[j]))
1855 n += 2; /* will use %-encoding */
1858 ret = GNUNET_malloc (n);
1859 strcpy (ret, GNUNET_FS_URI_PREFIX);
1860 strcat (ret, GNUNET_FS_URI_KSK_INFIX);
1861 wpos = strlen (ret);
1862 for (i = 0; i < keywordCount; i++)
1864 keyword = keywords[i];
1865 slen = strlen (keyword);
1866 for (j = 0; j < slen; j++)
1868 if ((j == 0) && (keyword[j] == ' '))
1869 continue; /* skip leading space */
1870 if (needs_percent (keyword[j]))
1872 sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]);
1877 ret[wpos++] = keyword[j];
1880 if (i != keywordCount - 1)
1888 * Convert SKS URI to a string.
1890 * @param uri sks uri to convert
1891 * @return NULL on error
1894 uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
1899 if (GNUNET_FS_URI_SKS != uri->type)
1901 ret = GNUNET_STRINGS_data_to_string (&uri->data.sks.ns,
1902 sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
1905 GNUNET_assert (NULL != ret);
1907 GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
1908 GNUNET_FS_URI_SKS_INFIX, buf,
1909 uri->data.sks.identifier);
1915 * Convert a CHK URI to a string.
1917 * @param uri chk uri to convert
1918 * @return NULL on error
1921 uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
1923 const struct FileIdentifier *fi;
1925 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1926 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1928 if (uri->type != GNUNET_FS_URI_CHK)
1930 fi = &uri->data.chk;
1931 GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
1932 GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
1934 GNUNET_asprintf (&ret, "%s%s%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1935 GNUNET_FS_URI_CHK_INFIX, (const char *) &keyhash,
1936 (const char *) &queryhash, GNUNET_ntohll (fi->file_length));
1942 * Convert a LOC URI to a string.
1944 * @param uri loc uri to convert
1945 * @return NULL on error
1948 uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
1951 struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
1952 struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
1954 char peer_sig[SIGNATURE_ASCII_LENGTH + 1];
1956 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
1957 GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
1959 GNUNET_CRYPTO_eddsa_public_key_to_string (&uri->data.loc.peer.public_key);
1960 GNUNET_assert (NULL !=
1961 GNUNET_STRINGS_data_to_string (&uri->data.loc.contentSignature,
1962 sizeof (struct GNUNET_CRYPTO_EddsaSignature),
1964 sizeof (peer_sig)));
1965 GNUNET_asprintf (&ret,
1966 "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
1967 GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
1968 (const char *) &queryhash,
1969 (unsigned long long) GNUNET_ntohll (uri->data.loc.
1973 (unsigned long long) uri->data.loc.expirationTime.abs_value_us / 1000000LL);
1974 GNUNET_free (peer_id);
1980 * Convert a URI to a UTF-8 String.
1982 * @param uri uri to convert to a string
1983 * @return the UTF-8 string
1986 GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
1995 case GNUNET_FS_URI_KSK:
1996 return uri_ksk_to_string (uri);
1997 case GNUNET_FS_URI_SKS:
1998 return uri_sks_to_string (uri);
1999 case GNUNET_FS_URI_CHK:
2000 return uri_chk_to_string (uri);
2001 case GNUNET_FS_URI_LOC:
2002 return uri_loc_to_string (uri);
2009 /* end of fs_uri.c */