strings.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Tilghman Lesher <tlesher@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief String manipulation API
  21. *
  22. * \author Tilghman Lesher <tilghman@digium.com>
  23. */
  24. /*** MAKEOPTS
  25. <category name="MENUSELECT_CFLAGS" displayname="Compiler Flags" positive_output="yes">
  26. <member name="DEBUG_OPAQUE" displayname="Change ast_str internals to detect improper usage" touch_on_change="include/asterisk/strings.h">
  27. <defaultenabled>yes</defaultenabled>
  28. </member>
  29. </category>
  30. ***/
  31. /*** MODULEINFO
  32. <support_level>core</support_level>
  33. ***/
  34. #include "asterisk.h"
  35. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  36. #include "asterisk/strings.h"
  37. #include "asterisk/pbx.h"
  38. /*!
  39. * core handler for dynamic strings.
  40. * This is not meant to be called directly, but rather through the
  41. * various wrapper macros
  42. * ast_str_set(...)
  43. * ast_str_append(...)
  44. * ast_str_set_va(...)
  45. * ast_str_append_va(...)
  46. */
  47. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  48. int __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
  49. int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function)
  50. #else
  51. int __ast_str_helper(struct ast_str **buf, ssize_t max_len,
  52. int append, const char *fmt, va_list ap)
  53. #endif
  54. {
  55. int res, need;
  56. int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0;
  57. va_list aq;
  58. do {
  59. if (max_len < 0) {
  60. max_len = (*buf)->__AST_STR_LEN; /* don't exceed the allocated space */
  61. }
  62. /*
  63. * Ask vsnprintf how much space we need. Remember that vsnprintf
  64. * does not count the final <code>'\0'</code> so we must add 1.
  65. */
  66. va_copy(aq, ap);
  67. res = vsnprintf((*buf)->__AST_STR_STR + offset, (*buf)->__AST_STR_LEN - offset, fmt, aq);
  68. need = res + offset + 1;
  69. /*
  70. * If there is not enough space and we are below the max length,
  71. * reallocate the buffer and return a message telling to retry.
  72. */
  73. if (need > (*buf)->__AST_STR_LEN && (max_len == 0 || (*buf)->__AST_STR_LEN < max_len) ) {
  74. int len = (int)(*buf)->__AST_STR_LEN;
  75. if (max_len && max_len < need) { /* truncate as needed */
  76. need = max_len;
  77. } else if (max_len == 0) { /* if unbounded, give more room for next time */
  78. need += 16 + need / 4;
  79. }
  80. if (
  81. #if (defined(MALLOC_DEBUG) && !defined(STANDALONE))
  82. _ast_str_make_space(buf, need, file, lineno, function)
  83. #else
  84. ast_str_make_space(buf, need)
  85. #endif
  86. ) {
  87. ast_log_safe(LOG_VERBOSE, "failed to extend from %d to %d\n", len, need);
  88. va_end(aq);
  89. return AST_DYNSTR_BUILD_FAILED;
  90. }
  91. (*buf)->__AST_STR_STR[offset] = '\0'; /* Truncate the partial write. */
  92. /* Restart va_copy before calling vsnprintf() again. */
  93. va_end(aq);
  94. continue;
  95. }
  96. va_end(aq);
  97. break;
  98. } while (1);
  99. /* update space used, keep in mind the truncation */
  100. (*buf)->__AST_STR_USED = (res + offset > (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_LEN - 1: res + offset;
  101. return res;
  102. }
  103. char *__ast_str_helper2(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc, int append, int escapecommas)
  104. {
  105. int dynamic = 0;
  106. char *ptr = append ? &((*buf)->__AST_STR_STR[(*buf)->__AST_STR_USED]) : (*buf)->__AST_STR_STR;
  107. if (maxlen < 1) {
  108. if (maxlen == 0) {
  109. dynamic = 1;
  110. }
  111. maxlen = (*buf)->__AST_STR_LEN;
  112. }
  113. while (*src && maxsrc && maxlen && (!escapecommas || (maxlen - 1))) {
  114. if (escapecommas && (*src == '\\' || *src == ',')) {
  115. *ptr++ = '\\';
  116. maxlen--;
  117. (*buf)->__AST_STR_USED++;
  118. }
  119. *ptr++ = *src++;
  120. maxsrc--;
  121. maxlen--;
  122. (*buf)->__AST_STR_USED++;
  123. if ((ptr >= (*buf)->__AST_STR_STR + (*buf)->__AST_STR_LEN - 3) ||
  124. (dynamic && (!maxlen || (escapecommas && !(maxlen - 1))))) {
  125. char *oldbase = (*buf)->__AST_STR_STR;
  126. size_t old = (*buf)->__AST_STR_LEN;
  127. if (ast_str_make_space(buf, (*buf)->__AST_STR_LEN * 2)) {
  128. /* If the buffer can't be extended, end it. */
  129. break;
  130. }
  131. /* What we extended the buffer by */
  132. maxlen = old;
  133. ptr += (*buf)->__AST_STR_STR - oldbase;
  134. }
  135. }
  136. if (__builtin_expect(!maxlen, 0)) {
  137. ptr--;
  138. }
  139. *ptr = '\0';
  140. return (*buf)->__AST_STR_STR;
  141. }