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