GNUnet 0.21.0
Strings library

Strings and string handling functions, including malloc and string tokenizing. More...

Collaboration diagram for Strings library:

Data Structures

struct  GNUNET_STRINGS_PortPolicy
 
struct  GNUNET_STRINGS_IPv4NetworkPolicy
 IPV4 network in CIDR notation. More...
 
struct  GNUNET_STRINGS_IPv6NetworkPolicy
 network in CIDR notation for IPV6. More...
 

Enumerations

enum  GNUNET_STRINGS_FilenameCheck { GNUNET_STRINGS_CHECK_EXISTS = 0x00000001 , GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002 , GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004 , GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008 }
 Flags for what we should check a file for. More...
 

Functions

enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, unsigned long long *size)
 Convert a given fancy human-readable size to bytes. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
 Convert a given fancy human-readable time to our internal representation. More...
 
char * GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
 Convert a given filesize into a fancy human-readable format. More...
 
char * GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, const char *output_charset)
 Convert the len characters long character sequence given in input that is in the given input charset to a string in given output charset. More...
 
char * GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
 Convert the len characters long character sequence given in input that is in the given charset to UTF-8. More...
 
char * GNUNET_STRINGS_utf8_normalize (const char *input)
 Normalize the utf-8 input string to NFC. More...
 
char * GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
 Convert the len bytes-long UTF-8 string given in input to the given charset. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_tolower (const char *input, char *output)
 Convert the utf-8 input string to lower case. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_toupper (const char *input, char *output)
 Convert the utf-8 input string to upper case. More...
 
char * GNUNET_STRINGS_filename_expand (const char *fil)
 Complete filename (a la shell) from abbrevition. More...
 
size_t GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count,...)
 Fill a buffer of the given size with count 0-terminated strings (given as varargs). More...
 
unsigned int GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, unsigned int count,...)
 Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count (varargs) of type "const char**" to the locations of the respective strings in the buffer. More...
 
const char * GNUNET_STRINGS_get_short_name (const char *filename)
 "man basename" Returns a pointer to a part of filename (allocates nothing)! More...
 
char * GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t out_size)
 Convert binary data to ASCII encoding using CrockfordBase32. More...
 
char * GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size)
 Return the base32crockford encoding of the given buffer. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, void *out, size_t out_size)
 Convert CrockfordBase32 encoding back to data. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc (const char *enc, size_t enclen, void **out, size_t *out_size)
 Convert CrockfordBase32 encoding back to data. More...
 
size_t GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
 Encode into Base64. More...
 
size_t GNUNET_STRINGS_urlencode (size_t len, const char data[static len], char **out)
 url/percent encode (RFC3986). More...
 
size_t GNUNET_STRINGS_base64url_encode (const void *in, size_t len, char **output)
 Encode into Base64url. More...
 
size_t GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **output)
 Decode from Base64. More...
 
size_t GNUNET_STRINGS_base64url_decode (const char *data, size_t len, void **out)
 Decode from Base64url. More...
 
size_t GNUNET_STRINGS_urldecode (const char *data, size_t len, char **out)
 url/percent encode (RFC3986). More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part, const char **path_part)
 Parse a path that might be an URI. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute (const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
 Check whether filename is absolute or not, and if it's an URI. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_check_filename (const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
 Perform checks on filename. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, size_t addrlen, struct sockaddr_in6 *r_buf)
 Tries to convert zt_addr string to an IPv6 address. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, size_t addrlen, struct sockaddr_in *r_buf)
 Tries to convert zt_addr string to an IPv4 address. More...
 
size_t GNUNET_STRINGS_parse_socket_addr (const char *addr, uint8_t *af, struct sockaddr **sa)
 Parse an address given as a string into a struct sockaddr. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ip (const char *addr, uint16_t addrlen, struct sockaddr_storage *r_buf)
 Tries to convert addr string to an IP (v4 or v6) address. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
 Returns utf-8 encoded arguments. More...
 
size_t GNUNET_strlcpy (char *dst, const char *src, size_t n)
 Like strlcpy but portable. More...
 
char * GNUNET_STRINGS_get_suffix_from_binary_name (const char *argv0)
 Sometimes we use the binary name to determine which specific test to run. More...
 
struct GNUNET_STRINGS_IPv4NetworkPolicyGNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
 Parse an IPv4 network policy. More...
 
struct GNUNET_STRINGS_IPv6NetworkPolicyGNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
 Parse an IPv6 network policy. More...
 

Detailed Description

Strings and string handling functions, including malloc and string tokenizing.

Enumeration Type Documentation

◆ GNUNET_STRINGS_FilenameCheck

Flags for what we should check a file for.

Enumerator
GNUNET_STRINGS_CHECK_EXISTS 

Check that it exists.

GNUNET_STRINGS_CHECK_IS_DIRECTORY 

Check that it is a directory.

GNUNET_STRINGS_CHECK_IS_LINK 

Check that it is a link.

GNUNET_STRINGS_CHECK_IS_ABSOLUTE 

Check that the path is an absolute path.

Definition at line 534 of file gnunet_strings_lib.h.

535{
539 GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
540
545
549 GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
550
555};
@ GNUNET_STRINGS_CHECK_IS_ABSOLUTE
Check that the path is an absolute path.
@ GNUNET_STRINGS_CHECK_IS_DIRECTORY
Check that it is a directory.
@ GNUNET_STRINGS_CHECK_EXISTS
Check that it exists.
@ GNUNET_STRINGS_CHECK_IS_LINK
Check that it is a link.

Function Documentation

◆ GNUNET_STRINGS_fancy_size_to_bytes()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_size_to_bytes ( const char *  fancy_size,
unsigned long long *  size 
)

Convert a given fancy human-readable size to bytes.

Parameters
fancy_sizehuman readable string (e.g. 1 MB)
sizeset to the size in bytes
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 236 of file strings.c.

238{
239 static const struct ConversionTable table[] =
240 { { "B", 1 },
241 { "KiB", 1024 },
242 { "kB", 1000 },
243 { "MiB", 1024 * 1024 },
244 { "MB", 1000 * 1000 },
245 { "GiB", 1024 * 1024 * 1024 },
246 { "GB", 1000 * 1000 * 1000 },
247 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
248 { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
249 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
250 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
251 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
252 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
253 { NULL, 0 } };
254
255 return convert_with_table (fancy_size, table, size);
256}
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:56
static unsigned int size
Size of the "table".
Definition: peer.c:68
static enum GNUNET_GenericReturnValue convert_with_table(const char *input, const struct ConversionTable *table, unsigned long long *output)
Convert a string of the form "4 X 5 Y" into a numeric value by interpreting "X" and "Y" as units and ...
Definition: strings.c:181
Unit conversion table entry for 'convert_with_table'.
Definition: strings.c:156

References convert_with_table(), size, and table.

Referenced by GNUNET_CONFIGURATION_get_value_size().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_fancy_time_to_relative()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative ( const char *  fancy_time,
struct GNUNET_TIME_Relative rtime 
)

Convert a given fancy human-readable time to our internal representation.

Parameters
fancy_timehuman readable string (e.g. 1 minute)
rtimeset to the relative time
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 260 of file strings.c.

262{
263 static const struct ConversionTable table[] =
264 { { "us", 1 },
265 { "ms", 1000 },
266 { "s", 1000 * 1000LL },
267 { "second", 1000 * 1000LL },
268 { "seconds", 1000 * 1000LL },
269 { "\"", 1000 * 1000LL },
270 { "m", 60 * 1000 * 1000LL },
271 { "min", 60 * 1000 * 1000LL },
272 { "minute", 60 * 1000 * 1000LL },
273 { "minutes", 60 * 1000 * 1000LL },
274 { "'", 60 * 1000 * 1000LL },
275 { "h", 60 * 60 * 1000 * 1000LL },
276 { "hour", 60 * 60 * 1000 * 1000LL },
277 { "hours", 60 * 60 * 1000 * 1000LL },
278 { "d", 24 * 60 * 60 * 1000LL * 1000LL },
279 { "day", 24 * 60 * 60 * 1000LL * 1000LL },
280 { "days", 24 * 60 * 60 * 1000LL * 1000LL },
281 { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
282 { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
283 { "year", 31536000000000LL /* year */ },
284 { "years", 31536000000000LL /* year */ },
285 { "a", 31536000000000LL /* year */ },
286 { NULL, 0 } };
287 int ret;
288 unsigned long long val;
289
290 if (0 == strcasecmp ("forever", fancy_time))
291 {
293 return GNUNET_OK;
294 }
295 ret = convert_with_table (fancy_time, table, &val);
296 rtime->rel_value_us = (uint64_t) val;
297 return ret;
298}
static int ret
Final status code.
Definition: gnunet-arm.c:94
@ GNUNET_OK
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
uint64_t rel_value_us
The actual value.

References convert_with_table(), GNUNET_OK, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_TIME_Relative::rel_value_us, ret, and table.

Referenced by create_did_ego_lockup_cb(), GNUNET_CONFIGURATION_get_value_time(), parse_expiration(), set_relative_time(), and set_timetravel_time().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_byte_size_fancy()

char * GNUNET_STRINGS_byte_size_fancy ( unsigned long long  size)

Convert a given filesize into a fancy human-readable format.

Parameters
sizenumber of bytes
Returns
fancy representation of the size (possibly rounded) for humans

Definition at line 106 of file strings.c.

107{
108 const char *unit = /* size unit */ "b";
109 char *ret;
110
111 if (size > 5 * 1024)
112 {
113 size = size / 1024;
114 unit = "KiB";
115 if (size > 5 * 1024)
116 {
117 size = size / 1024;
118 unit = "MiB";
119 if (size > 5 * 1024)
120 {
121 size = size / 1024;
122 unit = "GiB";
123 if (size > 5 * 1024)
124 {
125 size = size / 1024;
126 unit = "TiB";
127 }
128 }
129 }
130 }
131 ret = GNUNET_malloc (32);
132 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
133 return ret;
134}
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_malloc(size)
Wrapper around malloc.

References GNUNET_malloc, GNUNET_snprintf(), ret, and size.

Referenced by progress_cb(), and publish_inspector().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_conv()

char * GNUNET_STRINGS_conv ( const char *  input,
size_t  len,
const char *  input_charset,
const char *  output_charset 
)

Convert the len characters long character sequence given in input that is in the given input charset to a string in given output charset.

Parameters
inputinput string
lennumber of bytes in input
input_charsetcharacter set used for input
output_charsetdesired character set for the return value
Returns
the converted string (0-terminated), if conversion fails, a copy of the original string is returned.

Definition at line 343 of file strings.c.

347{
348 char *ret;
349 uint8_t *u8_string;
350 char *encoded_string;
351 size_t u8_string_length;
352 size_t encoded_string_length;
353
354 u8_string = u8_conv_from_encoding (input_charset,
355 iconveh_error,
356 input,
357 len,
358 NULL,
359 NULL,
360 &u8_string_length);
361 if (NULL == u8_string)
362 {
363 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
364 goto fail;
365 }
366 if (0 == strcmp (output_charset, "UTF-8"))
367 {
368 ret = GNUNET_malloc (u8_string_length + 1);
369 GNUNET_memcpy (ret, u8_string, u8_string_length);
370 ret[u8_string_length] = '\0';
371 free (u8_string);
372 return ret;
373 }
374 encoded_string = u8_conv_to_encoding (output_charset,
375 iconveh_error,
376 u8_string,
377 u8_string_length,
378 NULL,
379 NULL,
380 &encoded_string_length);
381 free (u8_string);
382 if (NULL == encoded_string)
383 {
384 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
385 goto fail;
386 }
387 ret = GNUNET_malloc (encoded_string_length + 1);
388 GNUNET_memcpy (ret, encoded_string, encoded_string_length);
389 ret[encoded_string_length] = '\0';
390 free (encoded_string);
391 return ret;
392fail:
394 _ ("Character sets requested were `%s'->`%s'\n"),
395 "UTF-8",
396 output_charset);
397 ret = GNUNET_malloc (len + 1);
398 GNUNET_memcpy (ret, input, len);
399 ret[len] = '\0';
400 return ret;
401}
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_ERROR_TYPE_WARNING
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#define LOG(kind,...)
Definition: strings.c:37
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39

References _, GNUNET_ERROR_TYPE_WARNING, GNUNET_malloc, GNUNET_memcpy, LOG, LOG_STRERROR, and ret.

Referenced by GNUNET_STRINGS_from_utf8(), and GNUNET_STRINGS_to_utf8().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_utf8()

char * GNUNET_STRINGS_to_utf8 ( const char *  input,
size_t  len,
const char *  charset 
)

Convert the len characters long character sequence given in input that is in the given charset to UTF-8.

Parameters
inputthe input string (not necessarily 0-terminated)
lenthe number of bytes in the input
charsetcharacter set to convert from
Returns
the converted string (0-terminated)

Definition at line 405 of file strings.c.

408{
409 return GNUNET_STRINGS_conv (input,
410 len,
411 charset,
412 "UTF-8");
413}
char * GNUNET_STRINGS_conv(const char *input, size_t len, const char *input_charset, const char *output_charset)
Convert the len characters long character sequence given in input that is in the given input charset ...
Definition: strings.c:343

References GNUNET_STRINGS_conv().

Here is the call graph for this function:

◆ GNUNET_STRINGS_utf8_normalize()

char * GNUNET_STRINGS_utf8_normalize ( const char *  input)

Normalize the utf-8 input string to NFC.

Parameters
inputinput string
Returns
result (freshly allocated) or NULL on error.

Definition at line 429 of file strings.c.

430{
431 uint8_t *tmp;
432 size_t len;
433 char *output;
434 tmp = u8_normalize (UNINORM_NFC,
435 (uint8_t *) input,
436 strlen ((char*) input),
437 NULL,
438 &len);
439 if (NULL == tmp)
440 return NULL;
441 output = GNUNET_malloc (len + 1);
442 GNUNET_memcpy (output, tmp, len);
443 output[len] = '\0';
444 free (tmp);
445 return output;
446}

References GNUNET_malloc, and GNUNET_memcpy.

Referenced by GNUNET_GNSRECORD_string_normalize().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_from_utf8()

char * GNUNET_STRINGS_from_utf8 ( const char *  input,
size_t  len,
const char *  charset 
)

Convert the len bytes-long UTF-8 string given in input to the given charset.

Parameters
inputthe input string (not necessarily 0-terminated)
lenthe number of bytes in the input
charsetcharacter set to convert to
Returns
the converted string (0-terminated), if conversion fails, a copy of the original string is returned.

Definition at line 417 of file strings.c.

420{
421 return GNUNET_STRINGS_conv (input,
422 len,
423 "UTF-8",
424 charset);
425}

References GNUNET_STRINGS_conv().

Here is the call graph for this function:

◆ GNUNET_STRINGS_utf8_tolower()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_tolower ( const char *  input,
char *  output 
)

Convert the utf-8 input string to lower case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer
Returns
GNUNET_OK on success

Definition at line 450 of file strings.c.

452{
453 uint8_t *tmp_in;
454 size_t len;
455
456 tmp_in = u8_tolower ((uint8_t *) input,
457 strlen ((char *) input),
458 NULL,
459 UNINORM_NFD,
460 NULL,
461 &len);
462 if (NULL == tmp_in)
463 return GNUNET_SYSERR;
464 GNUNET_memcpy (output, tmp_in, len);
465 output[len] = '\0';
466 GNUNET_free (tmp_in);
467 return GNUNET_OK;
468}
@ GNUNET_SYSERR
#define GNUNET_free(ptr)
Wrapper around free.

References GNUNET_free, GNUNET_memcpy, GNUNET_OK, and GNUNET_SYSERR.

Referenced by ego_create(), GNUNET_RECLAIM_attribute_new(), GNUNET_RECLAIM_credential_new(), handle_collect(), handle_create_message(), handle_delete_message(), handle_rename_message(), handle_verify(), and header_iterator().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_utf8_toupper()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_toupper ( const char *  input,
char *  output 
)

Convert the utf-8 input string to upper case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer
Returns
GNUNET_OK on success

Definition at line 472 of file strings.c.

474{
475 uint8_t *tmp_in;
476 size_t len;
477
478 tmp_in = u8_toupper ((uint8_t *) input,
479 strlen ((char *) input),
480 NULL,
481 UNINORM_NFD,
482 NULL,
483 &len);
484 if (NULL == tmp_in)
485 return GNUNET_SYSERR;
486 /* 0-terminator does not fit */
487 GNUNET_memcpy (output, tmp_in, len);
488 output[len] = '\0';
489 GNUNET_free (tmp_in);
490 return GNUNET_OK;
491}

References GNUNET_free, GNUNET_memcpy, GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_CRYPTO_hash_from_string2(), and GNUNET_HOSTLIST_client_start().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_filename_expand()

char * GNUNET_STRINGS_filename_expand ( const char *  fil)

Complete filename (a la shell) from abbrevition.

Parameters
filthe name of the file, may contain ~/ or be relative to the current directory
Returns
the full file name, NULL is returned on error

Definition at line 495 of file strings.c.

496{
497 char *buffer;
498 size_t len;
499 char *fm;
500 const char *fil_ptr;
501
502 if (fil == NULL)
503 return NULL;
504
505 if (fil[0] == DIR_SEPARATOR)
506 /* absolute path, just copy */
507 return GNUNET_strdup (fil);
508 if (fil[0] == '~')
509 {
510 fm = getenv ("HOME");
511 if (fm == NULL)
512 {
514 _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
515 return NULL;
516 }
517 fm = GNUNET_strdup (fm);
518 /* do not copy '~' */
519 fil_ptr = fil + 1;
520
521 /* skip over dir separator to be consistent */
522 if (fil_ptr[0] == DIR_SEPARATOR)
523 fil_ptr++;
524 }
525 else
526 {
527 /* relative path */
528 fil_ptr = fil;
529 len = 512;
530 fm = NULL;
531 while (1)
532 {
533 buffer = GNUNET_malloc (len);
534 if (getcwd (buffer, len) != NULL)
535 {
536 fm = buffer;
537 break;
538 }
539 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
540 {
541 len *= 2;
542 GNUNET_free (buffer);
543 continue;
544 }
545 GNUNET_free (buffer);
546 break;
547 }
548 if (fm == NULL)
549 {
551 buffer = getenv ("PWD"); /* alternative */
552 if (buffer != NULL)
553 fm = GNUNET_strdup (buffer);
554 }
555 if (fm == NULL)
556 fm = GNUNET_strdup ("./"); /* give up */
557 }
558 GNUNET_asprintf (&buffer,
559 "%s%s%s",
560 fm,
561 (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
563 fil_ptr);
564 GNUNET_free (fm);
565 return buffer;
566}
char * getenv()
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define DIR_SEPARATOR
Definition: platform.h:165
#define DIR_SEPARATOR_STR
Definition: platform.h:166

References _, DIR_SEPARATOR, DIR_SEPARATOR_STR, getenv(), GNUNET_asprintf(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_malloc, GNUNET_strdup, LOG, and LOG_STRERROR.

Referenced by check_client_index_start(), file_test_internal(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_load(), GNUNET_CONFIGURATION_parse(), GNUNET_CONFIGURATION_write(), GNUNET_DISK_directory_create(), GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_directory_scan(), GNUNET_DISK_file_open(), GNUNET_FS_directory_scan_start(), GNUNET_FS_make_file_reader_context_(), GNUNET_log_setup(), handle_client_index_start(), handle_inline(), hash_for_index_cb(), print_filename_option(), and set_filename().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_buffer_fill()

size_t GNUNET_STRINGS_buffer_fill ( char *  buffer,
size_t  size,
unsigned int  count,
  ... 
)

Fill a buffer of the given size with count 0-terminated strings (given as varargs).

If "buffer" is NULL, only compute the amount of space required (sum of "strlen(arg)+1").

Unlike using "snprintf" with "%s", this function will add 0-terminators after each string. The "GNUNET_string_buffer_tokenize" function can be used to parse the buffer back into individual strings.

Parameters
bufferthe buffer to fill with strings, can be NULL in which case only the necessary amount of space will be calculated
sizenumber of bytes available in buffer
countnumber of strings that follow
...count 0-terminated strings to copy to buffer
Returns
number of bytes written to the buffer (or number of bytes that would have been written)

Definition at line 44 of file strings.c.

47{
48 size_t needed;
49 va_list ap;
50
51 needed = 0;
52 va_start (ap, count);
53 while (count > 0)
54 {
55 const char *s = va_arg (ap, const char *);
56 size_t slen = strlen (s) + 1;
57
58 GNUNET_assert (slen <= size - needed);
59 if (NULL != buffer)
60 GNUNET_memcpy (&buffer[needed],
61 s,
62 slen);
63 needed += slen;
64 count--;
65 }
66 va_end (ap);
67 return needed;
68}
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.

References GNUNET_assert, GNUNET_memcpy, and size.

Referenced by save(), transmit(), transmit_get(), transmit_set(), and transmit_watch().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_buffer_tokenize()

unsigned int GNUNET_STRINGS_buffer_tokenize ( const char *  buffer,
size_t  size,
unsigned int  count,
  ... 
)

Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count (varargs) of type "const char**" to the locations of the respective strings in the buffer.

Parameters
bufferthe buffer to parse
sizesize of the buffer
countnumber of strings to locate
...pointers to where to store the strings
Returns
offset of the character after the last 0-termination in the buffer, or 0 on error.

Definition at line 72 of file strings.c.

76{
77 unsigned int start;
78 unsigned int needed;
79 const char **r;
80 va_list ap;
81
82 needed = 0;
83 va_start (ap, count);
84 while (count > 0)
85 {
86 r = va_arg (ap, const char **);
87
88 start = needed;
89 while ((needed < size) && (buffer[needed] != '\0'))
90 needed++;
91 if (needed == size)
92 {
93 va_end (ap);
94 return 0; /* error */
95 }
96 *r = &buffer[start];
97 needed++; /* skip 0-termination */
98 count--;
99 }
100 va_end (ap);
101 return needed;
102}
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39

References size, and start.

Referenced by check_get(), check_set(), check_statistics_value(), check_watch(), handle_get(), handle_set(), handle_statistics_value(), handle_watch(), and process_sblock().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_short_name()

const char * GNUNET_STRINGS_get_short_name ( const char *  filename)

"man basename" Returns a pointer to a part of filename (allocates nothing)!

Parameters
filenamefilename to extract basename from
Returns
short (base) name of the file (that is, everything following the last directory separator in filename. If filename ends with a directory separator, the result will be a zero-length string. If filename has no directory separators, the result is filename itself.

Definition at line 640 of file strings.c.

641{
642 const char *short_fn = filename;
643 const char *ss;
644
645 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
646 short_fn = 1 + ss;
647 return short_fn;
648}
static char * filename

References DIR_SEPARATOR_STR, and filename.

Referenced by expand_tree(), iter_check_config(), and iter_testbed_path().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_data_to_string()

char * GNUNET_STRINGS_data_to_string ( const void *  data,
size_t  size,
char *  out,
size_t  out_size 
)

Convert binary data to ASCII encoding using CrockfordBase32.

Does not append 0-terminator, but returns a pointer to the place where it should be placed, if needed.

Parameters
datadata to encode
sizesize of data (in bytes)
outbuffer to fill
out_sizesize of the buffer. Must be large enough to hold ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
Returns
pointer to the next byte in 'out' or NULL on error.

32 characters for encoding

Definition at line 709 of file strings.c.

713{
717 static char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
718 unsigned int wpos;
719 unsigned int rpos;
720 unsigned int bits;
721 unsigned int vbit;
722 const unsigned char *udata;
723
724 GNUNET_assert (size < SIZE_MAX / 8 - 4);
725 udata = data;
726 if (out_size < (size * 8 + 4) / 5)
727 {
728 GNUNET_break (0);
729 return NULL;
730 }
731 vbit = 0;
732 wpos = 0;
733 rpos = 0;
734 bits = 0;
735 while ((rpos < size) || (vbit > 0))
736 {
737 if ((rpos < size) && (vbit < 5))
738 {
739 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
740 vbit += 8;
741 }
742 if (vbit < 5)
743 {
744 bits <<= (5 - vbit); /* zero-padding */
745 GNUNET_assert (vbit == ((size * 8) % 5));
746 vbit = 5;
747 }
748 if (wpos >= out_size)
749 {
750 GNUNET_break (0);
751 return NULL;
752 }
753 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
754 vbit -= 5;
755 }
756 GNUNET_assert (0 == vbit);
757 if (wpos < out_size)
758 out[wpos] = '\0';
759 return &out[wpos];
760}
static char * data
The data to insert into the dht.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define SIZE_MAX
Definition: platform.h:208

References data, GNUNET_assert, GNUNET_break, size, and SIZE_MAX.

Referenced by auth_key_to_string(), gnunet-chk::encode_data_to_string(), GNUNET_buffer_write_data_encoded(), GNUNET_CRYPTO_ecdsa_private_key_to_string(), GNUNET_CRYPTO_ecdsa_public_key_to_string(), GNUNET_CRYPTO_eddsa_private_key_to_string(), GNUNET_CRYPTO_eddsa_public_key_to_string(), GNUNET_CRYPTO_hash_to_enc(), GNUNET_GNSRECORD_z2s(), GNUNET_PQ_event_notify(), GNUNET_sh2s(), GNUNET_STRINGS_data_to_string_alloc(), GNUNET_uuid2s(), output_message(), print_examples_ecdh(), run_edkey(), run_pkey(), run_with_key(), sh_to_channel(), uri_loc_to_string(), and uri_sks_to_string().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_data_to_string_alloc()

char * GNUNET_STRINGS_data_to_string_alloc ( const void *  buf,
size_t  size 
)

Return the base32crockford encoding of the given buffer.

The returned string will be freshly allocated, and must be free'd with GNUNET_free().

Parameters
bufbuffer with data
sizesize of the buffer buf
Returns
freshly allocated, null-terminated string

Definition at line 764 of file strings.c.

765{
766 char *str_buf;
767 size_t len = size * 8;
768 char *end;
769
770 if (len % 5 > 0)
771 len += 5 - len % 5;
772 len /= 5;
773 str_buf = GNUNET_malloc (len + 1);
775 size,
776 str_buf,
777 len);
778 if (NULL == end)
779 {
780 GNUNET_free (str_buf);
781 return NULL;
782 }
783 *end = '\0';
784 return str_buf;
785}
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:709

References end, GNUNET_free, GNUNET_malloc, GNUNET_STRINGS_data_to_string(), and size.

Referenced by attr_collect(), attr_store_task(), conversation_value_to_string(), cred_collect(), cred_iter_cb(), cred_store_task(), d2j(), generate_id_token_body(), generate_userinfo_json(), GNUNET_b2s(), GNUNET_CRYPTO_private_key_to_string(), GNUNET_CRYPTO_public_key_to_string(), GNUNET_HELLO_builder_to_url(), GNUNET_JSON_from_data(), handle_attribute_delete_message(), handle_credential_delete_message(), issue_ticket(), iter_cb(), lookup_authz_cb(), main(), messenger_value_to_string(), move_attr_finished(), move_attrs(), oidc_ticket_issue_cb(), process_attrs(), process_record(), purge_attributes(), RECLAIM_TICKETS_consume(), RECLAIM_TICKETS_revoke(), run(), rvk_move_attr_cb(), save_member(), save_member_session(), save_operation(), setup_job_headers(), ticket_collect(), ticket_issue_cb(), ticket_iter(), value_to_string(), and write_encrypted_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_string_to_data()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data ( const char *  enc,
size_t  enclen,
void *  out,
size_t  out_size 
)

Convert CrockfordBase32 encoding back to data.

out_size must match exactly the size of the data before it was encoded.

Parameters
encthe encoding
enclennumber of characters in enc (without 0-terminator, which can be missing)
outlocation where to store the decoded data
out_sizesize of the output buffer out
Returns
GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding

Definition at line 789 of file strings.c.

793{
794 size_t rpos;
795 size_t wpos;
796 unsigned int bits;
797 unsigned int vbit;
798 int ret;
799 int shift;
800 unsigned char *uout;
801 size_t encoded_len;
802
803 if (0 == enclen)
804 {
805 if (0 == out_size)
806 return GNUNET_OK;
807 return GNUNET_SYSERR;
808 }
809 GNUNET_assert (out_size < SIZE_MAX / 8);
810 encoded_len = out_size * 8;
811 uout = out;
812 wpos = out_size;
813 rpos = enclen;
814 if ((encoded_len % 5) > 0)
815 {
816 vbit = encoded_len % 5; /* padding! */
817 shift = 5 - vbit;
818 bits = (ret = getValue__ (enc[--rpos])) >> shift;
819 }
820 else
821 {
822 vbit = 5;
823 shift = 0;
824 bits = (ret = getValue__ (enc[--rpos]));
825 }
826 if ((encoded_len + shift) / 5 != enclen)
827 return GNUNET_SYSERR;
828 if (-1 == ret)
829 return GNUNET_SYSERR;
830 while (wpos > 0)
831 {
832 if (0 == rpos)
833 {
834 GNUNET_break (0);
835 return GNUNET_SYSERR;
836 }
837 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
838 if (-1 == ret)
839 return GNUNET_SYSERR;
840 vbit += 5;
841 if (vbit >= 8)
842 {
843 uout[--wpos] = (unsigned char) bits;
844 bits >>= 8;
845 vbit -= 8;
846 }
847 }
848 if ((0 != rpos) || (0 != vbit))
849 return GNUNET_SYSERR;
850 return GNUNET_OK;
851}
static OpusEncoder * enc
OPUS encoder.
static unsigned int getValue__(unsigned char a)
Get the decoded value corresponding to a character according to Crockford Base32 encoding.
Definition: strings.c:659

References enc, getValue__(), GNUNET_assert, GNUNET_break, GNUNET_OK, GNUNET_SYSERR, ret, and SIZE_MAX.

Referenced by channel_to_sh(), check_authorization(), conversation_string_to_value(), delete_attribute_cont(), delete_credential_cont(), ego_create(), expect_data_dynamic(), expect_data_fixed(), GNUNET_CONFIGURATION_get_data(), GNUNET_CRYPTO_ecdsa_public_key_from_string(), GNUNET_CRYPTO_eddsa_private_key_from_string(), GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_CRYPTO_hash_from_string2(), GNUNET_CRYPTO_private_key_from_string(), GNUNET_CRYPTO_public_key_from_string(), GNUNET_HELLO_builder_from_url(), GNUNET_STRINGS_string_to_data_alloc(), lookup_redirect_uri_result(), main(), messenger_string_to_value(), parse_attr(), parse_credential(), parse_fixed_data(), parse_rsa_public_key(), parse_rsa_signature(), parse_ticket(), read_service_conf(), run(), set_base32(), start_process(), string_to_auth_key(), string_to_value(), uri_loc_parse(), and uri_sks_parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_string_to_data_alloc()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc ( const char *  enc,
size_t  enclen,
void **  out,
size_t *  out_size 
)

Convert CrockfordBase32 encoding back to data.

out_size will be determined from enc and out will be allocated to be large enough.

Parameters
encthe encoding
enclennumber of characters in enc (without 0-terminator, which can be missing)
[out]outlocation where to allocate and store the decoded data
[out]out_sizeset to the size of the output buffer out
Returns
GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding

Definition at line 855 of file strings.c.

859{
860 size_t size;
861 void *data;
862 int res;
863
864 size = (enclen * 5) / 8;
866 {
867 GNUNET_break_op (0);
868 return GNUNET_SYSERR;
869 }
872 enclen,
873 data,
874 size);
875 if ( (0 < size) &&
876 (GNUNET_OK != res) )
877 {
878 size--;
880 enclen,
881 data,
882 size);
883 }
884 if (GNUNET_OK != res)
885 {
886 GNUNET_break_op (0);
888 return GNUNET_SYSERR;
889 }
890 *out = data;
891 *out_size = size;
892 return GNUNET_OK;
893}
static char * res
Currently read line or NULL on EOF.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:789

References data, enc, GNUNET_break_op, GNUNET_free, GNUNET_malloc, GNUNET_MAX_MALLOC_CHECKED, GNUNET_OK, GNUNET_STRINGS_string_to_data(), GNUNET_SYSERR, res, and size.

Referenced by GNUNET_PQ_event_do_poll(), parse_variable_data(), and read_encrypted_message().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_base64_encode()

size_t GNUNET_STRINGS_base64_encode ( const void *  in,
size_t  len,
char **  output 
)

Encode into Base64.

Parameters
datathe data to encode
lenthe length of the input
outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 1622 of file strings.c.

1625{
1626 const unsigned char *data = in;
1627 size_t ret;
1628 char *opt;
1629
1630 ret = 0;
1631 GNUNET_assert (len < SIZE_MAX / 4);
1632 opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1633 for (size_t i = 0; i < len; ++i)
1634 {
1635 char c;
1636
1637 c = (data[i] >> 2) & 0x3f;
1638 opt[ret++] = cvt[(int) c];
1639 c = (data[i] << 4) & 0x3f;
1640 if (++i < len)
1641 c |= (data[i] >> 4) & 0x0f;
1642 opt[ret++] = cvt[(int) c];
1643 if (i < len)
1644 {
1645 c = (data[i] << 2) & 0x3f;
1646 if (++i < len)
1647 c |= (data[i] >> 6) & 0x03;
1648 opt[ret++] = cvt[(int) c];
1649 }
1650 else
1651 {
1652 ++i;
1653 opt[ret++] = FILLCHAR;
1654 }
1655 if (i < len)
1656 {
1657 c = data[i] & 0x3f;
1658 opt[ret++] = cvt[(int) c];
1659 }
1660 else
1661 {
1662 opt[ret++] = FILLCHAR;
1663 }
1664 }
1665 *output = opt;
1666 return ret;
1667}
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1615
static char * cvt
Definition: strings.c:1616

References cvt, data, FILLCHAR, GNUNET_assert, GNUNET_malloc, consensus-simulation::int, ret, and SIZE_MAX.

Referenced by dns_value_to_string(), GNUNET_ABD_delegate_to_string(), GNUNET_HELLO_sign_address(), GNUNET_JSON_from_data64(), GNUNET_STRINGS_base64url_encode(), OIDC_access_token_new(), pabc_create_presentation(), and store_and_free_entries().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_urlencode()

size_t GNUNET_STRINGS_urlencode ( size_t  len,
const char  data[static len],
char **  out 
)

url/percent encode (RFC3986).

FIXME: awkward API, len is not actually used out is 0-terminated, should probably be changed to only input data and directly return @out or NULL.

Parameters
datathe data to decode
lenthe length of the input
outwhere to write the output (*out should be NULL, is allocated)
Returns
the size of the output

Definition at line 1877 of file strings.c.

1880{
1881 struct GNUNET_Buffer buf = { 0 };
1882 const uint8_t *i8 = (uint8_t *) data;
1883 const uint8_t *end = (uint8_t *) (data + len);
1884
1885 while (end != i8)
1886 {
1887 if (0 == *i8)
1888 {
1889 /* invalid UTF-8 (or bad @a len): fail */
1890 GNUNET_break (0);
1891 GNUNET_buffer_clear (&buf);
1892 return 0;
1893 }
1894 if (0 == (0x80 & *i8))
1895 {
1896 /* traditional ASCII */
1897 if (isalnum (*i8) ||
1898 (*i8 == '-') ||
1899 (*i8 == '_') ||
1900 (*i8 == '.') ||
1901 (*i8 == '~') )
1902 GNUNET_buffer_write (&buf,
1903 (const char*) i8,
1904 1);
1905 else if (*i8 == ' ')
1906 GNUNET_buffer_write (&buf,
1907 "+",
1908 1);
1909 else
1911 "%%%X%X",
1912 *i8 >> 4,
1913 *i8 & 15);
1914 i8++;
1915 continue;
1916 }
1917 if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
1918 {
1919 /* 2-byte value, percent-encode */
1921 "%%%X%X",
1922 *i8 >> 4,
1923 *i8 & 15);
1924 i8++;
1925 if ( (end == i8) ||
1926 (0 == *i8) )
1927 {
1928 /* invalid UTF-8 (or bad @a len): fail */
1929 GNUNET_break (0);
1930 GNUNET_buffer_clear (&buf);
1931 return 0;
1932 }
1934 "%%%X%X",
1935 *i8 >> 4,
1936 *i8 & 15);
1937 i8++;
1938 continue;
1939 }
1940 if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
1941 {
1942 /* 3-byte value, percent-encode */
1943 for (unsigned int i = 0; i<3; i++)
1944 {
1945 if ( (end == i8) ||
1946 (0 == *i8) )
1947 {
1948 /* invalid UTF-8 (or bad @a len): fail */
1949 GNUNET_break (0);
1950 GNUNET_buffer_clear (&buf);
1951 return 0;
1952 }
1954 "%%%X%X",
1955 *i8 >> 4,
1956 *i8 & 15);
1957 i8++;
1958 }
1959 continue;
1960 }
1961 if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
1962 {
1963 /* 4-byte value, percent-encode */
1964 for (unsigned int i = 0; i<4; i++)
1965 {
1966 if ( (end == i8) ||
1967 (0 == *i8) )
1968 {
1969 /* invalid UTF-8 (or bad @a len): fail */
1970 GNUNET_break (0);
1971 GNUNET_buffer_clear (&buf);
1972 return 0;
1973 }
1975 "%%%X%X",
1976 *i8 >> 4,
1977 *i8 & 15);
1978 i8++;
1979 }
1980 continue;
1981 }
1982 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
1983 + 0x04) & *i8))
1984 {
1985 /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1986 for (unsigned int i = 0; i<5; i++)
1987 {
1988 if ( (end == i8) ||
1989 (0 == *i8) )
1990 {
1991 /* invalid UTF-8 (or bad @a len): fail */
1992 GNUNET_break (0);
1993 GNUNET_buffer_clear (&buf);
1994 return 0;
1995 }
1997 "%%%X%X",
1998 *i8 >> 4,
1999 *i8 & 15);
2000 i8++;
2001 }
2002 continue;
2003 }
2004 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
2005 + 0x08 + 0x04 + 0x02)
2006 & *i8))
2007 {
2008 /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
2009 for (unsigned int i = 0; i<6; i++)
2010 {
2011 if ( (end == i8) ||
2012 (0 == *i8) )
2013 {
2014 /* invalid UTF-8 (or bad @a len): fail */
2015 GNUNET_break (0);
2016 GNUNET_buffer_clear (&buf);
2017 return 0;
2018 }
2020 "%%%X%X",
2021 *i8 >> 4,
2022 *i8 & 15);
2023 i8++;
2024 }
2025 continue;
2026 }
2027 /* really, really invalid UTF-8: fail */
2028 GNUNET_break (0);
2029 GNUNET_buffer_clear (&buf);
2030 return 0;
2031 }
2032 *out = GNUNET_buffer_reap_str (&buf);
2033 return strlen (*out);
2034}
char * GNUNET_buffer_reap_str(struct GNUNET_Buffer *buf)
Clear the buffer and return the string it contained.
Definition: buffer.c:123
void GNUNET_buffer_write_fstr(struct GNUNET_Buffer *buf, const char *fmt,...) __attribute__((format(printf
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition: buffer.c:164
Dynamically growing buffer.

References data, end, GNUNET_break, GNUNET_buffer_clear(), GNUNET_buffer_reap_str(), GNUNET_buffer_write(), and GNUNET_buffer_write_fstr().

Referenced by GNUNET_HELLO_builder_to_url(), login_redirect(), PABC_load_public_parameters(), PABC_read_usr_ctx(), PABC_write_public_parameters(), and PABC_write_usr_ctx().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_base64url_encode()

size_t GNUNET_STRINGS_base64url_encode ( const void *  in,
size_t  len,
char **  output 
)

Encode into Base64url.

RFC7515

Parameters
inthe data to encode
lenthe length of the input
outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 1671 of file strings.c.

1674{
1675 char *enc;
1676 size_t pos;
1677
1679 len,
1680 output);
1681 enc = *output;
1682 /* Replace with correct characters for base64url */
1683 pos = 0;
1684 while ('\0' != enc[pos])
1685 {
1686 if ('+' == enc[pos])
1687 enc[pos] = '-';
1688 if ('/' == enc[pos])
1689 enc[pos] = '_';
1690 if ('=' == enc[pos])
1691 {
1692 enc[pos] = '\0';
1693 break;
1694 }
1695 pos++;
1696 }
1697 return strlen (enc);
1698}
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1622

References enc, and GNUNET_STRINGS_base64_encode().

Referenced by check_code_challenge(), DID_key_convert_gnunet_to_multibase_base64(), ego_sign_data_cb(), OIDC_build_authz_code(), and OIDC_generate_id_token_hmac().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_base64_decode()

size_t GNUNET_STRINGS_base64_decode ( const char *  data,
size_t  len,
void **  output 
)

Decode from Base64.

Parameters
datathe data to encode
lenthe length of the input
[out]outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 1724 of file strings.c.

1727{
1728 unsigned char *output;
1729 size_t ret = 0;
1730
1731 GNUNET_assert (len / 3 < SIZE_MAX);
1732 output = GNUNET_malloc ((len * 3 / 4) + 8);
1734 "base64_decode decoding len=%d\n",
1735 (int) len);
1736 for (size_t i = 0; i < len; ++i)
1737 {
1738 unsigned char c;
1739 unsigned char c1;
1740
1741 CHECK_CRLF;
1742 if (FILLCHAR == data[i])
1743 break;
1744 c = (unsigned char) cvtfind (data[i]);
1745 ++i;
1746 CHECK_CRLF;
1747 c1 = (unsigned char) cvtfind (data[i]);
1748 c = (c << 2) | ((c1 >> 4) & 0x3);
1749 output[ret++] = c;
1750 if (++i < len)
1751 {
1752 CHECK_CRLF;
1753 c = data[i];
1754 if (FILLCHAR == c)
1755 break;
1756 c = (unsigned char) cvtfind (c);
1757 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1758 output[ret++] = c1;
1759 }
1760 if (++i < len)
1761 {
1762 CHECK_CRLF;
1763 c1 = data[i];
1764 if (FILLCHAR == c1)
1765 break;
1766
1767 c1 = (unsigned char) cvtfind (c1);
1768 c = ((c << 6) & 0xc0) | c1;
1769 output[ret++] = c;
1770 }
1771 }
1772END:
1773 *out = output;
1774 return ret;
1775}
@ END
We're done processing.
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_DEBUG
#define CHECK_CRLF
Definition: strings.c:1711
#define cvtfind(a)
Definition: strings.c:1701

References CHECK_CRLF, cvtfind, data, END, FILLCHAR, GNUNET_assert, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_malloc, ret, and SIZE_MAX.

Referenced by database_setup(), dns_string_to_value(), GNUNET_ABD_delegate_from_string(), GNUNET_HELLO_extract_address(), GNUNET_STRINGS_base64url_decode(), OIDC_access_token_parse(), parse_credentials_basic_auth(), parse_fixed64_data(), and parse_jwt().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_base64url_decode()

size_t GNUNET_STRINGS_base64url_decode ( const char *  data,
size_t  len,
void **  out 
)

Decode from Base64url.

RFC7515

Parameters
datathe data to decode
lenthe length of the input
outwhere to write the output (*out should be NULL, is allocated)
Returns
the size of the output

Definition at line 1782 of file strings.c.

1785{
1786 char *s;
1787 int padding;
1788 size_t ret;
1789
1790 /* make enough space for padding */
1791 GNUNET_assert (len < SIZE_MAX - 3);
1792 s = GNUNET_malloc (len + 3);
1793 memcpy (s,
1794 data,
1795 len);
1796 for (size_t i = 0; i < strlen (s); i++)
1797 {
1798 if (s[i] == '-')
1799 s[i] = '+';
1800 if (s[i] == '_')
1801 s[i] = '/';
1802 }
1803 padding = len % 4;
1804 switch (padding) // Pad with trailing '='s
1805 {
1806 case 0:
1807 break; // No pad chars in this case
1808 case 2:
1809 memcpy (&s[len],
1810 "==",
1811 2);
1812 len += 2;
1813 break; // Two pad chars
1814 case 3:
1815 s[len] = '=';
1816 len++;
1817 break; // One pad char
1818 default:
1819 GNUNET_assert (0);
1820 break;
1821 }
1823 len,
1824 out);
1825 GNUNET_free (s);
1826 return ret;
1827}
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out)
Decode from Base64.
Definition: strings.c:1724

References data, GNUNET_assert, GNUNET_free, GNUNET_malloc, GNUNET_STRINGS_base64_decode(), ret, and SIZE_MAX.

Referenced by jwt_get_expiration(), jwt_get_issuer(), jwt_parse_attributes(), OIDC_parse_authz_code(), and set_attributes_from_idtoken().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_urldecode()

size_t GNUNET_STRINGS_urldecode ( const char *  data,
size_t  len,
char **  out 
)

url/percent encode (RFC3986).

Parameters
datathe data to encode
lenthe length of the input
[out]outwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 1831 of file strings.c.

1834{
1835 const char *rpos = data;
1836 *out = GNUNET_malloc (len + 1); /* output should always fit into input */
1837 char *wpos = *out;
1838 size_t resl = 0;
1839
1840 while ( ('\0' != *rpos) &&
1841 (data + len != rpos) )
1842 {
1843 unsigned int num;
1844 switch (*rpos)
1845 {
1846 case '%':
1847 if (rpos + 3 > data + len)
1848 {
1849 GNUNET_break_op (0);
1850 GNUNET_free (*out);
1851 return 0;
1852 }
1853 if (1 != sscanf (rpos + 1,
1854 "%2x",
1855 &num))
1856 break;
1857 *wpos = (char) ((unsigned char) num);
1858 wpos++;
1859 resl++;
1860 rpos += 3;
1861 break;
1862 /* TODO: add bad sequence handling */
1863 /* intentional fall through! */
1864 default:
1865 *wpos = *rpos;
1866 wpos++;
1867 resl++;
1868 rpos++;
1869 }
1870 }
1871 *wpos = '\0'; /* add 0-terminator */
1872 return resl;
1873}

References data, GNUNET_break_op, GNUNET_free, and GNUNET_malloc.

Referenced by get_url_parameter_copy(), and GNUNET_HELLO_builder_from_url().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_uri()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_parse_uri ( const char *  path,
char **  scheme_part,
const char **  path_part 
)

Parse a path that might be an URI.

Parameters
pathpath to parse. Must be NULL-terminated.
[out]scheme_partpointer to a string that represents the URI scheme will be stored. Can be NULL. The string is allocated by the function, and should be freed by GNUNET_free() when it is no longer needed.
path_parta pointer to 'const char *' where a pointer to the path part of the URI will be stored. Can be NULL. Points to the same block of memory as path, and thus must not be freed. Might point to '\0', if path part is zero-length.
Returns
GNUNET_YES if it's an URI, GNUNET_NO otherwise. If 'path' is not an URI, '* scheme_part' and '*path_part' will remain unchanged (if they weren't NULL).

Definition at line 897 of file strings.c.

900{
901 size_t len;
902 size_t i;
903 int end;
904 int pp_state = 0;
905 const char *post_scheme_part = NULL;
906
907 len = strlen (path);
908 for (end = 0, i = 0; ! end && i < len; i++)
909 {
910 switch (pp_state)
911 {
912 case 0:
913 if ((path[i] == ':') && (i > 0))
914 {
915 pp_state += 1;
916 continue;
917 }
918 if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
919 ((path[i] >= 'a') && (path[i] <= 'z') ) ||
920 ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
921 (path[i] == '-') || (path[i] == '.')))
922 end = 1;
923 break;
924
925 case 1:
926 case 2:
927 if (path[i] == '/')
928 {
929 pp_state += 1;
930 continue;
931 }
932 end = 1;
933 break;
934
935 case 3:
936 post_scheme_part = &path[i];
937 end = 1;
938 break;
939
940 default:
941 end = 1;
942 }
943 }
944 if (post_scheme_part == NULL)
945 return GNUNET_NO;
946 if (scheme_part)
947 {
948 *scheme_part = GNUNET_strndup (path,
949 post_scheme_part - path);
950 }
951 if (path_part)
952 *path_part = post_scheme_part;
953 return GNUNET_YES;
954}
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.

References end, GNUNET_NO, GNUNET_strndup, and GNUNET_YES.

Referenced by GNUNET_STRINGS_path_is_absolute().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_path_is_absolute()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute ( const char *  filename,
int  can_be_uri,
int *  r_is_uri,
char **  r_uri_scheme 
)

Check whether filename is absolute or not, and if it's an URI.

Parameters
filenamefilename to check
can_be_uriGNUNET_YES to check for being URI, GNUNET_NO - to assume it's not URI
r_is_uria pointer to an int that is set to GNUNET_YES if 'filename' is URI and to GNUNET_NO otherwise. Can be NULL. If 'can_be_uri' is not GNUNET_YES, *r_is_uri is set to GNUNET_NO.
r_uri_schemea pointer to a char * that is set to a pointer to URI scheme. The string is allocated by the function, and should be freed with GNUNET_free (). Can be NULL.
Returns
GNUNET_YES if 'filename' is absolute, GNUNET_NO otherwise.

Definition at line 958 of file strings.c.

962{
963 const char *post_scheme_path;
964 int is_uri;
965 char *uri;
966 /* consider POSIX paths to be absolute too, even on W32,
967 * as plibc expansion will fix them for us.
968 */
969 if (filename[0] == '/')
970 return GNUNET_YES;
971 if (can_be_uri)
972 {
973 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
974 if (r_is_uri)
975 *r_is_uri = is_uri;
976 if (is_uri)
977 {
978 if (r_uri_scheme)
979 *r_uri_scheme = uri;
980 else
982
983 return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
984 GNUNET_NO,
985 NULL,
986 NULL);
987 }
988 }
989 else
990 {
991 if (r_is_uri)
992 *r_is_uri = GNUNET_NO;
993 }
994
995 return GNUNET_NO;
996}
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it's an URI.
Definition: strings.c:958
enum GNUNET_GenericReturnValue GNUNET_STRINGS_parse_uri(const char *path, char **scheme_part, const char **path_part)
Parse a path that might be an URI.
Definition: strings.c:897

References filename, GNUNET_free, GNUNET_NO, GNUNET_STRINGS_parse_uri(), GNUNET_STRINGS_path_is_absolute(), GNUNET_YES, and uri.

Referenced by GNUNET_OS_check_helper_binary(), GNUNET_OS_get_libexec_binary_path(), GNUNET_OS_get_suid_binary_path(), GNUNET_STRINGS_check_filename(), and GNUNET_STRINGS_path_is_absolute().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_check_filename()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_check_filename ( const char *  filename,
enum GNUNET_STRINGS_FilenameCheck  checks 
)

Perform checks on filename.

FIXME: some duplication with "GNUNET_DISK_"-APIs. We should unify those.

Parameters
filenamefile to check
checkschecks to perform
Returns
GNUNET_YES if all checks pass, GNUNET_NO if at least one of them fails, GNUNET_SYSERR when a check can't be performed

Definition at line 1000 of file strings.c.

1002{
1003 struct stat st;
1004
1005 if ((NULL == filename) || (filename[0] == '\0'))
1006 return GNUNET_SYSERR;
1007 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1009 return GNUNET_NO;
1010 if (0 != (checks
1013 {
1014 if (0 != lstat (filename, &st))
1015 {
1016 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1017 return GNUNET_NO;
1018 else
1019 return GNUNET_SYSERR;
1020 }
1021 }
1022 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1023 if (! S_ISDIR (st.st_mode))
1024 return GNUNET_NO;
1025 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1026 if (! S_ISLNK (st.st_mode))
1027 return GNUNET_NO;
1028 return GNUNET_YES;
1029}
#define S_ISLNK(m)
Definition: disk.c:60
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.

References filename, GNUNET_NO, GNUNET_STRINGS_CHECK_EXISTS, GNUNET_STRINGS_CHECK_IS_ABSOLUTE, GNUNET_STRINGS_CHECK_IS_DIRECTORY, GNUNET_STRINGS_CHECK_IS_LINK, GNUNET_STRINGS_path_is_absolute(), GNUNET_SYSERR, GNUNET_YES, S_ISLNK, and st.

Here is the call graph for this function:

◆ GNUNET_STRINGS_to_address_ipv6()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv6 ( const char *  zt_addr,
size_t  addrlen,
struct sockaddr_in6 *  r_buf 
)

Tries to convert zt_addr string to an IPv6 address.

The string is expected to have the format "[ABCD::01]:80".

Parameters
zt_addr0-terminated string. May be mangled by the function.
addrlenlength of zt_addr (not counting 0-terminator).
r_bufa buffer to fill. Initially gets filled with zeroes, then its sin6_port, sin6_family and sin6_addr are set appropriately.
Returns
GNUNET_OK if conversion succeeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1033 of file strings.c.

1036{
1037 if (addrlen < 6)
1038 return GNUNET_SYSERR;
1039 if (addrlen > 512)
1040 return GNUNET_SYSERR; /* sanity check to protect zbuf allocation,
1041 actual limit is not precise */
1042 {
1043 char zbuf[addrlen + 1];
1044 int ret;
1045 char *port_colon;
1046 unsigned int port;
1047 char dummy[2];
1048
1049 GNUNET_memcpy (zbuf, zt_addr, addrlen);
1050 if ('[' != zbuf[0])
1051 {
1053 _ ("IPv6 address did not start with `['\n"));
1054 return GNUNET_SYSERR;
1055 }
1056 zbuf[addrlen] = '\0';
1057 port_colon = strrchr (zbuf, ':');
1058 if (NULL == port_colon)
1059 {
1061 _ ("IPv6 address did contain ':' to separate port number\n"));
1062 return GNUNET_SYSERR;
1063 }
1064 if (']' != *(port_colon - 1))
1065 {
1066 GNUNET_log (
1068 _ (
1069 "IPv6 address did contain ']' before ':' to separate port number\n"));
1070 return GNUNET_SYSERR;
1071 }
1072 ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1073 if ((1 != ret) || (port > 65535))
1074 {
1075 GNUNET_log (
1077 _ (
1078 "IPv6 address did contain a valid port number after the last ':'\n"));
1079 return GNUNET_SYSERR;
1080 }
1081 *(port_colon - 1) = '\0';
1082 memset (r_buf, 0, sizeof(struct sockaddr_in6));
1083 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1084 if (ret <= 0)
1085 {
1087 _ ("Invalid IPv6 address `%s': %s\n"),
1088 &zbuf[1],
1089 strerror (errno));
1090 return GNUNET_SYSERR;
1091 }
1092 r_buf->sin6_port = htons (port);
1093 r_buf->sin6_family = AF_INET6;
1094#if HAVE_SOCKADDR_IN_SIN_LEN
1095 r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1096#endif
1097 return GNUNET_OK;
1098 }
1099}
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.

References _, dummy, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_memcpy, GNUNET_OK, GNUNET_SYSERR, port, and ret.

Referenced by GNUNET_STRINGS_parse_socket_addr(), and GNUNET_STRINGS_to_address_ip().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_address_ipv4()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv4 ( const char *  zt_addr,
size_t  addrlen,
struct sockaddr_in *  r_buf 
)

Tries to convert zt_addr string to an IPv4 address.

The string is expected to have the format "1.2.3.4:80".

Parameters
zt_addr0-terminated string. May be mangled by the function.
addrlenlength of zt_addr (not counting 0-terminator).
r_bufa buffer to fill.
Returns
GNUNET_OK if conversion succeeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1103 of file strings.c.

1106{
1107 unsigned int temps[4];
1108 unsigned int port;
1109 unsigned int cnt;
1110 char dummy[2];
1111
1112 if (addrlen < 9)
1113 return GNUNET_SYSERR;
1114 cnt = sscanf (zt_addr,
1115 "%u.%u.%u.%u:%u%1s",
1116 &temps[0],
1117 &temps[1],
1118 &temps[2],
1119 &temps[3],
1120 &port,
1121 dummy);
1122 if (5 != cnt)
1123 return GNUNET_SYSERR;
1124 for (cnt = 0; cnt < 4; cnt++)
1125 if (temps[cnt] > 0xFF)
1126 return GNUNET_SYSERR;
1127 if (port > 65535)
1128 return GNUNET_SYSERR;
1129 r_buf->sin_family = AF_INET;
1130 r_buf->sin_port = htons (port);
1131 r_buf->sin_addr.s_addr =
1132 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1133#if HAVE_SOCKADDR_IN_SIN_LEN
1134 r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1135#endif
1136 return GNUNET_OK;
1137}

References dummy, GNUNET_OK, GNUNET_SYSERR, and port.

Referenced by GNUNET_STRINGS_parse_socket_addr(), and GNUNET_STRINGS_to_address_ip().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_socket_addr()

size_t GNUNET_STRINGS_parse_socket_addr ( const char *  addr,
uint8_t *  af,
struct sockaddr **  sa 
)

Parse an address given as a string into a struct sockaddr.

Parameters
addrthe address
[out]afset to the parsed address family (e.g. AF_INET)
[out]saset to the parsed address
Returns
0 on error, otherwise number of bytes in sa

Definition at line 1156 of file strings.c.

1159{
1160 *af = AF_UNSPEC;
1161 if ('[' == *addr)
1162 {
1163 /* IPv6 */
1164 *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1165 if (GNUNET_OK !=
1167 strlen (addr),
1168 (struct sockaddr_in6 *) *sa))
1169 {
1170 GNUNET_free (*sa);
1171 *sa = NULL;
1172 return 0;
1173 }
1174 *af = AF_INET6;
1175 return sizeof(struct sockaddr_in6);
1176 }
1177 else
1178 {
1179 /* IPv4 */
1180 *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1181 if (GNUNET_OK !=
1183 strlen (addr),
1184 (struct sockaddr_in *) *sa))
1185 {
1186 GNUNET_free (*sa);
1187 *sa = NULL;
1188 return 0;
1189 }
1190 *af = AF_INET;
1191 return sizeof(struct sockaddr_in);
1192 }
1193}
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, size_t addrlen, struct sockaddr_in6 *r_buf)
Tries to convert zt_addr string to an IPv6 address.
Definition: strings.c:1033
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, size_t addrlen, struct sockaddr_in *r_buf)
Tries to convert zt_addr string to an IPv4 address.
Definition: strings.c:1103

References GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_STRINGS_to_address_ipv4(), and GNUNET_STRINGS_to_address_ipv6().

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_address_ip()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ip ( const char *  addr,
uint16_t  addrlen,
struct sockaddr_storage *  r_buf 
)

Tries to convert addr string to an IP (v4 or v6) address.

Will automatically decide whether to treat 'addr' as v4 or v6 address.

Parameters
addra string, may not be 0-terminated.
addrlennumber of bytes in addr (if addr is 0-terminated, 0-terminator should not be counted towards addrlen).
r_bufa buffer to fill.
Returns
GNUNET_OK if conversion succeeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1141 of file strings.c.

1144{
1145 if (addr[0] == '[')
1146 return GNUNET_STRINGS_to_address_ipv6 (addr,
1147 addrlen,
1148 (struct sockaddr_in6 *) r_buf);
1149 return GNUNET_STRINGS_to_address_ipv4 (addr,
1150 addrlen,
1151 (struct sockaddr_in *) r_buf);
1152}

References GNUNET_STRINGS_to_address_ipv4(), and GNUNET_STRINGS_to_address_ipv6().

Here is the call graph for this function:

◆ GNUNET_STRINGS_get_utf8_args()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args ( int  argc,
char *const *  argv,
int *  u8argc,
char *const **  u8argv 
)

Returns utf-8 encoded arguments.

Does nothing (returns a copy of argc and argv) on any platform other than W32. Returned argv has u8argv[u8argc] == NULL. Returned argv is a single memory block, and can be freed with a single GNUNET_free() call.

Parameters
argcargc (as given by main())
argvargv (as given by main())
u8argca location to store new argc in (though it's th same as argc)
u8argva location to store new argv in
Returns
GNUNET_OK on success, GNUNET_SYSERR on failure

Definition at line 1230 of file strings.c.

1234{
1235 char *const *new_argv =
1236 (char *const *) _make_continuous_arg_copy (argc, argv);
1237 *u8argv = new_argv;
1238 *u8argc = argc;
1239 return GNUNET_OK;
1240}
static char *const * _make_continuous_arg_copy(int argc, char *const *argv)
Makes a copy of argv that consists of a single memory chunk that can be freed with a single call to G...
Definition: strings.c:1201

References _make_continuous_arg_copy(), and GNUNET_OK.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_strlcpy()

size_t GNUNET_strlcpy ( char *  dst,
const char *  src,
size_t  n 
)

Like strlcpy but portable.

The given string src is copied in full length (until its null byte). The destination buffer is guaranteed to be null-terminated.

to a destination buffer and ensures that the destination string is null-terminated.

Parameters
dstdestination of the copy
srcsource of the copy, must be null-terminated
nthe length of the string to copy, including its terminating null byte
Returns
the length of the string that was copied, excluding the terminating null byte

Definition at line 138 of file strings.c.

141{
142 size_t slen;
143
144 GNUNET_assert (0 != n);
145 slen = strnlen (src, n - 1);
146 memcpy (dst, src, slen);
147 dst[slen] = '\0';
148 return slen;
149}
static size_t strnlen(const char *s, size_t n)

References GNUNET_assert, and strnlen().

Referenced by add_unixpath(), create_keys(), GCP_2s(), GNUNET_i2s(), GNUNET_i2s2(), mylog(), regex_split(), tokenizer_cb(), and try_unixpath().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_suffix_from_binary_name()

char * GNUNET_STRINGS_get_suffix_from_binary_name ( const char *  argv0)

Sometimes we use the binary name to determine which specific test to run.

In those cases, the string after the last "_" in 'argv[0]' specifies a string that determines the configuration file or plugin to use.

This function returns the respective substring, taking care of issues such as binaries ending in '.exe' on W32.

Parameters
argv0the name of the binary
Returns
string between the last '_' and the '.exe' (or the end of the string), NULL if argv0 has no '_'

Definition at line 2051 of file strings.c.

2052{
2053 const char *ret;
2054 const char *dot;
2055
2056 ret = strrchr (argv0, '_');
2057 if (NULL == ret)
2058 return NULL;
2059 ret++; /* skip underscore */
2060 dot = strchr (ret,
2061 '.');
2062 if (NULL != dot)
2063 return GNUNET_strndup (ret,
2064 dot - ret);
2065 return GNUNET_strdup (ret);
2066}

References GNUNET_strdup, GNUNET_strndup, and ret.

◆ GNUNET_STRINGS_parse_ipv4_policy()

struct GNUNET_STRINGS_IPv4NetworkPolicy * GNUNET_STRINGS_parse_ipv4_policy ( const char *  routeListX)

Parse an IPv4 network policy.

The argument specifies a list of subnets. The format is (network[/netmask][:[!]SPORT-DPORT];)* (no whitespace, must be terminated with a semicolon). The network must be given in dotted-decimal notation. The netmask can be given in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).

Parameters
routeListXa string specifying the IPv4 subnets
Returns
the converted list, terminated with all zeros; NULL if the synatx is flawed

Definition at line 1298 of file strings.c.

1299{
1300 size_t count;
1301 size_t len;
1302 size_t pos;
1303 unsigned int temps[8];
1305 char *routeList;
1306
1307 if (NULL == routeListX)
1308 return NULL;
1309 len = strlen (routeListX);
1310 if (0 == len)
1311 return NULL;
1312 routeList = GNUNET_strdup (routeListX);
1313 count = 0;
1314 for (size_t i = 0; i < len; i++)
1315 if (routeList[i] == ';')
1316 count++;
1317 GNUNET_assert (count < SIZE_MAX);
1318 result = GNUNET_new_array (count + 1,
1320 pos = 0;
1321 for (size_t i = 0; i < count; i++)
1322 {
1323 size_t colon;
1324 size_t end;
1325 char dummy;
1326
1327 for (colon = pos; ':' != routeList[colon]; colon++)
1328 if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1329 break;
1330 for (end = colon; ';' != routeList[end]; end++)
1331 if ('\0' == routeList[end])
1332 break;
1333 if ('\0' == routeList[end])
1334 break;
1335 routeList[end] = '\0';
1336 if (':' == routeList[colon])
1337 {
1338 routeList[colon] = '\0';
1339 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1340 break;
1341 }
1342 if (8 ==
1343 sscanf (&routeList[pos],
1344 "%u.%u.%u.%u/%u.%u.%u.%u%c",
1345 &temps[0],
1346 &temps[1],
1347 &temps[2],
1348 &temps[3],
1349 &temps[4],
1350 &temps[5],
1351 &temps[6],
1352 &temps[7],
1353 &dummy))
1354 {
1355 for (unsigned int j = 0; j < 8; j++)
1356 if (temps[j] > 0xFF)
1357 {
1359 _ ("Invalid format for IP: `%s'\n"),
1360 &routeList[pos]);
1362 GNUNET_free (routeList);
1363 return NULL;
1364 }
1365 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1366 + (temps[2] << 8) + temps[3]);
1367 result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1368 + (temps[6] << 8) + temps[7]);
1369 pos = end + 1;
1370 continue;
1371 }
1372
1373 /* try second notation */
1374 {
1375 unsigned int slash;
1376
1377 if (5 ==
1378 sscanf (&routeList[pos],
1379 "%u.%u.%u.%u/%u%c",
1380 &temps[0],
1381 &temps[1],
1382 &temps[2],
1383 &temps[3],
1384 &slash,
1385 &dummy))
1386 {
1387 for (unsigned int j = 0; j < 4; j++)
1388 if (temps[j] > 0xFF)
1389 {
1391 _ ("Invalid format for IP: `%s'\n"),
1392 &routeList[pos]);
1394 GNUNET_free (routeList);
1395 return NULL;
1396 }
1397 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1398 + (temps[2] << 8) + temps[3]);
1399 if (slash <= 32)
1400 {
1401 result[i].netmask.s_addr = 0;
1402 while (slash > 0)
1403 {
1404 result[i].netmask.s_addr =
1405 (result[i].netmask.s_addr >> 1) + 0x80000000;
1406 slash--;
1407 }
1408 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1409 pos = end + 1;
1410 continue;
1411 }
1412 else
1413 {
1415 _ (
1416 "Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1417 slash);
1419 GNUNET_free (routeList);
1420 return NULL; /* error */
1421 }
1422 }
1423 }
1424
1425 /* try third notation */
1426 if (4 ==
1427 sscanf (&routeList[pos],
1428 "%u.%u.%u.%u%c",
1429 &temps[0],
1430 &temps[1],
1431 &temps[2],
1432 &temps[3],
1433 &dummy))
1434 {
1435 for (unsigned int j = 0; j < 4; j++)
1436 if (temps[j] > 0xFF)
1437 {
1439 _ ("Invalid format for IP: `%s'\n"),
1440 &routeList[pos]);
1442 GNUNET_free (routeList);
1443 return NULL;
1444 }
1445 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1446 + (temps[2] << 8) + temps[3]);
1447 result[i].netmask.s_addr = htonl (0xffffffff); /* yeah, the htonl is useless */
1448 pos = end + 1;
1449 continue;
1450 }
1452 _ ("Invalid format for IP: `%s'\n"),
1453 &routeList[pos]);
1455 GNUNET_free (routeList);
1456 return NULL; /* error */
1457 }
1458 if (pos < strlen (routeList))
1459 {
1461 _ ("Invalid format: `%s'\n"),
1462 &routeListX[pos]);
1464 GNUNET_free (routeList);
1465 return NULL; /* oops */
1466 }
1467 GNUNET_free (routeList);
1468 return result; /* ok */
1469}
static int result
Global testing status.
#define GNUNET_new_array(n, type)
Allocate a size n array with structs or unions of the given type.
static enum GNUNET_GenericReturnValue parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1253
IPV4 network in CIDR notation.
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
struct in_addr netmask
IPv4 netmask.

References _, dummy, end, GNUNET_assert, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_new_array, GNUNET_OK, GNUNET_strdup, LOG, GNUNET_STRINGS_IPv4NetworkPolicy::netmask, parse_port_policy(), GNUNET_STRINGS_IPv4NetworkPolicy::pp, result, and SIZE_MAX.

Referenced by GNUNET_TUN_ipv4policy2regex(), and process_acl4().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_ipv6_policy()

struct GNUNET_STRINGS_IPv6NetworkPolicy * GNUNET_STRINGS_parse_ipv6_policy ( const char *  routeListX)

Parse an IPv6 network policy.

The argument specifies a list of subnets. The format is (network[/netmask[:[!]SPORT[-DPORT]]];)* (no whitespace, must be terminated with a semicolon). The network must be given in colon-hex notation. The netmask must be given in CIDR notation (/16) or can be omitted to specify a single host. Note that the netmask is mandatory if ports are specified.

Parameters
routeListXa string specifying the policy
Returns
the converted list, 0-terminated, NULL if the synatx is flawed

Definition at line 1473 of file strings.c.

1474{
1475 size_t count;
1476 size_t len;
1477 size_t pos;
1478 int ret;
1479 char *routeList;
1481 unsigned int off;
1482
1483 if (NULL == routeListX)
1484 return NULL;
1485 len = strlen (routeListX);
1486 if (0 == len)
1487 return NULL;
1488 routeList = GNUNET_strdup (routeListX);
1489 count = 0;
1490 for (size_t j = 0; j < len; j++)
1491 if (';' == routeList[j])
1492 count++;
1493 if (';' != routeList[len - 1])
1494 {
1496 _ ("Invalid network notation (does not end with ';': `%s')\n"),
1497 routeList);
1498 GNUNET_free (routeList);
1499 return NULL;
1500 }
1501 GNUNET_assert (count < UINT_MAX);
1502 result = GNUNET_new_array (count + 1,
1504 pos = 0;
1505 for (size_t i = 0; i < count; i++)
1506 {
1507 size_t start;
1508 size_t slash;
1509
1510 start = pos;
1511 while (';' != routeList[pos])
1512 pos++;
1513 slash = pos;
1514 while ( (slash > start) &&
1515 (routeList[slash] != '/') )
1516 slash--;
1517 if (slash <= start)
1518 {
1519 memset (&result[i].netmask,
1520 0xFF,
1521 sizeof(struct in6_addr));
1522 slash = pos;
1523 }
1524 else
1525 {
1526 size_t colon;
1527
1528 routeList[pos] = '\0';
1529 for (colon = pos; ':' != routeList[colon]; colon--)
1530 if ('/' == routeList[colon])
1531 break;
1532 if (':' == routeList[colon])
1533 {
1534 routeList[colon] = '\0';
1535 if (GNUNET_OK !=
1536 parse_port_policy (&routeList[colon + 1],
1537 &result[i].pp))
1538 {
1540 GNUNET_free (routeList);
1541 return NULL;
1542 }
1543 }
1544 ret = inet_pton (AF_INET6,
1545 &routeList[slash + 1],
1546 &result[i].netmask);
1547 if (ret <= 0)
1548 {
1549 char dummy;
1550 unsigned int bits;
1551 int save = errno;
1552
1553 if ( (1 != sscanf (&routeList[slash + 1],
1554 "%u%c",
1555 &bits,
1556 &dummy)) ||
1557 (bits > 128))
1558 {
1559 if (0 == ret)
1560 {
1562 _ ("Wrong format `%s' for netmask\n"),
1563 &routeList[slash]);
1564 }
1565 else
1566 {
1567 errno = save;
1569 "inet_pton");
1570 }
1572 GNUNET_free (routeList);
1573 return NULL;
1574 }
1575 off = 0;
1576 while (bits > 8)
1577 {
1578 result[i].netmask.s6_addr[off++] = 0xFF;
1579 bits -= 8;
1580 }
1581 while (bits > 0)
1582 {
1583 result[i].netmask.s6_addr[off] =
1584 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1585 bits--;
1586 }
1587 }
1588 }
1589 routeList[slash] = '\0';
1590 ret = inet_pton (AF_INET6,
1591 &routeList[start],
1592 &result[i].network);
1593 if (ret <= 0)
1594 {
1595 if (0 == ret)
1597 _ ("Wrong format `%s' for network\n"),
1598 &routeList[slash + 1]);
1599 else
1601 "inet_pton");
1603 GNUNET_free (routeList);
1604 return NULL;
1605 }
1606 pos++;
1607 }
1608 GNUNET_free (routeList);
1609 return result;
1610}
static void save()
Write persistent statistics to disk.
@ GNUNET_ERROR_TYPE_ERROR
network in CIDR notation for IPV6.
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
struct in6_addr network
IPv6 address.
struct in6_addr netmask
IPv6 netmask.

References _, dummy, GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_new_array, GNUNET_OK, GNUNET_strdup, LOG, LOG_STRERROR, GNUNET_STRINGS_IPv6NetworkPolicy::netmask, GNUNET_STRINGS_IPv6NetworkPolicy::network, parse_port_policy(), GNUNET_STRINGS_IPv6NetworkPolicy::pp, result, ret, save(), and start.

Referenced by GNUNET_TUN_ipv6policy2regex(), and process_acl6().

Here is the call graph for this function:
Here is the caller graph for this function: