FreeSWITCH API Documentation  1.7.0
switch_utils.h
Go to the documentation of this file.
1 /*
2  * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3  * Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
4  *
5  * Version: MPL 1.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18  *
19  * The Initial Developer of the Original Code is
20  * Anthony Minessale II <anthm@freeswitch.org>
21  * Portions created by the Initial Developer are Copyright (C)
22  * the Initial Developer. All Rights Reserved.
23  *
24  * Contributor(s):
25  *
26  * Anthony Minessale II <anthm@freeswitch.org>
27  * Seven Du <dujinfang@gmail.com>
28  *
29  *
30  * switch_utils.h -- Compatability and Helper Code
31  *
32  */
33 /*! \file switch_utils.h
34  \brief Compatability and Helper Code
35 
36  Just a miscelaneaous set of general utility/helper functions.
37 
38 */
39 #ifndef SWITCH_UTILS_H
40 #define SWITCH_UTILS_H
41 
42 #include <switch.h>
43 #include <math.h>
44 
46 
47 #define SWITCH_URL_UNSAFE "\r\n #%&+:;<=>?@[\\]^`{|}\""
48 
49 
50 static inline uint32_t switch_round_to_step(uint32_t num, uint32_t step)
51 {
52  uint32_t r;
53  uint32_t x;
54 
55  if (!num) return 0;
56 
57  r = (num % step);
58  x = num - r;
59 
60  if (r > step / 2) {
61  x += step;
62  }
63 
64  return x;
65 }
66 
67 /* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
68  http://www.azillionmonkeys.com/qed/asmexample.html
69 */
70 static inline uint32_t switch_toupper(uint32_t eax)
71 {
72 uint32_t ebx = (0x7f7f7f7ful & eax) + 0x05050505ul;
73 ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
74  ebx = ((ebx & ~eax) >> 2 ) & 0x20202020ul;
75  return eax - ebx;
76 }
77 
78 /* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
79  http://www.azillionmonkeys.com/qed/asmexample.html
80 */
81 static inline uint32_t switch_tolower(uint32_t eax)
82 {
83  uint32_t ebx = (0x7f7f7f7ful & eax) + 0x25252525ul;
84  ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
85  ebx = ((ebx & ~eax) >> 2) & 0x20202020ul;
86  return eax + ebx;
87 }
88 
89 
90 #ifdef FS_64BIT
91 
92 /* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
93  http://www.azillionmonkeys.com/qed/asmexample.html
94 */
95 static inline uint64_t switch_toupper64(uint64_t eax)
96 {
97 uint64_t ebx = (0x7f7f7f7f7f7f7f7full & eax) + 0x0505050505050505ull;
98  ebx = (0x7f7f7f7f7f7f7f7full & ebx) + 0x1a1a1a1a1a1a1a1aull;
99  ebx = ((ebx & ~eax) >> 2 ) & 0x2020202020202020ull;
100  return eax - ebx;
101 }
102 
103 /* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
104  http://www.azillionmonkeys.com/qed/asmexample.html
105 */
106 static inline uint64_t switch_tolower64(uint64_t eax)
107 {
108  uint64_t ebx = (0x7f7f7f7f7f7f7f7full & eax) + 0x2525252525252525ull;
109  ebx = (0x7f7f7f7f7f7f7f7full & ebx) + 0x1a1a1a1a1a1a1a1aull;
110  ebx = ((ebx & ~eax) >> 2) & 0x2020202020202020ull;
111  return eax + ebx;
112 }
113 
114 static inline void switch_toupper_max(char *s)
115 {
116  uint64_t *b,*p;
117  char *c;
118  size_t l;
119 
120  l = strlen(s);
121 
122  p = (uint64_t *) s;
123 
124  while (l > 8) {
125  b = p;
126  *b = (uint64_t) switch_toupper64(*b);
127  b++;
128  p++;
129  l -= 8;
130  }
131 
132  c = (char *)p;
133 
134  while(l > 0) {
135  *c = (char) switch_toupper(*c);
136  c++;
137  l--;
138  }
139 
140 }
141 
142 static inline void switch_tolower_max(char *s)
143 {
144  uint64_t *b,*p;
145  char *c;
146  size_t l;
147 
148  l = strlen(s);
149 
150  p = (uint64_t *) s;
151 
152  while (l > 8) {
153  b = p;
154  *b = (uint64_t) switch_tolower64(*b);
155  b++;
156  p++;
157  l -= 8;
158  }
159 
160  c = (char *)p;
161 
162  while(l > 0) {
163  *c = (char) switch_tolower(*c);
164  c++;
165  l--;
166  }
167 
168 }
169 
170 #else
171 
172 static inline void switch_toupper_max(char *s)
173 {
174  uint32_t *b,*p;
175  char *c;
176  size_t l;
177 
178  l = strlen(s);
179 
180  p = (uint32_t *) s;
181 
182  while (l > 4) {
183  b = p;
184  *b = (uint32_t) switch_toupper(*b);
185  b++;
186  p++;
187  l -= 4;
188  }
189 
190  c = (char *)p;
191 
192  while(l > 0) {
193  *c = (char) switch_toupper(*c);
194  c++;
195  l--;
196  }
197 
198 }
199 
200 static inline void switch_tolower_max(char *s)
201 {
202  uint32_t *b,*p;
203  char *c;
204  size_t l;
205 
206  l = strlen(s);
207 
208  p = (uint32_t *) s;
209 
210  while (l > 4) {
211  b = p;
212  *b = (uint32_t) switch_tolower(*b);
213  b++;
214  p++;
215  l -= 4;
216  }
217 
218  c = (char *)p;
219 
220  while(l > 0) {
221  *c = (char) switch_tolower(*c);
222  c++;
223  l--;
224  }
225 
226 }
227 #endif
228 
229 
230 
231 
234 SWITCH_DECLARE(int) switch_isalnum(int c);
235 SWITCH_DECLARE(int) switch_isalpha(int c);
236 SWITCH_DECLARE(int) switch_iscntrl(int c);
237 SWITCH_DECLARE(int) switch_isdigit(int c);
238 SWITCH_DECLARE(int) switch_isgraph(int c);
239 SWITCH_DECLARE(int) switch_islower(int c);
240 SWITCH_DECLARE(int) switch_isprint(int c);
241 SWITCH_DECLARE(int) switch_ispunct(int c);
242 SWITCH_DECLARE(int) switch_isspace(int c);
243 SWITCH_DECLARE(int) switch_isupper(int c);
244 SWITCH_DECLARE(int) switch_isxdigit(int c);
245 
246 typedef union{
247  uint32_t v4;
248  struct in6_addr v6;
249 } ip_t;
250 
252 
253 
255 
256 #define switch_goto_status(_status, _label) status = _status; goto _label
257 #define switch_goto_int(_n, _i, _label) _n = _i; goto _label
258 #define switch_samples_per_packet(rate, interval) ((uint32_t)((float)rate / (1000.0f / (float)interval)))
259 #define SWITCH_SMAX 32767
260 #define SWITCH_SMIN -32768
261 #define switch_normalize_to_16bit(n) if (n > SWITCH_SMAX) n = SWITCH_SMAX; else if (n < SWITCH_SMIN) n = SWITCH_SMIN;
262 #define switch_codec2str(codec,buf,len) snprintf(buf, len, "%s@%uh@%ui", \
263  codec->implementation->iananame, \
264  codec->implementation->samples_per_second, \
265  codec->implementation->microseconds_per_packet / 1000)
266 
267 
268 
269 /*!
270  \brief Test for NULL or zero length string
271  \param s the string to test
272  \return true value if the string is NULL or zero length
273 */
274 _Check_return_ static inline int _zstr(_In_opt_z_ const char *s)
275 {
276  return !s || *s == '\0';
277 }
278 #ifdef _PREFAST_
279 #define zstr(x) (_zstr(x) ? 1 : __analysis_assume(x),0)
280 #else
281 #define zstr(x) _zstr(x)
282 #endif
283 #define switch_strlen_zero(x) zstr(x)
284 #define switch_strlen_zero_buf(x) zstr_buf(x)
285 #define zstr_buf(s) (*(s) == '\0')
286 static inline switch_bool_t switch_is_moh(const char *s)
287 {
288  if (zstr(s) || !strcasecmp(s, "silence") || !strcasecmp(s, "indicate_hold")) {
289  return SWITCH_FALSE;
290  }
291  return SWITCH_TRUE;
292 }
293 
294 
295 #define zset(_a, _b) if (!zstr(_b)) _a = _b
296 
297 
298 /* find a character (find) in a string (in) and return a pointer to that point in the string where the character was found
299  using the array (allowed) as allowed non-matching characters, when (allowed) is NULL, behaviour should be identical to strchr()
300  */
301 static inline char *switch_strchr_strict(const char *in, char find, const char *allowed)
302 {
303  const char *p;
304 
305  switch_assert(in);
306 
307  p = in;
308 
309  if (!*p) return NULL;
310 
311  while(p && *p) {
312  const char *a = allowed;
313  int acceptable = 0;
314 
315  if (*p == find) break;
316 
317  if (!a) {
318  acceptable = 1;
319  } else {
320 
321  while(a && *a) {
322 
323  if (*p == *a) {
324  acceptable = 1;
325  break;
326  }
327 
328  a++;
329  }
330 
331  }
332 
333  if (!acceptable) return NULL;
334 
335  p++;
336  }
337 
338  return (char *) p;
339 }
340 
341 #define switch_arraylen(_a) (sizeof(_a) / sizeof(_a[0]))
342 #define switch_split(_data, _delim, _array) switch_separate_string(_data, _delim, _array, switch_arraylen(_array))
343 
344 #define switch_is_valid_rate(_tmp) (_tmp == 8000 || _tmp == 12000 || _tmp == 16000 || _tmp == 24000 || _tmp == 32000 || _tmp == 11025 || _tmp == 22050 || _tmp == 44100 || _tmp == 48000)
345 
346 
347 #ifdef _MSC_VER
348 #pragma warning(disable:6011)
349 #endif
350 static inline int switch_string_has_escaped_data(const char *in)
351 {
352  const char *i;
353 
354  switch_assert(in);
355 
356  i = strchr(in, '\\');
357 
358  while (i && *i == '\\') {
359  i++;
360  if (*i == '\\' || *i == 'n' || *i == 's' || *i == 't' || *i == '\'') {
361  return 1;
362  }
363  i = strchr(i, '\\');
364  }
365 
366  return 0;
367 }
368 #ifdef _MSC_VER
369 #pragma warning(default:6011)
370 #endif
371 
372 SWITCH_DECLARE(switch_status_t) switch_b64_encode(unsigned char *in, switch_size_t ilen, unsigned char *out, switch_size_t olen);
373 SWITCH_DECLARE(switch_size_t) switch_b64_decode(char *in, char *out, switch_size_t olen);
374 SWITCH_DECLARE(char *) switch_amp_encode(char *s, char *buf, switch_size_t len);
375 
376 static inline switch_bool_t switch_is_digit_string(const char *s)
377 {
378 
379  while (s && *s) {
380  if (*s < 48 || *s > 57) {
381  return SWITCH_FALSE;
382  }
383  s++;
384  }
385 
386  return SWITCH_TRUE;
387 }
388 
389 static inline char switch_itodtmf(char i)
390 {
391  char r = i;
392 
393  if (i > 9 && i < 14) {
394  r += 55;
395  } else {
396  r += 48;
397  }
398 
399  return r;
400 }
401 
402 static inline int switch_dtmftoi(char *s)
403 {
404  int r;
405 
406  switch_assert(s);
407 
408  if (!(r = atoi(s))) {
409  int l = tolower((unsigned char)*s);
410  if (l > 96 && l < 101) {
411  r = l - 87;
412  }
413  }
414 
415  return r;
416 }
417 
418 static inline uint32_t switch_known_bitrate(switch_payload_t payload)
419 {
420  switch(payload) {
421  case 0: /* PCMU */ return 64000;
422  case 3: /* GSM */ return 13200;
423  case 4: /* G723 */ return 6300;
424  case 7: /* LPC */ return 2400;
425  case 8: /* PCMA */ return 64000;
426  case 9: /* G722 */ return 64000;
427  case 18: /* G729 */ return 8000;
428  default: break;
429  }
430 
431  return 0;
432 }
433 
437 
443 SWITCH_DECLARE(char *) switch_find_parameter(const char *str, const char *param, switch_memory_pool_t *pool);
444 
445 /*!
446  \brief Evaluate the truthfullness of a string expression
447  \param expr a string expression
448  \return true or false
449 */
450 static inline int switch_true(const char *expr)
451 {
452  return ((expr && ( !strcasecmp(expr, "yes") ||
453  !strcasecmp(expr, "on") ||
454  !strcasecmp(expr, "true") ||
455  !strcasecmp(expr, "t") ||
456  !strcasecmp(expr, "enabled") ||
457  !strcasecmp(expr, "active") ||
458  !strcasecmp(expr, "allow") ||
459  (switch_is_number(expr) && atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
460 }
461 
462 static inline switch_byte_t switch_true_byte(const char *expr)
463 {
464  return (switch_byte_t)switch_true(expr);
465 }
466 
467 #define switch_true_buf(expr)\
468 ((( !strcasecmp(expr, "yes") ||\
469 !strcasecmp(expr, "on") ||\
470 !strcasecmp(expr, "true") ||\
471 !strcasecmp(expr, "t") ||\
472 !strcasecmp(expr, "enabled") ||\
473 !strcasecmp(expr, "active") ||\
474 !strcasecmp(expr, "allow") ||\
475 (switch_is_number(expr) && atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE)
476 
477 /*!
478  \brief Evaluate the falsefullness of a string expression
479  \param expr a string expression
480  \return true or false
481 */
482 static inline int switch_false(const char *expr)
483 {
484  return ((expr && ( !strcasecmp(expr, "no") ||
485  !strcasecmp(expr, "off") ||
486  !strcasecmp(expr, "false") ||
487  !strcasecmp(expr, "f") ||
488  !strcasecmp(expr, "disabled") ||
489  !strcasecmp(expr, "inactive") ||
490  !strcasecmp(expr, "disallow") ||
491  (switch_is_number(expr) && !atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
492 }
493 
494 
495 SWITCH_DECLARE(switch_status_t) switch_resolve_host(const char *host, char *buf, size_t buflen);
496 
497 
498 /*!
499  \brief find local ip of the box
500  \param buf the buffer to write the ip address found into
501  \param len the length of the buf
502  \param mask the CIDR found (AF_INET only)
503  \param family the address family to return (AF_INET or AF_INET6)
504  \return SWITCH_STATUS_SUCCESSS for success, otherwise failure
505 */
507  char *buf, _In_ int len, _In_opt_ int *mask, _In_ int family);
508 
509 /*!
510  \brief find primary ip of the specified interface
511  \param buf the buffer to write the ip address found into
512  \param len the length of the buf
513  \param mask the CIDR found (AF_INET only)
514  \param ifname interface name to check
515  \param family the address family to return (AF_INET or AF_INET6)
516  \return SWITCH_STATUS_SUCCESSS for success, otherwise failure
517 */
519  char *buf, _In_ int len, _In_opt_ int *mask, _In_ const char *ifname, _In_ int family);
520 
521 /*!
522  \brief find the char representation of an ip adress
523  \param buf the buffer to write the ip adress found into
524  \param len the length of the buf
525  \param sa the struct sockaddr * to get the adress from
526  \param salen the length of sa
527  \return the ip adress string
528 */
529 SWITCH_DECLARE(char *) get_addr(char *buf, switch_size_t len, struct sockaddr *sa, socklen_t salen);
530 SWITCH_DECLARE(char *) get_addr6(char *buf, switch_size_t len, struct sockaddr_in6 *sa, socklen_t salen);
531 
535 
536 /*!
537  \brief get the port number of an ip address
538  \param sa the struct sockaddr * to get the port from
539  \return the ip adress string
540 */
541 SWITCH_DECLARE(unsigned short) get_port(struct sockaddr *sa);
542 
543 /*!
544  \brief flags to be used with switch_build_uri()
545  */
550  };
551 
552 /*!
553  \brief build a URI string from components
554  \param uri output string
555  \param size maximum size of output string (including trailing null)
556  \param scheme URI scheme
557  \param user user part or null if none
558  \param sa host address
559  \param flags logical OR-ed combination of flags from \ref switch_uri_flags
560  \return number of characters printed (not including the trailing null)
561  */
562 SWITCH_DECLARE(int) switch_build_uri(char *uri, switch_size_t size, const char *scheme, const char *user, const switch_sockaddr_t *sa, int flags);
563 
564 #define SWITCH_STATUS_IS_BREAK(x) (x == SWITCH_STATUS_BREAK || x == 730035 || x == 35 || x == SWITCH_STATUS_INTR)
565 
566 
567 #ifdef _MSC_VER
568 
569 #define switch_errno() WSAGetLastError()
570 
571 static inline int switch_errno_is_break(int errcode)
572 {
573  return errcode == WSAEWOULDBLOCK || errcode == WSAEINPROGRESS || errcode == WSAEINTR;
574 }
575 
576 #else
577 
578 #define switch_errno() errno
579 
580 static inline int switch_errno_is_break(int errcode)
581 {
582  return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS || errcode == EINTR || errcode == ETIMEDOUT;
583 }
584 
585 #endif
586 
587 
588 /*!
589  \brief Return a printable name of a switch_priority_t
590  \param priority the priority to get the name of
591  \return the printable form of the priority
592 */
594 
595 /*!
596  \brief Return the RFC2833 character based on an event id
597  \param event the event id to convert
598  \return the character represented by the event or null for an invalid event
599 */
600 SWITCH_DECLARE(char) switch_rfc2833_to_char(int event);
601 
602 /*!
603  \brief Return the RFC2833 event based on an key character
604  \param key the charecter to encode
605  \return the event id for the specified character or -1 on an invalid input
606 */
607 SWITCH_DECLARE(unsigned char) switch_char_to_rfc2833(char key);
608 
609 /*!
610  \brief determine if a character is a valid DTMF key
611  \param key the key to test
612  \return TRUE or FALSE
613  */
614 #define is_dtmf(key) ((key > 47 && key < 58) || (key > 64 && key < 69) || (key > 96 && key < 101) || key == 35 || key == 42 || key == 87 || key == 119 || key == 70 || key == 102)
615 
616 #define end_of(_s) *(*_s == '\0' ? _s : _s + strlen(_s) - 1)
617 #define end_of_p(_s) (*_s == '\0' ? _s : _s + strlen(_s) - 1)
618 /*!
619  \brief Test for the existance of a flag on an arbitary object
620  \param obj the object to test
621  \param flag the or'd list of flags to test
622  \return true value if the object has the flags defined
623 */
624 #define switch_test_flag(obj, flag) ((obj)->flags & flag)
625 
626 /*!
627  \brief Set a flag on an arbitrary object
628  \param obj the object to set the flags on
629  \param flag the or'd list of flags to set
630 */
631 #define switch_set_flag(obj, flag) (obj)->flags |= (flag)
632 
633 /*!
634  \brief Set a flag on an arbitrary object while locked
635  \param obj the object to set the flags on
636  \param flag the or'd list of flags to set
637 */
638 #define switch_set_flag_locked(obj, flag) assert((obj)->flag_mutex != NULL); \
639 switch_mutex_lock((obj)->flag_mutex); \
640 (obj)->flags |= (flag);\
641 switch_mutex_unlock((obj)->flag_mutex);
642 
643 /*!
644  \brief Clear a flag on an arbitrary object
645  \param obj the object to test
646  \param flag the or'd list of flags to clear
647 */
648 #define switch_clear_flag_locked(obj, flag) switch_mutex_lock((obj)->flag_mutex); (obj)->flags &= ~(flag); switch_mutex_unlock((obj)->flag_mutex);
649 
650 /*!
651  \brief Clear a flag on an arbitrary object while locked
652  \param obj the object to test
653  \param flag the or'd list of flags to clear
654 */
655 #define switch_clear_flag(obj, flag) (obj)->flags &= ~(flag)
656 
657 /*!
658  \brief Copy flags from one arbitrary object to another
659  \param dest the object to copy the flags to
660  \param src the object to copy the flags from
661  \param flags the flags to copy
662 */
663 #define switch_copy_flags(dest, src, flags) (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags))
664 
665 #define switch_set_string(_dst, _src) switch_copy_string(_dst, _src, sizeof(_dst))
666 
667 
668 static inline char *switch_sanitize_number(char *number)
669 {
670  char *p = number, *q;
671  char warp[] = "/:";
672  int i;
673 
674  switch_assert(number);
675 
676  if (!(strchr(p, '/') || strchr(p, ':') || strchr(p, '@'))) {
677  return number;
678  }
679 
680  while ((q = strrchr(p, '@')))
681  *q = '\0';
682 
683  for (i = 0; i < (int) strlen(warp); i++) {
684  while (p && (q = strchr(p, warp[i])))
685  p = q + 1;
686  }
687 
688  return p;
689 }
690 
691 static inline switch_bool_t switch_string_var_check(char *s, switch_bool_t disable)
692 {
693  char *p;
694  char *dol = NULL;
695 
696  for (p = s; p && *p; p++) {
697  if (*p == '$') {
698  dol = p;
699  } else if (dol) {
700  if (*p == '{') {
701  if (disable) {
702  *dol = '%';
703  dol = NULL;
704  } else {
705  return SWITCH_TRUE;
706  }
707  } else if (*p != '\\') {
708  dol = NULL;
709  }
710  }
711  }
712  return SWITCH_FALSE;
713 }
714 
715 
716 static inline switch_bool_t switch_string_var_check_const(const char *s)
717 {
718  const char *p;
719  int dol = 0;
720 
721  for (p = s; p && *p; p++) {
722  if (*p == '$') {
723  dol = 1;
724  } else if (dol) {
725  if (*p == '{') {
726  return SWITCH_TRUE;
727  } else if (*p != '\\') {
728  dol = 0;
729  }
730  }
731  }
732  return SWITCH_FALSE;
733 }
734 
735 static inline char *switch_var_clean_string(char *s)
736 {
738  return s;
739 }
740 
741 static inline char *switch_clean_string(char *s)
742 {
743  char *p;
744  for (p = s; p && *p; p++) {
745  uint8_t x = (uint8_t) * p;
746  if ((x < 32) && x != '\n' && x != '\r') {
747  *p = ' ';
748  }
749  }
750 
751  return s;
752 }
753 
754 
755 static inline char *switch_clean_name_string(char *s)
756 {
757  char *p;
758  for (p = s; p && *p; p++) {
759  uint8_t x = (uint8_t) * p;
760  if ((x < 32) || x == '\'' || x == '"' || x == '<' || x == '>' || x == '\\' || x == ':' || x == '@' || x == '/') {
761  *p = ' ';
762  }
763  if ( (p == s) && (*p == ' ') ) {
764  s++;
765  }
766  }
767 
768  return s;
769 }
770 
771 
772 
773 /*!
774  \brief Turn a string into a number (default if NULL)
775  \param nptr the string
776  \param dft the default
777  \return the number version of the string or the default
778 */
779 static inline int switch_safe_atoi(const char *nptr, int dft)
780 {
781  return nptr ? atoi(nptr) : dft;
782 }
783 
784 
785 /*!
786  \brief Free a pointer and set it to NULL unless it already is NULL
787  \param it the pointer
788 */
789 #define switch_safe_free(it) if (it) {free(it);it=NULL;}
790 
791 static inline char *switch_safe_strdup(const char *it)
792 {
793  if (it) {
794  return strdup(it);
795  }
796 
797  return NULL;
798 }
799 
800 
801 #ifdef _MSC_VER
802 #pragma warning(disable:6011)
803 #endif
804 static inline char *switch_lc_strdup(const char *it)
805 {
806  char *dup;
807  char *p;
808 
809  if (it) {
810  dup = strdup(it);
811  for (p = dup; p && *p; p++) {
812  *p = (char) switch_tolower(*p);
813  }
814  return dup;
815  }
816 
817  return NULL;
818 }
819 
820 
821 static inline char *switch_uc_strdup(const char *it)
822 {
823  char *dup;
824  char *p;
825 
826  if (it) {
827  dup = strdup(it);
828  for (p = dup; p && *p; p++) {
829  *p = (char) switch_toupper(*p);
830  }
831  return dup;
832  }
833 
834  return NULL;
835 }
836 #ifdef _MSC_VER
837 #pragma warning(default:6011)
838 #endif
839 
840 
841 /*!
842  \brief Test if one string is inside another with extra case checking
843  \param s the inner string
844  \param q the big string
845  \return SWITCH_TRUE or SWITCH_FALSE
846 */
847 static inline switch_bool_t switch_strstr(char *s, char *q)
848 {
849  char *p, *S = NULL, *Q = NULL;
851 
852  if (!s || !q) {
853  return SWITCH_FALSE;
854  }
855 
856  if (strstr(s, q)) {
857  return SWITCH_TRUE;
858  }
859 
860  S = strdup(s);
861 
862  switch_assert(S != NULL);
863 
864  for (p = S; p && *p; p++) {
865  *p = (char) switch_toupper(*p);
866  }
867 
868  if (strstr(S, q)) {
869  tf = SWITCH_TRUE;
870  goto done;
871  }
872 
873  Q = strdup(q);
874  switch_assert(Q != NULL);
875 
876  for (p = Q; p && *p; p++) {
877  *p = (char) switch_toupper(*p);
878  }
879 
880  if (strstr(s, Q)) {
881  tf = SWITCH_TRUE;
882  goto done;
883  }
884 
885  if (strstr(S, Q)) {
886  tf = SWITCH_TRUE;
887  goto done;
888  }
889 
890  done:
891  switch_safe_free(S);
892  switch_safe_free(Q);
893 
894  return tf;
895 }
896 
897 
898 /*!
899  \brief Make a null string a blank string instead
900  \param s the string to test
901  \return the original string or blank string.
902 */
903 #define switch_str_nil(s) (s ? s : "")
904 
905 /*!
906  \brief Wait a desired number of microseconds and yield the CPU
907 */
908 #define switch_yield(ms) switch_sleep(ms);
909 
910 /*!
911  \brief Converts a string representation of a date into a switch_time_t
912  \param in the string
913  \return the epoch time in usec
914 */
916 #define switch_time_from_sec(sec) ((switch_time_t)(sec) * 1000000)
917 
918 /*!
919  \brief Declares a function designed to set a dynamic global string
920  \param fname the function name to declare
921  \param vname the name of the global pointer to modify with the new function
922 */
923 #define SWITCH_DECLARE_GLOBAL_STRING_FUNC(fname, vname) static void fname(const char *string) { if (!string) return;\
924  if (vname) {free(vname); vname = NULL;}vname = strdup(string);} static void fname(const char *string)
925 
926 /*!
927  \brief Separate a string into an array based on a character delimiter
928  \param buf the string to parse
929  \param delim the character delimiter
930  \param array the array to split the values into
931  \param arraylen the max number of elements in the array
932  \return the number of elements added to the array
933 */
934 SWITCH_DECLARE(unsigned int) switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen);
935 SWITCH_DECLARE(unsigned int) switch_separate_string_string(char *buf, char *delim, _Post_count_(return) char **array, unsigned int arraylen);
936 
937 
938 SWITCH_DECLARE(char *) switch_strip_spaces(char *str, switch_bool_t dup);
939 SWITCH_DECLARE(char *) switch_strip_whitespace(const char *str);
940 SWITCH_DECLARE(char *) switch_strip_commas(char *in, char *out, switch_size_t len);
941 SWITCH_DECLARE(char *) switch_strip_nonnumerics(char *in, char *out, switch_size_t len);
942 SWITCH_DECLARE(char *) switch_separate_paren_args(char *str);
943 SWITCH_DECLARE(const char *) switch_stristr(const char *instr, const char *str);
945 SWITCH_DECLARE(char *) switch_replace_char(char *str, char from, char to, switch_bool_t dup);
946 SWITCH_DECLARE(switch_bool_t) switch_ast2regex(const char *pat, char *rbuf, size_t len);
947 
948 /*!
949  \brief Escape a string by prefixing a list of characters with an escape character
950  \param pool a memory pool to use
951  \param in the string
952  \param delim the list of characters to escape
953  \param esc the escape character
954  \return the escaped string
955 */
956 SWITCH_DECLARE(char *) switch_escape_char(switch_memory_pool_t *pool, char *in, const char *delim, char esc);
957 
958 SWITCH_DECLARE(char *) switch_escape_string(const char *in, char *out, switch_size_t outlen);
959 SWITCH_DECLARE(char *) switch_escape_string_pool(const char *in, switch_memory_pool_t *pool);
960 
961 /*!
962  \brief Wait for a socket
963  \param poll the pollfd to wait on
964  \param ms the number of milliseconds to wait
965  \return the requested condition
966 */
968 
969 /*!
970  \brief Create a pointer to the file name in a given file path eliminating the directory name
971  \return the pointer to the next character after the final / or \\ characters
972 */
973 SWITCH_DECLARE(const char *) switch_cut_path(const char *in);
974 
975 SWITCH_DECLARE(char *) switch_string_replace(const char *string, const char *search, const char *replace);
976 SWITCH_DECLARE(switch_status_t) switch_string_match(const char *string, size_t string_len, const char *search, size_t search_len);
977 SWITCH_DECLARE(int) switch_strcasecmp_any(const char *str, ...);
978 
979 /*!
980  \brief Quote shell argument
981  \param string the string to quote (example: a ' b"' c)
982  \return the quoted string (gives: 'a '\'' b"'\'' c' for unices, "a ' b ' c" for MS Windows), should be freed
983 */
984 SWITCH_DECLARE(char *) switch_util_quote_shell_arg(const char *string);
985 
986 /*!
987  \brief Quote shell argument, allocating from pool if provided
988  \param string the string to quote (example: a ' b"' c)
989  \param pool a memory pool to use
990  \return the quoted string (gives: 'a '\'' b"'\'' c' for unices, "a ' b ' c" for MS Windows), if pool not provided, returned value should be freed
991 */
992 SWITCH_DECLARE(char *) switch_util_quote_shell_arg_pool(const char *string, switch_memory_pool_t *pool);
993 
994 
995 #define SWITCH_READ_ACCEPTABLE(status) (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK || status == SWITCH_STATUS_INUSE)
996 
997 
998 static inline int32_t switch_calc_bitrate(int w, int h, int quality, double fps)
999 {
1000  int r;
1001 
1002  /* KUSH GAUGE*/
1003 
1004  if (!fps) fps = 15;
1005 
1006  r = (int32_t)((double)(w * h * fps * (quality ? quality : 1)) * 0.07) / 1000;
1007 
1008  if (!quality) r /= 2;
1009 
1010  return r;
1011 
1012 }
1013 
1014 static inline int32_t switch_parse_bandwidth_string(const char *bwv)
1015 {
1016  float bw = 0;
1017 
1018  if (!bwv) return 0;
1019 
1020  if (!strcasecmp(bwv, "auto")) {
1021  return -1;
1022  }
1023 
1024  if ((bw = (float) atof(bwv))) {
1025  if (bw < 0) return 0;
1026 
1027  if (strstr(bwv, "KB")) {
1028  bw *= 8;
1029  } else if (strstr(bwv, "mb")) {
1030  bw *= 1024;
1031  } else if (strstr(bwv, "MB")) {
1032  bw *= 8192;
1033  }
1034  }
1035 
1036  return (int32_t) roundf(bw);
1037 }
1038 
1039 static inline int switch_needs_url_encode(const char *s)
1040 {
1041  const char hex[] = "0123456789ABCDEF";
1042  const char *p, *e = end_of_p(s);
1043 
1044 
1045  for(p = s; p && *p; p++) {
1046  if (*p == '%' && e-p > 1) {
1047  if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
1048  p++;
1049  continue;
1050  }
1051  }
1052 
1053  if (strchr(SWITCH_URL_UNSAFE, *p)) {
1054  return 1;
1055  }
1056  }
1057 
1058  return 0;
1059 }
1060 
1061 SWITCH_DECLARE(char *) switch_url_encode_opt(const char *url, char *buf, size_t len, switch_bool_t double_encode);
1062 SWITCH_DECLARE(char *) switch_url_encode(const char *url, char *buf, size_t len);
1063 SWITCH_DECLARE(char *) switch_url_decode(char *s);
1065  const char *from,
1066  const char *headers,
1067  const char *body, const char *file, const char *convert_cmd, const char *convert_ext);
1068 SWITCH_DECLARE(char *) switch_find_end_paren(const char *s, char open, char close);
1069 
1070 static inline void switch_separate_file_params(const char *file, char **file_portion, char **params_portion)
1071 {
1072  char *e = NULL;
1073  char *space = strdup(file);
1074 
1075  file = space;
1076 
1077  *file_portion = NULL;
1078  *params_portion = NULL;
1079 
1080  while (*file == '{') {
1081  e = switch_find_end_paren(file, '{', '}');
1082  file = e + 1;
1083  while(*file == ' ') file++;
1084  }
1085 
1086 
1087  if (e) {
1088  *file_portion = strdup((char *)file);
1089  *++e = '\0';
1090  *params_portion = (char *)space;
1091  } else {
1092  *file_portion = (char *)space;
1093  }
1094 
1095  return;
1096 }
1097 
1098 static inline switch_bool_t switch_is_file_path(const char *file)
1099 {
1100  const char *e;
1101  int r;
1102 
1103  if (zstr(file)) {
1104  return SWITCH_FALSE;
1105  }
1106 
1107  while(*file == '{') {
1108  if ((e = switch_find_end_paren(file, '{', '}'))) {
1109  file = e + 1;
1110  while(*file == ' ') file++;
1111  }
1112  }
1113 
1114 #ifdef WIN32
1115  r = (*file == '\\' || *(file + 1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR));
1116 #else
1117  r = ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR));
1118 #endif
1119 
1120  return r ? SWITCH_TRUE : SWITCH_FALSE;
1121 }
1122 
1123 
1124 static inline const char *switch_parse_audio_col(switch_audio_col_t col)
1125 {
1126  const char *field = NULL;
1127 
1128  switch (col) {
1130  field = "title";
1131  break;
1133  field = "comment";
1134  break;
1136  field = "artist";
1137  break;
1139  field = "date";
1140  break;
1142  field = "software";
1143  break;
1145  field = "copyright";
1146  break;
1147  default:
1148  break;
1149  }
1150 
1151  return field;
1152 }
1153 
1154 SWITCH_DECLARE(int) switch_parse_cidr(const char *string, ip_t *ip, ip_t *mask, uint32_t *bitp);
1156  switch_memory_pool_t *pool);
1157 SWITCH_DECLARE(switch_status_t) switch_network_list_add_cidr_token(switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token);
1158 #define switch_network_list_add_cidr(_list, _cidr_str, _ok) switch_network_list_add_cidr_token(_list, _cidr_str, _ok, NULL)
1159 
1160 SWITCH_DECLARE(char *) switch_network_ipv4_mapped_ipv6_addr(const char* ip_str);
1161 SWITCH_DECLARE(switch_status_t) switch_network_list_add_host_mask(switch_network_list_t *list, const char *host, const char *mask_str, switch_bool_t ok);
1164 #define switch_network_list_validate_ip(_list, _ip) switch_network_list_validate_ip_token(_list, _ip, NULL);
1165 
1166 #define switch_test_subnet(_ip, _net, _mask) (_mask ? ((_net & _mask) == (_ip & _mask)) : _net ? _net == _ip : 1)
1167 
1168 SWITCH_DECLARE(int) switch_inet_pton(int af, const char *src, void *dst);
1169 
1170 SWITCH_DECLARE(const char *) switch_dow_int2str(int val);
1171 SWITCH_DECLARE(int) switch_dow_str2int(const char *exp);
1172 SWITCH_DECLARE(switch_bool_t) switch_dow_cmp(const char *exp, int val);
1173 SWITCH_DECLARE(int) switch_number_cmp(const char *exp, int val);
1174 SWITCH_DECLARE(int) switch_tod_cmp(const char *exp, int val);
1175 
1176 SWITCH_DECLARE(int) switch_fulldate_cmp(const char *exp, switch_time_t *ts);
1177 SWITCH_DECLARE(void) switch_split_date(const char *exp, int *year, int *month, int *day);
1178 SWITCH_DECLARE(void) switch_split_time(const char *exp, int *hour, int *min, int *sec);
1179 
1180 /*!
1181  \brief Split a user@domain string as user and domain
1182  \param in the input string
1183  \param user the string to put the user into
1184  \param domain the string to put the domain into
1185  \return 1 if successfull
1186  \note Extended formats protocol:user@domain:port (Example: sip:toto@example.org)
1187 */
1188 SWITCH_DECLARE(int) switch_split_user_domain(char *in, char **user, char **domain);
1189 
1190 /* malloc or DIE macros */
1191 #ifdef NDEBUG
1192 #define switch_malloc(ptr, len) (void)( (!!(ptr = malloc(len))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%d", __FILE__, __LINE__),abort(), 0), ptr )
1193 #define switch_zmalloc(ptr, len) (void)( (!!(ptr = calloc(1, (len)))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%d", __FILE__, __LINE__),abort(), 0), ptr)
1194 #if (_MSC_VER >= 1500) // VC9+
1195 #define switch_strdup(ptr, s) (void)( (!!(ptr = _strdup(s))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%d", __FILE__, __LINE__),abort(), 0), ptr)
1196 #else
1197 #define switch_strdup(ptr, s) (void)( (!!(ptr = strdup(s))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%d", __FILE__, __LINE__),abort(), 0), ptr)
1198 #endif
1199 #else
1200 #if (_MSC_VER >= 1500) // VC9+
1201 #define switch_malloc(ptr, len) (void)(assert(((ptr) = malloc((len)))),ptr);__analysis_assume( ptr )
1202 #define switch_zmalloc(ptr, len) (void)(assert((ptr = calloc(1, (len)))),ptr);__analysis_assume( ptr )
1203 #define switch_strdup(ptr, s) (void)(assert(((ptr) = _strdup(s))),ptr);__analysis_assume( ptr )
1204 #else
1205 #define switch_malloc(ptr, len) (void)(assert(((ptr) = malloc((len)))),ptr)
1206 #define switch_zmalloc(ptr, len) (void)(assert((ptr = calloc(1, (len)))),ptr)
1207 #define switch_strdup(ptr, s) (void)(assert(((ptr) = strdup((s)))),ptr)
1208 #endif
1209 #endif
1210 
1211 #define DUMP_EVENT(_e) {char *event_str;switch_event_serialize(_e, &event_str, SWITCH_FALSE);switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "DUMP\n%s\n", event_str);free(event_str);}
1212 
1213 #ifndef _MSC_VER
1214 #define switch_inet_ntop inet_ntop
1215 #else
1216 SWITCH_DECLARE(const char *) switch_inet_ntop(int af, void const *src, char *dst, size_t size);
1217 #endif
1218 
1219 SWITCH_DECLARE(char *) switch_uuid_str(char *buf, switch_size_t len);
1220 SWITCH_DECLARE(char *) switch_format_number(const char *num);
1221 
1222 SWITCH_DECLARE(unsigned int) switch_atoui(const char *nptr);
1223 SWITCH_DECLARE(unsigned long) switch_atoul(const char *nptr);
1224 
1225 /**
1226  * Portable version of strerror_r(), work around for the incompatible
1227  * return type of GNU and XSI variants.
1228  * \param[in] errnum Error number
1229  * \param[both] buf Buffer for error message
1230  * \param[in] buflen Size of message buffer
1231  * \return Pointer to message buffer, returning error message or "Unknown error xxx" if none found
1232  */
1233 SWITCH_DECLARE(char *) switch_strerror_r(int errnum, char *buf, switch_size_t buflen);
1234 SWITCH_DECLARE(int) switch_wait_sock(switch_os_socket_t sock, uint32_t ms, switch_poll_t flags);
1235 SWITCH_DECLARE(int) switch_wait_socklist(switch_waitlist_t *waitlist, uint32_t len, uint32_t ms);
1236 
1237 typedef struct switch_http_request_s {
1238  const char *method; /* GET POST PUT DELETE OPTIONS PATCH HEAD */
1239  const char *uri;
1240  const char *qs; /* query string*/
1241  const char *host;
1243  const char *from;
1244  const char *user_agent;
1245  const char *referer;
1246  const char *user;
1248  const char *content_type;
1254  void *user_data; /* private user data */
1255 
1256  /* private members used by the parser internally */
1257  char *_buffer;
1260 
1261 /**
1262  * parse http headers in a buffer
1263  * return status of success or not
1264  * \param[in] buffer the buffer start from the very begining of the http request, e.g. 'GET '
1265  * \param[in] datalen the buffer length
1266  * \param[out] the http request pointer or null, need destroy later if got non-NULL pointer
1267  * \return SWITCH_STATUS_SUCCESS | SWITCH_STATUS_FALSE
1268  */
1269 SWITCH_DECLARE(switch_status_t) switch_http_parse_header(char *buffer, uint32_t datalen, switch_http_request_t *request);
1272 /**
1273  * parse http query string
1274  * \param[in] request the http request object
1275  * \param[in] qs the query string buffer
1276  *
1277  * qs buffer will be modified, so be sure to dup the qs before passing into this function if you want to keep the original string untouched
1278  * if qs is NULL, the it will parse request->qs, request->qs will be duplicated before parse to avoid being modified
1279  */
1280 
1282 
1287 
1288 typedef struct {
1289  int64_t userms;
1290  int64_t kernelms;
1291 } switch_cputime;
1292 /**
1293 / Return used CPU time in this process for user and kernel code
1294 **/
1296 
1298 #endif
1299 /* For Emacs:
1300  * Local Variables:
1301  * mode:c
1302  * indent-tabs-mode:t
1303  * tab-width:4
1304  * c-basic-offset:4
1305  * End:
1306  * For VIM:
1307  * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
1308  */
static void switch_tolower_max(char *s)
Definition: switch_utils.h:200
int switch_isdigit(int c)
#define switch_inet_ntop
unsigned int switch_atoui(const char *nptr)
const char * switch_priority_name(switch_priority_t priority)
Return a printable name of a switch_priority_t.
int switch_isspace(int c)
int switch_islower(int c)
switch_status_t switch_frame_buffer_free(switch_frame_buffer_t *fb, switch_frame_t **frameP)
Definition: switch_utils.c:193
char * switch_find_end_paren(const char *s, char open, char close)
Definition: switch_utils.c:661
static void switch_toupper_max(char *s)
Definition: switch_utils.h:172
static char * switch_sanitize_number(char *number)
Definition: switch_utils.h:668
int switch_wait_sock(switch_os_socket_t sock, uint32_t ms, switch_poll_t flags)
char * switch_strip_nonnumerics(char *in, char *out, switch_size_t len)
switch_size_t bytes_read
switch_status_t switch_network_list_create(switch_network_list_t **list, const char *name, switch_bool_t default_type, switch_memory_pool_t *pool)
Definition: switch_utils.c:404
#define SWITCH_END_EXTERN_C
Definition: switch.h:43
int switch_iscntrl(int c)
switch_status_t switch_network_list_add_cidr_token(switch_network_list_t *list, const char *cidr_str, switch_bool_t ok, const char *token)
Definition: switch_utils.c:552
static int switch_errno_is_break(int errcode)
Definition: switch_utils.h:580
static void switch_separate_file_params(const char *file, char **file_portion, char **params_portion)
static char * switch_var_clean_string(char *s)
Definition: switch_utils.h:735
static int switch_string_has_escaped_data(const char *in)
Definition: switch_utils.h:350
switch_uri_flags
flags to be used with switch_build_uri()
Definition: switch_utils.h:546
unsigned int switch_separate_string_string(char *buf, char *delim, _Post_count_(return) char **array, unsigned int arraylen)
int get_addr_int(switch_sockaddr_t *sa)
int switch_build_uri(char *uri, switch_size_t size, const char *scheme, const char *user, const switch_sockaddr_t *sa, int flags)
build a URI string from components
switch_bool_t switch_is_leading_number(const char *str)
switch_bool_t
Definition: switch_types.h:405
int switch_parse_cidr(const char *string, ip_t *ip, ip_t *mask, uint32_t *bitp)
Definition: switch_utils.c:607
switch_status_t switch_frame_alloc(switch_frame_t **frame, switch_size_t size)
Definition: switch_utils.c:83
switch_audio_col_t
Definition: switch_types.h:578
#define SWITCH_URL_SEPARATOR
Definition: switch_types.h:124
switch_priority_t
Priority Indication.
Definition: switch_types.h:991
switch_memory_pool_t * pool
void switch_http_dump_request(switch_http_request_t *request)
Representation of an event.
Definition: switch_event.h:80
char * switch_amp_encode(char *s, char *buf, switch_size_t len)
Definition: switch_utils.c:804
switch_status_t switch_frame_free(switch_frame_t **frame)
Definition: switch_utils.c:308
char * switch_format_number(const char *num)
int old_switch_toupper(int c)
char * switch_util_quote_shell_arg_pool(const char *string, switch_memory_pool_t *pool)
Quote shell argument, allocating from pool if provided.
switch_status_t switch_http_parse_header(char *buffer, uint32_t datalen, switch_http_request_t *request)
char * switch_find_parameter(const char *str, const char *param, switch_memory_pool_t *pool)
Definition: switch_utils.c:360
char * switch_print_host(switch_sockaddr_t *addr, char *buf, switch_size_t len)
Definition: switch_utils.c:72
switch_bool_t switch_is_number(const char *str)
char * switch_strip_whitespace(const char *str)
switch_bool_t switch_network_list_validate_ip6_token(switch_network_list_t *list, ip_t ip, const char **token)
Definition: switch_utils.c:441
switch_size_t switch_fd_read_line(int fd, char *buf, switch_size_t len)
Definition: switch_utils.c:687
int switch_isgraph(int c)
void switch_split_date(const char *exp, int *year, int *month, int *day)
char * switch_url_encode(const char *url, char *buf, size_t len)
static int switch_true(const char *expr)
Evaluate the truthfullness of a string expression.
Definition: switch_utils.h:450
int switch_strcasecmp_any(const char *str,...)
Definition: switch_utils.c:339
switch_status_t switch_frame_buffer_dup(switch_frame_buffer_t *fb, switch_frame_t *orig, switch_frame_t **clone)
Definition: switch_utils.c:226
static int32_t switch_parse_bandwidth_string(const char *bwv)
switch_bool_t switch_is_lan_addr(const char *ip)
switch_size_t switch_b64_decode(char *in, char *out, switch_size_t olen)
Definition: switch_utils.c:929
const char * content_type
switch_status_t switch_frame_buffer_destroy(switch_frame_buffer_t **fbP)
Definition: switch_utils.c:243
#define SWITCH_URL_UNSAFE
Definition: switch_utils.h:47
static uint32_t switch_known_bitrate(switch_payload_t payload)
Definition: switch_utils.h:418
uint8_t switch_byte_t
Definition: switch_types.h:246
const char * user_agent
switch_bool_t _destroy_headers
#define end_of_p(_s)
Definition: switch_utils.h:617
#define zstr(x)
Definition: switch_utils.h:281
static int switch_safe_atoi(const char *nptr, int dft)
Turn a string into a number (default if NULL)
Definition: switch_utils.h:779
static char * switch_strchr_strict(const char *in, char find, const char *allowed)
Definition: switch_utils.h:301
int switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2)
switch_status_t switch_resolve_host(const char *host, char *buf, size_t buflen)
switch_bool_t switch_network_list_validate_ip_token(switch_network_list_t *list, uint32_t ip, const char **token)
Definition: switch_utils.c:468
static char * switch_safe_strdup(const char *it)
Definition: switch_utils.h:791
switch_bool_t switch_dow_cmp(const char *exp, int val)
void switch_getcputime(switch_cputime *t)
int64_t switch_time_t
Definition: switch_apr.h:188
switch_status_t switch_network_list_add_host_mask(switch_network_list_t *list, const char *host, const char *mask_str, switch_bool_t ok)
Definition: switch_utils.c:579
switch_byte_t switch_byte_t * buf
unsigned int switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen)
Separate a string into an array based on a character delimiter.
static switch_bool_t switch_strstr(char *s, char *q)
Test if one string is inside another with extra case checking.
Definition: switch_utils.h:847
switch_byte_t in
static switch_bool_t switch_string_var_check(char *s, switch_bool_t disable)
Definition: switch_utils.h:691
switch_bool_t switch_testv6_subnet(ip_t _ip, ip_t _net, ip_t _mask)
Definition: switch_utils.c:428
static switch_bool_t switch_is_moh(const char *s)
Definition: switch_utils.h:286
struct apr_sockaddr_t switch_sockaddr_t
Definition: switch_apr.h:1029
unsigned short get_port(struct sockaddr *sa)
get the port number of an ip address
int switch_split_user_domain(char *in, char **user, char **domain)
Split a user string as user and domain.
switch_status_t switch_b64_encode(unsigned char *in, switch_size_t ilen, unsigned char *out, switch_size_t olen)
Definition: switch_utils.c:890
switch_status_t switch_frame_buffer_create(switch_frame_buffer_t **fbP)
Definition: switch_utils.c:254
uint32_t v4
Definition: switch_utils.h:247
int old_switch_tolower(int c)
#define _Out_opt_bytecapcount_(x)
static int32_t switch_calc_bitrate(int w, int h, int quality, double fps)
Definition: switch_utils.h:998
int switch_isxdigit(int c)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
Definition: switch_utils.h:789
static uint32_t switch_toupper(uint32_t eax)
Definition: switch_utils.h:70
int switch_isprint(int c)
unsigned long switch_atoul(const char *nptr)
int switch_os_socket_t
#define _In_opt_
char * switch_network_ipv4_mapped_ipv6_addr(const char *ip_str)
Definition: switch_utils.c:494
char * switch_escape_string(const char *in, char *out, switch_size_t outlen)
char * switch_strip_commas(char *in, char *out, switch_size_t len)
char * switch_strerror_r(int errnum, char *buf, switch_size_t buflen)
An abstraction of a data frame.
Definition: switch_frame.h:43
uintptr_t switch_size_t
uint16_t switch_port_t
switch_size_t switch_fp_read_dline(FILE *fd, char **buf, switch_size_t *len)
Definition: switch_utils.c:757
int switch_socket_waitfor(switch_pollfd_t *poll, int ms)
Wait for a socket.
static switch_bool_t switch_string_var_check_const(const char *s)
Definition: switch_utils.h:716
switch_byte_t switch_byte_t uint32_t buflen
struct switch_http_request_s switch_http_request_t
char * switch_url_decode(char *s)
char switch_rfc2833_to_char(int event)
Return the RFC2833 character based on an event id.
int switch_inet_pton(int af, const char *src, void *dst)
Definition: inet_pton.c:74
void switch_split_time(const char *exp, int *hour, int *min, int *sec)
char * switch_replace_char(char *str, char from, char to, switch_bool_t dup)
int switch_isalpha(int c)
void switch_http_free_request(switch_http_request_t *request)
char * switch_util_quote_shell_arg(const char *string)
Quote shell argument.
static char * switch_clean_string(char *s)
Definition: switch_utils.h:741
#define _Post_count_(x)
int switch_ispunct(int c)
unsigned char switch_char_to_rfc2833(char key)
Return the RFC2833 event based on an key character.
char * switch_url_encode_opt(const char *url, char *buf, size_t len, switch_bool_t double_encode)
static char switch_itodtmf(char i)
Definition: switch_utils.h:389
#define _Check_return_
static _Check_return_ int _zstr(_In_opt_z_ const char *s)
Test for NULL or zero length string.
Definition: switch_utils.h:274
char * switch_separate_paren_args(char *str)
const char * switch_dow_int2str(int val)
static char * switch_lc_strdup(const char *it)
Definition: switch_utils.h:804
switch_size_t content_length
static char * switch_uc_strdup(const char *it)
Definition: switch_utils.h:821
switch_status_t
Common return values.
char * switch_string_replace(const char *string, const char *search, const char *replace)
char * switch_uuid_str(char *buf, switch_size_t len)
int switch_tod_cmp(const char *exp, int val)
static int switch_needs_url_encode(const char *s)
char * get_addr(char *buf, switch_size_t len, struct sockaddr *sa, socklen_t salen)
find the char representation of an ip adress
switch_status_t switch_string_match(const char *string, size_t string_len, const char *search, size_t search_len)
Main Library Header.
static switch_bool_t switch_is_file_path(const char *file)
int switch_dow_str2int(const char *exp)
#define SWITCH_DECLARE(type)
switch_bool_t switch_simple_email(const char *to, const char *from, const char *headers, const char *body, const char *file, const char *convert_cmd, const char *convert_ext)
Definition: switch_utils.c:981
switch_status_t switch_frame_dup(switch_frame_t *orig, switch_frame_t **clone)
Definition: switch_utils.c:269
char * switch_escape_char(switch_memory_pool_t *pool, char *in, const char *delim, char esc)
Escape a string by prefixing a list of characters with an escape character.
int switch_number_cmp(const char *exp, int val)
void switch_http_parse_qs(switch_http_request_t *request, char *qs)
char * get_addr6(char *buf, switch_size_t len, struct sockaddr_in6 *sa, socklen_t salen)
static int switch_dtmftoi(char *s)
Definition: switch_utils.h:402
struct apr_pool_t switch_memory_pool_t
int switch_isalnum(int c)
const char * switch_stristr(const char *instr, const char *str)
static switch_bool_t switch_is_digit_string(const char *s)
Definition: switch_utils.h:376
switch_status_t switch_find_local_ip(_Out_opt_bytecapcount_(len) char *buf, _In_ int len, _In_opt_ int *mask, _In_ int family)
find local ip of the box
#define _In_opt_z_
static int switch_false(const char *expr)
Evaluate the falsefullness of a string expression.
Definition: switch_utils.h:482
switch_status_t switch_find_interface_ip(_Out_opt_bytecapcount_(len) char *buf, _In_ int len, _In_opt_ int *mask, _In_ const char *ifname, _In_ int family)
find primary ip of the specified interface
switch_size_t switch_fd_read_dline(int fd, char **buf, switch_size_t *len)
Definition: switch_utils.c:709
switch_bool_t keepalive
switch_poll_t
static uint32_t switch_round_to_step(uint32_t num, uint32_t step)
Definition: switch_utils.h:50
#define switch_assert(expr)
const char * switch_cut_path(const char *in)
Create a pointer to the file name in a given file path eliminating the directory name.
static uint32_t switch_tolower(uint32_t eax)
Definition: switch_utils.h:81
switch_bool_t switch_ast2regex(const char *pat, char *rbuf, size_t len)
char * switch_strip_spaces(char *str, switch_bool_t dup)
static const char * switch_parse_audio_col(switch_audio_col_t col)
switch_event_t * headers
switch_time_t switch_str_time(const char *in)
Converts a string representation of a date into a switch_time_t.
static char * switch_clean_name_string(char *s)
Definition: switch_utils.h:755
int switch_fulldate_cmp(const char *exp, switch_time_t *ts)
switch_size_t bytes_header
static switch_byte_t switch_true_byte(const char *expr)
Definition: switch_utils.h:462
int switch_isupper(int c)
switch_size_t bytes_buffered
int switch_cp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2)
uint8_t switch_payload_t
char * switch_escape_string_pool(const char *in, switch_memory_pool_t *pool)
#define _In_
int switch_wait_socklist(switch_waitlist_t *waitlist, uint32_t len, uint32_t ms)
#define SWITCH_BEGIN_EXTERN_C
Definition: switch.h:42