2008-12-13 08:36:35 +00:00
|
|
|
/*
|
|
|
|
* Asterisk -- An open source telephony toolkit.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Tilghman Lesher <tlesher@digium.com>
|
|
|
|
*
|
|
|
|
* See http://www.asterisk.org for more information about
|
|
|
|
* the Asterisk project. Please do not directly contact
|
|
|
|
* any of the maintainers of this project for assistance;
|
|
|
|
* the project provides a web site, mailing lists and IRC
|
|
|
|
* channels for your use.
|
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file
|
|
|
|
*
|
|
|
|
* \brief String manipulation API
|
|
|
|
*
|
|
|
|
* \author Tilghman Lesher <tilghman@digium.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*** MAKEOPTS
|
2010-02-16 00:52:45 +00:00
|
|
|
<category name="MENUSELECT_CFLAGS" displayname="Compiler Flags" positive_output="yes">
|
|
|
|
<member name="DEBUG_OPAQUE" displayname="Change ast_str internals to detect improper usage" touch_on_change="include/asterisk/strings.h">
|
2008-12-13 08:36:35 +00:00
|
|
|
<defaultenabled>yes</defaultenabled>
|
|
|
|
</member>
|
|
|
|
</category>
|
|
|
|
***/
|
|
|
|
|
2012-06-15 16:20:16 +00:00
|
|
|
/*** MODULEINFO
|
|
|
|
<support_level>core</support_level>
|
|
|
|
***/
|
|
|
|
|
2008-12-13 08:36:35 +00:00
|
|
|
#include "asterisk.h"
|
|
|
|
|
|
|
|
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
|
|
|
|
|
|
|
|
#include "asterisk/strings.h"
|
|
|
|
#include "asterisk/pbx.h"
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* core handler for dynamic strings.
|
|
|
|
* This is not meant to be called directly, but rather through the
|
|
|
|
* various wrapper macros
|
|
|
|
* ast_str_set(...)
|
|
|
|
* ast_str_append(...)
|
|
|
|
* ast_str_set_va(...)
|
|
|
|
* ast_str_append_va(...)
|
|
|
|
*/
|
|
|
|
|
2009-03-11 00:29:59 +00:00
|
|
|
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
|
2010-02-17 21:22:40 +00:00
|
|
|
int __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
|
2009-03-11 00:29:59 +00:00
|
|
|
int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function)
|
|
|
|
#else
|
2010-02-17 21:22:40 +00:00
|
|
|
int __ast_str_helper(struct ast_str **buf, ssize_t max_len,
|
2008-12-13 08:36:35 +00:00
|
|
|
int append, const char *fmt, va_list ap)
|
2009-03-11 00:29:59 +00:00
|
|
|
#endif
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
|
|
|
int res, need;
|
2008-12-18 15:25:15 +00:00
|
|
|
int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0;
|
2008-12-13 08:36:35 +00:00
|
|
|
va_list aq;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (max_len < 0) {
|
2008-12-18 15:25:15 +00:00
|
|
|
max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Ask vsnprintf how much space we need. Remember that vsnprintf
|
2012-09-21 17:14:59 +00:00
|
|
|
* does not count the final <code>'\\0'</code> so we must add 1.
|
2008-12-13 08:36:35 +00:00
|
|
|
*/
|
|
|
|
va_copy(aq, ap);
|
2008-12-18 15:25:15 +00:00
|
|
|
res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq);
|
2008-12-13 08:36:35 +00:00
|
|
|
|
|
|
|
need = res + offset + 1;
|
|
|
|
/*
|
|
|
|
* If there is not enough space and we are below the max length,
|
|
|
|
* reallocate the buffer and return a message telling to retry.
|
|
|
|
*/
|
2008-12-18 15:25:15 +00:00
|
|
|
if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) {
|
2010-05-18 22:48:51 +00:00
|
|
|
int len = (int)(*buf)->__AST_STR_LEN;
|
2008-12-13 08:36:35 +00:00
|
|
|
if (max_len && max_len < need) { /* truncate as needed */
|
|
|
|
need = max_len;
|
|
|
|
} else if (max_len == 0) { /* if unbounded, give more room for next time */
|
|
|
|
need += 16 + need / 4;
|
|
|
|
}
|
|
|
|
if (0) { /* debugging */
|
2010-05-18 22:48:51 +00:00
|
|
|
ast_verbose("extend from %d to %d\n", len, need);
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
2009-03-11 00:29:59 +00:00
|
|
|
if (
|
|
|
|
#if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
|
|
|
|
_ast_str_make_space(buf, need, file, lineno, function)
|
|
|
|
#else
|
|
|
|
ast_str_make_space(buf, need)
|
|
|
|
#endif
|
|
|
|
) {
|
2010-05-18 22:48:51 +00:00
|
|
|
ast_verbose("failed to extend from %d to %d\n", len, need);
|
2010-02-16 23:44:33 +00:00
|
|
|
va_end(aq);
|
2008-12-13 08:36:35 +00:00
|
|
|
return AST_DYNSTR_BUILD_FAILED;
|
|
|
|
}
|
2008-12-18 15:25:15 +00:00
|
|
|
(*buf)->__AST_STR_STR[offset] = '\0'; /* Truncate the partial write. */
|
2008-12-13 08:36:35 +00:00
|
|
|
|
|
|
|
/* Restart va_copy before calling vsnprintf() again. */
|
|
|
|
va_end(aq);
|
|
|
|
continue;
|
|
|
|
}
|
2010-02-16 23:44:33 +00:00
|
|
|
va_end(aq);
|
2008-12-13 08:36:35 +00:00
|
|
|
break;
|
|
|
|
} while (1);
|
|
|
|
/* update space used, keep in mind the truncation */
|
2010-02-17 21:22:40 +00:00
|
|
|
(*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1: res + offset;
|
2008-12-13 08:36:35 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-02-17 21:22:40 +00:00
|
|
|
char *__ast_str_helper2(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
|
2008-12-13 08:36:35 +00:00
|
|
|
{
|
|
|
|
int dynamic = 0;
|
2008-12-18 15:25:15 +00:00
|
|
|
char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
|
2008-12-13 08:36:35 +00:00
|
|
|
|
2009-02-18 23:51:35 +00:00
|
|
|
if (maxlen < 1) {
|
|
|
|
if (maxlen == 0) {
|
|
|
|
dynamic = 1;
|
|
|
|
}
|
2008-12-18 15:25:15 +00:00
|
|
|
maxlen = (*buf)->__AST_STR_LEN;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
|
|
|
|
if (escapecommas && (*src == '\\' || *src == ',')) {
|
|
|
|
*ptr++ = '\\';
|
|
|
|
maxlen--;
|
2008-12-18 15:25:15 +00:00
|
|
|
(*buf)->__AST_STR_USED++;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
*ptr++ = *src++;
|
|
|
|
maxsrc--;
|
|
|
|
maxlen--;
|
2008-12-18 15:25:15 +00:00
|
|
|
(*buf)->__AST_STR_USED++;
|
|
|
|
|
2009-04-02 15:14:22 +00:00
|
|
|
if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) ||
|
|
|
|
(dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) {
|
2009-02-16 23:48:54 +00:00
|
|
|
char *oldbase = (*buf)->__AST_STR_STR;
|
2008-12-18 15:25:15 +00:00
|
|
|
size_t old = (*buf)->__AST_STR_LEN;
|
|
|
|
if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
|
2008-12-13 08:36:35 +00:00
|
|
|
/* If the buffer can't be extended, end it. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* What we extended the buffer by */
|
|
|
|
maxlen = old;
|
2009-02-16 23:48:54 +00:00
|
|
|
|
2009-02-18 23:51:35 +00:00
|
|
|
ptr += (*buf)->__AST_STR_STR - oldbase;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-01 20:13:28 +00:00
|
|
|
if (__builtin_expect(!maxlen, 0)) {
|
2008-12-13 08:36:35 +00:00
|
|
|
ptr--;
|
|
|
|
}
|
|
|
|
*ptr = '\0';
|
2008-12-18 15:25:15 +00:00
|
|
|
return (*buf)->__AST_STR_STR;
|
2008-12-13 08:36:35 +00:00
|
|
|
}
|
|
|
|
|
2013-08-02 14:08:34 +00:00
|
|
|
static int str_hash(const void *obj, const int flags)
|
|
|
|
{
|
|
|
|
return ast_str_hash(obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int str_cmp(void *lhs, void *rhs, int flags)
|
|
|
|
{
|
|
|
|
return strcmp(lhs, rhs) ? 0 : CMP_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ao2_container *ast_str_container_alloc_options(enum ao2_container_opts opts, int buckets)
|
|
|
|
{
|
|
|
|
return ao2_container_alloc_options(opts, buckets, str_hash, str_cmp);
|
|
|
|
}
|
2013-05-21 18:00:22 +00:00
|
|
|
|
2013-08-02 14:08:34 +00:00
|
|
|
int ast_str_container_add(struct ao2_container *str_container, const char *add)
|
|
|
|
{
|
|
|
|
RAII_VAR(char *, ao2_add, ao2_alloc(strlen(add) + 1, NULL), ao2_cleanup);
|
|
|
|
|
|
|
|
if (!ao2_add) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* safe strcpy */
|
|
|
|
strcpy(ao2_add, add);
|
|
|
|
ao2_link(str_container, ao2_add);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ast_str_container_remove(struct ao2_container *str_container, const char *remove)
|
|
|
|
{
|
|
|
|
ao2_find(str_container, remove, OBJ_KEY | OBJ_NODATA | OBJ_UNLINK);
|
|
|
|
}
|