strings.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@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. * \brief String manipulation functions
  20. */
  21. #ifndef _ASTERISK_STRINGS_H
  22. #define _ASTERISK_STRINGS_H
  23. /* #define DEBUG_OPAQUE */
  24. #include <ctype.h>
  25. #include <limits.h>
  26. #include "asterisk/utils.h"
  27. #include "asterisk/threadstorage.h"
  28. #include "asterisk/astobj2.h"
  29. #if defined(DEBUG_OPAQUE)
  30. #define __AST_STR_USED used2
  31. #define __AST_STR_LEN len2
  32. #define __AST_STR_STR str2
  33. #define __AST_STR_TS ts2
  34. #else
  35. #define __AST_STR_USED used
  36. #define __AST_STR_LEN len
  37. #define __AST_STR_STR str
  38. #define __AST_STR_TS ts
  39. #endif
  40. /* You may see casts in this header that may seem useless but they ensure this file is C++ clean */
  41. #define AS_OR(a,b) (a && ast_str_strlen(a)) ? ast_str_buffer(a) : (b)
  42. #ifdef AST_DEVMODE
  43. #define ast_strlen_zero(foo) _ast_strlen_zero(foo, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  44. static force_inline int _ast_strlen_zero(const char *s, const char *file, const char *function, int line)
  45. {
  46. if (!s || (*s == '\0')) {
  47. return 1;
  48. }
  49. if (!strcmp(s, "(null)")) {
  50. ast_log(__LOG_WARNING, file, line, function, "Possible programming error: \"(null)\" is not NULL!\n");
  51. }
  52. return 0;
  53. }
  54. #else
  55. static force_inline int attribute_pure ast_strlen_zero(const char *s)
  56. {
  57. return (!s || (*s == '\0'));
  58. }
  59. #endif
  60. #ifdef SENSE_OF_HUMOR
  61. #define ast_strlen_real(a) (a) ? strlen(a) : 0
  62. #define ast_strlen_imaginary(a) ast_random()
  63. #endif
  64. /*! \brief returns the equivalent of logic or for strings:
  65. * first one if not empty, otherwise second one.
  66. */
  67. #define S_OR(a, b) ({typeof(&((a)[0])) __x = (a); ast_strlen_zero(__x) ? (b) : __x;})
  68. /*! \brief returns the equivalent of logic or for strings, with an additional boolean check:
  69. * second one if not empty and first one is true, otherwise third one.
  70. * example: S_COR(usewidget, widget, "<no widget>")
  71. */
  72. #define S_COR(a, b, c) ({typeof(&((b)[0])) __x = (b); (a) && !ast_strlen_zero(__x) ? (__x) : (c);})
  73. /*
  74. \brief Checks whether a string begins with another.
  75. \since 12.0.0
  76. \param str String to check.
  77. \param prefix Prefix to look for.
  78. \param 1 if \a str begins with \a prefix, 0 otherwise.
  79. */
  80. static int force_inline attribute_pure ast_begins_with(const char *str, const char *prefix)
  81. {
  82. ast_assert(str != NULL);
  83. ast_assert(prefix != NULL);
  84. while (*str == *prefix && *prefix != '\0') {
  85. ++str;
  86. ++prefix;
  87. }
  88. return *prefix == '\0';
  89. }
  90. /*
  91. \brief Checks whether a string ends with another.
  92. \since 12.0.0
  93. \param str String to check.
  94. \param suffix Suffix to look for.
  95. \param 1 if \a str ends with \a suffix, 0 otherwise.
  96. */
  97. static int force_inline attribute_pure ast_ends_with(const char *str, const char *suffix)
  98. {
  99. size_t str_len;
  100. size_t suffix_len;
  101. ast_assert(str != NULL);
  102. ast_assert(suffix != NULL);
  103. str_len = strlen(str);
  104. suffix_len = strlen(suffix);
  105. if (suffix_len > str_len) {
  106. return 0;
  107. }
  108. return strcmp(str + str_len - suffix_len, suffix) == 0;
  109. }
  110. /*!
  111. * \brief return Yes or No depending on the argument.
  112. *
  113. * Note that this macro is used my AMI, where a literal "Yes" and "No" are
  114. * expected, and translations would cause problems.
  115. *
  116. * \param x Boolean value
  117. * \return "Yes" if x is true (non-zero)
  118. * \return "No" if x is false (zero)
  119. */
  120. #define AST_YESNO(x) ((x) ? "Yes" : "No")
  121. /*!
  122. \brief Gets a pointer to the first non-whitespace character in a string.
  123. \param str the input string
  124. \return a pointer to the first non-whitespace character
  125. */
  126. AST_INLINE_API(
  127. char * attribute_pure ast_skip_blanks(const char *str),
  128. {
  129. if (str) {
  130. while (*str && ((unsigned char) *str) < 33) {
  131. str++;
  132. }
  133. }
  134. return (char *) str;
  135. }
  136. )
  137. /*!
  138. \brief Trims trailing whitespace characters from a string.
  139. \param str the input string
  140. \return a pointer to the modified string
  141. */
  142. AST_INLINE_API(
  143. char *ast_trim_blanks(char *str),
  144. {
  145. char *work = str;
  146. if (work) {
  147. work += strlen(work) - 1;
  148. /* It's tempting to only want to erase after we exit this loop,
  149. but since ast_trim_blanks *could* receive a constant string
  150. (which we presumably wouldn't have to touch), we shouldn't
  151. actually set anything unless we must, and it's easier just
  152. to set each position to \0 than to keep track of a variable
  153. for it */
  154. while ((work >= str) && ((unsigned char) *work) < 33)
  155. *(work--) = '\0';
  156. }
  157. return str;
  158. }
  159. )
  160. /*!
  161. \brief Gets a pointer to first whitespace character in a string.
  162. \param str the input string
  163. \return a pointer to the first whitespace character
  164. */
  165. AST_INLINE_API(
  166. char * attribute_pure ast_skip_nonblanks(const char *str),
  167. {
  168. if (str) {
  169. while (*str && ((unsigned char) *str) > 32) {
  170. str++;
  171. }
  172. }
  173. return (char *) str;
  174. }
  175. )
  176. /*!
  177. \brief Strip leading/trailing whitespace from a string.
  178. \param s The string to be stripped (will be modified).
  179. \return The stripped string.
  180. This functions strips all leading and trailing whitespace
  181. characters from the input string, and returns a pointer to
  182. the resulting string. The string is modified in place.
  183. */
  184. AST_INLINE_API(
  185. char *ast_strip(char *s),
  186. {
  187. if ((s = ast_skip_blanks(s))) {
  188. ast_trim_blanks(s);
  189. }
  190. return s;
  191. }
  192. )
  193. /*!
  194. \brief Strip leading/trailing whitespace and quotes from a string.
  195. \param s The string to be stripped (will be modified).
  196. \param beg_quotes The list of possible beginning quote characters.
  197. \param end_quotes The list of matching ending quote characters.
  198. \return The stripped string.
  199. This functions strips all leading and trailing whitespace
  200. characters from the input string, and returns a pointer to
  201. the resulting string. The string is modified in place.
  202. It can also remove beginning and ending quote (or quote-like)
  203. characters, in matching pairs. If the first character of the
  204. string matches any character in beg_quotes, and the last
  205. character of the string is the matching character in
  206. end_quotes, then they are removed from the string.
  207. Examples:
  208. \code
  209. ast_strip_quoted(buf, "\"", "\"");
  210. ast_strip_quoted(buf, "'", "'");
  211. ast_strip_quoted(buf, "[{(", "]})");
  212. \endcode
  213. */
  214. char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
  215. /*!
  216. \brief Flags for ast_strsep
  217. */
  218. enum ast_strsep_flags {
  219. AST_STRSEP_STRIP = 0x01, /*!< Trim, then strip quotes. You may want to trim again */
  220. AST_STRSEP_TRIM = 0x02, /*!< Trim leading and trailing whitespace */
  221. AST_STRSEP_UNESCAPE = 0x04, /*!< Unescape '\' */
  222. AST_STRSEP_ALL = 0x07, /*!< Trim, strip, unescape */
  223. };
  224. /*!
  225. \brief Act like strsep but ignore separators inside quotes.
  226. \param s Pointer to address of the string to be processed.
  227. Will be modified and can't be constant.
  228. \param sep A single character delimiter.
  229. \param flags Controls post-processing of the result.
  230. AST_STRSEP_TRIM trims all leading and trailing whitespace from the result.
  231. AST_STRSEP_STRIP does a trim then strips the outermost quotes. You may want
  232. to trim again after the strip. Just OR both the TRIM and STRIP flags.
  233. AST_STRSEP_UNESCAPE unescapes '\' sequences.
  234. AST_STRSEP_ALL does all of the above processing.
  235. \return The next token or NULL if done or if there are more than 8 levels of
  236. nested quotes.
  237. This function acts like strsep with three exceptions...
  238. The separator is a single character instead of a string.
  239. Separators inside quotes are treated literally instead of like separators.
  240. You can elect to have leading and trailing whitespace and quotes
  241. stripped from the result and have '\' sequences unescaped.
  242. Like strsep, ast_strsep maintains no internal state and you can call it
  243. recursively using different separators on the same storage.
  244. Also like strsep, for consistent results, consecutive separators are not
  245. collapsed so you may get an empty string as a valid result.
  246. Examples:
  247. \code
  248. char *mystr = ast_strdupa("abc=def,ghi='zzz=yyy,456',jkl");
  249. char *token, *token2, *token3;
  250. while((token = ast_strsep(&mystr, ',', AST_SEP_STRIP))) {
  251. // 1st token will be aaa=def
  252. // 2nd token will be ghi='zzz=yyy,456'
  253. while((token2 = ast_strsep(&token, '=', AST_SEP_STRIP))) {
  254. // 1st token2 will be ghi
  255. // 2nd token2 will be zzz=yyy,456
  256. while((token3 = ast_strsep(&token2, ',', AST_SEP_STRIP))) {
  257. // 1st token3 will be zzz=yyy
  258. // 2nd token3 will be 456
  259. // and so on
  260. }
  261. }
  262. // 3rd token will be jkl
  263. }
  264. \endcode
  265. */
  266. char *ast_strsep(char **s, const char sep, uint32_t flags);
  267. /*!
  268. \brief Strip backslash for "escaped" semicolons,
  269. the string to be stripped (will be modified).
  270. \return The stripped string.
  271. */
  272. char *ast_unescape_semicolon(char *s);
  273. /*!
  274. \brief Convert some C escape sequences \verbatim (\b\f\n\r\t) \endverbatim into the
  275. equivalent characters. The string to be converted (will be modified).
  276. \return The converted string.
  277. */
  278. char *ast_unescape_c(char *s);
  279. /*!
  280. * \brief Escape the 'to_escape' characters in the given string.
  281. *
  282. * \note The given output buffer will contain a truncated escaped
  283. * version of the source string if the given buffer is not large
  284. * enough.
  285. *
  286. * \param dest the escaped string
  287. * \param s the source string to escape
  288. * \param size The size of the destination buffer
  289. * \param to_escape an array of characters to escape
  290. *
  291. * \return Pointer to the destination.
  292. */
  293. char *ast_escape(char *dest, const char *s, size_t size, const char *to_escape);
  294. /*!
  295. * \brief Escape standard 'C' sequences in the given string.
  296. *
  297. * \note The given output buffer will contain a truncated escaped
  298. * version of the source string if the given buffer is not large
  299. * enough.
  300. *
  301. * \param dest the escaped string
  302. * \param s the source string to escape
  303. * \param size The size of the destination buffer
  304. *
  305. * \return Pointer to the escaped string.
  306. */
  307. char *ast_escape_c(char *dest, const char *s, size_t size);
  308. /*!
  309. * \brief Escape the 'to_escape' characters in the given string.
  310. *
  311. * \note Caller is responsible for freeing the returned string
  312. *
  313. * \param s the source string to escape
  314. * \param to_escape an array of characters to escape
  315. *
  316. * \return Pointer to the escaped string or NULL.
  317. */
  318. char *ast_escape_alloc(const char *s, const char *to_escape);
  319. /*!
  320. * \brief Escape standard 'C' sequences in the given string.
  321. *
  322. * \note Caller is responsible for freeing the returned string
  323. *
  324. * \param s the source string to escape
  325. *
  326. * \return Pointer to the escaped string or NULL.
  327. */
  328. char *ast_escape_c_alloc(const char *s);
  329. /*!
  330. \brief Size-limited null-terminating string copy.
  331. \param dst The destination buffer.
  332. \param src The source string
  333. \param size The size of the destination buffer
  334. \return Nothing.
  335. This is similar to \a strncpy, with two important differences:
  336. - the destination buffer will \b always be null-terminated
  337. - the destination buffer is not filled with zeros past the copied string length
  338. These differences make it slightly more efficient, and safer to use since it will
  339. not leave the destination buffer unterminated. There is no need to pass an artificially
  340. reduced buffer size to this function (unlike \a strncpy), and the buffer does not need
  341. to be initialized to zeroes prior to calling this function.
  342. */
  343. AST_INLINE_API(
  344. void ast_copy_string(char *dst, const char *src, size_t size),
  345. {
  346. while (*src && size) {
  347. *dst++ = *src++;
  348. size--;
  349. }
  350. if (__builtin_expect(!size, 0))
  351. dst--;
  352. *dst = '\0';
  353. }
  354. )
  355. /*!
  356. \brief Build a string in a buffer, designed to be called repeatedly
  357. \note This method is not recommended. New code should use ast_str_*() instead.
  358. This is a wrapper for snprintf, that properly handles the buffer pointer
  359. and buffer space available.
  360. \param buffer current position in buffer to place string into (will be updated on return)
  361. \param space remaining space in buffer (will be updated on return)
  362. \param fmt printf-style format string
  363. \retval 0 on success
  364. \retval non-zero on failure.
  365. */
  366. int ast_build_string(char **buffer, size_t *space, const char *fmt, ...) __attribute__((format(printf, 3, 4)));
  367. /*!
  368. \brief Build a string in a buffer, designed to be called repeatedly
  369. This is a wrapper for snprintf, that properly handles the buffer pointer
  370. and buffer space available.
  371. \return 0 on success, non-zero on failure.
  372. \param buffer current position in buffer to place string into (will be updated on return)
  373. \param space remaining space in buffer (will be updated on return)
  374. \param fmt printf-style format string
  375. \param ap varargs list of arguments for format
  376. */
  377. int ast_build_string_va(char **buffer, size_t *space, const char *fmt, va_list ap) __attribute__((format(printf, 3, 0)));
  378. /*!
  379. * \brief Make sure something is true.
  380. * Determine if a string containing a boolean value is "true".
  381. * This function checks to see whether a string passed to it is an indication of an "true" value.
  382. * It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
  383. *
  384. * \retval 0 if val is a NULL pointer.
  385. * \retval -1 if "true".
  386. * \retval 0 otherwise.
  387. */
  388. int attribute_pure ast_true(const char *val);
  389. /*!
  390. * \brief Make sure something is false.
  391. * Determine if a string containing a boolean value is "false".
  392. * This function checks to see whether a string passed to it is an indication of an "false" value.
  393. * It checks to see if the string is "no", "false", "n", "f", "off" or "0".
  394. *
  395. * \retval 0 if val is a NULL pointer.
  396. * \retval -1 if "true".
  397. * \retval 0 otherwise.
  398. */
  399. int attribute_pure ast_false(const char *val);
  400. /*
  401. * \brief Join an array of strings into a single string.
  402. * \param s the resulting string buffer
  403. * \param len the length of the result buffer, s
  404. * \param w an array of strings to join.
  405. * \param size the number of elements to join
  406. * \param delim delimiter between elements
  407. *
  408. * This function will join all of the strings in the array 'w' into a single
  409. * string. It will also place 'delim' in the result buffer in between each
  410. * string from 'w'.
  411. * \since 12
  412. */
  413. void ast_join_delim(char *s, size_t len, const char * const w[],
  414. unsigned int size, char delim);
  415. /*
  416. * \brief Join an array of strings into a single string.
  417. * \param s the resulting string buffer
  418. * \param len the length of the result buffer, s
  419. * \param w an array of strings to join.
  420. *
  421. * This function will join all of the strings in the array 'w' into a single
  422. * string. It will also place a space in the result buffer in between each
  423. * string from 'w'.
  424. */
  425. #define ast_join(s, len, w) ast_join_delim(s, len, w, -1, ' ')
  426. /*
  427. * \brief Attempts to convert the given string to camel case using
  428. * the specified delimiter.
  429. *
  430. * note - returned string needs to be freed
  431. *
  432. * \param s the string to convert
  433. * \param delim delimiter to parse out
  434. *
  435. * \retval The string converted to "CamelCase"
  436. * \since 12
  437. */
  438. char *ast_to_camel_case_delim(const char *s, const char *delim);
  439. /*
  440. * \brief Attempts to convert the given string to camel case using
  441. * an underscore as the specified delimiter.
  442. *
  443. * note - returned string needs to be freed
  444. *
  445. * \param s the string to convert
  446. *
  447. * \retval The string converted to "CamelCase"
  448. */
  449. #define ast_to_camel_case(s) ast_to_camel_case_delim(s, "_")
  450. /*
  451. \brief Parse a time (integer) string.
  452. \param src String to parse
  453. \param dst Destination
  454. \param _default Value to use if the string does not contain a valid time
  455. \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
  456. \retval 0 on success
  457. \retval non-zero on failure.
  458. */
  459. int ast_get_time_t(const char *src, time_t *dst, time_t _default, int *consumed);
  460. /*
  461. \brief Parse a time (float) string.
  462. \param src String to parse
  463. \param dst Destination
  464. \param _default Value to use if the string does not contain a valid time
  465. \param consumed The number of characters 'consumed' in the string by the parse (see 'man sscanf' for details)
  466. \return zero on success, non-zero on failure
  467. */
  468. int ast_get_timeval(const char *src, struct timeval *tv, struct timeval _default, int *consumed);
  469. /*!
  470. * Support for dynamic strings.
  471. *
  472. * A dynamic string is just a C string prefixed by a few control fields
  473. * that help setting/appending/extending it using a printf-like syntax.
  474. *
  475. * One should never declare a variable with this type, but only a pointer
  476. * to it, e.g.
  477. *
  478. * struct ast_str *ds;
  479. *
  480. * The pointer can be initialized with the following:
  481. *
  482. * ds = ast_str_create(init_len);
  483. * creates a malloc()'ed dynamic string;
  484. *
  485. * ds = ast_str_alloca(init_len);
  486. * creates a string on the stack (not very dynamic!).
  487. *
  488. * ds = ast_str_thread_get(ts, init_len)
  489. * creates a malloc()'ed dynamic string associated to
  490. * the thread-local storage key ts
  491. *
  492. * Finally, the string can be manipulated with the following:
  493. *
  494. * ast_str_set(&buf, max_len, fmt, ...)
  495. * ast_str_append(&buf, max_len, fmt, ...)
  496. *
  497. * and their varargs variant
  498. *
  499. * ast_str_set_va(&buf, max_len, ap)
  500. * ast_str_append_va(&buf, max_len, ap)
  501. *
  502. * \param max_len The maximum allowed capacity of the ast_str. Note that
  503. * if the value of max_len is less than the current capacity of the
  504. * ast_str (as returned by ast_str_size), then the parameter is effectively
  505. * ignored.
  506. * 0 means unlimited, -1 means "at most the available space"
  507. *
  508. * \return All the functions return <0 in case of error, or the
  509. * length of the string added to the buffer otherwise. Note that
  510. * in most cases where an error is returned, characters ARE written
  511. * to the ast_str.
  512. */
  513. /*! \brief The descriptor of a dynamic string
  514. * XXX storage will be optimized later if needed
  515. * We use the ts field to indicate the type of storage.
  516. * Three special constants indicate malloc, ast_alloca() or static
  517. * variables, all other values indicate a
  518. * struct ast_threadstorage pointer.
  519. */
  520. struct ast_str {
  521. size_t __AST_STR_LEN; /*!< The current maximum length of the string */
  522. size_t __AST_STR_USED; /*!< Amount of space used */
  523. struct ast_threadstorage *__AST_STR_TS; /*!< What kind of storage is this ? */
  524. #define DS_MALLOC ((struct ast_threadstorage *)1)
  525. #define DS_ALLOCA ((struct ast_threadstorage *)2)
  526. #define DS_STATIC ((struct ast_threadstorage *)3) /* not supported yet */
  527. char __AST_STR_STR[0]; /*!< The string buffer */
  528. };
  529. /*!
  530. * \brief Given a string regex_string in the form of "/regex/", convert it into the form of "regex"
  531. *
  532. * This function will trim one leading / and one trailing / from a given input string
  533. * ast_str regex_pattern must be preallocated before calling this function
  534. *
  535. * \return 0 on success, non-zero on failure.
  536. * \return 1 if we only stripped a leading /
  537. * \return 2 if we only stripped a trailing /
  538. * \return 3 if we did not strip any / characters
  539. * \param regex_string the string containing /regex/
  540. * \param regex_pattern the destination ast_str which will contain "regex" after execution
  541. */
  542. int ast_regex_string_to_regex_pattern(const char *regex_string, struct ast_str **regex_pattern);
  543. /*!
  544. * \brief Create a malloc'ed dynamic length string
  545. *
  546. * \param init_len This is the initial length of the string buffer
  547. *
  548. * \return This function returns a pointer to the dynamic string length. The
  549. * result will be NULL in the case of a memory allocation error.
  550. *
  551. * \note The result of this function is dynamically allocated memory, and must
  552. * be free()'d after it is no longer needed.
  553. */
  554. #ifdef __AST_DEBUG_MALLOC
  555. #define ast_str_create(a) _ast_str_create(a,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  556. AST_INLINE_API(
  557. struct ast_str * attribute_malloc _ast_str_create(size_t init_len,
  558. const char *file, int lineno, const char *func),
  559. {
  560. struct ast_str *buf;
  561. buf = (struct ast_str *)__ast_calloc(1, sizeof(*buf) + init_len, file, lineno, func);
  562. if (buf == NULL)
  563. return NULL;
  564. buf->__AST_STR_LEN = init_len;
  565. buf->__AST_STR_USED = 0;
  566. buf->__AST_STR_TS = DS_MALLOC;
  567. return buf;
  568. }
  569. )
  570. #else
  571. AST_INLINE_API(
  572. struct ast_str * attribute_malloc ast_str_create(size_t init_len),
  573. {
  574. struct ast_str *buf;
  575. buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len);
  576. if (buf == NULL)
  577. return NULL;
  578. buf->__AST_STR_LEN = init_len;
  579. buf->__AST_STR_USED = 0;
  580. buf->__AST_STR_TS = DS_MALLOC;
  581. return buf;
  582. }
  583. )
  584. #endif
  585. /*! \brief Reset the content of a dynamic string.
  586. * Useful before a series of ast_str_append.
  587. */
  588. AST_INLINE_API(
  589. void ast_str_reset(struct ast_str *buf),
  590. {
  591. if (buf) {
  592. buf->__AST_STR_USED = 0;
  593. if (buf->__AST_STR_LEN) {
  594. buf->__AST_STR_STR[0] = '\0';
  595. }
  596. }
  597. }
  598. )
  599. /*! \brief Update the length of the buffer, after using ast_str merely as a buffer.
  600. * \param buf A pointer to the ast_str string.
  601. */
  602. AST_INLINE_API(
  603. void ast_str_update(struct ast_str *buf),
  604. {
  605. buf->__AST_STR_USED = strlen(buf->__AST_STR_STR);
  606. }
  607. )
  608. /*! \brief Trims trailing whitespace characters from an ast_str string.
  609. * \param buf A pointer to the ast_str string.
  610. */
  611. AST_INLINE_API(
  612. void ast_str_trim_blanks(struct ast_str *buf),
  613. {
  614. if (!buf) {
  615. return;
  616. }
  617. while (buf->__AST_STR_USED && ((unsigned char) buf->__AST_STR_STR[buf->__AST_STR_USED - 1]) < 33) {
  618. buf->__AST_STR_STR[--(buf->__AST_STR_USED)] = '\0';
  619. }
  620. }
  621. )
  622. /*!\brief Returns the current length of the string stored within buf.
  623. * \param buf A pointer to the ast_str structure.
  624. */
  625. AST_INLINE_API(
  626. size_t attribute_pure ast_str_strlen(const struct ast_str *buf),
  627. {
  628. return buf->__AST_STR_USED;
  629. }
  630. )
  631. /*!\brief Returns the current maximum length (without reallocation) of the current buffer.
  632. * \param buf A pointer to the ast_str structure.
  633. * \retval Current maximum length of the buffer.
  634. */
  635. AST_INLINE_API(
  636. size_t attribute_pure ast_str_size(const struct ast_str *buf),
  637. {
  638. return buf->__AST_STR_LEN;
  639. }
  640. )
  641. /*!\brief Returns the string buffer within the ast_str buf.
  642. * \param buf A pointer to the ast_str structure.
  643. * \retval A pointer to the enclosed string.
  644. */
  645. AST_INLINE_API(
  646. char * attribute_pure ast_str_buffer(const struct ast_str *buf),
  647. {
  648. /* for now, cast away the const qualifier on the pointer
  649. * being returned; eventually, it should become truly const
  650. * and only be modified via accessor functions
  651. */
  652. return (char *) buf->__AST_STR_STR;
  653. }
  654. )
  655. /*!\brief Truncates the enclosed string to the given length.
  656. * \param buf A pointer to the ast_str structure.
  657. * \param len Maximum length of the string. If len is larger than the
  658. * current maximum length, things will explode. If it is negative
  659. * at most -len characters will be trimmed off the end.
  660. * \retval A pointer to the resulting string.
  661. */
  662. AST_INLINE_API(
  663. char *ast_str_truncate(struct ast_str *buf, ssize_t len),
  664. {
  665. if (len < 0) {
  666. if ((typeof(buf->__AST_STR_USED)) -len >= buf->__AST_STR_USED) {
  667. buf->__AST_STR_USED = 0;
  668. } else {
  669. buf->__AST_STR_USED += len;
  670. }
  671. } else {
  672. buf->__AST_STR_USED = len;
  673. }
  674. buf->__AST_STR_STR[buf->__AST_STR_USED] = '\0';
  675. return buf->__AST_STR_STR;
  676. }
  677. )
  678. /*
  679. * AST_INLINE_API() is a macro that takes a block of code as an argument.
  680. * Using preprocessor #directives in the argument is not supported by all
  681. * compilers, and it is a bit of an obfuscation anyways, so avoid it.
  682. * As a workaround, define a macro that produces either its argument
  683. * or nothing, and use that instead of #ifdef/#endif within the
  684. * argument to AST_INLINE_API().
  685. */
  686. #if defined(DEBUG_THREADLOCALS)
  687. #define _DB1(x) x
  688. #else
  689. #define _DB1(x)
  690. #endif
  691. /*!
  692. * Make space in a new string (e.g. to read in data from a file)
  693. */
  694. #ifdef __AST_DEBUG_MALLOC
  695. AST_INLINE_API(
  696. int _ast_str_make_space(struct ast_str **buf, size_t new_len, const char *file, int lineno, const char *function),
  697. {
  698. struct ast_str *old_buf = *buf;
  699. if (new_len <= (*buf)->__AST_STR_LEN)
  700. return 0; /* success */
  701. if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
  702. return -1; /* cannot extend */
  703. *buf = (struct ast_str *)__ast_realloc(*buf, new_len + sizeof(struct ast_str), file, lineno, function);
  704. if (*buf == NULL) {
  705. *buf = old_buf;
  706. return -1;
  707. }
  708. if ((*buf)->__AST_STR_TS != DS_MALLOC) {
  709. pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
  710. _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
  711. }
  712. (*buf)->__AST_STR_LEN = new_len;
  713. return 0;
  714. }
  715. )
  716. #define ast_str_make_space(a,b) _ast_str_make_space(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  717. #else
  718. AST_INLINE_API(
  719. int ast_str_make_space(struct ast_str **buf, size_t new_len),
  720. {
  721. struct ast_str *old_buf = *buf;
  722. if (new_len <= (*buf)->__AST_STR_LEN)
  723. return 0; /* success */
  724. if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC)
  725. return -1; /* cannot extend */
  726. *buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str));
  727. if (*buf == NULL) {
  728. *buf = old_buf;
  729. return -1;
  730. }
  731. if ((*buf)->__AST_STR_TS != DS_MALLOC) {
  732. pthread_setspecific((*buf)->__AST_STR_TS->key, *buf);
  733. _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));)
  734. }
  735. (*buf)->__AST_STR_LEN = new_len;
  736. return 0;
  737. }
  738. )
  739. #endif
  740. AST_INLINE_API(
  741. int ast_str_copy_string(struct ast_str **dst, struct ast_str *src),
  742. {
  743. /* make sure our destination is large enough */
  744. if (src->__AST_STR_USED + 1 > (*dst)->__AST_STR_LEN) {
  745. if (ast_str_make_space(dst, src->__AST_STR_USED + 1)) {
  746. return -1;
  747. }
  748. }
  749. memcpy((*dst)->__AST_STR_STR, src->__AST_STR_STR, src->__AST_STR_USED + 1);
  750. (*dst)->__AST_STR_USED = src->__AST_STR_USED;
  751. return 0;
  752. }
  753. )
  754. #define ast_str_alloca(init_len) \
  755. ({ \
  756. struct ast_str *__ast_str_buf; \
  757. __ast_str_buf = ast_alloca(sizeof(*__ast_str_buf) + init_len); \
  758. __ast_str_buf->__AST_STR_LEN = init_len; \
  759. __ast_str_buf->__AST_STR_USED = 0; \
  760. __ast_str_buf->__AST_STR_TS = DS_ALLOCA; \
  761. __ast_str_buf->__AST_STR_STR[0] = '\0'; \
  762. (__ast_str_buf); \
  763. })
  764. /*!
  765. * \brief Retrieve a thread locally stored dynamic string
  766. *
  767. * \param ts This is a pointer to the thread storage structure declared by using
  768. * the AST_THREADSTORAGE macro. If declared with
  769. * AST_THREADSTORAGE(my_buf, my_buf_init), then this argument would be
  770. * (&my_buf).
  771. * \param init_len This is the initial length of the thread's dynamic string. The
  772. * current length may be bigger if previous operations in this thread have
  773. * caused it to increase.
  774. *
  775. * \return This function will return the thread locally stored dynamic string
  776. * associated with the thread storage management variable passed as the
  777. * first argument.
  778. * The result will be NULL in the case of a memory allocation error.
  779. *
  780. * Example usage:
  781. * \code
  782. * AST_THREADSTORAGE(my_str, my_str_init);
  783. * #define MY_STR_INIT_SIZE 128
  784. * ...
  785. * void my_func(const char *fmt, ...)
  786. * {
  787. * struct ast_str *buf;
  788. *
  789. * if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
  790. * return;
  791. * ...
  792. * }
  793. * \endcode
  794. */
  795. #if !defined(DEBUG_THREADLOCALS)
  796. AST_INLINE_API(
  797. struct ast_str *ast_str_thread_get(struct ast_threadstorage *ts,
  798. size_t init_len),
  799. {
  800. struct ast_str *buf;
  801. buf = (struct ast_str *)ast_threadstorage_get(ts, sizeof(*buf) + init_len);
  802. if (buf == NULL)
  803. return NULL;
  804. if (!buf->__AST_STR_LEN) {
  805. buf->__AST_STR_LEN = init_len;
  806. buf->__AST_STR_USED = 0;
  807. buf->__AST_STR_TS = ts;
  808. }
  809. return buf;
  810. }
  811. )
  812. #else /* defined(DEBUG_THREADLOCALS) */
  813. AST_INLINE_API(
  814. struct ast_str *__ast_str_thread_get(struct ast_threadstorage *ts,
  815. size_t init_len, const char *file, const char *function, unsigned int line),
  816. {
  817. struct ast_str *buf;
  818. buf = (struct ast_str *)__ast_threadstorage_get(ts, sizeof(*buf) + init_len, file, function, line);
  819. if (buf == NULL)
  820. return NULL;
  821. if (!buf->__AST_STR_LEN) {
  822. buf->__AST_STR_LEN = init_len;
  823. buf->__AST_STR_USED = 0;
  824. buf->__AST_STR_TS = ts;
  825. }
  826. return buf;
  827. }
  828. )
  829. #define ast_str_thread_get(ts, init_len) __ast_str_thread_get(ts, init_len, __FILE__, __PRETTY_FUNCTION__, __LINE__)
  830. #endif /* defined(DEBUG_THREADLOCALS) */
  831. /*!
  832. * \brief Error codes from __ast_str_helper()
  833. * The undelying processing to manipulate dynamic string is done
  834. * by __ast_str_helper(), which can return a success or a
  835. * permanent failure (e.g. no memory).
  836. */
  837. enum {
  838. /*! An error has occurred and the contents of the dynamic string
  839. * are undefined */
  840. AST_DYNSTR_BUILD_FAILED = -1,
  841. /*! The buffer size for the dynamic string had to be increased, and
  842. * __ast_str_helper() needs to be called again after
  843. * a va_end() and va_start(). This return value is legacy and will
  844. * no longer be used.
  845. */
  846. AST_DYNSTR_BUILD_RETRY = -2
  847. };
  848. /*!
  849. * \brief Core functionality of ast_str_(set|append)_va
  850. *
  851. * The arguments to this function are the same as those described for
  852. * ast_str_set_va except for an addition argument, append.
  853. * If append is non-zero, this will append to the current string instead of
  854. * writing over it.
  855. *
  856. * AST_DYNSTR_BUILD_RETRY is a legacy define. It should probably never
  857. * again be used.
  858. *
  859. * A return of AST_DYNSTR_BUILD_FAILED indicates a memory allocation error.
  860. *
  861. * A return value greater than or equal to zero indicates the number of
  862. * characters that have been written, not including the terminating '\0'.
  863. * In the append case, this only includes the number of characters appended.
  864. *
  865. * \note This function should never need to be called directly. It should
  866. * through calling one of the other functions or macros defined in this
  867. * file.
  868. */
  869. #ifdef __AST_DEBUG_MALLOC
  870. int __attribute__((format(printf, 4, 0))) __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len,
  871. int append, const char *fmt, va_list ap, const char *file, int lineno, const char *func);
  872. #define __ast_str_helper(a,b,c,d,e) __ast_debug_str_helper(a,b,c,d,e,__FILE__,__LINE__,__PRETTY_FUNCTION__)
  873. #else
  874. int __attribute__((format(printf, 4, 0))) __ast_str_helper(struct ast_str **buf, ssize_t max_len,
  875. int append, const char *fmt, va_list ap);
  876. #endif
  877. char *__ast_str_helper2(struct ast_str **buf, ssize_t max_len,
  878. const char *src, size_t maxsrc, int append, int escapecommas);
  879. /*!
  880. * \brief Set a dynamic string from a va_list
  881. *
  882. * \param buf This is the address of a pointer to a struct ast_str.
  883. * If it is retrieved using ast_str_thread_get, the
  884. struct ast_threadstorage pointer will need to
  885. * be updated in the case that the buffer has to be reallocated to
  886. * accommodate a longer string than what it currently has space for.
  887. * \param max_len This is the maximum length to allow the string buffer to grow
  888. * to. If this is set to 0, then there is no maximum length.
  889. * \param fmt This is the format string (printf style)
  890. * \param ap This is the va_list
  891. *
  892. * \return The return value of this function is the same as that of the printf
  893. * family of functions.
  894. *
  895. * Example usage (the first part is only for thread-local storage)
  896. * \code
  897. * AST_THREADSTORAGE(my_str, my_str_init);
  898. * #define MY_STR_INIT_SIZE 128
  899. * ...
  900. * void my_func(const char *fmt, ...)
  901. * {
  902. * struct ast_str *buf;
  903. * va_list ap;
  904. *
  905. * if (!(buf = ast_str_thread_get(&my_str, MY_STR_INIT_SIZE)))
  906. * return;
  907. * ...
  908. * va_start(fmt, ap);
  909. * ast_str_set_va(&buf, 0, fmt, ap);
  910. * va_end(ap);
  911. *
  912. * printf("This is the string we just built: %s\n", buf->str);
  913. * ...
  914. * }
  915. * \endcode
  916. *
  917. * \note Care should be taken when using this function. The function can
  918. * result in reallocating the ast_str. If a pointer to the ast_str is passed
  919. * by value to a function that calls ast_str_set_va(), then the original ast_str
  920. * pointer may be invalidated due to a reallocation.
  921. *
  922. */
  923. 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),
  924. {
  925. return __ast_str_helper(buf, max_len, 0, fmt, ap);
  926. }
  927. )
  928. /*!
  929. * \brief Append to a dynamic string using a va_list
  930. *
  931. * Same as ast_str_set_va(), but append to the current content.
  932. *
  933. * \note Care should be taken when using this function. The function can
  934. * result in reallocating the ast_str. If a pointer to the ast_str is passed
  935. * by value to a function that calls ast_str_append_va(), then the original ast_str
  936. * pointer may be invalidated due to a reallocation.
  937. *
  938. * \param buf, max_len, fmt, ap
  939. */
  940. 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),
  941. {
  942. return __ast_str_helper(buf, max_len, 1, fmt, ap);
  943. }
  944. )
  945. /*!\brief Set a dynamic string to a non-NULL terminated substring. */
  946. AST_INLINE_API(char *ast_str_set_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
  947. {
  948. return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 0);
  949. }
  950. )
  951. /*!\brief Append a non-NULL terminated substring to the end of a dynamic string. */
  952. AST_INLINE_API(char *ast_str_append_substr(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
  953. {
  954. return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 0);
  955. }
  956. )
  957. /*!\brief Set a dynamic string to a non-NULL terminated substring, with escaping of commas. */
  958. AST_INLINE_API(char *ast_str_set_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
  959. {
  960. return __ast_str_helper2(buf, maxlen, src, maxsrc, 0, 1);
  961. }
  962. )
  963. /*!\brief Append a non-NULL terminated substring to the end of a dynamic string, with escaping of commas. */
  964. AST_INLINE_API(char *ast_str_append_escapecommas(struct ast_str **buf, ssize_t maxlen, const char *src, size_t maxsrc),
  965. {
  966. return __ast_str_helper2(buf, maxlen, src, maxsrc, 1, 1);
  967. }
  968. )
  969. /*!
  970. * \brief Set a dynamic string using variable arguments
  971. *
  972. * \note Care should be taken when using this function. The function can
  973. * result in reallocating the ast_str. If a pointer to the ast_str is passed
  974. * by value to a function that calls ast_str_set(), then the original ast_str
  975. * pointer may be invalidated due to a reallocation.
  976. *
  977. * \param buf This is the address of a pointer to a struct ast_str which should
  978. * have been retrieved using ast_str_thread_get. It will need to
  979. * be updated in the case that the buffer has to be reallocated to
  980. * accomodate a longer string than what it currently has space for.
  981. * \param max_len This is the maximum length to allow the string buffer to grow
  982. * to. If this is set to 0, then there is no maximum length.
  983. * If set to -1, we are bound to the current maximum length.
  984. * \param fmt This is the format string (printf style)
  985. *
  986. * \return The return value of this function is the same as that of the printf
  987. * family of functions.
  988. *
  989. * All the rest is the same as ast_str_set_va()
  990. */
  991. AST_INLINE_API(
  992. int __attribute__((format(printf, 3, 4))) ast_str_set(
  993. struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
  994. {
  995. int res;
  996. va_list ap;
  997. va_start(ap, fmt);
  998. res = ast_str_set_va(buf, max_len, fmt, ap);
  999. va_end(ap);
  1000. return res;
  1001. }
  1002. )
  1003. /*!
  1004. * \brief Append to a thread local dynamic string
  1005. *
  1006. * \note Care should be taken when using this function. The function can
  1007. * result in reallocating the ast_str. If a pointer to the ast_str is passed
  1008. * by value to a function that calls ast_str_append(), then the original ast_str
  1009. * pointer may be invalidated due to a reallocation.
  1010. *
  1011. * The arguments, return values, and usage of this function are the same as
  1012. * ast_str_set(), but the new data is appended to the current value.
  1013. */
  1014. AST_INLINE_API(
  1015. int __attribute__((format(printf, 3, 4))) ast_str_append(
  1016. struct ast_str **buf, ssize_t max_len, const char *fmt, ...),
  1017. {
  1018. int res;
  1019. va_list ap;
  1020. va_start(ap, fmt);
  1021. res = ast_str_append_va(buf, max_len, fmt, ap);
  1022. va_end(ap);
  1023. return res;
  1024. }
  1025. )
  1026. /*!
  1027. * \brief Check if a string is only digits
  1028. *
  1029. * \retval 1 The string contains only digits
  1030. * \retval 0 The string contains non-digit characters
  1031. */
  1032. AST_INLINE_API(
  1033. int ast_check_digits(const char *arg),
  1034. {
  1035. while (*arg) {
  1036. if (*arg < '0' || *arg > '9') {
  1037. return 0;
  1038. }
  1039. arg++;
  1040. }
  1041. return 1;
  1042. }
  1043. )
  1044. /*!
  1045. * \brief Convert the tech portion of a device string to upper case
  1046. *
  1047. * \retval dev_str Returns the char* passed in for convenience
  1048. */
  1049. AST_INLINE_API(
  1050. char *ast_tech_to_upper(char *dev_str),
  1051. {
  1052. char *pos;
  1053. if (!dev_str || !strchr(dev_str, '/')) {
  1054. return dev_str;
  1055. }
  1056. for (pos = dev_str; *pos && *pos != '/'; pos++) {
  1057. *pos = toupper(*pos);
  1058. }
  1059. return dev_str;
  1060. }
  1061. )
  1062. /*!
  1063. * \brief Restrict hash value range
  1064. *
  1065. * \details
  1066. * Hash values used all over asterisk are expected to be non-negative
  1067. * (signed) int values. This function restricts an unsigned int hash
  1068. * value to the positive half of the (signed) int values.
  1069. */
  1070. static force_inline int attribute_pure ast_str_hash_restrict(unsigned int hash)
  1071. {
  1072. return (int) (hash & (unsigned int) INT_MAX);
  1073. }
  1074. /*!
  1075. * \brief Compute a hash value on a string
  1076. *
  1077. * This famous hash algorithm was written by Dan Bernstein and is
  1078. * commonly used.
  1079. *
  1080. * http://www.cse.yorku.ca/~oz/hash.html
  1081. */
  1082. static force_inline int attribute_pure ast_str_hash(const char *str)
  1083. {
  1084. unsigned int hash = 5381;
  1085. while (*str) {
  1086. hash = hash * 33 ^ (unsigned char) *str++;
  1087. }
  1088. return ast_str_hash_restrict(hash);
  1089. }
  1090. /*!
  1091. * \brief Compute a hash value on a string
  1092. *
  1093. * \param[in] str The string to add to the hash
  1094. * \param[in] seed The hash value to start with
  1095. *
  1096. * \details
  1097. * This version of the function is for when you need to compute a
  1098. * string hash of more than one string.
  1099. *
  1100. * This famous hash algorithm was written by Dan Bernstein and is
  1101. * commonly used.
  1102. *
  1103. * \sa http://www.cse.yorku.ca/~oz/hash.html
  1104. */
  1105. static force_inline int ast_str_hash_add(const char *str, int seed)
  1106. {
  1107. unsigned int hash = (unsigned int) seed;
  1108. while (*str) {
  1109. hash = hash * 33 ^ (unsigned char) *str++;
  1110. }
  1111. return ast_str_hash_restrict(hash);
  1112. }
  1113. /*!
  1114. * \brief Compute a hash value on a case-insensitive string
  1115. *
  1116. * Uses the same hash algorithm as ast_str_hash, but converts
  1117. * all characters to lowercase prior to computing a hash. This
  1118. * allows for easy case-insensitive lookups in a hash table.
  1119. */
  1120. static force_inline int attribute_pure ast_str_case_hash(const char *str)
  1121. {
  1122. unsigned int hash = 5381;
  1123. while (*str) {
  1124. hash = hash * 33 ^ (unsigned char) tolower(*str++);
  1125. }
  1126. return ast_str_hash_restrict(hash);
  1127. }
  1128. /*!
  1129. * \brief Convert a string to all lower-case
  1130. *
  1131. * \param str The string to be converted to lower case
  1132. *
  1133. * \retval str for convenience
  1134. */
  1135. static force_inline char *attribute_pure ast_str_to_lower(char *str)
  1136. {
  1137. char *str_orig = str;
  1138. if (!str) {
  1139. return str;
  1140. }
  1141. for (; *str; ++str) {
  1142. *str = tolower(*str);
  1143. }
  1144. return str_orig;
  1145. }
  1146. /*!
  1147. * \brief Convert a string to all upper-case
  1148. *
  1149. * \param str The string to be converted to upper case
  1150. *
  1151. * \retval str for convenience
  1152. */
  1153. static force_inline char *attribute_pure ast_str_to_upper(char *str)
  1154. {
  1155. char *str_orig = str;
  1156. if (!str) {
  1157. return str;
  1158. }
  1159. for (; *str; ++str) {
  1160. *str = toupper(*str);
  1161. }
  1162. return str_orig;
  1163. }
  1164. /*!
  1165. * \since 12
  1166. * \brief Allocates a hash container for bare strings
  1167. *
  1168. * \param buckets The number of buckets to use for the hash container
  1169. *
  1170. * \retval AO2 container for strings
  1171. * \retval NULL if allocation failed
  1172. */
  1173. #define ast_str_container_alloc(buckets) ast_str_container_alloc_options(AO2_ALLOC_OPT_LOCK_MUTEX, buckets)
  1174. /*!
  1175. * \since 12
  1176. * \brief Allocates a hash container for bare strings
  1177. *
  1178. * \param opts Options to be provided to the container
  1179. * \param buckets The number of buckets to use for the hash container
  1180. *
  1181. * \retval AO2 container for strings
  1182. * \retval NULL if allocation failed
  1183. */
  1184. //struct ao2_container *ast_str_container_alloc_options(enum ao2_container_opts opts, int buckets);
  1185. struct ao2_container *ast_str_container_alloc_options(enum ao2_alloc_opts opts, int buckets);
  1186. /*!
  1187. * \since 12
  1188. * \brief Adds a string to a string container allocated by ast_str_container_alloc
  1189. *
  1190. * \param str_container The container to which to add a string
  1191. * \param add The string to add to the container
  1192. *
  1193. * \retval zero on success
  1194. * \retval non-zero if the operation failed
  1195. */
  1196. int ast_str_container_add(struct ao2_container *str_container, const char *add);
  1197. /*!
  1198. * \since 12
  1199. * \brief Removes a string from a string container allocated by ast_str_container_alloc
  1200. *
  1201. * \param str_container The container from which to remove a string
  1202. * \param remove The string to remove from the container
  1203. */
  1204. void ast_str_container_remove(struct ao2_container *str_container, const char *remove);
  1205. /*!
  1206. * \brief Create a pseudo-random string of a fixed length.
  1207. *
  1208. * This function is useful for generating a string whose randomness
  1209. * does not need to be across all time and space, does not need to
  1210. * be cryptographically secure, and needs to fit in a limited space.
  1211. *
  1212. * This function will write a null byte at the final position
  1213. * in the buffer (buf[size - 1]). So if you pass in a size of
  1214. * 10, then this will generate a random 9-character string.
  1215. *
  1216. * \param buf Buffer to write random string into.
  1217. * \param size The size of the buffer.
  1218. * \return A pointer to buf
  1219. */
  1220. char *ast_generate_random_string(char *buf, size_t size);
  1221. /*!
  1222. * \brief Compares 2 strings using realtime-style operators
  1223. * \since 13.9.0
  1224. *
  1225. * \param left The left side of the equation
  1226. * \param op The operator to apply
  1227. * \param right The right side of the equation
  1228. *
  1229. * \retval 1 matches
  1230. * \retval 0 doesn't match
  1231. *
  1232. * \details
  1233. *
  1234. * Operators:
  1235. * "=", "!=", "<", "<=", ">", ">=":
  1236. * If both left and right can be converted to float, then they will be
  1237. * compared as such. Otherwise the result will be derived from strcmp(left, right).
  1238. * "regex":
  1239. * The right value will be compiled as a regular expression and matched against the left
  1240. * value.
  1241. * "like":
  1242. * Any '%' character in the right value will be converted to '.*' and the resulting
  1243. * string will be handled as a regex.
  1244. * NULL , "":
  1245. * If the right value starts and ends with a '/' then it will be processed as a regex.
  1246. * Otherwise, same as "=".
  1247. */
  1248. int ast_strings_match(const char *left, const char *op, const char *right);
  1249. /*!
  1250. * \brief Read lines from a string buffer
  1251. * \since 13.18.0
  1252. *
  1253. * \param buffer [IN/OUT] A pointer to a char * string with either Unix or Windows line endings
  1254. *
  1255. * \return The "next" line
  1256. *
  1257. * \warning The original string and *buffer will be modified.
  1258. *
  1259. * \details
  1260. * Both '\n' and '\r\n' are treated as single delimiters but consecutive occurrances of
  1261. * the delimiters are NOT considered to be a single delimiter. This preserves blank
  1262. * lines in the input.
  1263. *
  1264. * MacOS line endings ('\r') are not supported at this time.
  1265. *
  1266. */
  1267. char *ast_read_line_from_buffer(char **buffer);
  1268. #endif /* _ASTERISK_STRINGS_H */