ftbfs
[oweals/gnunet.git] / src / include / gnunet_strings_lib.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20
21 /**
22  * @author Christian Grothoff
23  * @author Krista Bennett
24  * @author Gerd Knorr <kraxel@bytesex.org>
25  * @author Ioana Patrascu
26  * @author Tzvetan Horozov
27  *
28  * @file
29  * Strings and string handling functions
30  *
31  * @defgroup strings  Strings library
32  * Strings and string handling functions, including malloc and string tokenizing.
33  * @{
34  */
35
36 #ifndef GNUNET_STRINGS_LIB_H
37 #define GNUNET_STRINGS_LIB_H
38
39 /* we need size_t, and since it can be both unsigned int
40    or unsigned long long, this IS platform dependent;
41    but "stdlib.h" should be portable 'enough' to be
42    unconditionally available... */
43 #include <stdlib.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47
48 #ifdef __cplusplus
49 extern "C"
50 {
51 #if 0                           /* keep Emacsens' auto-indent happy */
52 }
53 #endif
54 #endif
55
56 #include "gnunet_time_lib.h"
57
58
59 /**
60  * Convert a given fancy human-readable size to bytes.
61  *
62  * @param fancy_size human readable string (i.e. 1 MB)
63  * @param size set to the size in bytes
64  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
65  */
66 int
67 GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size,
68                                     unsigned long long *size);
69
70
71 /**
72  * Convert a given fancy human-readable time to our internal
73  * representation.
74  *
75  * @param fancy_time human readable string (i.e. 1 minute)
76  * @param rtime set to the relative time
77  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
78  */
79 int
80 GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time,
81                                        struct GNUNET_TIME_Relative *rtime);
82
83
84 /**
85  * @ingroup time
86  * Convert a given fancy human-readable time to our internal
87  * representation.  The human-readable time is expected to be
88  * in local time, whereas the returned value will be in UTC.
89  *
90  * @param fancy_time human readable string (i.e. %Y-%m-%d %H:%M:%S)
91  * @param atime set to the absolute time
92  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
93  */
94 int
95 GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time,
96                                        struct GNUNET_TIME_Absolute *atime);
97
98
99 /**
100  * Convert a given filesize into a fancy human-readable format.
101  *
102  * @param size number of bytes
103  * @return fancy representation of the size (possibly rounded) for humans
104  */
105 char *
106 GNUNET_STRINGS_byte_size_fancy (unsigned long long size);
107
108
109 /**
110  * Convert the len characters long character sequence
111  * given in input that is in the given input charset
112  * to a string in given output charset.
113  *
114  * @param input input string
115  * @param len number of bytes in @a input
116  * @param input_charset character set used for @a input
117  * @param output_charset desired character set for the return value
118  * @return the converted string (0-terminated),
119  *  if conversion fails, a copy of the orignal
120  *  string is returned.
121  */
122 char *
123 GNUNET_STRINGS_conv (const char *input, size_t len,
124                      const char *input_charset,
125                      const char *output_charset);
126
127
128 /**
129  * Convert the len characters long character sequence
130  * given in input that is in the given charset
131  * to UTF-8.
132  *
133  * @param input the input string (not necessarily 0-terminated)
134  * @param len the number of bytes in the @a input
135  * @param charset character set to convert from
136  * @return the converted string (0-terminated)
137  */
138 char *
139 GNUNET_STRINGS_to_utf8 (const char *input,
140                         size_t len,
141                         const char *charset);
142
143
144 /**
145  * Convert the len bytes-long UTF-8 string
146  * given in input to the given charset.
147  *
148  * @param input the input string (not necessarily 0-terminated)
149  * @param len the number of bytes in the @a input
150  * @param charset character set to convert to
151  * @return the converted string (0-terminated),
152  *  if conversion fails, a copy of the orignal
153  *  string is returned.
154  */
155 char *
156 GNUNET_STRINGS_from_utf8 (const char *input,
157                           size_t len,
158                           const char *charset);
159
160
161 /**
162  * Convert the utf-8 input string to lower case.
163  * Output needs to be allocated appropriately.
164  *
165  * @param input input string
166  * @param output output buffer
167  */
168 void
169 GNUNET_STRINGS_utf8_tolower (const char *input,
170                              char *output);
171
172
173 /**
174  * Convert the utf-8 input string to upper case.
175  * Output needs to be allocated appropriately.
176  *
177  * @param input input string
178  * @param output output buffer
179  */
180 void
181 GNUNET_STRINGS_utf8_toupper (const char *input,
182                              char *output);
183
184
185 /**
186  * Complete filename (a la shell) from abbrevition.
187  *
188  * @param fil the name of the file, may contain ~/ or
189  *        be relative to the current directory
190  * @return the full file name,
191  *          NULL is returned on error
192  */
193 char *
194 GNUNET_STRINGS_filename_expand (const char *fil);
195
196
197 /**
198  * Fill a buffer of the given size with count 0-terminated strings
199  * (given as varargs).  If "buffer" is NULL, only compute the amount
200  * of space required (sum of "strlen(arg)+1").
201  *
202  * Unlike using "snprintf" with "%s", this function will add
203  * 0-terminators after each string.  The
204  * "GNUNET_string_buffer_tokenize" function can be used to parse the
205  * buffer back into individual strings.
206  *
207  * @param buffer the buffer to fill with strings, can
208  *               be NULL in which case only the necessary
209  *               amount of space will be calculated
210  * @param size number of bytes available in buffer
211  * @param count number of strings that follow
212  * @param ... count 0-terminated strings to copy to buffer
213  * @return number of bytes written to the buffer
214  *         (or number of bytes that would have been written)
215  */
216 size_t
217 GNUNET_STRINGS_buffer_fill (char *buffer,
218                             size_t size,
219                             unsigned int count,
220                             ...);
221
222
223 /**
224  * Given a buffer of a given size, find "count" 0-terminated strings
225  * in the buffer and assign the count (varargs) of type "const char**"
226  * to the locations of the respective strings in the buffer.
227  *
228  * @param buffer the buffer to parse
229  * @param size size of the @a buffer
230  * @param count number of strings to locate
231  * @param ... pointers to where to store the strings
232  * @return offset of the character after the last 0-termination
233  *         in the buffer, or 0 on error.
234  */
235 unsigned int
236 GNUNET_STRINGS_buffer_tokenize (const char *buffer,
237                                 size_t size,
238                                 unsigned int count, ...);
239
240
241 /**
242  * @ingroup time
243  * Like `asctime`, except for GNUnet time.  Converts a GNUnet internal
244  * absolute time (which is in UTC) to a string in local time.
245  * Note that the returned value will be overwritten if this function
246  * is called again.
247  *
248  * @param t the absolute time to convert
249  * @return timestamp in human-readable form in local time
250  */
251 const char *
252 GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t);
253
254
255 /**
256  * @ingroup time
257  * Give relative time in human-readable fancy format.
258  * This is one of the very few calls in the entire API that is
259  * NOT reentrant!
260  *
261  * @param delta time in milli seconds
262  * @param do_round are we allowed to round a bit?
263  * @return string in human-readable form
264  */
265 const char *
266 GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta,
267                                         int do_round);
268
269
270 /**
271  * "man basename"
272  * Returns a pointer to a part of filename (allocates nothing)!
273  *
274  * @param filename filename to extract basename from
275  * @return short (base) name of the file (that is, everything following the
276  *         last directory separator in filename. If filename ends with a
277  *         directory separator, the result will be a zero-length string.
278  *         If filename has no directory separators, the result is filename
279  *         itself.
280  */
281 const char *
282 GNUNET_STRINGS_get_short_name (const char *filename);
283
284
285 /**
286  * Convert binary data to ASCII encoding using CrockfordBase32.
287  * Does not append 0-terminator, but returns a pointer to the place where
288  * it should be placed, if needed.
289  *
290  * @param data data to encode
291  * @param size size of data (in bytes)
292  * @param out buffer to fill
293  * @param out_size size of the buffer. Must be large enough to hold
294  * ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
295  * @return pointer to the next byte in 'out' or NULL on error.
296  */
297 char *
298 GNUNET_STRINGS_data_to_string (const void *data,
299                                size_t size,
300                                char *out,
301                                size_t out_size);
302
303
304 /**
305  * Return the base32crockford encoding of the given buffer.
306  *
307  * The returned string will be freshly allocated, and must be free'd
308  * with #GNUNET_free().
309  *
310  * @param buf buffer with data
311  * @param size size of the buffer @a buf
312  * @return freshly allocated, null-terminated string
313  */
314 char *
315 GNUNET_STRINGS_data_to_string_alloc (const void *buf,
316                                      size_t size);
317
318
319 /**
320  * Convert CrockfordBase32 encoding back to data.
321  * @a out_size must match exactly the size of the data before it was encoded.
322  *
323  * @param enc the encoding
324  * @param enclen number of characters in @a enc (without 0-terminator, which can be missing)
325  * @param out location where to store the decoded data
326  * @param out_size size of the output buffer @a out
327  * @return #GNUNET_OK on success, #GNUNET_SYSERR if result has the wrong encoding
328  */
329 int
330 GNUNET_STRINGS_string_to_data (const char *enc,
331                                size_t enclen,
332                                void *out,
333                                size_t out_size);
334
335
336 /**
337  * Encode into Base64.
338  *
339  * @param data the data to encode
340  * @param len the length of the input
341  * @param output where to write the output (*output should be NULL,
342  *   is allocated)
343  * @return the size of the output
344  */
345 size_t
346 GNUNET_STRINGS_base64_encode (const void *in,
347                               size_t len,
348                               char **output);
349
350
351 /**
352  * Encode into Base64url. RFC7515
353  *
354  * @param in the data to encode
355  * @param len the length of the input
356  * @param output where to write the output (*output should be NULL,
357  *   is allocated)
358  * @return the size of the output
359  */
360 size_t
361 GNUNET_STRINGS_base64url_encode (const void *in, size_t len, char **output);
362
363
364 /**
365  * Decode from Base64.
366  *
367  * @param data the data to encode
368  * @param len the length of the input
369  * @param[out] output where to write the output (*output should be NULL,
370  *   is allocated)
371  * @return the size of the output
372  */
373 size_t
374 GNUNET_STRINGS_base64_decode (const char *data,
375                               size_t len,
376                               void **output);
377
378
379 /**
380  * Decode from Base64url. RFC7515
381  *
382  * @param data the data to decode
383  * @param len the length of the input
384  * @param output where to write the output (*output should be NULL,
385  *   is allocated)
386  * @return the size of the output
387  */
388 size_t
389 GNUNET_STRINGS_base64url_decode (const char *data, size_t len, void **out);
390
391
392 /**
393  * Convert a peer path to a human-readable string.
394  *
395  * @param pids array of PIDs to convert to a string
396  * @param num_pids length of the @a pids array
397  * @return string representing the array of @a pids
398  */
399 char *
400 GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids,
401                      unsigned int num_pids);
402
403
404 /**
405  * Parse a path that might be an URI.
406  *
407  * @param path path to parse. Must be NULL-terminated.
408  * @param[out] scheme_part pointer to a string that
409  *        represents the URI scheme will be stored. Can be NULL. The string is
410  *        allocated by the function, and should be freed by GNUNET_free() when
411  *        it is no longer needed.
412  * @param path_part a pointer to 'const char *' where a pointer to the path
413  *        part of the URI will be stored. Can be NULL. Points to the same block
414  *        of memory as @a path, and thus must not be freed. Might point to '\0',
415  *        if path part is zero-length.
416  * @return #GNUNET_YES if it's an URI, #GNUNET_NO otherwise. If 'path' is not
417  *         an URI, '* scheme_part' and '*path_part' will remain unchanged
418  *         (if they weren't NULL).
419  */
420 int
421 GNUNET_STRINGS_parse_uri (const char *path,
422                           char **scheme_part,
423                           const char **path_part);
424
425
426 /**
427  * Check whether filename is absolute or not, and if it's an URI
428  *
429  * @param filename filename to check
430  * @param can_be_uri #GNUNET_YES to check for being URI, #GNUNET_NO - to
431  *        assume it's not URI
432  * @param r_is_uri a pointer to an int that is set to #GNUNET_YES if 'filename'
433  *        is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is
434  *        not #GNUNET_YES, *r_is_uri is set to #GNUNET_NO.
435  * @param r_uri_scheme a pointer to a char * that is set to a pointer to URI scheme.
436  *        The string is allocated by the function, and should be freed with
437  *        GNUNET_free (). Can be NULL.
438  * @return #GNUNET_YES if 'filename' is absolute, #GNUNET_NO otherwise.
439  */
440 int
441 GNUNET_STRINGS_path_is_absolute (const char *filename,
442                                  int can_be_uri,
443                                  int *r_is_uri,
444                                  char **r_uri_scheme);
445
446
447 /**
448  * Flags for what we should check a file for.
449  */
450 enum GNUNET_STRINGS_FilenameCheck
451 {
452   /**
453    * Check that it exists.
454    */
455   GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
456
457   /**
458    * Check that it is a directory.
459    */
460   GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002,
461
462   /**
463    * Check that it is a link.
464    */
465   GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
466
467   /**
468    * Check that the path is an absolute path.
469    */
470   GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008
471 };
472
473
474 /**
475  * Perform checks on @a filename.  FIXME: some duplication with
476  * "GNUNET_DISK_"-APIs.  We should unify those.
477  *
478  * @param filename file to check
479  * @param checks checks to perform
480  * @return #GNUNET_YES if all checks pass, #GNUNET_NO if at least one of them
481  *         fails, #GNUNET_SYSERR when a check can't be performed
482  */
483 int
484 GNUNET_STRINGS_check_filename (const char *filename,
485                                enum GNUNET_STRINGS_FilenameCheck checks);
486
487
488 /**
489  * Tries to convert @a zt_addr string to an IPv6 address.
490  * The string is expected to have the format "[ABCD::01]:80".
491  *
492  * @param zt_addr 0-terminated string. May be mangled by the function.
493  * @param addrlen length of zt_addr (not counting 0-terminator).
494  * @param r_buf a buffer to fill. Initially gets filled with zeroes,
495  *        then its sin6_port, sin6_family and sin6_addr are set appropriately.
496  * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
497  *         case the contents of r_buf are undefined.
498  */
499 int
500 GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr,
501                                 uint16_t addrlen,
502                                 struct sockaddr_in6 *r_buf);
503
504
505 /**
506  * Tries to convert @a zt_addr string to an IPv4 address.
507  * The string is expected to have the format "1.2.3.4:80".
508  *
509  * @param zt_addr 0-terminated string. May be mangled by the function.
510  * @param addrlen length of zt_addr (not counting 0-terminator).
511  * @param r_buf a buffer to fill.
512  * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which case
513  *         the contents of r_buf are undefined.
514  */
515 int
516 GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr,
517                                 uint16_t addrlen,
518                                 struct sockaddr_in *r_buf);
519
520
521 /**
522  * Parse an address given as a string into a
523  * `struct sockaddr`.
524  *
525  * @param addr the address
526  * @param[out] af set to the parsed address family (i.e. AF_INET)
527  * @param[out] sa set to the parsed address
528  * @return 0 on error, otherwise number of bytes in @a sa
529  */
530 size_t
531 GNUNET_STRINGS_parse_socket_addr (const char *addr,
532                                   uint8_t *af,
533                                   struct sockaddr **sa);
534
535
536 /**
537  * Tries to convert @a addr string to an IP (v4 or v6) address.
538  * Will automatically decide whether to treat 'addr' as v4 or v6 address.
539  *
540  * @param addr a string, may not be 0-terminated.
541  * @param addrlen number of bytes in @a addr (if addr is 0-terminated,
542  *        0-terminator should not be counted towards addrlen).
543  * @param r_buf a buffer to fill.
544  * @return #GNUNET_OK if conversion succeded. #GNUNET_SYSERR otherwise, in which
545  *         case the contents of r_buf are undefined.
546  */
547 int
548 GNUNET_STRINGS_to_address_ip (const char *addr,
549                               uint16_t addrlen,
550                               struct sockaddr_storage *r_buf);
551
552
553 /**
554  * Returns utf-8 encoded arguments.  Does nothing (returns a copy of
555  * @a argc and @a argv) on any platform other than W32.  Returned @a
556  * argv has `u8argv[u8argc] == NULL`.  Returned @a argv is a single
557  * memory block, and can be freed with a single GNUNET_free() call.
558  *
559  * @param argc argc (as given by main())
560  * @param argv argv (as given by main())
561  * @param u8argc a location to store new argc in (though it's th same as argc)
562  * @param u8argv a location to store new argv in
563  * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
564  */
565 int
566 GNUNET_STRINGS_get_utf8_args (int argc,
567                               char *const *argv,
568                               int *u8argc,
569                               char *const **u8argv);
570
571
572 /**
573  * Like strlcpy but portable. The given string @a src is copied in full length
574  * (until its null byte). The destination buffer is guaranteed to be
575  * null-terminated.
576  *
577  * to a destination buffer
578  * and ensures that the destination string is null-terminated.
579  *
580  * @param dst destination of the copy
581  * @param src source of the copy, must be null-terminated
582  * @param n the length of the string to copy, including its terminating null
583  *          byte
584  * @return the length of the string that was copied, excluding the terminating
585  *         null byte
586  */
587 size_t
588 GNUNET_strlcpy (char *dst, const char *src, size_t n);
589
590
591 /* ***************** IPv4/IPv6 parsing ****************** */
592
593 struct GNUNET_STRINGS_PortPolicy
594 {
595   /**
596    * Starting port range (0 if none given).
597    */
598   uint16_t start_port;
599
600   /**
601    * End of port range (0 if none given).
602    */
603   uint16_t end_port;
604
605   /**
606    * #GNUNET_YES if the port range should be negated
607    * ("!" in policy).
608    */
609   int negate_portrange;
610 };
611
612
613 /**
614  * @brief IPV4 network in CIDR notation.
615  */
616 struct GNUNET_STRINGS_IPv4NetworkPolicy
617 {
618   /**
619    * IPv4 address.
620    */
621   struct in_addr network;
622
623   /**
624    * IPv4 netmask.
625    */
626   struct in_addr netmask;
627
628   /**
629    * Policy for port access.
630    */
631   struct GNUNET_STRINGS_PortPolicy pp;
632 };
633
634
635 /**
636  * @brief network in CIDR notation for IPV6.
637  */
638 struct GNUNET_STRINGS_IPv6NetworkPolicy
639 {
640   /**
641    * IPv6 address.
642    */
643   struct in6_addr network;
644
645   /**
646    * IPv6 netmask.
647    */
648   struct in6_addr netmask;
649
650   /**
651    * Policy for port access.
652    */
653   struct GNUNET_STRINGS_PortPolicy pp;
654 };
655
656
657 /**
658  * Parse an IPv4 network policy. The argument specifies a list of
659  * subnets. The format is <tt>(network[/netmask][:[!]SPORT-DPORT];)*</tt>
660  * (no whitespace, must be terminated with a semicolon). The network
661  * must be given in dotted-decimal notation. The netmask can be given
662  * in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).
663  *
664  * @param routeListX a string specifying the IPv4 subnets
665  * @return the converted list, terminated with all zeros;
666  *         NULL if the synatx is flawed
667  */
668 struct GNUNET_STRINGS_IPv4NetworkPolicy *
669 GNUNET_STRINGS_parse_ipv4_policy (const char *routeListX);
670
671
672 /**
673  * Parse an IPv6 network policy. The argument specifies a list of
674  * subnets. The format is <tt>(network[/netmask[:[!]SPORT[-DPORT]]];)*</tt>
675  * (no whitespace, must be terminated with a semicolon). The network
676  * must be given in colon-hex notation.  The netmask must be given in
677  * CIDR notation (/16) or can be omitted to specify a single host.
678  * Note that the netmask is mandatory if ports are specified.
679  *
680  * @param routeListX a string specifying the policy
681  * @return the converted list, 0-terminated, NULL if the synatx is flawed
682  */
683 struct GNUNET_STRINGS_IPv6NetworkPolicy *
684 GNUNET_STRINGS_parse_ipv6_policy (const char *routeListX);
685
686
687 #if 0                           /* keep Emacsens' auto-indent happy */
688 {
689 #endif
690 #ifdef __cplusplus
691 }
692 #endif
693
694 /* ifndef GNUNET_UTIL_STRING_H */
695 #endif
696
697 /** @} */  /* end of group */
698
699 /* end of gnunet_util_string.h */