summaryrefslogtreecommitdiffstats
path: root/src/sdp/sdp_parse_ctrl.cpp
blob: 1f2e6a80c7da13deb90b45d603cd9609ec42fb8d (plain)
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
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "sdp_parse_ctrl.h"
#include "audits/memman.h"

// Interface to Bison
extern int yysdpparse(void);

// Interface to Flex
struct yy_buffer_state;
extern struct yy_buffer_state *yysdp_scan_string(const char *);
extern void yysdp_delete_buffer(struct yy_buffer_state *);

t_mutex t_sdp_parser::mtx_parser;
t_sdp_parser::t_context t_sdp_parser::context = t_sdp_parser::X_INITIAL;
t_sdp *t_sdp_parser::sdp = NULL;

t_sdp *t_sdp_parser::parse(const string &s) {
	int ret;
	struct yy_buffer_state *b;
	
	t_mutex_guard guard(mtx_parser);
	
	sdp = new t_sdp();
	MEMMAN_NEW(sdp);
	
	// The SDP body should end with a CRLF. Some implementations
	// do not send this last CRLF. Allow this deviation by adding
	// the last CRLF if it is missing.
	char last_char = s.at(s.size()-1);
	if (last_char == '\n' || last_char == '\r') {
		// The SDP parser allows \r, \r\n and \n as CRLF
		b = yysdp_scan_string(s.c_str());
	} else {
		// Last CRLF is missing.
		b = yysdp_scan_string((s + "\r\n").c_str());
	}
	
	ret = yysdpparse();
	yysdp_delete_buffer(b);

	if (ret != 0) {
		MEMMAN_DELETE(sdp);
		delete sdp;
		sdp = NULL;
		throw ret;
	}

	return sdp;
}

t_sdp_syntax_error::t_sdp_syntax_error(const string &e) {
	error = e;
}