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