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

string functions More...

#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "gnunet_buffer_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...
 
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 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 then multiplying the numbers with the values associated with the respective unit from the conversion table. More...
 
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...
 
enum GNUNET_GenericReturnValue 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...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_timestamp (const char *fancy_time, struct GNUNET_TIME_Timestamp *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...
 
char * GNUNET_STRINGS_utf8_normalize (const char *input)
 Normalize the utf-8 input string to NFC. 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...
 
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)
 Like 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 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...
 
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...
 
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...
 
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...
 
enum GNUNET_GenericReturnValue GNUNET_STRINGS_get_utf8_args (int argc, char *const *argv, int *u8argc, char *const **u8argv)
 Returns utf-8 encoded arguments. More...
 
static enum GNUNET_GenericReturnValue 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_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 **out)
 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...
 
size_t GNUNET_STRINGS_urlencode (const char *data, size_t len, char **out)
 url/percent encode (RFC3986). 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__)

Definition at line 38 of file strings.c.

◆ LOG_STRERROR

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

Definition at line 40 of file strings.c.

◆ FILLCHAR

#define FILLCHAR   '='

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

Definition at line 1601 of file strings.c.

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

◆ CHECK_CRLF

#define CHECK_CRLF
Value:
while (data[i] == '\r' || data[i] == '\n') \
{ \
"ignoring CR/LF\n"); \
i++; \
if (i >= len) \
goto END; \
}
@ END
We'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...
@ GNUNET_ERROR_TYPE_BULK
@ GNUNET_ERROR_TYPE_DEBUG

Function Documentation

◆ convert_with_table()

static enum GNUNET_GenericReturnValue 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 139 of file strings.c.

185 {
186  unsigned long long ret;
187  char *in;
188  const char *tok;
189  unsigned long long last;
190  unsigned int i;
191  char *sptr;
192 
193  ret = 0;
194  last = 0;
195  in = GNUNET_strdup (input);
196  for (tok = strtok_r (in, " ", &sptr);
197  tok != NULL;
198  tok = strtok_r (NULL, " ", &sptr))
199  {
200  do
201  {
202  i = 0;
203  while ((table[i].name != NULL) && (0 != strcasecmp (table[i].name, tok)))
204  i++;
205  if (table[i].name != NULL)
206  {
207  last *= table[i].value;
208  break; /* next tok */
209  }
210  else
211  {
212  char *endptr;
213  ret += last;
214  errno = 0;
215  last = strtoull (tok, &endptr, 10);
216  if ((0 != errno) || (endptr == tok))
217  {
218  GNUNET_free (in);
219  return GNUNET_SYSERR; /* expected number */
220  }
221  if ('\0' == endptr[0])
222  break; /* next tok */
223  else
224  tok = endptr; /* and re-check (handles times like "10s") */
225  }
226  }
227  while (GNUNET_YES);
228  }
229  ret += last;
230  *output = ret;
231  GNUNET_free (in);
232  return GNUNET_OK;
233 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_free(ptr)
Wrapper around free.
static struct PeerEntry ** table
Table with our interned peer IDs.
Definition: peer.c:55
const char * name

References GNUNET_assert, and strnlen().

Here is the call 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 659 of file strings.c.

660 {
661  unsigned int dec;
662 
663  switch (a)
664  {
665  case 'O':
666  case 'o':
667  a = '0';
668  break;
669 
670  case 'i':
671  case 'I':
672  case 'l':
673  case 'L':
674  a = '1';
675  break;
676 
677  /* also consider U to be V */
678  case 'u':
679  case 'U':
680  a = 'V';
681  break;
682 
683  default:
684  break;
685  }
686  if ((a >= '0') && (a <= '9'))
687  return a - '0';
688  if ((a >= 'a') && (a <= 'z'))
689  a = toupper (a);
690  /* return (a - 'a' + 10); */
691  dec = 0;
692  if ((a >= 'A') && (a <= 'Z'))
693  {
694  if ('I' < a)
695  dec++;
696  if ('L' < a)
697  dec++;
698  if ('O' < a)
699  dec++;
700  if ('U' < a)
701  dec++;
702  return(a - 'A' + 10 - dec);
703  }
704  return -1;
705 }
static OpusDecoder * dec
OPUS decoder.

References dec.

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

1202 {
1203  size_t argvsize = 0;
1204  char **new_argv;
1205  char *p;
1206 
1207  for (int i = 0; i < argc; i++)
1208  argvsize += strlen (argv[i]) + 1 + sizeof(char *);
1209  new_argv = GNUNET_malloc (argvsize + sizeof(char *));
1210  p = (char *) &new_argv[argc + 1];
1211  for (int i = 0; i < argc; i++)
1212  {
1213  new_argv[i] = p;
1214  strcpy (p, argv[i]);
1215  p += strlen (argv[i]) + 1;
1216  }
1217  new_argv[argc] = NULL;
1218  return (char *const *) new_argv;
1219 }
static struct GNUNET_OS_Process * p
Helper process we started.
Definition: gnunet-uri.c:37
#define GNUNET_malloc(size)
Wrapper around malloc.

References GNUNET_malloc, and p.

◆ parse_port_policy()

static enum GNUNET_GenericReturnValue 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 1201 of file strings.c.

1248 {
1249  const char *pos;
1250  int s;
1251  int e;
1252  char eol[2];
1253 
1254  pos = port_policy;
1255  if ('!' == *pos)
1256  {
1258  pos++;
1259  }
1260  if (2 == sscanf (pos, "%u-%u%1s", &s, &e, eol))
1261  {
1262  if ((0 == s) || (s > 0xFFFF) || (e < s) || (e > 0xFFFF))
1263  {
1264  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1265  return GNUNET_SYSERR;
1266  }
1267  pp->start_port = (uint16_t) s;
1268  pp->end_port = (uint16_t) e;
1269  return GNUNET_OK;
1270  }
1271  if (1 == sscanf (pos, "%u%1s", &s, eol))
1272  {
1273  if ((0 == s) || (s > 0xFFFF))
1274  {
1275  GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _ ("Port not in range\n"));
1276  return GNUNET_SYSERR;
1277  }
1278 
1279  pp->start_port = (uint16_t) s;
1280  pp->end_port = (uint16_t) s;
1281  return GNUNET_OK;
1282  }
1284  _ ("Malformed port policy `%s'\n"),
1285  port_policy);
1286  return GNUNET_SYSERR;
1287 }
static struct Experiment * e
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_WARNING
#define _(String)
GNU gettext support macro.
Definition: platform.h:177
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).
uint16_t end_port
End of port range (0 if none given).

Referenced by GNUNET_STRINGS_parse_ipv4_policy(), and GNUNET_STRINGS_parse_ipv6_policy().

Here is the caller graph for this function:

Variable Documentation

◆ cvt

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

Definition at line 1602 of file strings.c.

Referenced by GNUNET_STRINGS_base64_encode().