Flowgrind
Advanced TCP traffic generator
fg_string.c File Reference

Functions to manipulate strings used by Flowgrind. More...

#include "config.h"
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "fg_string.h"
#include "fg_definitions.h"

Go to the source code of this file.

Functions

int asprintf_append (char **strp, const char *fmt,...)
 
size_t fmtlen (const char *fmt, va_list ap)
 Determine the number of characters that would be generated by a printf with format string fmt and arguments ap. More...
 
char * strdup_append (char *s, const char *a)
 Append the duplication of string a to the given string s. More...
 
static char * strlendup_append (char *s, size_t slen, const char *a, size_t alen)
 Append the duplication of string a of length alen to the given string s at point slen. More...
 
char * strndup_append (char *s, const char *a, size_t n)
 Append at most n characters of the duplication of string a to the given string s. More...
 
int vasprintf_append (char **strp, const char *fmt, va_list ap)
 

Detailed Description

Functions to manipulate strings used by Flowgrind.

Definition in file fg_string.c.

Function Documentation

◆ asprintf_append()

int asprintf_append ( char **  strp,
const char *  fmt,
  ... 
)

Definition at line 98 of file fg_string.c.

99 {
100  va_list ap;
101 
102  va_start(ap, fmt);
103  size_t length = vasprintf_append(strp, fmt, ap);
104  va_end(ap);
105 
106  return length;
107 }

◆ fmtlen()

size_t fmtlen ( char const *  fmt,
va_list  ap 
)

Determine the number of characters that would be generated by a printf with format string fmt and arguments ap.

Parameters
[in]fmtformat string
[in]apparameters used to fill fmt
Returns
number of number of characters

Definition at line 61 of file fg_string.c.

62 {
63  va_list ap2;
64  char c;
65 
66  /* If the output of vsnprintf is truncated, its return value is the
67  * number of characters which would have been written to the string
68  * if enough space had been available */
69  va_copy(ap2, ap);
70  size_t length = vsnprintf(&c, 1, fmt, ap2);
71  va_end(ap2);
72 
73  return length;
74 }

◆ strdup_append()

char* strdup_append ( char *  s,
const char *  a 
)

Append the duplication of string a to the given string s.

Parameters
[in]sdestination string to append to
[in]asource string to be append
Returns
concatenated strings, NULL on error

Definition at line 76 of file fg_string.c.

77 {
78  if (unlikely(!s))
79  return strdup(a);
80 
81  if (unlikely(!a))
82  return s;
83 
84  return strlendup_append(s, strlen(s), a, strlen(a));
85 }

◆ strlendup_append()

static char* strlendup_append ( char *  s,
size_t  slen,
const char *  a,
size_t  alen 
)
inlinestatic

Append the duplication of string a of length alen to the given string s at point slen.

Parameters
[in]sdestination string to append to
[in]slenlength of string s
[in]asource string to be append
[in]alenlength of string a

Definition at line 47 of file fg_string.c.

49 {
50  char *ret = realloc(s, slen + alen + 1);
51  if (unlikely(!ret))
52  return NULL;
53 
54  /* append the string and the trailing \0 */
55  memcpy(&ret[slen], a, alen);
56  ret[slen+alen] = 0;
57 
58  return ret;
59 }

◆ strndup_append()

char* strndup_append ( char *  s,
const char *  a,
size_t  n 
)

Append at most n characters of the duplication of string a to the given string s.

Parameters
[in]sdestination string to append to
[in]asource string to be append
[in]nnumber of characters to be append from the string
Returns
concatenated strings, NULL on error

Definition at line 87 of file fg_string.c.

88 {
89  if (unlikely(!s))
90  return strdup(a);
91 
92  if (unlikely(!a))
93  return s;
94 
95  return strlendup_append(s, strlen(s), a, strnlen(a, n));
96 }

◆ vasprintf_append()

int vasprintf_append ( char **  strp,
const char *  fmt,
va_list  ap 
)

Definition at line 109 of file fg_string.c.

110 {
111  if (unlikely(!(*strp)))
112  return vasprintf(strp, fmt, ap);
113 
114  size_t slen = strlen(*strp);
115  size_t alen = fmtlen(fmt, ap);
116 
117  /* The format resulted in no characters being formatted */
118  if (unlikely(alen == 0))
119  return -1;
120 
121  char *new_strp = realloc(*strp, slen + alen + 1);
122  if (unlikely(!(*new_strp)))
123  return -1;
124 
125  *strp = new_strp;
126 
127  va_list ap2;
128  va_copy(ap2, ap);
129  size_t length = vsnprintf(*strp + slen, alen + 1, fmt, ap2);
130  va_end(ap2);
131 
132  return length;
133 }
vasprintf_append
int vasprintf_append(char **strp, const char *fmt, va_list ap)
Definition: fg_string.c:109
unlikely
#define unlikely(x)
These macros gain us a few percent of speed.
Definition: fg_definitions.h:35
strlendup_append
static char * strlendup_append(char *s, size_t slen, const char *a, size_t alen)
Append the duplication of string a of length alen to the given string s at point slen.
Definition: fg_string.c:47
fmtlen
size_t fmtlen(const char *fmt, va_list ap)
Determine the number of characters that would be generated by a printf with format string fmt and arg...
Definition: fg_string.c:61