forked from signalwire/freeswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch_utils.h
More file actions
1576 lines (1272 loc) · 43.9 KB
/
Copy pathswitch_utils.h
File metadata and controls
1576 lines (1272 loc) · 43.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <[email protected]>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <[email protected]>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <[email protected]>
* Seven Du <[email protected]>
*
*
* switch_utils.h -- Compatability and Helper Code
*
*/
/*! \file switch_utils.h
\brief Compatability and Helper Code
Just a miscelaneaous set of general utility/helper functions.
*/
#ifndef SWITCH_UTILS_H
#define SWITCH_UTILS_H
#include <switch.h>
#include <math.h>
SWITCH_BEGIN_EXTERN_C
#define SWITCH_URL_UNSAFE "\r\n #%&+:;<=>?@[\\]^`{|}\""
#define MAX_NETWORK_PORTS 10
struct switch_network_port_range {
int port;
int ports[MAX_NETWORK_PORTS];
int min_port;
int max_port;
};
typedef struct switch_network_port_range switch_network_port_range_t;
typedef switch_network_port_range_t* switch_network_port_range_p;
static inline char *switch_get_hex_bytes(switch_byte_t *buf, switch_size_t datalen, char *new_buf, switch_size_t new_datalen)
{
switch_byte_t *p, *e;
char *pp, *ee;
e = buf + datalen;
ee = new_buf + new_datalen;
pp = new_buf;
for (p = buf; p < e && pp < ee - 4; p++) {
if (snprintf(pp, 4, "%.2x ", (int)*p) < 0) {
return NULL;
}
pp += 3;
}
*(pp-1) = '\0';
return new_buf;
}
static inline uint32_t switch_round_to_step(uint32_t num, uint32_t step)
{
uint32_t r;
uint32_t x;
if (!num) return 0;
r = (num % step);
x = num - r;
if (r > step / 2) {
x += step;
}
return x;
}
/* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
http://www.azillionmonkeys.com/qed/asmexample.html
*/
static inline uint32_t switch_toupper(uint32_t eax)
{
uint32_t ebx = (0x7f7f7f7ful & eax) + 0x05050505ul;
ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
ebx = ((ebx & ~eax) >> 2 ) & 0x20202020ul;
return eax - ebx;
}
/* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
http://www.azillionmonkeys.com/qed/asmexample.html
*/
static inline uint32_t switch_tolower(uint32_t eax)
{
uint32_t ebx = (0x7f7f7f7ful & eax) + 0x25252525ul;
ebx = (0x7f7f7f7ful & ebx) + 0x1a1a1a1aul;
ebx = ((ebx & ~eax) >> 2) & 0x20202020ul;
return eax + ebx;
}
#ifdef FS_64BIT
/* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
http://www.azillionmonkeys.com/qed/asmexample.html
*/
static inline uint64_t switch_toupper64(uint64_t eax)
{
uint64_t ebx = (0x7f7f7f7f7f7f7f7full & eax) + 0x0505050505050505ull;
ebx = (0x7f7f7f7f7f7f7f7full & ebx) + 0x1a1a1a1a1a1a1a1aull;
ebx = ((ebx & ~eax) >> 2 ) & 0x2020202020202020ull;
return eax - ebx;
}
/* https://code.google.com/p/stringencoders/wiki/PerformanceAscii
http://www.azillionmonkeys.com/qed/asmexample.html
*/
static inline uint64_t switch_tolower64(uint64_t eax)
{
uint64_t ebx = (0x7f7f7f7f7f7f7f7full & eax) + 0x2525252525252525ull;
ebx = (0x7f7f7f7f7f7f7f7full & ebx) + 0x1a1a1a1a1a1a1a1aull;
ebx = ((ebx & ~eax) >> 2) & 0x2020202020202020ull;
return eax + ebx;
}
static inline void switch_toupper_max(char *s)
{
uint64_t *b,*p;
char *c;
size_t l;
l = strlen(s);
p = (uint64_t *) s;
while (l > 8) {
b = p;
*b = (uint64_t) switch_toupper64(*b);
b++;
p++;
l -= 8;
}
c = (char *)p;
while(l > 0) {
*c = (char) switch_toupper(*c);
c++;
l--;
}
}
static inline void switch_tolower_max(char *s)
{
uint64_t *b,*p;
char *c;
size_t l;
l = strlen(s);
p = (uint64_t *) s;
while (l > 8) {
b = p;
*b = (uint64_t) switch_tolower64(*b);
b++;
p++;
l -= 8;
}
c = (char *)p;
while(l > 0) {
*c = (char) switch_tolower(*c);
c++;
l--;
}
}
#else
static inline void switch_toupper_max(char *s)
{
uint32_t *b,*p;
char *c;
size_t l;
l = strlen(s);
p = (uint32_t *) s;
while (l > 4) {
b = p;
*b = (uint32_t) switch_toupper(*b);
b++;
p++;
l -= 4;
}
c = (char *)p;
while(l > 0) {
*c = (char) switch_toupper(*c);
c++;
l--;
}
}
static inline void switch_tolower_max(char *s)
{
uint32_t *b,*p;
char *c;
size_t l;
l = strlen(s);
p = (uint32_t *) s;
while (l > 4) {
b = p;
*b = (uint32_t) switch_tolower(*b);
b++;
p++;
l -= 4;
}
c = (char *)p;
while(l > 0) {
*c = (char) switch_tolower(*c);
c++;
l--;
}
}
#endif
SWITCH_DECLARE(int) old_switch_toupper(int c);
SWITCH_DECLARE(int) old_switch_tolower(int c);
SWITCH_DECLARE(int) switch_isalnum(int c);
SWITCH_DECLARE(int) switch_isalpha(int c);
SWITCH_DECLARE(int) switch_iscntrl(int c);
SWITCH_DECLARE(int) switch_isdigit(int c);
SWITCH_DECLARE(int) switch_isgraph(int c);
SWITCH_DECLARE(int) switch_islower(int c);
SWITCH_DECLARE(int) switch_isprint(int c);
SWITCH_DECLARE(int) switch_ispunct(int c);
SWITCH_DECLARE(int) switch_isspace(int c);
SWITCH_DECLARE(int) switch_isupper(int c);
SWITCH_DECLARE(int) switch_isxdigit(int c);
typedef union{
uint32_t v4;
struct in6_addr v6;
} ip_t;
SWITCH_DECLARE(switch_bool_t) switch_testv6_subnet(ip_t _ip, ip_t _net, ip_t _mask);
SWITCH_DECLARE(char *) switch_print_host(switch_sockaddr_t *addr, char *buf, switch_size_t len);
#define switch_goto_status(_status, _label) status = _status; goto _label
#define switch_goto_int(_n, _i, _label) _n = _i; goto _label
#define switch_samples_per_packet(rate, interval) ((uint32_t)((float)rate / (1000.0f / (float)interval)))
#define SWITCH_SMAX 32767
#define SWITCH_SMIN -32768
#define switch_normalize_to_16bit(n) if (n > SWITCH_SMAX) n = SWITCH_SMAX; else if (n < SWITCH_SMIN) n = SWITCH_SMIN;
#define switch_codec2str(codec,buf,len) snprintf(buf, len, "%s@%uh@%ui", \
codec->implementation->iananame, \
codec->implementation->samples_per_second, \
codec->implementation->microseconds_per_packet / 1000)
/*!
\brief Test for NULL or zero length string
\param s the string to test
\return true value if the string is NULL or zero length
*/
_Check_return_ static inline int _zstr(_In_opt_z_ const char *s)
{
if (!s) return 1;
if (*s == '\0') return 1;
return 0;
}
#ifdef _PREFAST_
#define zstr(x) (_zstr(x) ? 1 : __analysis_assume(x),0)
#else
#define zstr(x) _zstr(x)
#endif
#define switch_strlen_zero(x) zstr(x)
#define switch_strlen_zero_buf(x) zstr_buf(x)
#define zstr_buf(s) (*(s) == '\0')
static inline switch_bool_t switch_is_moh(const char *s)
{
if (zstr(s) || !strcasecmp(s, "silence") || !strcasecmp(s, "indicate_hold")) {
return SWITCH_FALSE;
}
return SWITCH_TRUE;
}
#define zset(_a, _b) if (!zstr(_b)) _a = _b
/* find a character (find) in a string (in) and return a pointer to that point in the string where the character was found
using the array (allowed) as allowed non-matching characters, when (allowed) is NULL, behaviour should be identical to strchr()
*/
static inline char *switch_strchr_strict(const char *in, char find, const char *allowed)
{
const char *p;
switch_assert(in);
p = in;
if (!*p) return NULL;
while(p && *p) {
const char *a = allowed;
int acceptable = 0;
if (*p == find) break;
if (!a) {
acceptable = 1;
} else {
while(a && *a) {
if (*p == *a) {
acceptable = 1;
break;
}
a++;
}
}
if (!acceptable) return NULL;
p++;
}
return (char *) p;
}
#define switch_arraylen(_a) (sizeof(_a) / sizeof(_a[0]))
#define switch_split(_data, _delim, _array) switch_separate_string(_data, _delim, _array, switch_arraylen(_array))
#define switch_is_valid_rate(_tmp) (_tmp == 8000 || _tmp == 12000 || _tmp == 16000 || _tmp == 24000 || _tmp == 32000 || _tmp == 11025 || _tmp == 22050 || _tmp == 44100 || _tmp == 48000)
#ifdef _MSC_VER
#pragma warning(disable:6011)
#endif
static inline int switch_string_has_escaped_data(const char *in)
{
const char *i;
switch_assert(in);
i = strchr(in, '\\');
while (i && *i == '\\') {
i++;
if (*i == '\\' || *i == 'n' || *i == 's' || *i == 't' || *i == '\'') {
return 1;
}
i = strchr(i, '\\');
}
return 0;
}
#ifdef _MSC_VER
#pragma warning(default:6011)
#endif
SWITCH_DECLARE(switch_status_t) switch_b64_encode(unsigned char *in, switch_size_t ilen, unsigned char *out, switch_size_t olen);
SWITCH_DECLARE(switch_size_t) switch_b64_decode(const char *in, char *out, switch_size_t olen);
SWITCH_DECLARE(char *) switch_amp_encode(char *s, char *buf, switch_size_t len);
static inline char *switch_print_bits(const unsigned char *byte, char *buf, switch_size_t buflen)
{
int i;
switch_size_t j = 0, k = 0, l = 0;
while(k < buflen) {
l = 0;
for (i = 7; i >= 0; i--) {
buf[j++] = (*byte & (1 << i)) ? '1' : '0';
if (++l % 4 == 0) {
buf[j++] = ' ';
}
}
k++;
byte++;
}
if (buf[j-1] == ' ') j--;
buf[j++] = '\0';
return buf;
}
static inline switch_bool_t switch_is_digit_string(const char *s)
{
while (s && *s) {
if (*s < 48 || *s > 57) {
return SWITCH_FALSE;
}
s++;
}
return SWITCH_TRUE;
}
static inline char switch_itodtmf(char i)
{
char r = i;
if (i > 9 && i < 14) {
r += 55;
} else {
r += 48;
}
return r;
}
static inline int switch_dtmftoi(char *s)
{
int r;
switch_assert(s);
if (!(r = atoi(s))) {
int l = tolower((unsigned char)*s);
if (l > 96 && l < 101) {
r = l - 87;
}
}
return r;
}
static inline uint32_t switch_known_bitrate(switch_payload_t payload)
{
switch(payload) {
case 0: /* PCMU */ return 64000;
case 3: /* GSM */ return 13200;
case 4: /* G723 */ return 6300;
case 7: /* LPC */ return 2400;
case 8: /* PCMA */ return 64000;
case 9: /* G722 */ return 64000;
case 18: /* G729 */ return 8000;
default: break;
}
return 0;
}
SWITCH_DECLARE(switch_size_t) switch_fd_read_line(int fd, char *buf, switch_size_t len);
SWITCH_DECLARE(switch_size_t) switch_fd_read_dline(int fd, char **buf, switch_size_t *len);
SWITCH_DECLARE(switch_size_t) switch_fp_read_dline(FILE *fd, char **buf, switch_size_t *len);
SWITCH_DECLARE(switch_status_t) switch_frame_alloc(switch_frame_t **frame, switch_size_t size);
SWITCH_DECLARE(switch_status_t) switch_frame_dup(switch_frame_t *orig, switch_frame_t **clone);
SWITCH_DECLARE(switch_status_t) switch_frame_free(switch_frame_t **frame);
SWITCH_DECLARE(switch_bool_t) switch_is_number(const char *str);
SWITCH_DECLARE(switch_bool_t) switch_is_leading_number(const char *str);
SWITCH_DECLARE(char *) switch_find_parameter(const char *str, const char *param, switch_memory_pool_t *pool);
/*!
\brief Evaluate the truthfullness of a string expression
\param expr a string expression
\return true or false
*/
static inline switch_bool_t switch_true(const char *expr)
{
return ((expr && ( !strcasecmp(expr, "yes") ||
!strcasecmp(expr, "on") ||
!strcasecmp(expr, "true") ||
!strcasecmp(expr, "t") ||
!strcasecmp(expr, "enabled") ||
!strcasecmp(expr, "active") ||
!strcasecmp(expr, "allow") ||
(switch_is_number(expr) && atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
}
static inline switch_byte_t switch_true_byte(const char *expr)
{
return (switch_byte_t)switch_true(expr);
}
#define switch_true_buf(expr)\
((( !strcasecmp(expr, "yes") ||\
!strcasecmp(expr, "on") ||\
!strcasecmp(expr, "true") ||\
!strcasecmp(expr, "t") ||\
!strcasecmp(expr, "enabled") ||\
!strcasecmp(expr, "active") ||\
!strcasecmp(expr, "allow") ||\
(switch_is_number(expr) && atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE)
/*!
\brief Evaluate the falsefullness of a string expression
\param expr a string expression
\return true or false
*/
static inline int switch_false(const char *expr)
{
return ((expr && ( !strcasecmp(expr, "no") ||
!strcasecmp(expr, "off") ||
!strcasecmp(expr, "false") ||
!strcasecmp(expr, "f") ||
!strcasecmp(expr, "disabled") ||
!strcasecmp(expr, "inactive") ||
!strcasecmp(expr, "disallow") ||
(switch_is_number(expr) && !atoi(expr)))) ? SWITCH_TRUE : SWITCH_FALSE);
}
SWITCH_DECLARE(switch_status_t) switch_resolve_host(const char *host, char *buf, size_t buflen);
/*!
\brief find local ip of the box
\param buf the buffer to write the ip address found into
\param len the length of the buf
\param mask the CIDR found (AF_INET only)
\param family the address family to return (AF_INET or AF_INET6)
\return SWITCH_STATUS_SUCCESSS for success, otherwise failure
*/
SWITCH_DECLARE(switch_status_t) switch_find_local_ip(_Out_opt_bytecapcount_(len)
char *buf, _In_ int len, _In_opt_ int *mask, _In_ int family);
/*!
\brief find primary ip of the specified interface
\param buf the buffer to write the ip address found into
\param len the length of the buf
\param mask the CIDR found (AF_INET only)
\param ifname interface name to check
\param family the address family to return (AF_INET or AF_INET6)
\return SWITCH_STATUS_SUCCESSS for success, otherwise failure
*/
SWITCH_DECLARE(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);
/*!
\brief find the char representation of an ip adress
\param buf the buffer to write the ip adress found into
\param len the length of the buf
\param sa the struct sockaddr * to get the adress from
\param salen the length of sa
\return the ip adress string
*/
SWITCH_DECLARE(char *) get_addr(char *buf, switch_size_t len, struct sockaddr *sa, socklen_t salen);
SWITCH_DECLARE(char *) get_addr6(char *buf, switch_size_t len, struct sockaddr_in6 *sa, socklen_t salen);
SWITCH_DECLARE(int) get_addr_int(switch_sockaddr_t *sa);
SWITCH_DECLARE(int) switch_cmp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2);
SWITCH_DECLARE(int) switch_cp_addr(switch_sockaddr_t *sa1, switch_sockaddr_t *sa2);
/*!
\brief get the port number of an ip address
\param sa the struct sockaddr * to get the port from
\return the ip adress string
*/
SWITCH_DECLARE(unsigned short) get_port(struct sockaddr *sa);
/*!
\brief flags to be used with switch_build_uri()
*/
enum switch_uri_flags {
SWITCH_URI_NUMERIC_HOST = 1,
SWITCH_URI_NUMERIC_PORT = 2,
SWITCH_URI_NO_SCOPE = 4
};
/*!
\brief build a URI string from components
\param uri output string
\param size maximum size of output string (including trailing null)
\param scheme URI scheme
\param user user part or null if none
\param sa host address
\param flags logical OR-ed combination of flags from \ref switch_uri_flags
\return number of characters printed (not including the trailing null)
*/
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);
#define SWITCH_STATUS_IS_BREAK(x) (x == SWITCH_STATUS_BREAK || x == 730035 || x == 35 || x == SWITCH_STATUS_INTR)
#ifdef _MSC_VER
#define switch_errno() WSAGetLastError()
static inline int switch_errno_is_break(int errcode)
{
return errcode == WSAEWOULDBLOCK || errcode == WSAEINPROGRESS || errcode == WSAEINTR;
}
#else
#define switch_errno() errno
static inline int switch_errno_is_break(int errcode)
{
return errcode == EAGAIN || errcode == EWOULDBLOCK || errcode == EINPROGRESS || errcode == EINTR || errcode == ETIMEDOUT;
}
#endif
/*!
\brief Return a printable name of a switch_priority_t
\param priority the priority to get the name of
\return the printable form of the priority
*/
SWITCH_DECLARE(const char *) switch_priority_name(switch_priority_t priority);
/*!
\brief Return the RFC2833 character based on an event id
\param event the event id to convert
\return the character represented by the event or null for an invalid event
*/
SWITCH_DECLARE(char) switch_rfc2833_to_char(int event);
/*!
\brief Return the RFC2833 event based on an key character
\param key the charecter to encode
\return the event id for the specified character or -1 on an invalid input
*/
SWITCH_DECLARE(unsigned char) switch_char_to_rfc2833(char key);
/*!
\brief determine if a character is a valid DTMF key
\param key the key to test
\return TRUE or FALSE
*/
#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)
#define end_of(_s) *(*_s == '\0' ? _s : _s + strlen(_s) - 1)
#define end_of_p(_s) (*_s == '\0' ? _s : _s + strlen(_s) - 1)
/*!
\brief Test for the existance of a flag on an arbitary object
\param obj the object to test
\param flag the or'd list of flags to test
\return true value if the object has the flags defined
*/
#define switch_test_flag(obj, flag) ((obj)->flags & flag)
/*!
\brief Set a flag on an arbitrary object
\param obj the object to set the flags on
\param flag the or'd list of flags to set
*/
#define switch_set_flag(obj, flag) (obj)->flags |= (flag)
/*!
\brief Set a flag on an arbitrary object while locked
\param obj the object to set the flags on
\param flag the or'd list of flags to set
*/
#define switch_set_flag_locked(obj, flag) assert((obj)->flag_mutex != NULL); \
switch_mutex_lock((obj)->flag_mutex); \
(obj)->flags |= (flag);\
switch_mutex_unlock((obj)->flag_mutex);
/*!
\brief Clear a flag on an arbitrary object
\param obj the object to test
\param flag the or'd list of flags to clear
*/
#define switch_clear_flag_locked(obj, flag) switch_mutex_lock((obj)->flag_mutex); (obj)->flags &= ~(flag); switch_mutex_unlock((obj)->flag_mutex);
/*!
\brief Clear a flag on an arbitrary object while locked
\param obj the object to test
\param flag the or'd list of flags to clear
*/
#define switch_clear_flag(obj, flag) (obj)->flags &= ~(flag)
/*!
\brief Copy flags from one arbitrary object to another
\param dest the object to copy the flags to
\param src the object to copy the flags from
\param flags the flags to copy
*/
#define switch_copy_flags(dest, src, flags) (dest)->flags &= ~(flags); (dest)->flags |= ((src)->flags & (flags))
#define switch_set_string(_dst, _src) switch_copy_string(_dst, _src, sizeof(_dst))
static inline char *switch_sanitize_number(char *number)
{
char *p = number, *q;
char warp[] = "/:";
int i;
switch_assert(number);
if (!(strchr(p, '/') || strchr(p, ':') || strchr(p, '@'))) {
return number;
}
while ((q = strrchr(p, '@')))
*q = '\0';
for (i = 0; i < (int) strlen(warp); i++) {
while (p && (q = strchr(p, warp[i])))
p = q + 1;
}
return p;
}
static inline switch_bool_t switch_string_var_check(char *s, switch_bool_t disable)
{
char *p;
char *dol = NULL;
for (p = s; p && *p; p++) {
if (*p == '$') {
dol = p;
} else if (dol) {
if (*p == '{') {
if (disable) {
*dol = '%';
dol = NULL;
} else {
return SWITCH_TRUE;
}
} else if (*p != '\\') {
dol = NULL;
}
}
}
return SWITCH_FALSE;
}
static inline switch_bool_t switch_string_var_check_const(const char *s)
{
const char *p;
int dol = 0;
for (p = s; p && *p; p++) {
if (*p == '$') {
dol = 1;
} else if (dol) {
if (*p == '{') {
return SWITCH_TRUE;
} else if (*p != '\\') {
dol = 0;
}
}
}
return SWITCH_FALSE;
}
static inline char *switch_var_clean_string(char *s)
{
switch_string_var_check(s, SWITCH_TRUE);
return s;
}
static inline char *switch_clean_string(char *s)
{
char *p;
for (p = s; p && *p; p++) {
uint8_t x = (uint8_t) * p;
if ((x < 32) && x != '\n' && x != '\r') {
*p = ' ';
}
}
return s;
}
static inline char *switch_clean_name_string(char *s)
{
char *p;
for (p = s; p && *p; p++) {
uint8_t x = (uint8_t) * p;
if ((x < 32) || x == '\'' || x == '"' || x == '<' || x == '>' || x == '\\' || x == ':' || x == '@' || x == '/') {
*p = ' ';
}
if ( (p == s) && (*p == ' ') ) {
s++;
}
}
return s;
}
/*!
\brief Turn a string into a number (default if NULL)
\param nptr the string
\param dft the default
\return the number version of the string or the default
*/
static inline int switch_safe_atoi(const char *nptr, int dft)
{
return nptr ? atoi(nptr) : dft;
}
/*!
\brief Free a pointer and set it to NULL unless it already is NULL
\param it the pointer
*/
#define switch_safe_free(it) if (it) {free(it);it=NULL;}
static inline char *switch_safe_strdup(const char *it)
{
if (it) {
return strdup(it);
}
return NULL;
}
#ifdef _MSC_VER
#pragma warning(disable:6011)
#endif
static inline char *switch_lc_strdup(const char *it)
{
char *dup;
char *p;
if (it) {
dup = strdup(it);
for (p = dup; p && *p; p++) {
*p = (char) switch_tolower(*p);
}
return dup;
}
return NULL;
}
static inline char *switch_uc_strdup(const char *it)
{
char *dup;
char *p;
if (it) {
dup = strdup(it);
for (p = dup; p && *p; p++) {
*p = (char) switch_toupper(*p);
}
return dup;
}
return NULL;
}
#ifdef _MSC_VER
#pragma warning(default:6011)
#endif
/*!
\brief Test if one string is inside another with extra case checking
\param s the inner string
\param q the big string
\return SWITCH_TRUE or SWITCH_FALSE
*/
static inline switch_bool_t switch_strstr(char *s, char *q)
{
char *p, *S = NULL, *Q = NULL;
switch_bool_t tf = SWITCH_FALSE;
if (!s || !q) {
return SWITCH_FALSE;
}
if (strstr(s, q)) {
return SWITCH_TRUE;
}
S = strdup(s);
switch_assert(S != NULL);
for (p = S; p && *p; p++) {
*p = (char) switch_toupper(*p);
}
if (strstr(S, q)) {
tf = SWITCH_TRUE;
goto done;
}
Q = strdup(q);
switch_assert(Q != NULL);
for (p = Q; p && *p; p++) {
*p = (char) switch_toupper(*p);
}
if (strstr(s, Q)) {
tf = SWITCH_TRUE;
goto done;
}
if (strstr(S, Q)) {
tf = SWITCH_TRUE;
goto done;
}
done:
switch_safe_free(S);
switch_safe_free(Q);
return tf;
}
/*!
\brief Make a null string a blank string instead
\param s the string to test
\return the original string or blank string.
*/
#define switch_str_nil(s) (s ? s : "")
/*!
\brief Wait a desired number of microseconds and yield the CPU
*/
#define switch_yield(ms) switch_sleep(ms);
/*!
\brief Converts a string representation of a date into a switch_time_t
\param in the string
\return the epoch time in usec
*/
SWITCH_DECLARE(switch_time_t) switch_str_time(const char *in);
#define switch_time_from_sec(sec) ((switch_time_t)(sec) * 1000000)
/*!
\brief Declares a function designed to set a dynamic global string
\param fname the function name to declare
\param vname the name of the global pointer to modify with the new function
*/
#define SWITCH_DECLARE_GLOBAL_STRING_FUNC(fname, vname) static void fname(const char *string) { if (!string) return;\
if (vname) {free(vname); vname = NULL;}vname = strdup(string);} static void fname(const char *string)
/*!
\brief Separate a string into an array based on a character delimiter
\param buf the string to parse
\param delim the character delimiter
\param array the array to split the values into
\param arraylen the max number of elements in the array
\return the number of elements added to the array
*/
SWITCH_DECLARE(unsigned int) switch_separate_string(_In_ char *buf, char delim, _Post_count_(return) char **array, unsigned int arraylen);
SWITCH_DECLARE(unsigned int) switch_separate_string_string(char *buf, char *delim, _Post_count_(return) char **array, unsigned int arraylen);
SWITCH_DECLARE(char *) switch_strip_spaces(char *str, switch_bool_t dup);
SWITCH_DECLARE(char *) switch_strip_whitespace(const char *str);
SWITCH_DECLARE(char *) switch_strip_commas(char *in, char *out, switch_size_t len);