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