2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2008, Digium, Inc.
6 * Tilghman Lesher <tlesher@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief String manipulation API
23 * \author Tilghman Lesher <tilghman@digium.com>
27 <category name="MENUSELECT_CFLAGS" displayname="Compiler Flags" positive_output="yes">
28 <member name="DEBUG_OPAQUE" displayname="Change ast_str internals to detect improper usage" touch_on_change="include/asterisk/strings.h">
29 <defaultenabled>yes</defaultenabled>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/strings.h"
39 #include "asterisk/pbx.h"
42 * core handler for dynamic strings.
43 * This is not meant to be called directly, but rather through the
44 * various wrapper macros
48 * ast_str_append_va(...)
51 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
52 int __ast_debug_str_helper(struct ast_str **buf, size_t max_len,
53 int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function)
55 int __ast_str_helper(struct ast_str **buf, size_t max_len,
56 int append, const char *fmt, va_list ap)
60 int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0;
65 max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */
68 * Ask vsnprintf how much space we need. Remember that vsnprintf
69 * does not count the final <code>'\0'</code> so we must add 1.
72 res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq);
74 need = res + offset + 1;
76 * If there is not enough space and we are below the max length,
77 * reallocate the buffer and return a message telling to retry.
79 if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) {
80 if (max_len && max_len < need) { /* truncate as needed */
82 } else if (max_len == 0) { /* if unbounded, give more room for next time */
83 need += 16 + need / 4;
85 if (0) { /* debugging */
86 ast_verbose("extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
89 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
90 _ast_str_make_space(buf, need, file, lineno, function)
92 ast_str_make_space(buf, need)
95 ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need);
97 return AST_DYNSTR_BUILD_FAILED;
99 (*buf)->__AST_STR_STR[offset] = '\0'; /* Truncate the partial write. */
101 /* Restart va_copy before calling vsnprintf() again. */
108 /* update space used, keep in mind the truncation */
109 (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN : res + offset;
114 char *__ast_str_helper2(struct ast_str **buf, size_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
117 char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
123 maxlen = (*buf)->__AST_STR_LEN;
126 while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
127 if (escapecommas && (*src == '\\' || *src == ',')) {
130 (*buf)->__AST_STR_USED++;
135 (*buf)->__AST_STR_USED++;
137 if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) ||
138 (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) {
139 char *oldbase = (*buf)->__AST_STR_STR;
140 size_t old = (*buf)->__AST_STR_LEN;
141 if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
142 /* If the buffer can't be extended, end it. */
145 /* What we extended the buffer by */
148 ptr += (*buf)->__AST_STR_STR - oldbase;
151 if (__builtin_expect(!maxlen, 0)) {
155 return (*buf)->__AST_STR_STR;