summaryrefslogtreecommitdiffstats
path: root/third_party/sipcc/cpr_string.h
blob: de6b1cc8a0ec87dc0546926e4583ea9b3f533fab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef _CPR_STRING_H_
#define _CPR_STRING_H_

#include <stdarg.h>

#include "cpr_types.h"
#include "cpr_strings.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * sstrncpy
 *
 * @brief The CPR wrapper for strncpy
 *
 * This is Cisco's *safe* version of strncpy.  The string will always
 * be NUL terminated (which is not ANSI compliant).
 *
 * @param[in] dst  - The destination string
 * @param[in] src  - The source
 * @param[in]  max - maximum length in octets to concatenate
 *
 * @return     Pointer to the @b end of the string
 *
 * @note       Modified to be explicitly safe for all inputs.
 *             Also return the number of characters copied excluding the
 *             NUL terminator vs. the original string s1.  This simplifies
 *             code where sstrncat functions follow.
 */
unsigned long
sstrncpy(char *dst, const char *src, unsigned long max);


/**
 * sstrncat
 *
 * @brief The CPR wrapper for strncat
 *
 * This is Cisco's *safe* version of strncat.  The string will always
 * be NUL terminated (which is not ANSI compliant).
 *
 * @param[in] s1  - first string
 * @param[in] s2  - second string
 * @param[in]  max - maximum length in octets to concatenate
 *
 * @return     Pointer to the @b end of the string
 *
 * @note    Modified to be explicitly safe for all inputs.
 *             Also return the end vs. the beginning of the string s1
 *             which is useful for multiple sstrncat calls.
 */
char *
sstrncat(char *s1, const char *s2, unsigned long max);


/*
 * flex_string
 */
#define FLEX_STRING_CHUNK_SIZE 256

typedef struct {
  char *buffer;
  size_t buffer_length;
  size_t string_length;
} flex_string;

/*
 * flex_string_init
 *
 * Not thread-safe
 */
void flex_string_init(flex_string *fs);

/*
 * flex_string_free
 *
 * Not thread-safe
 */
void flex_string_free(flex_string *fs);

/*
 * flex_string_check_alloc
 *
 * Allocate enough chunks to hold the new minimum size.
 *
 * Not thread-safe
 */
void flex_string_check_alloc(flex_string *fs, size_t new_min_length);

/*
 * flex_string_append
 *
 * Not thread-safe
 */
void flex_string_append(flex_string *fs, const char *more);

/*
 * flex_string_sprintf
 *
 * Not thread-safe
 */
void flex_string_vsprintf(flex_string *fs, const char *format, va_list original_ap);

/*
 * flex_string_sprintf
 *
 * Not thread-safe
 */
void flex_string_sprintf(flex_string *fs, const char *format, ...);


/* From cpr_linux_string.h */
/* cpr_strdup
 *
 * @brief The CPR wrapper for strdup

 * The cpr_strdup shall return a pointer to a new string, which is a duplicate
 * of the string pointed to by "str" argument. A null pointer is returned if the
 * new string cannot be created.
 *
 * @param[in] str  - The string that needs to be duplicated
 *
 * @return The duplicated string or NULL in case of no memory
 *
 */
char *
cpr_strdup(const char *str);

#ifdef __cplusplus
}
#endif

#endif