GNUnet  0.11.x
Data Structures | Enumerations | Functions
Strings library

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

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

int GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, unsigned long long *size)
 Convert a given fancy human-readable size to bytes. More...
 
int 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_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...
 
void GNUNET_STRINGS_utf8_tolower (const char *input, char *output)
 Convert the utf-8 input string to lower case. More...
 
void 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...
 
int GNUNET_STRINGS_string_to_data (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_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...
 
char * GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, unsigned int num_pids)
 Convert a peer path to a human-readable string. More...
 
int GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part, const char **path_part)
 Parse a path that might be an URI. More...
 
int 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...
 
int GNUNET_STRINGS_check_filename (const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
 Perform checks on filename. More...
 
int GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
 Tries to convert zt_addr string to an IPv6 address. More...
 
int GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_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...
 
int 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...
 
int GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
 Returns utf-8 encoded arguments. More...
 
size_t GNUNET_strlcpy (char *dst, const char *src, size_t n)
 Like strlcpy but portable. More...
 
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 451 of file gnunet_strings_lib.h.

452 {
456  GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
457 
462 
466  GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
467 
472 };
Check that the path is an absolute path.
Check that it exists.
Check that it is a link.
Check that it is a directory.

Function Documentation

◆ GNUNET_STRINGS_fancy_size_to_bytes()

int 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 (i.e. 1 MB)
sizeset to the size in bytes
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 323 of file strings.c.

References convert_with_table().

Referenced by GNUNET_ATS_solvers_load_quotas(), GNUNET_CONFIGURATION_get_value_size(), parse_quota(), and print_quotas().

325 {
326  static const struct ConversionTable table[] =
327  { { "B", 1 },
328  { "KiB", 1024 },
329  { "kB", 1000 },
330  { "MiB", 1024 * 1024 },
331  { "MB", 1000 * 1000 },
332  { "GiB", 1024 * 1024 * 1024 },
333  { "GB", 1000 * 1000 * 1000 },
334  { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
335  { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
336  { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
337  { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
338  { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
339  { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
340  { NULL, 0 } };
341 
342  return convert_with_table (fancy_size, table, size);
343 }
static int 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:261
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:55
Unit conversion table entry for 'convert_with_table'.
Definition: strings.c:235
static unsigned int size
Size of the "table".
Definition: peer.c:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_fancy_time_to_relative()

int 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 (i.e. 1 minute)
rtimeset to the relative time
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 355 of file strings.c.

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

Referenced by GNUNET_CONFIGURATION_get_value_time(), main(), parse_expiration(), parse_record(), set_relative_time(), and set_timetravel_time().

357 {
358  static const struct ConversionTable table[] =
359  { { "us", 1 },
360  { "ms", 1000 },
361  { "s", 1000 * 1000LL },
362  { "second", 1000 * 1000LL },
363  { "seconds", 1000 * 1000LL },
364  { "\"", 1000 * 1000LL },
365  { "m", 60 * 1000 * 1000LL },
366  { "min", 60 * 1000 * 1000LL },
367  { "minute", 60 * 1000 * 1000LL },
368  { "minutes", 60 * 1000 * 1000LL },
369  { "'", 60 * 1000 * 1000LL },
370  { "h", 60 * 60 * 1000 * 1000LL },
371  { "hour", 60 * 60 * 1000 * 1000LL },
372  { "hours", 60 * 60 * 1000 * 1000LL },
373  { "d", 24 * 60 * 60 * 1000LL * 1000LL },
374  { "day", 24 * 60 * 60 * 1000LL * 1000LL },
375  { "days", 24 * 60 * 60 * 1000LL * 1000LL },
376  { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
377  { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
378  { "year", 31536000000000LL /* year */ },
379  { "years", 31536000000000LL /* year */ },
380  { "a", 31536000000000LL /* year */ },
381  { NULL, 0 } };
382  int ret;
383  unsigned long long val;
384 
385  if (0 == strcasecmp ("forever", fancy_time))
386  {
388  return GNUNET_OK;
389  }
390  ret = convert_with_table (fancy_time, table, &val);
391  rtime->rel_value_us = (uint64_t) val;
392  return ret;
393 }
uint64_t rel_value_us
The actual value.
static int 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:261
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:55
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
Unit conversion table entry for 'convert_with_table'.
Definition: strings.c:235
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 176 of file strings.c.

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

Referenced by progress_cb(), and publish_inspector().

177 {
178  const char *unit = _ (/* size unit */ "b");
179  char *ret;
180 
181  if (size > 5 * 1024)
182  {
183  size = size / 1024;
184  unit = "KiB";
185  if (size > 5 * 1024)
186  {
187  size = size / 1024;
188  unit = "MiB";
189  if (size > 5 * 1024)
190  {
191  size = size / 1024;
192  unit = "GiB";
193  if (size > 5 * 1024)
194  {
195  size = size / 1024;
196  unit = "TiB";
197  }
198  }
199  }
200  }
201  ret = GNUNET_malloc (32);
202  GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
203  return ret;
204 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_malloc(size)
Wrapper around malloc.
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 orignal string is returned.

Definition at line 451 of file strings.c.

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

Referenced by GNUNET_STRINGS_from_utf8(), and GNUNET_STRINGS_to_utf8().

455 {
456  char *ret;
457  uint8_t *u8_string;
458  char *encoded_string;
459  size_t u8_string_length;
460  size_t encoded_string_length;
461 
462  u8_string = u8_conv_from_encoding (input_charset,
463  iconveh_error,
464  input,
465  len,
466  NULL,
467  NULL,
468  &u8_string_length);
469  if (NULL == u8_string)
470  {
471  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
472  goto fail;
473  }
474  if (0 == strcmp (output_charset, "UTF-8"))
475  {
476  ret = GNUNET_malloc (u8_string_length + 1);
477  GNUNET_memcpy (ret, u8_string, u8_string_length);
478  ret[u8_string_length] = '\0';
479  free (u8_string);
480  return ret;
481  }
482  encoded_string = u8_conv_to_encoding (output_charset,
483  iconveh_error,
484  u8_string,
485  u8_string_length,
486  NULL,
487  NULL,
488  &encoded_string_length);
489  free (u8_string);
490  if (NULL == encoded_string)
491  {
492  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
493  goto fail;
494  }
495  ret = GNUNET_malloc (encoded_string_length + 1);
496  GNUNET_memcpy (ret, encoded_string, encoded_string_length);
497  ret[encoded_string_length] = '\0';
498  free (encoded_string);
499  return ret;
500 fail:
502  _ ("Character sets requested were `%s'->`%s'\n"),
503  "UTF-8",
504  output_charset);
505  ret = GNUNET_malloc (len + 1);
506  GNUNET_memcpy (ret, input, len);
507  ret[len] = '\0';
508  return ret;
509 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
#define LOG(kind,...)
Definition: strings.c:37
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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)
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), if conversion fails, a copy of the orignal string is returned.

Definition at line 525 of file strings.c.

References GNUNET_STRINGS_conv().

528 {
529  return GNUNET_STRINGS_conv (input,
530  len,
531  charset,
532  "UTF-8");
533 }
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:451
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call 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 orignal string is returned.

Definition at line 548 of file strings.c.

References GNUNET_STRINGS_conv().

551 {
552  return GNUNET_STRINGS_conv (input,
553  len,
554  "UTF-8",
555  charset);
556 }
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:451
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ GNUNET_STRINGS_utf8_tolower()

void 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

Convert the utf-8 input string to lower case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer

Definition at line 567 of file strings.c.

References GNUNET_memcpy, and len.

Referenced by ego_create(), GNUNET_GNSRECORD_string_to_lowercase(), GNUNET_RECLAIM_attestation_new(), GNUNET_RECLAIM_attribute_new(), handle_collect(), handle_create_message(), handle_delete_message(), handle_get_default_message(), handle_lookup(), handle_rename_message(), handle_set_default_message(), handle_verify(), header_iterator(), and run_with_zone_pkey().

568 {
569  uint8_t *tmp_in;
570  size_t len;
571 
572  tmp_in = u8_tolower ((uint8_t *) input,
573  strlen ((char *) input),
574  NULL,
575  UNINORM_NFD,
576  NULL,
577  &len);
578  GNUNET_memcpy (output, tmp_in, len);
579  output[len] = '\0';
580  free (tmp_in);
581 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_utf8_toupper()

void 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

Convert the utf-8 input string to upper case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer

Definition at line 592 of file strings.c.

References GNUNET_memcpy, and len.

Referenced by client_configure_plugin(), GNUNET_CRYPTO_hash_from_string2(), GNUNET_HOSTLIST_client_start(), and load_op_add_address().

593 {
594  uint8_t *tmp_in;
595  size_t len;
596 
597  tmp_in = u8_toupper ((uint8_t *) input,
598  strlen ((char *) input),
599  NULL,
600  UNINORM_NFD,
601  NULL,
602  &len);
603  GNUNET_memcpy (output, tmp_in, len);
604  output[len] = '\0';
605  free (tmp_in);
606 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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
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 617 of file strings.c.

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

Referenced by check_client_index_start(), GNUNET_CONFIGURATION_get_value_filename(), 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_DISK_file_test(), GNUNET_FS_directory_scan_start(), GNUNET_FS_make_file_reader_context_(), GNUNET_log_setup(), handle_client_index_start(), hash_for_index_cb(), print_option(), and set_filename().

618 {
619  char *buffer;
620  size_t len;
621  char *fm;
622  const char *fil_ptr;
623 
624  if (fil == NULL)
625  return NULL;
626 
627  if (fil[0] == DIR_SEPARATOR)
628  /* absolute path, just copy */
629  return GNUNET_strdup (fil);
630  if (fil[0] == '~')
631  {
632  fm = getenv ("HOME");
633  if (fm == NULL)
634  {
636  _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
637  return NULL;
638  }
639  fm = GNUNET_strdup (fm);
640  /* do not copy '~' */
641  fil_ptr = fil + 1;
642 
643  /* skip over dir seperator to be consistent */
644  if (fil_ptr[0] == DIR_SEPARATOR)
645  fil_ptr++;
646  }
647  else
648  {
649  /* relative path */
650  fil_ptr = fil;
651  len = 512;
652  fm = NULL;
653  while (1)
654  {
655  buffer = GNUNET_malloc (len);
656  if (getcwd (buffer, len) != NULL)
657  {
658  fm = buffer;
659  break;
660  }
661  if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
662  {
663  len *= 2;
664  GNUNET_free (buffer);
665  continue;
666  }
667  GNUNET_free (buffer);
668  break;
669  }
670  if (fm == NULL)
671  {
673  buffer = getenv ("PWD"); /* alternative */
674  if (buffer != NULL)
675  fm = GNUNET_strdup (buffer);
676  }
677  if (fm == NULL)
678  fm = GNUNET_strdup ("./"); /* give up */
679  }
680  GNUNET_asprintf (&buffer,
681  "%s%s%s",
682  fm,
683  (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
685  fil_ptr);
686  GNUNET_free (fm);
687  return buffer;
688 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
int GNUNET_asprintf(char **buf, const char *format,...)
Like asprintf, just portable.
#define DIR_SEPARATOR_STR
Definition: platform.h:171
#define LOG(kind,...)
Definition: strings.c:37
#define DIR_SEPARATOR
Definition: platform.h:170
char * getenv()
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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)

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

References GNUNET_assert, and GNUNET_memcpy.

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

68 {
69  size_t needed;
70  va_list ap;
71 
72  needed = 0;
73  va_start (ap, count);
74  while (count > 0)
75  {
76  const char *s = va_arg (ap, const char *);
77  size_t slen = strlen (s) + 1;
78 
79  GNUNET_assert (slen <= size - needed);
80  if (NULL != buffer)
81  GNUNET_memcpy (&buffer[needed],
82  s,
83  slen);
84  needed += slen;
85  count--;
86  }
87  va_end (ap);
88  return needed;
89 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static unsigned int size
Size of the "table".
Definition: peer.c:67
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.
Parameters
bufferthe buffer to parse
sizesize of the buffer
countnumber of strings to locate
Returns
offset of the character after the last 0-termination in the buffer, or 0 on error.

Definition at line 136 of file strings.c.

References start.

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

140 {
141  unsigned int start;
142  unsigned int needed;
143  const char **r;
144  va_list ap;
145 
146  needed = 0;
147  va_start (ap, count);
148  while (count > 0)
149  {
150  r = va_arg (ap, const char **);
151 
152  start = needed;
153  while ((needed < size) && (buffer[needed] != '\0'))
154  needed++;
155  if (needed == size)
156  {
157  va_end (ap);
158  return 0; /* error */
159  }
160  *r = &buffer[start];
161  needed++; /* skip 0-termination */
162  count--;
163  }
164  va_end (ap);
165  return needed;
166 }
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
static unsigned int size
Size of the "table".
Definition: peer.c:67
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 789 of file strings.c.

References DIR_SEPARATOR_STR, and filename.

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

790 {
791  const char *short_fn = filename;
792  const char *ss;
793 
794  while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
795  short_fn = 1 + ss;
796  return short_fn;
797 }
#define DIR_SEPARATOR_STR
Definition: platform.h:171
static char * filename
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.

Convert binary data to ASCII encoding using CrockfordBase32.

Returns a pointer to the byte after the last byte in the string, that is where the 0-terminator was placed if there was room.

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 + 4) / 5 bytes
Returns
pointer to the next byte in out or NULL on error.

32 characters for encoding

Definition at line 870 of file strings.c.

References data, GNUNET_assert, GNUNET_break, and SIZE_MAX.

Referenced by auth_key_to_string(), 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_sh2s(), GNUNET_STRINGS_data_to_string_alloc(), GNUNET_uuid2s(), output_message(), print_examples_ecdh(), secret_ready_cb(), uri_loc_to_string(), and uri_sks_to_string().

874 {
878  static char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
879  unsigned int wpos;
880  unsigned int rpos;
881  unsigned int bits;
882  unsigned int vbit;
883  const unsigned char *udata;
884 
885  GNUNET_assert (size < SIZE_MAX / 8 - 4);
886  udata = data;
887  if (out_size < (size * 8 + 4) / 5)
888  {
889  GNUNET_break (0);
890  return NULL;
891  }
892  vbit = 0;
893  wpos = 0;
894  rpos = 0;
895  bits = 0;
896  while ((rpos < size) || (vbit > 0))
897  {
898  if ((rpos < size) && (vbit < 5))
899  {
900  bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
901  vbit += 8;
902  }
903  if (vbit < 5)
904  {
905  bits <<= (5 - vbit); /* zero-padding */
906  GNUNET_assert (vbit == ((size * 8) % 5));
907  vbit = 5;
908  }
909  if (wpos >= out_size)
910  {
911  GNUNET_break (0);
912  return NULL;
913  }
914  out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
915  vbit -= 5;
916  }
917  GNUNET_assert (0 == vbit);
918  if (wpos < out_size)
919  out[wpos] = '\0';
920  return &out[wpos];
921 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define SIZE_MAX
Definition: platform.h:220
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static unsigned int size
Size of the "table".
Definition: peer.c:67
uint32_t data
The data value.
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

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

Parameters
bufferwith data
sizesize of the buffer
Returns
freshly allocated, null-terminated string

Definition at line 935 of file strings.c.

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

Referenced by attest_collect(), attest_iter_cb(), attest_store_task(), attr_collect(), attr_store_task(), conversation_value_to_string(), display_data(), GNUNET_JSON_from_data(), handle_attestation_delete_message(), handle_attribute_delete_message(), issue_ticket(), iter_cb(), lookup_authz_cb(), move_attr_finished(), move_attrs(), OIDC_id_token_new(), oidc_ticket_issue_cb(), process_attrs(), process_record(), RECLAIM_TICKETS_consume(), RECLAIM_TICKETS_revoke(), rvk_move_attr_cb(), setup_job_headers(), ticket_collect(), ticket_issue_cb(), ticket_iter(), and value_to_string().

936 {
937  char *str_buf;
938  size_t len = size * 8;
939  char *end;
940 
941  if (len % 5 > 0)
942  len += 5 - len % 5;
943  len /= 5;
944  str_buf = GNUNET_malloc (len + 1);
946  size,
947  str_buf,
948  len);
949  if (NULL == end)
950  {
951  GNUNET_free (str_buf);
952  return NULL;
953  }
954  *end = '\0';
955  return str_buf;
956 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static char buf[2048]
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_malloc(size)
Wrapper around malloc.
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 Crockford Base32 encoding.
Definition: strings.c:870
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_string_to_data()

int 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

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

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

Referenced by check_authorization(), conversation_string_to_value(), delete_attestation_cont(), delete_attribute_cont(), ego_create(), 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_HELLO_parse_uri(), lookup_redirect_uri_result(), parse_attest(), parse_attr(), parse_fixed_data(), parse_rsa_public_key(), parse_rsa_signature(), parse_ticket(), parse_variable_data(), read_service_conf(), run(), set_base32(), start_process(), string_to_auth_key(), string_to_value(), uri_loc_parse(), and uri_sks_parse().

974 {
975  size_t rpos;
976  size_t wpos;
977  unsigned int bits;
978  unsigned int vbit;
979  int ret;
980  int shift;
981  unsigned char *uout;
982  size_t encoded_len;
983 
984  if (0 == enclen)
985  {
986  if (0 == out_size)
987  return GNUNET_OK;
988  return GNUNET_SYSERR;
989  }
990  GNUNET_assert (out_size < SIZE_MAX / 8);
991  encoded_len = out_size * 8;
992  uout = out;
993  wpos = out_size;
994  rpos = enclen;
995  if ((encoded_len % 5) > 0)
996  {
997  vbit = encoded_len % 5; /* padding! */
998  shift = 5 - vbit;
999  bits = (ret = getValue__ (enc[--rpos])) >> shift;
1000  }
1001  else
1002  {
1003  vbit = 5;
1004  shift = 0;
1005  bits = (ret = getValue__ (enc[--rpos]));
1006  }
1007  if ((encoded_len + shift) / 5 != enclen)
1008  return GNUNET_SYSERR;
1009  if (-1 == ret)
1010  return GNUNET_SYSERR;
1011  while (wpos > 0)
1012  {
1013  if (0 == rpos)
1014  {
1015  GNUNET_break (0);
1016  return GNUNET_SYSERR;
1017  }
1018  bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
1019  if (-1 == ret)
1020  return GNUNET_SYSERR;
1021  vbit += 5;
1022  if (vbit >= 8)
1023  {
1024  uout[--wpos] = (unsigned char) bits;
1025  bits >>= 8;
1026  vbit -= 8;
1027  }
1028  }
1029  if ((0 != rpos) || (0 != vbit))
1030  return GNUNET_SYSERR;
1031  return GNUNET_OK;
1032 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define SIZE_MAX
Definition: platform.h:220
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static unsigned int getValue__(unsigned char a)
Get the decoded value corresponding to a character according to Crockford Base32 encoding.
Definition: strings.c:808
static OpusEncoder * enc
OPUS encoder.
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
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 1874 of file strings.c.

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

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

1875 {
1876  const char *data = in;
1877  size_t ret;
1878  char *opt;
1879 
1880  ret = 0;
1881  GNUNET_assert (len / 4 < SIZE_MAX);
1882  opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1883  for (size_t i = 0; i < len; ++i)
1884  {
1885  char c;
1886 
1887  c = (data[i] >> 2) & 0x3f;
1888  opt[ret++] = cvt[(int) c];
1889  c = (data[i] << 4) & 0x3f;
1890  if (++i < len)
1891  c |= (data[i] >> 4) & 0x0f;
1892  opt[ret++] = cvt[(int) c];
1893  if (i < len)
1894  {
1895  c = (data[i] << 2) & 0x3f;
1896  if (++i < len)
1897  c |= (data[i] >> 6) & 0x03;
1898  opt[ret++] = cvt[(int) c];
1899  }
1900  else
1901  {
1902  ++i;
1903  opt[ret++] = FILLCHAR;
1904  }
1905  if (i < len)
1906  {
1907  c = data[i] & 0x3f;
1908  opt[ret++] = cvt[(int) c];
1909  }
1910  else
1911  {
1912  opt[ret++] = FILLCHAR;
1913  }
1914  }
1915  *output = opt;
1916  return ret;
1917 }
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1858
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define SIZE_MAX
Definition: platform.h:220
static char * cvt
Definition: strings.c:1859
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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 1930 of file strings.c.

References enc, and GNUNET_STRINGS_base64_encode().

Referenced by OIDC_build_authz_code(), OIDC_id_token_new(), and OIDC_parse_authz_code().

1931 {
1932  char *enc;
1933  size_t pos;
1934 
1935  GNUNET_STRINGS_base64_encode (in, len, output);
1936  enc = *output;
1937  /* Replace with correct characters for base64url */
1938  pos = 0;
1939  while ('\0' != enc[pos])
1940  {
1941  if ('+' == enc[pos])
1942  enc[pos] = '-';
1943  if ('/' == enc[pos])
1944  enc[pos] = '_';
1945  if ('=' == enc[pos])
1946  {
1947  enc[pos] = '\0';
1948  break;
1949  }
1950  pos++;
1951  }
1952  return strlen (enc);
1953 }
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1874
static OpusEncoder * enc
OPUS encoder.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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 **  out 
)

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

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

Referenced by check_authorization(), database_setup(), dns_string_to_value(), GNUNET_ABD_delegate_from_string(), GNUNET_HELLO_extract_address(), GNUNET_STRINGS_base64url_decode(), listenAndDistribute(), and parse_jwt().

1977 {
1978  char *output;
1979  size_t ret = 0;
1980 
1981 #define CHECK_CRLF \
1982  while (data[i] == '\r' || data[i] == '\n') \
1983  { \
1984  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
1985  "ignoring CR/LF\n"); \
1986  i++; \
1987  if (i >= len) \
1988  goto END; \
1989  }
1990 
1991  GNUNET_assert (len / 3 < SIZE_MAX);
1992  output = GNUNET_malloc ((len * 3 / 4) + 8);
1994  "base64_decode decoding len=%d\n",
1995  (int) len);
1996  for (size_t i = 0; i < len; ++i)
1997  {
1998  char c;
1999  char c1;
2000 
2001  CHECK_CRLF;
2002  if (FILLCHAR == data[i])
2003  break;
2004  c = (char) cvtfind (data[i]);
2005  ++i;
2006  CHECK_CRLF;
2007  c1 = (char) cvtfind (data[i]);
2008  c = (c << 2) | ((c1 >> 4) & 0x3);
2009  output[ret++] = c;
2010  if (++i < len)
2011  {
2012  CHECK_CRLF;
2013  c = data[i];
2014  if (FILLCHAR == c)
2015  break;
2016  c = (char) cvtfind (c);
2017  c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
2018  output[ret++] = c1;
2019  }
2020  if (++i < len)
2021  {
2022  CHECK_CRLF;
2023  c1 = data[i];
2024  if (FILLCHAR == c1)
2025  break;
2026 
2027  c1 = (char) cvtfind (c1);
2028  c = ((c << 6) & 0xc0) | c1;
2029  output[ret++] = c;
2030  }
2031  }
2032 END:
2033  *out = output;
2034  return ret;
2035 }
#define CHECK_CRLF
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1858
#define cvtfind(a)
Definition: strings.c:1956
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
We&#39;re done processing.
#define SIZE_MAX
Definition: platform.h:220
#define GNUNET_log(kind,...)
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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
outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 2048 of file strings.c.

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

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

2049 {
2050  char *s;
2051  int padding;
2052  size_t ret;
2053 
2054  /* make enough space for padding */
2055  GNUNET_assert (len < SIZE_MAX - 3);
2056  s = GNUNET_malloc (len + 3);
2057  memcpy (s, data, len);
2058 
2059  for (int i = 0; i < strlen (s); i++)
2060  {
2061  if (s[i] == '-')
2062  s[i] = '+';
2063  if (s[i] == '_')
2064  s[i] = '/';
2065  }
2066  padding = len % 4;
2067  switch (padding) // Pad with trailing '='s
2068  {
2069  case 0:
2070  break; // No pad chars in this case
2071  case 2:
2072  memcpy (&s[len],
2073  "==",
2074  2);
2075  len += 2;
2076  break; // Two pad chars
2077  case 3:
2078  s[len] = '=';
2079  len++;
2080  break; // One pad char
2081  default:
2082  GNUNET_assert (0);
2083  break;
2084  }
2085  ret = GNUNET_STRINGS_base64_decode (s, len, out);
2086  GNUNET_free (s);
2087  return ret;
2088 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define SIZE_MAX
Definition: platform.h:220
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out)
Decode from Base64.
Definition: strings.c:1976
uint32_t data
The data value.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_pp2s()

char* GNUNET_STRINGS_pp2s ( const struct GNUNET_PeerIdentity pids,
unsigned int  num_pids 
)

Convert a peer path to a human-readable string.

Parameters
pidsarray of PIDs to convert to a string
num_pidslength of the pids array
Returns
string representing the array of pids

Definition at line 100 of file strings.c.

References buf, GNUNET_assert, GNUNET_i2s(), GNUNET_malloc, and GNUNET_snprintf().

Referenced by handle_dht_p2p_put(), handle_dht_p2p_result(), handle_local_result(), and process_client_result().

102 {
103  char *buf;
104  size_t off;
105  size_t plen = num_pids * 5 + 1;
106 
107  GNUNET_assert (num_pids < UINT32_MAX / 5);
108  off = 0;
109  buf = GNUNET_malloc (plen);
110  for (unsigned int i = 0; i < num_pids; i++)
111  {
112  off += GNUNET_snprintf (&buf[off],
113  plen - off,
114  "%s%s",
115  GNUNET_i2s (&pids[i]),
116  (i == num_pids - 1) ? "" : "-");
117  }
118  return buf;
119 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static char buf[2048]
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_uri()

int 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).
Parameters
pathpath to parse. Must be NULL-terminated.
scheme_parta pointer to 'char *' where a pointer 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 1052 of file strings.c.

References end, GNUNET_malloc, GNUNET_memcpy, GNUNET_NO, GNUNET_YES, and len.

Referenced by GNUNET_STRINGS_path_is_absolute().

1055 {
1056  size_t len;
1057  size_t i;
1058  int end;
1059  int pp_state = 0;
1060  const char *post_scheme_part = NULL;
1061 
1062  len = strlen (path);
1063  for (end = 0, i = 0; ! end && i < len; i++)
1064  {
1065  switch (pp_state)
1066  {
1067  case 0:
1068  if ((path[i] == ':') && (i > 0))
1069  {
1070  pp_state += 1;
1071  continue;
1072  }
1073  if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
1074  ((path[i] >= 'a') && (path[i] <= 'z') ) ||
1075  ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
1076  (path[i] == '-') || (path[i] == '.')))
1077  end = 1;
1078  break;
1079 
1080  case 1:
1081  case 2:
1082  if (path[i] == '/')
1083  {
1084  pp_state += 1;
1085  continue;
1086  }
1087  end = 1;
1088  break;
1089 
1090  case 3:
1091  post_scheme_part = &path[i];
1092  end = 1;
1093  break;
1094 
1095  default:
1096  end = 1;
1097  }
1098  }
1099  if (post_scheme_part == NULL)
1100  return GNUNET_NO;
1101  if (scheme_part)
1102  {
1103  *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
1104  GNUNET_memcpy (*scheme_part, path, post_scheme_part - path);
1105  (*scheme_part)[post_scheme_part - path] = '\0';
1106  }
1107  if (path_part)
1108  *path_part = post_scheme_part;
1109  return GNUNET_YES;
1110 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function:

◆ GNUNET_STRINGS_path_is_absolute()

int 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.

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

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

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

1132 {
1133  const char *post_scheme_path;
1134  int is_uri;
1135  char *uri;
1136  /* consider POSIX paths to be absolute too, even on W32,
1137  * as plibc expansion will fix them for us.
1138  */
1139  if (filename[0] == '/')
1140  return GNUNET_YES;
1141  if (can_be_uri)
1142  {
1143  is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1144  if (r_is_uri)
1145  *r_is_uri = is_uri;
1146  if (is_uri)
1147  {
1148  if (r_uri_scheme)
1149  *r_uri_scheme = uri;
1150  else
1151  GNUNET_free (uri);
1152 
1153  return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1154  GNUNET_NO,
1155  NULL,
1156  NULL);
1157  }
1158  }
1159  else
1160  {
1161  if (r_is_uri)
1162  *r_is_uri = GNUNET_NO;
1163  }
1164 
1165  return GNUNET_NO;
1166 }
int 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:1052
int 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&#39;s an URI.
Definition: strings.c:1128
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
static char * filename
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_check_filename()

int 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

Perform checks on filename.

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

References 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, and S_ISLNK.

1180 {
1181  struct stat st;
1182 
1183  if ((NULL == filename) || (filename[0] == '\0'))
1184  return GNUNET_SYSERR;
1185  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1186  if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
1187  return GNUNET_NO;
1188  if (0 != (checks
1191  {
1192  if (0 != lstat (filename, &st))
1193  {
1194  if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1195  return GNUNET_NO;
1196  else
1197  return GNUNET_SYSERR;
1198  }
1199  }
1200  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1201  if (! S_ISDIR (st.st_mode))
1202  return GNUNET_NO;
1203  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1204  if (! S_ISLNK (st.st_mode))
1205  return GNUNET_NO;
1206  return GNUNET_YES;
1207 }
int 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&#39;s an URI.
Definition: strings.c:1128
Check that the path is an absolute path.
Check that it exists.
static char * filename
#define S_ISLNK(m)
Definition: disk.c:61
Check that it is a link.
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.
Check that it is a directory.
Here is the call graph for this function:

◆ GNUNET_STRINGS_to_address_ipv6()

int GNUNET_STRINGS_to_address_ipv6 ( const char *  zt_addr,
uint16_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 succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

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 succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1223 of file strings.c.

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

1226 {
1227  char zbuf[addrlen + 1];
1228  int ret;
1229  char *port_colon;
1230  unsigned int port;
1231  char dummy[2];
1232 
1233  if (addrlen < 6)
1234  return GNUNET_SYSERR;
1235  GNUNET_memcpy (zbuf, zt_addr, addrlen);
1236  if ('[' != zbuf[0])
1237  {
1239  _ ("IPv6 address did not start with `['\n"));
1240  return GNUNET_SYSERR;
1241  }
1242  zbuf[addrlen] = '\0';
1243  port_colon = strrchr (zbuf, ':');
1244  if (NULL == port_colon)
1245  {
1247  _ ("IPv6 address did contain ':' to separate port number\n"));
1248  return GNUNET_SYSERR;
1249  }
1250  if (']' != *(port_colon - 1))
1251  {
1252  GNUNET_log (
1254  _ ("IPv6 address did contain ']' before ':' to separate port number\n"));
1255  return GNUNET_SYSERR;
1256  }
1257  ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1258  if ((1 != ret) || (port > 65535))
1259  {
1260  GNUNET_log (
1262  _ ("IPv6 address did contain a valid port number after the last ':'\n"));
1263  return GNUNET_SYSERR;
1264  }
1265  *(port_colon - 1) = '\0';
1266  memset (r_buf, 0, sizeof(struct sockaddr_in6));
1267  ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1268  if (ret <= 0)
1269  {
1271  _ ("Invalid IPv6 address `%s': %s\n"),
1272  &zbuf[1],
1273  strerror (errno));
1274  return GNUNET_SYSERR;
1275  }
1276  r_buf->sin6_port = htons (port);
1277  r_buf->sin6_family = AF_INET6;
1278 #if HAVE_SOCKADDR_IN_SIN_LEN
1279  r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1280 #endif
1281  return GNUNET_OK;
1282 }
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:82
#define GNUNET_log(kind,...)
Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_address_ipv4()

int GNUNET_STRINGS_to_address_ipv4 ( const char *  zt_addr,
uint16_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 succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

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 succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1297 of file strings.c.

References dummy, GNUNET_OK, GNUNET_SYSERR, and port.

Referenced by GNUNET_STRINGS_parse_socket_addr(), and GNUNET_STRINGS_to_address_ip().

1300 {
1301  unsigned int temps[4];
1302  unsigned int port;
1303  unsigned int cnt;
1304  char dummy[2];
1305 
1306  if (addrlen < 9)
1307  return GNUNET_SYSERR;
1308  cnt = sscanf (zt_addr,
1309  "%u.%u.%u.%u:%u%1s",
1310  &temps[0],
1311  &temps[1],
1312  &temps[2],
1313  &temps[3],
1314  &port,
1315  dummy);
1316  if (5 != cnt)
1317  return GNUNET_SYSERR;
1318  for (cnt = 0; cnt < 4; cnt++)
1319  if (temps[cnt] > 0xFF)
1320  return GNUNET_SYSERR;
1321  if (port > 65535)
1322  return GNUNET_SYSERR;
1323  r_buf->sin_family = AF_INET;
1324  r_buf->sin_port = htons (port);
1325  r_buf->sin_addr.s_addr =
1326  htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1327 #if HAVE_SOCKADDR_IN_SIN_LEN
1328  r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1329 #endif
1330  return GNUNET_OK;
1331 }
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static uint16_t port
Port number.
Definition: gnunet-bcd.c:82
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 (i.e. AF_INET)
[out]saset to the parsed address
Returns
0 on error, otherwise number of bytes in sa

Definition at line 1370 of file strings.c.

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

Referenced by run().

1373 {
1374  char *cp = GNUNET_strdup (addr);
1375 
1376  *af = AF_UNSPEC;
1377  if ('[' == *addr)
1378  {
1379  /* IPv6 */
1380  *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1381  if (GNUNET_OK !=
1383  strlen (cp),
1384  (struct sockaddr_in6 *) *sa))
1385  {
1386  GNUNET_free (*sa);
1387  *sa = NULL;
1388  GNUNET_free (cp);
1389  return 0;
1390  }
1391  *af = AF_INET6;
1392  GNUNET_free (cp);
1393  return sizeof(struct sockaddr_in6);
1394  }
1395  else
1396  {
1397  /* IPv4 */
1398  *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1399  if (GNUNET_OK !=
1401  strlen (cp),
1402  (struct sockaddr_in *) *sa))
1403  {
1404  GNUNET_free (*sa);
1405  *sa = NULL;
1406  GNUNET_free (cp);
1407  return 0;
1408  }
1409  *af = AF_INET;
1410  GNUNET_free (cp);
1411  return sizeof(struct sockaddr_in);
1412  }
1413 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
int GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, uint16_t addrlen, struct sockaddr_in *r_buf)
Tries to convert &#39;zt_addr&#39; string to an IPv4 address.
Definition: strings.c:1297
int GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
Tries to convert zt_addr string to an IPv6 address.
Definition: strings.c:1223
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_address_ip()

int 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 succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

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 succeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1346 of file strings.c.

References GNUNET_STRINGS_to_address_ipv4(), and GNUNET_STRINGS_to_address_ipv6().

Referenced by http_common_socket_from_address(), tcp_plugin_string_to_address(), and udp_string_to_address().

1349 {
1350  if (addr[0] == '[')
1351  return GNUNET_STRINGS_to_address_ipv6 (addr,
1352  addrlen,
1353  (struct sockaddr_in6 *) r_buf);
1354  return GNUNET_STRINGS_to_address_ipv4 (addr,
1355  addrlen,
1356  (struct sockaddr_in *) r_buf);
1357 }
int GNUNET_STRINGS_to_address_ipv4(const char *zt_addr, uint16_t addrlen, struct sockaddr_in *r_buf)
Tries to convert &#39;zt_addr&#39; string to an IPv4 address.
Definition: strings.c:1297
int GNUNET_STRINGS_to_address_ipv6(const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
Tries to convert zt_addr string to an IPv6 address.
Definition: strings.c:1223
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_utf8_args()

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

Returns utf-8 encoded arguments.

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

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

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

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

Definition at line 1457 of file strings.c.

References _make_continuous_arg_copy(), and GNUNET_OK.

Referenced by main().

1461 {
1462  char *const *new_argv =
1463  (char *const *) _make_continuous_arg_copy (argc, argv);
1464  *u8argv = new_argv;
1465  *u8argc = argc;
1466  return GNUNET_OK;
1467 }
static char *const * _make_continuous_arg_copy(int argc, char *const *argv)
Makes a copy of argv that consists of a single memory chunk that can be freed with a single call to G...
Definition: strings.c:1421
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_strlcpy()

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

Like strlcpy but portable.

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

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

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

The given string src is copied until its null byte or until n - 1 bytes have been read. The destination buffer is guaranteed to be null-terminated.

Parameters
dstdestination of the copy (must be n bytes long)
srcsource of the copy (at most n - 1 bytes will be read)
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 220 of file strings.c.

References GNUNET_assert, and strnlen().

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

221 {
222  size_t slen;
223 
224  GNUNET_assert (0 != n);
225  slen = strnlen (src, n - 1);
226  memcpy (dst, src, slen);
227  dst[slen] = '\0';
228  return slen;
229 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static size_t strnlen(const char *s, size_t n)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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

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

References _, dummy, end, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_strdup, len, LOG, GNUNET_STRINGS_IPv4NetworkPolicy::netmask, GNUNET_STRINGS_IPv4NetworkPolicy::network, parse_port_policy(), GNUNET_STRINGS_IPv4NetworkPolicy::pp, and result.

Referenced by GNUNET_TUN_ipv4policy2regex(), and process_acl4().

1538 {
1539  unsigned int count;
1540  unsigned int i;
1541  unsigned int j;
1542  unsigned int len;
1543  int cnt;
1544  unsigned int pos;
1545  unsigned int temps[8];
1546  int slash;
1548  int colon;
1549  int end;
1550  char *routeList;
1551  char dummy[2];
1552 
1553  if (NULL == routeListX)
1554  return NULL;
1555  len = strlen (routeListX);
1556  if (0 == len)
1557  return NULL;
1558  routeList = GNUNET_strdup (routeListX);
1559  count = 0;
1560  for (i = 0; i < len; i++)
1561  if (routeList[i] == ';')
1562  count++;
1563  result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy)
1564  * (count + 1));
1565  i = 0;
1566  pos = 0;
1567  while (i < count)
1568  {
1569  for (colon = pos; ':' != routeList[colon]; colon++)
1570  if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1571  break;
1572  for (end = colon; ';' != routeList[end]; end++)
1573  if ('\0' == routeList[end])
1574  break;
1575  if ('\0' == routeList[end])
1576  break;
1577  routeList[end] = '\0';
1578  if (':' == routeList[colon])
1579  {
1580  routeList[colon] = '\0';
1581  if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1582  break;
1583  }
1584  cnt = sscanf (&routeList[pos],
1585  "%u.%u.%u.%u/%u.%u.%u.%u%1s",
1586  &temps[0],
1587  &temps[1],
1588  &temps[2],
1589  &temps[3],
1590  &temps[4],
1591  &temps[5],
1592  &temps[6],
1593  &temps[7],
1594  dummy);
1595  if (8 == cnt)
1596  {
1597  for (j = 0; j < 8; j++)
1598  if (temps[j] > 0xFF)
1599  {
1601  _ ("Invalid format for IP: `%s'\n"),
1602  &routeList[pos]);
1603  GNUNET_free (result);
1604  GNUNET_free (routeList);
1605  return NULL;
1606  }
1607  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1608  + (temps[2] << 8) + temps[3]);
1609  result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1610  + (temps[6] << 8) + temps[7]);
1611  pos = end + 1;
1612  i++;
1613  continue;
1614  }
1615  /* try second notation */
1616  cnt = sscanf (&routeList[pos],
1617  "%u.%u.%u.%u/%u%1s",
1618  &temps[0],
1619  &temps[1],
1620  &temps[2],
1621  &temps[3],
1622  &slash,
1623  dummy);
1624  if (5 == cnt)
1625  {
1626  for (j = 0; j < 4; j++)
1627  if (temps[j] > 0xFF)
1628  {
1630  _ ("Invalid format for IP: `%s'\n"),
1631  &routeList[pos]);
1632  GNUNET_free (result);
1633  GNUNET_free (routeList);
1634  return NULL;
1635  }
1636  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1637  + (temps[2] << 8) + temps[3]);
1638  if ((slash <= 32) && (slash >= 0))
1639  {
1640  result[i].netmask.s_addr = 0;
1641  while (slash > 0)
1642  {
1643  result[i].netmask.s_addr =
1644  (result[i].netmask.s_addr >> 1) + 0x80000000;
1645  slash--;
1646  }
1647  result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1648  pos = end + 1;
1649  i++;
1650  continue;
1651  }
1652  else
1653  {
1655  _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1656  slash);
1657  GNUNET_free (result);
1658  GNUNET_free (routeList);
1659  return NULL; /* error */
1660  }
1661  }
1662  /* try third notation */
1663  slash = 32;
1664  cnt = sscanf (&routeList[pos],
1665  "%u.%u.%u.%u%1s",
1666  &temps[0],
1667  &temps[1],
1668  &temps[2],
1669  &temps[3],
1670  dummy);
1671  if (4 == cnt)
1672  {
1673  for (j = 0; j < 4; j++)
1674  if (temps[j] > 0xFF)
1675  {
1677  _ ("Invalid format for IP: `%s'\n"),
1678  &routeList[pos]);
1679  GNUNET_free (result);
1680  GNUNET_free (routeList);
1681  return NULL;
1682  }
1683  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1684  + (temps[2] << 8) + temps[3]);
1685  result[i].netmask.s_addr = 0;
1686  while (slash > 0)
1687  {
1688  result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
1689  slash--;
1690  }
1691  result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1692  pos = end + 1;
1693  i++;
1694  continue;
1695  }
1697  _ ("Invalid format for IP: `%s'\n"),
1698  &routeList[pos]);
1699  GNUNET_free (result);
1700  GNUNET_free (routeList);
1701  return NULL; /* error */
1702  }
1703  if (pos < strlen (routeList))
1704  {
1706  _ ("Invalid format: `%s'\n"),
1707  &routeListX[pos]);
1708  GNUNET_free (result);
1709  GNUNET_free (routeList);
1710  return NULL; /* oops */
1711  }
1712  GNUNET_free (routeList);
1713  return result; /* ok */
1714 }
static int parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1480
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
struct in_addr netmask
IPv4 netmask.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
struct in_addr network
IPv4 address.
IPV4 network in CIDR notation.
static int result
Global testing status.
#define LOG(kind,...)
Definition: strings.c:37
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
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

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

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

Referenced by GNUNET_TUN_ipv6policy2regex(), and process_acl6().

1730 {
1731  unsigned int count;
1732  unsigned int i;
1733  unsigned int len;
1734  unsigned int pos;
1735  int start;
1736  int slash;
1737  int ret;
1738  char *routeList;
1740  unsigned int bits;
1741  unsigned int off;
1742  int save;
1743  int colon;
1744  char dummy[2];
1745 
1746  if (NULL == routeListX)
1747  return NULL;
1748  len = strlen (routeListX);
1749  if (0 == len)
1750  return NULL;
1751  routeList = GNUNET_strdup (routeListX);
1752  count = 0;
1753  for (i = 0; i < len; i++)
1754  if (';' == routeList[i])
1755  count++;
1756  if (';' != routeList[len - 1])
1757  {
1759  _ ("Invalid network notation (does not end with ';': `%s')\n"),
1760  routeList);
1761  GNUNET_free (routeList);
1762  return NULL;
1763  }
1764 
1765  result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy)
1766  * (count + 1));
1767  i = 0;
1768  pos = 0;
1769  while (i < count)
1770  {
1771  start = pos;
1772  while (';' != routeList[pos])
1773  pos++;
1774  slash = pos;
1775  while ((slash >= start) && (routeList[slash] != '/'))
1776  slash--;
1777 
1778  if (slash < start)
1779  {
1780  memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr));
1781  slash = pos;
1782  }
1783  else
1784  {
1785  routeList[pos] = '\0';
1786  for (colon = pos; ':' != routeList[colon]; colon--)
1787  if ('/' == routeList[colon])
1788  break;
1789  if (':' == routeList[colon])
1790  {
1791  routeList[colon] = '\0';
1792  if (GNUNET_OK !=
1793  parse_port_policy (&routeList[colon + 1], &result[i].pp))
1794  {
1795  GNUNET_free (result);
1796  GNUNET_free (routeList);
1797  return NULL;
1798  }
1799  }
1800  ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
1801  if (ret <= 0)
1802  {
1803  save = errno;
1804  if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1805  (bits > 128))
1806  {
1807  if (0 == ret)
1809  _ ("Wrong format `%s' for netmask\n"),
1810  &routeList[slash + 1]);
1811  else
1812  {
1813  errno = save;
1814  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1815  }
1816  GNUNET_free (result);
1817  GNUNET_free (routeList);
1818  return NULL;
1819  }
1820  off = 0;
1821  while (bits > 8)
1822  {
1823  result[i].netmask.s6_addr[off++] = 0xFF;
1824  bits -= 8;
1825  }
1826  while (bits > 0)
1827  {
1828  result[i].netmask.s6_addr[off] =
1829  (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1830  bits--;
1831  }
1832  }
1833  }
1834  routeList[slash] = '\0';
1835  ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
1836  if (ret <= 0)
1837  {
1838  if (0 == ret)
1840  _ ("Wrong format `%s' for network\n"),
1841  &routeList[slash + 1]);
1842  else
1843  LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
1844  GNUNET_free (result);
1845  GNUNET_free (routeList);
1846  return NULL;
1847  }
1848  pos++;
1849  i++;
1850  }
1851  GNUNET_free (routeList);
1852  return result;
1853 }
static int parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1480
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
static int start
Set if we are to start default services (including ARM).
Definition: gnunet-arm.c:39
network in CIDR notation for IPV6.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static void save()
Write persistent statistics to disk.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
struct in6_addr netmask
IPv6 netmask.
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
static int result
Global testing status.
#define LOG(kind,...)
Definition: strings.c:37
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39
struct in6_addr network
IPv6 address.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function: