use shared::error::{Error, Result};
pub(crate) fn syntax(what: &str, value: &str) -> Error {
Error::SdpInvalidSyntax(format!("{what}: `{}`", abbreviate(value)))
}
pub(crate) fn invalid(what: &str, value: &str) -> Error {
Error::SdpInvalidValue(format!("{what}: `{}`", abbreviate(value)))
}
const MAX_QUOTED: usize = 96;
fn abbreviate(value: &str) -> &str {
if value.len() <= MAX_QUOTED {
return value;
}
let mut end = MAX_QUOTED;
while !value.is_char_boundary(end) {
end -= 1;
}
&value[..end]
}
pub(crate) fn is_token_char(b: u8) -> bool {
b.is_ascii_alphanumeric()
|| matches!(
b,
b'!' | b'#'
| b'$'
| b'%'
| b'&'
| b'\''
| b'*'
| b'+'
| b'-'
| b'.'
| b'^'
| b'_'
| b'`'
| b'{'
| b'|'
| b'}'
| b'~'
)
}
pub(crate) fn is_token(value: &str) -> bool {
!value.is_empty() && value.bytes().all(is_token_char)
}
pub(crate) fn is_byte_string(value: &str) -> bool {
!value.is_empty() && !value.bytes().any(|b| matches!(b, 0 | b'\r' | b'\n'))
}
pub(crate) fn is_non_ws_string(value: &str) -> bool {
!value.is_empty() && value.bytes().all(|b| b > 0x20 && b != 0x7f)
}
pub(crate) fn is_digits(value: &str) -> bool {
!value.is_empty() && value.bytes().all(|b| b.is_ascii_digit())
}
pub(crate) fn is_integer(value: &str) -> bool {
is_digits(value) && !value.starts_with('0')
}
pub(crate) fn is_alnum_dash_underscore(value: &str) -> bool {
!value.is_empty()
&& value
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
}
pub(crate) fn digits<T: core::str::FromStr>(what: &str, value: &str) -> Result<T> {
if !is_digits(value) {
return Err(syntax(what, value));
}
value.parse::<T>().map_err(|_| invalid(what, value))
}
pub(crate) fn digits_no_leading_zero<T: core::str::FromStr>(what: &str, value: &str) -> Result<T> {
if value.len() > 1 && value.starts_with('0') {
return Err(syntax(what, value));
}
digits(what, value)
}
pub(crate) fn is_non_zero_int_or_real(value: &str) -> bool {
match value.split_once('.') {
None => is_integer(value),
Some((int, frac)) => {
(int == "0" || is_integer(int))
&& is_digits(frac)
&& !frac.ends_with('0')
&& frac.bytes().all(|b| b.is_ascii_digit())
}
}
}
fn decimal_uchar(value: &str) -> Option<u8> {
if value.is_empty() || value.len() > 3 || !is_digits(value) {
return None;
}
if value.len() > 1 && value.starts_with('0') {
return None;
}
value.parse::<u8>().ok()
}
fn dotted_quad(value: &str) -> Option<[u8; 4]> {
let mut out = [0u8; 4];
let mut parts = value.split('.');
for slot in &mut out {
*slot = decimal_uchar(parts.next()?)?;
}
if parts.next().is_some() {
return None;
}
Some(out)
}
pub(crate) fn is_ip4_address(value: &str) -> bool {
matches!(dotted_quad(value), Some(q) if q[0] < 224)
}
pub(crate) fn is_ip4_multicast_address(value: &str) -> bool {
matches!(dotted_quad(value), Some(q) if (224..=239).contains(&q[0]))
}
pub(crate) fn is_ip6_address(value: &str) -> bool {
value
.bytes()
.all(|b| b.is_ascii_hexdigit() || b == b':' || b == b'.')
&& value.parse::<std::net::Ipv6Addr>().is_ok()
}
pub(crate) fn is_ip6_multicast_address(value: &str) -> bool {
matches!(value.parse::<std::net::Ipv6Addr>(), Ok(a) if a.is_multicast())
&& is_ip6_address(value)
}
pub(crate) fn is_fqdn(value: &str) -> bool {
let name = value.strip_suffix('.').unwrap_or(value);
value.len() >= 4
&& name.len() <= 253
&& value
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'.')
&& name.split('.').all(|label| {
(1..=63).contains(&label.len()) && !label.starts_with('-') && !label.ends_with('-')
})
}
pub(crate) fn is_unicast_address(address_type: &str, value: &str) -> bool {
match address_type {
"IP4" => is_ip4_address(value) || is_fqdn(value),
"IP6" => is_ip6_address(value) || is_fqdn(value),
_ => is_non_ws_string(value),
}
}
pub(crate) fn is_candidate_address(value: &str) -> bool {
dotted_quad(value).is_some() || is_ip6_address(value) || is_fqdn(value)
}
pub(crate) fn port(value: &str) -> Result<u16> {
digits("port", value)
}
pub(crate) fn is_ice_chars(value: &str) -> bool {
!value.is_empty()
&& value
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'/')
}
pub(crate) fn is_vchars(value: &str) -> bool {
value.bytes().all(|b| (0x21..=0x7e).contains(&b))
}
pub(crate) fn is_base64(value: &str) -> bool {
let bytes = value.as_bytes();
if !bytes.len().is_multiple_of(4) {
return false;
}
let pad = bytes.iter().rev().take_while(|&&b| b == b'=').count();
pad <= 2
&& bytes[..bytes.len() - pad]
.iter()
.all(|&b| b.is_ascii_alphanumeric() || b == b'+' || b == b'/')
}
pub(crate) fn fields<'a>(what: &str, value: &'a str) -> Result<Vec<&'a str>> {
let fields: Vec<&str> = value.split(' ').collect();
if fields.iter().any(|f| f.is_empty()) {
return Err(syntax(what, value));
}
Ok(fields)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn token_is_the_rfc_8866_character_set() {
assert!(is_token("UDP"));
assert!(is_token("!#$%&'*+-.^_`{|}~aZ09"));
for bad in [
"", "a b", "a/b", "a:b", "a=b", "a(b", "a\"b", "é", "a@b", "a,b", "a;b",
] {
assert!(!is_token(bad), "{bad}");
}
}
#[test]
fn byte_string_excludes_nul_cr_lf_and_the_empty_string() {
assert!(is_byte_string(" "));
assert!(is_byte_string("é\t\u{7f}"));
for bad in ["", "a\0b", "a\rb", "a\nb"] {
assert!(!is_byte_string(bad), "{bad:?}");
}
}
#[test]
fn non_zero_int_or_real_follows_the_abnf() {
for good in ["1", "20", "0.5", "10.25", "0.001", "29.97"] {
assert!(is_non_zero_int_or_real(good), "{good}");
}
for bad in [
"0", "00", "01", "0.0", "1.0", "1.50", "1.", ".5", "-1", "1e3", "", "1.2.3",
] {
assert!(!is_non_zero_int_or_real(bad), "{bad}");
}
}
#[test]
fn ip4_addresses_are_decimal_uchars_and_unicast_is_below_224() {
assert!(is_ip4_address("0.0.0.0"));
assert!(is_ip4_address("223.255.255.255"));
assert!(!is_ip4_address("224.0.0.1"));
assert!(is_ip4_multicast_address("224.0.0.1"));
assert!(is_ip4_multicast_address("239.255.255.255"));
assert!(!is_ip4_multicast_address("240.0.0.1"));
for bad in [
"1.2.3",
"1.2.3.4.5",
"01.2.3.4",
"256.1.1.1",
"1..2.3",
"a.b.c.d",
"",
] {
assert!(!is_ip4_address(bad), "{bad}");
}
}
#[test]
fn ip6_addresses_follow_rfc_3986_and_have_no_zone() {
for good in [
"::",
"::1",
"2001:db8::1",
"FF15::101",
"::ffff:192.0.2.1",
"1:2:3:4:5:6:7:8",
] {
assert!(is_ip6_address(good), "{good}");
}
for bad in [
"",
"1.2.3.4",
"fe80::1%eth0",
"1:2:3:4:5:6:7:8:9",
"g::1",
"[::1]",
] {
assert!(!is_ip6_address(bad), "{bad}");
}
assert!(is_ip6_multicast_address("FF15::101"));
assert!(!is_ip6_multicast_address("2001:db8::1"));
}
#[test]
fn unicast_address_depends_on_the_address_type() {
assert!(is_unicast_address("IP4", "192.0.2.1"));
assert!(is_unicast_address("IP4", "host.example.com"));
assert!(!is_unicast_address("IP4", "::1"));
assert!(is_unicast_address("IP6", "::1"));
assert!(!is_unicast_address("IP6", "a b"));
assert!(is_unicast_address("E164", "+15551234"));
assert!(!is_unicast_address("E164", ""));
}
#[test]
fn base64_needs_whole_units_and_at_most_two_pad_characters() {
for good in ["", "QUJD", "QUI=", "QQ==", "ab+/"] {
assert!(is_base64(good), "{good}");
}
for bad in ["QUJ", "Q===", "QU-D", "QUJD=", "====="] {
assert!(!is_base64(bad), "{bad}");
}
}
#[test]
fn fields_are_separated_by_exactly_one_space() {
assert_eq!(fields("x", "a b c").unwrap(), vec!["a", "b", "c"]);
for bad in ["a b", " a", "a ", ""] {
assert!(fields("x", bad).is_err(), "{bad:?}");
}
}
#[test]
fn digits_reports_syntax_and_range_separately() {
assert_eq!(digits::<u8>("n", "255").unwrap(), 255);
assert!(matches!(
digits::<u8>("n", "256"),
Err(Error::SdpInvalidValue(_))
));
assert!(matches!(
digits::<u8>("n", "+1"),
Err(Error::SdpInvalidSyntax(_))
));
assert!(digits_no_leading_zero::<u16>("n", "0").is_ok());
assert!(digits_no_leading_zero::<u16>("n", "05").is_err());
}
}