| 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 |
#include "asterisk.h" |
| 39 |
|
| 40 |
#include <regex.h> |
| 41 |
#include "asterisk/strings.h" |
| 42 |
#include "asterisk/pbx.h" |
| 43 |
#include "asterisk/vector.h" |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
int __ast_str_helper(struct ast_str **buf, ssize_t max_len, |
| 56 |
int append, const char *fmt, va_list ap, |
| 57 |
const char *file, int lineno, const char *function) |
| 58 |
{ |
| 59 |
int res; |
| 60 |
int added; |
| 61 |
int need; |
| 62 |
int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0; |
| 63 |
va_list aq; |
| 64 |
|
| 65 |
if (max_len < 0) { |
| 66 |
max_len = (*buf)->__AST_STR_LEN; |
| 67 |
} |
| 68 |
|
| 69 |
do { |
| 70 |
va_copy(aq, ap); |
| 71 |
res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq); |
| 72 |
va_end(aq); |
| 73 |
|
| 74 |
if (res < 0) { |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
res = AST_DYNSTR_BUILD_FAILED; |
| 80 |
added = 0; |
| 81 |
break; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
added = res; |
| 90 |
need = offset + added + 1; |
| 91 |
if (need <= (*buf)->__AST_STR_LEN |
| 92 |
|| (max_len && max_len <= (*buf)->__AST_STR_LEN)) { |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
if (max_len == 0) { |
| 102 |
|
| 103 |
need += 16 + need / 4; |
| 104 |
} else if (max_len < need) { |
| 105 |
|
| 106 |
need = max_len; |
| 107 |
} |
| 108 |
|
| 109 |
if (_ast_str_make_space(buf, need, file, lineno, function)) { |
| 110 |
ast_log_safe(LOG_VERBOSE, "failed to extend from %d to %d\n", |
| 111 |
(int) (*buf)->__AST_STR_LEN, need); |
| 112 |
|
| 113 |
res = AST_DYNSTR_BUILD_FAILED; |
| 114 |
break; |
| 115 |
} |
| 116 |
} while (1); |
| 117 |
|
| 118 |
|
| 119 |
(*buf)->__AST_STR_USED = ((*buf)->__AST_STR_LEN <= offset + added) |
| 120 |
? (*buf)->__AST_STR_LEN - 1 |
| 121 |
: offset + added; |
| 122 |
|
| 123 |
|
| 124 |
(*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED] = '\0'; |
| 125 |
|
| 126 |
return res; |
| 127 |
} |
| 128 |
|
| 129 |
char *__ast_str_helper2(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas) |
| 130 |
{ |
| 131 |
int dynamic = 0; |
| 132 |
char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR; |
| 133 |
|
| 134 |
if (maxlen < 1) { |
| 135 |
if (maxlen == 0) { |
| 136 |
dynamic = 1; |
| 137 |
} |
| 138 |
maxlen = (*buf)->__AST_STR_LEN; |
| 139 |
} |
| 140 |
|
| 141 |
while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) { |
| 142 |
if (escapecommas && (*src == '\\' || *src == ',')) { |
| 143 |
*ptr++ = '\\'; |
| 144 |
maxlen--; |
| 145 |
(*buf)->__AST_STR_USED++; |
| 146 |
} |
| 147 |
*ptr++ = *src++; |
| 148 |
maxsrc--; |
| 149 |
maxlen--; |
| 150 |
(*buf)->__AST_STR_USED++; |
| 151 |
|
| 152 |
if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) || |
| 153 |
(dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) { |
| 154 |
char *oldbase = (*buf)->__AST_STR_STR; |
| 155 |
size_t old = (*buf)->__AST_STR_LEN; |
| 156 |
if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) { |
| 157 |
|
| 158 |
break; |
| 159 |
} |
| 160 |
|
| 161 |
maxlen = old; |
| 162 |
|
| 163 |
ptr += (*buf)->__AST_STR_STR - oldbase; |
| 164 |
} |
| 165 |
} |
| 166 |
if (__builtin_expect(!maxlen, 0)) { |
| 167 |
ptr--; |
| 168 |
} |
| 169 |
*ptr = '\0'; |
| 170 |
return (*buf)->__AST_STR_STR; |
| 171 |
} |
| 172 |
|
| 173 |
static int str_hash(const void *obj, const int flags) |
| 174 |
{ |
| 175 |
return ast_str_hash(obj); |
| 176 |
} |
| 177 |
|
| 178 |
static int str_sort(const void *lhs, const void *rhs, int flags) |
| 179 |
{ |
| 180 |
if ((flags & OBJ_SEARCH_MASK) == OBJ_SEARCH_PARTIAL_KEY) { |
| 181 |
return strncmp(lhs, rhs, strlen(rhs)); |
| 182 |
} else { |
| 183 |
return strcmp(lhs, rhs); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static int str_cmp(void *lhs, void *rhs, int flags) |
| 188 |
{ |
| 189 |
int cmp = 0; |
| 190 |
|
| 191 |
if ((flags & OBJ_SEARCH_MASK) == OBJ_SEARCH_PARTIAL_KEY) { |
| 192 |
cmp = strncmp(lhs, rhs, strlen(rhs)); |
| 193 |
} else { |
| 194 |
cmp = strcmp(lhs, rhs); |
| 195 |
} |
| 196 |
|
| 197 |
return cmp ? 0 : CMP_MATCH; |
| 198 |
} |
| 199 |
|
| 200 |
struct ao2_container *ast_str_container_alloc_options(enum ao2_alloc_opts opts, int buckets) |
| 201 |
{ |
| 202 |
return ao2_container_alloc_hash(opts, 0, buckets, str_hash, str_sort, str_cmp); |
| 203 |
} |
| 204 |
|
| 205 |
int ast_str_container_add(struct ao2_container *str_container, const char *add) |
| 206 |
{ |
| 207 |
char *ao2_add; |
| 208 |
|
| 209 |
|
| 210 |
ao2_add = ao2_alloc_options(strlen(add) + 1, NULL, AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 211 |
if (!ao2_add) { |
| 212 |
return -1; |
| 213 |
} |
| 214 |
strcpy(ao2_add, add); |
| 215 |
|
| 216 |
ao2_link(str_container, ao2_add); |
| 217 |
ao2_ref(ao2_add, -1); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
void ast_str_container_remove(struct ao2_container *str_container, const char *remove) |
| 222 |
{ |
| 223 |
ao2_find(str_container, remove, OBJ_SEARCH_KEY | OBJ_NODATA | OBJ_UNLINK); |
| 224 |
} |
| 225 |
|
| 226 |
char *ast_generate_random_string(char *buf, size_t size) |
| 227 |
{ |
| 228 |
int i; |
| 229 |
|
| 230 |
for (i = 0; i < size - 1; ++i) { |
| 231 |
buf[i] = 'a' + (ast_random() % 26); |
| 232 |
} |
| 233 |
buf[i] = '\0'; |
| 234 |
|
| 235 |
return buf; |
| 236 |
} |
| 237 |
|
| 238 |
int ast_strings_equal(const char *str1, const char *str2) |
| 239 |
{ |
| 240 |
if (!str1 || !str2) { |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
return str1 == str2 || !strcmp(str1, str2); |
| 245 |
} |
| 246 |
|
| 247 |
static int parse_double(const char *input, double *result) |
| 248 |
{ |
| 249 |
char *endptr; |
| 250 |
|
| 251 |
errno = 0; |
| 252 |
*result = strtod(input, &endptr); |
| 253 |
if (*endptr || errno == ERANGE) { |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
return 1; |
| 258 |
} |
| 259 |
|
| 260 |
int ast_strings_match(const char *left, const char *op, const char *right) |
| 261 |
{ |
| 262 |
char *internal_op = (char *)op; |
| 263 |
char *internal_right = (char *)right; |
| 264 |
double left_num; |
| 265 |
double right_num; |
| 266 |
int scan_numeric = 0; |
| 267 |
|
| 268 |
if (!(left && right)) { |
| 269 |
return 0; |
| 270 |
} |
| 271 |
|
| 272 |
if (ast_strlen_zero(op)) { |
| 273 |
if (ast_strlen_zero(left) && ast_strlen_zero(right)) { |
| 274 |
return 1; |
| 275 |
} |
| 276 |
|
| 277 |
if (strlen(right) >= 2 && right[0] == '/' && right[strlen(right) - 1] == '/') { |
| 278 |
internal_op = "regex"; |
| 279 |
internal_right = ast_strdupa(right); |
| 280 |
|
| 281 |
internal_right++; |
| 282 |
internal_right[strlen(internal_right) - 1] = '\0'; |
| 283 |
goto regex; |
| 284 |
} else { |
| 285 |
internal_op = "="; |
| 286 |
goto equals; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
if (!strcasecmp(op, "like")) { |
| 291 |
char *tok; |
| 292 |
struct ast_str *buffer = ast_str_alloca(128); |
| 293 |
|
| 294 |
if (!strchr(right, '%')) { |
| 295 |
return !strcmp(left, right); |
| 296 |
} else { |
| 297 |
internal_op = "regex"; |
| 298 |
internal_right = ast_strdupa(right); |
| 299 |
tok = strsep(&internal_right, "%"); |
| 300 |
ast_str_set(&buffer, 0, "^%s", tok); |
| 301 |
|
| 302 |
while ((tok = strsep(&internal_right, "%"))) { |
| 303 |
ast_str_append(&buffer, 0, ".*%s", tok); |
| 304 |
} |
| 305 |
ast_str_append(&buffer, 0, "%s", "$"); |
| 306 |
|
| 307 |
internal_right = ast_str_buffer(buffer); |
| 308 |
|
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
regex: |
| 313 |
if (!strcasecmp(internal_op, "regex")) { |
| 314 |
regex_t expression; |
| 315 |
int rc; |
| 316 |
|
| 317 |
if (regcomp(&expression, internal_right, REG_EXTENDED | REG_NOSUB)) { |
| 318 |
return 0; |
| 319 |
} |
| 320 |
|
| 321 |
rc = regexec(&expression, left, 0, NULL, 0); |
| 322 |
regfree(&expression); |
| 323 |
return !rc; |
| 324 |
} |
| 325 |
|
| 326 |
equals: |
| 327 |
scan_numeric = parse_double(left, &left_num) |
| 328 |
&& parse_double(internal_right, &right_num); |
| 329 |
|
| 330 |
if (internal_op[0] == '=') { |
| 331 |
if (ast_strlen_zero(left) && ast_strlen_zero(internal_right)) { |
| 332 |
return 1; |
| 333 |
} |
| 334 |
|
| 335 |
if (scan_numeric) { |
| 336 |
return (left_num == right_num); |
| 337 |
} else { |
| 338 |
return (!strcmp(left, internal_right)); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
if (internal_op[0] == '!' && internal_op[1] == '=') { |
| 343 |
if (scan_numeric) { |
| 344 |
return (left_num != right_num); |
| 345 |
} else { |
| 346 |
return !!strcmp(left, internal_right); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if (internal_op[0] == '<') { |
| 351 |
if (scan_numeric) { |
| 352 |
if (internal_op[1] == '=') { |
| 353 |
return (left_num <= right_num); |
| 354 |
} else { |
| 355 |
return (left_num < right_num); |
| 356 |
} |
| 357 |
} else { |
| 358 |
if (internal_op[1] == '=') { |
| 359 |
return strcmp(left, internal_right) <= 0; |
| 360 |
} else { |
| 361 |
return strcmp(left, internal_right) < 0; |
| 362 |
} |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
if (internal_op[0] == '>') { |
| 367 |
if (scan_numeric) { |
| 368 |
if (internal_op[1] == '=') { |
| 369 |
return (left_num >= right_num); |
| 370 |
} else { |
| 371 |
return (left_num > right_num); |
| 372 |
} |
| 373 |
} else { |
| 374 |
if (internal_op[1] == '=') { |
| 375 |
return strcmp(left, internal_right) >= 0; |
| 376 |
} else { |
| 377 |
return strcmp(left, internal_right) > 0; |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
return 0; |
| 383 |
} |
| 384 |
|
| 385 |
char *ast_read_line_from_buffer(char **buffer) |
| 386 |
{ |
| 387 |
char *start = *buffer; |
| 388 |
|
| 389 |
if (!buffer || !*buffer || *(*buffer) == '\0') { |
| 390 |
return NULL; |
| 391 |
} |
| 392 |
|
| 393 |
while (*(*buffer) && *(*buffer) != '\n' ) { |
| 394 |
(*buffer)++; |
| 395 |
} |
| 396 |
|
| 397 |
*(*buffer) = '\0'; |
| 398 |
if (*(*buffer - 1) == '\r') { |
| 399 |
*(*buffer - 1) = '\0'; |
| 400 |
} |
| 401 |
(*buffer)++; |
| 402 |
|
| 403 |
return start; |
| 404 |
} |
| 405 |
|
| 406 |
char *ast_vector_string_join(struct ast_vector_string *vec, const char *delim) |
| 407 |
{ |
| 408 |
struct ast_str *buf = ast_str_create(256); |
| 409 |
char *rtn; |
| 410 |
int i; |
| 411 |
|
| 412 |
if (!buf) { |
| 413 |
return NULL; |
| 414 |
} |
| 415 |
|
| 416 |
for (i = 0; i < AST_VECTOR_SIZE(vec); i++) { |
| 417 |
ast_str_append(&buf, 0, "%s%s", AST_VECTOR_GET(vec, i), delim); |
| 418 |
} |
| 419 |
ast_str_truncate(buf, -strlen(delim)); |
| 420 |
rtn = ast_strdup(ast_str_buffer(buf)); |
| 421 |
ast_free(buf); |
| 422 |
return rtn; |
| 423 |
} |
| 424 |
|
| 425 |
int ast_vector_string_split(struct ast_vector_string *dest, |
| 426 |
const char *input, const char *delim, int flags, |
| 427 |
int (*excludes_cmp)(const char *s1, const char *s2)) |
| 428 |
{ |
| 429 |
char *buf; |
| 430 |
char *cur; |
| 431 |
int no_trim = flags & AST_VECTOR_STRING_SPLIT_NO_TRIM; |
| 432 |
int allow_empty = flags & AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY; |
| 433 |
|
| 434 |
ast_assert(dest != NULL); |
| 435 |
ast_assert(!ast_strlen_zero(delim)); |
| 436 |
|
| 437 |
if (ast_strlen_zero(input)) { |
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
buf = ast_strdupa(input); |
| 442 |
while ((cur = strsep(&buf, delim))) { |
| 443 |
if (!no_trim) { |
| 444 |
cur = ast_strip(cur); |
| 445 |
} |
| 446 |
|
| 447 |
if (!allow_empty && ast_strlen_zero(cur)) { |
| 448 |
continue; |
| 449 |
} |
| 450 |
|
| 451 |
if (excludes_cmp && AST_VECTOR_GET_CMP(dest, cur, !excludes_cmp)) { |
| 452 |
continue; |
| 453 |
} |
| 454 |
|
| 455 |
cur = ast_strdup(cur); |
| 456 |
if (!cur || AST_VECTOR_APPEND(dest, cur)) { |
| 457 |
ast_free(cur); |
| 458 |
|
| 459 |
return -1; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
return 0; |
| 464 |
} |
| 465 |
|
| 466 |
int ast_in_delimited_string(const char *needle, const char *haystack, char delim) |
| 467 |
{ |
| 468 |
const char *end; |
| 469 |
unsigned long needle_size; |
| 470 |
|
| 471 |
ast_assert(haystack != NULL); |
| 472 |
|
| 473 |
if (!needle) { |
| 474 |
return 0; |
| 475 |
} |
| 476 |
|
| 477 |
needle_size = strlen(needle); |
| 478 |
haystack = ast_skip_blanks(haystack); |
| 479 |
|
| 480 |
while ((end = strchr(haystack, delim))) { |
| 481 |
if (needle_size == end - haystack && !strncmp(haystack, needle, needle_size)) { |
| 482 |
return 1; |
| 483 |
} |
| 484 |
haystack = ast_skip_blanks(end + 1); |
| 485 |
} |
| 486 |
|
| 487 |
return strcmp(haystack, needle) ? 0 : -1; |
| 488 |
} |
| 489 |
|