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