2 This file is part of GNUnet.
3 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_strings_lib.h
23 * @brief strings and string handling functions (including malloc
24 * and string tokenizing)
26 * @author Christian Grothoff
27 * @author Krista Bennett
28 * @author Gerd Knorr <kraxel@bytesex.org>
29 * @author Ioana Patrascu
30 * @author Tzvetan Horozov
33 #ifndef GNUNET_STRINGS_LIB_H
34 #define GNUNET_STRINGS_LIB_H
36 /* we need size_t, and since it can be both unsigned int
37 or unsigned long long, this IS platform dependent;
38 but "stdlib.h" should be portable 'enough' to be
39 unconditionally available... */
45 #if 0 /* keep Emacsens' auto-indent happy */
50 #include "gnunet_time_lib.h"
54 * Convert a given fancy human-readable size to bytes.
56 * @param fancy_size human readable string (i.e. 1 MB)
57 * @param size set to the size in bytes
58 * @return GNUNET_OK on success, GNUNET_SYSERR on error
61 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
62 unsigned long long *size);
66 * Convert a given fancy human-readable time to our internal
69 * @param fancy_size human readable string (i.e. 1 minute)
70 * @param rtime set to the relative time
71 * @return GNUNET_OK on success, GNUNET_SYSERR on error
74 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_size,
75 struct GNUNET_TIME_Relative *rtime);
79 * Convert a given filesize into a fancy human-readable format.
81 * @param size number of bytes
82 * @return fancy representation of the size (possibly rounded) for humans
85 GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
89 * Convert the len characters long character sequence
90 * given in input that is in the given input charset
91 * to a string in given output charset.
92 * @return the converted string (0-terminated),
93 * if conversion fails, a copy of the orignal
97 GNUNET_STRINGS_conv (const char *input, size_t len,
98 const char *input_charset, const char *output_charset);
101 * Convert the len characters long character sequence
102 * given in input that is in the given charset
105 * @param input the input string (not necessarily 0-terminated)
106 * @param len the number of bytes in the input
107 * @param charset character set to convert from
108 * @return the converted string (0-terminated)
111 GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset);
114 * Convert the len bytes-long UTF-8 string
115 * given in input to the given charset.
117 * @return the converted string (0-terminated),
118 * if conversion fails, a copy of the orignal
119 * string is returned.
122 GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset);
126 * Complete filename (a la shell) from abbrevition.
128 * @param fil the name of the file, may contain ~/ or
129 * be relative to the current directory
130 * @return the full file name,
131 * NULL is returned on error
134 GNUNET_STRINGS_filename_expand (const char *fil);
138 * Fill a buffer of the given size with
139 * count 0-terminated strings (given as varargs).
140 * If "buffer" is NULL, only compute the amount of
141 * space required (sum of "strlen(arg)+1").
143 * Unlike using "snprintf" with "%s", this function
144 * will add 0-terminators after each string. The
145 * "GNUNET_string_buffer_tokenize" function can be
146 * used to parse the buffer back into individual
149 * @param buffer the buffer to fill with strings, can
150 * be NULL in which case only the necessary
151 * amount of space will be calculated
152 * @param size number of bytes available in buffer
153 * @param count number of strings that follow
154 * @param ... count 0-terminated strings to copy to buffer
155 * @return number of bytes written to the buffer
156 * (or number of bytes that would have been written)
159 GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count, ...);
163 * Given a buffer of a given size, find "count"
164 * 0-terminated strings in the buffer and assign
165 * the count (varargs) of type "const char**" to the
166 * locations of the respective strings in the
169 * @param buffer the buffer to parse
170 * @param size size of the buffer
171 * @param count number of strings to locate
172 * @param ... pointers to where to store the strings
173 * @return offset of the character after the last 0-termination
174 * in the buffer, or 0 on error.
177 GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size,
178 unsigned int count, ...);
183 * "man ctime_r", except for GNUnet time; also, unlike ctime, the
184 * return value does not include the newline character.
186 * @param t the absolute time to convert
187 * @return timestamp in human-readable form
190 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t);
194 * Give relative time in human-readable fancy format.
196 * @param delta time in milli seconds
197 * @return string in human-readable form
200 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta);
204 * Returns a pointer to a part of filename (allocates nothing)!
206 * @param filename filename to extract basename from
207 * @return short (base) name of the file (that is, everything following the
208 * last directory separator in filename. If filename ends with a
209 * directory separator, the result will be a zero-length string.
210 * If filename has no directory separators, the result is filename
214 GNUNET_STRINGS_get_short_name (const char *filename);
218 * Convert binary data to ASCII encoding. The ASCII encoding is rather
219 * GNUnet specific. It was chosen such that it only uses characters
220 * in [0-9A-V], can be produced without complex arithmetics and uses a
221 * small number of characters. The GNUnet encoding uses 103 characters.
222 * Does not append 0-terminator, but returns a pointer to the place where
223 * it should be placed, if needed.
225 * @param data data to encode
226 * @param size size of data (in bytes)
227 * @param out buffer to fill
228 * @param out_size size of the buffer. Must be large enough to hold
229 * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
230 * @return pointer to the next byte in 'out' or NULL on error.
233 GNUNET_STRINGS_data_to_string (unsigned char *data, size_t size,
234 char *out, size_t out_size);
238 * Convert ASCII encoding back to data
239 * out_size must match exactly the size of the data before it was encoded.
241 * @param enc the encoding
242 * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
243 * @param out location where to store the decoded data
244 * @param out_size sizeof the output buffer
245 * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
248 GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen,
249 unsigned char *out, size_t out_size);
252 #if 0 /* keep Emacsens' auto-indent happy */
259 enum GNUNET_STRINGS_FilenameCheck
261 GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
262 GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002,
263 GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
264 GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008
268 * Parse a path that might be an URI.
270 * @param path path to parse. Must be NULL-terminated.
271 * @param scheme_part a pointer to 'char *' where a pointer to a string that
272 * represents the URI scheme will be stored. Can be NULL. The string is
273 * allocated by the function, and should be freed by GNUNET_free() when
274 * it is no longer needed.
275 * @param path_part a pointer to 'const char *' where a pointer to the path
276 * part of the URI will be stored. Can be NULL. Points to the same block
277 * of memory as 'path', and thus must not be freed. Might point to '\0',
278 * if path part is zero-length.
279 * @return GNUNET_YES if it's an URI, GNUNET_NO otherwise. If 'path' is not
280 * an URI, '* scheme_part' and '*path_part' will remain unchanged
281 * (if they weren't NULL).
284 GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part,
285 const char **path_part);
289 * Check whether filename is absolute or not, and if it's an URI
291 * @param filename filename to check
292 * @param can_be_uri GNUNET_YES to check for being URI, GNUNET_NO - to
293 * assume it's not URI
294 * @param r_is_uri a pointer to an int that is set to GNUNET_YES if 'filename'
295 * is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is
296 * not GNUNET_YES, *r_is_uri is set to GNUNET_NO.
297 * @param r_uri a pointer to a char * that is set to a pointer to URI scheme.
298 * The string is allocated by the function, and should be freed with
299 * GNUNET_free (). Can be NULL.
300 * @return GNUNET_YES if 'filename' is absolute, GNUNET_NO otherwise.
303 GNUNET_STRINGS_path_is_absolute (const char *filename,
306 char **r_uri_scheme);
310 * Perform checks on 'filename;
312 * @param filename file to check
313 * @param checks checks to perform
314 * @return GNUNET_YES if all checks pass, GNUNET_NO if at least one of them
315 * fails, GNUNET_SYSERR when a check can't be performed
318 GNUNET_STRINGS_check_filename (const char *filename,
319 enum GNUNET_STRINGS_FilenameCheck checks);
323 * Tries to convert 'zt_addr' string to an IPv6 address.
325 * @param zt_addr 0-terminated string. May be mangled by the function.
326 * @param addrlen length of zt_addr (not counting 0-terminator).
327 * @param r_buf a buffer to fill. Initially gets filled with zeroes,
328 * then its sin6_port, sin6_family and sin6_addr are set appropriately.
329 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
330 * case the contents of r_buf are undefined.
333 GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
335 struct sockaddr_in6 *r_buf);
339 * Tries to convert 'zt_addr' string to an IPv4 address.
341 * @param zt_addr 0-terminated string. May be mangled by the function.
342 * @param addrlen length of zt_addr (not counting 0-terminator).
343 * @param r_buf a buffer to fill.
344 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which case
345 * the contents of r_buf are undefined.
348 GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
350 struct sockaddr_in *r_buf);
354 * Tries to convert 'addr' string to an IP (v4 or v6) address.
355 * IPv6 address must have its address part enclosed in '()' parens
357 * Will automatically decide whether to treat 'addr' as v4 or v6 address.
359 * @param addr a string, may not be 0-terminated.
360 * @param addrlen number of bytes in addr (if addr is 0-terminated,
361 * 0-terminator should not be counted towards addrlen).
362 * @param r_buf a buffer to fill.
363 * @return GNUNET_OK if conversion succeded. GNUNET_SYSERR otherwise, in which
364 * case the contents of r_buf are undefined.
367 GNUNET_STRINGS_to_address_ip (const char *addr,
369 struct sockaddr_storage *r_buf);
372 /* ifndef GNUNET_UTIL_STRING_H */
374 /* end of gnunet_util_string.h */