GNUnet  0.11.x
Data Structures | Macros | Functions | Variables
strings.c File Reference

string functions More...

#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_strings_lib.h"
#include <unicase.h>
#include <unistr.h>
#include <uniconv.h>
Include dependency graph for strings.c:

Go to the source code of this file.

Data Structures

struct  ConversionTable
 Unit conversion table entry for 'convert_with_table'. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-strings", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)   GNUNET_log_from_strerror (kind, "util-strings", syscall)
 
#define FILLCHAR   '='
 ******************** Base64 encoding More...
 
#define cvtfind(a)
 
#define CHECK_CRLF
 

Functions

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...
 
char * GNUNET_STRINGS_pp2s (const struct GNUNET_PeerIdentity *pids, unsigned int num_pids)
 Convert a peer path to a human-readable string. 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...
 
char * GNUNET_STRINGS_byte_size_fancy (unsigned long long size)
 Convert a given filesize into a fancy human-readable format. More...
 
size_t GNUNET_strlcpy (char *dst, const char *src, size_t n)
 Like strlcpy but portable. More...
 
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 then multiplying the numbers with the values associated with the respective unit from the conversion table. More...
 
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...
 
int GNUNET_STRINGS_fancy_time_to_absolute (const char *fancy_time, struct GNUNET_TIME_Absolute *atime)
 Convert a given fancy human-readable time to our internal representation. 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 lowercase. More...
 
void GNUNET_STRINGS_utf8_toupper (const char *input, char *output)
 Convert the utf-8 input string to uppercase. More...
 
char * GNUNET_STRINGS_filename_expand (const char *fil)
 Complete filename (a la shell) from abbrevition. More...
 
const char * GNUNET_STRINGS_relative_time_to_string (struct GNUNET_TIME_Relative delta, int do_round)
 Give relative time in human-readable fancy format. More...
 
const char * GNUNET_STRINGS_absolute_time_to_string (struct GNUNET_TIME_Absolute t)
 "asctime", except for GNUnet time. More...
 
const char * GNUNET_STRINGS_get_short_name (const char *filename)
 "man basename" Returns a pointer to a part of filename (allocates nothing)! More...
 
static unsigned int getValue__ (unsigned char a)
 Get the decoded value corresponding to a character according to Crockford Base32 encoding. 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 Crockford Base32 encoding. More...
 
char * GNUNET_STRINGS_data_to_string_alloc (const void *buf, size_t size)
 Return the base32crockford encoding of the given buffer. More...
 
int GNUNET_STRINGS_string_to_data (const char *enc, size_t enclen, void *out, size_t out_size)
 Convert Crockford Base32hex encoding back to data. More...
 
int GNUNET_STRINGS_parse_uri (const char *path, char **scheme_part, const char **path_part)
 Parse a path that might be an URI. More...
 
int GNUNET_STRINGS_path_is_absolute (const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
 Check whether filename is absolute or not, and if it's an URI. More...
 
int GNUNET_STRINGS_check_filename (const char *filename, enum GNUNET_STRINGS_FilenameCheck checks)
 Perform checks on filename. More...
 
int GNUNET_STRINGS_to_address_ipv6 (const char *zt_addr, uint16_t addrlen, struct sockaddr_in6 *r_buf)
 Tries to convert zt_addr string to an IPv6 address. More...
 
int GNUNET_STRINGS_to_address_ipv4 (const char *zt_addr, uint16_t addrlen, struct sockaddr_in *r_buf)
 Tries to convert 'zt_addr' string to an IPv4 address. More...
 
int GNUNET_STRINGS_to_address_ip (const char *addr, uint16_t addrlen, struct sockaddr_storage *r_buf)
 Tries to convert addr string to an IP (v4 or v6) address. More...
 
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...
 
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 GNUNET_free();. More...
 
int GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
 Returns utf-8 encoded arguments. More...
 
static int parse_port_policy (const char *port_policy, struct GNUNET_STRINGS_PortPolicy *pp)
 Parse the given port policy. 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...
 
size_t GNUNET_STRINGS_base64_encode (const void *in, size_t len, char **output)
 Encode into Base64. More...
 
size_t GNUNET_STRINGS_base64_decode (const char *data, size_t len, void **out)
 Decode from Base64. More...
 

Variables

static char * cvt
 

Detailed Description

string functions

Author
Nils Durner
Christian Grothoff

Definition in file strings.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util-strings", __VA_ARGS__)

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)    GNUNET_log_from_strerror (kind, "util-strings", syscall)

◆ FILLCHAR

#define FILLCHAR   '='

******************** Base64 encoding

Definition at line 1839 of file strings.c.

Referenced by GNUNET_STRINGS_base64_decode(), and GNUNET_STRINGS_base64_encode().

◆ cvtfind

#define cvtfind (   a)
Value:
((((a) >= 'A') && ((a) <= 'Z')) \
? (a) - 'A' \
: (((a) >= 'a') && ((a) <= 'z')) \
? (a) - 'a' + 26 \
: (((a) >= '0') && ((a) <= '9')) \
? (a) - '0' + 52 \
: ((a) == '+') ? 62 : ((a) == '/') ? 63 : -1)

Definition at line 1900 of file strings.c.

Referenced by GNUNET_STRINGS_base64_decode().

◆ CHECK_CRLF

#define CHECK_CRLF
Value:
while (data[i] == '\r' || data[i] == '\n') \
{ \
"ignoring CR/LF\n"); \
i++; \
if (i >= len) \
goto END; \
}
We&#39;re done processing.
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...

Referenced by GNUNET_STRINGS_base64_decode().

Function Documentation

◆ convert_with_table()

static int convert_with_table ( const char *  input,
const struct ConversionTable table,
unsigned long long *  output 
)
static

Convert a string of the form "4 X 5 Y" into a numeric value by interpreting "X" and "Y" as units and then multiplying the numbers with the values associated with the respective unit from the conversion table.

Parameters
inputinput string to parse
tabletable with the conversion of unit names to numbers
outputwhere to store the result
Returns
GNUNET_OK on success, GNUNET_SYSERR on error

Definition at line 260 of file strings.c.

References GNUNET_free, GNUNET_OK, GNUNET_strdup, GNUNET_SYSERR, GNUNET_YES, ConversionTable::name, ret, and ConversionTable::value.

Referenced by GNUNET_STRINGS_fancy_size_to_bytes(), and GNUNET_STRINGS_fancy_time_to_relative().

263 {
264  unsigned long long ret;
265  char *in;
266  const char *tok;
267  unsigned long long last;
268  unsigned int i;
269 
270  ret = 0;
271  last = 0;
272  in = GNUNET_strdup (input);
273  for (tok = strtok (in, " "); tok != NULL; tok = strtok (NULL, " "))
274  {
275  do
276  {
277  i = 0;
278  while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
279  i++;
280  if (table[i].name != NULL)
281  {
282  last *= table[i].value;
283  break; /* next tok */
284  }
285  else
286  {
287  char *endptr;
288  ret += last;
289  errno = 0;
290  last = strtoull (tok, &endptr, 10);
291  if ((0 != errno) || (endptr == tok))
292  {
293  GNUNET_free (in);
294  return GNUNET_SYSERR; /* expected number */
295  }
296  if ('\0' == endptr[0])
297  break; /* next tok */
298  else
299  tok = endptr; /* and re-check (handles times like "10s") */
300  }
301  }
302  while (GNUNET_YES);
303  }
304  ret += last;
305  *output = ret;
306  GNUNET_free (in);
307  return GNUNET_OK;
308 }
unsigned long long value
Factor to apply for this unit.
Definition: strings.c:244
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
const char * name
#define GNUNET_YES
Definition: gnunet_common.h:77
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function:

◆ getValue__()

static unsigned int getValue__ ( unsigned char  a)
static

Get the decoded value corresponding to a character according to Crockford Base32 encoding.

Parameters
aa character
Returns
corresponding numeric value

Definition at line 794 of file strings.c.

References dec.

Referenced by GNUNET_STRINGS_string_to_data().

795 {
796  unsigned int dec;
797 
798  switch (a)
799  {
800  case 'O':
801  case 'o':
802  a = '0';
803  break;
804 
805  case 'i':
806  case 'I':
807  case 'l':
808  case 'L':
809  a = '1';
810  break;
811 
812  /* also consider U to be V */
813  case 'u':
814  case 'U':
815  a = 'V';
816  break;
817 
818  default:
819  break;
820  }
821  if ((a >= '0') && (a <= '9'))
822  return a - '0';
823  if ((a >= 'a') && (a <= 'z'))
824  a = toupper (a);
825  /* return (a - 'a' + 10); */
826  dec = 0;
827  if ((a >= 'A') && (a <= 'Z'))
828  {
829  if ('I' < a)
830  dec++;
831  if ('L' < a)
832  dec++;
833  if ('O' < a)
834  dec++;
835  if ('U' < a)
836  dec++;
837  return(a - 'A' + 10 - dec);
838  }
839  return -1;
840 }
static OpusDecoder * dec
OPUS decoder.
Here is the caller graph for this function:

◆ _make_continuous_arg_copy()

static char* const* _make_continuous_arg_copy ( int  argc,
char *const *  argv 
)
static

Makes a copy of argv that consists of a single memory chunk that can be freed with a single call to GNUNET_free();.

Definition at line 1401 of file strings.c.

References GNUNET_malloc, and p.

Referenced by GNUNET_STRINGS_get_utf8_args().

1402 {
1403  size_t argvsize = 0;
1404  int i;
1405  char **new_argv;
1406  char *p;
1407 
1408  for (i = 0; i < argc; i++)
1409  argvsize += strlen (argv[i]) + 1 + sizeof(char *);
1410  new_argv = GNUNET_malloc (argvsize + sizeof(char *));
1411  p = (char *) &new_argv[argc + 1];
1412  for (i = 0; i < argc; i++)
1413  {
1414  new_argv[i] = p;
1415  strcpy (p, argv[i]);
1416  p += strlen (argv[i]) + 1;
1417  }
1418  new_argv[argc] = NULL;
1419  return (char *const *) new_argv;
1420 }
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-qr.c:59
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ parse_port_policy()

static int parse_port_policy ( const char *  port_policy,
struct GNUNET_STRINGS_PortPolicy pp 
)
static

Parse the given port policy.

The format is "[!]SPORT[-DPORT]".

Parameters
port_policystring to parse
pppolicy to fill in
Returns
GNUNET_OK on success, GNUNET_SYSERR if the port_policy is malformed

Definition at line 1461 of file strings.c.

References _, e, GNUNET_STRINGS_PortPolicy::end_port, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, GNUNET_STRINGS_PortPolicy::negate_portrange, and GNUNET_STRINGS_PortPolicy::start_port.

Referenced by GNUNET_STRINGS_parse_ipv4_policy(), and GNUNET_STRINGS_parse_ipv6_policy().

1463 {
1464  const char *pos;
1465  int s;
1466  int e;
1467  char eol[2];
1468 
1469  pos = port_policy;
1470  if ('!' == *pos)
1471  {
1473  pos++;
1474  }
1475  if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol))
1476  {
1477  if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF))
1478  {
1479  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1480  return GNUNET_SYSERR;
1481  }
1482  pp->start_port = (uint16_t) s;
1483  pp->end_port = (uint16_t) e;
1484  return GNUNET_OK;
1485  }
1486  if (1 == sscanf (pos, "%u%1s", &s, eol))
1487  {
1488  if ((0 == s) || (s > 0xFFFF))
1489  {
1490  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1491  return GNUNET_SYSERR;
1492  }
1493 
1494  pp->start_port = (uint16_t) s;
1495  pp->end_port = (uint16_t) s;
1496  return GNUNET_OK;
1497  }
1499  _ ("Malformed port policy `%s'\n"),
1500  port_policy);
1501  return GNUNET_SYSERR;
1502 }
uint16_t start_port
Starting port range (0 if none given).
int negate_portrange
GNUNET_YES if the port range should be negated ("!" in policy).
static struct Experiment * e
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
uint16_t end_port
End of port range (0 if none given).
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:77
Here is the caller graph for this function:

Variable Documentation

◆ cvt

char* cvt
static
Initial value:
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/"

Definition at line 1840 of file strings.c.

Referenced by GNUNET_STRINGS_base64_encode().