GNUnet 0.22.2
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...
 
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 547 of file gnunet_strings_lib.h.

548{
552 GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
553
558
562 GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
563
568};
@ 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 { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
281 { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
282 { "year", 31536000000000LL /* year */ },
283 { "years", 31536000000000LL /* year */ },
284 { "a", 31536000000000LL /* year */ },
285 { NULL, 0 } };
286 int ret;
287 unsigned long long val;
288
289 if (0 == strcasecmp ("forever", fancy_time))
290 {
292 return GNUNET_OK;
293 }
294 ret = convert_with_table (fancy_time, table, &val);
295 rtime->rel_value_us = (uint64_t) val;
296 return ret;
297}
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(), 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 352 of file strings.c.

356{
357 char *ret;
358 uint8_t *u8_string;
359 char *encoded_string;
360 size_t u8_string_length;
361 size_t encoded_string_length;
362
363 u8_string = u8_conv_from_encoding (input_charset,
364 iconveh_error,
365 input,
366 len,
367 NULL,
368 NULL,
369 &u8_string_length);
370 if (NULL == u8_string)
371 {
372 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
373 goto fail;
374 }
375 if (0 == strcmp (output_charset, "UTF-8"))
376 {
377 ret = GNUNET_malloc (u8_string_length + 1);
378 GNUNET_memcpy (ret, u8_string, u8_string_length);
379 ret[u8_string_length] = '\0';
380 free (u8_string);
381 return ret;
382 }
383 encoded_string = u8_conv_to_encoding (output_charset,
384 iconveh_error,
385 u8_string,
386 u8_string_length,
387 NULL,
388 NULL,
389 &encoded_string_length);
390 free (u8_string);
391 if (NULL == encoded_string)
392 {
393 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
394 goto fail;
395 }
396 ret = GNUNET_malloc (encoded_string_length + 1);
397 GNUNET_memcpy (ret, encoded_string, encoded_string_length);
398 ret[encoded_string_length] = '\0';
399 free (encoded_string);
400 return ret;
401fail:
403 _ ("Character sets requested were `%s'->`%s'\n"),
404 "UTF-8",
405 output_charset);
406 ret = GNUNET_malloc (len + 1);
407 GNUNET_memcpy (ret, input, len);
408 ret[len] = '\0';
409 return ret;
410}
#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 414 of file strings.c.

417{
418 return GNUNET_STRINGS_conv (input,
419 len,
420 charset,
421 "UTF-8");
422}
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:352

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

439{
440 uint8_t *tmp;
441 size_t len;
442 char *output;
443 tmp = u8_normalize (UNINORM_NFC,
444 (uint8_t *) input,
445 strlen ((char*) input),
446 NULL,
447 &len);
448 if (NULL == tmp)
449 return NULL;
450 output = GNUNET_malloc (len + 1);
451 GNUNET_memcpy (output, tmp, len);
452 output[len] = '\0';
453 free (tmp);
454 return output;
455}

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

429{
430 return GNUNET_STRINGS_conv (input,
431 len,
432 "UTF-8",
433 charset);
434}

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

461{
462 uint8_t *tmp_in;
463 size_t len;
464
465 tmp_in = u8_tolower ((uint8_t *) input,
466 strlen ((char *) input),
467 NULL,
468 UNINORM_NFD,
469 NULL,
470 &len);
471 if (NULL == tmp_in)
472 return GNUNET_SYSERR;
473 GNUNET_memcpy (output, tmp_in, len);
474 output[len] = '\0';
475 GNUNET_free (tmp_in);
476 return GNUNET_OK;
477}
@ 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 481 of file strings.c.

483{
484 uint8_t *tmp_in;
485 size_t len;
486
487 tmp_in = u8_toupper ((uint8_t *) input,
488 strlen ((char *) input),
489 NULL,
490 UNINORM_NFD,
491 NULL,
492 &len);
493 if (NULL == tmp_in)
494 return GNUNET_SYSERR;
495 /* 0-terminator does not fit */
496 GNUNET_memcpy (output, tmp_in, len);
497 output[len] = '\0';
498 GNUNET_free (tmp_in);
499 return GNUNET_OK;
500}

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

505{
506 char *buffer;
507 size_t len;
508 char *fm;
509 const char *fil_ptr;
510
511 if (fil == NULL)
512 return NULL;
513
514 if (fil[0] == DIR_SEPARATOR)
515 /* absolute path, just copy */
516 return GNUNET_strdup (fil);
517 if (fil[0] == '~')
518 {
519 fm = getenv ("HOME");
520 if (fm == NULL)
521 {
523 _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
524 return NULL;
525 }
526 fm = GNUNET_strdup (fm);
527 /* do not copy '~' */
528 fil_ptr = fil + 1;
529
530 /* skip over dir separator to be consistent */
531 if (fil_ptr[0] == DIR_SEPARATOR)
532 fil_ptr++;
533 }
534 else
535 {
536 /* relative path */
537 fil_ptr = fil;
538 len = 512;
539 fm = NULL;
540 while (1)
541 {
542 buffer = GNUNET_malloc (len);
543 if (getcwd (buffer, len) != NULL)
544 {
545 fm = buffer;
546 break;
547 }
548 if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
549 {
550 len *= 2;
551 GNUNET_free (buffer);
552 continue;
553 }
554 GNUNET_free (buffer);
555 break;
556 }
557 if (fm == NULL)
558 {
560 buffer = getenv ("PWD"); /* alternative */
561 if (buffer != NULL)
562 fm = GNUNET_strdup (buffer);
563 }
564 if (fm == NULL)
565 fm = GNUNET_strdup ("./"); /* give up */
566 }
567 GNUNET_asprintf (&buffer,
568 "%s%s%s",
569 fm,
570 (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
572 fil_ptr);
573 GNUNET_free (fm);
574 return buffer;
575}
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: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 663 of file strings.c.

664{
665 const char *short_fn = filename;
666 const char *ss;
667
668 while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
669 short_fn = 1 + ss;
670 return short_fn;
671}
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 732 of file strings.c.

736{
740 static const char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
741 unsigned int wpos;
742 unsigned int rpos;
743 unsigned int bits;
744 unsigned int vbit;
745 const unsigned char *udata;
746
747 GNUNET_assert (size < SIZE_MAX / 8 - 4);
748 udata = data;
749 if (out_size < (size * 8 + 4) / 5)
750 {
751 GNUNET_break (0);
752 return NULL;
753 }
754 vbit = 0;
755 wpos = 0;
756 rpos = 0;
757 bits = 0;
758 while ((rpos < size) || (vbit > 0))
759 {
760 if ((rpos < size) && (vbit < 5))
761 {
762 bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
763 vbit += 8;
764 }
765 if (vbit < 5)
766 {
767 bits <<= (5 - vbit); /* zero-padding */
768 GNUNET_assert (vbit == ((size * 8) % 5));
769 vbit = 5;
770 }
771 if (wpos >= out_size)
772 {
773 GNUNET_break (0);
774 return NULL;
775 }
776 out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
777 vbit -= 5;
778 }
779 GNUNET_assert (0 == vbit);
780 if (wpos < out_size)
781 out[wpos] = '\0';
782 return &out[wpos];
783}
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-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 787 of file strings.c.

788{
789 char *str_buf;
790 size_t len = size * 8;
791 char *end;
792
793 if (len % 5 > 0)
794 len += 5 - len % 5;
795 len /= 5;
796 str_buf = GNUNET_malloc (len + 1);
798 size,
799 str_buf,
800 len);
801 if (NULL == end)
802 {
803 GNUNET_free (str_buf);
804 return NULL;
805 }
806 *end = '\0';
807 return str_buf;
808}
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:732

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_url2(), 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 812 of file strings.c.

816{
817 size_t rpos;
818 size_t wpos;
819 unsigned int bits;
820 unsigned int vbit;
821 int ret;
822 int shift;
823 unsigned char *uout;
824 size_t encoded_len;
825
826 if (0 == enclen)
827 {
828 if (0 == out_size)
829 return GNUNET_OK;
830 return GNUNET_SYSERR;
831 }
832 GNUNET_assert (out_size < SIZE_MAX / 8);
833 encoded_len = out_size * 8;
834 uout = out;
835 wpos = out_size;
836 rpos = enclen;
837 if ((encoded_len % 5) > 0)
838 {
839 vbit = encoded_len % 5; /* padding! */
840 shift = 5 - vbit;
841 bits = (ret = getValue__ (enc[--rpos])) >> shift;
842 }
843 else
844 {
845 vbit = 5;
846 shift = 0;
847 bits = (ret = getValue__ (enc[--rpos]));
848 }
849 if ((encoded_len + shift) / 5 != enclen)
850 return GNUNET_SYSERR;
851 if (-1 == ret)
852 return GNUNET_SYSERR;
853 while (wpos > 0)
854 {
855 if (0 == rpos)
856 {
857 GNUNET_break (0);
858 return GNUNET_SYSERR;
859 }
860 bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
861 if (-1 == ret)
862 return GNUNET_SYSERR;
863 vbit += 5;
864 if (vbit >= 8)
865 {
866 uout[--wpos] = (unsigned char) bits;
867 bits >>= 8;
868 vbit -= 8;
869 }
870 }
871 if ((0 != rpos) || (0 != vbit))
872 return GNUNET_SYSERR;
873 return GNUNET_OK;
874}
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:682

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

882{
883 size_t size;
884 void *data;
885 int res;
886
887 size = (enclen * 5) / 8;
889 {
890 GNUNET_break_op (0);
891 return GNUNET_SYSERR;
892 }
895 enclen,
896 data,
897 size);
898 if ( (0 < size) &&
899 (GNUNET_OK != res) )
900 {
901 size--;
903 enclen,
904 data,
905 size);
906 }
907 if (GNUNET_OK != res)
908 {
909 GNUNET_break_op (0);
911 return GNUNET_SYSERR;
912 }
913 *out = data;
914 *out_size = size;
915 return GNUNET_OK;
916}
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:812

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

1602{
1603 const unsigned char *data = in;
1604 size_t ret;
1605 char *opt;
1606
1607 ret = 0;
1608 GNUNET_assert (len < SIZE_MAX / 4);
1609 opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1610 for (size_t i = 0; i < len; ++i)
1611 {
1612 char c;
1613
1614 c = (data[i] >> 2) & 0x3f;
1615 opt[ret++] = cvt[(int) c];
1616 c = (data[i] << 4) & 0x3f;
1617 if (++i < len)
1618 c |= (data[i] >> 4) & 0x0f;
1619 opt[ret++] = cvt[(int) c];
1620 if (i < len)
1621 {
1622 c = (data[i] << 2) & 0x3f;
1623 if (++i < len)
1624 c |= (data[i] >> 6) & 0x03;
1625 opt[ret++] = cvt[(int) c];
1626 }
1627 else
1628 {
1629 ++i;
1630 opt[ret++] = FILLCHAR;
1631 }
1632 if (i < len)
1633 {
1634 c = data[i] & 0x3f;
1635 opt[ret++] = cvt[(int) c];
1636 }
1637 else
1638 {
1639 opt[ret++] = FILLCHAR;
1640 }
1641 }
1642 *output = opt;
1643 return ret;
1644}
static const char * cvt
Definition: strings.c:1593
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1592

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

1866{
1867 struct GNUNET_Buffer buf = { 0 };
1868 const uint8_t *i8 = (uint8_t *) data;
1869 const uint8_t *end = (uint8_t *) (data + len);
1870
1871 while (end != i8)
1872 {
1873 if (0 == *i8)
1874 {
1875 /* invalid UTF-8 (or bad @a len): fail */
1876 GNUNET_break (0);
1877 GNUNET_buffer_clear (&buf);
1878 return 0;
1879 }
1880 if (0 == (0x80 & *i8))
1881 {
1882 /* traditional ASCII */
1883 if (isalnum (*i8) ||
1884 (*i8 == '-') ||
1885 (*i8 == '_') ||
1886 (*i8 == '.') ||
1887 (*i8 == '~') )
1888 GNUNET_buffer_write (&buf,
1889 (const char*) i8,
1890 1);
1891 else if (*i8 == ' ')
1892 GNUNET_buffer_write (&buf,
1893 "+",
1894 1);
1895 else
1897 "%%%X%X",
1898 *i8 >> 4,
1899 *i8 & 15);
1900 i8++;
1901 continue;
1902 }
1903 if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
1904 {
1905 /* 2-byte value, percent-encode */
1907 "%%%X%X",
1908 *i8 >> 4,
1909 *i8 & 15);
1910 i8++;
1911 if ( (end == i8) ||
1912 (0 == *i8) )
1913 {
1914 /* invalid UTF-8 (or bad @a len): fail */
1915 GNUNET_break (0);
1916 GNUNET_buffer_clear (&buf);
1917 return 0;
1918 }
1920 "%%%X%X",
1921 *i8 >> 4,
1922 *i8 & 15);
1923 i8++;
1924 continue;
1925 }
1926 if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
1927 {
1928 /* 3-byte value, percent-encode */
1929 for (unsigned int i = 0; i<3; i++)
1930 {
1931 if ( (end == i8) ||
1932 (0 == *i8) )
1933 {
1934 /* invalid UTF-8 (or bad @a len): fail */
1935 GNUNET_break (0);
1936 GNUNET_buffer_clear (&buf);
1937 return 0;
1938 }
1940 "%%%X%X",
1941 *i8 >> 4,
1942 *i8 & 15);
1943 i8++;
1944 }
1945 continue;
1946 }
1947 if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
1948 {
1949 /* 4-byte value, percent-encode */
1950 for (unsigned int i = 0; i<4; i++)
1951 {
1952 if ( (end == i8) ||
1953 (0 == *i8) )
1954 {
1955 /* invalid UTF-8 (or bad @a len): fail */
1956 GNUNET_break (0);
1957 GNUNET_buffer_clear (&buf);
1958 return 0;
1959 }
1961 "%%%X%X",
1962 *i8 >> 4,
1963 *i8 & 15);
1964 i8++;
1965 }
1966 continue;
1967 }
1968 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
1969 + 0x04) & *i8))
1970 {
1971 /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1972 for (unsigned int i = 0; i<5; i++)
1973 {
1974 if ( (end == i8) ||
1975 (0 == *i8) )
1976 {
1977 /* invalid UTF-8 (or bad @a len): fail */
1978 GNUNET_break (0);
1979 GNUNET_buffer_clear (&buf);
1980 return 0;
1981 }
1983 "%%%X%X",
1984 *i8 >> 4,
1985 *i8 & 15);
1986 i8++;
1987 }
1988 continue;
1989 }
1990 if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
1991 + 0x08 + 0x04 + 0x02)
1992 & *i8))
1993 {
1994 /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1995 for (unsigned int i = 0; i<6; i++)
1996 {
1997 if ( (end == i8) ||
1998 (0 == *i8) )
1999 {
2000 /* invalid UTF-8 (or bad @a len): fail */
2001 GNUNET_break (0);
2002 GNUNET_buffer_clear (&buf);
2003 return 0;
2004 }
2006 "%%%X%X",
2007 *i8 >> 4,
2008 *i8 & 15);
2009 i8++;
2010 }
2011 continue;
2012 }
2013 /* really, really invalid UTF-8: fail */
2014 GNUNET_break (0);
2015 GNUNET_buffer_clear (&buf);
2016 return 0;
2017 }
2018 *out = GNUNET_buffer_reap_str (&buf);
2019 return strlen (*out);
2020}
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:165
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_url2(), 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 1648 of file strings.c.

1651{
1652 char *enc;
1653 size_t pos;
1654
1656 len,
1657 output);
1658 enc = *output;
1659 /* Replace with correct characters for base64url */
1660 pos = 0;
1661 while ('\0' != enc[pos])
1662 {
1663 if ('+' == enc[pos])
1664 enc[pos] = '-';
1665 if ('/' == enc[pos])
1666 enc[pos] = '_';
1667 if ('=' == enc[pos])
1668 {
1669 enc[pos] = '\0';
1670 break;
1671 }
1672 pos++;
1673 }
1674 return strlen (enc);
1675}
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1599

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

1704{
1705 unsigned char *output;
1706 size_t ret = 0;
1707
1708 GNUNET_assert (len / 3 < SIZE_MAX);
1709 output = GNUNET_malloc ((len * 3 / 4) + 8);
1711 "base64_decode decoding len=%d\n",
1712 (int) len);
1713 for (size_t i = 0; i < len; ++i)
1714 {
1715 unsigned char c;
1716 unsigned char c1;
1717
1718 CHECK_CRLF;
1719 if (FILLCHAR == data[i])
1720 break;
1721 c = (unsigned char) cvtfind (data[i]);
1722 ++i;
1723 CHECK_CRLF;
1724 c1 = (unsigned char) cvtfind (data[i]);
1725 c = (c << 2) | ((c1 >> 4) & 0x3);
1726 output[ret++] = c;
1727 if (++i < len)
1728 {
1729 CHECK_CRLF;
1730 c = data[i];
1731 if (FILLCHAR == c)
1732 break;
1733 c = (unsigned char) cvtfind (c);
1734 c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1735 output[ret++] = c1;
1736 }
1737 if (++i < len)
1738 {
1739 CHECK_CRLF;
1740 c1 = data[i];
1741 if (FILLCHAR == c1)
1742 break;
1743
1744 c1 = (unsigned char) cvtfind (c1);
1745 c = ((c << 6) & 0xc0) | c1;
1746 output[ret++] = c;
1747 }
1748 }
1749END:
1750 *out = output;
1751 return ret;
1752}
@ END
We're done processing.
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_DEBUG
#define CHECK_CRLF
Definition: strings.c:1688
#define cvtfind(a)
Definition: strings.c:1678

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

1762{
1763 char *s;
1764 int padding;
1765 size_t ret;
1766
1767 /* make enough space for padding */
1768 GNUNET_assert (len < SIZE_MAX - 3);
1769 s = GNUNET_malloc (len + 3);
1770 memcpy (s,
1771 data,
1772 len);
1773 for (size_t i = 0; i < strlen (s); i++)
1774 {
1775 if (s[i] == '-')
1776 s[i] = '+';
1777 if (s[i] == '_')
1778 s[i] = '/';
1779 }
1780 padding = len % 4;
1781 switch (padding) // Pad with trailing '='s
1782 {
1783 case 0:
1784 break; // No pad chars in this case
1785 case 2:
1786 memcpy (&s[len],
1787 "==",
1788 2);
1789 len += 2;
1790 break; // Two pad chars
1791 case 3:
1792 s[len] = '=';
1793 len++;
1794 break; // One pad char
1795 default:
1796 GNUNET_assert (0);
1797 break;
1798 }
1800 len,
1801 out);
1802 GNUNET_free (s);
1803 return ret;
1804}
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out)
Decode from Base64.
Definition: strings.c:1701

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

1811{
1812 const char *rpos = data;
1813 char *wpos;
1814 size_t resl = 0;
1815 *out = GNUNET_malloc (len + 1); /* output should always fit into input */
1816 wpos = *out;
1817
1818 while ( ('\0' != *rpos) &&
1819 (data + len != rpos) )
1820 {
1821 unsigned int num;
1822 switch (*rpos)
1823 {
1824 case '%':
1825 if (rpos + 3 > data + len)
1826 {
1827 GNUNET_break_op (0);
1828 GNUNET_free (*out);
1829 return 0;
1830 }
1831 if (1 != sscanf (rpos + 1,
1832 "%2x",
1833 &num))
1834 {
1835 /* Invalid URL encoding, try to continue anyway */
1836 GNUNET_break_op (0);
1837 *wpos = *rpos;
1838 wpos++;
1839 resl++;
1840 rpos++;
1841 break;
1842 }
1843 *wpos = (char) ((unsigned char) num);
1844 wpos++;
1845 resl++;
1846 rpos += 3;
1847 break;
1848 /* TODO: add bad sequence handling */
1849 /* intentional fall through! */
1850 default:
1851 *wpos = *rpos;
1852 wpos++;
1853 resl++;
1854 rpos++;
1855 }
1856 }
1857 *wpos = '\0'; /* add 0-terminator */
1858 return resl;
1859}

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

923{
924 size_t len;
925 size_t i;
926 int end;
927 int pp_state = 0;
928 const char *post_scheme_part = NULL;
929
930 len = strlen (path);
931 for (end = 0, i = 0; ! end && i < len; i++)
932 {
933 switch (pp_state)
934 {
935 case 0:
936 if ((path[i] == ':') && (i > 0))
937 {
938 pp_state += 1;
939 continue;
940 }
941 if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
942 ((path[i] >= 'a') && (path[i] <= 'z') ) ||
943 ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
944 (path[i] == '-') || (path[i] == '.')))
945 end = 1;
946 break;
947
948 case 1:
949 case 2:
950 if (path[i] == '/')
951 {
952 pp_state += 1;
953 continue;
954 }
955 end = 1;
956 break;
957
958 case 3:
959 post_scheme_part = &path[i];
960 end = 1;
961 break;
962
963 default:
964 end = 1;
965 }
966 }
967 if (post_scheme_part == NULL)
968 return GNUNET_NO;
969 if (scheme_part)
970 {
971 *scheme_part = GNUNET_strndup (path,
972 post_scheme_part - path);
973 }
974 if (path_part)
975 *path_part = post_scheme_part;
976 return GNUNET_YES;
977}
@ 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 981 of file strings.c.

985{
986 const char *post_scheme_path;
987 int is_uri;
988 char *uri;
989 /* consider POSIX paths to be absolute too, even on W32,
990 * as plibc expansion will fix them for us.
991 */
992 if (filename[0] == '/')
993 return GNUNET_YES;
994 if (can_be_uri)
995 {
996 is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
997 if (r_is_uri)
998 *r_is_uri = is_uri;
999 if (is_uri)
1000 {
1001 if (r_uri_scheme)
1002 *r_uri_scheme = uri;
1003 else
1004 GNUNET_free (uri);
1005
1006 return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1007 GNUNET_NO,
1008 NULL,
1009 NULL);
1010 }
1011 }
1012 else
1013 {
1014 if (r_is_uri)
1015 *r_is_uri = GNUNET_NO;
1016 }
1017
1018 return GNUNET_NO;
1019}
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:981
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:920

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

1025{
1026 struct stat st;
1027
1028 if ((NULL == filename) || (filename[0] == '\0'))
1029 return GNUNET_SYSERR;
1030 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1032 return GNUNET_NO;
1033 if (0 != (checks
1036 {
1037 if (0 != lstat (filename, &st))
1038 {
1039 if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1040 return GNUNET_NO;
1041 else
1042 return GNUNET_SYSERR;
1043 }
1044 }
1045 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1046 if (! S_ISDIR (st.st_mode))
1047 return GNUNET_NO;
1048 if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1049 if (! S_ISLNK (st.st_mode))
1050 return GNUNET_NO;
1051 return GNUNET_YES;
1052}
#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 1056 of file strings.c.

1059{
1060 if (addrlen < 6)
1061 return GNUNET_SYSERR;
1062 if (addrlen > 512)
1063 return GNUNET_SYSERR; /* sanity check to protect zbuf allocation,
1064 actual limit is not precise */
1065 {
1066 char zbuf[addrlen + 1];
1067 int ret;
1068 char *port_colon;
1069 unsigned int port;
1070 char dummy[2];
1071
1072 GNUNET_memcpy (zbuf, zt_addr, addrlen);
1073 if ('[' != zbuf[0])
1074 {
1076 _ ("IPv6 address did not start with `['\n"));
1077 return GNUNET_SYSERR;
1078 }
1079 zbuf[addrlen] = '\0';
1080 port_colon = strrchr (zbuf, ':');
1081 if (NULL == port_colon)
1082 {
1084 _ ("IPv6 address did contain ':' to separate port number\n"));
1085 return GNUNET_SYSERR;
1086 }
1087 if (']' != *(port_colon - 1))
1088 {
1089 GNUNET_log (
1091 _ (
1092 "IPv6 address did contain ']' before ':' to separate port number\n"));
1093 return GNUNET_SYSERR;
1094 }
1095 ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1096 if ((1 != ret) || (port > 65535))
1097 {
1098 GNUNET_log (
1100 _ (
1101 "IPv6 address did contain a valid port number after the last ':'\n"));
1102 return GNUNET_SYSERR;
1103 }
1104 *(port_colon - 1) = '\0';
1105 memset (r_buf, 0, sizeof(struct sockaddr_in6));
1106 ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1107 if (ret <= 0)
1108 {
1110 _ ("Invalid IPv6 address `%s': %s\n"),
1111 &zbuf[1],
1112 strerror (errno));
1113 return GNUNET_SYSERR;
1114 }
1115 r_buf->sin6_port = htons (port);
1116 r_buf->sin6_family = AF_INET6;
1117#if HAVE_SOCKADDR_IN_SIN_LEN
1118 r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1119#endif
1120 return GNUNET_OK;
1121 }
1122}
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 1126 of file strings.c.

1129{
1130 unsigned int temps[4];
1131 unsigned int port;
1132 unsigned int cnt;
1133 char dummy[2];
1134
1135 if (addrlen < 9)
1136 return GNUNET_SYSERR;
1137 cnt = sscanf (zt_addr,
1138 "%u.%u.%u.%u:%u%1s",
1139 &temps[0],
1140 &temps[1],
1141 &temps[2],
1142 &temps[3],
1143 &port,
1144 dummy);
1145 if (5 != cnt)
1146 return GNUNET_SYSERR;
1147 for (cnt = 0; cnt < 4; cnt++)
1148 if (temps[cnt] > 0xFF)
1149 return GNUNET_SYSERR;
1150 if (port > 65535)
1151 return GNUNET_SYSERR;
1152 r_buf->sin_family = AF_INET;
1153 r_buf->sin_port = htons (port);
1154 r_buf->sin_addr.s_addr =
1155 htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1156#if HAVE_SOCKADDR_IN_SIN_LEN
1157 r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1158#endif
1159 return GNUNET_OK;
1160}

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

1182{
1183 *af = AF_UNSPEC;
1184 if ('[' == *addr)
1185 {
1186 /* IPv6 */
1187 *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1188 if (GNUNET_OK !=
1190 strlen (addr),
1191 (struct sockaddr_in6 *) *sa))
1192 {
1193 GNUNET_free (*sa);
1194 *sa = NULL;
1195 return 0;
1196 }
1197 *af = AF_INET6;
1198 return sizeof(struct sockaddr_in6);
1199 }
1200 else
1201 {
1202 /* IPv4 */
1203 *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1204 if (GNUNET_OK !=
1206 strlen (addr),
1207 (struct sockaddr_in *) *sa))
1208 {
1209 GNUNET_free (*sa);
1210 *sa = NULL;
1211 return 0;
1212 }
1213 *af = AF_INET;
1214 return sizeof(struct sockaddr_in);
1215 }
1216}
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:1056
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:1126

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

1167{
1168 if (addr[0] == '[')
1169 return GNUNET_STRINGS_to_address_ipv6 (addr,
1170 addrlen,
1171 (struct sockaddr_in6 *) r_buf);
1172 return GNUNET_STRINGS_to_address_ipv4 (addr,
1173 addrlen,
1174 (struct sockaddr_in *) r_buf);
1175}

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(), 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 2037 of file strings.c.

2038{
2039 const char *ret;
2040 const char *dot;
2041
2042 ret = strrchr (argv0, '_');
2043 if (NULL == ret)
2044 return NULL;
2045 ret++; /* skip underscore */
2046 dot = strchr (ret,
2047 '.');
2048 if (NULL != dot)
2049 return GNUNET_strndup (ret,
2050 dot - ret);
2051 return GNUNET_strdup (ret);
2052}

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

1276{
1277 size_t count;
1278 size_t len;
1279 size_t pos;
1280 unsigned int temps[8];
1282 char *routeList;
1283
1284 if (NULL == routeListX)
1285 return NULL;
1286 len = strlen (routeListX);
1287 if (0 == len)
1288 return NULL;
1289 routeList = GNUNET_strdup (routeListX);
1290 count = 0;
1291 for (size_t i = 0; i < len; i++)
1292 if (routeList[i] == ';')
1293 count++;
1294 GNUNET_assert (count < SIZE_MAX);
1295 result = GNUNET_new_array (count + 1,
1297 pos = 0;
1298 for (size_t i = 0; i < count; i++)
1299 {
1300 size_t colon;
1301 size_t end;
1302 char dummy;
1303
1304 for (colon = pos; ':' != routeList[colon]; colon++)
1305 if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1306 break;
1307 for (end = colon; ';' != routeList[end]; end++)
1308 if ('\0' == routeList[end])
1309 break;
1310 if ('\0' == routeList[end])
1311 break;
1312 routeList[end] = '\0';
1313 if (':' == routeList[colon])
1314 {
1315 routeList[colon] = '\0';
1316 if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1317 break;
1318 }
1319 if (8 ==
1320 sscanf (&routeList[pos],
1321 "%u.%u.%u.%u/%u.%u.%u.%u%c",
1322 &temps[0],
1323 &temps[1],
1324 &temps[2],
1325 &temps[3],
1326 &temps[4],
1327 &temps[5],
1328 &temps[6],
1329 &temps[7],
1330 &dummy))
1331 {
1332 for (unsigned int j = 0; j < 8; j++)
1333 if (temps[j] > 0xFF)
1334 {
1336 _ ("Invalid format for IP: `%s'\n"),
1337 &routeList[pos]);
1339 GNUNET_free (routeList);
1340 return NULL;
1341 }
1342 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1343 + (temps[2] << 8) + temps[3]);
1344 result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1345 + (temps[6] << 8) + temps[7]);
1346 pos = end + 1;
1347 continue;
1348 }
1349
1350 /* try second notation */
1351 {
1352 unsigned int slash;
1353
1354 if (5 ==
1355 sscanf (&routeList[pos],
1356 "%u.%u.%u.%u/%u%c",
1357 &temps[0],
1358 &temps[1],
1359 &temps[2],
1360 &temps[3],
1361 &slash,
1362 &dummy))
1363 {
1364 for (unsigned int j = 0; j < 4; j++)
1365 if (temps[j] > 0xFF)
1366 {
1368 _ ("Invalid format for IP: `%s'\n"),
1369 &routeList[pos]);
1371 GNUNET_free (routeList);
1372 return NULL;
1373 }
1374 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1375 + (temps[2] << 8) + temps[3]);
1376 if (slash <= 32)
1377 {
1378 result[i].netmask.s_addr = 0;
1379 while (slash > 0)
1380 {
1381 result[i].netmask.s_addr =
1382 (result[i].netmask.s_addr >> 1) + 0x80000000;
1383 slash--;
1384 }
1385 result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1386 pos = end + 1;
1387 continue;
1388 }
1389 else
1390 {
1392 _ (
1393 "Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1394 slash);
1396 GNUNET_free (routeList);
1397 return NULL; /* error */
1398 }
1399 }
1400 }
1401
1402 /* try third notation */
1403 if (4 ==
1404 sscanf (&routeList[pos],
1405 "%u.%u.%u.%u%c",
1406 &temps[0],
1407 &temps[1],
1408 &temps[2],
1409 &temps[3],
1410 &dummy))
1411 {
1412 for (unsigned int j = 0; j < 4; j++)
1413 if (temps[j] > 0xFF)
1414 {
1416 _ ("Invalid format for IP: `%s'\n"),
1417 &routeList[pos]);
1419 GNUNET_free (routeList);
1420 return NULL;
1421 }
1422 result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1423 + (temps[2] << 8) + temps[3]);
1424 result[i].netmask.s_addr = htonl (0xffffffff); /* yeah, the htonl is useless */
1425 pos = end + 1;
1426 continue;
1427 }
1429 _ ("Invalid format for IP: `%s'\n"),
1430 &routeList[pos]);
1432 GNUNET_free (routeList);
1433 return NULL; /* error */
1434 }
1435 if (pos < strlen (routeList))
1436 {
1438 _ ("Invalid format: `%s'\n"),
1439 &routeListX[pos]);
1441 GNUNET_free (routeList);
1442 return NULL; /* oops */
1443 }
1444 GNUNET_free (routeList);
1445 return result; /* ok */
1446}
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:1230
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 synatx is flawed

Definition at line 1450 of file strings.c.

1451{
1452 size_t count;
1453 size_t len;
1454 size_t pos;
1455 int ret;
1456 char *routeList;
1458 unsigned int off;
1459
1460 if (NULL == routeListX)
1461 return NULL;
1462 len = strlen (routeListX);
1463 if (0 == len)
1464 return NULL;
1465 routeList = GNUNET_strdup (routeListX);
1466 count = 0;
1467 for (size_t j = 0; j < len; j++)
1468 if (';' == routeList[j])
1469 count++;
1470 if (';' != routeList[len - 1])
1471 {
1473 _ ("Invalid network notation (does not end with ';': `%s')\n"),
1474 routeList);
1475 GNUNET_free (routeList);
1476 return NULL;
1477 }
1478 GNUNET_assert (count < UINT_MAX);
1479 result = GNUNET_new_array (count + 1,
1481 pos = 0;
1482 for (size_t i = 0; i < count; i++)
1483 {
1484 size_t start;
1485 size_t slash;
1486
1487 start = pos;
1488 while (';' != routeList[pos])
1489 pos++;
1490 slash = pos;
1491 while ( (slash > start) &&
1492 (routeList[slash] != '/') )
1493 slash--;
1494 if (slash <= start)
1495 {
1496 memset (&result[i].netmask,
1497 0xFF,
1498 sizeof(struct in6_addr));
1499 slash = pos;
1500 }
1501 else
1502 {
1503 size_t colon;
1504
1505 routeList[pos] = '\0';
1506 for (colon = pos; ':' != routeList[colon]; colon--)
1507 if ('/' == routeList[colon])
1508 break;
1509 if (':' == routeList[colon])
1510 {
1511 routeList[colon] = '\0';
1512 if (GNUNET_OK !=
1513 parse_port_policy (&routeList[colon + 1],
1514 &result[i].pp))
1515 {
1517 GNUNET_free (routeList);
1518 return NULL;
1519 }
1520 }
1521 ret = inet_pton (AF_INET6,
1522 &routeList[slash + 1],
1523 &result[i].netmask);
1524 if (ret <= 0)
1525 {
1526 char dummy;
1527 unsigned int bits;
1528 int save = errno;
1529
1530 if ( (1 != sscanf (&routeList[slash + 1],
1531 "%u%c",
1532 &bits,
1533 &dummy)) ||
1534 (bits > 128))
1535 {
1536 if (0 == ret)
1537 {
1539 _ ("Wrong format `%s' for netmask\n"),
1540 &routeList[slash]);
1541 }
1542 else
1543 {
1544 errno = save;
1546 "inet_pton");
1547 }
1549 GNUNET_free (routeList);
1550 return NULL;
1551 }
1552 off = 0;
1553 while (bits > 8)
1554 {
1555 result[i].netmask.s6_addr[off++] = 0xFF;
1556 bits -= 8;
1557 }
1558 while (bits > 0)
1559 {
1560 result[i].netmask.s6_addr[off] =
1561 (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1562 bits--;
1563 }
1564 }
1565 }
1566 routeList[slash] = '\0';
1567 ret = inet_pton (AF_INET6,
1568 &routeList[start],
1569 &result[i].network);
1570 if (ret <= 0)
1571 {
1572 if (0 == ret)
1574 _ ("Wrong format `%s' for network\n"),
1575 &routeList[slash + 1]);
1576 else
1578 "inet_pton");
1580 GNUNET_free (routeList);
1581 return NULL;
1582 }
1583 pos++;
1584 }
1585 GNUNET_free (routeList);
1586 return result;
1587}
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: