GNUnet debian-0.26.1-2-gb7dda1fc7
 
Loading...
Searching...
No Matches
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.
 
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.
 
char * GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
 Convert a given filesize into a fancy human-readable format.
 
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.
 
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.
 
char * GNUNET_STRINGS_utf8_normalize (const char *input)
 Normalize the utf-8 input string to NFC.
 
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.
 
char * GNUNET_STRINGS_utf8_tolower (const char *input)
 Convert the utf-8 input string to lower case.
 
char * GNUNET_STRINGS_utf8_toupper (const char *input)
 Convert the utf-8 input string to upper case.
 
char * GNUNET_STRINGS_filename_expand (const char *fil)
 Complete filename (a la shell) from abbrevition.
 
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).
 
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.
 
const char * GNUNET_STRINGS_get_short_name (const char *filename)
 "man basename" Returns a pointer to a part of filename (allocates nothing)!
 
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.
 
char * GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size)
 Return the base32crockford encoding of the given buffer.
 
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.
 
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.
 
size_t GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
 Encode into Base64.
 
size_t GNUNET_STRINGS_urlencode (size_t len, const char data[static len], char **out)
 url/percent encode (RFC3986).
 
size_t GNUNET_STRINGS_base64url_encode (const void *in, size_t len, char **output)
 Encode into Base64url.
 
size_t GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **output)
 Decode from Base64.
 
size_t GNUNET_STRINGS_base64url_decode (const char *data, size_t len, void **out)
 Decode from Base64url.
 
size_t GNUNET_STRINGS_urldecode (const char *data, size_t len, char **out)
 url/percent encode (RFC3986).
 
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.
 
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.
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_check_filename (const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
 Perform checks on filename.
 
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.
 
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.
 
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.
 
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.
 
size_t GNUNET_strlcpy (char *dst, const char *src, size_t n)
 Like strlcpy but portable.
 
char * GNUNET_STRINGS_get_suffix_from_binary_name (const char *argv0)
 Sometimes we use the binary name to determine which specific test to run.
 
struct GNUNET_STRINGS_IPv4NetworkPolicyGNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
 Parse an IPv4 network policy.
 
struct GNUNET_STRINGS_IPv6NetworkPolicyGNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
 Parse an IPv6 network policy.
 

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 542 of file gnunet_strings_lib.h.

543{
547 GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
548
553
557 GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
558
563};
@ 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 235 of file strings.c.

237{
238 static const struct ConversionTable table[] =
239 { { "B", 1 },
240 { "KiB", 1024 },
241 { "kB", 1000 },
242 { "MiB", 1024 * 1024 },
243 { "MB", 1000 * 1000 },
244 { "GiB", 1024 * 1024 * 1024 },
245 { "GB", 1000 * 1000 * 1000 },
246 { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
247 { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
248 { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
249 { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
250 { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
251 { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
252 { NULL, 0 } };
253
254 return convert_with_table (fancy_size, table, size);
255}
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:180
Unit conversion table entry for 'convert_with_table'.
Definition strings.c:155

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 259 of file strings.c.

261{
262 static const struct ConversionTable table[] =
263 { { "us", 1 },
264 { "ms", 1000 },
265 { "s", 1000 * 1000LL },
266 { "second", 1000 * 1000LL },
267 { "seconds", 1000 * 1000LL },
268 { "\"", 1000 * 1000LL },
269 { "m", 60 * 1000 * 1000LL },
270 { "min", 60 * 1000 * 1000LL },
271 { "minute", 60 * 1000 * 1000LL },
272 { "minutes", 60 * 1000 * 1000LL },
273 { "'", 60 * 1000 * 1000LL },
274 { "h", 60 * 60 * 1000 * 1000LL },
275 { "hour", 60 * 60 * 1000 * 1000LL },
276 { "hours", 60 * 60 * 1000 * 1000LL },
277 { "d", 24 * 60 * 60 * 1000LL * 1000LL },
278 { "day", 24 * 60 * 60 * 1000LL * 1000LL },
279 { "days", 24 * 60 * 60 * 1000LL * 1000LL },
280 { "w", 7 * 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 { "y", 31536000000000LL /* year */ },
286 { "a", 31536000000000LL /* year */ },
287 { NULL, 0 } };
288 int ret;
289 unsigned long long val;
290
291 if (0 == strcasecmp ("forever", fancy_time))
292 {
294 return GNUNET_OK;
295 }
296 ret = convert_with_table (fancy_time, table, &val);
297 rtime->rel_value_us = (uint64_t) val;
298 return ret;
299}
static int ret
Final status code.
Definition gnunet-arm.c:93
@ 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(), parse_expiration(), run(), 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 105 of file strings.c.

106{
107 const char *unit = /* size unit */ "b";
108 char *ret;
109
110 if (size > 5 * 1024)
111 {
112 size = size / 1024;
113 unit = "KiB";
114 if (size > 5 * 1024)
115 {
116 size = size / 1024;
117 unit = "MiB";
118 if (size > 5 * 1024)
119 {
120 size = size / 1024;
121 unit = "GiB";
122 if (size > 5 * 1024)
123 {
124 size = size / 1024;
125 unit = "TiB";
126 }
127 }
128 }
129 }
130 ret = GNUNET_malloc (32);
131 GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
132 return ret;
133}
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 368 of file strings.c.

372{
373 char *ret;
374 uint8_t *u8_string;
375 char *encoded_string;
376 size_t u8_string_length;
377 size_t encoded_string_length;
378
379 u8_string = u8_conv_from_encoding (input_charset,
380 iconveh_error,
381 input,
382 len,
383 NULL,
384 NULL,
385 &u8_string_length);
386 if (NULL == u8_string)
387 {
388 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
389 goto fail;
390 }
391 if (0 == strcmp (output_charset, "UTF-8"))
392 {
393 ret = GNUNET_malloc (u8_string_length + 1);
394 GNUNET_memcpy (ret, u8_string, u8_string_length);
395 ret[u8_string_length] = '\0';
396 free (u8_string);
397 return ret;
398 }
399 encoded_string = u8_conv_to_encoding (output_charset,
400 iconveh_error,
401 u8_string,
402 u8_string_length,
403 NULL,
404 NULL,
405 &encoded_string_length);
406 free (u8_string);
407 if (NULL == encoded_string)
408 {
409 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
410 goto fail;
411 }
412 ret = GNUNET_malloc (encoded_string_length + 1);
413 GNUNET_memcpy (ret, encoded_string, encoded_string_length);
414 ret[encoded_string_length] = '\0';
415 free (encoded_string);
416 return ret;
417fail:
419 _ ("Character sets requested were `%s'->`%s'\n"),
420 "UTF-8",
421 output_charset);
422 ret = GNUNET_malloc (len + 1);
423 GNUNET_memcpy (ret, input, len);
424 ret[len] = '\0';
425 return ret;
426}
#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:179
#define LOG(kind,...)
Definition strings.c:36
#define LOG_STRERROR(kind, syscall)
Definition strings.c:38

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 430 of file strings.c.

433{
434 return GNUNET_STRINGS_conv (input,
435 len,
436 charset,
437 "UTF-8");
438}
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:368

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 454 of file strings.c.

455{
456 uint8_t *tmp;
457 size_t len;
458 char *output;
459 tmp = u8_normalize (UNINORM_NFC,
460 (uint8_t *) input,
461 strlen ((char*) input),
462 NULL,
463 &len);
464 if (NULL == tmp)
465 return NULL;
466 output = GNUNET_malloc (len + 1);
467 GNUNET_memcpy (output, tmp, len);
468 output[len] = '\0';
469 free (tmp);
470 return output;
471}

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 442 of file strings.c.

445{
446 return GNUNET_STRINGS_conv (input,
447 len,
448 "UTF-8",
449 charset);
450}

References GNUNET_STRINGS_conv().

Here is the call graph for this function:

◆ GNUNET_STRINGS_utf8_tolower()

char * GNUNET_STRINGS_utf8_tolower ( const char *  input)

Convert the utf-8 input string to lower case.

Parameters
inputinput string
Returns
NULL on error

Definition at line 475 of file strings.c.

476{
477 uint8_t *tmp_in;
478 size_t len;
479 char *ret;
480
481 tmp_in = u8_tolower ((uint8_t *) input,
482 strlen ((char *) input),
483 NULL,
484 UNINORM_NFD,
485 NULL,
486 &len);
487
488 if (NULL == tmp_in)
489 return NULL;
490 ret = GNUNET_malloc (len + 1);
491 memcpy (ret,
492 tmp_in,
493 len);
494 free (tmp_in);
495 return ret;
496}

References GNUNET_malloc, and ret.

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()

char * GNUNET_STRINGS_utf8_toupper ( const char *  input)

Convert the utf-8 input string to upper case.

Parameters
inputinput string
Returns
NULL on error

Definition at line 500 of file strings.c.

501{
502 uint8_t *tmp_in;
503 size_t len;
504 char *ret;
505
506 tmp_in = u8_toupper ((uint8_t *) input,
507 strlen ((char *) input),
508 NULL,
509 UNINORM_NFD,
510 NULL,
511 &len);
512
513 if (NULL == tmp_in)
514 return NULL;
515 ret = GNUNET_malloc (len + 1);
516 memcpy (ret,
517 tmp_in,
518 len);
519 free (tmp_in);
520 return ret;
521}

References GNUNET_malloc, and ret.

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 525 of file strings.c.

526{
527 char *buffer;
528 size_t len;
529 char *fm;
530 const char *fil_ptr;
531
532 if (NULL == fil)
533 return NULL;
534
535 if (fil[0] == DIR_SEPARATOR)
536 /* absolute path, just copy */
537 return GNUNET_strdup (fil);
538 if (fil[0] == '~')
539 {
540 fm = getenv ("HOME");
541 if (fm == NULL)
542 {
544 _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
545 return NULL;
546 }
547 fm = GNUNET_strdup (fm);
548 /* do not copy '~' */
549 fil_ptr = fil + 1;
550
551 /* skip over dir separator to be consistent */
552 if (fil_ptr[0] == DIR_SEPARATOR)
553 fil_ptr++;
554 }
555 else
556 {
557 /* relative path */
558 fil_ptr = fil;
559 len = 512;
560 fm = NULL;
561 while (1)
562 {
563 buffer = GNUNET_malloc (len);
564 if (NULL != getcwd (buffer,
565 len))
566 {
567 fm = buffer;
568 break;
569 }
570 if ( (errno == ERANGE) &&
571 (len < 1024 * 1024 * 4) )
572 {
573 len *= 2;
574 GNUNET_free (buffer);
575 continue;
576 }
577 GNUNET_free (buffer);
578 break;
579 }
580 if (NULL == fm)
581 {
583 "getcwd");
584 buffer = getenv ("PWD"); /* alternative */
585 if (buffer != NULL)
586 fm = GNUNET_strdup (buffer);
587 }
588 if (NULL == fm)
589 fm = GNUNET_strdup ("./"); /* give up */
590 }
591 GNUNET_asprintf (&buffer,
592 "%s%s%s",
593 fm,
594 (fm[strlen (fm) - 1] == DIR_SEPARATOR)
595 ? ""
597 fil_ptr);
598 GNUNET_free (fm);
599 return buffer;
600}
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 GNUNET_free(ptr)
Wrapper around free.
#define DIR_SEPARATOR
Definition platform.h:166
#define DIR_SEPARATOR_STR
Definition platform.h:167

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 43 of file strings.c.

46{
47 size_t needed;
48 va_list ap;
49
50 needed = 0;
51 va_start (ap, count);
52 while (count > 0)
53 {
54 const char *s = va_arg (ap, const char *);
55 size_t slen = strlen (s) + 1;
56
57 GNUNET_assert (slen <= size - needed);
58 if (NULL != buffer)
59 GNUNET_memcpy (&buffer[needed],
60 s,
61 slen);
62 needed += slen;
63 count--;
64 }
65 va_end (ap);
66 return needed;
67}
#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 71 of file strings.c.

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

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 688 of file strings.c.

689{
690 const char *short_fn = filename;
691 const char *ss;
692
693 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
694 short_fn = 1 + ss;
695 return short_fn;
696}
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 757 of file strings.c.

761{
765 static const char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
766 unsigned int wpos;
767 unsigned int rpos;
768 unsigned int bits;
769 unsigned int vbit;
770 const unsigned char *udata;
771
772 GNUNET_assert (size < SIZE_MAX / 8 - 4);
773 udata = data;
774 if (out_size < (size * 8 + 4) / 5)
775 {
776 GNUNET_break (0);
777 return NULL;
778 }
779 vbit = 0;
780 wpos = 0;
781 rpos = 0;
782 bits = 0;
783 while ((rpos < size) || (vbit > 0))
784 {
785 if ((rpos < size) && (vbit < 5))
786 {
787 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
788 vbit += 8;
789 }
790 if (vbit < 5)
791 {
792 bits <<= (5 - vbit); /* zero-padding */
793 GNUNET_assert (vbit == ((size * 8) % 5));
794 vbit = 5;
795 }
796 if (wpos >= out_size)
797 {
798 GNUNET_break (0);
799 return NULL;
800 }
801 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
802 vbit -= 5;
803 }
804 GNUNET_assert (0 == vbit);
805 if (wpos < out_size)
806 out[wpos] = '\0';
807 return &out[wpos];
808}
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:209

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

Referenced by auth_key_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 812 of file strings.c.

813{
814 char *str_buf;
815 size_t len = size * 8;
816 char *end;
817
818 if (len % 5 > 0)
819 len += 5 - len % 5;
820 len /= 5;
821 str_buf = GNUNET_malloc (len + 1);
823 size,
824 str_buf,
825 len);
826 if (NULL == end)
827 {
828 GNUNET_free (str_buf);
829 return NULL;
830 }
831 *end = '\0';
832 return str_buf;
833}
static int end
Set if we are to shutdown all services (including ARM).
Definition gnunet-arm.c:33
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:757

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_blindable_private_key_to_string(), GNUNET_CRYPTO_blindable_public_key_to_string(), GNUNET_HELLO_build_url(), GNUNET_HELLO_parser_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(), run(), rvk_move_attr_cb(), save_member(), save_member_session(), save_operation(), setup_job_headers(), 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 837 of file strings.c.

841{
842 size_t rpos;
843 size_t wpos;
844 unsigned int bits;
845 unsigned int vbit;
846 int ret;
847 int shift;
848 unsigned char *uout;
849 size_t encoded_len;
850
851 if (0 == enclen)
852 {
853 if (0 == out_size)
854 return GNUNET_OK;
855 return GNUNET_SYSERR;
856 }
857 GNUNET_assert (out_size < SIZE_MAX / 8);
858 encoded_len = out_size * 8;
859 uout = out;
860 wpos = out_size;
861 rpos = enclen;
862 if ((encoded_len % 5) > 0)
863 {
864 vbit = encoded_len % 5; /* padding! */
865 shift = 5 - vbit;
866 bits = (ret = getValue__ (enc[--rpos])) >> shift;
867 }
868 else
869 {
870 vbit = 5;
871 shift = 0;
872 bits = (ret = getValue__ (enc[--rpos]));
873 }
874 if ((encoded_len + shift) / 5 != enclen)
875 return GNUNET_SYSERR;
876 if (-1 == ret)
877 return GNUNET_SYSERR;
878 while (wpos > 0)
879 {
880 if (0 == rpos)
881 {
882 GNUNET_break (0);
883 return GNUNET_SYSERR;
884 }
885 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
886 if (-1 == ret)
887 return GNUNET_SYSERR;
888 vbit += 5;
889 if (vbit >= 8)
890 {
891 uout[--wpos] = (unsigned char) bits;
892 bits >>= 8;
893 vbit -= 8;
894 }
895 }
896 if ((0 != rpos) || (0 != vbit))
897 return GNUNET_SYSERR;
898 return GNUNET_OK;
899}
static OpusEncoder * enc
OPUS encoder.
@ GNUNET_SYSERR
static unsigned int getValue__(unsigned char a)
Get the decoded value corresponding to a character according to Crockford Base32 encoding.
Definition strings.c:707

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

Referenced by channel_to_sh(), check_authorization(), consume_fail(), conversation_string_to_value(), delete_attribute_cont(), delete_credential_cont(), ego_create(), expect_data_dynamic(), expect_data_fixed(), GNUNET_CONFIGURATION_get_data(), GNUNET_CRYPTO_blindable_private_key_from_string(), GNUNET_CRYPTO_blindable_public_key_from_string(), 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_HELLO_parser_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(), 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 903 of file strings.c.

907{
908 size_t size;
909 void *data;
910 int res;
911
912 size = (enclen * 5) / 8;
914 {
915 GNUNET_break_op (0);
916 return GNUNET_SYSERR;
917 }
920 enclen,
921 data,
922 size);
923 if ( (0 < size) &&
924 (GNUNET_OK != res) )
925 {
926 size--;
928 enclen,
929 data,
930 size);
931 }
932 if (GNUNET_OK != res)
933 {
934 GNUNET_break_op (0);
936 return GNUNET_SYSERR;
937 }
938 *out = data;
939 *out_size = size;
940 return GNUNET_OK;
941}
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:837

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 1623 of file strings.c.

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

References cvt, data, FILLCHAR, GNUNET_assert, GNUNET_malloc, ret, and SIZE_MAX.

Referenced by dns_value_to_string(), GNUNET_ABD_delegate_to_string(), GNUNET_JSON_from_data64(), GNUNET_STRINGS_base64url_encode(), OIDC_access_token_new(), pabc_create_presentation(), pils_sign_addr_cb(), store_and_free_entries(), 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 1887 of file strings.c.

1890{
1891 struct GNUNET_Buffer buf = { 0 };
1892 const uint8_t *i8 = (uint8_t *) data;
1893 const uint8_t *end = (uint8_t *) (data + len);
1894
1895 while (end != i8)
1896 {
1897 if (0 == *i8)
1898 {
1899 /* invalid UTF-8 (or bad @a len): fail */
1900 GNUNET_break (0);
1901 GNUNET_buffer_clear (&buf);
1902 return 0;
1903 }
1904 if (0 == (0x80 & *i8))
1905 {
1906 /* traditional ASCII */
1907 if (isalnum (*i8) ||
1908 (*i8 == '-') ||
1909 (*i8 == '_') ||
1910 (*i8 == '.') ||
1911 (*i8 == '~') )
1912 GNUNET_buffer_write (&buf,
1913 (const char*) i8,
1914 1);
1915 else if (*i8 == ' ')
1916 GNUNET_buffer_write (&buf,
1917 "+",
1918 1);
1919 else
1921 "%%%X%X",
1922 *i8 >> 4,
1923 *i8 & 15);
1924 i8++;
1925 continue;
1926 }
1927 if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
1928 {
1929 /* 2-byte value, percent-encode */
1931 "%%%X%X",
1932 *i8 >> 4,
1933 *i8 & 15);
1934 i8++;
1935 if ( (end == i8) ||
1936 (0 == *i8) )
1937 {
1938 /* invalid UTF-8 (or bad @a len): fail */
1939 GNUNET_break (0);
1940 GNUNET_buffer_clear (&buf);
1941 return 0;
1942 }
1944 "%%%X%X",
1945 *i8 >> 4,
1946 *i8 & 15);
1947 i8++;
1948 continue;
1949 }
1950 if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
1951 {
1952 /* 3-byte value, percent-encode */
1953 for (unsigned int i = 0; i<3; i++)
1954 {
1955 if ( (end == i8) ||
1956 (0 == *i8) )
1957 {
1958 /* invalid UTF-8 (or bad @a len): fail */
1959 GNUNET_break (0);
1960 GNUNET_buffer_clear (&buf);
1961 return 0;
1962 }
1964 "%%%X%X",
1965 *i8 >> 4,
1966 *i8 & 15);
1967 i8++;
1968 }
1969 continue;
1970 }
1971 if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
1972 {
1973 /* 4-byte value, percent-encode */
1974 for (unsigned int i = 0; i<4; i++)
1975 {
1976 if ( (end == i8) ||
1977 (0 == *i8) )
1978 {
1979 /* invalid UTF-8 (or bad @a len): fail */
1980 GNUNET_break (0);
1981 GNUNET_buffer_clear (&buf);
1982 return 0;
1983 }
1985 "%%%X%X",
1986 *i8 >> 4,
1987 *i8 & 15);
1988 i8++;
1989 }
1990 continue;
1991 }
1992 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
1993 + 0x04) & *i8))
1994 {
1995 /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1996 for (unsigned int i = 0; i<5; i++)
1997 {
1998 if ( (end == i8) ||
1999 (0 == *i8) )
2000 {
2001 /* invalid UTF-8 (or bad @a len): fail */
2002 GNUNET_break (0);
2003 GNUNET_buffer_clear (&buf);
2004 return 0;
2005 }
2007 "%%%X%X",
2008 *i8 >> 4,
2009 *i8 & 15);
2010 i8++;
2011 }
2012 continue;
2013 }
2014 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
2015 + 0x08 + 0x04 + 0x02)
2016 & *i8))
2017 {
2018 /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
2019 for (unsigned int i = 0; i<6; i++)
2020 {
2021 if ( (end == i8) ||
2022 (0 == *i8) )
2023 {
2024 /* invalid UTF-8 (or bad @a len): fail */
2025 GNUNET_break (0);
2026 GNUNET_buffer_clear (&buf);
2027 return 0;
2028 }
2030 "%%%X%X",
2031 *i8 >> 4,
2032 *i8 & 15);
2033 i8++;
2034 }
2035 continue;
2036 }
2037 /* really, really invalid UTF-8: fail */
2038 GNUNET_break (0);
2039 GNUNET_buffer_clear (&buf);
2040 return 0;
2041 }
2042 *out = GNUNET_buffer_reap_str (&buf);
2043 return strlen (*out);
2044}
char * GNUNET_buffer_reap_str(struct GNUNET_Buffer *buf)
Clear the buffer and return the string it contained.
Definition buffer.c:89
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:63
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition buffer.c:123
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_build_url(), GNUNET_HELLO_parser_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 1672 of file strings.c.

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

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 1725 of file strings.c.

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

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(), database_setup(), dns_string_to_value(), GNUNET_ABD_delegate_from_string(), GNUNET_STRINGS_base64url_decode(), OIDC_access_token_parse(), parse_credentials_basic_auth(), and parse_fixed64_data().

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 1783 of file strings.c.

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

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 1832 of file strings.c.

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

References data, GNUNET_break_op, GNUNET_free, and GNUNET_malloc.

Referenced by get_url_parameter_copy(), and GNUNET_HELLO_parser_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 945 of file strings.c.

948{
949 size_t len;
950 size_t i;
951 int end;
952 int pp_state = 0;
953 const char *post_scheme_part = NULL;
954
955 len = strlen (path);
956 for (end = 0, i = 0; ! end && i < len; i++)
957 {
958 switch (pp_state)
959 {
960 case 0:
961 if ((path[i] == ':') && (i > 0))
962 {
963 pp_state += 1;
964 continue;
965 }
966 if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
967 ((path[i] >= 'a') && (path[i] <= 'z') ) ||
968 ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
969 (path[i] == '-') || (path[i] == '.')))
970 end = 1;
971 break;
972
973 case 1:
974 case 2:
975 if (path[i] == '/')
976 {
977 pp_state += 1;
978 continue;
979 }
980 end = 1;
981 break;
982
983 case 3:
984 post_scheme_part = &path[i];
985 end = 1;
986 break;
987
988 default:
989 end = 1;
990 }
991 }
992 if (post_scheme_part == NULL)
993 return GNUNET_NO;
994 if (scheme_part)
995 {
996 *scheme_part = GNUNET_strndup (path,
997 post_scheme_part - path);
998 }
999 if (path_part)
1000 *path_part = post_scheme_part;
1001 return GNUNET_YES;
1002}
@ 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 1006 of file strings.c.

1010{
1011 const char *post_scheme_path;
1012 int is_uri;
1013 char *uri;
1014 /* consider POSIX paths to be absolute too, even on W32,
1015 * as plibc expansion will fix them for us.
1016 */
1017 if (filename[0] == '/')
1018 return GNUNET_YES;
1019 if (can_be_uri)
1020 {
1021 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1022 if (r_is_uri)
1023 *r_is_uri = is_uri;
1024 if (is_uri)
1025 {
1026 if (r_uri_scheme)
1027 *r_uri_scheme = uri;
1028 else
1029 GNUNET_free (uri);
1030
1031 return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1032 GNUNET_NO,
1033 NULL,
1034 NULL);
1035 }
1036 }
1037 else
1038 {
1039 if (r_is_uri)
1040 *r_is_uri = GNUNET_NO;
1041 }
1042
1043 return GNUNET_NO;
1044}
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:1006
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:945

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 1048 of file strings.c.

1050{
1051 struct stat st;
1052
1053 if ((NULL == filename) || (filename[0] == '\0'))
1054 return GNUNET_SYSERR;
1055 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1057 return GNUNET_NO;
1058 if (0 != (checks
1061 {
1062 if (0 != lstat (filename, &st))
1063 {
1064 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1065 return GNUNET_NO;
1066 else
1067 return GNUNET_SYSERR;
1068 }
1069 }
1070 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1071 if (! S_ISDIR (st.st_mode))
1072 return GNUNET_NO;
1073 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1074 if (! S_ISLNK (st.st_mode))
1075 return GNUNET_NO;
1076 return GNUNET_YES;
1077}
#define S_ISLNK(m)
Definition disk.c:61
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 1081 of file strings.c.

1084{
1085 if (addrlen < 6)
1086 return GNUNET_SYSERR;
1087 if (addrlen > 512)
1088 return GNUNET_SYSERR; /* sanity check to protect zbuf allocation,
1089 actual limit is not precise */
1090 {
1091 char zbuf[addrlen + 1];
1092 int ret;
1093 char *port_colon;
1094 unsigned int port;
1095 char dummy[2];
1096
1097 GNUNET_memcpy (zbuf, zt_addr, addrlen);
1098 if ('[' != zbuf[0])
1099 {
1101 _ ("IPv6 address did not start with `['\n"));
1102 return GNUNET_SYSERR;
1103 }
1104 zbuf[addrlen] = '\0';
1105 port_colon = strrchr (zbuf, ':');
1106 if (NULL == port_colon)
1107 {
1109 _ ("IPv6 address did contain ':' to separate port number\n"));
1110 return GNUNET_SYSERR;
1111 }
1112 if (']' != *(port_colon - 1))
1113 {
1114 GNUNET_log (
1116 _ (
1117 "IPv6 address did contain ']' before ':' to separate port number\n"));
1118 return GNUNET_SYSERR;
1119 }
1120 ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1121 if ((1 != ret) || (port > 65535))
1122 {
1123 GNUNET_log (
1125 _ (
1126 "IPv6 address did contain a valid port number after the last ':'\n"));
1127 return GNUNET_SYSERR;
1128 }
1129 *(port_colon - 1) = '\0';
1130 memset (r_buf, 0, sizeof(struct sockaddr_in6));
1131 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1132 if (ret <= 0)
1133 {
1135 _ ("Invalid IPv6 address `%s': %s\n"),
1136 &zbuf[1],
1137 strerror (errno));
1138 return GNUNET_SYSERR;
1139 }
1140 r_buf->sin6_port = htons (port);
1141 r_buf->sin6_family = AF_INET6;
1142#if HAVE_SOCKADDR_IN_SIN_LEN
1143 r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1144#endif
1145 return GNUNET_OK;
1146 }
1147}
static uint16_t port
Port number.
Definition gnunet-bcd.c:146
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 1151 of file strings.c.

1154{
1155 unsigned int temps[4];
1156 unsigned int port;
1157 unsigned int cnt;
1158 char dummy[2];
1159
1160 if (addrlen < 9)
1161 return GNUNET_SYSERR;
1162 cnt = sscanf (zt_addr,
1163 "%u.%u.%u.%u:%u%1s",
1164 &temps[0],
1165 &temps[1],
1166 &temps[2],
1167 &temps[3],
1168 &port,
1169 dummy);
1170 if (5 != cnt)
1171 return GNUNET_SYSERR;
1172 for (cnt = 0; cnt < 4; cnt++)
1173 if (temps[cnt] > 0xFF)
1174 return GNUNET_SYSERR;
1175 if (port > 65535)
1176 return GNUNET_SYSERR;
1177 r_buf->sin_family = AF_INET;
1178 r_buf->sin_port = htons (port);
1179 r_buf->sin_addr.s_addr =
1180 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1181#if HAVE_SOCKADDR_IN_SIN_LEN
1182 r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1183#endif
1184 return GNUNET_OK;
1185}

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 1204 of file strings.c.

1207{
1208 *af = AF_UNSPEC;
1209 if ('[' == *addr)
1210 {
1211 /* IPv6 */
1212 *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1213 if (GNUNET_OK !=
1215 strlen (addr),
1216 (struct sockaddr_in6 *) *sa))
1217 {
1218 GNUNET_free (*sa);
1219 *sa = NULL;
1220 return 0;
1221 }
1222 *af = AF_INET6;
1223 return sizeof(struct sockaddr_in6);
1224 }
1225 else
1226 {
1227 /* IPv4 */
1228 *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1229 if (GNUNET_OK !=
1231 strlen (addr),
1232 (struct sockaddr_in *) *sa))
1233 {
1234 GNUNET_free (*sa);
1235 *sa = NULL;
1236 return 0;
1237 }
1238 *af = AF_INET;
1239 return sizeof(struct sockaddr_in);
1240 }
1241}
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:1081
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:1151

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 1189 of file strings.c.

1192{
1193 if (addr[0] == '[')
1194 return GNUNET_STRINGS_to_address_ipv6 (addr,
1195 addrlen,
1196 (struct sockaddr_in6 *) r_buf);
1197 return GNUNET_STRINGS_to_address_ipv4 (addr,
1198 addrlen,
1199 (struct sockaddr_in *) r_buf);
1200}

References GNUNET_STRINGS_to_address_ipv4(), and GNUNET_STRINGS_to_address_ipv6().

Here is the call 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 137 of file strings.c.

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

References GNUNET_assert, and strnlen().

Referenced by add_unixpath(), add_unixpath(), create_keys(), GCP_2s(), GNUNET_i2s(), GNUNET_i2s2(), mylog(), regex_split(), 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 2061 of file strings.c.

2062{
2063 const char *ret;
2064 const char *dot;
2065
2066 ret = strrchr (argv0, '_');
2067 if (NULL == ret)
2068 return NULL;
2069 ret++; /* skip underscore */
2070 dot = strchr (ret,
2071 '.');
2072 if (NULL != dot)
2073 return GNUNET_strndup (ret,
2074 dot - ret);
2075 return GNUNET_strdup (ret);
2076}

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 syntax is flawed

Definition at line 1299 of file strings.c.

1300{
1301 size_t count;
1302 size_t len;
1303 size_t pos;
1304 unsigned int temps[8];
1306 char *routeList;
1307
1308 if (NULL == routeListX)
1309 return NULL;
1310 len = strlen (routeListX);
1311 if (0 == len)
1312 return NULL;
1313 routeList = GNUNET_strdup (routeListX);
1314 count = 0;
1315 for (size_t i = 0; i < len; i++)
1316 if (routeList[i] == ';')
1317 count++;
1318 GNUNET_assert (count < SIZE_MAX);
1319 result = GNUNET_new_array (count + 1,
1321 pos = 0;
1322 for (size_t i = 0; i < count; i++)
1323 {
1324 size_t colon;
1325 size_t end;
1326 char dummy;
1327
1328 for (colon = pos; ':' != routeList[colon]; colon++)
1329 if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1330 break;
1331 for (end = colon; ';' != routeList[end]; end++)
1332 if ('\0' == routeList[end])
1333 break;
1334 if ('\0' == routeList[end])
1335 break;
1336 routeList[end] = '\0';
1337 if (':' == routeList[colon])
1338 {
1339 routeList[colon] = '\0';
1340 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1341 break;
1342 }
1343 if (8 ==
1344 sscanf (&routeList[pos],
1345 "%u.%u.%u.%u/%u.%u.%u.%u%c",
1346 &temps[0],
1347 &temps[1],
1348 &temps[2],
1349 &temps[3],
1350 &temps[4],
1351 &temps[5],
1352 &temps[6],
1353 &temps[7],
1354 &dummy))
1355 {
1356 for (unsigned int j = 0; j < 8; j++)
1357 if (temps[j] > 0xFF)
1358 {
1360 _ ("Invalid format for IP: `%s'\n"),
1361 &routeList[pos]);
1363 GNUNET_free (routeList);
1364 return NULL;
1365 }
1366 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1367 + (temps[2] << 8) + temps[3]);
1368 result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1369 + (temps[6] << 8) + temps[7]);
1370 pos = end + 1;
1371 continue;
1372 }
1373
1374 /* try second notation */
1375 {
1376 unsigned int slash;
1377
1378 if (5 ==
1379 sscanf (&routeList[pos],
1380 "%u.%u.%u.%u/%u%c",
1381 &temps[0],
1382 &temps[1],
1383 &temps[2],
1384 &temps[3],
1385 &slash,
1386 &dummy))
1387 {
1388 for (unsigned int j = 0; j < 4; j++)
1389 if (temps[j] > 0xFF)
1390 {
1392 _ ("Invalid format for IP: `%s'\n"),
1393 &routeList[pos]);
1395 GNUNET_free (routeList);
1396 return NULL;
1397 }
1398 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1399 + (temps[2] << 8) + temps[3]);
1400 if (slash <= 32)
1401 {
1402 result[i].netmask.s_addr = 0;
1403 while (slash > 0)
1404 {
1405 result[i].netmask.s_addr =
1406 (result[i].netmask.s_addr >> 1) + 0x80000000;
1407 slash--;
1408 }
1409 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1410 pos = end + 1;
1411 continue;
1412 }
1413 else
1414 {
1416 _ (
1417 "Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1418 slash);
1420 GNUNET_free (routeList);
1421 return NULL; /* error */
1422 }
1423 }
1424 }
1425
1426 /* try third notation */
1427 if (4 ==
1428 sscanf (&routeList[pos],
1429 "%u.%u.%u.%u%c",
1430 &temps[0],
1431 &temps[1],
1432 &temps[2],
1433 &temps[3],
1434 &dummy))
1435 {
1436 for (unsigned int j = 0; j < 4; j++)
1437 if (temps[j] > 0xFF)
1438 {
1440 _ ("Invalid format for IP: `%s'\n"),
1441 &routeList[pos]);
1443 GNUNET_free (routeList);
1444 return NULL;
1445 }
1446 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1447 + (temps[2] << 8) + temps[3]);
1448 result[i].netmask.s_addr = htonl (0xffffffff); /* yeah, the htonl is useless */
1449 pos = end + 1;
1450 continue;
1451 }
1453 _ ("Invalid format for IP: `%s'\n"),
1454 &routeList[pos]);
1456 GNUNET_free (routeList);
1457 return NULL; /* error */
1458 }
1459 if (pos < strlen (routeList))
1460 {
1462 _ ("Invalid format: `%s'\n"),
1463 &routeListX[pos]);
1465 GNUNET_free (routeList);
1466 return NULL; /* oops */
1467 }
1468 GNUNET_free (routeList);
1469 return result; /* ok */
1470}
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:1254
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_REGEX_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 syntax is flawed

Definition at line 1474 of file strings.c.

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

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