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