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_urlencode (const char *data, size_t 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...
 
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 475 of file gnunet_strings_lib.h.

476 {
480  GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
481 
486 
490  GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
491 
496 };
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 324 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().

326 {
327  static const struct ConversionTable table[] =
328  { { "B", 1 },
329  { "KiB", 1024 },
330  { "kB", 1000 },
331  { "MiB", 1024 * 1024 },
332  { "MB", 1000 * 1000 },
333  { "GiB", 1024 * 1024 * 1024 },
334  { "GB", 1000 * 1000 * 1000 },
335  { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
336  { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
337  { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
338  { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
339  { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
340  { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
341  { NULL, 0 } };
342 
343  return convert_with_table (fancy_size, table, size);
344 }
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:262
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:236
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 356 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().

358 {
359  static const struct ConversionTable table[] =
360  { { "us", 1 },
361  { "ms", 1000 },
362  { "s", 1000 * 1000LL },
363  { "second", 1000 * 1000LL },
364  { "seconds", 1000 * 1000LL },
365  { "\"", 1000 * 1000LL },
366  { "m", 60 * 1000 * 1000LL },
367  { "min", 60 * 1000 * 1000LL },
368  { "minute", 60 * 1000 * 1000LL },
369  { "minutes", 60 * 1000 * 1000LL },
370  { "'", 60 * 1000 * 1000LL },
371  { "h", 60 * 60 * 1000 * 1000LL },
372  { "hour", 60 * 60 * 1000 * 1000LL },
373  { "hours", 60 * 60 * 1000 * 1000LL },
374  { "d", 24 * 60 * 60 * 1000LL * 1000LL },
375  { "day", 24 * 60 * 60 * 1000LL * 1000LL },
376  { "days", 24 * 60 * 60 * 1000LL * 1000LL },
377  { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
378  { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
379  { "year", 31536000000000LL /* year */ },
380  { "years", 31536000000000LL /* year */ },
381  { "a", 31536000000000LL /* year */ },
382  { NULL, 0 } };
383  int ret;
384  unsigned long long val;
385 
386  if (0 == strcasecmp ("forever", fancy_time))
387  {
389  return GNUNET_OK;
390  }
391  ret = convert_with_table (fancy_time, table, &val);
392  rtime->rel_value_us = (uint64_t) val;
393  return ret;
394 }
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:262
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:236
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 177 of file strings.c.

References GNUNET_malloc, GNUNET_snprintf(), and ret.

Referenced by progress_cb(), and publish_inspector().

178 {
179  const char *unit = /* size unit */ "b";
180  char *ret;
181 
182  if (size > 5 * 1024)
183  {
184  size = size / 1024;
185  unit = "KiB";
186  if (size > 5 * 1024)
187  {
188  size = size / 1024;
189  unit = "MiB";
190  if (size > 5 * 1024)
191  {
192  size = size / 1024;
193  unit = "GiB";
194  if (size > 5 * 1024)
195  {
196  size = size / 1024;
197  unit = "TiB";
198  }
199  }
200  }
201  }
202  ret = GNUNET_malloc (32);
203  GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
204  return ret;
205 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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.
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 original string is returned.

Definition at line 452 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().

456 {
457  char *ret;
458  uint8_t *u8_string;
459  char *encoded_string;
460  size_t u8_string_length;
461  size_t encoded_string_length;
462 
463  u8_string = u8_conv_from_encoding (input_charset,
464  iconveh_error,
465  input,
466  len,
467  NULL,
468  NULL,
469  &u8_string_length);
470  if (NULL == u8_string)
471  {
472  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
473  goto fail;
474  }
475  if (0 == strcmp (output_charset, "UTF-8"))
476  {
477  ret = GNUNET_malloc (u8_string_length + 1);
478  GNUNET_memcpy (ret, u8_string, u8_string_length);
479  ret[u8_string_length] = '\0';
480  free (u8_string);
481  return ret;
482  }
483  encoded_string = u8_conv_to_encoding (output_charset,
484  iconveh_error,
485  u8_string,
486  u8_string_length,
487  NULL,
488  NULL,
489  &encoded_string_length);
490  free (u8_string);
491  if (NULL == encoded_string)
492  {
493  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
494  goto fail;
495  }
496  ret = GNUNET_malloc (encoded_string_length + 1);
497  GNUNET_memcpy (ret, encoded_string, encoded_string_length);
498  ret[encoded_string_length] = '\0';
499  free (encoded_string);
500  return ret;
501 fail:
503  _ ("Character sets requested were `%s'->`%s'\n"),
504  "UTF-8",
505  output_charset);
506  ret = GNUNET_malloc (len + 1);
507  GNUNET_memcpy (ret, input, len);
508  ret[len] = '\0';
509  return ret;
510 }
#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:38
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:40
#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 original string is returned.

Definition at line 526 of file strings.c.

References GNUNET_STRINGS_conv().

529 {
530  return GNUNET_STRINGS_conv (input,
531  len,
532  charset,
533  "UTF-8");
534 }
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:452
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 original string is returned.

Definition at line 549 of file strings.c.

References GNUNET_STRINGS_conv().

552 {
553  return GNUNET_STRINGS_conv (input,
554  len,
555  "UTF-8",
556  charset);
557 }
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:452
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 568 of file strings.c.

References GNUNET_memcpy, and len.

Referenced by ego_create(), GNUNET_GNSRECORD_string_to_lowercase(), GNUNET_RECLAIM_attribute_new(), GNUNET_RECLAIM_credential_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().

569 {
570  uint8_t *tmp_in;
571  size_t len;
572 
573  tmp_in = u8_tolower ((uint8_t *) input,
574  strlen ((char *) input),
575  NULL,
576  UNINORM_NFD,
577  NULL,
578  &len);
579  GNUNET_memcpy (output, tmp_in, len);
580  output[len] = '\0';
581  free (tmp_in);
582 }
#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 593 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().

594 {
595  uint8_t *tmp_in;
596  size_t len;
597 
598  tmp_in = u8_toupper ((uint8_t *) input,
599  strlen ((char *) input),
600  NULL,
601  UNINORM_NFD,
602  NULL,
603  &len);
604  GNUNET_memcpy (output, tmp_in, len);
605  output[len] = '\0';
606  free (tmp_in);
607 }
#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 618 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_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_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().

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

References GNUNET_assert, and GNUNET_memcpy.

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

69 {
70  size_t needed;
71  va_list ap;
72 
73  needed = 0;
74  va_start (ap, count);
75  while (count > 0)
76  {
77  const char *s = va_arg (ap, const char *);
78  size_t slen = strlen (s) + 1;
79 
80  GNUNET_assert (slen <= size - needed);
81  if (NULL != buffer)
82  GNUNET_memcpy (&buffer[needed],
83  s,
84  slen);
85  needed += slen;
86  count--;
87  }
88  va_end (ap);
89  return needed;
90 }
#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 137 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().

141 {
142  unsigned int start;
143  unsigned int needed;
144  const char **r;
145  va_list ap;
146 
147  needed = 0;
148  va_start (ap, count);
149  while (count > 0)
150  {
151  r = va_arg (ap, const char **);
152 
153  start = needed;
154  while ((needed < size) && (buffer[needed] != '\0'))
155  needed++;
156  if (needed == size)
157  {
158  va_end (ap);
159  return 0; /* error */
160  }
161  *r = &buffer[start];
162  needed++; /* skip 0-termination */
163  count--;
164  }
165  va_end (ap);
166  return needed;
167 }
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 791 of file strings.c.

References DIR_SEPARATOR_STR, and filename.

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

792 {
793  const char *short_fn = filename;
794  const char *ss;
795 
796  while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
797  short_fn = 1 + ss;
798  return short_fn;
799 }
#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 872 of file strings.c.

References data, GNUNET_assert, GNUNET_break, and SIZE_MAX.

Referenced by auth_key_to_string(), GNUNET_buffer_write_data_encoded(), GNUNET_CRYPTO_ecdsa_private_key_to_string(), GNUNET_CRYPTO_ecdsa_public_key_to_string(), GNUNET_CRYPTO_eddsa_private_key_to_string(), GNUNET_CRYPTO_eddsa_public_key_to_string(), GNUNET_CRYPTO_hash_to_enc(), GNUNET_GNSRECORD_z2s(), GNUNET_sh2s(), GNUNET_STRINGS_data_to_string_alloc(), GNUNET_uuid2s(), output_message(), print_examples_ecdh(), run(), secret_ready_cb(), uri_loc_to_string(), and uri_sks_to_string().

876 {
880  static char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
881  unsigned int wpos;
882  unsigned int rpos;
883  unsigned int bits;
884  unsigned int vbit;
885  const unsigned char *udata;
886 
887  GNUNET_assert (size < SIZE_MAX / 8 - 4);
888  udata = data;
889  if (out_size < (size * 8 + 4) / 5)
890  {
891  GNUNET_break (0);
892  return NULL;
893  }
894  vbit = 0;
895  wpos = 0;
896  rpos = 0;
897  bits = 0;
898  while ((rpos < size) || (vbit > 0))
899  {
900  if ((rpos < size) && (vbit < 5))
901  {
902  bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
903  vbit += 8;
904  }
905  if (vbit < 5)
906  {
907  bits <<= (5 - vbit); /* zero-padding */
908  GNUNET_assert (vbit == ((size * 8) % 5));
909  vbit = 5;
910  }
911  if (wpos >= out_size)
912  {
913  GNUNET_break (0);
914  return NULL;
915  }
916  out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
917  vbit -= 5;
918  }
919  GNUNET_assert (0 == vbit);
920  if (wpos < out_size)
921  out[wpos] = '\0';
922  return &out[wpos];
923 }
#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 937 of file strings.c.

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

Referenced by attr_collect(), attr_store_task(), conversation_value_to_string(), cred_collect(), cred_iter_cb(), cred_store_task(), display_data(), generate_userinfo_json(), GNUNET_IDENTITY_private_key_to_string(), GNUNET_IDENTITY_public_key_to_string(), GNUNET_JSON_from_data(), handle_attribute_delete_message(), handle_credential_delete_message(), issue_ticket(), iter_cb(), iterate_save_rooms(), lookup_authz_cb(), move_attr_finished(), move_attrs(), OIDC_generate_id_token(), 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().

938 {
939  char *str_buf;
940  size_t len = size * 8;
941  char *end;
942 
943  if (len % 5 > 0)
944  len += 5 - len % 5;
945  len /= 5;
946  str_buf = GNUNET_malloc (len + 1);
948  size,
949  str_buf,
950  len);
951  if (NULL == end)
952  {
953  GNUNET_free (str_buf);
954  return NULL;
955  }
956  *end = '\0';
957  return str_buf;
958 }
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:872
#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 972 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_attribute_cont(), delete_credential_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(), GNUNET_IDENTITY_private_key_from_string(), GNUNET_IDENTITY_public_key_from_string(), lookup_redirect_uri_result(), parse_attr(), parse_credential(), 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().

976 {
977  size_t rpos;
978  size_t wpos;
979  unsigned int bits;
980  unsigned int vbit;
981  int ret;
982  int shift;
983  unsigned char *uout;
984  size_t encoded_len;
985 
986  if (0 == enclen)
987  {
988  if (0 == out_size)
989  return GNUNET_OK;
990  return GNUNET_SYSERR;
991  }
992  GNUNET_assert (out_size < SIZE_MAX / 8);
993  encoded_len = out_size * 8;
994  uout = out;
995  wpos = out_size;
996  rpos = enclen;
997  if ((encoded_len % 5) > 0)
998  {
999  vbit = encoded_len % 5; /* padding! */
1000  shift = 5 - vbit;
1001  bits = (ret = getValue__ (enc[--rpos])) >> shift;
1002  }
1003  else
1004  {
1005  vbit = 5;
1006  shift = 0;
1007  bits = (ret = getValue__ (enc[--rpos]));
1008  }
1009  if ((encoded_len + shift) / 5 != enclen)
1010  return GNUNET_SYSERR;
1011  if (-1 == ret)
1012  return GNUNET_SYSERR;
1013  while (wpos > 0)
1014  {
1015  if (0 == rpos)
1016  {
1017  GNUNET_break (0);
1018  return GNUNET_SYSERR;
1019  }
1020  bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
1021  if (-1 == ret)
1022  return GNUNET_SYSERR;
1023  vbit += 5;
1024  if (vbit >= 8)
1025  {
1026  uout[--wpos] = (unsigned char) bits;
1027  bits >>= 8;
1028  vbit -= 8;
1029  }
1030  }
1031  if ((0 != rpos) || (0 != vbit))
1032  return GNUNET_SYSERR;
1033  return GNUNET_OK;
1034 }
#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:810
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 1876 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().

1877 {
1878  const char *data = in;
1879  size_t ret;
1880  char *opt;
1881 
1882  ret = 0;
1883  GNUNET_assert (len / 4 < SIZE_MAX);
1884  opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1885  for (size_t i = 0; i < len; ++i)
1886  {
1887  char c;
1888 
1889  c = (data[i] >> 2) & 0x3f;
1890  opt[ret++] = cvt[(int) c];
1891  c = (data[i] << 4) & 0x3f;
1892  if (++i < len)
1893  c |= (data[i] >> 4) & 0x0f;
1894  opt[ret++] = cvt[(int) c];
1895  if (i < len)
1896  {
1897  c = (data[i] << 2) & 0x3f;
1898  if (++i < len)
1899  c |= (data[i] >> 6) & 0x03;
1900  opt[ret++] = cvt[(int) c];
1901  }
1902  else
1903  {
1904  ++i;
1905  opt[ret++] = FILLCHAR;
1906  }
1907  if (i < len)
1908  {
1909  c = data[i] & 0x3f;
1910  opt[ret++] = cvt[(int) c];
1911  }
1912  else
1913  {
1914  opt[ret++] = FILLCHAR;
1915  }
1916  }
1917  *output = opt;
1918  return ret;
1919 }
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1860
#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:1861
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_urlencode()

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

url/percent encode (RFC3986).

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

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

Referenced by login_redirect().

2148 {
2149  struct GNUNET_Buffer buf = { 0 };
2150  const uint8_t *i8 = (uint8_t *) data;
2151 
2152  while (0 != *i8)
2153  {
2154  if (0 == (0x80 & *i8))
2155  {
2156  /* traditional ASCII */
2157  if (isalnum (*i8) || (*i8 == '-') || (*i8 == '_') || (*i8 == '.') ||
2158  (*i8 == '~') )
2159  GNUNET_buffer_write (&buf, (const char*) i8, 1);
2160  else if (*i8 == ' ')
2161  GNUNET_buffer_write (&buf, "+", 1);
2162  else
2164  "%%%X%X",
2165  *i8 >> 4,
2166  *i8 & 15);
2167  i8++;
2168  continue;
2169  }
2170  if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
2171  {
2172  /* 2-byte value, percent-encode */
2174  "%%%X%X",
2175  *i8 >> 4,
2176  *i8 & 15);
2177  i8++;
2179  "%%%X%X",
2180  *i8 >> 4,
2181  *i8 & 15);
2182  i8++;
2183  continue;
2184  }
2185  if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
2186  {
2187  /* 3-byte value, percent-encode */
2188  for (unsigned int i = 0; i<3; i++)
2189  {
2191  "%%%X%X",
2192  *i8 >> 4,
2193  *i8 & 15);
2194  i8++;
2195  }
2196  continue;
2197  }
2198  if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
2199  {
2200  /* 4-byte value, percent-encode */
2201  for (unsigned int i = 0; i<4; i++)
2202  {
2204  "%%%X%X",
2205  *i8 >> 4,
2206  *i8 & 15);
2207  i8++;
2208  }
2209  continue;
2210  }
2211  if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
2212  + 0x04) & *i8))
2213  {
2214  /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
2215  for (unsigned int i = 0; i<5; i++)
2216  {
2218  "%%%X%X",
2219  *i8 >> 4,
2220  *i8 & 15);
2221  i8++;
2222  }
2223  continue;
2224  }
2225  if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
2226  + 0x08 + 0x04 + 0x02)
2227  & *i8))
2228  {
2229  /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
2230  for (unsigned int i = 0; i<6; i++)
2231  {
2233  "%%%X%X",
2234  *i8 >> 4,
2235  *i8 & 15);
2236  i8++;
2237  }
2238  continue;
2239  }
2240  /* really, really invalid UTF-8: fail */
2241  GNUNET_break (0);
2242  GNUNET_buffer_clear (&buf);
2243  return 0;
2244  }
2245  *out = GNUNET_buffer_reap_str (&buf);
2246  return strlen (*out);
2247 }
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
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static char buf[2048]
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition: buffer.c:164
Common buffer management functions.
uint32_t data
The data value.
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 1932 of file strings.c.

References enc, and GNUNET_STRINGS_base64_encode().

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

1933 {
1934  char *enc;
1935  size_t pos;
1936 
1937  GNUNET_STRINGS_base64_encode (in, len, output);
1938  enc = *output;
1939  /* Replace with correct characters for base64url */
1940  pos = 0;
1941  while ('\0' != enc[pos])
1942  {
1943  if ('+' == enc[pos])
1944  enc[pos] = '-';
1945  if ('/' == enc[pos])
1946  enc[pos] = '_';
1947  if ('=' == enc[pos])
1948  {
1949  enc[pos] = '\0';
1950  break;
1951  }
1952  pos++;
1953  }
1954  return strlen (enc);
1955 }
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1876
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 1978 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 database_setup(), dns_string_to_value(), GNUNET_ABD_delegate_from_string(), GNUNET_HELLO_extract_address(), GNUNET_STRINGS_base64url_decode(), listenAndDistribute(), OIDC_access_token_parse(), parse_credentials_basic_auth(), and parse_jwt().

1979 {
1980  char *output;
1981  size_t ret = 0;
1982 
1983 #define CHECK_CRLF \
1984  while (data[i] == '\r' || data[i] == '\n') \
1985  { \
1986  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
1987  "ignoring CR/LF\n"); \
1988  i++; \
1989  if (i >= len) \
1990  goto END; \
1991  }
1992 
1993  GNUNET_assert (len / 3 < SIZE_MAX);
1994  output = GNUNET_malloc ((len * 3 / 4) + 8);
1996  "base64_decode decoding len=%d\n",
1997  (int) len);
1998  for (size_t i = 0; i < len; ++i)
1999  {
2000  char c;
2001  char c1;
2002 
2003  CHECK_CRLF;
2004  if (FILLCHAR == data[i])
2005  break;
2006  c = (char) cvtfind (data[i]);
2007  ++i;
2008  CHECK_CRLF;
2009  c1 = (char) cvtfind (data[i]);
2010  c = (c << 2) | ((c1 >> 4) & 0x3);
2011  output[ret++] = c;
2012  if (++i < len)
2013  {
2014  CHECK_CRLF;
2015  c = data[i];
2016  if (FILLCHAR == c)
2017  break;
2018  c = (char) cvtfind (c);
2019  c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
2020  output[ret++] = c1;
2021  }
2022  if (++i < len)
2023  {
2024  CHECK_CRLF;
2025  c1 = data[i];
2026  if (FILLCHAR == c1)
2027  break;
2028 
2029  c1 = (char) cvtfind (c1);
2030  c = ((c << 6) & 0xc0) | c1;
2031  output[ret++] = c;
2032  }
2033  }
2034 END:
2035  *out = output;
2036  return ret;
2037 }
#define CHECK_CRLF
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1860
#define cvtfind(a)
Definition: strings.c:1958
#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 2050 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().

2051 {
2052  char *s;
2053  int padding;
2054  size_t ret;
2055 
2056  /* make enough space for padding */
2057  GNUNET_assert (len < SIZE_MAX - 3);
2058  s = GNUNET_malloc (len + 3);
2059  memcpy (s, data, len);
2060 
2061  for (int i = 0; i < strlen (s); i++)
2062  {
2063  if (s[i] == '-')
2064  s[i] = '+';
2065  if (s[i] == '_')
2066  s[i] = '/';
2067  }
2068  padding = len % 4;
2069  switch (padding) // Pad with trailing '='s
2070  {
2071  case 0:
2072  break; // No pad chars in this case
2073  case 2:
2074  memcpy (&s[len],
2075  "==",
2076  2);
2077  len += 2;
2078  break; // Two pad chars
2079  case 3:
2080  s[len] = '=';
2081  len++;
2082  break; // One pad char
2083  default:
2084  GNUNET_assert (0);
2085  break;
2086  }
2087  ret = GNUNET_STRINGS_base64_decode (s, len, out);
2088  GNUNET_free (s);
2089  return ret;
2090 }
#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:1978
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_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
outputwhere to write the output (*output should be NULL, is allocated)
Returns
the size of the output

Definition at line 2103 of file strings.c.

References data, and GNUNET_malloc.

Referenced by get_url_parameter_copy().

2104 {
2105  const char *rpos = data;
2106  *out = GNUNET_malloc (len + 1); /* output should always fit into input */
2107  char *wpos = *out;
2108  size_t resl = 0;
2109 
2110  while ('\0' != *rpos)
2111  {
2112  unsigned int num;
2113  switch (*rpos)
2114  {
2115  case '%':
2116  if (1 != sscanf (rpos + 1, "%2x", &num))
2117  break;
2118  *wpos = (char) ((unsigned char) num);
2119  wpos++;
2120  resl++;
2121  rpos += 3;
2122  break;
2123  /* TODO: add bad sequence handling */
2124  /* intentional fall through! */
2125  default:
2126  *wpos = *rpos;
2127  wpos++;
2128  resl++;
2129  rpos++;
2130  }
2131  }
2132  *wpos = '\0'; /* add 0-terminator */
2133  return resl;
2134 }
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_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 101 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().

103 {
104  char *buf;
105  size_t off;
106  size_t plen = num_pids * 5 + 1;
107 
108  GNUNET_assert (num_pids < UINT32_MAX / 5);
109  off = 0;
110  buf = GNUNET_malloc (plen);
111  for (unsigned int i = 0; i < num_pids; i++)
112  {
113  off += GNUNET_snprintf (&buf[off],
114  plen - off,
115  "%s%s",
116  GNUNET_i2s (&pids[i]),
117  (i == num_pids - 1) ? "" : "-");
118  }
119  return buf;
120 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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.
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 1054 of file strings.c.

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

Referenced by GNUNET_STRINGS_path_is_absolute().

1057 {
1058  size_t len;
1059  size_t i;
1060  int end;
1061  int pp_state = 0;
1062  const char *post_scheme_part = NULL;
1063 
1064  len = strlen (path);
1065  for (end = 0, i = 0; ! end && i < len; i++)
1066  {
1067  switch (pp_state)
1068  {
1069  case 0:
1070  if ((path[i] == ':') && (i > 0))
1071  {
1072  pp_state += 1;
1073  continue;
1074  }
1075  if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
1076  ((path[i] >= 'a') && (path[i] <= 'z') ) ||
1077  ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
1078  (path[i] == '-') || (path[i] == '.')))
1079  end = 1;
1080  break;
1081 
1082  case 1:
1083  case 2:
1084  if (path[i] == '/')
1085  {
1086  pp_state += 1;
1087  continue;
1088  }
1089  end = 1;
1090  break;
1091 
1092  case 3:
1093  post_scheme_part = &path[i];
1094  end = 1;
1095  break;
1096 
1097  default:
1098  end = 1;
1099  }
1100  }
1101  if (post_scheme_part == NULL)
1102  return GNUNET_NO;
1103  if (scheme_part)
1104  {
1105  *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
1106  GNUNET_memcpy (*scheme_part, path, post_scheme_part - path);
1107  (*scheme_part)[post_scheme_part - path] = '\0';
1108  }
1109  if (path_part)
1110  *path_part = post_scheme_part;
1111  return GNUNET_YES;
1112 }
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 1130 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().

1134 {
1135  const char *post_scheme_path;
1136  int is_uri;
1137  char *uri;
1138  /* consider POSIX paths to be absolute too, even on W32,
1139  * as plibc expansion will fix them for us.
1140  */
1141  if (filename[0] == '/')
1142  return GNUNET_YES;
1143  if (can_be_uri)
1144  {
1145  is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
1146  if (r_is_uri)
1147  *r_is_uri = is_uri;
1148  if (is_uri)
1149  {
1150  if (r_uri_scheme)
1151  *r_uri_scheme = uri;
1152  else
1153  GNUNET_free (uri);
1154 
1155  return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
1156  GNUNET_NO,
1157  NULL,
1158  NULL);
1159  }
1160  }
1161  else
1162  {
1163  if (r_is_uri)
1164  *r_is_uri = GNUNET_NO;
1165  }
1166 
1167  return GNUNET_NO;
1168 }
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:1054
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:1130
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 1180 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.

1182 {
1183  struct stat st;
1184 
1185  if ((NULL == filename) || (filename[0] == '\0'))
1186  return GNUNET_SYSERR;
1187  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1188  if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
1189  return GNUNET_NO;
1190  if (0 != (checks
1193  {
1194  if (0 != lstat (filename, &st))
1195  {
1196  if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1197  return GNUNET_NO;
1198  else
1199  return GNUNET_SYSERR;
1200  }
1201  }
1202  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1203  if (! S_ISDIR (st.st_mode))
1204  return GNUNET_NO;
1205  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1206  if (! S_ISLNK (st.st_mode))
1207  return GNUNET_NO;
1208  return GNUNET_YES;
1209 }
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:1130
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 succeeded. 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 succeeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1225 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().

1228 {
1229  char zbuf[addrlen + 1];
1230  int ret;
1231  char *port_colon;
1232  unsigned int port;
1233  char dummy[2];
1234 
1235  if (addrlen < 6)
1236  return GNUNET_SYSERR;
1237  GNUNET_memcpy (zbuf, zt_addr, addrlen);
1238  if ('[' != zbuf[0])
1239  {
1241  _ ("IPv6 address did not start with `['\n"));
1242  return GNUNET_SYSERR;
1243  }
1244  zbuf[addrlen] = '\0';
1245  port_colon = strrchr (zbuf, ':');
1246  if (NULL == port_colon)
1247  {
1249  _ ("IPv6 address did contain ':' to separate port number\n"));
1250  return GNUNET_SYSERR;
1251  }
1252  if (']' != *(port_colon - 1))
1253  {
1254  GNUNET_log (
1256  _ ("IPv6 address did contain ']' before ':' to separate port number\n"));
1257  return GNUNET_SYSERR;
1258  }
1259  ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1260  if ((1 != ret) || (port > 65535))
1261  {
1262  GNUNET_log (
1264  _ ("IPv6 address did contain a valid port number after the last ':'\n"));
1265  return GNUNET_SYSERR;
1266  }
1267  *(port_colon - 1) = '\0';
1268  memset (r_buf, 0, sizeof(struct sockaddr_in6));
1269  ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1270  if (ret <= 0)
1271  {
1273  _ ("Invalid IPv6 address `%s': %s\n"),
1274  &zbuf[1],
1275  strerror (errno));
1276  return GNUNET_SYSERR;
1277  }
1278  r_buf->sin6_port = htons (port);
1279  r_buf->sin6_family = AF_INET6;
1280 #if HAVE_SOCKADDR_IN_SIN_LEN
1281  r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1282 #endif
1283  return GNUNET_OK;
1284 }
#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 succeeded. 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 succeeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1299 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().

1302 {
1303  unsigned int temps[4];
1304  unsigned int port;
1305  unsigned int cnt;
1306  char dummy[2];
1307 
1308  if (addrlen < 9)
1309  return GNUNET_SYSERR;
1310  cnt = sscanf (zt_addr,
1311  "%u.%u.%u.%u:%u%1s",
1312  &temps[0],
1313  &temps[1],
1314  &temps[2],
1315  &temps[3],
1316  &port,
1317  dummy);
1318  if (5 != cnt)
1319  return GNUNET_SYSERR;
1320  for (cnt = 0; cnt < 4; cnt++)
1321  if (temps[cnt] > 0xFF)
1322  return GNUNET_SYSERR;
1323  if (port > 65535)
1324  return GNUNET_SYSERR;
1325  r_buf->sin_family = AF_INET;
1326  r_buf->sin_port = htons (port);
1327  r_buf->sin_addr.s_addr =
1328  htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1329 #if HAVE_SOCKADDR_IN_SIN_LEN
1330  r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1331 #endif
1332  return GNUNET_OK;
1333 }
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 1372 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().

1375 {
1376  char *cp = GNUNET_strdup (addr);
1377 
1378  *af = AF_UNSPEC;
1379  if ('[' == *addr)
1380  {
1381  /* IPv6 */
1382  *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1383  if (GNUNET_OK !=
1385  strlen (cp),
1386  (struct sockaddr_in6 *) *sa))
1387  {
1388  GNUNET_free (*sa);
1389  *sa = NULL;
1390  GNUNET_free (cp);
1391  return 0;
1392  }
1393  *af = AF_INET6;
1394  GNUNET_free (cp);
1395  return sizeof(struct sockaddr_in6);
1396  }
1397  else
1398  {
1399  /* IPv4 */
1400  *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1401  if (GNUNET_OK !=
1403  strlen (cp),
1404  (struct sockaddr_in *) *sa))
1405  {
1406  GNUNET_free (*sa);
1407  *sa = NULL;
1408  GNUNET_free (cp);
1409  return 0;
1410  }
1411  *af = AF_INET;
1412  GNUNET_free (cp);
1413  return sizeof(struct sockaddr_in);
1414  }
1415 }
#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:1299
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:1225
#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 succeeded. 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 succeeded. GNUNET_SYSERR otherwise, in which case the contents of r_buf are undefined.

Definition at line 1348 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().

1351 {
1352  if (addr[0] == '[')
1353  return GNUNET_STRINGS_to_address_ipv6 (addr,
1354  addrlen,
1355  (struct sockaddr_in6 *) r_buf);
1356  return GNUNET_STRINGS_to_address_ipv4 (addr,
1357  addrlen,
1358  (struct sockaddr_in *) r_buf);
1359 }
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:1299
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:1225
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 1459 of file strings.c.

References _make_continuous_arg_copy(), and GNUNET_OK.

Referenced by main().

1463 {
1464  char *const *new_argv =
1465  (char *const *) _make_continuous_arg_copy (argc, argv);
1466  *u8argv = new_argv;
1467  *u8argc = argc;
1468  return GNUNET_OK;
1469 }
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:1423
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 221 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().

222 {
223  size_t slen;
224 
225  GNUNET_assert (0 != n);
226  slen = strnlen (src, n - 1);
227  memcpy (dst, src, slen);
228  dst[slen] = '\0';
229  return slen;
230 }
#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 1539 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().

1540 {
1541  unsigned int count;
1542  unsigned int i;
1543  unsigned int j;
1544  unsigned int len;
1545  int cnt;
1546  unsigned int pos;
1547  unsigned int temps[8];
1548  int slash;
1550  int colon;
1551  int end;
1552  char *routeList;
1553  char dummy[2];
1554 
1555  if (NULL == routeListX)
1556  return NULL;
1557  len = strlen (routeListX);
1558  if (0 == len)
1559  return NULL;
1560  routeList = GNUNET_strdup (routeListX);
1561  count = 0;
1562  for (i = 0; i < len; i++)
1563  if (routeList[i] == ';')
1564  count++;
1565  result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv4NetworkPolicy)
1566  * (count + 1));
1567  i = 0;
1568  pos = 0;
1569  while (i < count)
1570  {
1571  for (colon = pos; ':' != routeList[colon]; colon++)
1572  if ((';' == routeList[colon]) || ('\0' == routeList[colon]))
1573  break;
1574  for (end = colon; ';' != routeList[end]; end++)
1575  if ('\0' == routeList[end])
1576  break;
1577  if ('\0' == routeList[end])
1578  break;
1579  routeList[end] = '\0';
1580  if (':' == routeList[colon])
1581  {
1582  routeList[colon] = '\0';
1583  if (GNUNET_OK != parse_port_policy (&routeList[colon + 1], &result[i].pp))
1584  break;
1585  }
1586  cnt = sscanf (&routeList[pos],
1587  "%u.%u.%u.%u/%u.%u.%u.%u%1s",
1588  &temps[0],
1589  &temps[1],
1590  &temps[2],
1591  &temps[3],
1592  &temps[4],
1593  &temps[5],
1594  &temps[6],
1595  &temps[7],
1596  dummy);
1597  if (8 == cnt)
1598  {
1599  for (j = 0; j < 8; j++)
1600  if (temps[j] > 0xFF)
1601  {
1603  _ ("Invalid format for IP: `%s'\n"),
1604  &routeList[pos]);
1605  GNUNET_free (result);
1606  GNUNET_free (routeList);
1607  return NULL;
1608  }
1609  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1610  + (temps[2] << 8) + temps[3]);
1611  result[i].netmask.s_addr = htonl ((temps[4] << 24) + (temps[5] << 16)
1612  + (temps[6] << 8) + temps[7]);
1613  pos = end + 1;
1614  i++;
1615  continue;
1616  }
1617  /* try second notation */
1618  cnt = sscanf (&routeList[pos],
1619  "%u.%u.%u.%u/%u%1s",
1620  &temps[0],
1621  &temps[1],
1622  &temps[2],
1623  &temps[3],
1624  &slash,
1625  dummy);
1626  if (5 == cnt)
1627  {
1628  for (j = 0; j < 4; j++)
1629  if (temps[j] > 0xFF)
1630  {
1632  _ ("Invalid format for IP: `%s'\n"),
1633  &routeList[pos]);
1634  GNUNET_free (result);
1635  GNUNET_free (routeList);
1636  return NULL;
1637  }
1638  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1639  + (temps[2] << 8) + temps[3]);
1640  if ((slash <= 32) && (slash >= 0))
1641  {
1642  result[i].netmask.s_addr = 0;
1643  while (slash > 0)
1644  {
1645  result[i].netmask.s_addr =
1646  (result[i].netmask.s_addr >> 1) + 0x80000000;
1647  slash--;
1648  }
1649  result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1650  pos = end + 1;
1651  i++;
1652  continue;
1653  }
1654  else
1655  {
1657  _ ("Invalid network notation ('/%d' is not legal in IPv4 CIDR)."),
1658  slash);
1659  GNUNET_free (result);
1660  GNUNET_free (routeList);
1661  return NULL; /* error */
1662  }
1663  }
1664  /* try third notation */
1665  slash = 32;
1666  cnt = sscanf (&routeList[pos],
1667  "%u.%u.%u.%u%1s",
1668  &temps[0],
1669  &temps[1],
1670  &temps[2],
1671  &temps[3],
1672  dummy);
1673  if (4 == cnt)
1674  {
1675  for (j = 0; j < 4; j++)
1676  if (temps[j] > 0xFF)
1677  {
1679  _ ("Invalid format for IP: `%s'\n"),
1680  &routeList[pos]);
1681  GNUNET_free (result);
1682  GNUNET_free (routeList);
1683  return NULL;
1684  }
1685  result[i].network.s_addr = htonl ((temps[0] << 24) + (temps[1] << 16)
1686  + (temps[2] << 8) + temps[3]);
1687  result[i].netmask.s_addr = 0;
1688  while (slash > 0)
1689  {
1690  result[i].netmask.s_addr = (result[i].netmask.s_addr >> 1) + 0x80000000;
1691  slash--;
1692  }
1693  result[i].netmask.s_addr = htonl (result[i].netmask.s_addr);
1694  pos = end + 1;
1695  i++;
1696  continue;
1697  }
1699  _ ("Invalid format for IP: `%s'\n"),
1700  &routeList[pos]);
1701  GNUNET_free (result);
1702  GNUNET_free (routeList);
1703  return NULL; /* error */
1704  }
1705  if (pos < strlen (routeList))
1706  {
1708  _ ("Invalid format: `%s'\n"),
1709  &routeListX[pos]);
1710  GNUNET_free (result);
1711  GNUNET_free (routeList);
1712  return NULL; /* oops */
1713  }
1714  GNUNET_free (routeList);
1715  return result; /* ok */
1716 }
static int parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1482
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:38
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 1731 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().

1732 {
1733  unsigned int count;
1734  unsigned int i;
1735  unsigned int len;
1736  unsigned int pos;
1737  int start;
1738  int slash;
1739  int ret;
1740  char *routeList;
1742  unsigned int bits;
1743  unsigned int off;
1744  int save;
1745  int colon;
1746  char dummy[2];
1747 
1748  if (NULL == routeListX)
1749  return NULL;
1750  len = strlen (routeListX);
1751  if (0 == len)
1752  return NULL;
1753  routeList = GNUNET_strdup (routeListX);
1754  count = 0;
1755  for (i = 0; i < len; i++)
1756  if (';' == routeList[i])
1757  count++;
1758  if (';' != routeList[len - 1])
1759  {
1761  _ ("Invalid network notation (does not end with ';': `%s')\n"),
1762  routeList);
1763  GNUNET_free (routeList);
1764  return NULL;
1765  }
1766 
1767  result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy)
1768  * (count + 1));
1769  i = 0;
1770  pos = 0;
1771  while (i < count)
1772  {
1773  start = pos;
1774  while (';' != routeList[pos])
1775  pos++;
1776  slash = pos;
1777  while ((slash >= start) && (routeList[slash] != '/'))
1778  slash--;
1779 
1780  if (slash < start)
1781  {
1782  memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr));
1783  slash = pos;
1784  }
1785  else
1786  {
1787  routeList[pos] = '\0';
1788  for (colon = pos; ':' != routeList[colon]; colon--)
1789  if ('/' == routeList[colon])
1790  break;
1791  if (':' == routeList[colon])
1792  {
1793  routeList[colon] = '\0';
1794  if (GNUNET_OK !=
1795  parse_port_policy (&routeList[colon + 1], &result[i].pp))
1796  {
1797  GNUNET_free (result);
1798  GNUNET_free (routeList);
1799  return NULL;
1800  }
1801  }
1802  ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
1803  if (ret <= 0)
1804  {
1805  save = errno;
1806  if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1807  (bits > 128))
1808  {
1809  if (0 == ret)
1811  _ ("Wrong format `%s' for netmask\n"),
1812  &routeList[slash + 1]);
1813  else
1814  {
1815  errno = save;
1816  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1817  }
1818  GNUNET_free (result);
1819  GNUNET_free (routeList);
1820  return NULL;
1821  }
1822  off = 0;
1823  while (bits > 8)
1824  {
1825  result[i].netmask.s6_addr[off++] = 0xFF;
1826  bits -= 8;
1827  }
1828  while (bits > 0)
1829  {
1830  result[i].netmask.s6_addr[off] =
1831  (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1832  bits--;
1833  }
1834  }
1835  }
1836  routeList[slash] = '\0';
1837  ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
1838  if (ret <= 0)
1839  {
1840  if (0 == ret)
1842  _ ("Wrong format `%s' for network\n"),
1843  &routeList[slash + 1]);
1844  else
1845  LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
1846  GNUNET_free (result);
1847  GNUNET_free (routeList);
1848  return NULL;
1849  }
1850  pos++;
1851  i++;
1852  }
1853  GNUNET_free (routeList);
1854  return result;
1855 }
static int parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1482
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:38
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:40
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: