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...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, void *out, size_t out_size)
 Convert CrockfordBase32 encoding back to data. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc (const char *enc, size_t enclen, void **out, size_t *out_size)
 Convert CrockfordBase32 encoding back to data. More...
 
size_t GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
 Encode into Base64. More...
 
size_t GNUNET_STRINGS_urlencode (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...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part, const char **path_part)
 Parse a path that might be an URI. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute (const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
 Check whether filename is absolute or not, and if it's an URI. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_check_filename (const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
 Perform checks on filename. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
 Tries to convert zt_addr string to an IPv6 address. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, 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...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ip (const char *addr, uint16_t addrlen, struct sockaddr_storage *r_buf)
 Tries to convert addr string to an IP (v4 or v6) address. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
 Returns utf-8 encoded arguments. More...
 
size_t GNUNET_strlcpy (char *dst, const char *src, size_t n)
 Like strlcpy but portable. More...
 
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 502 of file gnunet_strings_lib.h.

503 {
507  GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
508 
513 
517  GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
518 
523 };
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 (e.g. 1 MB)
sizeset to the size in bytes
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

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

262 {
263  static const struct ConversionTable table[] =
264  { { "B", 1 },
265  { "KiB", 1024 },
266  { "kB", 1000 },
267  { "MiB", 1024 * 1024 },
268  { "MB", 1000 * 1000 },
269  { "GiB", 1024 * 1024 * 1024 },
270  { "GB", 1000 * 1000 * 1000 },
271  { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
272  { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
273  { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
274  { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
275  { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
276  { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
277  { NULL, 0 } };
278 
279  return convert_with_table (fancy_size, table, size);
280 }
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:205
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:179
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 (e.g. 1 minute)
rtimeset to the relative time
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

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

286 {
287  static const struct ConversionTable table[] =
288  { { "us", 1 },
289  { "ms", 1000 },
290  { "s", 1000 * 1000LL },
291  { "second", 1000 * 1000LL },
292  { "seconds", 1000 * 1000LL },
293  { "\"", 1000 * 1000LL },
294  { "m", 60 * 1000 * 1000LL },
295  { "min", 60 * 1000 * 1000LL },
296  { "minute", 60 * 1000 * 1000LL },
297  { "minutes", 60 * 1000 * 1000LL },
298  { "'", 60 * 1000 * 1000LL },
299  { "h", 60 * 60 * 1000 * 1000LL },
300  { "hour", 60 * 60 * 1000 * 1000LL },
301  { "hours", 60 * 60 * 1000 * 1000LL },
302  { "d", 24 * 60 * 60 * 1000LL * 1000LL },
303  { "day", 24 * 60 * 60 * 1000LL * 1000LL },
304  { "days", 24 * 60 * 60 * 1000LL * 1000LL },
305  { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
306  { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
307  { "year", 31536000000000LL /* year */ },
308  { "years", 31536000000000LL /* year */ },
309  { "a", 31536000000000LL /* year */ },
310  { NULL, 0 } };
311  int ret;
312  unsigned long long val;
313 
314  if (0 == strcasecmp ("forever", fancy_time))
315  {
317  return GNUNET_OK;
318  }
319  ret = convert_with_table (fancy_time, table, &val);
320  rtime->rel_value_us = (uint64_t) val;
321  return ret;
322 }
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:205
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:179
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 130 of file strings.c.

References GNUNET_malloc, GNUNET_snprintf(), and ret.

Referenced by progress_cb(), and publish_inspector().

131 {
132  const char *unit = /* size unit */ "b";
133  char *ret;
134 
135  if (size > 5 * 1024)
136  {
137  size = size / 1024;
138  unit = "KiB";
139  if (size > 5 * 1024)
140  {
141  size = size / 1024;
142  unit = "MiB";
143  if (size > 5 * 1024)
144  {
145  size = size / 1024;
146  unit = "GiB";
147  if (size > 5 * 1024)
148  {
149  size = size / 1024;
150  unit = "TiB";
151  }
152  }
153  }
154  }
155  ret = GNUNET_malloc (32);
156  GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
157  return ret;
158 }
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 358 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().

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

Definition at line 420 of file strings.c.

References GNUNET_STRINGS_conv().

423 {
424  return GNUNET_STRINGS_conv (input,
425  len,
426  charset,
427  "UTF-8");
428 }
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:358
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 432 of file strings.c.

References GNUNET_STRINGS_conv().

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

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

446 {
447  uint8_t *tmp_in;
448  size_t len;
449 
450  tmp_in = u8_tolower ((uint8_t *) input,
451  strlen ((char *) input),
452  NULL,
453  UNINORM_NFD,
454  NULL,
455  &len);
456  GNUNET_memcpy (output, tmp_in, len);
457  output[len] = '\0';
458  free (tmp_in);
459 }
#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

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

465 {
466  uint8_t *tmp_in;
467  size_t len;
468 
469  tmp_in = u8_toupper ((uint8_t *) input,
470  strlen ((char *) input),
471  NULL,
472  UNINORM_NFD,
473  NULL,
474  &len);
475  GNUNET_memcpy (output, tmp_in, len);
476  output[len] = '\0';
477  free (tmp_in);
478 }
#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

Definition at line 482 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(), file_test_internal(), GNUNET_CONFIGURATION_get_value_filename(), GNUNET_CONFIGURATION_load(), GNUNET_CONFIGURATION_parse(), GNUNET_CONFIGURATION_write(), GNUNET_DISK_directory_create(), GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_directory_scan(), GNUNET_DISK_file_open(), GNUNET_FS_directory_scan_start(), GNUNET_FS_make_file_reader_context_(), GNUNET_log_setup(), handle_client_index_start(), handle_inline(), hash_for_index_cb(), print_option(), and set_filename().

483 {
484  char *buffer;
485  size_t len;
486  char *fm;
487  const char *fil_ptr;
488 
489  if (fil == NULL)
490  return NULL;
491 
492  if (fil[0] == DIR_SEPARATOR)
493  /* absolute path, just copy */
494  return GNUNET_strdup (fil);
495  if (fil[0] == '~')
496  {
497  fm = getenv ("HOME");
498  if (fm == NULL)
499  {
501  _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
502  return NULL;
503  }
504  fm = GNUNET_strdup (fm);
505  /* do not copy '~' */
506  fil_ptr = fil + 1;
507 
508  /* skip over dir separator to be consistent */
509  if (fil_ptr[0] == DIR_SEPARATOR)
510  fil_ptr++;
511  }
512  else
513  {
514  /* relative path */
515  fil_ptr = fil;
516  len = 512;
517  fm = NULL;
518  while (1)
519  {
520  buffer = GNUNET_malloc (len);
521  if (getcwd (buffer, len) != NULL)
522  {
523  fm = buffer;
524  break;
525  }
526  if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
527  {
528  len *= 2;
529  GNUNET_free (buffer);
530  continue;
531  }
532  GNUNET_free (buffer);
533  break;
534  }
535  if (fm == NULL)
536  {
538  buffer = getenv ("PWD"); /* alternative */
539  if (buffer != NULL)
540  fm = GNUNET_strdup (buffer);
541  }
542  if (fm == NULL)
543  fm = GNUNET_strdup ("./"); /* give up */
544  }
545  GNUNET_asprintf (&buffer,
546  "%s%s%s",
547  fm,
548  (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
550  fil_ptr);
551  GNUNET_free (fm);
552  return buffer;
553 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#define DIR_SEPARATOR_STR
Definition: platform.h:165
#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:164
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)

Definition at line 45 of file strings.c.

References GNUNET_assert, and GNUNET_memcpy.

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

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

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

100 {
101  unsigned int start;
102  unsigned int needed;
103  const char **r;
104  va_list ap;
105 
106  needed = 0;
107  va_start (ap, count);
108  while (count > 0)
109  {
110  r = va_arg (ap, const char **);
111 
112  start = needed;
113  while ((needed < size) && (buffer[needed] != '\0'))
114  needed++;
115  if (needed == size)
116  {
117  va_end (ap);
118  return 0; /* error */
119  }
120  *r = &buffer[start];
121  needed++; /* skip 0-termination */
122  count--;
123  }
124  va_end (ap);
125  return needed;
126 }
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 626 of file strings.c.

References DIR_SEPARATOR_STR, and filename.

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

627 {
628  const char *short_fn = filename;
629  const char *ss;
630 
631  while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
632  short_fn = 1 + ss;
633  return short_fn;
634 }
#define DIR_SEPARATOR_STR
Definition: platform.h:165
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.

32 characters for encoding

Definition at line 695 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_PQ_event_notify(), GNUNET_sh2s(), GNUNET_STRINGS_data_to_string_alloc(), GNUNET_uuid2s(), output_message(), print_examples_ecdh(), run(), run_edkey(), run_pkey(), secret_ready_cb(), sh_to_channel(), uri_loc_to_string(), and uri_sks_to_string().

699 {
703  static char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
704  unsigned int wpos;
705  unsigned int rpos;
706  unsigned int bits;
707  unsigned int vbit;
708  const unsigned char *udata;
709 
710  GNUNET_assert (size < SIZE_MAX / 8 - 4);
711  udata = data;
712  if (out_size < (size * 8 + 4) / 5)
713  {
714  GNUNET_break (0);
715  return NULL;
716  }
717  vbit = 0;
718  wpos = 0;
719  rpos = 0;
720  bits = 0;
721  while ((rpos < size) || (vbit > 0))
722  {
723  if ((rpos < size) && (vbit < 5))
724  {
725  bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
726  vbit += 8;
727  }
728  if (vbit < 5)
729  {
730  bits <<= (5 - vbit); /* zero-padding */
731  GNUNET_assert (vbit == ((size * 8) % 5));
732  vbit = 5;
733  }
734  if (wpos >= out_size)
735  {
736  GNUNET_break (0);
737  return NULL;
738  }
739  out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
740  vbit -= 5;
741  }
742  GNUNET_assert (0 == vbit);
743  if (wpos < out_size)
744  out[wpos] = '\0';
745  return &out[wpos];
746 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define SIZE_MAX
Definition: platform.h:213
#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

Definition at line 750 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(), d2j(), 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(), main(), move_attr_finished(), move_attrs(), OIDC_generate_id_token(), oidc_ticket_issue_cb(), process_attrs(), process_record(), purge_attributes(), RECLAIM_TICKETS_consume(), RECLAIM_TICKETS_revoke(), rvk_move_attr_cb(), save_member(), save_member_session(), save_operation(), setup_job_headers(), ticket_collect(), ticket_issue_cb(), ticket_iter(), and value_to_string().

751 {
752  char *str_buf;
753  size_t len = size * 8;
754  char *end;
755 
756  if (len % 5 > 0)
757  len += 5 - len % 5;
758  len /= 5;
759  str_buf = GNUNET_malloc (len + 1);
761  size,
762  str_buf,
763  len);
764  if (NULL == end)
765  {
766  GNUNET_free (str_buf);
767  return NULL;
768  }
769  *end = '\0';
770  return str_buf;
771 }
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 CrockfordBase32.
Definition: strings.c:695
#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()

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

Convert CrockfordBase32 encoding back to data.

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

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

Definition at line 775 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(), expect_data_dynamic(), expect_data_fixed(), GNUNET_CONFIGURATION_get_data(), GNUNET_CRYPTO_ecdsa_public_key_from_string(), GNUNET_CRYPTO_eddsa_private_key_from_string(), GNUNET_CRYPTO_eddsa_public_key_from_string(), GNUNET_CRYPTO_hash_from_string2(), GNUNET_HELLO_parse_uri(), GNUNET_IDENTITY_private_key_from_string(), GNUNET_IDENTITY_public_key_from_string(), GNUNET_PQ_event_do_poll(), GNUNET_STRINGS_string_to_data_alloc(), lookup_redirect_uri_result(), main(), parse_attr(), parse_credential(), parse_fixed_data(), parse_rsa_public_key(), parse_rsa_signature(), parse_ticket(), read_service_conf(), run(), set_base32(), start_process(), string_to_auth_key(), string_to_value(), uri_loc_parse(), and uri_sks_parse().

779 {
780  size_t rpos;
781  size_t wpos;
782  unsigned int bits;
783  unsigned int vbit;
784  int ret;
785  int shift;
786  unsigned char *uout;
787  size_t encoded_len;
788 
789  if (0 == enclen)
790  {
791  if (0 == out_size)
792  return GNUNET_OK;
793  return GNUNET_SYSERR;
794  }
795  GNUNET_assert (out_size < SIZE_MAX / 8);
796  encoded_len = out_size * 8;
797  uout = out;
798  wpos = out_size;
799  rpos = enclen;
800  if ((encoded_len % 5) > 0)
801  {
802  vbit = encoded_len % 5; /* padding! */
803  shift = 5 - vbit;
804  bits = (ret = getValue__ (enc[--rpos])) >> shift;
805  }
806  else
807  {
808  vbit = 5;
809  shift = 0;
810  bits = (ret = getValue__ (enc[--rpos]));
811  }
812  if ((encoded_len + shift) / 5 != enclen)
813  return GNUNET_SYSERR;
814  if (-1 == ret)
815  return GNUNET_SYSERR;
816  while (wpos > 0)
817  {
818  if (0 == rpos)
819  {
820  GNUNET_break (0);
821  return GNUNET_SYSERR;
822  }
823  bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
824  if (-1 == ret)
825  return GNUNET_SYSERR;
826  vbit += 5;
827  if (vbit >= 8)
828  {
829  uout[--wpos] = (unsigned char) bits;
830  bits >>= 8;
831  vbit -= 8;
832  }
833  }
834  if ((0 != rpos) || (0 != vbit))
835  return GNUNET_SYSERR;
836  return GNUNET_OK;
837 }
#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:213
#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:645
static OpusEncoder * enc
OPUS encoder.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_string_to_data_alloc()

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

Convert CrockfordBase32 encoding back to data.

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

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

Definition at line 841 of file strings.c.

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

Referenced by GNUNET_PQ_event_do_poll(), and parse_variable_data().

845 {
846  size_t size;
847  void *data;
848  int res;
849 
850  size = (enclen * 5) / 8;
851  if (size >= GNUNET_MAX_MALLOC_CHECKED)
852  {
853  GNUNET_break_op (0);
854  return GNUNET_SYSERR;
855  }
856  data = GNUNET_malloc (size);
858  enclen,
859  data,
860  size);
861  if ( (0 < size) &&
862  (GNUNET_OK != res) )
863  {
864  size--;
866  enclen,
867  data,
868  size);
869  }
870  if (GNUNET_OK != res)
871  {
872  GNUNET_break_op (0);
873  GNUNET_free (data);
874  return GNUNET_SYSERR;
875  }
876  *out = data;
877  *out_size = size;
878  return GNUNET_OK;
879 }
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
static int res
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:775
static unsigned int size
Size of the "table".
Definition: peer.c:67
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
static OpusEncoder * enc
OPUS encoder.
uint32_t data
The data value.
#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_base64_encode()

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

Encode into Base64.

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

Definition at line 1594 of file strings.c.

References cvt, data, FILLCHAR, GNUNET_assert, GNUNET_malloc, consensus-simulation::int, 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(), pabc_create_presentation(), and store_and_free_entries().

1597 {
1598  const char *data = in;
1599  size_t ret;
1600  char *opt;
1601 
1602  ret = 0;
1603  GNUNET_assert (len / 4 < SIZE_MAX);
1604  opt = GNUNET_malloc (2 + (len * 4 / 3) + 8);
1605  for (size_t i = 0; i < len; ++i)
1606  {
1607  char c;
1608 
1609  c = (data[i] >> 2) & 0x3f;
1610  opt[ret++] = cvt[(int) c];
1611  c = (data[i] << 4) & 0x3f;
1612  if (++i < len)
1613  c |= (data[i] >> 4) & 0x0f;
1614  opt[ret++] = cvt[(int) c];
1615  if (i < len)
1616  {
1617  c = (data[i] << 2) & 0x3f;
1618  if (++i < len)
1619  c |= (data[i] >> 6) & 0x03;
1620  opt[ret++] = cvt[(int) c];
1621  }
1622  else
1623  {
1624  ++i;
1625  opt[ret++] = FILLCHAR;
1626  }
1627  if (i < len)
1628  {
1629  c = data[i] & 0x3f;
1630  opt[ret++] = cvt[(int) c];
1631  }
1632  else
1633  {
1634  opt[ret++] = FILLCHAR;
1635  }
1636  }
1637  *output = opt;
1638  return ret;
1639 }
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1587
#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:213
static char * cvt
Definition: strings.c:1588
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 1830 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(), PABC_load_public_parameters(), PABC_read_usr_ctx(), PABC_write_public_parameters(), and PABC_write_usr_ctx().

1833 {
1834  struct GNUNET_Buffer buf = { 0 };
1835  const uint8_t *i8 = (uint8_t *) data;
1836 
1837  while (0 != *i8)
1838  {
1839  if (0 == (0x80 & *i8))
1840  {
1841  /* traditional ASCII */
1842  if (isalnum (*i8) || (*i8 == '-') || (*i8 == '_') || (*i8 == '.') ||
1843  (*i8 == '~') )
1844  GNUNET_buffer_write (&buf, (const char*) i8, 1);
1845  else if (*i8 == ' ')
1846  GNUNET_buffer_write (&buf, "+", 1);
1847  else
1849  "%%%X%X",
1850  *i8 >> 4,
1851  *i8 & 15);
1852  i8++;
1853  continue;
1854  }
1855  if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
1856  {
1857  /* 2-byte value, percent-encode */
1859  "%%%X%X",
1860  *i8 >> 4,
1861  *i8 & 15);
1862  i8++;
1864  "%%%X%X",
1865  *i8 >> 4,
1866  *i8 & 15);
1867  i8++;
1868  continue;
1869  }
1870  if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
1871  {
1872  /* 3-byte value, percent-encode */
1873  for (unsigned int i = 0; i<3; i++)
1874  {
1876  "%%%X%X",
1877  *i8 >> 4,
1878  *i8 & 15);
1879  i8++;
1880  }
1881  continue;
1882  }
1883  if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
1884  {
1885  /* 4-byte value, percent-encode */
1886  for (unsigned int i = 0; i<4; i++)
1887  {
1889  "%%%X%X",
1890  *i8 >> 4,
1891  *i8 & 15);
1892  i8++;
1893  }
1894  continue;
1895  }
1896  if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
1897  + 0x04) & *i8))
1898  {
1899  /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1900  for (unsigned int i = 0; i<5; i++)
1901  {
1903  "%%%X%X",
1904  *i8 >> 4,
1905  *i8 & 15);
1906  i8++;
1907  }
1908  continue;
1909  }
1910  if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
1911  + 0x08 + 0x04 + 0x02)
1912  & *i8))
1913  {
1914  /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1915  for (unsigned int i = 0; i<6; i++)
1916  {
1918  "%%%X%X",
1919  *i8 >> 4,
1920  *i8 & 15);
1921  i8++;
1922  }
1923  continue;
1924  }
1925  /* really, really invalid UTF-8: fail */
1926  GNUNET_break (0);
1927  GNUNET_buffer_clear (&buf);
1928  return 0;
1929  }
1930  *out = GNUNET_buffer_reap_str (&buf);
1931  return strlen (*out);
1932 }
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 1643 of file strings.c.

References enc, and GNUNET_STRINGS_base64_encode().

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

1646 {
1647  char *enc;
1648  size_t pos;
1649 
1650  GNUNET_STRINGS_base64_encode (in, len, output);
1651  enc = *output;
1652  /* Replace with correct characters for base64url */
1653  pos = 0;
1654  while ('\0' != enc[pos])
1655  {
1656  if ('+' == enc[pos])
1657  enc[pos] = '-';
1658  if ('/' == enc[pos])
1659  enc[pos] = '_';
1660  if ('=' == enc[pos])
1661  {
1662  enc[pos] = '\0';
1663  break;
1664  }
1665  pos++;
1666  }
1667  return strlen (enc);
1668 }
size_t GNUNET_STRINGS_base64_encode(const void *in, size_t len, char **output)
Encode into Base64.
Definition: strings.c:1594
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 **  output 
)

Decode from Base64.

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

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

1685 {
1686  char *output;
1687  size_t ret = 0;
1688 
1689 #define CHECK_CRLF \
1690  while (data[i] == '\r' || data[i] == '\n') \
1691  { \
1692  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK, \
1693  "ignoring CR/LF\n"); \
1694  i++; \
1695  if (i >= len) \
1696  goto END; \
1697  }
1698 
1699  GNUNET_assert (len / 3 < SIZE_MAX);
1700  output = GNUNET_malloc ((len * 3 / 4) + 8);
1702  "base64_decode decoding len=%d\n",
1703  (int) len);
1704  for (size_t i = 0; i < len; ++i)
1705  {
1706  char c;
1707  char c1;
1708 
1709  CHECK_CRLF;
1710  if (FILLCHAR == data[i])
1711  break;
1712  c = (char) cvtfind (data[i]);
1713  ++i;
1714  CHECK_CRLF;
1715  c1 = (char) cvtfind (data[i]);
1716  c = (c << 2) | ((c1 >> 4) & 0x3);
1717  output[ret++] = c;
1718  if (++i < len)
1719  {
1720  CHECK_CRLF;
1721  c = data[i];
1722  if (FILLCHAR == c)
1723  break;
1724  c = (char) cvtfind (c);
1725  c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
1726  output[ret++] = c1;
1727  }
1728  if (++i < len)
1729  {
1730  CHECK_CRLF;
1731  c1 = data[i];
1732  if (FILLCHAR == c1)
1733  break;
1734 
1735  c1 = (char) cvtfind (c1);
1736  c = ((c << 6) & 0xc0) | c1;
1737  output[ret++] = c;
1738  }
1739  }
1740 END:
1741  *out = output;
1742  return ret;
1743 }
#define CHECK_CRLF
#define FILLCHAR
******************** Base64 encoding
Definition: strings.c:1587
#define cvtfind(a)
Definition: strings.c:1671
#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:213
#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 1747 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(), OIDC_parse_authz_code(), and set_attributes_from_idtoken().

1750 {
1751  char *s;
1752  int padding;
1753  size_t ret;
1754 
1755  /* make enough space for padding */
1756  GNUNET_assert (len < SIZE_MAX - 3);
1757  s = GNUNET_malloc (len + 3);
1758  memcpy (s, data, len);
1759 
1760  for (int i = 0; i < strlen (s); i++)
1761  {
1762  if (s[i] == '-')
1763  s[i] = '+';
1764  if (s[i] == '_')
1765  s[i] = '/';
1766  }
1767  padding = len % 4;
1768  switch (padding) // Pad with trailing '='s
1769  {
1770  case 0:
1771  break; // No pad chars in this case
1772  case 2:
1773  memcpy (&s[len],
1774  "==",
1775  2);
1776  len += 2;
1777  break; // Two pad chars
1778  case 3:
1779  s[len] = '=';
1780  len++;
1781  break; // One pad char
1782  default:
1783  GNUNET_assert (0);
1784  break;
1785  }
1786  ret = GNUNET_STRINGS_base64_decode (s, len, out);
1787  GNUNET_free (s);
1788  return ret;
1789 }
#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:213
size_t GNUNET_STRINGS_base64_decode(const char *data, size_t len, void **out)
Decode from Base64.
Definition: strings.c:1682
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 1793 of file strings.c.

References data, and GNUNET_malloc.

Referenced by get_url_parameter_copy().

1796 {
1797  const char *rpos = data;
1798  *out = GNUNET_malloc (len + 1); /* output should always fit into input */
1799  char *wpos = *out;
1800  size_t resl = 0;
1801 
1802  while ('\0' != *rpos)
1803  {
1804  unsigned int num;
1805  switch (*rpos)
1806  {
1807  case '%':
1808  if (1 != sscanf (rpos + 1, "%2x", &num))
1809  break;
1810  *wpos = (char) ((unsigned char) num);
1811  wpos++;
1812  resl++;
1813  rpos += 3;
1814  break;
1815  /* TODO: add bad sequence handling */
1816  /* intentional fall through! */
1817  default:
1818  *wpos = *rpos;
1819  wpos++;
1820  resl++;
1821  rpos++;
1822  }
1823  }
1824  *wpos = '\0'; /* add 0-terminator */
1825  return resl;
1826 }
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 73 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().

75 {
76  char *buf;
77  size_t off;
78  size_t plen = num_pids * 5 + 1;
79 
80  GNUNET_assert (num_pids < UINT32_MAX / 5);
81  off = 0;
82  buf = GNUNET_malloc (plen);
83  for (unsigned int i = 0; i < num_pids; i++)
84  {
85  off += GNUNET_snprintf (&buf[off],
86  plen - off,
87  "%s%s",
88  GNUNET_i2s (&pids[i]),
89  (i == num_pids - 1) ? "" : "-");
90  }
91  return buf;
92 }
#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()

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

Parse a path that might be an URI.

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

Definition at line 883 of file strings.c.

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

Referenced by GNUNET_STRINGS_path_is_absolute().

886 {
887  size_t len;
888  size_t i;
889  int end;
890  int pp_state = 0;
891  const char *post_scheme_part = NULL;
892 
893  len = strlen (path);
894  for (end = 0, i = 0; ! end && i < len; i++)
895  {
896  switch (pp_state)
897  {
898  case 0:
899  if ((path[i] == ':') && (i > 0))
900  {
901  pp_state += 1;
902  continue;
903  }
904  if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
905  ((path[i] >= 'a') && (path[i] <= 'z') ) ||
906  ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
907  (path[i] == '-') || (path[i] == '.')))
908  end = 1;
909  break;
910 
911  case 1:
912  case 2:
913  if (path[i] == '/')
914  {
915  pp_state += 1;
916  continue;
917  }
918  end = 1;
919  break;
920 
921  case 3:
922  post_scheme_part = &path[i];
923  end = 1;
924  break;
925 
926  default:
927  end = 1;
928  }
929  }
930  if (post_scheme_part == NULL)
931  return GNUNET_NO;
932  if (scheme_part)
933  {
934  *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
935  GNUNET_memcpy (*scheme_part, path, post_scheme_part - path);
936  (*scheme_part)[post_scheme_part - path] = '\0';
937  }
938  if (path_part)
939  *path_part = post_scheme_part;
940  return GNUNET_YES;
941 }
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()

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

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

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

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

949 {
950  const char *post_scheme_path;
951  int is_uri;
952  char *uri;
953  /* consider POSIX paths to be absolute too, even on W32,
954  * as plibc expansion will fix them for us.
955  */
956  if (filename[0] == '/')
957  return GNUNET_YES;
958  if (can_be_uri)
959  {
960  is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
961  if (r_is_uri)
962  *r_is_uri = is_uri;
963  if (is_uri)
964  {
965  if (r_uri_scheme)
966  *r_uri_scheme = uri;
967  else
968  GNUNET_free (uri);
969 
970  return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
971  GNUNET_NO,
972  NULL,
973  NULL);
974  }
975  }
976  else
977  {
978  if (r_is_uri)
979  *r_is_uri = GNUNET_NO;
980  }
981 
982  return GNUNET_NO;
983 }
enum GNUNET_GenericReturnValue GNUNET_STRINGS_parse_uri(const char *path, char **scheme_part, const char **path_part)
Parse a path that might be an URI.
Definition: strings.c:883
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
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it&#39;s an URI.
Definition: strings.c:945
#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()

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

Perform checks on filename.

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

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

Definition at line 987 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.

989 {
990  struct stat st;
991 
992  if ((NULL == filename) || (filename[0] == '\0'))
993  return GNUNET_SYSERR;
994  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
996  return GNUNET_NO;
997  if (0 != (checks
1000  {
1001  if (0 != lstat (filename, &st))
1002  {
1003  if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1004  return GNUNET_NO;
1005  else
1006  return GNUNET_SYSERR;
1007  }
1008  }
1009  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1010  if (! S_ISDIR (st.st_mode))
1011  return GNUNET_NO;
1012  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1013  if (! S_ISLNK (st.st_mode))
1014  return GNUNET_NO;
1015  return GNUNET_YES;
1016 }
Check that the path is an absolute path.
Check that it exists.
static char * filename
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it&#39;s an URI.
Definition: strings.c:945
#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()

enum GNUNET_GenericReturnValue 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.

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

1023 {
1024  char zbuf[addrlen + 1];
1025  int ret;
1026  char *port_colon;
1027  unsigned int port;
1028  char dummy[2];
1029 
1030  if (addrlen < 6)
1031  return GNUNET_SYSERR;
1032  GNUNET_memcpy (zbuf, zt_addr, addrlen);
1033  if ('[' != zbuf[0])
1034  {
1036  _ ("IPv6 address did not start with `['\n"));
1037  return GNUNET_SYSERR;
1038  }
1039  zbuf[addrlen] = '\0';
1040  port_colon = strrchr (zbuf, ':');
1041  if (NULL == port_colon)
1042  {
1044  _ ("IPv6 address did contain ':' to separate port number\n"));
1045  return GNUNET_SYSERR;
1046  }
1047  if (']' != *(port_colon - 1))
1048  {
1049  GNUNET_log (
1051  _ ("IPv6 address did contain ']' before ':' to separate port number\n"));
1052  return GNUNET_SYSERR;
1053  }
1054  ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1055  if ((1 != ret) || (port > 65535))
1056  {
1057  GNUNET_log (
1059  _ ("IPv6 address did contain a valid port number after the last ':'\n"));
1060  return GNUNET_SYSERR;
1061  }
1062  *(port_colon - 1) = '\0';
1063  memset (r_buf, 0, sizeof(struct sockaddr_in6));
1064  ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1065  if (ret <= 0)
1066  {
1068  _ ("Invalid IPv6 address `%s': %s\n"),
1069  &zbuf[1],
1070  strerror (errno));
1071  return GNUNET_SYSERR;
1072  }
1073  r_buf->sin6_port = htons (port);
1074  r_buf->sin6_family = AF_INET6;
1075 #if HAVE_SOCKADDR_IN_SIN_LEN
1076  r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1077 #endif
1078  return GNUNET_OK;
1079 }
#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:178
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:83
#define GNUNET_log(kind,...)
Here is the caller graph for this function:

◆ GNUNET_STRINGS_to_address_ipv4()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_to_address_ipv4 ( const char *  zt_addr,
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.

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

1086 {
1087  unsigned int temps[4];
1088  unsigned int port;
1089  unsigned int cnt;
1090  char dummy[2];
1091 
1092  if (addrlen < 9)
1093  return GNUNET_SYSERR;
1094  cnt = sscanf (zt_addr,
1095  "%u.%u.%u.%u:%u%1s",
1096  &temps[0],
1097  &temps[1],
1098  &temps[2],
1099  &temps[3],
1100  &port,
1101  dummy);
1102  if (5 != cnt)
1103  return GNUNET_SYSERR;
1104  for (cnt = 0; cnt < 4; cnt++)
1105  if (temps[cnt] > 0xFF)
1106  return GNUNET_SYSERR;
1107  if (port > 65535)
1108  return GNUNET_SYSERR;
1109  r_buf->sin_family = AF_INET;
1110  r_buf->sin_port = htons (port);
1111  r_buf->sin_addr.s_addr =
1112  htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1113 #if HAVE_SOCKADDR_IN_SIN_LEN
1114  r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1115 #endif
1116  return GNUNET_OK;
1117 }
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:83
Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_socket_addr()

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

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

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

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

1139 {
1140  char *cp = GNUNET_strdup (addr);
1141 
1142  *af = AF_UNSPEC;
1143  if ('[' == *addr)
1144  {
1145  /* IPv6 */
1146  *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1147  if (GNUNET_OK !=
1149  strlen (cp),
1150  (struct sockaddr_in6 *) *sa))
1151  {
1152  GNUNET_free (*sa);
1153  *sa = NULL;
1154  GNUNET_free (cp);
1155  return 0;
1156  }
1157  *af = AF_INET6;
1158  GNUNET_free (cp);
1159  return sizeof(struct sockaddr_in6);
1160  }
1161  else
1162  {
1163  /* IPv4 */
1164  *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1165  if (GNUNET_OK !=
1167  strlen (cp),
1168  (struct sockaddr_in *) *sa))
1169  {
1170  GNUNET_free (*sa);
1171  *sa = NULL;
1172  GNUNET_free (cp);
1173  return 0;
1174  }
1175  *af = AF_INET;
1176  GNUNET_free (cp);
1177  return sizeof(struct sockaddr_in);
1178  }
1179 }
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
enum GNUNET_GenericReturnValue 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.
Definition: strings.c:1083
#define GNUNET_malloc(size)
Wrapper around malloc.
enum GNUNET_GenericReturnValue 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:1020
#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()

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

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

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

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

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

1124 {
1125  if (addr[0] == '[')
1126  return GNUNET_STRINGS_to_address_ipv6 (addr,
1127  addrlen,
1128  (struct sockaddr_in6 *) r_buf);
1129  return GNUNET_STRINGS_to_address_ipv4 (addr,
1130  addrlen,
1131  (struct sockaddr_in *) r_buf);
1132 }
enum GNUNET_GenericReturnValue 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.
Definition: strings.c:1083
enum GNUNET_GenericReturnValue 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:1020
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_utf8_args()

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

Returns utf-8 encoded arguments.

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

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

Definition at line 1209 of file strings.c.

References _make_continuous_arg_copy(), and GNUNET_OK.

Referenced by main().

1213 {
1214  char *const *new_argv =
1215  (char *const *) _make_continuous_arg_copy (argc, argv);
1216  *u8argv = new_argv;
1217  *u8argc = argc;
1218  return GNUNET_OK;
1219 }
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:1187
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_strlcpy()

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

Like strlcpy but portable.

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

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

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

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

165 {
166  size_t slen;
167 
168  GNUNET_assert (0 != n);
169  slen = strnlen (src, n - 1);
170  memcpy (dst, src, slen);
171  dst[slen] = '\0';
172  return slen;
173 }
#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

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

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

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

1459 {
1460  unsigned int count;
1461  unsigned int i;
1462  unsigned int len;
1463  unsigned int pos;
1464  int start;
1465  int slash;
1466  int ret;
1467  char *routeList;
1469  unsigned int bits;
1470  unsigned int off;
1471  int save;
1472  int colon;
1473  char dummy[2];
1474 
1475  if (NULL == routeListX)
1476  return NULL;
1477  len = strlen (routeListX);
1478  if (0 == len)
1479  return NULL;
1480  routeList = GNUNET_strdup (routeListX);
1481  count = 0;
1482  for (i = 0; i < len; i++)
1483  if (';' == routeList[i])
1484  count++;
1485  if (';' != routeList[len - 1])
1486  {
1488  _ ("Invalid network notation (does not end with ';': `%s')\n"),
1489  routeList);
1490  GNUNET_free (routeList);
1491  return NULL;
1492  }
1493 
1494  result = GNUNET_malloc (sizeof(struct GNUNET_STRINGS_IPv6NetworkPolicy)
1495  * (count + 1));
1496  i = 0;
1497  pos = 0;
1498  while (i < count)
1499  {
1500  start = pos;
1501  while (';' != routeList[pos])
1502  pos++;
1503  slash = pos;
1504  while ((slash >= start) && (routeList[slash] != '/'))
1505  slash--;
1506 
1507  if (slash < start)
1508  {
1509  memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr));
1510  slash = pos;
1511  }
1512  else
1513  {
1514  routeList[pos] = '\0';
1515  for (colon = pos; ':' != routeList[colon]; colon--)
1516  if ('/' == routeList[colon])
1517  break;
1518  if (':' == routeList[colon])
1519  {
1520  routeList[colon] = '\0';
1521  if (GNUNET_OK !=
1522  parse_port_policy (&routeList[colon + 1], &result[i].pp))
1523  {
1524  GNUNET_free (result);
1525  GNUNET_free (routeList);
1526  return NULL;
1527  }
1528  }
1529  ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
1530  if (ret <= 0)
1531  {
1532  save = errno;
1533  if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1534  (bits > 128))
1535  {
1536  if (0 == ret)
1538  _ ("Wrong format `%s' for netmask\n"),
1539  &routeList[slash + 1]);
1540  else
1541  {
1542  errno = save;
1543  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1544  }
1545  GNUNET_free (result);
1546  GNUNET_free (routeList);
1547  return NULL;
1548  }
1549  off = 0;
1550  while (bits > 8)
1551  {
1552  result[i].netmask.s6_addr[off++] = 0xFF;
1553  bits -= 8;
1554  }
1555  while (bits > 0)
1556  {
1557  result[i].netmask.s6_addr[off] =
1558  (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1559  bits--;
1560  }
1561  }
1562  }
1563  routeList[slash] = '\0';
1564  ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
1565  if (ret <= 0)
1566  {
1567  if (0 == ret)
1569  _ ("Wrong format `%s' for network\n"),
1570  &routeList[slash + 1]);
1571  else
1572  LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
1573  GNUNET_free (result);
1574  GNUNET_free (routeList);
1575  return NULL;
1576  }
1577  pos++;
1578  i++;
1579  }
1580  GNUNET_free (routeList);
1581  return result;
1582 }
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:178
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.
static enum GNUNET_GenericReturnValue parse_port_policy(const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
Parse the given port policy.
Definition: strings.c:1232
#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: