Skip to content

Commit e806866

Browse files
committed
rust/sip+sdp: update to nom 8
Done together as there is a dependence of SDP by SIP. Ticket: OISF#8025
1 parent 16656a2 commit e806866

3 files changed

Lines changed: 130 additions & 135 deletions

File tree

rust/src/sdp/parser.rs

Lines changed: 94 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,17 @@
1717

1818
// written by Giuseppe Longo <giuseppe@glongo.it>
1919

20-
use nom7::{
20+
use nom8::{
2121
branch::alt,
2222
bytes::complete::{tag, take_till, take_while, take_while_m_n},
23-
character::{
24-
complete::{char as char_parser, digit1, line_ending, space1, u8 as take_u8},
25-
is_alphabetic,
26-
},
27-
character::{is_alphanumeric, is_digit, is_space},
23+
character::complete::{char as char_parser, digit1, line_ending, space1, u8 as take_u8},
2824
combinator::map_res,
2925
combinator::{opt, peek, verify},
3026
error::{make_error, ErrorKind},
3127
multi::{many0, many1},
3228
number::complete::be_u8,
33-
sequence::{preceded, tuple},
34-
{Err, IResult},
29+
sequence::preceded,
30+
{AsChar, Err, IResult, Parser},
3531
};
3632

3733
use std::net::IpAddr;
@@ -83,7 +79,7 @@ fn is_token_char(b: u8) -> bool {
8379

8480
#[inline]
8581
fn is_request_uri_char(b: u8) -> bool {
86-
is_alphanumeric(b) || is_token_char(b) || b"~#@:;=?+&$,/".contains(&b)
82+
b.is_alphanum() || is_token_char(b) || b"~#@:;=?+&$,/".contains(&b)
8783
}
8884

8985
#[inline]
@@ -98,16 +94,16 @@ fn is_ipaddr_char(b: u8) -> bool {
9894

9995
#[inline]
10096
fn is_session_name_char(b: u8) -> bool {
101-
is_alphanumeric(b) || is_space(b)
97+
b.is_alphanum() || b.is_space()
10298
}
10399

104100
#[inline]
105101
fn is_time_char(b: u8) -> bool {
106-
is_digit(b) || b"dhms-".contains(&b)
102+
b.is_dec_digit() || b"dhms-".contains(&b)
107103
}
108104

109105
fn parse_num(i: &[u8]) -> IResult<&[u8], u8> {
110-
let (i, num) = preceded(verify(peek(be_u8), |d| *d != 0x30), take_u8)(i)?;
106+
let (i, num) = preceded(verify(peek(be_u8), |d| *d != 0x30), take_u8).parse(i)?;
111107
Ok((i, num))
112108
}
113109

@@ -148,17 +144,17 @@ pub fn sdp_parse_message(i: &[u8]) -> IResult<&[u8], SdpMessage> {
148144
let (i, version) = parse_version_line(i)?;
149145
let (i, origin) = parse_origin_line(i)?;
150146
let (i, session_name) = parse_session_name(i)?;
151-
let (i, session_info) = opt(parse_session_info)(i)?;
152-
let (i, uri) = opt(parse_uri)(i)?;
153-
let (i, email) = opt(parse_email)(i)?;
154-
let (i, phone_number) = opt(parse_phone_number)(i)?;
155-
let (i, connection_data) = opt(parse_connection_data)(i)?;
156-
let (i, bandwidths) = opt(parse_bandwidth)(i)?;
157-
let (i, time_description) = many1(parse_time_description)(i)?;
158-
let (i, time_zone) = opt(parse_time_zone)(i)?;
159-
let (i, encryption_key) = opt(parse_encryption_key)(i)?;
160-
let (i, attributes) = opt(parse_attributes)(i)?;
161-
let (i, media_description) = opt(many0(parse_media_description))(i)?;
147+
let (i, session_info) = opt(parse_session_info).parse(i)?;
148+
let (i, uri) = opt(parse_uri).parse(i)?;
149+
let (i, email) = opt(parse_email).parse(i)?;
150+
let (i, phone_number) = opt(parse_phone_number).parse(i)?;
151+
let (i, connection_data) = opt(parse_connection_data).parse(i)?;
152+
let (i, bandwidths) = opt(parse_bandwidth).parse(i)?;
153+
let (i, time_description) = many1(parse_time_description).parse(i)?;
154+
let (i, time_zone) = opt(parse_time_zone).parse(i)?;
155+
let (i, encryption_key) = opt(parse_encryption_key).parse(i)?;
156+
let (i, attributes) = opt(parse_attributes).parse(i)?;
157+
let (i, media_description) = opt(many0(parse_media_description)).parse(i)?;
162158
Ok((
163159
i,
164160
SdpMessage {
@@ -181,27 +177,27 @@ pub fn sdp_parse_message(i: &[u8]) -> IResult<&[u8], SdpMessage> {
181177
}
182178

183179
fn parse_version_line(i: &[u8]) -> IResult<&[u8], u32> {
184-
let (i, _) = tag("v=")(i)?;
185-
let (i, _v) = tag("0")(i)?;
186-
let (i, _) = line_ending(i)?;
180+
let (i, _) = tag("v=").parse(i)?;
181+
let (i, _v) = tag("0").parse(i)?;
182+
let (i, _) = line_ending.parse(i)?;
187183

188184
Ok((i, 0))
189185
}
190186

191187
fn parse_origin_line(i: &[u8]) -> IResult<&[u8], String> {
192-
let (i, _) = tag("o=")(i)?;
193-
let (i, username) = map_res(take_while(is_token_char), std::str::from_utf8)(i)?;
194-
let (i, _) = space1(i)?;
195-
let (i, sess_id) = map_res(take_while(is_digit), std::str::from_utf8)(i)?;
196-
let (i, _) = space1(i)?;
197-
let (i, sess_version) = map_res(take_while(is_digit), std::str::from_utf8)(i)?;
198-
let (i, _) = space1(i)?;
199-
let (i, nettype) = map_res(take_while(is_alphabetic), std::str::from_utf8)(i)?;
200-
let (i, _) = space1(i)?;
201-
let (i, addrtype) = map_res(take_while(is_alphanumeric), std::str::from_utf8)(i)?;
202-
let (i, _) = space1(i)?;
203-
let (i, unicast_address) = map_res(take_till(is_line_ending), std::str::from_utf8)(i)?;
204-
let (i, _) = line_ending(i)?;
188+
let (i, _) = tag("o=").parse(i)?;
189+
let (i, username) = map_res(take_while(is_token_char), std::str::from_utf8).parse(i)?;
190+
let (i, _) = space1.parse(i)?;
191+
let (i, sess_id) = map_res(take_while(|c: u8| c.is_dec_digit()), std::str::from_utf8).parse(i)?;
192+
let (i, _) = space1.parse(i)?;
193+
let (i, sess_version) = map_res(take_while(|c: u8| c.is_dec_digit()), std::str::from_utf8).parse(i)?;
194+
let (i, _) = space1.parse(i)?;
195+
let (i, nettype) = map_res(take_while(|c: u8| c.is_alpha()), std::str::from_utf8).parse(i)?;
196+
let (i, _) = space1.parse(i)?;
197+
let (i, addrtype) = map_res(take_while(|c: u8| c.is_alphanum()), std::str::from_utf8).parse(i)?;
198+
let (i, _) = space1.parse(i)?;
199+
let (i, unicast_address) = map_res(take_till(is_line_ending), std::str::from_utf8).parse(i)?;
200+
let (i, _) = line_ending.parse(i)?;
205201

206202
let origin_line = format!(
207203
"{} {} {} {} {} {}",
@@ -212,39 +208,39 @@ fn parse_origin_line(i: &[u8]) -> IResult<&[u8], String> {
212208
}
213209

214210
fn parse_session_name(i: &[u8]) -> IResult<&[u8], String> {
215-
let (i, _) = tag("s=")(i)?;
216-
let (i, name) = map_res(take_while(is_session_name_char), std::str::from_utf8)(i)?;
217-
let (i, _) = line_ending(i)?;
211+
let (i, _) = tag("s=").parse(i)?;
212+
let (i, name) = map_res(take_while(is_session_name_char), std::str::from_utf8).parse(i)?;
213+
let (i, _) = line_ending.parse(i)?;
218214
Ok((i, name.to_string()))
219215
}
220216

221217
fn parse_session_info(i: &[u8]) -> IResult<&[u8], String> {
222-
let (i, _) = tag("i=")(i)?;
223-
let (i, info) = map_res(take_while(is_session_name_char), std::str::from_utf8)(i)?;
224-
let (i, _) = line_ending(i)?;
218+
let (i, _) = tag("i=").parse(i)?;
219+
let (i, info) = map_res(take_while(is_session_name_char), std::str::from_utf8).parse(i)?;
220+
let (i, _) = line_ending.parse(i)?;
225221
Ok((i, info.to_string()))
226222
}
227223

228224
fn parse_uri(i: &[u8]) -> IResult<&[u8], String> {
229-
let (i, _) = tag("u=")(i)?;
230-
let (i, uri) = map_res(take_while(is_request_uri_char), std::str::from_utf8)(i)?;
231-
let (i, _) = line_ending(i)?;
225+
let (i, _) = tag("u=").parse(i)?;
226+
let (i, uri) = map_res(take_while(is_request_uri_char), std::str::from_utf8).parse(i)?;
227+
let (i, _) = line_ending.parse(i)?;
232228
Ok((i, uri.to_string()))
233229
}
234230

235231
fn parse_connection_data(i: &[u8]) -> IResult<&[u8], String> {
236-
let (i, _) = tag("c=")(i)?;
237-
let (i, nettype) = map_res(take_while(is_alphabetic), std::str::from_utf8)(i)?;
238-
let (i, _) = space1(i)?;
239-
let (i, addrtype) = map_res(take_while(is_alphanumeric), std::str::from_utf8)(i)?;
240-
let (i, _) = space1(i)?;
232+
let (i, _) = tag("c=").parse(i)?;
233+
let (i, nettype) = map_res(take_while(|c: u8| c.is_alpha()), std::str::from_utf8).parse(i)?;
234+
let (i, _) = space1.parse(i)?;
235+
let (i, addrtype) = map_res(take_while(|c: u8| c.is_alphanum()), std::str::from_utf8).parse(i)?;
236+
let (i, _) = space1.parse(i)?;
241237
let (i, connection_address) = map_res(
242238
map_res(take_while(is_ipaddr_char), std::str::from_utf8),
243239
IpAddr::from_str,
244-
)(i)?;
245-
let (i, first_num) = opt(preceded(char_parser('/'), parse_num))(i)?;
246-
let (i, second_num) = opt(preceded(char_parser('/'), parse_num))(i)?;
247-
let (i, _) = line_ending(i)?;
240+
).parse(i)?;
241+
let (i, first_num) = opt(preceded(char_parser('/'), parse_num)).parse(i)?;
242+
let (i, second_num) = opt(preceded(char_parser('/'), parse_num)).parse(i)?;
243+
let (i, _) = line_ending.parse(i)?;
248244

249245
let (ttl, number_of_addresses) = match connection_address {
250246
_ if connection_address.is_ipv6() => (None, first_num),
@@ -281,89 +277,89 @@ fn parse_email(i: &[u8]) -> IResult<&[u8], String> {
281277
let (i, email) = preceded(
282278
tag("e="),
283279
map_res(take_till(is_line_ending), std::str::from_utf8),
284-
)(i)?;
285-
let (i, _) = line_ending(i)?;
280+
).parse(i)?;
281+
let (i, _) = line_ending.parse(i)?;
286282
Ok((i, email.to_string()))
287283
}
288284

289285
fn parse_phone_number(i: &[u8]) -> IResult<&[u8], String> {
290286
let (i, phone_number) = preceded(
291287
tag("p="),
292288
map_res(take_till(is_line_ending), std::str::from_utf8),
293-
)(i)?;
294-
let (i, _) = line_ending(i)?;
289+
).parse(i)?;
290+
let (i, _) = line_ending.parse(i)?;
295291
Ok((i, phone_number.to_string()))
296292
}
297293

298294
fn parse_bandwidth(i: &[u8]) -> IResult<&[u8], Vec<String>> {
299295
let (i, bws) = many0(preceded(
300296
tag("b="),
301-
tuple((
297+
(
302298
map_res(
303299
alt((tag("CT"), tag("AS"), tag("TIAS"))),
304300
std::str::from_utf8,
305301
),
306302
char_parser(':'),
307303
map_res(digit1, std::str::from_utf8),
308304
line_ending,
309-
)),
310-
))(i)?;
305+
),
306+
)).parse(i)?;
311307
let vec = bws.iter().map(|bw| format!("{}:{}", bw.0, bw.2)).collect();
312308
Ok((i, vec))
313309
}
314310

315311
fn parse_time_description(i: &[u8]) -> IResult<&[u8], TimeDescription> {
316312
let (i, time) = parse_time(i)?;
317-
let (i, repeat_time) = opt(parse_repeat_times)(i)?;
313+
let (i, repeat_time) = opt(parse_repeat_times).parse(i)?;
318314
Ok((i, TimeDescription { time, repeat_time }))
319315
}
320316

321317
fn parse_time(i: &[u8]) -> IResult<&[u8], String> {
322318
let (i, (start_time, _, stop_time)) = preceded(
323319
tag("t="),
324-
tuple((
320+
(
325321
map_res(digit1, std::str::from_utf8),
326322
space1,
327323
map_res(digit1, std::str::from_utf8),
328-
)),
329-
)(i)?;
330-
let (i, _) = line_ending(i)?;
324+
),
325+
).parse(i)?;
326+
let (i, _) = line_ending.parse(i)?;
331327
let time = format!("{} {}", start_time, stop_time);
332328
Ok((i, time))
333329
}
334330

335331
fn parse_repeat_times(i: &[u8]) -> IResult<&[u8], String> {
336332
let (i, (d, _, h, _, m, _, s)) = preceded(
337333
tag("r="),
338-
tuple((
334+
(
339335
map_res(take_while(is_time_char), std::str::from_utf8),
340336
space1,
341337
map_res(take_while(is_time_char), std::str::from_utf8),
342338
space1,
343339
map_res(take_while(is_time_char), std::str::from_utf8),
344340
space1,
345341
map_res(take_while(is_time_char), std::str::from_utf8),
346-
)),
347-
)(i)?;
348-
let (i, _) = line_ending(i)?;
342+
),
343+
).parse(i)?;
344+
let (i, _) = line_ending.parse(i)?;
349345
let val = format!("{} {} {} {}", d, h, m, s);
350346
Ok((i, val.to_string()))
351347
}
352348

353349
fn parse_time_zone(i: &[u8]) -> IResult<&[u8], String> {
354350
let (i, (z1, _, z2, _, z3, _, z4)) = preceded(
355351
tag("z="),
356-
tuple((
352+
(
357353
map_res(take_while(is_time_char), std::str::from_utf8),
358354
space1,
359355
map_res(take_while(is_time_char), std::str::from_utf8),
360356
space1,
361357
map_res(take_while(is_time_char), std::str::from_utf8),
362358
space1,
363359
map_res(take_while(is_time_char), std::str::from_utf8),
364-
)),
365-
)(i)?;
366-
let (i, _) = line_ending(i)?;
360+
),
361+
).parse(i)?;
362+
let (i, _) = line_ending.parse(i)?;
367363
let tz = format!("{} {} {} {}", z1, z2, z3, z4);
368364
Ok((i, tz.to_string()))
369365
}
@@ -372,23 +368,23 @@ fn parse_encryption_key(i: &[u8]) -> IResult<&[u8], String> {
372368
let (i, key) = preceded(
373369
tag("k="),
374370
map_res(take_till(is_line_ending), std::str::from_utf8),
375-
)(i)?;
376-
let (i, _) = line_ending(i)?;
371+
).parse(i)?;
372+
let (i, _) = line_ending.parse(i)?;
377373
Ok((i, key.to_string()))
378374
}
379375

380376
fn parse_attributes(i: &[u8]) -> IResult<&[u8], Vec<String>> {
381377
let (i, attrs) = many0(preceded(
382378
tag("a="),
383-
tuple((
384-
map_res(take_while(is_alphabetic), std::str::from_utf8),
379+
(
380+
map_res(take_while(|c: u8| c.is_alpha()), std::str::from_utf8),
385381
opt(preceded(
386382
char_parser(':'),
387383
map_res(take_till(is_line_ending), std::str::from_utf8),
388384
)),
389385
line_ending,
390-
)),
391-
))(i)?;
386+
),
387+
)).parse(i)?;
392388
let vec = attrs
393389
.iter()
394390
.map(|a| {
@@ -403,7 +399,7 @@ fn parse_attributes(i: &[u8]) -> IResult<&[u8], Vec<String>> {
403399
}
404400

405401
fn parse_media_description(i: &[u8]) -> IResult<&[u8], MediaDescription> {
406-
let (i, _) = tag("m=")(i)?;
402+
let (i, _) = tag("m=").parse(i)?;
407403
let (i, media) = map_res(
408404
alt((
409405
tag("audio"),
@@ -413,41 +409,41 @@ fn parse_media_description(i: &[u8]) -> IResult<&[u8], MediaDescription> {
413409
tag("message"),
414410
)),
415411
|bytes: &[u8]| String::from_utf8(bytes.to_vec()),
416-
)(i)?;
417-
let (i, _) = space1(i)?;
412+
).parse(i)?;
413+
let (i, _) = space1.parse(i)?;
418414

419415
let (i, port) = map_res(
420416
take_while_m_n(1, 5, |b: u8| b.is_ascii_digit()),
421417
std::str::from_utf8,
422-
)(i)?;
418+
).parse(i)?;
423419
let (i, number_of_ports) = opt(preceded(
424420
char_parser('/'),
425421
map_res(
426422
take_while_m_n(1, 5, |b: u8| b.is_ascii_digit()),
427423
std::str::from_utf8,
428424
),
429-
))(i)?;
430-
let (i, _) = space1(i)?;
425+
)).parse(i)?;
426+
let (i, _) = space1.parse(i)?;
431427

432428
let (i, proto) = map_res(
433429
alt((tag("udp"), tag("RTP/AVP"), tag("RTP/SAVP"))),
434430
|bytes: &[u8]| String::from_utf8(bytes.to_vec()),
435-
)(i)?;
431+
).parse(i)?;
436432

437433
let (i, fmt) = many1(preceded(
438434
space1,
439435
map_res(
440436
take_while_m_n(1, 255, |b: u8| b.is_ascii_alphanumeric()),
441437
std::str::from_utf8,
442438
),
443-
))(i)?;
444-
let (i, _) = line_ending(i)?;
445-
446-
let (i, session_info) = opt(parse_session_info)(i)?;
447-
let (i, connection_data) = opt(parse_connection_data)(i)?;
448-
let (i, bandwidths) = opt(parse_bandwidth)(i)?;
449-
let (i, encryption_key) = opt(parse_encryption_key)(i)?;
450-
let (i, attributes) = opt(parse_attributes)(i)?;
439+
)).parse(i)?;
440+
let (i, _) = line_ending.parse(i)?;
441+
442+
let (i, session_info) = opt(parse_session_info).parse(i)?;
443+
let (i, connection_data) = opt(parse_connection_data).parse(i)?;
444+
let (i, bandwidths) = opt(parse_bandwidth).parse(i)?;
445+
let (i, encryption_key) = opt(parse_encryption_key).parse(i)?;
446+
let (i, attributes) = opt(parse_attributes).parse(i)?;
451447

452448
let port: u16 = match port.parse::<u16>() {
453449
Ok(p) => p,

0 commit comments

Comments
 (0)