GNUnet  0.20.0
Strings library

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

Collaboration diagram for Strings library:

Data Structures

struct  GNUNET_STRINGS_PortPolicy
 
struct  GNUNET_STRINGS_IPv4NetworkPolicy
 IPV4 network in CIDR notation. More...
 
struct  GNUNET_STRINGS_IPv6NetworkPolicy
 network in CIDR notation for IPV6. More...
 

Enumerations

enum  GNUNET_STRINGS_FilenameCheck { GNUNET_STRINGS_CHECK_EXISTS = 0x00000001 , GNUNET_STRINGS_CHECK_IS_DIRECTORY = 0x00000002 , GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004 , GNUNET_STRINGS_CHECK_IS_ABSOLUTE = 0x00000008 }
 Flags for what we should check a file for. More...
 

Functions

enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_size_to_bytes (const char *fancy_size, unsigned long long *size)
 Convert a given fancy human-readable size to bytes. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative (const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
 Convert a given fancy human-readable time to our internal representation. More...
 
char * GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
 Convert a given filesize into a fancy human-readable format. More...
 
char * GNUNET_STRINGS_conv (const char *input, size_t len, const char *input_charset, const char *output_charset)
 Convert the len characters long character sequence given in input that is in the given input charset to a string in given output charset. More...
 
char * GNUNET_STRINGS_to_utf8 (const char *input, size_t len, const char *charset)
 Convert the len characters long character sequence given in input that is in the given charset to UTF-8. More...
 
char * GNUNET_STRINGS_utf8_normalize (const char *input)
 Normalize the utf-8 input string to NFC. More...
 
char * GNUNET_STRINGS_from_utf8 (const char *input, size_t len, const char *charset)
 Convert the len bytes-long UTF-8 string given in input to the given charset. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_tolower (const char *input, char *output)
 Convert the utf-8 input string to lower case. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_toupper (const char *input, char *output)
 Convert the utf-8 input string to upper case. More...
 
char * GNUNET_STRINGS_filename_expand (const char *fil)
 Complete filename (a la shell) from abbrevition. More...
 
size_t GNUNET_STRINGS_buffer_fill (char *buffer, size_t size, unsigned int count,...)
 Fill a buffer of the given size with count 0-terminated strings (given as varargs). More...
 
unsigned int GNUNET_STRINGS_buffer_tokenize (const char *buffer, size_t size, unsigned int count,...)
 Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count (varargs) of type "const char**" to the locations of the respective strings in the buffer. More...
 
const char * GNUNET_STRINGS_get_short_name (const char *filename)
 "man basename" Returns a pointer to a part of filename (allocates nothing)! More...
 
char * GNUNET_STRINGS_data_to_string (const void *data, size_t size, char *out, size_t out_size)
 Convert binary data to ASCII encoding using CrockfordBase32. More...
 
char * GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size)
 Return the base32crockford encoding of the given buffer. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, void *out, size_t out_size)
 Convert CrockfordBase32 encoding back to data. More...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data_alloc (const char *enc, size_t enclen, void **out, size_t *out_size)
 Convert CrockfordBase32 encoding back to data. More...
 
size_t GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
 Encode into Base64. More...
 
size_t GNUNET_STRINGS_urlencode (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...
 
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...
 
char * GNUNET_STRINGS_get_suffix_from_binary_name (const char *argv0)
 Sometimes we use the binary name to determine which specific test to run. More...
 
struct GNUNET_STRINGS_IPv4NetworkPolicyGNUNET_STRINGS_parse_ipv4_policy (const char *routeListX)
 Parse an IPv4 network policy. More...
 
struct GNUNET_STRINGS_IPv6NetworkPolicyGNUNET_STRINGS_parse_ipv6_policy (const char *routeListX)
 Parse an IPv6 network policy. More...
 

Detailed Description

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

Enumeration Type Documentation

◆ GNUNET_STRINGS_FilenameCheck

Flags for what we should check a file for.

Enumerator
GNUNET_STRINGS_CHECK_EXISTS 

Check that it exists.

GNUNET_STRINGS_CHECK_IS_DIRECTORY 

Check that it is a directory.

GNUNET_STRINGS_CHECK_IS_LINK 

Check that it is a link.

GNUNET_STRINGS_CHECK_IS_ABSOLUTE 

Check that the path is an absolute path.

Definition at line 530 of file gnunet_strings_lib.h.

531 {
535  GNUNET_STRINGS_CHECK_EXISTS = 0x00000001,
536 
541 
545  GNUNET_STRINGS_CHECK_IS_LINK = 0x00000004,
546 
551 };
@ GNUNET_STRINGS_CHECK_IS_ABSOLUTE
Check that the path is an absolute path.
@ GNUNET_STRINGS_CHECK_IS_DIRECTORY
Check that it is a directory.
@ GNUNET_STRINGS_CHECK_EXISTS
Check that it exists.
@ GNUNET_STRINGS_CHECK_IS_LINK
Check that it is a link.

Function Documentation

◆ GNUNET_STRINGS_fancy_size_to_bytes()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_size_to_bytes ( const char *  fancy_size,
unsigned long long *  size 
)

Convert a given fancy human-readable size to bytes.

Parameters
fancy_sizehuman readable string (e.g. 1 MB)
sizeset to the size in bytes
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 138 of file strings.c.

238 {
239  static const struct ConversionTable table[] =
240  { { "B", 1 },
241  { "KiB", 1024 },
242  { "kB", 1000 },
243  { "MiB", 1024 * 1024 },
244  { "MB", 1000 * 1000 },
245  { "GiB", 1024 * 1024 * 1024 },
246  { "GB", 1000 * 1000 * 1000 },
247  { "TiB", 1024LL * 1024LL * 1024LL * 1024LL },
248  { "TB", 1000LL * 1000LL * 1000LL * 1024LL },
249  { "PiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
250  { "PB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL },
251  { "EiB", 1024LL * 1024LL * 1024LL * 1024LL * 1024LL * 1024LL },
252  { "EB", 1000LL * 1000LL * 1000LL * 1024LL * 1000LL * 1000LL },
253  { NULL, 0 } };
254 
255  return convert_with_table (fancy_size, table, size);
256 }
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:56
static unsigned int size
Size of the "table".
Definition: peer.c:68
static enum GNUNET_GenericReturnValue convert_with_table(const char *input, const struct ConversionTable *table, unsigned long long *output)
Convert a string of the form "4 X 5 Y" into a numeric value by interpreting "X" and "Y" as units and ...
Definition: strings.c:181
Unit conversion table entry for 'convert_with_table'.
Definition: strings.c:156

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

Here is the caller graph for this function:

◆ GNUNET_STRINGS_fancy_time_to_relative()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative ( const char *  fancy_time,
struct GNUNET_TIME_Relative rtime 
)

Convert a given fancy human-readable time to our internal representation.

Parameters
fancy_timehuman readable string (e.g. 1 minute)
rtimeset to the relative time
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 138 of file strings.c.

262 {
263  static const struct ConversionTable table[] =
264  { { "us", 1 },
265  { "ms", 1000 },
266  { "s", 1000 * 1000LL },
267  { "second", 1000 * 1000LL },
268  { "seconds", 1000 * 1000LL },
269  { "\"", 1000 * 1000LL },
270  { "m", 60 * 1000 * 1000LL },
271  { "min", 60 * 1000 * 1000LL },
272  { "minute", 60 * 1000 * 1000LL },
273  { "minutes", 60 * 1000 * 1000LL },
274  { "'", 60 * 1000 * 1000LL },
275  { "h", 60 * 60 * 1000 * 1000LL },
276  { "hour", 60 * 60 * 1000 * 1000LL },
277  { "hours", 60 * 60 * 1000 * 1000LL },
278  { "d", 24 * 60 * 60 * 1000LL * 1000LL },
279  { "day", 24 * 60 * 60 * 1000LL * 1000LL },
280  { "days", 24 * 60 * 60 * 1000LL * 1000LL },
281  { "week", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
282  { "weeks", 7 * 24 * 60 * 60 * 1000LL * 1000LL },
283  { "year", 31536000000000LL /* year */ },
284  { "years", 31536000000000LL /* year */ },
285  { "a", 31536000000000LL /* year */ },
286  { NULL, 0 } };
287  int ret;
288  unsigned long long val;
289 
290  if (0 == strcasecmp ("forever", fancy_time))
291  {
293  return GNUNET_OK;
294  }
295  ret = convert_with_table (fancy_time, table, &val);
296  rtime->rel_value_us = (uint64_t) val;
297  return ret;
298 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
@ GNUNET_OK
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
uint64_t rel_value_us
The actual value.

Referenced by create_did_ego_lockup_cb(), and parse_expiration().

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

107 {
108  const char *unit = /* size unit */ "b";
109  char *ret;
110 
111  if (size > 5 * 1024)
112  {
113  size = size / 1024;
114  unit = "KiB";
115  if (size > 5 * 1024)
116  {
117  size = size / 1024;
118  unit = "MiB";
119  if (size > 5 * 1024)
120  {
121  size = size / 1024;
122  unit = "GiB";
123  if (size > 5 * 1024)
124  {
125  size = size / 1024;
126  unit = "TiB";
127  }
128  }
129  }
130  }
131  ret = GNUNET_malloc (32);
132  GNUNET_snprintf (ret, 32, "%llu %s", size, unit);
133  return ret;
134 }
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_malloc(size)
Wrapper around malloc.

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

Referenced by progress_cb(), and publish_inspector().

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

◆ GNUNET_STRINGS_conv()

char* GNUNET_STRINGS_conv ( const char *  input,
size_t  len,
const char *  input_charset,
const char *  output_charset 
)

Convert the len characters long character sequence given in input that is in the given input charset to a string in given output charset.

Parameters
inputinput string
lennumber of bytes in input
input_charsetcharacter set used for input
output_charsetdesired character set for the return value
Returns
the converted string (0-terminated), if conversion fails, a copy of the original string is returned.

Definition at line 343 of file strings.c.

347 {
348  char *ret;
349  uint8_t *u8_string;
350  char *encoded_string;
351  size_t u8_string_length;
352  size_t encoded_string_length;
353 
354  u8_string = u8_conv_from_encoding (input_charset,
355  iconveh_error,
356  input,
357  len,
358  NULL,
359  NULL,
360  &u8_string_length);
361  if (NULL == u8_string)
362  {
363  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_from_encoding");
364  goto fail;
365  }
366  if (0 == strcmp (output_charset, "UTF-8"))
367  {
368  ret = GNUNET_malloc (u8_string_length + 1);
369  GNUNET_memcpy (ret, u8_string, u8_string_length);
370  ret[u8_string_length] = '\0';
371  free (u8_string);
372  return ret;
373  }
374  encoded_string = u8_conv_to_encoding (output_charset,
375  iconveh_error,
376  u8_string,
377  u8_string_length,
378  NULL,
379  NULL,
380  &encoded_string_length);
381  free (u8_string);
382  if (NULL == encoded_string)
383  {
384  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "u8_conv_to_encoding");
385  goto fail;
386  }
387  ret = GNUNET_malloc (encoded_string_length + 1);
388  GNUNET_memcpy (ret, encoded_string, encoded_string_length);
389  ret[encoded_string_length] = '\0';
390  free (encoded_string);
391  return ret;
392  fail:
394  _ ("Character sets requested were `%s'->`%s'\n"),
395  "UTF-8",
396  output_charset);
397  ret = GNUNET_malloc (len + 1);
398  GNUNET_memcpy (ret, input, len);
399  ret[len] = '\0';
400  return ret;
401 }
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_ERROR_TYPE_WARNING
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
#define LOG(kind,...)
Definition: strings.c:37
#define LOG_STRERROR(kind, syscall)
Definition: strings.c:39

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

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

408 {
409  return GNUNET_STRINGS_conv (input,
410  len,
411  charset,
412  "UTF-8");
413 }
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:343

References GNUNET_STRINGS_conv(), and len.

Here is the call graph for this function:

◆ GNUNET_STRINGS_utf8_normalize()

char* GNUNET_STRINGS_utf8_normalize ( const char *  input)

Normalize the utf-8 input string to NFC.

Parameters
inputinput string
Returns
result (freshly allocated) or NULL on error.

Definition at line 429 of file strings.c.

430 {
431  uint8_t *tmp;
432  size_t len;
433  char *output;
434  tmp = u8_normalize (UNINORM_NFC,
435  (uint8_t *) input,
436  strlen ((char*) input),
437  NULL,
438  &len);
439  if (NULL == tmp)
440  return NULL;
441  output = GNUNET_malloc (len + 1);
442  GNUNET_memcpy (output, tmp, len);
443  output[len] = '\0';
444  free (tmp);
445  return output;
446 }

References GNUNET_malloc, GNUNET_memcpy, and len.

Referenced by GNUNET_GNSRECORD_label_check().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_from_utf8()

char* GNUNET_STRINGS_from_utf8 ( const char *  input,
size_t  len,
const char *  charset 
)

Convert the len bytes-long UTF-8 string given in input to the given charset.

Parameters
inputthe input string (not necessarily 0-terminated)
lenthe number of bytes in the input
charsetcharacter set to convert to
Returns
the converted string (0-terminated), if conversion fails, a copy of the original string is returned.

Definition at line 417 of file strings.c.

420 {
421  return GNUNET_STRINGS_conv (input,
422  len,
423  "UTF-8",
424  charset);
425 }

References GNUNET_STRINGS_conv(), and len.

Here is the call graph for this function:

◆ GNUNET_STRINGS_utf8_tolower()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_tolower ( const char *  input,
char *  output 
)

Convert the utf-8 input string to lower case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer
Returns
GNUNET_OK on success

Definition at line 429 of file strings.c.

451 {
452  uint8_t *tmp_in;
453  size_t len;
454 
455  tmp_in = u8_tolower ((uint8_t *) input,
456  strlen ((char *) input),
457  NULL,
458  UNINORM_NFD,
459  NULL,
460  &len);
461  if (NULL == tmp_in)
462  return GNUNET_SYSERR;
463  GNUNET_memcpy (output, tmp_in, len);
464  output[len] = '\0';
465  GNUNET_free (tmp_in);
466  return GNUNET_OK;
467 }
@ GNUNET_SYSERR
#define GNUNET_free(ptr)
Wrapper around free.

Referenced by ego_create(), GNUNET_RECLAIM_attribute_new(), GNUNET_RECLAIM_credential_new(), handle_collect(), handle_create_message(), handle_delete_message(), handle_rename_message(), handle_verify(), and header_iterator().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_utf8_toupper()

enum GNUNET_GenericReturnValue GNUNET_STRINGS_utf8_toupper ( const char *  input,
char *  output 
)

Convert the utf-8 input string to upper case.

Output needs to be allocated appropriately.

Parameters
inputinput string
outputoutput buffer
Returns
GNUNET_OK on success

Definition at line 429 of file strings.c.

473 {
474  uint8_t *tmp_in;
475  size_t len;
476 
477  tmp_in = u8_toupper ((uint8_t *) input,
478  strlen ((char *) input),
479  NULL,
480  UNINORM_NFD,
481  NULL,
482  &len);
483  if (NULL == tmp_in)
484  return GNUNET_SYSERR;
485  /* 0-terminator does not fit */
486  GNUNET_memcpy (output, tmp_in, len);
487  output[len] = '\0';
488  GNUNET_free (tmp_in);
489  return GNUNET_OK;
490 }

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

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

495 {
496  char *buffer;
497  size_t len;
498  char *fm;
499  const char *fil_ptr;
500 
501  if (fil == NULL)
502  return NULL;
503 
504  if (fil[0] == DIR_SEPARATOR)
505  /* absolute path, just copy */
506  return GNUNET_strdup (fil);
507  if (fil[0] == '~')
508  {
509  fm = getenv ("HOME");
510  if (fm == NULL)
511  {
513  _ ("Failed to expand `$HOME': environment variable `HOME' not set"));
514  return NULL;
515  }
516  fm = GNUNET_strdup (fm);
517  /* do not copy '~' */
518  fil_ptr = fil + 1;
519 
520  /* skip over dir separator to be consistent */
521  if (fil_ptr[0] == DIR_SEPARATOR)
522  fil_ptr++;
523  }
524  else
525  {
526  /* relative path */
527  fil_ptr = fil;
528  len = 512;
529  fm = NULL;
530  while (1)
531  {
532  buffer = GNUNET_malloc (len);
533  if (getcwd (buffer, len) != NULL)
534  {
535  fm = buffer;
536  break;
537  }
538  if ((errno == ERANGE) && (len < 1024 * 1024 * 4))
539  {
540  len *= 2;
541  GNUNET_free (buffer);
542  continue;
543  }
544  GNUNET_free (buffer);
545  break;
546  }
547  if (fm == NULL)
548  {
550  buffer = getenv ("PWD"); /* alternative */
551  if (buffer != NULL)
552  fm = GNUNET_strdup (buffer);
553  }
554  if (fm == NULL)
555  fm = GNUNET_strdup ("./"); /* give up */
556  }
557  GNUNET_asprintf (&buffer,
558  "%s%s%s",
559  fm,
560  (fm[strlen (fm) - 1] == DIR_SEPARATOR) ? ""
562  fil_ptr);
563  GNUNET_free (fm);
564  return buffer;
565 }
char * getenv()
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define DIR_SEPARATOR
Definition: platform.h:165
#define DIR_SEPARATOR_STR
Definition: platform.h:166

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_DISK_directory_scan(), GNUNET_DISK_file_close(), GNUNET_FS_directory_scan_start(), GNUNET_FS_make_file_reader_context_(), GNUNET_log_setup(), handle_client_index_start(), hash_for_index_cb(), and print_filename_option().

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

47 {
48  size_t needed;
49  va_list ap;
50 
51  needed = 0;
52  va_start (ap, count);
53  while (count > 0)
54  {
55  const char *s = va_arg (ap, const char *);
56  size_t slen = strlen (s) + 1;
57 
58  GNUNET_assert (slen <= size - needed);
59  if (NULL != buffer)
60  GNUNET_memcpy (&buffer[needed],
61  s,
62  slen);
63  needed += slen;
64  count--;
65  }
66  va_end (ap);
67  return needed;
68 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.

References GNUNET_assert, GNUNET_memcpy, and size.

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

Here is the caller graph for this function:

◆ GNUNET_STRINGS_buffer_tokenize()

unsigned int GNUNET_STRINGS_buffer_tokenize ( const char *  buffer,
size_t  size,
unsigned int  count,
  ... 
)

Given a buffer of a given size, find "count" 0-terminated strings in the buffer and assign the count (varargs) of type "const char**" to the locations of the respective strings in the buffer.

Parameters
bufferthe buffer to parse
sizesize of the buffer
countnumber of strings to locate
...pointers to where to store the strings
Returns
offset of the character after the last 0-termination in the buffer, or 0 on error.

Definition at line 72 of file strings.c.

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

References size, and start.

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

Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_short_name()

const char* GNUNET_STRINGS_get_short_name ( const char *  filename)

"man basename" Returns a pointer to a part of filename (allocates nothing)!

Parameters
filenamefilename to extract basename from
Returns
short (base) name of the file (that is, everything following the last directory separator in filename. If filename ends with a directory separator, the result will be a zero-length string. If filename has no directory separators, the result is filename itself.

Definition at line 639 of file strings.c.

640 {
641  const char *short_fn = filename;
642  const char *ss;
643 
644  while (NULL != (ss = strstr (short_fn, DIR_SEPARATOR_STR)) && (ss[1] != '\0'))
645  short_fn = 1 + ss;
646  return short_fn;
647 }
static char * filename

References DIR_SEPARATOR_STR, and filename.

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

Here is the caller graph for this function:

◆ GNUNET_STRINGS_data_to_string()

char* GNUNET_STRINGS_data_to_string ( const void *  data,
size_t  size,
char *  out,
size_t  out_size 
)

Convert binary data to ASCII encoding using CrockfordBase32.

Does not append 0-terminator, but returns a pointer to the place where it should be placed, if needed.

Parameters
datadata to encode
sizesize of data (in bytes)
outbuffer to fill
out_sizesize of the buffer. Must be large enough to hold ((size*8) + (((size*8) % 5) > 0 ? 5 - ((size*8) % 5) : 0)) / 5
Returns
pointer to the next byte in 'out' or NULL on error.

32 characters for encoding

Definition at line 708 of file strings.c.

712 {
716  static char *encTable__ = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
717  unsigned int wpos;
718  unsigned int rpos;
719  unsigned int bits;
720  unsigned int vbit;
721  const unsigned char *udata;
722 
723  GNUNET_assert (size < SIZE_MAX / 8 - 4);
724  udata = data;
725  if (out_size < (size * 8 + 4) / 5)
726  {
727  GNUNET_break (0);
728  return NULL;
729  }
730  vbit = 0;
731  wpos = 0;
732  rpos = 0;
733  bits = 0;
734  while ((rpos < size) || (vbit > 0))
735  {
736  if ((rpos < size) && (vbit < 5))
737  {
738  bits = (bits << 8) | udata[rpos++]; /* eat 8 more bits */
739  vbit += 8;
740  }
741  if (vbit < 5)
742  {
743  bits <<= (5 - vbit); /* zero-padding */
744  GNUNET_assert (vbit == ((size * 8) % 5));
745  vbit = 5;
746  }
747  if (wpos >= out_size)
748  {
749  GNUNET_break (0);
750  return NULL;
751  }
752  out[wpos++] = encTable__[(bits >> (vbit - 5)) & 31];
753  vbit -= 5;
754  }
755  GNUNET_assert (0 == vbit);
756  if (wpos < out_size)
757  out[wpos] = '\0';
758  return &out[wpos];
759 }
uint32_t data
The data value.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define SIZE_MAX
Definition: platform.h:208

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

Referenced by auth_key_to_string(), channel_to_sh(), 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_from_string2(), GNUNET_GNSRECORD_record_get_expiration_time(), GNUNET_sh2s(), GNUNET_STRINGS_check_filename(), GNUNET_uuid2s(), output_message(), print_examples_ecdh(), run_edkey(), run_pkey(), run_with_key(), secret_ready_cb(), uri_loc_to_string(), and uri_sks_to_string().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_data_to_string_alloc()

char* GNUNET_STRINGS_data_to_string_alloc ( const void *  buf,
size_t  size 
)

Return the base32crockford encoding of the given buffer.

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

Parameters
bufbuffer with data
sizesize of the buffer buf
Returns
freshly allocated, null-terminated string

Definition at line 763 of file strings.c.

764 {
765  char *str_buf;
766  size_t len = size * 8;
767  char *end;
768 
769  if (len % 5 > 0)
770  len += 5 - len % 5;
771  len /= 5;
772  str_buf = GNUNET_malloc (len + 1);
774  size,
775  str_buf,
776  len);
777  if (NULL == end)
778  {
779  GNUNET_free (str_buf);
780  return NULL;
781  }
782  *end = '\0';
783  return str_buf;
784 }
static int end
Set if we are to shutdown all services (including ARM).
Definition: gnunet-arm.c:34
static char buf[2048]
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:708

Referenced by attr_collect(), attr_store_task(), conversation_value_to_string(), cred_collect(), cred_iter_cb(), cred_store_task(), d2j(), generate_id_token_body(), generate_userinfo_json(), GNUNET_b2s(), GNUNET_HELLO_builder_add_address(), GNUNET_IDENTITY_private_key_from_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(), messenger_value_to_string(), move_attr_finished(), move_attrs(), oidc_ticket_issue_cb(), process_attrs(), process_record(), purge_attributes(), RECLAIM_TICKETS_consume(), RECLAIM_TICKETS_revoke(), run(), rvk_move_attr_cb(), save_member(), save_operation(), setup_job_headers(), ticket_collect(), ticket_issue_cb(), ticket_iter(), value_to_string(), and write_encrypted_message().

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

792 {
793  size_t rpos;
794  size_t wpos;
795  unsigned int bits;
796  unsigned int vbit;
797  int ret;
798  int shift;
799  unsigned char *uout;
800  size_t encoded_len;
801 
802  if (0 == enclen)
803  {
804  if (0 == out_size)
805  return GNUNET_OK;
806  return GNUNET_SYSERR;
807  }
808  GNUNET_assert (out_size < SIZE_MAX / 8);
809  encoded_len = out_size * 8;
810  uout = out;
811  wpos = out_size;
812  rpos = enclen;
813  if ((encoded_len % 5) > 0)
814  {
815  vbit = encoded_len % 5; /* padding! */
816  shift = 5 - vbit;
817  bits = (ret = getValue__ (enc[--rpos])) >> shift;
818  }
819  else
820  {
821  vbit = 5;
822  shift = 0;
823  bits = (ret = getValue__ (enc[--rpos]));
824  }
825  if ((encoded_len + shift) / 5 != enclen)
826  return GNUNET_SYSERR;
827  if (-1 == ret)
828  return GNUNET_SYSERR;
829  while (wpos > 0)
830  {
831  if (0 == rpos)
832  {
833  GNUNET_break (0);
834  return GNUNET_SYSERR;
835  }
836  bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
837  if (-1 == ret)
838  return GNUNET_SYSERR;
839  vbit += 5;
840  if (vbit >= 8)
841  {
842  uout[--wpos] = (unsigned char) bits;
843  bits >>= 8;
844  vbit -= 8;
845  }
846  }
847  if ((0 != rpos) || (0 != vbit))
848  return GNUNET_SYSERR;
849  return GNUNET_OK;
850 }
static OpusEncoder * enc
OPUS encoder.
static unsigned int getValue__(unsigned char a)
Get the decoded value corresponding to a character according to Crockford Base32 encoding.
Definition: strings.c:658

Referenced by check_authorization(), conversation_string_to_value(), delete_attribute_cont(), delete_credential_cont(), ego_create(), expect_data_dynamic(), expect_data_fixed(), GNUNET_HELLO_builder_from_url(), GNUNET_HELLO_parse_uri(), lookup_redirect_uri_result(), main(), messenger_string_to_value(), parse_attr(), parse_credential(), parse_ticket(), read_service_conf(), run(), start_process(), string_to_value(), uri_loc_parse(), and uri_sks_parse().

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

858 {
859  size_t size;
860  void *data;
861  int res;
862 
863  size = (enclen * 5) / 8;
865  {
866  GNUNET_break_op (0);
867  return GNUNET_SYSERR;
868  }
869  data = GNUNET_malloc (size);
871  enclen,
872  data,
873  size);
874  if ( (0 < size) &&
875  (GNUNET_OK != res) )
876  {
877  size--;
879  enclen,
880  data,
881  size);
882  }
883  if (GNUNET_OK != res)
884  {
885  GNUNET_break_op (0);
886  GNUNET_free (data);
887  return GNUNET_SYSERR;
888  }
889  *out = data;
890  *out_size = size;
891  return GNUNET_OK;
892 }
static int res
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition: strings.c:788

Referenced by GNUNET_PQ_event_do_poll(), and read_encrypted_message().

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

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

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_JSON_from_data64(), GNUNET_STRINGS_base64url_encode(), OIDC_access_token_new(), pabc_create_presentation(), and store_and_free_entries().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_urlencode()

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

url/percent encode (RFC3986).

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

Definition at line 1850 of file strings.c.

1853 {
1854  struct GNUNET_Buffer buf = { 0 };
1855  const uint8_t *i8 = (uint8_t *) data;
1856 
1857  while (0 != *i8)
1858  {
1859  if (0 == (0x80 & *i8))
1860  {
1861  /* traditional ASCII */
1862  if ( isalnum (*i8) ||
1863  (*i8 == '-') ||
1864  (*i8 == '_') ||
1865  (*i8 == '.') ||
1866  (*i8 == '~') )
1868  (const char*) i8,
1869  1);
1870  else if (*i8 == ' ')
1872  "+",
1873  1);
1874  else
1876  "%%%X%X",
1877  *i8 >> 4,
1878  *i8 & 15);
1879  i8++;
1880  continue;
1881  }
1882  if (0x80 + 0x40 == ((0x80 + 0x40 + 0x20) & *i8))
1883  {
1884  /* 2-byte value, percent-encode */
1886  "%%%X%X",
1887  *i8 >> 4,
1888  *i8 & 15);
1889  i8++;
1891  "%%%X%X",
1892  *i8 >> 4,
1893  *i8 & 15);
1894  i8++;
1895  continue;
1896  }
1897  if (0x80 + 0x40 + 0x20 == ((0x80 + 0x40 + 0x20 + 0x10) & *i8))
1898  {
1899  /* 3-byte value, percent-encode */
1900  for (unsigned int i = 0; i<3; i++)
1901  {
1903  "%%%X%X",
1904  *i8 >> 4,
1905  *i8 & 15);
1906  i8++;
1907  }
1908  continue;
1909  }
1910  if (0x80 + 0x40 + 0x20 + 0x10 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08) & *i8))
1911  {
1912  /* 4-byte value, percent-encode */
1913  for (unsigned int i = 0; i<4; i++)
1914  {
1916  "%%%X%X",
1917  *i8 >> 4,
1918  *i8 & 15);
1919  i8++;
1920  }
1921  continue;
1922  }
1923  if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 == ((0x80 + 0x40 + 0x20 + 0x10 + 0x08
1924  + 0x04) & *i8))
1925  {
1926  /* 5-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1927  for (unsigned int i = 0; i<5; i++)
1928  {
1930  "%%%X%X",
1931  *i8 >> 4,
1932  *i8 & 15);
1933  i8++;
1934  }
1935  continue;
1936  }
1937  if (0x80 + 0x40 + 0x20 + 0x10 + 0x08 + 0x04 == ((0x80 + 0x40 + 0x20 + 0x10
1938  + 0x08 + 0x04 + 0x02)
1939  & *i8))
1940  {
1941  /* 6-byte value, percent-encode (outside of UTF-8 modern standard, but so what) */
1942  for (unsigned int i = 0; i<6; i++)
1943  {
1945  "%%%X%X",
1946  *i8 >> 4,
1947  *i8 & 15);
1948  i8++;
1949  }
1950  continue;
1951  }
1952  /* really, really invalid UTF-8: fail */
1953  GNUNET_break (0);
1955  return 0;
1956  }
1957  *out = GNUNET_buffer_reap_str (&buf);
1958  return strlen (*out);
1959 }
char * GNUNET_buffer_reap_str(struct GNUNET_Buffer *buf)
Clear the buffer and return the string it contained.
Definition: buffer.c:123
void GNUNET_buffer_write_fstr(struct GNUNET_Buffer *buf, const char *fmt,...) __attribute__((format(printf
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:86
void GNUNET_buffer_clear(struct GNUNET_Buffer *buf)
Free the backing memory of the given buffer.
Definition: buffer.c:164
Dynamically growing buffer.

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

Referenced by GNUNET_HELLO_builder_add_address(), and login_redirect().

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

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

References enc, GNUNET_STRINGS_base64_encode(), and len.

Referenced by check_code_challenge(), DID_key_convert_gnunet_to_multibase_base64(), ego_sign_data_cb(), and OIDC_generate_id_token_hmac().

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

◆ GNUNET_STRINGS_base64_decode()

size_t GNUNET_STRINGS_base64_decode ( const char *  data,
size_t  len,
void **  output 
)

Decode from Base64.

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

Definition at line 1695 of file strings.c.

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

References CHECK_CRLF, cvtfind, data, 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(), parse_credentials_basic_auth(), and parse_jwt().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_base64url_decode()

size_t GNUNET_STRINGS_base64url_decode ( const char *  data,
size_t  len,
void **  out 
)

Decode from Base64url.

RFC7515

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

Definition at line 1760 of file strings.c.

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

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

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

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

◆ GNUNET_STRINGS_urldecode()

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

url/percent encode (RFC3986).

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

Definition at line 1806 of file strings.c.

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

References data, GNUNET_break_op, GNUNET_free, GNUNET_malloc, and len.

Referenced by get_url_parameter_copy(), and GNUNET_HELLO_builder_from_url().

Here is the caller graph for this function:

◆ GNUNET_STRINGS_parse_uri()

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

Parse a path that might be an URI.

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

Definition at line 763 of file strings.c.

899 {
900  size_t len;
901  size_t i;
902  int end;
903  int pp_state = 0;
904  const char *post_scheme_part = NULL;
905 
906  len = strlen (path);
907  for (end = 0, i = 0; ! end && i < len; i++)
908  {
909  switch (pp_state)
910  {
911  case 0:
912  if ((path[i] == ':') && (i > 0))
913  {
914  pp_state += 1;
915  continue;
916  }
917  if (! (((path[i] >= 'A') && (path[i] <= 'Z') ) ||
918  ((path[i] >= 'a') && (path[i] <= 'z') ) ||
919  ((path[i] >= '0') && (path[i] <= '9') ) || (path[i] == '+') ||
920  (path[i] == '-') || (path[i] == '.')))
921  end = 1;
922  break;
923 
924  case 1:
925  case 2:
926  if (path[i] == '/')
927  {
928  pp_state += 1;
929  continue;
930  }
931  end = 1;
932  break;
933 
934  case 3:
935  post_scheme_part = &path[i];
936  end = 1;
937  break;
938 
939  default:
940  end = 1;
941  }
942  }
943  if (post_scheme_part == NULL)
944  return GNUNET_NO;
945  if (scheme_part)
946  {
947  *scheme_part = GNUNET_malloc (post_scheme_part - path + 1);
948  GNUNET_memcpy (*scheme_part, path, post_scheme_part - path);
949  (*scheme_part)[post_scheme_part - path] = '\0';
950  }
951  if (path_part)
952  *path_part = post_scheme_part;
953  return GNUNET_YES;
954 }
@ GNUNET_YES
@ GNUNET_NO

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

962 {
963  const char *post_scheme_path;
964  int is_uri;
965  char *uri;
966  /* consider POSIX paths to be absolute too, even on W32,
967  * as plibc expansion will fix them for us.
968  */
969  if (filename[0] == '/')
970  return GNUNET_YES;
971  if (can_be_uri)
972  {
973  is_uri = GNUNET_STRINGS_parse_uri (filename, &uri, &post_scheme_path);
974  if (r_is_uri)
975  *r_is_uri = is_uri;
976  if (is_uri)
977  {
978  if (r_uri_scheme)
979  *r_uri_scheme = uri;
980  else
981  GNUNET_free (uri);
982 
983  return GNUNET_STRINGS_path_is_absolute (post_scheme_path,
984  GNUNET_NO,
985  NULL,
986  NULL);
987  }
988  }
989  else
990  {
991  if (r_is_uri)
992  *r_is_uri = GNUNET_NO;
993  }
994 
995  return GNUNET_NO;
996 }
static struct GNUNET_FS_Uri * uri
Value of URI provided on command-line (when not publishing a file but just creating UBlocks to refer ...
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it's an URI.
Definition: strings.c:958
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:896

Referenced by GNUNET_OS_check_helper_binary(), and GNUNET_OS_get_libexec_binary_path().

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

1002 {
1003  struct stat st;
1004 
1005  if ((NULL == filename) || (filename[0] == '\0'))
1006  return GNUNET_SYSERR;
1007  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_ABSOLUTE))
1008  if (! GNUNET_STRINGS_path_is_absolute (filename, GNUNET_NO, NULL, NULL))
1009  return GNUNET_NO;
1010  if (0 != (checks
1013  {
1014  if (0 != lstat (filename, &st))
1015  {
1016  if (0 != (checks & GNUNET_STRINGS_CHECK_EXISTS))
1017  return GNUNET_NO;
1018  else
1019  return GNUNET_SYSERR;
1020  }
1021  }
1022  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_DIRECTORY))
1023  if (! S_ISDIR (st.st_mode))
1024  return GNUNET_NO;
1025  if (0 != (checks & GNUNET_STRINGS_CHECK_IS_LINK))
1026  if (! S_ISLNK (st.st_mode))
1027  return GNUNET_NO;
1028  return GNUNET_YES;
1029 }
#define S_ISLNK(m)
Definition: disk.c:60
static struct GNUNET_SCHEDULER_Task * st
The shutdown task.

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

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

1036 {
1037  char zbuf[addrlen + 1];
1038  int ret;
1039  char *port_colon;
1040  unsigned int port;
1041  char dummy[2];
1042 
1043  if (addrlen < 6)
1044  return GNUNET_SYSERR;
1045  GNUNET_memcpy (zbuf, zt_addr, addrlen);
1046  if ('[' != zbuf[0])
1047  {
1049  _ ("IPv6 address did not start with `['\n"));
1050  return GNUNET_SYSERR;
1051  }
1052  zbuf[addrlen] = '\0';
1053  port_colon = strrchr (zbuf, ':');
1054  if (NULL == port_colon)
1055  {
1057  _ ("IPv6 address did contain ':' to separate port number\n"));
1058  return GNUNET_SYSERR;
1059  }
1060  if (']' != *(port_colon - 1))
1061  {
1062  GNUNET_log (
1064  _ ("IPv6 address did contain ']' before ':' to separate port number\n"));
1065  return GNUNET_SYSERR;
1066  }
1067  ret = sscanf (port_colon, ":%u%1s", &port, dummy);
1068  if ((1 != ret) || (port > 65535))
1069  {
1070  GNUNET_log (
1072  _ ("IPv6 address did contain a valid port number after the last ':'\n"));
1073  return GNUNET_SYSERR;
1074  }
1075  *(port_colon - 1) = '\0';
1076  memset (r_buf, 0, sizeof(struct sockaddr_in6));
1077  ret = inet_pton (AF_INET6, &zbuf[1], &r_buf->sin6_addr);
1078  if (ret <= 0)
1079  {
1081  _ ("Invalid IPv6 address `%s': %s\n"),
1082  &zbuf[1],
1083  strerror (errno));
1084  return GNUNET_SYSERR;
1085  }
1086  r_buf->sin6_port = htons (port);
1087  r_buf->sin6_family = AF_INET6;
1088 #if HAVE_SOCKADDR_IN_SIN_LEN
1089  r_buf->sin6_len = (u_char) sizeof(struct sockaddr_in6);
1090 #endif
1091  return GNUNET_OK;
1092 }
static uint16_t port
Port number.
Definition: gnunet-bcd.c:147
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.

Referenced by GNUNET_STRINGS_parse_socket_addr().

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

1099 {
1100  unsigned int temps[4];
1101  unsigned int port;
1102  unsigned int cnt;
1103  char dummy[2];
1104 
1105  if (addrlen < 9)
1106  return GNUNET_SYSERR;
1107  cnt = sscanf (zt_addr,
1108  "%u.%u.%u.%u:%u%1s",
1109  &temps[0],
1110  &temps[1],
1111  &temps[2],
1112  &temps[3],
1113  &port,
1114  dummy);
1115  if (5 != cnt)
1116  return GNUNET_SYSERR;
1117  for (cnt = 0; cnt < 4; cnt++)
1118  if (temps[cnt] > 0xFF)
1119  return GNUNET_SYSERR;
1120  if (port > 65535)
1121  return GNUNET_SYSERR;
1122  r_buf->sin_family = AF_INET;
1123  r_buf->sin_port = htons (port);
1124  r_buf->sin_addr.s_addr =
1125  htonl ((temps[0] << 24) + (temps[1] << 16) + (temps[2] << 8) + temps[3]);
1126 #if HAVE_SOCKADDR_IN_SIN_LEN
1127  r_buf->sin_len = (u_char) sizeof(struct sockaddr_in);
1128 #endif
1129  return GNUNET_OK;
1130 }

Referenced by GNUNET_STRINGS_parse_socket_addr().

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

1152 {
1153  char *cp = GNUNET_strdup (addr);
1154 
1155  *af = AF_UNSPEC;
1156  if ('[' == *addr)
1157  {
1158  /* IPv6 */
1159  *sa = GNUNET_malloc (sizeof(struct sockaddr_in6));
1160  if (GNUNET_OK !=
1162  strlen (cp),
1163  (struct sockaddr_in6 *) *sa))
1164  {
1165  GNUNET_free (*sa);
1166  *sa = NULL;
1167  GNUNET_free (cp);
1168  return 0;
1169  }
1170  *af = AF_INET6;
1171  GNUNET_free (cp);
1172  return sizeof(struct sockaddr_in6);
1173  }
1174  else
1175  {
1176  /* IPv4 */
1177  *sa = GNUNET_malloc (sizeof(struct sockaddr_in));
1178  if (GNUNET_OK !=
1180  strlen (cp),
1181  (struct sockaddr_in *) *sa))
1182  {
1183  GNUNET_free (*sa);
1184  *sa = NULL;
1185  GNUNET_free (cp);
1186  return 0;
1187  }
1188  *af = AF_INET;
1189  GNUNET_free (cp);
1190  return sizeof(struct sockaddr_in);
1191  }
1192 }
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:1096
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:1033

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

Referenced by run().

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

◆ GNUNET_STRINGS_to_address_ip()

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

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

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

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

Definition at line 763 of file strings.c.

1137 {
1138  if (addr[0] == '[')
1139  return GNUNET_STRINGS_to_address_ipv6 (addr,
1140  addrlen,
1141  (struct sockaddr_in6 *) r_buf);
1142  return GNUNET_STRINGS_to_address_ipv4 (addr,
1143  addrlen,
1144  (struct sockaddr_in *) r_buf);
1145 }

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

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

1226 {
1227  char *const *new_argv =
1228  (char *const *) _make_continuous_arg_copy (argc, argv);
1229  *u8argv = new_argv;
1230  *u8argc = argc;
1231  return GNUNET_OK;
1232 }
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:1200

Referenced by main().

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

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

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

Here is the caller graph for this function:

◆ GNUNET_STRINGS_get_suffix_from_binary_name()

char* GNUNET_STRINGS_get_suffix_from_binary_name ( const char *  argv0)

Sometimes we use the binary name to determine which specific test to run.

In those cases, the string after the last "_" in 'argv[0]' specifies a string that determines the configuration file or plugin to use.

This function returns the respective substring, taking care of issues such as binaries ending in '.exe' on W32.

Parameters
argv0the name of the binary
Returns
string between the last '_' and the '.exe' (or the end of the string), NULL if argv0 has no '_'

Definition at line 1976 of file strings.c.

1977 {
1978  const char *ret;
1979  const char *dot;
1980 
1981  ret = strrchr (argv0, '_');
1982  if (NULL == ret)
1983  return NULL;
1984  ret++; /* skip underscore */
1985  dot = strchr (ret,
1986  '.');
1987  if (NULL != dot)
1988  return GNUNET_strndup (ret,
1989  dot - ret);
1990  return GNUNET_strdup (ret);
1991 }
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.

References GNUNET_strdup, GNUNET_strndup, and ret.

◆ GNUNET_STRINGS_parse_ipv4_policy()

struct GNUNET_STRINGS_IPv4NetworkPolicy* GNUNET_STRINGS_parse_ipv4_policy ( const char *  routeListX)

Parse an IPv4 network policy.

The argument specifies a list of subnets. The format is (network[/netmask][:[!]SPORT-DPORT];)* (no whitespace, must be terminated with a semicolon). The network must be given in dotted-decimal notation. The netmask can be given in CIDR notation (/16) or in dotted-decimal (/255.255.0.0).

Parameters
routeListXa string specifying the IPv4 subnets
Returns
the converted list, terminated with all zeros; NULL if the synatx is flawed

Definition at line 1290 of file strings.c.

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

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

Referenced by GNUNET_TUN_ipv4policy2regex(), and process_acl4().

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

◆ GNUNET_STRINGS_parse_ipv6_policy()

struct GNUNET_STRINGS_IPv6NetworkPolicy* GNUNET_STRINGS_parse_ipv6_policy ( const char *  routeListX)

Parse an IPv6 network policy.

The argument specifies a list of subnets. The format is (network[/netmask[:[!]SPORT[-DPORT]]];)* (no whitespace, must be terminated with a semicolon). The network must be given in colon-hex notation. The netmask must be given in CIDR notation (/16) or can be omitted to specify a single host. Note that the netmask is mandatory if ports are specified.

Parameters
routeListXa string specifying the policy
Returns
the converted list, 0-terminated, NULL if the synatx is flawed

Definition at line 1471 of file strings.c.

1472 {
1473  unsigned int count;
1474  unsigned int i;
1475  unsigned int len;
1476  unsigned int pos;
1477  int start;
1478  int slash;
1479  int ret;
1480  char *routeList;
1482  unsigned int bits;
1483  unsigned int off;
1484  int save;
1485  int colon;
1486  char dummy[2];
1487 
1488  if (NULL == routeListX)
1489  return NULL;
1490  len = strlen (routeListX);
1491  if (0 == len)
1492  return NULL;
1493  routeList = GNUNET_strdup (routeListX);
1494  count = 0;
1495  for (i = 0; i < len; i++)
1496  if (';' == routeList[i])
1497  count++;
1498  if (';' != routeList[len - 1])
1499  {
1501  _ ("Invalid network notation (does not end with ';': `%s')\n"),
1502  routeList);
1503  GNUNET_free (routeList);
1504  return NULL;
1505  }
1506 
1508  * (count + 1));
1509  i = 0;
1510  pos = 0;
1511  while (i < count)
1512  {
1513  start = pos;
1514  while (';' != routeList[pos])
1515  pos++;
1516  slash = pos;
1517  while ((slash >= start) && (routeList[slash] != '/'))
1518  slash--;
1519 
1520  if (slash < start)
1521  {
1522  memset (&result[i].netmask, 0xFF, sizeof(struct in6_addr));
1523  slash = pos;
1524  }
1525  else
1526  {
1527  routeList[pos] = '\0';
1528  for (colon = pos; ':' != routeList[colon]; colon--)
1529  if ('/' == routeList[colon])
1530  break;
1531  if (':' == routeList[colon])
1532  {
1533  routeList[colon] = '\0';
1534  if (GNUNET_OK !=
1535  parse_port_policy (&routeList[colon + 1], &result[i].pp))
1536  {
1537  GNUNET_free (result);
1538  GNUNET_free (routeList);
1539  return NULL;
1540  }
1541  }
1542  ret = inet_pton (AF_INET6, &routeList[slash + 1], &result[i].netmask);
1543  if (ret <= 0)
1544  {
1545  save = errno;
1546  if ((1 != sscanf (&routeList[slash + 1], "%u%1s", &bits, dummy)) ||
1547  (bits > 128))
1548  {
1549  if (0 == ret)
1551  _ ("Wrong format `%s' for netmask\n"),
1552  &routeList[slash + 1]);
1553  else
1554  {
1555  errno = save;
1556  LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "inet_pton");
1557  }
1558  GNUNET_free (result);
1559  GNUNET_free (routeList);
1560  return NULL;
1561  }
1562  off = 0;
1563  while (bits > 8)
1564  {
1565  result[i].netmask.s6_addr[off++] = 0xFF;
1566  bits -= 8;
1567  }
1568  while (bits > 0)
1569  {
1570  result[i].netmask.s6_addr[off] =
1571  (result[i].netmask.s6_addr[off] >> 1) + 0x80;
1572  bits--;
1573  }
1574  }
1575  }
1576  routeList[slash] = '\0';
1577  ret = inet_pton (AF_INET6, &routeList[start], &result[i].network);
1578  if (ret <= 0)
1579  {
1580  if (0 == ret)
1582  _ ("Wrong format `%s' for network\n"),
1583  &routeList[slash + 1]);
1584  else
1585  LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "inet_pton");
1586  GNUNET_free (result);
1587  GNUNET_free (routeList);
1588  return NULL;
1589  }
1590  pos++;
1591  i++;
1592  }
1593  GNUNET_free (routeList);
1594  return result;
1595 }
static void save()
Write persistent statistics to disk.
@ GNUNET_ERROR_TYPE_ERROR
network in CIDR notation for IPV6.
struct GNUNET_STRINGS_PortPolicy pp
Policy for port access.
struct in6_addr network
IPv6 address.
struct in6_addr netmask
IPv6 netmask.

References _, dummy, GNUNET_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().

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