00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _ASTERISK_STRINGS_H
00024 #define _ASTERISK_STRINGS_H
00025
00026
00027
00028 #include <ctype.h>
00029
00030 #include "asterisk/utils.h"
00031 #include "asterisk/threadstorage.h"
00032
00033 #if defined(DEBUG_OPAQUE)
00034 #define __AST_STR_USED used2
00035 #define __AST_STR_LEN len2
00036 #define __AST_STR_STR str2
00037 #define __AST_STR_TS ts2
00038 #else
00039 #define __AST_STR_USED used
00040 #define __AST_STR_LEN len
00041 #define __AST_STR_STR str
00042 #define __AST_STR_TS ts
00043 #endif
00044
00045
00046
00047 #define AS_OR(a,b) (a && ast_str_strlen(a)) ? ast_str_buffer(a) : (b)
00048
00049 #ifdef AST_DEVMODE
00050 #define ast_strlen_zero(foo) _ast_strlen_zero(foo, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00051 static force_inline int _ast_strlen_zero(const char *s, const char *file, const char *function, int line)
00052 {
00053 if (!s || (*s == '\0')) {
00054 return 1;
00055 }
00056 if (!strcmp(s, "(null)")) {
00057 ast_log(__LOG_WARNING, file, line, function, "Possible programming error: \"(null)\" is not NULL!\n");
00058 }
00059 return 0;
00060 }
00061
00062 #else
00063 static force_inline int attribute_pure ast_strlen_zero(const char *s)
00064 {
00065 return (!s || (*s == '\0'));
00066 }
00067 #endif
00068
00069 #ifdef SENSE_OF_HUMOR
00070 #define ast_strlen_real(a) (a) ? strlen(a) : 0
00071 #define ast_strlen_imaginary(a) ast_random()
00072 #endif
00073
00074
00075
00076
00077 #define S_OR(a, b) ({typeof(&((a)[0])) __x = (a); ast_strlen_zero(__x) ? (b) : __x;})
00078
00079
00080
00081
00082
00083 #define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
00084
00085
00086
00087
00088
00089
00090 AST_INLINE_API(
00091 char * attribute_pure ast_skip_blanks(const char *str),
00092 {
00093 while (*str && ((unsigned char) *str) < 33)
00094 str++;
00095 return (char *) str;
00096 }
00097 )
00098
00099
00100
00101
00102
00103
00104 AST_INLINE_API(
00105 char *ast_trim_blanks(char *str),
00106 {
00107 char *work = str;
00108
00109 if (work) {
00110 work += strlen(work) - 1;
00111
00112
00113
00114
00115
00116
00117 while ((work >= str) && ((unsigned char) *work) < 33)
00118 *(work--) = '\0';
00119 }
00120 return str;
00121 }
00122 )
00123
00124
00125
00126
00127
00128
00129 AST_INLINE_API(
00130 char * attribute_pure ast_skip_nonblanks(const char *str),
00131 {
00132 while (*str && ((unsigned char) *str) > 32)
00133 str++;
00134 return (char *) str;
00135 }
00136 )
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 AST_INLINE_API(
00148 char *ast_strip(char *s),
00149 {
00150 if ((s = ast_skip_blanks(s))) {
00151 ast_trim_blanks(s);
00152 }
00153 return s;
00154 }
00155 )
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
00182
00183
00184
00185
00186
00187
00188 char *ast_unescape_semicolon(char *s);
00189
00190
00191
00192
00193
00194
00195 char *ast_unescape_c(char *s);
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 AST_INLINE_API(
00213 void ast_copy_string(char *dst, const char *src, size_t size),
00214 {
00215 while (*src && size) {
00216 *dst++ = *src++;
00217 size--;
00218 }
00219 if (__builtin_expect(!size, 0))
00220 dst--;
00221 *dst = '\0';
00222 }
00223 )
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253 int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap) __attribute__((format(printf, 3, 0)));
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265 int attribute_pure ast_true(const char *val);
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 int attribute_pure ast_false(const char *val);
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289 void ast_join(char *s, size_t len, const char * const w[]);
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310 int ast_get_timeval(const char *src, struct timeval *tv, struct timeval _default, int *consumed);
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364 struct ast_str {
00365 size_t __AST_STR_LEN;
00366 size_t __AST_STR_USED;
00367 struct ast_threadstorage *__AST_STR_TS;
00368 #define DS_MALLOC ((struct ast_threadstorage *)1)
00369 #define DS_ALLOCA ((struct ast_threadstorage *)2)
00370 #define DS_STATIC ((struct ast_threadstorage *)3)
00371 char __AST_STR_STR[0];
00372 };
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00386 #define ast_str_create(a) _ast_str_create(a,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00387 AST_INLINE_API(
00388 struct ast_str * attribute_malloc _ast_str_create(size_t init_len,
00389 const char *file, int lineno, const char *func),
00390 {
00391 struct ast_str *buf;
00392
00393 buf = (struct ast_str *)__ast_calloc(1, sizeof(*buf) + init_len, file, lineno, func);
00394 if (buf == NULL)
00395 return NULL;
00396
00397 buf->__AST_STR_LEN = init_len;
00398 buf->__AST_STR_USED = 0;
00399 buf->__AST_STR_TS = DS_MALLOC;
00400
00401 return buf;
00402 }
00403 )
00404 #else
00405 AST_INLINE_API(
00406 struct ast_str * attribute_malloc ast_str_create(size_t init_len),
00407 {
00408 struct ast_str *buf;
00409
00410 buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len);
00411 if (buf == NULL)
00412 return NULL;
00413
00414 buf->__AST_STR_LEN = init_len;
00415 buf->__AST_STR_USED = 0;
00416 buf->__AST_STR_TS = DS_MALLOC;
00417
00418 return buf;
00419 }
00420 )
00421 #endif
00422
00423
00424
00425
00426 AST_INLINE_API(
00427 void ast_str_reset(struct ast_str *buf),
00428 {
00429 if (buf) {
00430 buf->__AST_STR_USED = 0;
00431 if (buf->__AST_STR_LEN) {
00432 buf->__AST_STR_STR[0] = '\0';
00433 }
00434 }
00435 }
00436 )
00437
00438
00439
00440
00441 AST_INLINE_API(
00442 void ast_str_update(struct ast_str *buf),
00443 {
00444 buf->__AST_STR_USED = strlen(buf->__AST_STR_STR);
00445 }
00446 )
00447
00448
00449
00450
00451 AST_INLINE_API(
00452 void ast_str_trim_blanks(struct ast_str *buf),
00453 {
00454 if (!buf) {
00455 return;
00456 }
00457 while (buf->__AST_STR_USED && buf->__AST_STR_STR[buf->__AST_STR_USED - 1] < 33) {
00458 buf->__AST_STR_STR[--(buf->__AST_STR_USED)] = '\0';
00459 }
00460 }
00461 )
00462
00463
00464
00465
00466 AST_INLINE_API(
00467 size_t attribute_pure ast_str_strlen(const struct ast_str *buf),
00468 {
00469 return buf->__AST_STR_USED;
00470 }
00471 )
00472
00473
00474
00475
00476
00477 AST_INLINE_API(
00478 size_t attribute_pure ast_str_size(const struct ast_str *buf),
00479 {
00480 return buf->__AST_STR_LEN;
00481 }
00482 )
00483
00484
00485
00486
00487
00488 AST_INLINE_API(
00489 char * attribute_pure ast_str_buffer(const struct ast_str *buf),
00490 {
00491
00492
00493
00494
00495 return (char *) buf->__AST_STR_STR;
00496 }
00497 )
00498
00499
00500
00501
00502
00503
00504
00505
00506 AST_INLINE_API(
00507 char *ast_str_truncate(struct ast_str *buf, ssize_t len),
00508 {
00509 if (len < 0) {
00510 if ((typeof(buf->__AST_STR_USED)) -len >= buf->__AST_STR_USED) {
00511 buf->__AST_STR_USED = 0;
00512 } else {
00513 buf->__AST_STR_USED += len;
00514 }
00515 } else {
00516 buf->__AST_STR_USED = len;
00517 }
00518 buf->__AST_STR_STR[buf->__AST_STR_USED] = '\0';
00519 return buf->__AST_STR_STR;
00520 }
00521 )
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531 #if defined(DEBUG_THREADLOCALS)
00532 #define _DB1(x) x
00533 #else
00534 #define _DB1(x)
00535 #endif
00536
00537
00538
00539
00540 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00541 AST_INLINE_API(
00542 int _ast_str_make_space(struct ast_str **buf, size_t new_len, const char *file, int lineno, const char *function),
00543 {
00544 struct ast_str *old_buf = *buf;
00545
00546 if (new_len <= (*buf)->__AST_STR_LEN)
00547 return 0;
00548 if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
00549 return -1;
00550 *buf = (struct ast_str *)__ast_realloc(*buf, new_len + sizeof(struct ast_str), file, lineno, function);
00551 if (*buf == NULL) {
00552 *buf = old_buf;
00553 return -1;
00554 }
00555 if ((*buf)->__AST_STR_TS != DS_MALLOC) {
00556 pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
00557 _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
00558 }
00559
00560 (*buf)->__AST_STR_LEN = new_len;
00561 return 0;
00562 }
00563 )
00564 #define ast_str_make_space(a,b) _ast_str_make_space(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00565 #else
00566 AST_INLINE_API(
00567 int ast_str_make_space(struct ast_str **buf, size_t new_len),
00568 {
00569 struct ast_str *old_buf = *buf;
00570
00571 if (new_len <= (*buf)->__AST_STR_LEN)
00572 return 0;
00573 if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
00574 return -1;
00575 *buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
00576 if (*buf == NULL) {
00577 *buf = old_buf;
00578 return -1;
00579 }
00580 if ((*buf)->__AST_STR_TS != DS_MALLOC) {
00581 pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
00582 _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
00583 }
00584
00585 (*buf)->__AST_STR_LEN = new_len;
00586 return 0;
00587 }
00588 )
00589 #endif
00590
00591 AST_INLINE_API(
00592 int ast_str_copy_string(struct ast_str **dst, struct ast_str *src),
00593 {
00594
00595
00596 if (src->__AST_STR_USED + 1 > (*dst)->__AST_STR_LEN) {
00597 if (ast_str_make_space(dst, src->__AST_STR_USED + 1)) {
00598 return -1;
00599 }
00600 }
00601
00602 memcpy((*dst)->__AST_STR_STR, src->__AST_STR_STR, src->__AST_STR_USED + 1);
00603 (*dst)->__AST_STR_USED = src->__AST_STR_USED;
00604 return 0;
00605 }
00606 )
00607
00608 #define ast_str_alloca(init_len) \
00609 ({ \
00610 struct ast_str *__ast_str_buf; \
00611 __ast_str_buf = alloca(sizeof(*__ast_str_buf) + init_len); \
00612 __ast_str_buf->__AST_STR_LEN = init_len; \
00613 __ast_str_buf->__AST_STR_USED = 0; \
00614 __ast_str_buf->__AST_STR_TS = DS_ALLOCA; \
00615 __ast_str_buf->__AST_STR_STR[0] = '\0'; \
00616 (__ast_str_buf); \
00617 })
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650 #if !defined(DEBUG_THREADLOCALS)
00651 AST_INLINE_API(
00652 struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
00653 size_t init_len),
00654 {
00655 struct ast_str *buf;
00656
00657 buf = (struct ast_str *)ast_threadstorage_get(ts, sizeof(*buf) + init_len);
00658 if (buf == NULL)
00659 return NULL;
00660
00661 if (!buf->__AST_STR_LEN) {
00662 buf->__AST_STR_LEN = init_len;
00663 buf->__AST_STR_USED = 0;
00664 buf->__AST_STR_TS = ts;
00665 }
00666
00667 return buf;
00668 }
00669 )
00670 #else
00671 AST_INLINE_API(
00672 struct ast_str *__ast_str_thread_get(struct ast_threadstorage *ts,
00673 size_t init_len, const char *file, const char *function, unsigned int line),
00674 {
00675 struct ast_str *buf;
00676
00677 buf = (struct ast_str *)__ast_threadstorage_get(ts, sizeof(*buf) + init_len, file, function, line);
00678 if (buf == NULL)
00679 return NULL;
00680
00681 if (!buf->__AST_STR_LEN) {
00682 buf->__AST_STR_LEN = init_len;
00683 buf->__AST_STR_USED = 0;
00684 buf->__AST_STR_TS = ts;
00685 }
00686
00687 return buf;
00688 }
00689 )
00690
00691 #define ast_str_thread_get(ts, init_len) __ast_str_thread_get(ts, init_len, __FILE__, __PRETTY_FUNCTION__, __LINE__)
00692 #endif
00693
00694
00695
00696
00697
00698
00699
00700 enum {
00701
00702
00703 AST_DYNSTR_BUILD_FAILED = -1,
00704
00705
00706
00707
00708
00709 AST_DYNSTR_BUILD_RETRY = -2
00710 };
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733 #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
00734 int __attribute__((format(printf, 4, 0))) __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
00735 int append, const char *fmt, va_list ap, const char *file, int lineno, const char *func);
00736 #define __ast_str_helper(a,b,c,d,e) __ast_debug_str_helper(a,b,c,d,e,__FILE__,__LINE__,__PRETTY_FUNCTION__)
00737 #else
00738 int __attribute__((format(printf, 4, 0))) __ast_str_helper(struct ast_str **buf, ssize_t max_len,
00739 int append, const char *fmt, va_list ap);
00740 #endif
00741 char *__ast_str_helper2(struct ast_str **buf, ssize_t max_len,
00742 const char *src, size_t maxsrc, int append, int escapecommas);
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782 AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_set_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap),
00783 {
00784 return __ast_str_helper(buf, max_len, 0, fmt, ap);
00785 }
00786 )
00787
00788
00789
00790
00791
00792
00793 AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_append_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap),
00794 {
00795 return __ast_str_helper(buf, max_len, 1, fmt, ap);
00796 }
00797 )
00798
00799
00800 AST_INLINE_API(char *ast_str_set_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
00801 {
00802 return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 0);
00803 }
00804 )
00805
00806
00807 AST_INLINE_API(char *ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
00808 {
00809 return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 0);
00810 }
00811 )
00812
00813
00814 AST_INLINE_API(char *ast_str_set_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
00815 {
00816 return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 1);
00817 }
00818 )
00819
00820
00821 AST_INLINE_API(char *ast_str_append_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
00822 {
00823 return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 1);
00824 }
00825 )
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844 AST_INLINE_API(
00845 int __attribute__((format(printf, 3, 4))) ast_str_set(
00846 struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
00847 {
00848 int res;
00849 va_list ap;
00850
00851 va_start(ap, fmt);
00852 res = ast_str_set_va(buf, max_len, fmt, ap);
00853 va_end(ap);
00854
00855 return res;
00856 }
00857 )
00858
00859
00860
00861
00862
00863
00864
00865 AST_INLINE_API(
00866 int __attribute__((format(printf, 3, 4))) ast_str_append(
00867 struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
00868 {
00869 int res;
00870 va_list ap;
00871
00872 va_start(ap, fmt);
00873 res = ast_str_append_va(buf, max_len, fmt, ap);
00874 va_end(ap);
00875
00876 return res;
00877 }
00878 )
00879
00880
00881
00882
00883
00884
00885
00886 AST_INLINE_API(
00887 int ast_check_digits(const char *arg),
00888 {
00889 while (*arg) {
00890 if (*arg < '0' || *arg > '9') {
00891 return 0;
00892 }
00893 arg++;
00894 }
00895 return 1;
00896 }
00897 )
00898
00899
00900
00901
00902
00903
00904 AST_INLINE_API(
00905 char *ast_tech_to_upper(char *dev_str),
00906 {
00907 char *pos;
00908 if (!dev_str || !strchr(dev_str, '/')) {
00909 return dev_str;
00910 }
00911
00912 for (pos = dev_str; *pos && *pos != '/'; pos++) {
00913 *pos = toupper(*pos);
00914 }
00915 return dev_str;
00916 }
00917 )
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927 static force_inline int attribute_pure ast_str_hash(const char *str)
00928 {
00929 int hash = 5381;
00930
00931 while (*str)
00932 hash = hash * 33 ^ *str++;
00933
00934 return abs(hash);
00935 }
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952 static force_inline int ast_str_hash_add(const char *str, int hash)
00953 {
00954 while (*str)
00955 hash = hash * 33 ^ *str++;
00956
00957 return abs(hash);
00958 }
00959
00960
00961
00962
00963
00964
00965
00966
00967 static force_inline int attribute_pure ast_str_case_hash(const char *str)
00968 {
00969 int hash = 5381;
00970
00971 while (*str) {
00972 hash = hash * 33 ^ tolower(*str++);
00973 }
00974
00975 return abs(hash);
00976 }
00977
00978 #endif