39 #ifndef SWITCH_UTILS_H
40 #define SWITCH_UTILS_H
47 #define SWITCH_URL_UNSAFE "\r\n #%&+:;<=>?@[\\]^`{|}\""
72 uint32_t ebx = (0x7f7f7f7ful & eax) + 0x05050505ul;
73 ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
74 ebx = ((ebx & ~eax) >> 2 ) & 0x20202020ul;
83 uint32_t ebx = (0x7f7f7f7ful & eax) + 0x25252525ul;
84 ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
85 ebx = ((ebx & ~eax) >> 2) & 0x20202020ul;
95 static inline uint64_t switch_toupper64(uint64_t eax)
97 uint64_t ebx = (0x7f7f7f7f7f7f7f7full & eax) + 0x0505050505050505ull;
98 ebx = (0x7f7f7f7f7f7f7f7full & ebx) + 0x1a1a1a1a1a1a1a1aull;
99 ebx = ((ebx & ~eax) >> 2 ) & 0x2020202020202020ull;
106 static inline uint64_t switch_tolower64(uint64_t eax)
108 uint64_t ebx = (0x7f7f7f7f7f7f7f7full & eax) + 0x2525252525252525ull;
109 ebx = (0x7f7f7f7f7f7f7f7full & ebx) + 0x1a1a1a1a1a1a1a1aull;
110 ebx = ((ebx & ~eax) >> 2) & 0x2020202020202020ull;
126 *b = (uint64_t) switch_toupper64(*b);
154 *b = (uint64_t) switch_tolower64(*b);
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)
276 return !s || *s ==
'\0';
279 #define zstr(x) (_zstr(x) ? 1 : __analysis_assume(x),0)
281 #define zstr(x) _zstr(x)
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')
288 if (
zstr(s) || !strcasecmp(s,
"silence") || !strcasecmp(s,
"indicate_hold")) {
295 #define zset(_a, _b) if (!zstr(_b)) _a = _b
309 if (!*p)
return NULL;
312 const char *a = allowed;
315 if (*p == find)
break;
333 if (!acceptable)
return NULL;
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))
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)
348 #pragma warning(disable:6011)
356 i = strchr(in,
'\\');
358 while (i && *i ==
'\\') {
360 if (*i ==
'\\' || *i ==
'n' || *i ==
's' || *i ==
't' || *i ==
'\'') {
369 #pragma warning(default:6011)
380 if (*s < 48 || *s > 57) {
393 if (i > 9 && i < 14) {
408 if (!(r = atoi(s))) {
409 int l = tolower((
unsigned char)*s);
410 if (l > 96 && l < 101) {
421 case 0:
return 64000;
422 case 3:
return 13200;
425 case 8:
return 64000;
426 case 9:
return 64000;
427 case 18:
return 8000;
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") ||
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)
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") ||
564 #define SWITCH_STATUS_IS_BREAK(x) (x == SWITCH_STATUS_BREAK || x == 730035 || x == 35 || x == SWITCH_STATUS_INTR)
569 #define switch_errno() WSAGetLastError()
573 return errcode == WSAEWOULDBLOCK || errcode == WSAEINPROGRESS || errcode == WSAEINTR;
578 #define switch_errno() errno
582 return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS || errcode == EINTR || errcode == ETIMEDOUT;
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)
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)
624 #define switch_test_flag(obj, flag) ((obj)->flags & flag)
631 #define switch_set_flag(obj, flag) (obj)->flags |= (flag)
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);
648 #define switch_clear_flag_locked(obj, flag) switch_mutex_lock((obj)->flag_mutex); (obj)->flags &= ~(flag); switch_mutex_unlock((obj)->flag_mutex);
655 #define switch_clear_flag(obj, flag) (obj)->flags &= ~(flag)
663 #define switch_copy_flags(dest, src, flags) (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags))
665 #define switch_set_string(_dst, _src) switch_copy_string(_dst, _src, sizeof(_dst))
670 char *p = number, *q;
676 if (!(strchr(p,
'/') || strchr(p,
':') || strchr(p,
'@'))) {
680 while ((q = strrchr(p,
'@')))
683 for (i = 0; i < (int) strlen(warp); i++) {
684 while (p && (q = strchr(p, warp[i])))
696 for (p = s; p && *p; p++) {
707 }
else if (*p !=
'\\') {
721 for (p = s; p && *p; p++) {
727 }
else if (*p !=
'\\') {
744 for (p = s; p && *p; p++) {
745 uint8_t x = (uint8_t) * p;
746 if ((x < 32) && x !=
'\n' && x !=
'\r') {
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 ==
'/') {
763 if ( (p == s) && (*p ==
' ') ) {
781 return nptr ? atoi(nptr) : dft;
789 #define switch_safe_free(it) if (it) {free(it);it=NULL;}
802 #pragma warning(disable:6011)
811 for (p = dup; p && *p; p++) {
828 for (p = dup; p && *p; p++) {
837 #pragma warning(default:6011)
849 char *p, *S = NULL, *Q = NULL;
864 for (p = S; p && *p; p++) {
876 for (p = Q; p && *p; p++) {
903 #define switch_str_nil(s) (s ? s : "")
908 #define switch_yield(ms) switch_sleep(ms);
916 #define switch_time_from_sec(sec) ((switch_time_t)(sec) * 1000000)
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)
995 #define SWITCH_READ_ACCEPTABLE(status) (status == SWITCH_STATUS_SUCCESS || status == SWITCH_STATUS_BREAK || status == SWITCH_STATUS_INUSE)
1006 r = (int32_t)((
double)(w * h * fps * (quality ? quality : 1)) * 0.07) / 1000;
1008 if (!quality) r /= 2;
1020 if (!strcasecmp(bwv,
"auto")) {
1024 if ((bw = (
float) atof(bwv))) {
1025 if (bw < 0)
return 0;
1027 if (strstr(bwv,
"KB")) {
1029 }
else if (strstr(bwv,
"mb")) {
1031 }
else if (strstr(bwv,
"MB")) {
1036 return (int32_t) roundf(bw);
1041 const char hex[] =
"0123456789ABCDEF";
1045 for(p = s; p && *p; p++) {
1046 if (*p ==
'%' && e-p > 1) {
1047 if (strchr(hex, *(p+1)) && strchr(hex, *(p+2))) {
1066 const
char *headers,
1067 const
char *body, const
char *file, const
char *convert_cmd, const
char *convert_ext);
1073 char *space = strdup(file);
1077 *file_portion = NULL;
1078 *params_portion = NULL;
1080 while (*file ==
'{') {
1083 while(*file ==
' ') file++;
1088 *file_portion = strdup((
char *)file);
1090 *params_portion = (
char *)space;
1092 *file_portion = (
char *)space;
1107 while(*file ==
'{') {
1110 while(*file ==
' ') file++;
1115 r = (*file ==
'\\' || *(file + 1) ==
':' || *file ==
'/' || strstr(file,
SWITCH_URL_SEPARATOR));
1126 const char *field = NULL;
1145 field =
"copyright";
1158 #define switch_network_list_add_cidr(_list, _cidr_str, _ok) switch_network_list_add_cidr_token(_list, _cidr_str, _ok, NULL)
1164 #define switch_network_list_validate_ip(_list, _ip) switch_network_list_validate_ip_token(_list, _ip, NULL);
1166 #define switch_test_subnet(_ip, _net, _mask) (_mask ? ((_net & _mask) == (_ip & _mask)) : _net ? _net == _ip : 1)
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)
1197 #define switch_strdup(ptr, s) (void)( (!!(ptr = strdup(s))) || (fprintf(stderr,"ABORT! Malloc failure at: %s:%d", __FILE__, __LINE__),abort(), 0), ptr)
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 )
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)
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);}
1214 #define switch_inet_ntop inet_ntop
static void switch_tolower_max(char *s)
int switch_isdigit(int c)
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)
char * switch_find_end_paren(const char *s, char open, char close)
static void switch_toupper_max(char *s)
static char * switch_sanitize_number(char *number)
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_status_t switch_network_list_create(switch_network_list_t **list, const char *name, switch_bool_t default_type, switch_memory_pool_t *pool)
#define SWITCH_END_EXTERN_C
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)
static int switch_errno_is_break(int errcode)
static void switch_separate_file_params(const char *file, char **file_portion, char **params_portion)
static char * switch_var_clean_string(char *s)
static int switch_string_has_escaped_data(const char *in)
switch_uri_flags
flags to be used with switch_build_uri()
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)
int switch_parse_cidr(const char *string, ip_t *ip, ip_t *mask, uint32_t *bitp)
switch_status_t switch_frame_alloc(switch_frame_t **frame, switch_size_t size)
#define SWITCH_URL_SEPARATOR
switch_priority_t
Priority Indication.
switch_memory_pool_t * pool
void switch_http_dump_request(switch_http_request_t *request)
Representation of an event.
char * switch_amp_encode(char *s, char *buf, switch_size_t len)
switch_status_t switch_frame_free(switch_frame_t **frame)
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)
char * switch_print_host(switch_sockaddr_t *addr, char *buf, switch_size_t len)
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)
switch_size_t switch_fd_read_line(int fd, char *buf, switch_size_t len)
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.
int switch_strcasecmp_any(const char *str,...)
switch_status_t switch_frame_buffer_dup(switch_frame_buffer_t *fb, switch_frame_t *orig, switch_frame_t **clone)
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)
const char * content_type
switch_status_t switch_frame_buffer_destroy(switch_frame_buffer_t **fbP)
#define SWITCH_URL_UNSAFE
static uint32_t switch_known_bitrate(switch_payload_t payload)
switch_bool_t _destroy_headers
static int switch_safe_atoi(const char *nptr, int dft)
Turn a string into a number (default if NULL)
static char * switch_strchr_strict(const char *in, char find, const char *allowed)
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)
static char * switch_safe_strdup(const char *it)
switch_bool_t switch_dow_cmp(const char *exp, int val)
void switch_getcputime(switch_cputime *t)
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)
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.
static switch_bool_t switch_string_var_check(char *s, switch_bool_t disable)
switch_bool_t switch_testv6_subnet(ip_t _ip, ip_t _net, ip_t _mask)
static switch_bool_t switch_is_moh(const char *s)
struct apr_sockaddr_t switch_sockaddr_t
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)
switch_status_t switch_frame_buffer_create(switch_frame_buffer_t **fbP)
int old_switch_tolower(int c)
static int32_t switch_calc_bitrate(int w, int h, int quality, double fps)
int switch_isxdigit(int c)
#define switch_safe_free(it)
Free a pointer and set it to NULL unless it already is NULL.
static uint32_t switch_toupper(uint32_t eax)
int switch_isprint(int c)
unsigned long switch_atoul(const char *nptr)
char * switch_network_ipv4_mapped_ipv6_addr(const char *ip_str)
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.
switch_size_t switch_fp_read_dline(FILE *fd, char **buf, switch_size_t *len)
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)
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)
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)
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)
static _Check_return_ int _zstr(_In_opt_z_ const char *s)
Test for NULL or zero length string.
char * switch_separate_paren_args(char *str)
const char * switch_dow_int2str(int val)
static char * switch_lc_strdup(const char *it)
switch_size_t content_length
static char * switch_uc_strdup(const char *it)
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)
static switch_bool_t switch_is_file_path(const char *file)
int switch_dow_str2int(const char *exp)
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)
switch_status_t switch_frame_dup(switch_frame_t *orig, switch_frame_t **clone)
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)
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)
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
static int switch_false(const char *expr)
Evaluate the falsefullness of a string expression.
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)
static uint32_t switch_round_to_step(uint32_t num, uint32_t step)
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)
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_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)
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)
int switch_isupper(int c)
switch_size_t bytes_buffered
int switch_cp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2)
char * switch_escape_string_pool(const char *in, switch_memory_pool_t *pool)
int switch_wait_socklist(switch_waitlist_t *waitlist, uint32_t len, uint32_t ms)
#define SWITCH_BEGIN_EXTERN_C