fs_uri.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2003--2014 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fs/fs_uri.c
  18. * @brief Parses and produces uri strings.
  19. * @author Igor Wronsky, Christian Grothoff
  20. *
  21. * GNUnet URIs are of the general form "gnunet://MODULE/IDENTIFIER".
  22. * The specific structure of "IDENTIFIER" depends on the module and
  23. * maybe differenciated into additional subcategories if applicable.
  24. * This module only deals with fs identifiers (MODULE = "fs").
  25. * <p>
  26. *
  27. * This module only parses URIs for the AFS module. The FS URIs fall
  28. * into four categories, "chk", "sks", "ksk" and "loc". The first three
  29. * categories were named in analogy (!) to Freenet, but they do NOT
  30. * work in exactly the same way. They are very similar from the user's
  31. * point of view (unique file identifier, subspace, keyword), but the
  32. * implementation is rather different in pretty much every detail.
  33. * The concrete URI formats are:
  34. *
  35. * </p>
  36. *
  37. * <ul><li>
  38. *
  39. * First, there are URIs that identify a file. They have the format
  40. * "gnunet://fs/chk/HEX1.HEX2.SIZE". These URIs can be used to
  41. * download the file. The description, filename, mime-type and other
  42. * meta-data is NOT part of the file-URI since a URI uniquely
  43. * identifies a resource (and the contents of the file would be the
  44. * same even if it had a different description).
  45. *
  46. * </li><li>
  47. *
  48. * The second category identifies entries in a namespace. The format
  49. * is "gnunet://fs/sks/NAMESPACE/IDENTIFIER" where the namespace
  50. * should be given in HEX. Applications may allow using a nickname
  51. * for the namespace if the nickname is not ambiguous. The identifier
  52. * can be either an ASCII sequence or a HEX-encoding. If the
  53. * identifier is in ASCII but the format is ambiguous and could denote
  54. * a HEX-string a "/" is appended to indicate ASCII encoding.
  55. *
  56. * </li> <li>
  57. *
  58. * The third category identifies ordinary searches. The format is
  59. * "gnunet://fs/ksk/KEYWORD[+KEYWORD]*". Using the "+" syntax
  60. * it is possible to encode searches with the boolean "AND" operator.
  61. * "+" is used since it indicates a commutative 'and' operation and
  62. * is unlikely to be used in a keyword by itself.
  63. *
  64. * </li><li>
  65. *
  66. * The last category identifies a datum on a specific machine. The
  67. * format is "gnunet://fs/loc/HEX1.HEX2.SIZE.PEER.SIG.EXPTIME". PEER is
  68. * the BinName of the public key of the peer storing the datum. The
  69. * signature (SIG) certifies that this peer has this content.
  70. * HEX1, HEX2 and SIZE correspond to a 'chk' URI.
  71. *
  72. * </li></ul>
  73. *
  74. * The encoding for hexadecimal values is defined in the hashing.c
  75. * module in the gnunetutil library and discussed there.
  76. *
  77. */
  78. #include "platform.h"
  79. #include "gnunet_fs_service.h"
  80. #include "gnunet_signatures.h"
  81. #include "fs_api.h"
  82. #include <unitypes.h>
  83. #include <unicase.h>
  84. #include <uniconv.h>
  85. #include <unistr.h>
  86. #include <unistdio.h>
  87. /**
  88. * Get a unique key from a URI. This is for putting URIs
  89. * into HashMaps. The key may change between FS implementations.
  90. *
  91. * @param uri uri to convert to a unique key
  92. * @param key where to store the unique key
  93. * @return #GNUNET_OK on success
  94. */
  95. int
  96. GNUNET_FS_uri_to_key (const struct GNUNET_FS_Uri *uri,
  97. struct GNUNET_HashCode *key)
  98. {
  99. switch (uri->type)
  100. {
  101. case GNUNET_FS_URI_CHK:
  102. *key = uri->data.chk.chk.query;
  103. return GNUNET_OK;
  104. case GNUNET_FS_URI_SKS:
  105. GNUNET_CRYPTO_hash (uri->data.sks.identifier,
  106. strlen (uri->data.sks.identifier),
  107. key);
  108. return GNUNET_OK;
  109. case GNUNET_FS_URI_KSK:
  110. if (uri->data.ksk.keywordCount > 0)
  111. {
  112. GNUNET_CRYPTO_hash (uri->data.ksk.keywords[0],
  113. strlen (uri->data.ksk.keywords[0]),
  114. key);
  115. return GNUNET_OK;
  116. }
  117. else
  118. {
  119. memset (key, 0, sizeof (struct GNUNET_HashCode));
  120. return GNUNET_SYSERR;
  121. }
  122. break;
  123. case GNUNET_FS_URI_LOC:
  124. GNUNET_CRYPTO_hash (&uri->data.loc.fi,
  125. sizeof (struct FileIdentifier) +
  126. sizeof (struct GNUNET_PeerIdentity),
  127. key);
  128. return GNUNET_OK;
  129. default:
  130. memset (key, 0, sizeof (struct GNUNET_HashCode));
  131. return GNUNET_SYSERR;
  132. }
  133. }
  134. /**
  135. * Convert keyword URI to a human readable format
  136. * (i.e. the search query that was used in the first place)
  137. *
  138. * @param uri ksk uri to convert to a string
  139. * @return string with the keywords
  140. */
  141. char *
  142. GNUNET_FS_uri_ksk_to_string_fancy (const struct GNUNET_FS_Uri *uri)
  143. {
  144. size_t n;
  145. char *ret;
  146. unsigned int i;
  147. const char *keyword;
  148. char **keywords;
  149. unsigned int keywordCount;
  150. if ((NULL == uri) || (GNUNET_FS_URI_KSK != uri->type))
  151. {
  152. GNUNET_break (0);
  153. return NULL;
  154. }
  155. keywords = uri->data.ksk.keywords;
  156. keywordCount = uri->data.ksk.keywordCount;
  157. n = keywordCount + 1;
  158. for (i = 0; i < keywordCount; i++)
  159. {
  160. keyword = keywords[i];
  161. n += strlen (keyword) - 1;
  162. if (NULL != strstr (&keyword[1], " "))
  163. n += 2;
  164. if (keyword[0] == '+')
  165. n++;
  166. }
  167. ret = GNUNET_malloc (n);
  168. strcpy (ret, "");
  169. for (i = 0; i < keywordCount; i++)
  170. {
  171. keyword = keywords[i];
  172. if (NULL != strstr (&keyword[1], " "))
  173. {
  174. strcat (ret, "\"");
  175. if (keyword[0] == '+')
  176. strcat (ret, keyword);
  177. else
  178. strcat (ret, &keyword[1]);
  179. strcat (ret, "\"");
  180. }
  181. else
  182. {
  183. if (keyword[0] == '+')
  184. strcat (ret, keyword);
  185. else
  186. strcat (ret, &keyword[1]);
  187. }
  188. strcat (ret, " ");
  189. }
  190. return ret;
  191. }
  192. /**
  193. * Given a keyword with %-encoding (and possibly quotes to protect
  194. * spaces), return a copy of the keyword without %-encoding and
  195. * without double-quotes (%22). Also, add a space at the beginning
  196. * if there is not a '+'.
  197. *
  198. * @param in string with %-encoding
  199. * @param emsg where to store the parser error message (if any)
  200. * @return decodded string with leading space (or preserved plus)
  201. */
  202. static char *
  203. percent_decode_keyword (const char *in,
  204. char **emsg)
  205. {
  206. char *out;
  207. char *ret;
  208. unsigned int rpos;
  209. unsigned int wpos;
  210. unsigned int hx;
  211. out = GNUNET_strdup (in);
  212. rpos = 0;
  213. wpos = 0;
  214. while (out[rpos] != '\0')
  215. {
  216. if (out[rpos] == '%')
  217. {
  218. if (1 != SSCANF (&out[rpos + 1], "%2X", &hx))
  219. {
  220. GNUNET_free (out);
  221. *emsg = GNUNET_strdup (_(/* xgettext:no-c-format */
  222. "Malformed KSK URI (`%' must be followed by HEX number)"));
  223. return NULL;
  224. }
  225. rpos += 3;
  226. if (hx == '"')
  227. continue; /* skip double quote */
  228. out[wpos++] = (char) hx;
  229. }
  230. else
  231. {
  232. out[wpos++] = out[rpos++];
  233. }
  234. }
  235. out[wpos] = '\0';
  236. if (out[0] == '+')
  237. {
  238. ret = GNUNET_strdup (out);
  239. }
  240. else
  241. {
  242. /* need to prefix with space */
  243. ret = GNUNET_malloc (strlen (out) + 2);
  244. strcpy (ret, " ");
  245. strcat (ret, out);
  246. }
  247. GNUNET_free (out);
  248. return ret;
  249. }
  250. #define GNUNET_FS_URI_KSK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_KSK_INFIX
  251. /**
  252. * Parse a KSK URI.
  253. *
  254. * @param s an uri string
  255. * @param emsg where to store the parser error message (if any)
  256. * @return NULL on error, otherwise the KSK URI
  257. */
  258. static struct GNUNET_FS_Uri *
  259. uri_ksk_parse (const char *s,
  260. char **emsg)
  261. {
  262. struct GNUNET_FS_Uri *ret;
  263. char **keywords;
  264. unsigned int pos;
  265. int max;
  266. int iret;
  267. int i;
  268. size_t slen;
  269. char *dup;
  270. int saw_quote;
  271. slen = strlen (s);
  272. pos = strlen (GNUNET_FS_URI_KSK_PREFIX);
  273. if ((slen <= pos) || (0 != strncmp (s, GNUNET_FS_URI_KSK_PREFIX, pos)))
  274. return NULL; /* not KSK URI */
  275. if ((s[slen - 1] == '+') || (s[pos] == '+'))
  276. {
  277. *emsg =
  278. GNUNET_strdup (_("Malformed KSK URI (must not begin or end with `+')"));
  279. return NULL;
  280. }
  281. max = 1;
  282. saw_quote = 0;
  283. for (i = pos; i < slen; i++)
  284. {
  285. if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
  286. {
  287. saw_quote = (saw_quote + 1) % 2;
  288. i += 3;
  289. continue;
  290. }
  291. if ((s[i] == '+') && (saw_quote == 0))
  292. {
  293. max++;
  294. if (s[i - 1] == '+')
  295. {
  296. *emsg = GNUNET_strdup (_("Malformed KSK URI (`++' not allowed)"));
  297. return NULL;
  298. }
  299. }
  300. }
  301. if (saw_quote == 1)
  302. {
  303. *emsg = GNUNET_strdup (_("Malformed KSK URI (quotes not balanced)"));
  304. return NULL;
  305. }
  306. iret = max;
  307. dup = GNUNET_strdup (s);
  308. keywords = GNUNET_new_array (max,
  309. char *);
  310. for (i = slen - 1; i >= (int) pos; i--)
  311. {
  312. if ((s[i] == '%') && (&s[i] == strstr (&s[i], "%22")))
  313. {
  314. saw_quote = (saw_quote + 1) % 2;
  315. continue;
  316. }
  317. if ((dup[i] == '+') && (saw_quote == 0))
  318. {
  319. keywords[--max] = percent_decode_keyword (&dup[i + 1], emsg);
  320. if (NULL == keywords[max])
  321. goto CLEANUP;
  322. dup[i] = '\0';
  323. }
  324. }
  325. keywords[--max] = percent_decode_keyword (&dup[pos], emsg);
  326. if (NULL == keywords[max])
  327. goto CLEANUP;
  328. GNUNET_assert (0 == max);
  329. GNUNET_free (dup);
  330. ret = GNUNET_new (struct GNUNET_FS_Uri);
  331. ret->type = GNUNET_FS_URI_KSK;
  332. ret->data.ksk.keywordCount = iret;
  333. ret->data.ksk.keywords = keywords;
  334. return ret;
  335. CLEANUP:
  336. for (i = 0; i < max; i++)
  337. GNUNET_free_non_null (keywords[i]);
  338. GNUNET_free (keywords);
  339. GNUNET_free (dup);
  340. return NULL;
  341. }
  342. #define GNUNET_FS_URI_SKS_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_SKS_INFIX
  343. /**
  344. * Parse an SKS URI.
  345. *
  346. * @param s an uri string
  347. * @param emsg where to store the parser error message (if any)
  348. * @return NULL on error, SKS URI otherwise
  349. */
  350. static struct GNUNET_FS_Uri *
  351. uri_sks_parse (const char *s,
  352. char **emsg)
  353. {
  354. struct GNUNET_FS_Uri *ret;
  355. struct GNUNET_CRYPTO_EcdsaPublicKey ns;
  356. size_t pos;
  357. char *end;
  358. pos = strlen (GNUNET_FS_URI_SKS_PREFIX);
  359. if ((strlen (s) <= pos) || (0 != strncmp (s, GNUNET_FS_URI_SKS_PREFIX, pos)))
  360. return NULL; /* not an SKS URI */
  361. end = strchr (&s[pos], '/');
  362. if ( (NULL == end) ||
  363. (GNUNET_OK !=
  364. GNUNET_STRINGS_string_to_data (&s[pos],
  365. end - &s[pos],
  366. &ns,
  367. sizeof (ns))) )
  368. {
  369. *emsg = GNUNET_strdup (_("Malformed SKS URI (wrong syntax)"));
  370. return NULL; /* malformed */
  371. }
  372. end++; /* skip over '/' */
  373. ret = GNUNET_new (struct GNUNET_FS_Uri);
  374. ret->type = GNUNET_FS_URI_SKS;
  375. ret->data.sks.ns = ns;
  376. ret->data.sks.identifier = GNUNET_strdup (end);
  377. return ret;
  378. }
  379. #define GNUNET_FS_URI_CHK_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_CHK_INFIX
  380. /**
  381. * Parse a CHK URI.
  382. *
  383. * @param s an uri string
  384. * @param emsg where to store the parser error message (if any)
  385. * @return NULL on error, CHK URI otherwise
  386. */
  387. static struct GNUNET_FS_Uri *
  388. uri_chk_parse (const char *s,
  389. char **emsg)
  390. {
  391. struct GNUNET_FS_Uri *ret;
  392. struct FileIdentifier fi;
  393. unsigned int pos;
  394. unsigned long long flen;
  395. size_t slen;
  396. char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
  397. char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
  398. slen = strlen (s);
  399. pos = strlen (GNUNET_FS_URI_CHK_PREFIX);
  400. if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
  401. (0 != strncmp (s, GNUNET_FS_URI_CHK_PREFIX, pos)))
  402. return NULL; /* not a CHK URI */
  403. if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
  404. (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
  405. {
  406. *emsg = GNUNET_strdup (_("Malformed CHK URI (wrong syntax)"));
  407. return NULL;
  408. }
  409. GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
  410. h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
  411. GNUNET_memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
  412. sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
  413. h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
  414. if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &fi.chk.key)) ||
  415. (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &fi.chk.query)) ||
  416. (1 !=
  417. SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
  418. "%llu", &flen)))
  419. {
  420. *emsg = GNUNET_strdup (_("Malformed CHK URI (failed to decode CHK)"));
  421. return NULL;
  422. }
  423. fi.file_length = GNUNET_htonll (flen);
  424. ret = GNUNET_new (struct GNUNET_FS_Uri);
  425. ret->type = GNUNET_FS_URI_CHK;
  426. ret->data.chk = fi;
  427. return ret;
  428. }
  429. GNUNET_NETWORK_STRUCT_BEGIN
  430. /**
  431. * Structure that defines how the contents of a location URI must be
  432. * assembled in memory to create or verify the signature of a location
  433. * URI.
  434. */
  435. struct LocUriAssembly
  436. {
  437. /**
  438. * What is being signed (rest of this struct).
  439. */
  440. struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
  441. /**
  442. * Expiration time of the offer.
  443. */
  444. struct GNUNET_TIME_AbsoluteNBO exptime;
  445. /**
  446. * File being offered.
  447. */
  448. struct FileIdentifier fi;
  449. /**
  450. * Peer offering the file.
  451. */
  452. struct GNUNET_PeerIdentity peer;
  453. };
  454. GNUNET_NETWORK_STRUCT_END
  455. #define GNUNET_FS_URI_LOC_PREFIX GNUNET_FS_URI_PREFIX GNUNET_FS_URI_LOC_INFIX
  456. #define SIGNATURE_ASCII_LENGTH 103
  457. /**
  458. * Parse a LOC URI.
  459. * Also verifies validity of the location URI.
  460. *
  461. * @param s an uri string
  462. * @param emsg where to store the parser error message (if any)
  463. * @return NULL on error, valid LOC URI otherwise
  464. */
  465. static struct GNUNET_FS_Uri *
  466. uri_loc_parse (const char *s,
  467. char **emsg)
  468. {
  469. struct GNUNET_FS_Uri *uri;
  470. char h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
  471. char h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)];
  472. unsigned int pos;
  473. unsigned int npos;
  474. unsigned long long exptime;
  475. unsigned long long flen;
  476. struct GNUNET_TIME_Absolute et;
  477. struct GNUNET_CRYPTO_EddsaSignature sig;
  478. struct LocUriAssembly ass;
  479. size_t slen;
  480. slen = strlen (s);
  481. pos = strlen (GNUNET_FS_URI_LOC_PREFIX);
  482. if ((slen < pos + 2 * sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) + 1) ||
  483. (0 != strncmp (s, GNUNET_FS_URI_LOC_PREFIX, pos)))
  484. return NULL; /* not a LOC URI */
  485. if ((s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] != '.') ||
  486. (s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2 - 1] != '.'))
  487. {
  488. *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax)"));
  489. return NULL;
  490. }
  491. GNUNET_memcpy (h1, &s[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
  492. h1[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
  493. GNUNET_memcpy (h2, &s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)],
  494. sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
  495. h2[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
  496. if ((GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h1, &ass.fi.chk.key)) ||
  497. (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (h2, &ass.fi.chk.query)) ||
  498. (1 !=
  499. SSCANF (&s[pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2],
  500. "%llu", &flen)))
  501. {
  502. *emsg = GNUNET_strdup (_("LOC URI malformed (no CHK)"));
  503. return NULL;
  504. }
  505. ass.fi.file_length = GNUNET_htonll (flen);
  506. npos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) * 2;
  507. while ((s[npos] != '\0') && (s[npos] != '.'))
  508. npos++;
  509. if (s[npos] == '\0')
  510. {
  511. *emsg = GNUNET_strdup (_("LOC URI malformed (missing LOC)"));
  512. goto ERR;
  513. }
  514. npos++;
  515. if ( (strlen (&s[npos]) <= GNUNET_CRYPTO_PKEY_ASCII_LENGTH + 1) ||
  516. ('.' != s[npos+GNUNET_CRYPTO_PKEY_ASCII_LENGTH]) )
  517. {
  518. *emsg =
  519. GNUNET_strdup (_("LOC URI malformed (wrong syntax for public key)"));
  520. }
  521. if (GNUNET_OK !=
  522. GNUNET_CRYPTO_eddsa_public_key_from_string (&s[npos],
  523. GNUNET_CRYPTO_PKEY_ASCII_LENGTH,
  524. &ass.peer.public_key))
  525. {
  526. *emsg =
  527. GNUNET_strdup (_("LOC URI malformed (could not decode public key)"));
  528. goto ERR;
  529. }
  530. npos += GNUNET_CRYPTO_PKEY_ASCII_LENGTH;
  531. if (s[npos++] != '.')
  532. {
  533. *emsg = GNUNET_strdup (_("LOC URI malformed (could not find signature)"));
  534. goto ERR;
  535. }
  536. if ( (strlen (&s[npos]) <= SIGNATURE_ASCII_LENGTH + 1) ||
  537. ('.' != s[npos + SIGNATURE_ASCII_LENGTH]) )
  538. {
  539. *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax for signature)"));
  540. goto ERR;
  541. }
  542. if (GNUNET_OK !=
  543. GNUNET_STRINGS_string_to_data (&s[npos],
  544. SIGNATURE_ASCII_LENGTH,
  545. &sig,
  546. sizeof (struct GNUNET_CRYPTO_EddsaSignature)))
  547. {
  548. *emsg = GNUNET_strdup (_("LOC URI malformed (could not decode signature)"));
  549. goto ERR;
  550. }
  551. npos += SIGNATURE_ASCII_LENGTH;
  552. if (s[npos++] != '.')
  553. {
  554. *emsg = GNUNET_strdup (_("LOC URI malformed (wrong syntax for expiration time)"));
  555. goto ERR;
  556. }
  557. if (1 != SSCANF (&s[npos], "%llu", &exptime))
  558. {
  559. *emsg =
  560. GNUNET_strdup (_("LOC URI malformed (could not parse expiration time)"));
  561. goto ERR;
  562. }
  563. ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
  564. ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
  565. et.abs_value_us = exptime * 1000LL * 1000LL;
  566. ass.exptime = GNUNET_TIME_absolute_hton (et);
  567. if (GNUNET_OK !=
  568. GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT,
  569. &ass.purpose, &sig, &ass.peer.public_key))
  570. {
  571. *emsg =
  572. GNUNET_strdup (_("LOC URI malformed (signature failed validation)"));
  573. goto ERR;
  574. }
  575. uri = GNUNET_new (struct GNUNET_FS_Uri);
  576. uri->type = GNUNET_FS_URI_LOC;
  577. uri->data.loc.fi = ass.fi;
  578. uri->data.loc.peer = ass.peer;
  579. uri->data.loc.expirationTime = et;
  580. uri->data.loc.contentSignature = sig;
  581. return uri;
  582. ERR:
  583. return NULL;
  584. }
  585. /**
  586. * Convert a UTF-8 String to a URI.
  587. *
  588. * @param uri string to parse
  589. * @param emsg where to store the parser error message (if any)
  590. * @return NULL on error
  591. */
  592. struct GNUNET_FS_Uri *
  593. GNUNET_FS_uri_parse (const char *uri,
  594. char **emsg)
  595. {
  596. struct GNUNET_FS_Uri *ret;
  597. char *msg;
  598. if (NULL == uri)
  599. {
  600. GNUNET_break (0);
  601. if (NULL != emsg)
  602. *emsg = GNUNET_strdup (_("invalid argument"));
  603. return NULL;
  604. }
  605. if (NULL == emsg)
  606. emsg = &msg;
  607. *emsg = NULL;
  608. if ((NULL != (ret = uri_chk_parse (uri, emsg))) ||
  609. (NULL != (ret = uri_ksk_parse (uri, emsg))) ||
  610. (NULL != (ret = uri_sks_parse (uri, emsg))) ||
  611. (NULL != (ret = uri_loc_parse (uri, emsg))))
  612. return ret;
  613. if (NULL == *emsg)
  614. *emsg = GNUNET_strdup (_("Unrecognized URI type"));
  615. if (emsg == &msg)
  616. GNUNET_free (msg);
  617. return NULL;
  618. }
  619. /**
  620. * Free URI.
  621. *
  622. * @param uri uri to free
  623. */
  624. void
  625. GNUNET_FS_uri_destroy (struct GNUNET_FS_Uri *uri)
  626. {
  627. unsigned int i;
  628. switch (uri->type)
  629. {
  630. case GNUNET_FS_URI_KSK:
  631. for (i = 0; i < uri->data.ksk.keywordCount; i++)
  632. GNUNET_free (uri->data.ksk.keywords[i]);
  633. GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount, 0);
  634. break;
  635. case GNUNET_FS_URI_SKS:
  636. GNUNET_free (uri->data.sks.identifier);
  637. break;
  638. case GNUNET_FS_URI_LOC:
  639. break;
  640. default:
  641. /* do nothing */
  642. break;
  643. }
  644. GNUNET_free (uri);
  645. }
  646. /**
  647. * How many keywords are ANDed in this keyword URI?
  648. *
  649. * @param uri ksk uri to get the number of keywords from
  650. * @return 0 if this is not a keyword URI
  651. */
  652. unsigned int
  653. GNUNET_FS_uri_ksk_get_keyword_count (const struct GNUNET_FS_Uri *uri)
  654. {
  655. if (uri->type != GNUNET_FS_URI_KSK)
  656. return 0;
  657. return uri->data.ksk.keywordCount;
  658. }
  659. /**
  660. * Iterate over all keywords in this keyword URI.
  661. *
  662. * @param uri ksk uri to get the keywords from
  663. * @param iterator function to call on each keyword
  664. * @param iterator_cls closure for iterator
  665. * @return -1 if this is not a keyword URI, otherwise number of
  666. * keywords iterated over until iterator aborted
  667. */
  668. int
  669. GNUNET_FS_uri_ksk_get_keywords (const struct GNUNET_FS_Uri *uri,
  670. GNUNET_FS_KeywordIterator iterator,
  671. void *iterator_cls)
  672. {
  673. unsigned int i;
  674. char *keyword;
  675. if (uri->type != GNUNET_FS_URI_KSK)
  676. return -1;
  677. if (NULL == iterator)
  678. return uri->data.ksk.keywordCount;
  679. for (i = 0; i < uri->data.ksk.keywordCount; i++)
  680. {
  681. keyword = uri->data.ksk.keywords[i];
  682. /* first character of keyword indicates
  683. * if it is mandatory or not */
  684. if (GNUNET_OK != iterator (iterator_cls, &keyword[1], keyword[0] == '+'))
  685. return i;
  686. }
  687. return i;
  688. }
  689. /**
  690. * Add the given keyword to the set of keywords represented by the URI.
  691. * Does nothing if the keyword is already present.
  692. *
  693. * @param uri ksk uri to modify
  694. * @param keyword keyword to add
  695. * @param is_mandatory is this keyword mandatory?
  696. */
  697. void
  698. GNUNET_FS_uri_ksk_add_keyword (struct GNUNET_FS_Uri *uri,
  699. const char *keyword,
  700. int is_mandatory)
  701. {
  702. unsigned int i;
  703. const char *old;
  704. char *n;
  705. GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
  706. for (i = 0; i < uri->data.ksk.keywordCount; i++)
  707. {
  708. old = uri->data.ksk.keywords[i];
  709. if (0 == strcmp (&old[1], keyword))
  710. return;
  711. }
  712. GNUNET_asprintf (&n, is_mandatory ? "+%s" : " %s", keyword);
  713. GNUNET_array_append (uri->data.ksk.keywords, uri->data.ksk.keywordCount, n);
  714. }
  715. /**
  716. * Remove the given keyword from the set of keywords represented by the URI.
  717. * Does nothing if the keyword is not present.
  718. *
  719. * @param uri ksk uri to modify
  720. * @param keyword keyword to add
  721. */
  722. void
  723. GNUNET_FS_uri_ksk_remove_keyword (struct GNUNET_FS_Uri *uri,
  724. const char *keyword)
  725. {
  726. unsigned int i;
  727. char *old;
  728. GNUNET_assert (uri->type == GNUNET_FS_URI_KSK);
  729. for (i = 0; i < uri->data.ksk.keywordCount; i++)
  730. {
  731. old = uri->data.ksk.keywords[i];
  732. if (0 == strcmp (&old[1], keyword))
  733. {
  734. uri->data.ksk.keywords[i] =
  735. uri->data.ksk.keywords[uri->data.ksk.keywordCount - 1];
  736. GNUNET_array_grow (uri->data.ksk.keywords, uri->data.ksk.keywordCount,
  737. uri->data.ksk.keywordCount - 1);
  738. GNUNET_free (old);
  739. return;
  740. }
  741. }
  742. }
  743. /**
  744. * Obtain the identity of the peer offering the data
  745. *
  746. * @param uri the location URI to inspect
  747. * @param peer where to store the identify of the peer (presumably) offering the content
  748. * @return #GNUNET_SYSERR if this is not a location URI, otherwise #GNUNET_OK
  749. */
  750. int
  751. GNUNET_FS_uri_loc_get_peer_identity (const struct GNUNET_FS_Uri *uri,
  752. struct GNUNET_PeerIdentity *peer)
  753. {
  754. if (uri->type != GNUNET_FS_URI_LOC)
  755. return GNUNET_SYSERR;
  756. *peer = uri->data.loc.peer;
  757. return GNUNET_OK;
  758. }
  759. /**
  760. * Obtain the expiration of the LOC URI.
  761. *
  762. * @param uri location URI to get the expiration from
  763. * @return expiration time of the URI
  764. */
  765. struct GNUNET_TIME_Absolute
  766. GNUNET_FS_uri_loc_get_expiration (const struct GNUNET_FS_Uri *uri)
  767. {
  768. GNUNET_assert (uri->type == GNUNET_FS_URI_LOC);
  769. return uri->data.loc.expirationTime;
  770. }
  771. /**
  772. * Obtain the URI of the content itself.
  773. *
  774. * @param uri location URI to get the content URI from
  775. * @return NULL if argument is not a location URI
  776. */
  777. struct GNUNET_FS_Uri *
  778. GNUNET_FS_uri_loc_get_uri (const struct GNUNET_FS_Uri *uri)
  779. {
  780. struct GNUNET_FS_Uri *ret;
  781. if (uri->type != GNUNET_FS_URI_LOC)
  782. return NULL;
  783. ret = GNUNET_new (struct GNUNET_FS_Uri);
  784. ret->type = GNUNET_FS_URI_CHK;
  785. ret->data.chk = uri->data.loc.fi;
  786. return ret;
  787. }
  788. /**
  789. * Construct a location URI (this peer will be used for the location).
  790. * This function should only be called from within gnunet-service-fs,
  791. * as it requires the peer's private key which is generally unavailable
  792. * to processes directly under the user's control. However, for
  793. * testing and as it logically fits under URIs, it is in this API.
  794. *
  795. * @param base_uri content offered by the sender
  796. * @param sign_key private key of the peer
  797. * @param expiration_time how long will the content be offered?
  798. * @return the location URI, NULL on error
  799. */
  800. struct GNUNET_FS_Uri *
  801. GNUNET_FS_uri_loc_create (const struct GNUNET_FS_Uri *base_uri,
  802. const struct GNUNET_CRYPTO_EddsaPrivateKey *sign_key,
  803. struct GNUNET_TIME_Absolute expiration_time)
  804. {
  805. struct GNUNET_FS_Uri *uri;
  806. struct GNUNET_CRYPTO_EddsaPublicKey my_public_key;
  807. struct LocUriAssembly ass;
  808. struct GNUNET_TIME_Absolute et;
  809. if (GNUNET_FS_URI_CHK != base_uri->type)
  810. return NULL;
  811. /* we round expiration time to full seconds for SKS URIs */
  812. et.abs_value_us = (expiration_time.abs_value_us / 1000000LL) * 1000000LL;
  813. GNUNET_CRYPTO_eddsa_key_get_public (sign_key,
  814. &my_public_key);
  815. ass.purpose.size = htonl (sizeof (struct LocUriAssembly));
  816. ass.purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_PEER_PLACEMENT);
  817. ass.exptime = GNUNET_TIME_absolute_hton (et);
  818. ass.fi = base_uri->data.chk;
  819. ass.peer.public_key = my_public_key;
  820. uri = GNUNET_new (struct GNUNET_FS_Uri);
  821. uri->type = GNUNET_FS_URI_LOC;
  822. uri->data.loc.fi = base_uri->data.chk;
  823. uri->data.loc.expirationTime = et;
  824. uri->data.loc.peer.public_key = my_public_key;
  825. GNUNET_assert (GNUNET_OK ==
  826. GNUNET_CRYPTO_eddsa_sign (sign_key,
  827. &ass.purpose,
  828. &uri->data.loc.contentSignature));
  829. return uri;
  830. }
  831. /**
  832. * Create an SKS URI from a namespace ID and an identifier.
  833. *
  834. * @param ns namespace ID
  835. * @param id identifier
  836. * @return an FS URI for the given namespace and identifier
  837. */
  838. struct GNUNET_FS_Uri *
  839. GNUNET_FS_uri_sks_create (const struct GNUNET_CRYPTO_EcdsaPublicKey *ns,
  840. const char *id)
  841. {
  842. struct GNUNET_FS_Uri *ns_uri;
  843. ns_uri = GNUNET_new (struct GNUNET_FS_Uri);
  844. ns_uri->type = GNUNET_FS_URI_SKS;
  845. ns_uri->data.sks.ns = *ns;
  846. ns_uri->data.sks.identifier = GNUNET_strdup (id);
  847. return ns_uri;
  848. }
  849. /**
  850. * Merge the sets of keywords from two KSK URIs.
  851. * (useful for merging the canonicalized keywords with
  852. * the original keywords for sharing).
  853. *
  854. * @param u1 first uri
  855. * @param u2 second uri
  856. * @return merged URI, NULL on error
  857. */
  858. struct GNUNET_FS_Uri *
  859. GNUNET_FS_uri_ksk_merge (const struct GNUNET_FS_Uri *u1,
  860. const struct GNUNET_FS_Uri *u2)
  861. {
  862. struct GNUNET_FS_Uri *ret;
  863. unsigned int kc;
  864. unsigned int i;
  865. unsigned int j;
  866. int found;
  867. const char *kp;
  868. char **kl;
  869. if ((u1 == NULL) && (u2 == NULL))
  870. return NULL;
  871. if (u1 == NULL)
  872. return GNUNET_FS_uri_dup (u2);
  873. if (u2 == NULL)
  874. return GNUNET_FS_uri_dup (u1);
  875. if ((u1->type != GNUNET_FS_URI_KSK) || (u2->type != GNUNET_FS_URI_KSK))
  876. {
  877. GNUNET_break (0);
  878. return NULL;
  879. }
  880. kc = u1->data.ksk.keywordCount;
  881. kl = GNUNET_new_array (kc + u2->data.ksk.keywordCount,
  882. char *);
  883. for (i = 0; i < u1->data.ksk.keywordCount; i++)
  884. kl[i] = GNUNET_strdup (u1->data.ksk.keywords[i]);
  885. for (i = 0; i < u2->data.ksk.keywordCount; i++)
  886. {
  887. kp = u2->data.ksk.keywords[i];
  888. found = 0;
  889. for (j = 0; j < u1->data.ksk.keywordCount; j++)
  890. if (0 == strcmp (kp + 1, kl[j] + 1))
  891. {
  892. found = 1;
  893. if (kp[0] == '+')
  894. kl[j][0] = '+';
  895. break;
  896. }
  897. if (0 == found)
  898. kl[kc++] = GNUNET_strdup (kp);
  899. }
  900. ret = GNUNET_new (struct GNUNET_FS_Uri);
  901. ret->type = GNUNET_FS_URI_KSK;
  902. ret->data.ksk.keywordCount = kc;
  903. ret->data.ksk.keywords = kl;
  904. return ret;
  905. }
  906. /**
  907. * Duplicate URI.
  908. *
  909. * @param uri the URI to duplicate
  910. * @return copy of the URI
  911. */
  912. struct GNUNET_FS_Uri *
  913. GNUNET_FS_uri_dup (const struct GNUNET_FS_Uri *uri)
  914. {
  915. struct GNUNET_FS_Uri *ret;
  916. unsigned int i;
  917. if (uri == NULL)
  918. return NULL;
  919. ret = GNUNET_new (struct GNUNET_FS_Uri);
  920. GNUNET_memcpy (ret, uri, sizeof (struct GNUNET_FS_Uri));
  921. switch (ret->type)
  922. {
  923. case GNUNET_FS_URI_KSK:
  924. if (ret->data.ksk.keywordCount >=
  925. GNUNET_MAX_MALLOC_CHECKED / sizeof (char *))
  926. {
  927. GNUNET_break (0);
  928. GNUNET_free (ret);
  929. return NULL;
  930. }
  931. if (ret->data.ksk.keywordCount > 0)
  932. {
  933. ret->data.ksk.keywords
  934. = GNUNET_new_array (ret->data.ksk.keywordCount,
  935. char *);
  936. for (i = 0; i < ret->data.ksk.keywordCount; i++)
  937. ret->data.ksk.keywords[i] = GNUNET_strdup (uri->data.ksk.keywords[i]);
  938. }
  939. else
  940. ret->data.ksk.keywords = NULL; /* just to be sure */
  941. break;
  942. case GNUNET_FS_URI_SKS:
  943. ret->data.sks.identifier = GNUNET_strdup (uri->data.sks.identifier);
  944. break;
  945. case GNUNET_FS_URI_LOC:
  946. break;
  947. default:
  948. break;
  949. }
  950. return ret;
  951. }
  952. /**
  953. * Create an FS URI from a single user-supplied string of keywords.
  954. * The string is broken up at spaces into individual keywords.
  955. * Keywords that start with "+" are mandatory. Double-quotes can
  956. * be used to prevent breaking up strings at spaces (and also
  957. * to specify non-mandatory keywords starting with "+").
  958. *
  959. * Keywords must contain a balanced number of double quotes and
  960. * double quotes can not be used in the actual keywords (for
  961. * example, the string '""foo bar""' will be turned into two
  962. * "OR"ed keywords 'foo' and 'bar', not into '"foo bar"'.
  963. *
  964. * @param keywords the keyword string
  965. * @param emsg where to store an error message
  966. * @return an FS URI for the given keywords, NULL
  967. * if keywords is not legal (i.e. empty).
  968. */
  969. struct GNUNET_FS_Uri *
  970. GNUNET_FS_uri_ksk_create (const char *keywords,
  971. char **emsg)
  972. {
  973. char **keywordarr;
  974. unsigned int num_Words;
  975. int inWord;
  976. char *pos;
  977. struct GNUNET_FS_Uri *uri;
  978. char *searchString;
  979. int saw_quote;
  980. if (keywords == NULL)
  981. {
  982. *emsg = GNUNET_strdup (_("No keywords specified!\n"));
  983. GNUNET_break (0);
  984. return NULL;
  985. }
  986. searchString = GNUNET_strdup (keywords);
  987. num_Words = 0;
  988. inWord = 0;
  989. saw_quote = 0;
  990. pos = searchString;
  991. while ('\0' != *pos)
  992. {
  993. if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
  994. {
  995. inWord = 0;
  996. }
  997. else if (0 == inWord)
  998. {
  999. inWord = 1;
  1000. ++num_Words;
  1001. }
  1002. if ('"' == *pos)
  1003. saw_quote = (saw_quote + 1) % 2;
  1004. pos++;
  1005. }
  1006. if (num_Words == 0)
  1007. {
  1008. GNUNET_free (searchString);
  1009. *emsg = GNUNET_strdup (_("No keywords specified!\n"));
  1010. return NULL;
  1011. }
  1012. if (saw_quote != 0)
  1013. {
  1014. GNUNET_free (searchString);
  1015. *emsg = GNUNET_strdup (_("Number of double-quotes not balanced!\n"));
  1016. return NULL;
  1017. }
  1018. keywordarr = GNUNET_new_array (num_Words,
  1019. char *);
  1020. num_Words = 0;
  1021. inWord = 0;
  1022. pos = searchString;
  1023. while ('\0' != *pos)
  1024. {
  1025. if ((saw_quote == 0) && (isspace ((unsigned char) *pos)))
  1026. {
  1027. inWord = 0;
  1028. *pos = '\0';
  1029. }
  1030. else if (0 == inWord)
  1031. {
  1032. keywordarr[num_Words] = pos;
  1033. inWord = 1;
  1034. ++num_Words;
  1035. }
  1036. if ('"' == *pos)
  1037. saw_quote = (saw_quote + 1) % 2;
  1038. pos++;
  1039. }
  1040. uri =
  1041. GNUNET_FS_uri_ksk_create_from_args (num_Words,
  1042. (const char **) keywordarr);
  1043. GNUNET_free (keywordarr);
  1044. GNUNET_free (searchString);
  1045. return uri;
  1046. }
  1047. /**
  1048. * Create an FS URI from a user-supplied command line of keywords.
  1049. * Arguments should start with "+" to indicate mandatory
  1050. * keywords.
  1051. *
  1052. * @param argc number of keywords
  1053. * @param argv keywords (double quotes are not required for
  1054. * keywords containing spaces; however, double
  1055. * quotes are required for keywords starting with
  1056. * "+"); there is no mechanism for having double
  1057. * quotes in the actual keywords (if the user
  1058. * did specifically specify double quotes, the
  1059. * caller should convert each double quote
  1060. * into two single quotes).
  1061. * @return an FS URI for the given keywords, NULL
  1062. * if keywords is not legal (i.e. empty).
  1063. */
  1064. struct GNUNET_FS_Uri *
  1065. GNUNET_FS_uri_ksk_create_from_args (unsigned int argc,
  1066. const char **argv)
  1067. {
  1068. unsigned int i;
  1069. struct GNUNET_FS_Uri *uri;
  1070. const char *keyword;
  1071. char *val;
  1072. const char *r;
  1073. char *w;
  1074. char *emsg;
  1075. if (argc == 0)
  1076. return NULL;
  1077. /* allow URI to be given as one and only keyword and
  1078. * handle accordingly */
  1079. emsg = NULL;
  1080. if ((argc == 1) && (strlen (argv[0]) > strlen (GNUNET_FS_URI_PREFIX)) &&
  1081. (0 ==
  1082. strncmp (argv[0], GNUNET_FS_URI_PREFIX, strlen (GNUNET_FS_URI_PREFIX)))
  1083. && (NULL != (uri = GNUNET_FS_uri_parse (argv[0], &emsg))))
  1084. return uri;
  1085. GNUNET_free_non_null (emsg);
  1086. uri = GNUNET_new (struct GNUNET_FS_Uri);
  1087. uri->type = GNUNET_FS_URI_KSK;
  1088. uri->data.ksk.keywordCount = argc;
  1089. uri->data.ksk.keywords = GNUNET_new_array (argc,
  1090. char *);
  1091. for (i = 0; i < argc; i++)
  1092. {
  1093. keyword = argv[i];
  1094. if (keyword[0] == '+')
  1095. val = GNUNET_strdup (keyword);
  1096. else
  1097. GNUNET_asprintf (&val, " %s", keyword);
  1098. r = val;
  1099. w = val;
  1100. while ('\0' != *r)
  1101. {
  1102. if ('"' == *r)
  1103. r++;
  1104. else
  1105. *(w++) = *(r++);
  1106. }
  1107. *w = '\0';
  1108. uri->data.ksk.keywords[i] = val;
  1109. }
  1110. return uri;
  1111. }
  1112. /**
  1113. * Test if two URIs are equal.
  1114. *
  1115. * @param u1 one of the URIs
  1116. * @param u2 the other URI
  1117. * @return #GNUNET_YES if the URIs are equal
  1118. */
  1119. int
  1120. GNUNET_FS_uri_test_equal (const struct GNUNET_FS_Uri *u1,
  1121. const struct GNUNET_FS_Uri *u2)
  1122. {
  1123. int ret;
  1124. unsigned int i;
  1125. unsigned int j;
  1126. GNUNET_assert (u1 != NULL);
  1127. GNUNET_assert (u2 != NULL);
  1128. if (u1->type != u2->type)
  1129. return GNUNET_NO;
  1130. switch (u1->type)
  1131. {
  1132. case GNUNET_FS_URI_CHK:
  1133. if (0 ==
  1134. memcmp (&u1->data.chk, &u2->data.chk, sizeof (struct FileIdentifier)))
  1135. return GNUNET_YES;
  1136. return GNUNET_NO;
  1137. case GNUNET_FS_URI_SKS:
  1138. if ((0 ==
  1139. memcmp (&u1->data.sks.ns, &u2->data.sks.ns,
  1140. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))) &&
  1141. (0 == strcmp (u1->data.sks.identifier, u2->data.sks.identifier)))
  1142. return GNUNET_YES;
  1143. return GNUNET_NO;
  1144. case GNUNET_FS_URI_KSK:
  1145. if (u1->data.ksk.keywordCount != u2->data.ksk.keywordCount)
  1146. return GNUNET_NO;
  1147. for (i = 0; i < u1->data.ksk.keywordCount; i++)
  1148. {
  1149. ret = GNUNET_NO;
  1150. for (j = 0; j < u2->data.ksk.keywordCount; j++)
  1151. {
  1152. if (0 == strcmp (u1->data.ksk.keywords[i], u2->data.ksk.keywords[j]))
  1153. {
  1154. ret = GNUNET_YES;
  1155. break;
  1156. }
  1157. }
  1158. if (ret == GNUNET_NO)
  1159. return GNUNET_NO;
  1160. }
  1161. return GNUNET_YES;
  1162. case GNUNET_FS_URI_LOC:
  1163. if (memcmp
  1164. (&u1->data.loc, &u2->data.loc,
  1165. sizeof (struct FileIdentifier) +
  1166. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
  1167. sizeof (struct GNUNET_TIME_Absolute) + sizeof (unsigned short) +
  1168. sizeof (unsigned short)) != 0)
  1169. return GNUNET_NO;
  1170. return GNUNET_YES;
  1171. default:
  1172. return GNUNET_NO;
  1173. }
  1174. }
  1175. /**
  1176. * Is this a namespace URI?
  1177. *
  1178. * @param uri the uri to check
  1179. * @return #GNUNET_YES if this is an SKS uri
  1180. */
  1181. int
  1182. GNUNET_FS_uri_test_sks (const struct GNUNET_FS_Uri *uri)
  1183. {
  1184. return uri->type == GNUNET_FS_URI_SKS;
  1185. }
  1186. /**
  1187. * Get the ID of a namespace from the given
  1188. * namespace URI.
  1189. *
  1190. * @param uri the uri to get the namespace ID from
  1191. * @param pseudonym where to store the ID of the namespace
  1192. * @return #GNUNET_OK on success
  1193. */
  1194. int
  1195. GNUNET_FS_uri_sks_get_namespace (const struct GNUNET_FS_Uri *uri,
  1196. struct GNUNET_CRYPTO_EcdsaPublicKey *pseudonym)
  1197. {
  1198. if (!GNUNET_FS_uri_test_sks (uri))
  1199. {
  1200. GNUNET_break (0);
  1201. return GNUNET_SYSERR;
  1202. }
  1203. *pseudonym = uri->data.sks.ns;
  1204. return GNUNET_OK;
  1205. }
  1206. /**
  1207. * Get the content identifier of an SKS URI.
  1208. *
  1209. * @param uri the sks uri
  1210. * @return NULL on error (not a valid SKS URI)
  1211. */
  1212. char *
  1213. GNUNET_FS_uri_sks_get_content_id (const struct GNUNET_FS_Uri *uri)
  1214. {
  1215. if (!GNUNET_FS_uri_test_sks (uri))
  1216. {
  1217. GNUNET_break (0);
  1218. return NULL;
  1219. }
  1220. return GNUNET_strdup (uri->data.sks.identifier);
  1221. }
  1222. /**
  1223. * Is this a keyword URI?
  1224. *
  1225. * @param uri the uri
  1226. * @return #GNUNET_YES if this is a KSK uri
  1227. */
  1228. int
  1229. GNUNET_FS_uri_test_ksk (const struct GNUNET_FS_Uri *uri)
  1230. {
  1231. #if EXTRA_CHECKS
  1232. unsigned int i;
  1233. if (uri->type == GNUNET_FS_URI_KSK)
  1234. {
  1235. for (i=0;i < uri->data.ksk.keywordCount; i++)
  1236. GNUNET_assert (uri->data.ksk.keywords[i] != NULL);
  1237. }
  1238. #endif
  1239. return uri->type == GNUNET_FS_URI_KSK;
  1240. }
  1241. /**
  1242. * Is this a file (or directory) URI?
  1243. *
  1244. * @param uri the uri to check
  1245. * @return #GNUNET_YES if this is a CHK uri
  1246. */
  1247. int
  1248. GNUNET_FS_uri_test_chk (const struct GNUNET_FS_Uri *uri)
  1249. {
  1250. return uri->type == GNUNET_FS_URI_CHK;
  1251. }
  1252. /**
  1253. * What is the size of the file that this URI
  1254. * refers to?
  1255. *
  1256. * @param uri the CHK URI to inspect
  1257. * @return size of the file as specified in the CHK URI
  1258. */
  1259. uint64_t
  1260. GNUNET_FS_uri_chk_get_file_size (const struct GNUNET_FS_Uri * uri)
  1261. {
  1262. switch (uri->type)
  1263. {
  1264. case GNUNET_FS_URI_CHK:
  1265. return GNUNET_ntohll (uri->data.chk.file_length);
  1266. case GNUNET_FS_URI_LOC:
  1267. return GNUNET_ntohll (uri->data.loc.fi.file_length);
  1268. default:
  1269. GNUNET_assert (0);
  1270. }
  1271. return 0; /* unreachable */
  1272. }
  1273. /**
  1274. * Is this a location URI?
  1275. *
  1276. * @param uri the uri to check
  1277. * @return #GNUNET_YES if this is a LOC uri
  1278. */
  1279. int
  1280. GNUNET_FS_uri_test_loc (const struct GNUNET_FS_Uri *uri)
  1281. {
  1282. return uri->type == GNUNET_FS_URI_LOC;
  1283. }
  1284. /**
  1285. * Add a keyword as non-mandatory (with ' '-prefix) to the
  1286. * given keyword list at offset 'index'. The array is
  1287. * guaranteed to be long enough.
  1288. *
  1289. * @param s keyword to add
  1290. * @param array array to add the keyword to
  1291. * @param index offset where to add the keyword
  1292. */
  1293. static void
  1294. insert_non_mandatory_keyword (const char *s,
  1295. char **array,
  1296. int index)
  1297. {
  1298. char *nkword;
  1299. GNUNET_asprintf (&nkword,
  1300. " %s", /* space to mark as 'non mandatory' */
  1301. s);
  1302. array[index] = nkword;
  1303. }
  1304. /**
  1305. * Test if the given keyword @a s is already present in the
  1306. * given array, ignoring the '+'-mandatory prefix in the array.
  1307. *
  1308. * @param s keyword to test
  1309. * @param array keywords to test against, with ' ' or '+' prefix to ignore
  1310. * @param array_length length of the @a array
  1311. * @return #GNUNET_YES if the keyword exists, #GNUNET_NO if not
  1312. */
  1313. static int
  1314. find_duplicate (const char *s,
  1315. const char **array,
  1316. int array_length)
  1317. {
  1318. int j;
  1319. for (j = array_length - 1; j >= 0; j--)
  1320. if (0 == strcmp (&array[j][1], s))
  1321. return GNUNET_YES;
  1322. return GNUNET_NO;
  1323. }
  1324. /**
  1325. * FIXME: comment
  1326. */
  1327. static char *
  1328. normalize_metadata (enum EXTRACTOR_MetaFormat format,
  1329. const char *data,
  1330. size_t data_len)
  1331. {
  1332. uint8_t *free_str = NULL;
  1333. uint8_t *str_to_normalize = (uint8_t *) data;
  1334. uint8_t *normalized;
  1335. size_t r_len;
  1336. if (str_to_normalize == NULL)
  1337. return NULL;
  1338. /* Don't trust libextractor */
  1339. if (format == EXTRACTOR_METAFORMAT_UTF8)
  1340. {
  1341. free_str = (uint8_t *) u8_check ((const uint8_t *) data, data_len);
  1342. if (free_str == NULL)
  1343. free_str = NULL;
  1344. else
  1345. format = EXTRACTOR_METAFORMAT_C_STRING;
  1346. }
  1347. if (format == EXTRACTOR_METAFORMAT_C_STRING)
  1348. {
  1349. free_str = u8_strconv_from_encoding (data, locale_charset (), iconveh_escape_sequence);
  1350. if (free_str == NULL)
  1351. return NULL;
  1352. }
  1353. normalized = u8_tolower (str_to_normalize, strlen ((char *) str_to_normalize), NULL, UNINORM_NFD, NULL, &r_len);
  1354. /* free_str is allocated by libunistring internally, use free() */
  1355. if (free_str != NULL)
  1356. free (free_str);
  1357. if (normalized != NULL)
  1358. {
  1359. /* u8_tolower allocates a non-NULL-terminated string! */
  1360. free_str = GNUNET_malloc (r_len + 1);
  1361. GNUNET_memcpy (free_str, normalized, r_len);
  1362. free_str[r_len] = '\0';
  1363. free (normalized);
  1364. normalized = free_str;
  1365. }
  1366. return (char *) normalized;
  1367. }
  1368. /**
  1369. * Counts the number of UTF-8 characters (not bytes) in the string,
  1370. * returns that count.
  1371. */
  1372. static size_t
  1373. u8_strcount (const uint8_t *s)
  1374. {
  1375. size_t count;
  1376. ucs4_t c;
  1377. GNUNET_assert (s != NULL);
  1378. if (s[0] == 0)
  1379. return 0;
  1380. for (count = 0; s != NULL; count++)
  1381. s = u8_next (&c, s);
  1382. return count - 1;
  1383. }
  1384. /**
  1385. * Break the filename up by matching [], () and {} pairs to make
  1386. * keywords. In case of nesting parentheses only the inner pair counts.
  1387. * You can't escape parentheses to scan something like "[blah\{foo]" to
  1388. * make a "blah{foo" keyword, this function is only a heuristic!
  1389. *
  1390. * @param s string to break down.
  1391. * @param array array to fill with enclosed tokens. If NULL, then tokens
  1392. * are only counted.
  1393. * @param index index at which to start filling the array (entries prior
  1394. * to it are used to check for duplicates). ignored if @a array == NULL.
  1395. * @return number of tokens counted (including duplicates), or number of
  1396. * tokens extracted (excluding duplicates). 0 if there are no
  1397. * matching parens in the string (when counting), or when all tokens
  1398. * were duplicates (when extracting).
  1399. */
  1400. static int
  1401. get_keywords_from_parens (const char *s,
  1402. char **array,
  1403. int index)
  1404. {
  1405. int count = 0;
  1406. char *open_paren;
  1407. char *close_paren;
  1408. char *ss;
  1409. char tmp;
  1410. if (NULL == s)
  1411. return 0;
  1412. ss = GNUNET_strdup (s);
  1413. open_paren = ss - 1;
  1414. while (NULL != (open_paren = strpbrk (open_paren + 1, "[{(")))
  1415. {
  1416. int match = 0;
  1417. close_paren = strpbrk (open_paren + 1, "]})");
  1418. if (NULL == close_paren)
  1419. continue;
  1420. switch (open_paren[0])
  1421. {
  1422. case '[':
  1423. if (']' == close_paren[0])
  1424. match = 1;
  1425. break;
  1426. case '{':
  1427. if ('}' == close_paren[0])
  1428. match = 1;
  1429. break;
  1430. case '(':
  1431. if (')' == close_paren[0])
  1432. match = 1;
  1433. break;
  1434. default:
  1435. break;
  1436. }
  1437. if (match && (close_paren - open_paren > 1))
  1438. {
  1439. tmp = close_paren[0];
  1440. close_paren[0] = '\0';
  1441. /* Keywords must be at least 3 characters long */
  1442. if (u8_strcount ((const uint8_t *) &open_paren[1]) <= 2)
  1443. {
  1444. close_paren[0] = tmp;
  1445. continue;
  1446. }
  1447. if (NULL != array)
  1448. {
  1449. char *normalized;
  1450. if (GNUNET_NO == find_duplicate ((const char *) &open_paren[1],
  1451. (const char **) array, index + count))
  1452. {
  1453. insert_non_mandatory_keyword ((const char *) &open_paren[1], array,
  1454. index + count);
  1455. count++;
  1456. }
  1457. normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
  1458. &open_paren[1], close_paren - &open_paren[1]);
  1459. if (normalized != NULL)
  1460. {
  1461. if (GNUNET_NO == find_duplicate ((const char *) normalized,
  1462. (const char **) array, index + count))
  1463. {
  1464. insert_non_mandatory_keyword ((const char *) normalized, array,
  1465. index + count);
  1466. count++;
  1467. }
  1468. GNUNET_free (normalized);
  1469. }
  1470. }
  1471. else
  1472. count++;
  1473. close_paren[0] = tmp;
  1474. }
  1475. }
  1476. GNUNET_free (ss);
  1477. return count;
  1478. }
  1479. /**
  1480. * Where to break up keywords
  1481. */
  1482. #define TOKENS "_. /-!?#&+@\"\'\\;:,()[]{}$<>|"
  1483. /**
  1484. * Break the filename up by TOKENS to make
  1485. * keywords.
  1486. *
  1487. * @param s string to break down.
  1488. * @param array array to fill with tokens. If NULL, then tokens are only
  1489. * counted.
  1490. * @param index index at which to start filling the array (entries prior
  1491. * to it are used to check for duplicates). ignored if @a array == NULL.
  1492. * @return number of tokens (>1) counted (including duplicates), or number of
  1493. * tokens extracted (excluding duplicates). 0 if there are no
  1494. * separators in the string (when counting), or when all tokens were
  1495. * duplicates (when extracting).
  1496. */
  1497. static int
  1498. get_keywords_from_tokens (const char *s,
  1499. char **array,
  1500. int index)
  1501. {
  1502. char *p;
  1503. char *ss;
  1504. int seps = 0;
  1505. ss = GNUNET_strdup (s);
  1506. for (p = strtok (ss, TOKENS); p != NULL; p = strtok (NULL, TOKENS))
  1507. {
  1508. /* Keywords must be at least 3 characters long */
  1509. if (u8_strcount ((const uint8_t *) p) <= 2)
  1510. continue;
  1511. if (NULL != array)
  1512. {
  1513. char *normalized;
  1514. if (GNUNET_NO == find_duplicate (p, (const char **) array, index + seps))
  1515. {
  1516. insert_non_mandatory_keyword (p, array,
  1517. index + seps);
  1518. seps++;
  1519. }
  1520. normalized = normalize_metadata (EXTRACTOR_METAFORMAT_UTF8,
  1521. p, strlen (p));
  1522. if (normalized != NULL)
  1523. {
  1524. if (GNUNET_NO == find_duplicate ((const char *) normalized,
  1525. (const char **) array, index + seps))
  1526. {
  1527. insert_non_mandatory_keyword ((const char *) normalized, array,
  1528. index + seps);
  1529. seps++;
  1530. }
  1531. GNUNET_free (normalized);
  1532. }
  1533. }
  1534. else
  1535. seps++;
  1536. }
  1537. GNUNET_free (ss);
  1538. return seps;
  1539. }
  1540. #undef TOKENS
  1541. /**
  1542. * Function called on each value in the meta data.
  1543. * Adds it to the URI.
  1544. *
  1545. * @param cls URI to update
  1546. * @param plugin_name name of the plugin that produced this value;
  1547. * special values can be used (i.e. '&lt;zlib&gt;' for zlib being
  1548. * used in the main libextractor library and yielding
  1549. * meta data).
  1550. * @param type libextractor-type describing the meta data
  1551. * @param format basic format information about data
  1552. * @param data_mime_type mime-type of data (not of the original file);
  1553. * can be NULL (if mime-type is not known)
  1554. * @param data actual meta-data found
  1555. * @param data_len number of bytes in @a data
  1556. * @return 0 (always)
  1557. */
  1558. static int
  1559. gather_uri_data (void *cls, const char *plugin_name,
  1560. enum EXTRACTOR_MetaType type,
  1561. enum EXTRACTOR_MetaFormat format,
  1562. const char *data_mime_type,
  1563. const char *data,
  1564. size_t data_len)
  1565. {
  1566. struct GNUNET_FS_Uri *uri = cls;
  1567. char *normalized_data;
  1568. const char *sep;
  1569. if ((format != EXTRACTOR_METAFORMAT_UTF8) &&
  1570. (format != EXTRACTOR_METAFORMAT_C_STRING))
  1571. return 0;
  1572. /* Keywords must be at least 3 characters long
  1573. * If given non-utf8 string it will, most likely, find it to be invalid,
  1574. * and will return the length of its valid part, skipping the keyword.
  1575. * If it does - fix the extractor, not this check!
  1576. */
  1577. if (u8_strcount ((const uint8_t *) data) <= 2)
  1578. return 0;
  1579. if ( (EXTRACTOR_METATYPE_MIMETYPE == type) &&
  1580. (NULL != (sep = memchr (data, '/', data_len))) &&
  1581. (sep != data) )
  1582. {
  1583. char *xtra;
  1584. GNUNET_asprintf (&xtra,
  1585. "mimetype:%.*s",
  1586. (int) (sep - data),
  1587. data);
  1588. if (! find_duplicate (xtra,
  1589. (const char **) uri->data.ksk.keywords,
  1590. uri->data.ksk.keywordCount))
  1591. {
  1592. insert_non_mandatory_keyword (xtra,
  1593. uri->data.ksk.keywords,
  1594. uri->data.ksk.keywordCount);
  1595. uri->data.ksk.keywordCount++;
  1596. }
  1597. GNUNET_free (xtra);
  1598. }
  1599. normalized_data = normalize_metadata (format, data, data_len);
  1600. if (! find_duplicate (data,
  1601. (const char **) uri->data.ksk.keywords,
  1602. uri->data.ksk.keywordCount))
  1603. {
  1604. insert_non_mandatory_keyword (data,
  1605. uri->data.ksk.keywords, uri->data.ksk.keywordCount);
  1606. uri->data.ksk.keywordCount++;
  1607. }
  1608. if (NULL != normalized_data)
  1609. {
  1610. if (! find_duplicate (normalized_data,
  1611. (const char **) uri->data.ksk.keywords,
  1612. uri->data.ksk.keywordCount))
  1613. {
  1614. insert_non_mandatory_keyword (normalized_data,
  1615. uri->data.ksk.keywords, uri->data.ksk.keywordCount);
  1616. uri->data.ksk.keywordCount++;
  1617. }
  1618. GNUNET_free (normalized_data);
  1619. }
  1620. return 0;
  1621. }
  1622. /**
  1623. * Construct a keyword-URI from meta-data (take all entries
  1624. * in the meta-data and construct one large keyword URI
  1625. * that lists all keywords that can be found in the meta-data).
  1626. *
  1627. * @param md metadata to use
  1628. * @return NULL on error, otherwise a KSK URI
  1629. */
  1630. struct GNUNET_FS_Uri *
  1631. GNUNET_FS_uri_ksk_create_from_meta_data (const struct GNUNET_CONTAINER_MetaData *md)
  1632. {
  1633. struct GNUNET_FS_Uri *ret;
  1634. char *filename;
  1635. char *full_name = NULL;
  1636. char *ss;
  1637. int ent;
  1638. int tok_keywords = 0;
  1639. int paren_keywords = 0;
  1640. if (NULL == md)
  1641. return NULL;
  1642. ret = GNUNET_new (struct GNUNET_FS_Uri);
  1643. ret->type = GNUNET_FS_URI_KSK;
  1644. ent = GNUNET_CONTAINER_meta_data_iterate (md, NULL, NULL);
  1645. if (ent > 0)
  1646. {
  1647. full_name = GNUNET_CONTAINER_meta_data_get_first_by_types (md,
  1648. EXTRACTOR_METATYPE_GNUNET_ORIGINAL_FILENAME, -1);
  1649. if (NULL != full_name)
  1650. {
  1651. filename = full_name;
  1652. while (NULL != (ss = strstr (filename, DIR_SEPARATOR_STR)))
  1653. filename = ss + 1;
  1654. tok_keywords = get_keywords_from_tokens (filename, NULL, 0);
  1655. paren_keywords = get_keywords_from_parens (filename, NULL, 0);
  1656. }
  1657. /* x3 because there might be a normalized variant of every keyword,
  1658. plus theoretically one more for mime... */
  1659. ret->data.ksk.keywords
  1660. = GNUNET_new_array ((ent + tok_keywords + paren_keywords) * 3,
  1661. char *);
  1662. GNUNET_CONTAINER_meta_data_iterate (md, &gather_uri_data, ret);
  1663. }
  1664. if (tok_keywords > 0)
  1665. ret->data.ksk.keywordCount += get_keywords_from_tokens (filename,
  1666. ret->data.ksk.keywords,
  1667. ret->data.ksk.keywordCount);
  1668. if (paren_keywords > 0)
  1669. ret->data.ksk.keywordCount += get_keywords_from_parens (filename,
  1670. ret->data.ksk.keywords,
  1671. ret->data.ksk.keywordCount);
  1672. if (ent > 0)
  1673. GNUNET_free_non_null (full_name);
  1674. return ret;
  1675. }
  1676. /**
  1677. * In URI-encoding, does the given character
  1678. * need to be encoded using %-encoding?
  1679. */
  1680. static int
  1681. needs_percent (char c)
  1682. {
  1683. return (!
  1684. ((isalnum ((unsigned char) c)) || (c == '-') || (c == '_') ||
  1685. (c == '.') || (c == '~')));
  1686. }
  1687. /**
  1688. * Convert a KSK URI to a string.
  1689. *
  1690. * @param uri the URI to convert
  1691. * @return NULL on error (i.e. keywordCount == 0)
  1692. */
  1693. static char *
  1694. uri_ksk_to_string (const struct GNUNET_FS_Uri *uri)
  1695. {
  1696. char **keywords;
  1697. unsigned int keywordCount;
  1698. size_t n;
  1699. char *ret;
  1700. unsigned int i;
  1701. unsigned int j;
  1702. unsigned int wpos;
  1703. size_t slen;
  1704. const char *keyword;
  1705. if (uri->type != GNUNET_FS_URI_KSK)
  1706. return NULL;
  1707. keywords = uri->data.ksk.keywords;
  1708. keywordCount = uri->data.ksk.keywordCount;
  1709. n = keywordCount + strlen (GNUNET_FS_URI_PREFIX) +
  1710. strlen (GNUNET_FS_URI_KSK_INFIX) + 1;
  1711. for (i = 0; i < keywordCount; i++)
  1712. {
  1713. keyword = keywords[i];
  1714. slen = strlen (keyword);
  1715. n += slen;
  1716. for (j = 0; j < slen; j++)
  1717. {
  1718. if ((j == 0) && (keyword[j] == ' '))
  1719. {
  1720. n--;
  1721. continue; /* skip leading space */
  1722. }
  1723. if (needs_percent (keyword[j]))
  1724. n += 2; /* will use %-encoding */
  1725. }
  1726. }
  1727. ret = GNUNET_malloc (n);
  1728. strcpy (ret, GNUNET_FS_URI_PREFIX);
  1729. strcat (ret, GNUNET_FS_URI_KSK_INFIX);
  1730. wpos = strlen (ret);
  1731. for (i = 0; i < keywordCount; i++)
  1732. {
  1733. keyword = keywords[i];
  1734. slen = strlen (keyword);
  1735. for (j = 0; j < slen; j++)
  1736. {
  1737. if ((j == 0) && (keyword[j] == ' '))
  1738. continue; /* skip leading space */
  1739. if (needs_percent (keyword[j]))
  1740. {
  1741. sprintf (&ret[wpos], "%%%02X", (unsigned char) keyword[j]);
  1742. wpos += 3;
  1743. }
  1744. else
  1745. {
  1746. ret[wpos++] = keyword[j];
  1747. }
  1748. }
  1749. if (i != keywordCount - 1)
  1750. ret[wpos++] = '+';
  1751. }
  1752. return ret;
  1753. }
  1754. /**
  1755. * Convert SKS URI to a string.
  1756. *
  1757. * @param uri sks uri to convert
  1758. * @return NULL on error
  1759. */
  1760. static char *
  1761. uri_sks_to_string (const struct GNUNET_FS_Uri *uri)
  1762. {
  1763. char *ret;
  1764. char buf[1024];
  1765. if (GNUNET_FS_URI_SKS != uri->type)
  1766. return NULL;
  1767. ret = GNUNET_STRINGS_data_to_string (&uri->data.sks.ns,
  1768. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey),
  1769. buf,
  1770. sizeof (buf));
  1771. GNUNET_assert (NULL != ret);
  1772. ret[0] = '\0';
  1773. GNUNET_asprintf (&ret, "%s%s%s/%s", GNUNET_FS_URI_PREFIX,
  1774. GNUNET_FS_URI_SKS_INFIX, buf,
  1775. uri->data.sks.identifier);
  1776. return ret;
  1777. }
  1778. /**
  1779. * Convert a CHK URI to a string.
  1780. *
  1781. * @param uri chk uri to convert
  1782. * @return NULL on error
  1783. */
  1784. static char *
  1785. uri_chk_to_string (const struct GNUNET_FS_Uri *uri)
  1786. {
  1787. const struct FileIdentifier *fi;
  1788. char *ret;
  1789. struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
  1790. struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
  1791. if (uri->type != GNUNET_FS_URI_CHK)
  1792. return NULL;
  1793. fi = &uri->data.chk;
  1794. GNUNET_CRYPTO_hash_to_enc (&fi->chk.key, &keyhash);
  1795. GNUNET_CRYPTO_hash_to_enc (&fi->chk.query, &queryhash);
  1796. GNUNET_asprintf (&ret, "%s%s%s.%s.%llu", GNUNET_FS_URI_PREFIX,
  1797. GNUNET_FS_URI_CHK_INFIX, (const char *) &keyhash,
  1798. (const char *) &queryhash, GNUNET_ntohll (fi->file_length));
  1799. return ret;
  1800. }
  1801. /**
  1802. * Convert a LOC URI to a string.
  1803. *
  1804. * @param uri loc uri to convert
  1805. * @return NULL on error
  1806. */
  1807. static char *
  1808. uri_loc_to_string (const struct GNUNET_FS_Uri *uri)
  1809. {
  1810. char *ret;
  1811. struct GNUNET_CRYPTO_HashAsciiEncoded keyhash;
  1812. struct GNUNET_CRYPTO_HashAsciiEncoded queryhash;
  1813. char *peer_id;
  1814. char peer_sig[SIGNATURE_ASCII_LENGTH + 1];
  1815. GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.key, &keyhash);
  1816. GNUNET_CRYPTO_hash_to_enc (&uri->data.loc.fi.chk.query, &queryhash);
  1817. peer_id =
  1818. GNUNET_CRYPTO_eddsa_public_key_to_string (&uri->data.loc.peer.public_key);
  1819. GNUNET_assert (NULL !=
  1820. GNUNET_STRINGS_data_to_string (&uri->data.loc.contentSignature,
  1821. sizeof (struct GNUNET_CRYPTO_EddsaSignature),
  1822. peer_sig,
  1823. sizeof (peer_sig)));
  1824. GNUNET_asprintf (&ret,
  1825. "%s%s%s.%s.%llu.%s.%s.%llu", GNUNET_FS_URI_PREFIX,
  1826. GNUNET_FS_URI_LOC_INFIX, (const char *) &keyhash,
  1827. (const char *) &queryhash,
  1828. (unsigned long long) GNUNET_ntohll (uri->data.loc.
  1829. fi.file_length),
  1830. peer_id,
  1831. peer_sig,
  1832. (unsigned long long) uri->data.loc.expirationTime.abs_value_us / 1000000LL);
  1833. GNUNET_free (peer_id);
  1834. return ret;
  1835. }
  1836. /**
  1837. * Convert a URI to a UTF-8 String.
  1838. *
  1839. * @param uri uri to convert to a string
  1840. * @return the UTF-8 string
  1841. */
  1842. char *
  1843. GNUNET_FS_uri_to_string (const struct GNUNET_FS_Uri *uri)
  1844. {
  1845. if (uri == NULL)
  1846. {
  1847. GNUNET_break (0);
  1848. return NULL;
  1849. }
  1850. switch (uri->type)
  1851. {
  1852. case GNUNET_FS_URI_KSK:
  1853. return uri_ksk_to_string (uri);
  1854. case GNUNET_FS_URI_SKS:
  1855. return uri_sks_to_string (uri);
  1856. case GNUNET_FS_URI_CHK:
  1857. return uri_chk_to_string (uri);
  1858. case GNUNET_FS_URI_LOC:
  1859. return uri_loc_to_string (uri);
  1860. default:
  1861. GNUNET_break (0);
  1862. return NULL;
  1863. }
  1864. }
  1865. /* end of fs_uri.c */