2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 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.
22 * @author Christian Grothoff
23 * @author Krista Bennett
24 * @author Gerd Knorr <kraxel@bytesex.org>
25 * @author Ioana Patrascu
26 * @author Tzvetan Horozov
29 * Strings and string handling functions
31 * @defgroup strings Strings library
32 * Strings and string handling functions, including malloc and string tokenizing.
36 #ifndef GNUNET_STRINGS_LIB_H
37 #define GNUNET_STRINGS_LIB_H
39 /* we need size_t, and since it can be both unsigned int
40 or unsigned long long, this IS platform dependent;
41 but "stdlib.h" should be portable 'enough' to be
42 unconditionally available... */
48 #if 0 /* keep Emacsens' auto-indent happy */
53 #include "gnunet_time_lib.h"
57 * Convert a given fancy human-readable size to bytes.
59 * @param fancy_size human readable string (i.e. 1 MB)
60 * @param size set to the size in bytes
61 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
64 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
65 unsigned long long *size);
69 * Convert a given fancy human-readable time to our internal
72 * @param fancy_time human readable string (i.e. 1 minute)
73 * @param rtime set to the relative time
74 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
77 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
78 struct GNUNET_TIME_Relative *rtime);
83 * Convert a given fancy human-readable time to our internal
84 * representation. The human-readable time is expected to be
85 * in local time, whereas the returned value will be in UTC.
87 * @param fancy_time human readable string (i.e. %Y-%m-%d %H:%M:%S)
88 * @param atime set to the absolute time
89 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
92 GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
93 struct GNUNET_TIME_Absolute *atime);
97 * Convert a given filesize into a fancy human-readable format.
99 * @param size number of bytes
100 * @return fancy representation of the size (possibly rounded) for humans
103 GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
107 * Convert the len characters long character sequence
108 * given in input that is in the given input charset
109 * to a string in given output charset.
111 * @param input input string
112 * @param len number of bytes in @a input
113 * @param input_charset character set used for @a input
114 * @param output_charset desired character set for the return value
115 * @return the converted string (0-terminated),
116 * if conversion fails, a copy of the orignal
117 * string is returned.
120 GNUNET_STRINGS_conv (const char *input, size_t len,
121 const char *input_charset,
122 const char *output_charset);
126 * Convert the len characters long character sequence
127 * given in input that is in the given charset
130 * @param input the input string (not necessarily 0-terminated)
131 * @param len the number of bytes in the @a input
132 * @param charset character set to convert from
133 * @return the converted string (0-terminated)
136 GNUNET_STRINGS_to_utf8 (const char *input,
138 const char *charset);
142 * Convert the len bytes-long UTF-8 string
143 * given in input to the given charset.
145 * @param input the input string (not necessarily 0-terminated)
146 * @param len the number of bytes in the @a input
147 * @param charset character set to convert to
148 * @return the converted string (0-terminated),
149 * if conversion fails, a copy of the orignal
150 * string is returned.
153 GNUNET_STRINGS_from_utf8 (const char *input,
155 const char *charset);
159 * Convert the utf-8 input string to lower case.
160 * Output needs to be allocated appropriately.
162 * @param input input string
163 * @param output output buffer
166 GNUNET_STRINGS_utf8_tolower (const char *input,
171 * Convert the utf-8 input string to upper case.
172 * Output needs to be allocated appropriately.
174 * @param input input string
175 * @param output output buffer
178 GNUNET_STRINGS_utf8_toupper (const char *input,
183 * Complete filename (a la shell) from abbrevition.
185 * @param fil the name of the file, may contain ~/ or
186 * be relative to the current directory
187 * @return the full file name,
188 * NULL is returned on error
191 GNUNET_STRINGS_filename_expand (const char *fil);
195 * Fill a buffer of the given size with count 0-terminated strings
196 * (given as varargs). If "buffer" is NULL, only compute the amount
197 * of space required (sum of "strlen(arg)+1").
199 * Unlike using "snprintf" with "%s", this function will add
200 * 0-terminators after each string. The
201 * "GNUNET_string_buffer_tokenize" function can be used to parse the
202 * buffer back into individual strings.
204 * @param buffer the buffer to fill with strings, can
205 * be NULL in which case only the necessary
206 * amount of space will be calculated
207 * @param size number of bytes available in buffer
208 * @param count number of strings that follow
209 * @param ... count 0-terminated strings to copy to buffer
210 * @return number of bytes written to the buffer
211 * (or number of bytes that would have been written)
214 GNUNET_STRINGS_buffer_fill (char *buffer,
221 * Given a buffer of a given size, find "count" 0-terminated strings
222 * in the buffer and assign the count (varargs) of type "const char**"
223 * to the locations of the respective strings in the buffer.
225 * @param buffer the buffer to parse FIXME: not 'const', is it?
226 * @param size size of the @a buffer
227 * @param count number of strings to locate
228 * @param ... pointers to where to store the strings
229 * @return offset of the character after the last 0-termination
230 * in the buffer, or 0 on error.
233 GNUNET_STRINGS_buffer_tokenize (const char *buffer,
235 unsigned int count, ...);
241 * Like `asctime`, except for GNUnet time. Converts a GNUnet internal
242 * absolute time (which is in UTC) to a string in local time.
243 * Note that the returned value will be overwritten if this function
246 * @param t the absolute time to convert
247 * @return timestamp in human-readable form in local time
250 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t);
255 * Give relative time in human-readable fancy format.
256 * This is one of the very few calls in the entire API that is
259 * @param delta time in milli seconds
260 * @param do_round are we allowed to round a bit?
261 * @return string in human-readable form
264 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
270 * Returns a pointer to a part of filename (allocates nothing)!
272 * @param filename filename to extract basename from
273 * @return short (base) name of the file (that is, everything following the
274 * last directory separator in filename. If filename ends with a
275 * directory separator, the result will be a zero-length string.
276 * If filename has no directory separators, the result is filename
280 GNUNET_STRINGS_get_short_name (const char *filename);
284 * Convert binary data to ASCII encoding using Base32Hex (RFC 4648).
285 * Does not append 0-terminator, but returns a pointer to the place where
286 * it should be placed, if needed.
288 * @param data data to encode
289 * @param size size of data (in bytes)
290 * @param out buffer to fill
291 * @param out_size size of the buffer. Must be large enough to hold
292 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
293 * @return pointer to the next byte in 'out' or NULL on error.
296 GNUNET_STRINGS_data_to_string (const void *data,
303 * Return the base32crockford encoding of the given buffer.
305 * The returned string will be freshly allocated, and must be free'd
306 * with #GNUNET_free().
308 * @param buf buffer with data
309 * @param size size of the buffer @a buf
310 * @return freshly allocated, null-terminated string
313 GNUNET_STRINGS_data_to_string_alloc (const void *buf,
318 * Convert Base32hex encoding back to data.
319 * @a out_size must match exactly the size of the data before it was encoded.
321 * @param enc the encoding
322 * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
323 * @param out location where to store the decoded data
324 * @param out_size size of the output buffer @a out
325 * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
328 GNUNET_STRINGS_string_to_data (const char *enc,
335 * Encode into Base64.
337 * @param data the data to encode
338 * @param len the length of the input
339 * @param output where to write the output (*output should be NULL,
341 * @return the size of the output
344 GNUNET_STRINGS_base64_encode (const char *data, size_t len, char **output);
348 * Decode from Base64.
350 * @param data the data to encode
351 * @param len the length of the input
352 * @param[out] output where to write the output (*output should be NULL,
354 * @return the size of the output
357 GNUNET_STRINGS_base64_decode (const char *data,
363 * Parse a path that might be an URI.
365 * @param path path to parse. Must be NULL-terminated.
366 * @param[out] scheme_part pointer to a string that
367 * represents the URI scheme will be stored. Can be NULL. The string is
368 * allocated by the function, and should be freed by GNUNET_free() when
369 * it is no longer needed.
370 * @param path_part a pointer to 'const char *' where a pointer to the path
371 * part of the URI will be stored. Can be NULL. Points to the same block
372 * of memory as @a path, and thus must not be freed. Might point to '\0',
373 * if path part is zero-length.
374 * @return #GNUNET_YES if it's an URI, #GNUNET_NO otherwise. If 'path' is not
375 * an URI, '* scheme_part' and '*path_part' will remain unchanged
376 * (if they weren't NULL).
379 GNUNET_STRINGS_parse_uri (const char *path,
381 const char **path_part);
385 * Check whether filename is absolute or not, and if it's an URI
387 * @param filename filename to check
388 * @param can_be_uri #GNUNET_YES to check for being URI, #GNUNET_NO - to
389 * assume it's not URI
390 * @param r_is_uri a pointer to an int that is set to #GNUNET_YES if 'filename'
391 * is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is
392 * not #GNUNET_YES, *r_is_uri is set to #GNUNET_NO.
393 * @param r_uri_scheme a pointer to a char * that is set to a pointer to URI scheme.
394 * The string is allocated by the function, and should be freed with
395 * GNUNET_free (). Can be NULL.
396 * @return #GNUNET_YES if 'filename' is absolute, #GNUNET_NO otherwise.
399 GNUNET_STRINGS_path_is_absolute (const char *filename,
402 char **r_uri_scheme);
406 * Flags for what we should check a file for.
408 enum GNUNET_STRINGS_FilenameCheck
411 * Check that it exists.
413 GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
416 * Check that it is a directory.
418 GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002,
421 * Check that it is a link.
423 GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
426 * Check that the path is an absolute path.
428 GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008
433 * Perform checks on @a filename. FIXME: some duplication with
434 * "GNUNET_DISK_"-APIs. We should unify those.
436 * @param filename file to check
437 * @param checks checks to perform
438 * @return #GNUNET_YES if all checks pass, #GNUNET_NO if at least one of them
439 * fails, #GNUNET_SYSERR when a check can't be performed
442 GNUNET_STRINGS_check_filename (const char *filename,
443 enum GNUNET_STRINGS_FilenameCheck checks);
447 * Tries to convert @a zt_addr string to an IPv6 address.
448 * The string is expected to have the format "[ABCD::01]:80".
450 * @param zt_addr 0-terminated string. May be mangled by the function.
451 * @param addrlen length of zt_addr (not counting 0-terminator).
452 * @param r_buf a buffer to fill. Initially gets filled with zeroes,
453 * then its sin6_port, sin6_family and sin6_addr are set appropriately.
454 * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
455 * case the contents of r_buf are undefined.
458 GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
460 struct sockaddr_in6 *r_buf);
464 * Tries to convert @a zt_addr string to an IPv4 address.
465 * The string is expected to have the format "1.2.3.4:80".
467 * @param zt_addr 0-terminated string. May be mangled by the function.
468 * @param addrlen length of zt_addr (not counting 0-terminator).
469 * @param r_buf a buffer to fill.
470 * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which case
471 * the contents of r_buf are undefined.
474 GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
476 struct sockaddr_in *r_buf);
480 * Parse an address given as a string into a
483 * @param addr the address
484 * @param[out] af set to the parsed address family (i.e. AF_INET)
485 * @param[out] sa set to the parsed address
486 * @return 0 on error, otherwise number of bytes in @a sa
489 GNUNET_STRINGS_parse_socket_addr (const char *addr,
491 struct sockaddr **sa);
495 * Tries to convert @a addr string to an IP (v4 or v6) address.
496 * Will automatically decide whether to treat 'addr' as v4 or v6 address.
498 * @param addr a string, may not be 0-terminated.
499 * @param addrlen number of bytes in @a addr (if addr is 0-terminated,
500 * 0-terminator should not be counted towards addrlen).
501 * @param r_buf a buffer to fill.
502 * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
503 * case the contents of r_buf are undefined.
506 GNUNET_STRINGS_to_address_ip (const char *addr,
508 struct sockaddr_storage *r_buf);
512 * Returns utf-8 encoded arguments. Does nothing (returns a copy of
513 * @a argc and @a argv) on any platform other than W32. Returned @a
514 * argv has `u8argv[u8argc] == NULL`. Returned @a argv is a single
515 * memory block, and can be freed with a single GNUNET_free() call.
517 * @param argc argc (as given by main())
518 * @param argv argv (as given by main())
519 * @param u8argc a location to store new argc in (though it's th same as argc)
520 * @param u8argv a location to store new argv in
521 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
524 GNUNET_STRINGS_get_utf8_args (int argc,
527 char *const **u8argv);
530 /* ***************** IPv4/IPv6 parsing ****************** */
532 struct GNUNET_STRINGS_PortPolicy
536 * Starting port range (0 if none given).
541 * End of port range (0 if none given).
546 * #GNUNET_YES if the port range should be negated
549 int negate_portrange;
555 * @brief IPV4 network in CIDR notation.
557 struct GNUNET_STRINGS_IPv4NetworkPolicy
562 struct in_addr network;
567 struct in_addr netmask;
570 * Policy for port access.
572 struct GNUNET_STRINGS_PortPolicy pp;
578 * @brief network in CIDR notation for IPV6.
580 struct GNUNET_STRINGS_IPv6NetworkPolicy
585 struct in6_addr network;
590 struct in6_addr netmask;
593 * Policy for port access.
595 struct GNUNET_STRINGS_PortPolicy pp;
601 * Parse an IPv4 network policy. The argument specifies a list of
602 * subnets. The format is <tt>(network[/netmask][:[!]SPORT-DPORT];)*</tt>
603 * (no whitespace, must be terminated with a semicolon). The network
604 * must be given in dotted-decimal notation. The netmask can be given
605 * in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).
607 * @param routeListX a string specifying the IPv4 subnets
608 * @return the converted list, terminated with all zeros;
609 * NULL if the synatx is flawed
611 struct GNUNET_STRINGS_IPv4NetworkPolicy *
612 GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX);
616 * Parse an IPv6 network policy. The argument specifies a list of
617 * subnets. The format is <tt>(network[/netmask[:[!]SPORT[-DPORT]]];)*</tt>
618 * (no whitespace, must be terminated with a semicolon). The network
619 * must be given in colon-hex notation. The netmask must be given in
620 * CIDR notation (/16) or can be omitted to specify a single host.
621 * Note that the netmask is mandatory if ports are specified.
623 * @param routeListX a string specifying the policy
624 * @return the converted list, 0-terminated, NULL if the synatx is flawed
626 struct GNUNET_STRINGS_IPv6NetworkPolicy *
627 GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX);
631 #if 0 /* keep Emacsens' auto-indent happy */
638 /* ifndef GNUNET_UTIL_STRING_H */
641 /** @} */ /* end of group */
643 /* end of gnunet_util_string.h */