00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "asterisk.h"
00035
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 263904 $")
00037
00038 #include "asterisk/strings.h"
00039 #include "asterisk/pbx.h"
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00052 int __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
00053 int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function)
00054 #else
00055 int __ast_str_helper(struct ast_str **buf, ssize_t max_len,
00056 int append, const char *fmt, va_list ap)
00057 #endif
00058 {
00059 int res, need;
00060 int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0;
00061 va_list aq;
00062
00063 do {
00064 if (max_len < 0) {
00065 max_len = (*buf)->__AST_STR_LEN;
00066 }
00067
00068
00069
00070
00071 va_copy(aq, ap);
00072 res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq);
00073
00074 need = res + offset + 1;
00075
00076
00077
00078
00079 if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) {
00080 int len = (int)(*buf)->__AST_STR_LEN;
00081 if (max_len && max_len < need) {
00082 need = max_len;
00083 } else if (max_len == 0) {
00084 need += 16 + need / 4;
00085 }
00086 if (0) {
00087 ast_verbose("extend from %d to %d\n", len, need);
00088 }
00089 if (
00090 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00091 _ast_str_make_space(buf, need, file, lineno, function)
00092 #else
00093 ast_str_make_space(buf, need)
00094 #endif
00095 ) {
00096 ast_verbose("failed to extend from %d to %d\n", len, need);
00097 va_end(aq);
00098 return AST_DYNSTR_BUILD_FAILED;
00099 }
00100 (*buf)->__AST_STR_STR[offset] = '\0';
00101
00102
00103 va_end(aq);
00104 continue;
00105 }
00106 va_end(aq);
00107 break;
00108 } while (1);
00109
00110 (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1: res + offset;
00111
00112 return res;
00113 }
00114
00115 char *__ast_str_helper2(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
00116 {
00117 int dynamic = 0;
00118 char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
00119
00120 if (maxlen < 1) {
00121 if (maxlen == 0) {
00122 dynamic = 1;
00123 }
00124 maxlen = (*buf)->__AST_STR_LEN;
00125 }
00126
00127 while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
00128 if (escapecommas && (*src == '\\' || *src == ',')) {
00129 *ptr++ = '\\';
00130 maxlen--;
00131 (*buf)->__AST_STR_USED++;
00132 }
00133 *ptr++ = *src++;
00134 maxsrc--;
00135 maxlen--;
00136 (*buf)->__AST_STR_USED++;
00137
00138 if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) ||
00139 (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) {
00140 char *oldbase = (*buf)->__AST_STR_STR;
00141 size_t old = (*buf)->__AST_STR_LEN;
00142 if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
00143
00144 break;
00145 }
00146
00147 maxlen = old;
00148
00149 ptr += (*buf)->__AST_STR_STR - oldbase;
00150 }
00151 }
00152 if (__builtin_expect(!maxlen, 0)) {
00153 ptr--;
00154 }
00155 *ptr = '\0';
00156 return (*buf)->__AST_STR_STR;
00157 }
00158