-downgrade version requirement further
[oweals/gnunet.git] / src / include / gnunet_strings_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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  * @ingroup time
80  * Convert a given fancy human-readable time to our internal
81  * representation.  The human-readable time is expected to be
82  * in local time, whereas the returned value will be in UTC.
83  *
84  * @param fancy_time human readable string (i.e. %Y-%m-%d %H:%M:%S)
85  * @param atime set to the absolute time
86  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
87  */
88 int
89 GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
90                                        struct GNUNET_TIME_Absolute *atime);
91
92
93 /**
94  * Convert a given filesize into a fancy human-readable format.
95  *
96  * @param size number of bytes
97  * @return fancy representation of the size (possibly rounded) for humans
98  */
99 char *
100 GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
101
102
103 /**
104  * Convert the len characters long character sequence
105  * given in input that is in the given input charset
106  * to a string in given output charset.
107  *
108  * @param input input string
109  * @param len number of bytes in @a input
110  * @param input_charset character set used for @a input
111  * @param output_charset desired character set for the return value
112  * @return the converted string (0-terminated),
113  *  if conversion fails, a copy of the orignal
114  *  string is returned.
115  */
116 char *
117 GNUNET_STRINGS_conv (const char *input, size_t len,
118                      const char *input_charset,
119                      const char *output_charset);
120
121
122 /**
123  * Convert the len characters long character sequence
124  * given in input that is in the given charset
125  * to UTF-8.
126  *
127  * @param input the input string (not necessarily 0-terminated)
128  * @param len the number of bytes in the @a input
129  * @param charset character set to convert from
130  * @return the converted string (0-terminated)
131  */
132 char *
133 GNUNET_STRINGS_to_utf8 (const char *input,
134                         size_t len,
135                         const char *charset);
136
137
138 /**
139  * Convert the len bytes-long UTF-8 string
140  * given in input to the given charset.
141  *
142  * @param input the input string (not necessarily 0-terminated)
143  * @param len the number of bytes in the @a input
144  * @param charset character set to convert to
145  * @return the converted string (0-terminated),
146  *  if conversion fails, a copy of the orignal
147  *  string is returned.
148  */
149 char *
150 GNUNET_STRINGS_from_utf8 (const char *input,
151                           size_t len,
152                           const char *charset);
153
154
155 /**
156  * Convert the utf-8 input string to lower case.
157  * Output needs to be allocated appropriately.
158  *
159  * @param input input string
160  * @param output output buffer
161  */
162 void
163 GNUNET_STRINGS_utf8_tolower (const char *input,
164                              char *output);
165
166
167 /**
168  * Convert the utf-8 input string to upper case.
169  * Output needs to be allocated appropriately.
170  *
171  * @param input input string
172  * @param output output buffer
173  */
174 void
175 GNUNET_STRINGS_utf8_toupper (const char *input,
176                              char *output);
177
178
179 /**
180  * Complete filename (a la shell) from abbrevition.
181  *
182  * @param fil the name of the file, may contain ~/ or
183  *        be relative to the current directory
184  * @return the full file name,
185  *          NULL is returned on error
186  */
187 char *
188 GNUNET_STRINGS_filename_expand (const char *fil);
189
190
191 /**
192  * Fill a buffer of the given size with count 0-terminated strings
193  * (given as varargs).  If "buffer" is NULL, only compute the amount
194  * of space required (sum of "strlen(arg)+1").
195  *
196  * Unlike using "snprintf" with "%s", this function will add
197  * 0-terminators after each string.  The
198  * "GNUNET_string_buffer_tokenize" function can be used to parse the
199  * buffer back into individual strings.
200  *
201  * @param buffer the buffer to fill with strings, can
202  *               be NULL in which case only the necessary
203  *               amount of space will be calculated
204  * @param size number of bytes available in buffer
205  * @param count number of strings that follow
206  * @param ... count 0-terminated strings to copy to buffer
207  * @return number of bytes written to the buffer
208  *         (or number of bytes that would have been written)
209  */
210 size_t
211 GNUNET_STRINGS_buffer_fill (char *buffer,
212                             size_t size,
213                             unsigned int count,
214                             ...);
215
216
217 /**
218  * Given a buffer of a given size, find "count" 0-terminated strings
219  * in the buffer and assign the count (varargs) of type "const char**"
220  * to the locations of the respective strings in the buffer.
221  *
222  * @param buffer the buffer to parse
223  * @param size size of the @a buffer
224  * @param count number of strings to locate
225  * @param ... pointers to where to store the strings
226  * @return offset of the character after the last 0-termination
227  *         in the buffer, or 0 on error.
228  */
229 unsigned int
230 GNUNET_STRINGS_buffer_tokenize (const char *buffer,
231                                 size_t size,
232                                 unsigned int count, ...);
233
234
235
236 /**
237  * @ingroup time
238  * Like `asctime`, except for GNUnet time.  Converts a GNUnet internal
239  * absolute time (which is in UTC) to a string in local time.
240  * Note that the returned value will be overwritten if this function
241  * is called again.
242  *
243  * @param t the absolute time to convert
244  * @return timestamp in human-readable form in local time
245  */
246 const char *
247 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t);
248
249
250 /**
251  * @ingroup time
252  * Give relative time in human-readable fancy format.
253  * This is one of the very few calls in the entire API that is
254  * NOT reentrant!
255  *
256  * @param delta time in milli seconds
257  * @param do_round are we allowed to round a bit?
258  * @return string in human-readable form
259  */
260 const char *
261 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
262                                         int do_round);
263
264
265 /**
266  * "man basename"
267  * Returns a pointer to a part of filename (allocates nothing)!
268  *
269  * @param filename filename to extract basename from
270  * @return short (base) name of the file (that is, everything following the
271  *         last directory separator in filename. If filename ends with a
272  *         directory separator, the result will be a zero-length string.
273  *         If filename has no directory separators, the result is filename
274  *         itself.
275  */
276 const char *
277 GNUNET_STRINGS_get_short_name (const char *filename);
278
279
280 /**
281  * Convert binary data to ASCII encoding using Base32Hex (RFC 4648).
282  * Does not append 0-terminator, but returns a pointer to the place where
283  * it should be placed, if needed.
284  *
285  * @param data data to encode
286  * @param size size of data (in bytes)
287  * @param out buffer to fill
288  * @param out_size size of the buffer. Must be large enough to hold
289  * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
290  * @return pointer to the next byte in 'out' or NULL on error.
291  */
292 char *
293 GNUNET_STRINGS_data_to_string (const void *data,
294                                size_t size,
295                                char *out,
296                                size_t out_size);
297
298
299 /**
300  * Return the base32crockford encoding of the given buffer.
301  *
302  * The returned string will be freshly allocated, and must be free'd
303  * with #GNUNET_free().
304  *
305  * @param buffer with data
306  * @param size size of the buffer
307  * @return freshly allocated, null-terminated string
308  */
309 char *
310 GNUNET_STRINGS_data_to_string_alloc (const void *buf,
311                                      size_t size);
312
313
314 /**
315  * Convert Base32hex encoding back to data.
316  * @a out_size must match exactly the size of the data before it was encoded.
317  *
318  * @param enc the encoding
319  * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
320  * @param out location where to store the decoded data
321  * @param out_size size of the output buffer @a out
322  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
323  */
324 int
325 GNUNET_STRINGS_string_to_data (const char *enc,
326                                size_t enclen,
327                                void *out,
328                                size_t out_size);
329
330
331 /**
332  * Encode into Base64.
333  *
334  * @param data the data to encode
335  * @param len the length of the input
336  * @param output where to write the output (*output should be NULL,
337  *   is allocated)
338  * @return the size of the output
339  */
340 size_t
341 GNUNET_STRINGS_base64_encode (const char *data, size_t len, char **output);
342
343
344 /**
345  * Decode from Base64.
346  *
347  * @param data the data to encode
348  * @param len the length of the input
349  * @param output where to write the output (*output should be NULL,
350  *   is allocated)
351  * @return the size of the output
352  */
353 size_t
354 GNUNET_STRINGS_base64_decode (const char *data, size_t len, char **output);
355
356
357 /**
358  * Parse a path that might be an URI.
359  *
360  * @param path path to parse. Must be NULL-terminated.
361  * @param scheme_part a pointer to 'char *' where a pointer to a string that
362  *        represents the URI scheme will be stored. Can be NULL. The string is
363  *        allocated by the function, and should be freed by GNUNET_free() when
364  *        it is no longer needed.
365  * @param path_part a pointer to 'const char *' where a pointer to the path
366  *        part of the URI will be stored. Can be NULL. Points to the same block
367  *        of memory as 'path', and thus must not be freed. Might point to '\0',
368  *        if path part is zero-length.
369  * @return #GNUNET_YES if it's an URI, #GNUNET_NO otherwise. If 'path' is not
370  *         an URI, '* scheme_part' and '*path_part' will remain unchanged
371  *         (if they weren't NULL).
372  */
373 int
374 GNUNET_STRINGS_parse_uri (const char *path,
375                           char **scheme_part,
376                           const char **path_part);
377
378
379 /**
380  * Check whether filename is absolute or not, and if it's an URI
381  *
382  * @param filename filename to check
383  * @param can_be_uri #GNUNET_YES to check for being URI, #GNUNET_NO - to
384  *        assume it's not URI
385  * @param r_is_uri a pointer to an int that is set to #GNUNET_YES if 'filename'
386  *        is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is
387  *        not #GNUNET_YES, *r_is_uri is set to #GNUNET_NO.
388  * @param r_uri_scheme a pointer to a char * that is set to a pointer to URI scheme.
389  *        The string is allocated by the function, and should be freed with
390  *        GNUNET_free (). Can be NULL.
391  * @return #GNUNET_YES if 'filename' is absolute, #GNUNET_NO otherwise.
392  */
393 int
394 GNUNET_STRINGS_path_is_absolute (const char *filename,
395                                  int can_be_uri,
396                                  int *r_is_uri,
397                                  char **r_uri_scheme);
398
399
400 /**
401  * Flags for what we should check a file for.
402  */
403 enum GNUNET_STRINGS_FilenameCheck
404 {
405   /**
406    * Check that it exists.
407    */
408   GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
409
410   /**
411    * Check that it is a directory.
412    */
413   GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002,
414
415   /**
416    * Check that it is a link.
417    */
418   GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
419
420   /**
421    * Check that the path is an absolute path.
422    */
423   GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008
424 };
425
426
427 /**
428  * Perform checks on @a filename.  FIXME: some duplication with
429  * "GNUNET_DISK_"-APIs.  We should unify those.
430  *
431  * @param filename file to check
432  * @param checks checks to perform
433  * @return #GNUNET_YES if all checks pass, #GNUNET_NO if at least one of them
434  *         fails, #GNUNET_SYSERR when a check can't be performed
435  */
436 int
437 GNUNET_STRINGS_check_filename (const char *filename,
438                                enum GNUNET_STRINGS_FilenameCheck checks);
439
440
441 /**
442  * Tries to convert @a zt_addr string to an IPv6 address.
443  * The string is expected to have the format "[ABCD::01]:80".
444  *
445  * @param zt_addr 0-terminated string. May be mangled by the function.
446  * @param addrlen length of zt_addr (not counting 0-terminator).
447  * @param r_buf a buffer to fill. Initially gets filled with zeroes,
448  *        then its sin6_port, sin6_family and sin6_addr are set appropriately.
449  * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
450  *         case the contents of r_buf are undefined.
451  */
452 int
453 GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
454                                 uint16_t addrlen,
455                                 struct sockaddr_in6 *r_buf);
456
457
458 /**
459  * Tries to convert @a zt_addr string to an IPv4 address.
460  * The string is expected to have the format "1.2.3.4:80".
461  *
462  * @param zt_addr 0-terminated string. May be mangled by the function.
463  * @param addrlen length of zt_addr (not counting 0-terminator).
464  * @param r_buf a buffer to fill.
465  * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which case
466  *         the contents of r_buf are undefined.
467  */
468 int
469 GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
470                                 uint16_t addrlen,
471                                 struct sockaddr_in *r_buf);
472
473
474 /**
475  * Tries to convert @a addr string to an IP (v4 or v6) address.
476  * Will automatically decide whether to treat 'addr' as v4 or v6 address.
477  *
478  * @param addr a string, may not be 0-terminated.
479  * @param addrlen number of bytes in @a addr (if addr is 0-terminated,
480  *        0-terminator should not be counted towards addrlen).
481  * @param r_buf a buffer to fill.
482  * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
483  *         case the contents of r_buf are undefined.
484  */
485 int
486 GNUNET_STRINGS_to_address_ip (const char *addr,
487                               uint16_t addrlen,
488                               struct sockaddr_storage *r_buf);
489
490
491 /**
492  * Returns utf-8 encoded arguments.  Does nothing (returns a copy of
493  * @a argc and @a argv) on any platform other than W32.  Returned @a
494  * argv has `u8argv[u8argc] == NULL`.  Returned @a argv is a single
495  * memory block, and can be freed with a single GNUNET_free() call.
496  *
497  * @param argc argc (as given by main())
498  * @param argv argv (as given by main())
499  * @param u8argc a location to store new argc in (though it's th same as argc)
500  * @param u8argv a location to store new argv in
501  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
502  */
503 int
504 GNUNET_STRINGS_get_utf8_args (int argc,
505                               char *const *argv,
506                               int *u8argc,
507                               char *const **u8argv);
508
509
510 /* ***************** IPv4/IPv6 parsing ****************** */
511
512 struct GNUNET_STRINGS_PortPolicy
513 {
514
515   /**
516    * Starting port range (0 if none given).
517    */
518   uint16_t start_port;
519
520   /**
521    * End of port range (0 if none given).
522    */
523   uint16_t end_port;
524
525   /**
526    * #GNUNET_YES if the port range should be negated
527    * ("!" in policy).
528    */
529   int negate_portrange;
530
531 };
532
533
534 /**
535  * @brief IPV4 network in CIDR notation.
536  */
537 struct GNUNET_STRINGS_IPv4NetworkPolicy
538 {
539   /**
540    * IPv4 address.
541    */
542   struct in_addr network;
543
544   /**
545    * IPv4 netmask.
546    */
547   struct in_addr netmask;
548
549   /**
550    * Policy for port access.
551    */
552   struct GNUNET_STRINGS_PortPolicy pp;
553
554 };
555
556
557 /**
558  * @brief network in CIDR notation for IPV6.
559  */
560 struct GNUNET_STRINGS_IPv6NetworkPolicy
561 {
562   /**
563    * IPv6 address.
564    */
565   struct in6_addr network;
566
567   /**
568    * IPv6 netmask.
569    */
570   struct in6_addr netmask;
571
572   /**
573    * Policy for port access.
574    */
575   struct GNUNET_STRINGS_PortPolicy pp;
576
577 };
578
579
580 /**
581  * Parse an IPv4 network policy. The argument specifies a list of
582  * subnets. The format is <tt>(network[/netmask][:[!]SPORT-DPORT];)*</tt>
583  * (no whitespace, must be terminated with a semicolon). The network
584  * must be given in dotted-decimal notation. The netmask can be given
585  * in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).
586  *
587  * @param routeListX a string specifying the IPv4 subnets
588  * @return the converted list, terminated with all zeros;
589  *         NULL if the synatx is flawed
590  */
591 struct GNUNET_STRINGS_IPv4NetworkPolicy *
592 GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX);
593
594
595 /**
596  * Parse an IPv6 network policy. The argument specifies a list of
597  * subnets. The format is <tt>(network[/netmask[:[!]SPORT[-DPORT]]];)*</tt>
598  * (no whitespace, must be terminated with a semicolon). The network
599  * must be given in colon-hex notation.  The netmask must be given in
600  * CIDR notation (/16) or can be omitted to specify a single host.
601  * Note that the netmask is mandatory if ports are specified.
602  *
603  * @param routeListX a string specifying the policy
604  * @return the converted list, 0-terminated, NULL if the synatx is flawed
605  */
606 struct GNUNET_STRINGS_IPv6NetworkPolicy *
607 GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX);
608
609
610
611 #if 0                           /* keep Emacsens' auto-indent happy */
612 {
613 #endif
614 #ifdef __cplusplus
615 }
616 #endif
617
618
619 /* ifndef GNUNET_UTIL_STRING_H */
620 #endif
621 /* end of gnunet_util_string.h */