gnunet_strings_lib.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2001-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file include/gnunet_strings_lib.h
  19. * @brief strings and string handling functions (including malloc
  20. * and string tokenizing)
  21. *
  22. * @author Christian Grothoff
  23. * @author Krista Bennett
  24. * @author Gerd Knorr <kraxel@bytesex.org>
  25. * @author Ioana Patrascu
  26. * @author Tzvetan Horozov
  27. */
  28. #ifndef GNUNET_STRINGS_LIB_H
  29. #define GNUNET_STRINGS_LIB_H
  30. /* we need size_t, and since it can be both unsigned int
  31. or unsigned long long, this IS platform dependent;
  32. but "stdlib.h" should be portable 'enough' to be
  33. unconditionally available... */
  34. #include <stdlib.h>
  35. #ifdef __cplusplus
  36. extern "C"
  37. {
  38. #if 0 /* keep Emacsens' auto-indent happy */
  39. }
  40. #endif
  41. #endif
  42. #include "gnunet_time_lib.h"
  43. /**
  44. * Convert a given fancy human-readable size to bytes.
  45. *
  46. * @param fancy_size human readable string (i.e. 1 MB)
  47. * @param size set to the size in bytes
  48. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  49. */
  50. int
  51. GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
  52. unsigned long long *size);
  53. /**
  54. * Convert a given fancy human-readable time to our internal
  55. * representation.
  56. *
  57. * @param fancy_time human readable string (i.e. 1 minute)
  58. * @param rtime set to the relative time
  59. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  60. */
  61. int
  62. GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
  63. struct GNUNET_TIME_Relative *rtime);
  64. /**
  65. * @ingroup time
  66. * Convert a given fancy human-readable time to our internal
  67. * representation. The human-readable time is expected to be
  68. * in local time, whereas the returned value will be in UTC.
  69. *
  70. * @param fancy_time human readable string (i.e. %Y-%m-%d %H:%M:%S)
  71. * @param atime set to the absolute time
  72. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  73. */
  74. int
  75. GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
  76. struct GNUNET_TIME_Absolute *atime);
  77. /**
  78. * Convert a given filesize into a fancy human-readable format.
  79. *
  80. * @param size number of bytes
  81. * @return fancy representation of the size (possibly rounded) for humans
  82. */
  83. char *
  84. GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
  85. /**
  86. * Convert the len characters long character sequence
  87. * given in input that is in the given input charset
  88. * to a string in given output charset.
  89. *
  90. * @param input input string
  91. * @param len number of bytes in @a input
  92. * @param input_charset character set used for @a input
  93. * @param output_charset desired character set for the return value
  94. * @return the converted string (0-terminated),
  95. * if conversion fails, a copy of the orignal
  96. * string is returned.
  97. */
  98. char *
  99. GNUNET_STRINGS_conv (const char *input, size_t len,
  100. const char *input_charset,
  101. const char *output_charset);
  102. /**
  103. * Convert the len characters long character sequence
  104. * given in input that is in the given charset
  105. * to UTF-8.
  106. *
  107. * @param input the input string (not necessarily 0-terminated)
  108. * @param len the number of bytes in the @a input
  109. * @param charset character set to convert from
  110. * @return the converted string (0-terminated)
  111. */
  112. char *
  113. GNUNET_STRINGS_to_utf8 (const char *input,
  114. size_t len,
  115. const char *charset);
  116. /**
  117. * Convert the len bytes-long UTF-8 string
  118. * given in input to the given charset.
  119. *
  120. * @param input the input string (not necessarily 0-terminated)
  121. * @param len the number of bytes in the @a input
  122. * @param charset character set to convert to
  123. * @return the converted string (0-terminated),
  124. * if conversion fails, a copy of the orignal
  125. * string is returned.
  126. */
  127. char *
  128. GNUNET_STRINGS_from_utf8 (const char *input,
  129. size_t len,
  130. const char *charset);
  131. /**
  132. * Convert the utf-8 input string to lower case.
  133. * Output needs to be allocated appropriately.
  134. *
  135. * @param input input string
  136. * @param output output buffer
  137. */
  138. void
  139. GNUNET_STRINGS_utf8_tolower (const char *input,
  140. char *output);
  141. /**
  142. * Convert the utf-8 input string to upper case.
  143. * Output needs to be allocated appropriately.
  144. *
  145. * @param input input string
  146. * @param output output buffer
  147. */
  148. void
  149. GNUNET_STRINGS_utf8_toupper (const char *input,
  150. char *output);
  151. /**
  152. * Complete filename (a la shell) from abbrevition.
  153. *
  154. * @param fil the name of the file, may contain ~/ or
  155. * be relative to the current directory
  156. * @return the full file name,
  157. * NULL is returned on error
  158. */
  159. char *
  160. GNUNET_STRINGS_filename_expand (const char *fil);
  161. /**
  162. * Fill a buffer of the given size with count 0-terminated strings
  163. * (given as varargs). If "buffer" is NULL, only compute the amount
  164. * of space required (sum of "strlen(arg)+1").
  165. *
  166. * Unlike using "snprintf" with "%s", this function will add
  167. * 0-terminators after each string. The
  168. * "GNUNET_string_buffer_tokenize" function can be used to parse the
  169. * buffer back into individual strings.
  170. *
  171. * @param buffer the buffer to fill with strings, can
  172. * be NULL in which case only the necessary
  173. * amount of space will be calculated
  174. * @param size number of bytes available in buffer
  175. * @param count number of strings that follow
  176. * @param ... count 0-terminated strings to copy to buffer
  177. * @return number of bytes written to the buffer
  178. * (or number of bytes that would have been written)
  179. */
  180. size_t
  181. GNUNET_STRINGS_buffer_fill (char *buffer,
  182. size_t size,
  183. unsigned int count,
  184. ...);
  185. /**
  186. * Given a buffer of a given size, find "count" 0-terminated strings
  187. * in the buffer and assign the count (varargs) of type "const char**"
  188. * to the locations of the respective strings in the buffer.
  189. *
  190. * @param buffer the buffer to parse
  191. * @param size size of the @a buffer
  192. * @param count number of strings to locate
  193. * @param ... pointers to where to store the strings
  194. * @return offset of the character after the last 0-termination
  195. * in the buffer, or 0 on error.
  196. */
  197. unsigned int
  198. GNUNET_STRINGS_buffer_tokenize (const char *buffer,
  199. size_t size,
  200. unsigned int count, ...);
  201. /**
  202. * @ingroup time
  203. * Like `asctime`, except for GNUnet time. Converts a GNUnet internal
  204. * absolute time (which is in UTC) to a string in local time.
  205. * Note that the returned value will be overwritten if this function
  206. * is called again.
  207. *
  208. * @param t the absolute time to convert
  209. * @return timestamp in human-readable form in local time
  210. */
  211. const char *
  212. GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t);
  213. /**
  214. * @ingroup time
  215. * Give relative time in human-readable fancy format.
  216. * This is one of the very few calls in the entire API that is
  217. * NOT reentrant!
  218. *
  219. * @param delta time in milli seconds
  220. * @param do_round are we allowed to round a bit?
  221. * @return string in human-readable form
  222. */
  223. const char *
  224. GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
  225. int do_round);
  226. /**
  227. * "man basename"
  228. * Returns a pointer to a part of filename (allocates nothing)!
  229. *
  230. * @param filename filename to extract basename from
  231. * @return short (base) name of the file (that is, everything following the
  232. * last directory separator in filename. If filename ends with a
  233. * directory separator, the result will be a zero-length string.
  234. * If filename has no directory separators, the result is filename
  235. * itself.
  236. */
  237. const char *
  238. GNUNET_STRINGS_get_short_name (const char *filename);
  239. /**
  240. * Convert binary data to ASCII encoding using Base32Hex (RFC 4648).
  241. * Does not append 0-terminator, but returns a pointer to the place where
  242. * it should be placed, if needed.
  243. *
  244. * @param data data to encode
  245. * @param size size of data (in bytes)
  246. * @param out buffer to fill
  247. * @param out_size size of the buffer. Must be large enough to hold
  248. * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
  249. * @return pointer to the next byte in 'out' or NULL on error.
  250. */
  251. char *
  252. GNUNET_STRINGS_data_to_string (const void *data,
  253. size_t size,
  254. char *out,
  255. size_t out_size);
  256. /**
  257. * Convert Base32hex encoding back to data.
  258. * @a out_size must match exactly the size of the data before it was encoded.
  259. *
  260. * @param enc the encoding
  261. * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
  262. * @param out location where to store the decoded data
  263. * @param out_size size of the output buffer @a out
  264. * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
  265. */
  266. int
  267. GNUNET_STRINGS_string_to_data (const char *enc,
  268. size_t enclen,
  269. void *out,
  270. size_t out_size);
  271. /**
  272. * Encode into Base64.
  273. *
  274. * @param data the data to encode
  275. * @param len the length of the input
  276. * @param output where to write the output (*output should be NULL,
  277. * is allocated)
  278. * @return the size of the output
  279. */
  280. size_t
  281. GNUNET_STRINGS_base64_encode (const char *data, size_t len, char **output);
  282. /**
  283. * Decode from Base64.
  284. *
  285. * @param data the data to encode
  286. * @param len the length of the input
  287. * @param output where to write the output (*output should be NULL,
  288. * is allocated)
  289. * @return the size of the output
  290. */
  291. size_t
  292. GNUNET_STRINGS_base64_decode (const char *data, size_t len, char **output);
  293. /**
  294. * Parse a path that might be an URI.
  295. *
  296. * @param path path to parse. Must be NULL-terminated.
  297. * @param scheme_part a pointer to 'char *' where a pointer to a string that
  298. * represents the URI scheme will be stored. Can be NULL. The string is
  299. * allocated by the function, and should be freed by GNUNET_free() when
  300. * it is no longer needed.
  301. * @param path_part a pointer to 'const char *' where a pointer to the path
  302. * part of the URI will be stored. Can be NULL. Points to the same block
  303. * of memory as 'path', and thus must not be freed. Might point to '\0',
  304. * if path part is zero-length.
  305. * @return #GNUNET_YES if it's an URI, #GNUNET_NO otherwise. If 'path' is not
  306. * an URI, '* scheme_part' and '*path_part' will remain unchanged
  307. * (if they weren't NULL).
  308. */
  309. int
  310. GNUNET_STRINGS_parse_uri (const char *path,
  311. char **scheme_part,
  312. const char **path_part);
  313. /**
  314. * Check whether filename is absolute or not, and if it's an URI
  315. *
  316. * @param filename filename to check
  317. * @param can_be_uri #GNUNET_YES to check for being URI, #GNUNET_NO - to
  318. * assume it's not URI
  319. * @param r_is_uri a pointer to an int that is set to #GNUNET_YES if 'filename'
  320. * is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is
  321. * not #GNUNET_YES, *r_is_uri is set to #GNUNET_NO.
  322. * @param r_uri_scheme a pointer to a char * that is set to a pointer to URI scheme.
  323. * The string is allocated by the function, and should be freed with
  324. * GNUNET_free (). Can be NULL.
  325. * @return #GNUNET_YES if 'filename' is absolute, #GNUNET_NO otherwise.
  326. */
  327. int
  328. GNUNET_STRINGS_path_is_absolute (const char *filename,
  329. int can_be_uri,
  330. int *r_is_uri,
  331. char **r_uri_scheme);
  332. /**
  333. * Flags for what we should check a file for.
  334. */
  335. enum GNUNET_STRINGS_FilenameCheck
  336. {
  337. /**
  338. * Check that it exists.
  339. */
  340. GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
  341. /**
  342. * Check that it is a directory.
  343. */
  344. GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002,
  345. /**
  346. * Check that it is a link.
  347. */
  348. GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
  349. /**
  350. * Check that the path is an absolute path.
  351. */
  352. GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008
  353. };
  354. /**
  355. * Perform checks on @a filename. FIXME: some duplication with
  356. * "GNUNET_DISK_"-APIs. We should unify those.
  357. *
  358. * @param filename file to check
  359. * @param checks checks to perform
  360. * @return #GNUNET_YES if all checks pass, #GNUNET_NO if at least one of them
  361. * fails, #GNUNET_SYSERR when a check can't be performed
  362. */
  363. int
  364. GNUNET_STRINGS_check_filename (const char *filename,
  365. enum GNUNET_STRINGS_FilenameCheck checks);
  366. /**
  367. * Tries to convert @a zt_addr string to an IPv6 address.
  368. * The string is expected to have the format "[ABCD::01]:80".
  369. *
  370. * @param zt_addr 0-terminated string. May be mangled by the function.
  371. * @param addrlen length of zt_addr (not counting 0-terminator).
  372. * @param r_buf a buffer to fill. Initially gets filled with zeroes,
  373. * then its sin6_port, sin6_family and sin6_addr are set appropriately.
  374. * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
  375. * case the contents of r_buf are undefined.
  376. */
  377. int
  378. GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
  379. uint16_t addrlen,
  380. struct sockaddr_in6 *r_buf);
  381. /**
  382. * Tries to convert @a zt_addr string to an IPv4 address.
  383. * The string is expected to have the format "1.2.3.4:80".
  384. *
  385. * @param zt_addr 0-terminated string. May be mangled by the function.
  386. * @param addrlen length of zt_addr (not counting 0-terminator).
  387. * @param r_buf a buffer to fill.
  388. * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which case
  389. * the contents of r_buf are undefined.
  390. */
  391. int
  392. GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
  393. uint16_t addrlen,
  394. struct sockaddr_in *r_buf);
  395. /**
  396. * Tries to convert @a addr string to an IP (v4 or v6) address.
  397. * Will automatically decide whether to treat 'addr' as v4 or v6 address.
  398. *
  399. * @param addr a string, may not be 0-terminated.
  400. * @param addrlen number of bytes in @a addr (if addr is 0-terminated,
  401. * 0-terminator should not be counted towards addrlen).
  402. * @param r_buf a buffer to fill.
  403. * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
  404. * case the contents of r_buf are undefined.
  405. */
  406. int
  407. GNUNET_STRINGS_to_address_ip (const char *addr,
  408. uint16_t addrlen,
  409. struct sockaddr_storage *r_buf);
  410. /**
  411. * Returns utf-8 encoded arguments. Does nothing (returns a copy of
  412. * @a argc and @a argv) on any platform other than W32. Returned @a
  413. * argv has `u8argv[u8argc] == NULL`. Returned @a argv is a single
  414. * memory block, and can be freed with a single GNUNET_free() call.
  415. *
  416. * @param argc argc (as given by main())
  417. * @param argv argv (as given by main())
  418. * @param u8argc a location to store new argc in (though it's th same as argc)
  419. * @param u8argv a location to store new argv in
  420. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  421. */
  422. int
  423. GNUNET_STRINGS_get_utf8_args (int argc,
  424. char *const *argv,
  425. int *u8argc,
  426. char *const **u8argv);
  427. /* ***************** IPv4/IPv6 parsing ****************** */
  428. struct GNUNET_STRINGS_PortPolicy
  429. {
  430. /**
  431. * Starting port range (0 if none given).
  432. */
  433. uint16_t start_port;
  434. /**
  435. * End of port range (0 if none given).
  436. */
  437. uint16_t end_port;
  438. /**
  439. * #GNUNET_YES if the port range should be negated
  440. * ("!" in policy).
  441. */
  442. int negate_portrange;
  443. };
  444. /**
  445. * @brief IPV4 network in CIDR notation.
  446. */
  447. struct GNUNET_STRINGS_IPv4NetworkPolicy
  448. {
  449. /**
  450. * IPv4 address.
  451. */
  452. struct in_addr network;
  453. /**
  454. * IPv4 netmask.
  455. */
  456. struct in_addr netmask;
  457. /**
  458. * Policy for port access.
  459. */
  460. struct GNUNET_STRINGS_PortPolicy pp;
  461. };
  462. /**
  463. * @brief network in CIDR notation for IPV6.
  464. */
  465. struct GNUNET_STRINGS_IPv6NetworkPolicy
  466. {
  467. /**
  468. * IPv6 address.
  469. */
  470. struct in6_addr network;
  471. /**
  472. * IPv6 netmask.
  473. */
  474. struct in6_addr netmask;
  475. /**
  476. * Policy for port access.
  477. */
  478. struct GNUNET_STRINGS_PortPolicy pp;
  479. };
  480. /**
  481. * Parse an IPv4 network policy. The argument specifies a list of
  482. * subnets. The format is <tt>(network[/netmask][:[!]SPORT-DPORT];)*</tt>
  483. * (no whitespace, must be terminated with a semicolon). The network
  484. * must be given in dotted-decimal notation. The netmask can be given
  485. * in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).
  486. *
  487. * @param routeListX a string specifying the IPv4 subnets
  488. * @return the converted list, terminated with all zeros;
  489. * NULL if the synatx is flawed
  490. */
  491. struct GNUNET_STRINGS_IPv4NetworkPolicy *
  492. GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX);
  493. /**
  494. * Parse an IPv6 network policy. The argument specifies a list of
  495. * subnets. The format is <tt>(network[/netmask[:[!]SPORT[-DPORT]]];)*</tt>
  496. * (no whitespace, must be terminated with a semicolon). The network
  497. * must be given in colon-hex notation. The netmask must be given in
  498. * CIDR notation (/16) or can be omitted to specify a single host.
  499. * Note that the netmask is mandatory if ports are specified.
  500. *
  501. * @param routeListX a string specifying the policy
  502. * @return the converted list, 0-terminated, NULL if the synatx is flawed
  503. */
  504. struct GNUNET_STRINGS_IPv6NetworkPolicy *
  505. GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX);
  506. #if 0 /* keep Emacsens' auto-indent happy */
  507. {
  508. #endif
  509. #ifdef __cplusplus
  510. }
  511. #endif
  512. /* ifndef GNUNET_UTIL_STRING_H */
  513. #endif
  514. /* end of gnunet_util_string.h */