forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdp_attr.c
5251 lines (4533 loc) · 188 KB
/
sdp_attr.c
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include "plstr.h"
#include "sdp_os_defs.h"
#include "sipcc_sdp.h"
#include "sdp_private.h"
#include "sdp_base64.h"
#include "sdp_log.h"
static const char* logTag = "sdp_attr";
/*
* Macro for sdp_build_attr_fmtp
* Adds name-value pair where value is char*
*/
#define FMTP_BUILD_STRING(condition, name, value) \
if ((condition)) { \
sdp_append_name_and_string(fs, (name), (value), semicolon); \
semicolon = TRUE; \
}
/*
* Macro for sdp_build_attr_fmtp
* Adds name-value pair where value is unsigned
*/
#define FMTP_BUILD_UNSIGNED(condition, name, value) \
if ((condition)) { \
sdp_append_name_and_unsigned(fs, (name), (value), semicolon); \
semicolon = TRUE; \
}
/*
* Macro for sdp_build_attr_fmtp
* Adds flag string on condition
*/
#define FMTP_BUILD_FLAG(condition, name) \
if ((condition)) { \
if (semicolon) { \
flex_string_append(fs, ";"); \
} \
flex_string_append(fs, name); \
semicolon = TRUE; \
}
static int find_token_enum(const char *attr_name,
sdp_t *sdp_p,
const char **ptr,
const sdp_namearray_t *types,
int type_count,
int unknown_value)
{
sdp_result_e result = SDP_SUCCESS;
char tmp[SDP_MAX_STRING_LEN+1];
int i;
*ptr = sdp_getnextstrtok(*ptr, tmp, sizeof(tmp), " \t", &result);
if (result != SDP_SUCCESS) {
sdp_parse_error(sdp_p,
"%s Warning: problem parsing %s", sdp_p->debug_str, attr_name);
sdp_p->conf_p->num_invalid_param++;
return -1;
}
for (i=0; i < type_count; i++) {
if (!cpr_strncasecmp(tmp, types[i].name, types[i].strlen)) {
return i;
}
}
return unknown_value;
}
/*
* Helper function for adding nv-pair where value is string.
*/
static void sdp_append_name_and_string(flex_string *fs,
const char *name,
const char *value,
tinybool semicolon)
{
flex_string_sprintf(fs, "%s%s=%s",
semicolon ? ";" : "",
name,
value);
}
/*
* Helper function for adding nv-pair where value is unsigned.
*/
static void sdp_append_name_and_unsigned(flex_string *fs,
const char *name,
unsigned int value,
tinybool semicolon)
{
flex_string_sprintf(fs, "%s%s=%u",
semicolon ? ";" : "",
name,
value);
}
/* Function: sdp_parse_attribute
* Description: Figure out the type of attribute and call the appropriate
* parsing routine. If parsing errors are encountered,
* warnings will be printed and the attribute will be ignored.
* Unrecognized/invalid attributes do not cause overall parsing
* errors. All errors detected are noted as warnings.
* Parameters: sdp_p The SDP handle returned by sdp_init_description.
* level The level to check for the attribute.
* ptr Pointer to the attribute string to parse.
*/
sdp_result_e sdp_parse_attribute (sdp_t *sdp_p, uint16_t level, const char *ptr)
{
int i;
uint8_t xcpar_flag = FALSE;
sdp_result_e result;
sdp_mca_t *mca_p=NULL;
sdp_attr_t *attr_p;
sdp_attr_t *next_attr_p;
sdp_attr_t *prev_attr_p = NULL;
char tmp[SDP_MAX_STRING_LEN];
/* Validate the level */
if (level != SDP_SESSION_LEVEL) {
mca_p = sdp_find_media_level(sdp_p, level);
if (mca_p == NULL) {
return (SDP_FAILURE);
}
}
/* Find the attribute type. */
ptr = sdp_getnextstrtok(ptr, tmp, sizeof(tmp), ": \t", &result);
if (ptr == NULL) {
sdp_parse_error(sdp_p,
"%s No attribute type specified, parse failed.", sdp_p->debug_str);
sdp_p->conf_p->num_invalid_param++;
return (SDP_INVALID_PARAMETER);
}
if (ptr[0] == ':') {
/* Skip the ':' char for parsing attribute parameters. */
ptr++;
}
if (result != SDP_SUCCESS) {
sdp_parse_error(sdp_p,
"%s No attribute type specified, parse failed.", sdp_p->debug_str);
sdp_p->conf_p->num_invalid_param++;
return (SDP_INVALID_PARAMETER);
}
attr_p = (sdp_attr_t *)SDP_MALLOC(sizeof(sdp_attr_t));
if (attr_p == NULL) {
sdp_p->conf_p->num_no_resource++;
return (SDP_NO_RESOURCE);
}
attr_p->line_number = sdp_p->parse_line;
attr_p->type = SDP_ATTR_INVALID;
attr_p->next_p = NULL;
for (i=0; i < SDP_MAX_ATTR_TYPES; i++) {
if (cpr_strncasecmp(tmp, sdp_attr[i].name, sdp_attr[i].strlen) == 0) {
attr_p->type = (sdp_attr_e)i;
break;
}
}
if (attr_p->type == SDP_ATTR_INVALID) {
sdp_parse_error(sdp_p,
"%s Warning: Unrecognized attribute (%s) ",
sdp_p->debug_str, tmp);
sdp_free_attr(attr_p);
return (SDP_SUCCESS);
}
/* If this is an X-cpar or cpar attribute, set the flag. The attribute
* type will be changed by the parse. */
if ((attr_p->type == SDP_ATTR_X_CPAR) ||
(attr_p->type == SDP_ATTR_CPAR)) {
xcpar_flag = TRUE;
}
/* Parse the attribute. */
result = sdp_attr[attr_p->type].parse_func(sdp_p, attr_p, ptr);
if (result != SDP_SUCCESS) {
sdp_free_attr(attr_p);
/* Return success so the parse won't fail. We don't want to
* fail on errors with attributes but just ignore them.
*/
return (SDP_SUCCESS);
}
/* If this was an X-cpar/cpar attribute, it was hooked into the X-cap/cdsc
* structure, so we're finished.
*/
if (xcpar_flag == TRUE) {
return (result);
}
/* Add the attribute in the appropriate place. */
if (level == SDP_SESSION_LEVEL) {
for (next_attr_p = sdp_p->sess_attrs_p; next_attr_p != NULL;
prev_attr_p = next_attr_p,
next_attr_p = next_attr_p->next_p) {
; /* Empty for */
}
if (prev_attr_p == NULL) {
sdp_p->sess_attrs_p = attr_p;
} else {
prev_attr_p->next_p = attr_p;
}
} else {
for (next_attr_p = mca_p->media_attrs_p; next_attr_p != NULL;
prev_attr_p = next_attr_p,
next_attr_p = next_attr_p->next_p) {
; /* Empty for */
}
if (prev_attr_p == NULL) {
mca_p->media_attrs_p = attr_p;
} else {
prev_attr_p->next_p = attr_p;
}
}
return (result);
}
/* Build all of the attributes defined for the specified level. */
sdp_result_e sdp_build_attribute (sdp_t *sdp_p, uint16_t level, flex_string *fs)
{
sdp_attr_t *attr_p;
sdp_mca_t *mca_p=NULL;
sdp_result_e result;
if (level == SDP_SESSION_LEVEL) {
attr_p = sdp_p->sess_attrs_p;
} else {
mca_p = sdp_find_media_level(sdp_p, level);
if (mca_p == NULL) {
return (SDP_FAILURE);
}
attr_p = mca_p->media_attrs_p;
}
/* Re-initialize the current capability number for this new level. */
sdp_p->cur_cap_num = 1;
/* Build all of the attributes for this level. Note that if there
* is a problem building an attribute, we don't fail but just ignore it.*/
while (attr_p != NULL) {
if (attr_p->type >= SDP_MAX_ATTR_TYPES) {
if (sdp_p->debug_flag[SDP_DEBUG_WARNINGS]) {
SDPLogDebug(logTag, "%s Invalid attribute type to build (%u)",
sdp_p->debug_str, (unsigned)attr_p->type);
}
} else {
result = sdp_attr[attr_p->type].build_func(sdp_p, attr_p, fs);
if (result != SDP_SUCCESS) {
SDPLogError(logTag, "%s error building attribute %d", __FUNCTION__, result);
return result;
}
if (sdp_p->debug_flag[SDP_DEBUG_TRACE]) {
SDP_PRINT("%s Built a=%s attribute line", sdp_p->debug_str,
sdp_get_attr_name(attr_p->type));
}
}
attr_p = attr_p->next_p;
}
return SDP_SUCCESS;
}
sdp_result_e sdp_parse_attr_simple_string (sdp_t *sdp_p, sdp_attr_t *attr_p,
const char *ptr)
{
sdp_result_e result;
ptr = sdp_getnextstrtok(ptr, attr_p->attr.string_val,
sizeof(attr_p->attr.string_val), " \t", &result);
if (result != SDP_SUCCESS) {
sdp_parse_error(sdp_p,
"%s Warning: No string token found for %s attribute",
sdp_p->debug_str, sdp_get_attr_name(attr_p->type));
sdp_p->conf_p->num_invalid_param++;
return (SDP_INVALID_PARAMETER);
} else {
if (sdp_p->debug_flag[SDP_DEBUG_TRACE]) {
SDP_PRINT("%s Parsed a=%s, %s", sdp_p->debug_str,
sdp_get_attr_name(attr_p->type),
attr_p->attr.string_val);
}
return (SDP_SUCCESS);
}
}
sdp_result_e sdp_build_attr_simple_string (sdp_t *sdp_p, sdp_attr_t *attr_p,
flex_string *fs)
{
flex_string_sprintf(fs, "a=%s:%s\r\n", sdp_attr[attr_p->type].name,
attr_p->attr.string_val);
return SDP_SUCCESS;
}
sdp_result_e sdp_parse_attr_simple_u32 (sdp_t *sdp_p, sdp_attr_t *attr_p,
const char *ptr)
{
sdp_result_e result;
attr_p->attr.u32_val = sdp_getnextnumtok(ptr, &ptr, " \t", &result);
if (result != SDP_SUCCESS) {
sdp_parse_error(sdp_p,
"%s Warning: Numeric token for %s attribute not found",
sdp_p->debug_str, sdp_get_attr_name(attr_p->type));
sdp_p->conf_p->num_invalid_param++;
return (SDP_INVALID_PARAMETER);
} else {
if (sdp_p->debug_flag[SDP_DEBUG_TRACE]) {
SDP_PRINT("%s Parsed a=%s, %u", sdp_p->debug_str,
sdp_get_attr_name(attr_p->type), attr_p->attr.u32_val);
}
return (SDP_SUCCESS);
}
}
sdp_result_e sdp_build_attr_simple_u32 (sdp_t *sdp_p, sdp_attr_t *attr_p,
flex_string *fs)
{
flex_string_sprintf(fs, "a=%s:%u\r\n", sdp_attr[attr_p->type].name,
attr_p->attr.u32_val);
return SDP_SUCCESS;
}
sdp_result_e sdp_parse_attr_simple_bool (sdp_t *sdp_p, sdp_attr_t *attr_p,
const char *ptr)
{
sdp_result_e result;
if (sdp_getnextnumtok(ptr, &ptr, " \t", &result) == 0) {
attr_p->attr.boolean_val = FALSE;
} else {
attr_p->attr.boolean_val= TRUE;
}
if (result != SDP_SUCCESS) {
sdp_parse_error(sdp_p,
"%s Warning: Boolean token for %s attribute not found",
sdp_p->debug_str, sdp_get_attr_name(attr_p->type));
sdp_p->conf_p->num_invalid_param++;
return (SDP_INVALID_PARAMETER);
} else {
if (sdp_p->debug_flag[SDP_DEBUG_TRACE]) {
if (attr_p->attr.boolean_val) {
SDP_PRINT("%s Parsed a=%s, boolean is TRUE", sdp_p->debug_str,
sdp_get_attr_name(attr_p->type));
} else {
SDP_PRINT("%s Parsed a=%s, boolean is FALSE", sdp_p->debug_str,
sdp_get_attr_name(attr_p->type));
}
}
return (SDP_SUCCESS);
}
}
sdp_result_e sdp_build_attr_simple_bool (sdp_t *sdp_p, sdp_attr_t *attr_p,
flex_string *fs)
{
flex_string_sprintf(fs, "a=%s:%s\r\n", sdp_attr[attr_p->type].name,
attr_p->attr.boolean_val ? "1" : "0");
return SDP_SUCCESS;
}
/*
* sdp_parse_attr_maxprate
*
* This function parses maxprate attribute lines. The ABNF for this a=
* line is:
* max-p-rate-def = "a" "=" "maxprate" ":" packet-rate CRLF
* packet-rate = 1*DIGIT ["." 1*DIGIT]
*
* Returns:
* SDP_INVALID_PARAMETER - If we are unable to parse the string OR if
* packet-rate is not in the right format as per
* the ABNF.
*
* SDP_SUCCESS - If we are able to successfully parse the a= line.
*/
sdp_result_e sdp_parse_attr_maxprate (sdp_t *sdp_p, sdp_attr_t *attr_p,
const char *ptr)
{
sdp_result_e result;
ptr = sdp_getnextstrtok(ptr, attr_p->attr.string_val,
sizeof(attr_p->attr.string_val), " \t", &result);
if (result != SDP_SUCCESS) {
sdp_parse_error(sdp_p,
"%s Warning: No string token found for %s attribute",
sdp_p->debug_str, sdp_get_attr_name(attr_p->type));
sdp_p->conf_p->num_invalid_param++;
return (SDP_INVALID_PARAMETER);
} else {
if (!sdp_validate_maxprate(attr_p->attr.string_val)) {
sdp_parse_error(sdp_p,
"%s is not a valid maxprate value.",
attr_p->attr.string_val);
sdp_p->conf_p->num_invalid_param++;
return (SDP_INVALID_PARAMETER);
}
if (sdp_p->debug_flag[SDP_DEBUG_TRACE]) {
SDP_PRINT("%s Parsed a=%s, %s", sdp_p->debug_str,
sdp_get_attr_name(attr_p->type),
attr_p->attr.string_val);
}
return (SDP_SUCCESS);
}
}
/*
* sdp_attr_fmtp_no_value
* Helper function for sending the warning when a parameter value is
* missing.
*
*/
static void sdp_attr_fmtp_no_value(sdp_t *sdp, const char *param_name)
{
sdp_parse_error(sdp,
"%s Warning: No %s value specified for fmtp attribute",
sdp->debug_str, param_name);
sdp->conf_p->num_invalid_param++;
}
/*
* sdp_attr_fmtp_invalid_value
* Helper function for sending the warning when a parameter value is
* incorrect.
*
*/
static void sdp_attr_fmtp_invalid_value(sdp_t *sdp, const char *param_name,
const char* param_value)
{
sdp_parse_error(sdp,
"%s Warning: Invalid %s: %s specified for fmtp attribute",
sdp->debug_str, param_name, param_value);
sdp->conf_p->num_invalid_param++;
}
/*
* sdp_verify_attr_fmtp_telephone_event
* Helper function for verifying the telephone-event fmtp format
*/
static sdp_result_e sdp_verify_attr_fmtp_telephone_event(char *fmtpVal)
{
size_t len = fmtpVal ? strlen(fmtpVal) : 0;
// make sure the basics are good:
// - at least 1 character
// - no illegal chars
// - first char is a number
if (len < 1
|| strspn(fmtpVal, "0123456789,-") != len
|| PL_strstr(fmtpVal, ",,")
|| fmtpVal[len-1] == ','
|| !('0' <= fmtpVal[0] && fmtpVal[0] <= '9')) {
return SDP_INVALID_PARAMETER;
}
// Now that we've passed the basic sanity test, copy the string so we
// can tokenize and check the format of the tokens without disturbing
// the input string.
char dtmf_tones[SDP_MAX_STRING_LEN+1];
PL_strncpyz(dtmf_tones, fmtpVal, sizeof(dtmf_tones));
char *strtok_state;
char *temp = PL_strtok_r(dtmf_tones, ",", &strtok_state);
while (temp != NULL) {
len = strlen(temp);
if (len > 5) {
// an example of a max size token is "11-15", so if the
// token is longer than 5 it is bad
return SDP_INVALID_PARAMETER;
}
// case where we have 1 or 2 characters, example 4 or 23
if (len < 3 && strspn(temp, "0123456789") != len) {
return SDP_INVALID_PARAMETER;
} else if (len >= 3) {
// case where we have 3-5 characters, ex 3-5, 2-33, or 10-20
sdp_result_e result1 = SDP_SUCCESS;
sdp_result_e result2 = SDP_SUCCESS;
uint8_t low_val;
uint8_t high_val;
low_val = (uint8_t)sdp_getnextnumtok(temp, (const char **)&temp,
"-", &result1);
high_val = (uint8_t)sdp_getnextnumtok(temp, (const char **)&temp,
"-", &result2);
if (temp[0] // we don't want to find a second hyphen
|| result1 != SDP_SUCCESS
|| result2 != SDP_SUCCESS) {
return SDP_INVALID_PARAMETER;
}
if (low_val > 99
|| high_val > 99
|| high_val <= low_val) {
return SDP_INVALID_PARAMETER;
}
}
temp=PL_strtok_r(NULL, ",", &strtok_state);
}
return SDP_SUCCESS;
}
/* Note: The fmtp attribute formats currently handled are:
* fmtp:<payload type> <event>,<event>...
* fmtp:<payload_type> [annexa=yes/no] [annexb=yes/no] [bitrate=<value>]
* [QCIF =<value>] [CIF =<value>] [MaxBR = <value>] one or more
* Other FMTP params as per H.263, H.263+, H.264 codec support.
* Note -"value" is a numeric value > 0 and each event is a
* single number or a range separated by a '-'.
* Example: fmtp:101 1,3-15,20
* Video codecs have annexes that can be listed in the following legal formats:
* a) a=fmtp:34 param1=token;D;I;J;K=1;N=2;P=1,3
* b) a=fmtp:34 param1=token;D;I;J;K=1;N=2;P=1,3;T
* c) a=fmtp:34 param1=token;D;I;J
*
*/
sdp_result_e sdp_get_fmtp_tok(sdp_t *sdp_p,
const char** fmtp_ptr,
const char* fmtp_name,
char* buf,
size_t buf_size,
char** tok)
{
sdp_result_e result1 = SDP_SUCCESS;
*fmtp_ptr = sdp_getnextstrtok(*fmtp_ptr, buf, buf_size, "; \t", &result1);
if (result1 != SDP_SUCCESS) {
*fmtp_ptr = sdp_getnextstrtok(*fmtp_ptr, buf, buf_size, " \t", &result1);
if (result1 != SDP_SUCCESS) {
sdp_attr_fmtp_no_value(sdp_p, fmtp_name);
return SDP_INVALID_PARAMETER;
}
}
*tok = buf;
(*tok)++;
return SDP_SUCCESS;
}
sdp_result_e sdp_get_fmtp_tok_val(sdp_t *sdp_p,
const char** fmtp_ptr,
const char* fmtp_name,
char* buf,
size_t buf_size,
char** tok,
unsigned long* strtoul_result,
unsigned long illegal_value,
unsigned long min_limit,
unsigned long max_limit)
{
sdp_result_e result1 = SDP_SUCCESS;
unsigned long value;
char* strtoul_end;
result1 = sdp_get_fmtp_tok(sdp_p, fmtp_ptr, fmtp_name, buf, buf_size, tok);
if (result1 != SDP_SUCCESS) return result1;
errno = 0;
value = strtoul(*tok, &strtoul_end, 10);
if (errno
|| (*tok == strtoul_end)
|| (illegal_value != ULONG_MAX && value == illegal_value)
|| (min_limit != ULONG_MAX && value < min_limit)
|| (max_limit != ULONG_MAX && value > max_limit)) {
sdp_attr_fmtp_invalid_value(sdp_p, fmtp_name, *tok);
return SDP_INVALID_PARAMETER;
}
*strtoul_result = value;
return SDP_SUCCESS;
}
sdp_result_e sdp_parse_attr_fmtp (sdp_t *sdp_p, sdp_attr_t *attr_p,
const char *ptr)
{
uint16_t i;
uint32_t mapword;
uint32_t bmap;
uint8_t low_val;
uint8_t high_val;
const char *ptr2;
const char *fmtp_ptr;
sdp_result_e result1 = SDP_SUCCESS;
sdp_result_e result2 = SDP_SUCCESS;
tinybool done = FALSE;
tinybool codec_info_found = FALSE;
sdp_fmtp_t *fmtp_p;
char tmp[SDP_MAX_STRING_LEN];
char *src_ptr;
char *temp_ptr = NULL;
char *tok=NULL;
char *temp=NULL;
uint16_t custom_x=0;
uint16_t custom_y=0;
uint16_t custom_mpi=0;
uint16_t par_height=0;
uint16_t par_width=0;
uint16_t cpcf=0;
uint16_t iter=0;
ulong l_val = 0;
char* strtok_state;
unsigned long strtoul_result;
char* strtoul_end;
/* Find the payload type number. */
attr_p->attr.fmtp.payload_num = (uint16_t)sdp_getnextnumtok(ptr, &ptr,
" \t", &result1);
if (result1 != SDP_SUCCESS) {
sdp_attr_fmtp_no_value(sdp_p, "payload type");
return SDP_INVALID_PARAMETER;
}
fmtp_p = &(attr_p->attr.fmtp);
fmtp_p->fmtp_format = SDP_FMTP_UNKNOWN_TYPE;
fmtp_p->parameter_add = 1;
fmtp_p->flag = 0;
/*
* set default value of packetization mode and level-asymmetry-allowed. If
* remote sdp does not specify any value for these two parameters, then the
* default value will be assumed for remote sdp. If remote sdp does specify
* any value for these parameters, then default value will be overridden.
*/
fmtp_p->packetization_mode = SDP_DEFAULT_PACKETIZATION_MODE_VALUE;
fmtp_p->level_asymmetry_allowed = SDP_DEFAULT_LEVEL_ASYMMETRY_ALLOWED_VALUE;
temp_ptr = cpr_strdup(ptr);
if (temp_ptr == NULL) {
return (SDP_FAILURE);
}
fmtp_ptr = src_ptr = temp_ptr;
src_ptr = temp_ptr;
while (!done) {
fmtp_ptr = sdp_getnextstrtok(fmtp_ptr, tmp, sizeof(tmp), "= \t", &result1);
if (result1 == SDP_SUCCESS) {
if (cpr_strncasecmp(tmp, sdp_fmtp_codec_param[1].name,
sdp_fmtp_codec_param[1].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "annexb", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
if (cpr_strncasecmp(tok,sdp_fmtp_codec_param_val[0].name,
sdp_fmtp_codec_param_val[0].strlen) == 0) {
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->annexb_required = TRUE;
fmtp_p->annexb = TRUE;
} else if (cpr_strncasecmp(tok,sdp_fmtp_codec_param_val[1].name,
sdp_fmtp_codec_param_val[1].strlen) == 0) {
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->annexb_required = TRUE;
fmtp_p->annexb = FALSE;
} else {
sdp_attr_fmtp_invalid_value(sdp_p, "annexb", tok);
SDP_FREE(temp_ptr);
return SDP_INVALID_PARAMETER;
}
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp, sdp_fmtp_codec_param[0].name,
sdp_fmtp_codec_param[0].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "annexa", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
if (cpr_strncasecmp(tok,sdp_fmtp_codec_param_val[0].name,
sdp_fmtp_codec_param_val[0].strlen) == 0) {
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->annexa = TRUE;
fmtp_p->annexa_required = TRUE;
} else if (cpr_strncasecmp(tok,sdp_fmtp_codec_param_val[1].name,
sdp_fmtp_codec_param_val[1].strlen) == 0) {
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->annexa = FALSE;
fmtp_p->annexa_required = TRUE;
} else {
sdp_attr_fmtp_invalid_value(sdp_p, "annexa", tok);
SDP_FREE(temp_ptr);
return SDP_INVALID_PARAMETER;
}
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[2].name,
sdp_fmtp_codec_param[2].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "bitrate", tmp, sizeof(tmp),
&tok, &strtoul_result, 0, -1, UINT_MAX);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->bitrate = (uint32_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[41].name,
sdp_fmtp_codec_param[41].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "mode", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, -1, UINT_MAX);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_MODE;
fmtp_p->mode = (uint32_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[3].name,
sdp_fmtp_codec_param[3].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "qcif", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, SDP_MIN_CIF_VALUE, SDP_MAX_CIF_VALUE);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->qcif = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[4].name,
sdp_fmtp_codec_param[4].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "cif", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, SDP_MIN_CIF_VALUE, SDP_MAX_CIF_VALUE);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->cif = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[5].name,
sdp_fmtp_codec_param[5].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "maxbr", tmp, sizeof(tmp),
&tok, &strtoul_result, 0, -1, USHRT_MAX);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->maxbr = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[6].name,
sdp_fmtp_codec_param[6].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "sqcif", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, SDP_MIN_CIF_VALUE, SDP_MAX_CIF_VALUE);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->sqcif = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[7].name,
sdp_fmtp_codec_param[7].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "cif4", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, SDP_MIN_CIF_VALUE, SDP_MAX_CIF_VALUE);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->cif4 = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[8].name,
sdp_fmtp_codec_param[8].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "cif16", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, SDP_MIN_CIF_VALUE, SDP_MAX_CIF_VALUE);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->cif16 = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[9].name,
sdp_fmtp_codec_param[9].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "custom", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
temp=PL_strtok_r(tok, ",", &strtok_state);
iter++;
if (temp) {
iter=1;
while (temp != NULL) {
errno = 0;
strtoul_result = strtoul(temp, &strtoul_end, 10);
if (errno || temp == strtoul_end || strtoul_result > USHRT_MAX){
custom_x = custom_y = custom_mpi = 0;
break;
}
if (iter == 1)
custom_x = (uint16_t) strtoul_result;
if (iter == 2)
custom_y = (uint16_t) strtoul_result;
if (iter == 3)
custom_mpi = (uint16_t) strtoul_result;
temp=PL_strtok_r(NULL, ",", &strtok_state);
iter++;
}
}
/* custom x,y and mpi values from tmp */
if (!custom_x || !custom_y || !custom_mpi) {
sdp_attr_fmtp_invalid_value(sdp_p, "x/y/MPI", temp);
SDP_FREE(temp_ptr);
return SDP_INVALID_PARAMETER;
}
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->custom_x = custom_x;
fmtp_p->custom_y = custom_y;
fmtp_p->custom_mpi = custom_mpi;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[10].name,
sdp_fmtp_codec_param[10].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "par", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
temp=PL_strtok_r(tok, ":", &strtok_state);
if (temp) {
iter=1;
/* get par width and par height for the aspect ratio */
while (temp != NULL) {
errno = 0;
strtoul_result = strtoul(temp, &strtoul_end, 10);
if (errno || temp == strtoul_end || strtoul_result > USHRT_MAX) {
par_width = par_height = 0;
break;
}
if (iter == 1)
par_width = (uint16_t) strtoul_result;
else
par_height = (uint16_t) strtoul_result;
temp=PL_strtok_r(NULL, ",", &strtok_state);
iter++;
}
}
if (!par_width || !par_height) {
sdp_attr_fmtp_invalid_value(sdp_p, "par_width or par_height", temp);
SDP_FREE(temp_ptr);
return SDP_INVALID_PARAMETER;
}
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->par_width = par_width;
fmtp_p->par_height = par_height;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[11].name,
sdp_fmtp_codec_param[11].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "cpcf", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
temp=PL_strtok_r(tok, ".", &strtok_state);
if ( temp != NULL ) {
errno = 0;
strtoul_result = strtoul(temp, &strtoul_end, 10);
if (errno || temp == strtoul_end || strtoul_result > USHRT_MAX) {
cpcf = 0;
} else {
cpcf = (uint16_t) strtoul_result;
}
}
if (!cpcf) {
sdp_attr_fmtp_invalid_value(sdp_p, "cpcf", tok);
SDP_FREE(temp_ptr);
return SDP_INVALID_PARAMETER;
}
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->cpcf = cpcf;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[12].name,
sdp_fmtp_codec_param[12].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "bpp", tmp, sizeof(tmp),
&tok, &strtoul_result, 0, -1, USHRT_MAX);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->bpp = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[13].name,
sdp_fmtp_codec_param[13].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "hrd", tmp, sizeof(tmp),
&tok, &strtoul_result, 0, -1, USHRT_MAX);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->hrd = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[14].name,
sdp_fmtp_codec_param[14].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "profile", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, -1, SDP_MAX_PROFILE_VALUE);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->profile = (short) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[15].name,
sdp_fmtp_codec_param[15].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "level", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, -1, SDP_MAX_LEVEL_VALUE);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->level = (short) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[16].name,
sdp_fmtp_codec_param[16].strlen) == 0) {
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->is_interlace = TRUE;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[17].name,
sdp_fmtp_codec_param[17].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "profile_level_id", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
sstrncpy(fmtp_p->profile_level_id , tok, sizeof(fmtp_p->profile_level_id));
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[18].name,
sdp_fmtp_codec_param[18].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "parameter_sets", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
sstrncpy(fmtp_p->parameter_sets , tok, sizeof(fmtp_p->parameter_sets));
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[19].name,
sdp_fmtp_codec_param[19].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "packetization_mode", tmp, sizeof(tmp),
&tok, &strtoul_result, -1, -1, 2);
// this one is different for some reason. Most others don't increment
// the num_invalid_param field. (mjf)
if (result1 == SDP_INVALID_PARAMETER) { sdp_p->conf_p->num_invalid_param++; }
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->packetization_mode = (int16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[20].name,
sdp_fmtp_codec_param[20].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "interleaving_depth", tmp, sizeof(tmp),
&tok, &strtoul_result, 0, -1, USHRT_MAX);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->interleaving_depth = (uint16_t) strtoul_result;
codec_info_found = TRUE;
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[21].name,
sdp_fmtp_codec_param[21].strlen) == 0) {
result1 = sdp_get_fmtp_tok(sdp_p, &fmtp_ptr, "deint_buf", tmp, sizeof(tmp), &tok);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
if (sdp_checkrange(sdp_p, tok, &l_val) == TRUE) {
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->deint_buf_req = (uint32_t) l_val;
fmtp_p->flag |= SDP_DEINT_BUF_REQ_FLAG;
codec_info_found = TRUE;
} else {
sdp_attr_fmtp_invalid_value(sdp_p, "deint_buf_req", tok);
SDP_FREE(temp_ptr);
return SDP_INVALID_PARAMETER;
}
} else if (cpr_strncasecmp(tmp,sdp_fmtp_codec_param[22].name,
sdp_fmtp_codec_param[22].strlen) == 0) {
result1 = sdp_get_fmtp_tok_val(sdp_p, &fmtp_ptr, "max_don_diff", tmp, sizeof(tmp),
&tok, &strtoul_result, 0, -1, UINT_MAX);
if (result1 != SDP_SUCCESS) { SDP_FREE(temp_ptr); return result1; }
fmtp_p->fmtp_format = SDP_FMTP_CODEC_INFO;
fmtp_p->max_don_diff = (uint32_t) strtoul_result;
codec_info_found = TRUE;