use crate::convert::TryInto;
use crate::error::Error;
use crate::fmt;
use crate::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use crate::str::FromStr;
trait ReadNumberHelper: Sized {
const ZERO: Self;
fn checked_mul(&self, other: u32) -> Option<Self>;
fn checked_add(&self, other: u32) -> Option<Self>;
}
macro_rules! impl_helper {
($($t:ty)*) => ($(impl ReadNumberHelper for $t {
const ZERO: Self = 0;
#[inline]
fn checked_mul(&self, other: u32) -> Option<Self> {
Self::checked_mul(*self, other.try_into().ok()?)
}
#[inline]
fn checked_add(&self, other: u32) -> Option<Self> {
Self::checked_add(*self, other.try_into().ok()?)
}
})*)
}
impl_helper! { u8 u16 u32 }
struct Parser<'a> {
state: &'a [u8],
}
impl<'a> Parser<'a> {
fn new(input: &'a [u8]) -> Parser<'a> {
Parser { state: input }
}
fn read_atomically<T, F>(&mut self, inner: F) -> Option<T>
where
F: FnOnce(&mut Parser<'_>) -> Option<T>,
{
let state = self.state;
let result = inner(self);
if result.is_none() {
self.state = state;
}
result
}
fn parse_with<T, F>(&mut self, inner: F, kind: AddrKind) -> Result<T, AddrParseError>
where
F: FnOnce(&mut Parser<'_>) -> Option<T>,
{
let result = inner(self);
if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(kind))
}
fn peek_char(&self) -> Option<char> {
self.state.first().map(|&b| char::from(b))
}
fn read_char(&mut self) -> Option<char> {
self.state.split_first().map(|(&b, tail)| {
self.state = tail;
char::from(b)
})
}
#[must_use]
fn read_given_char(&mut self, target: char) -> Option<()> {
self.read_atomically(|p| {
p.read_char().and_then(|c| if c == target { Some(()) } else { None })
})
}
fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T>
where
F: FnOnce(&mut Parser<'_>) -> Option<T>,
{
self.read_atomically(move |p| {
if index > 0 {
p.read_given_char(sep)?;
}
inner(p)
})
}
fn read_number<T: ReadNumberHelper>(
&mut self,
radix: u32,
max_digits: Option<usize>,
allow_zero_prefix: bool,
) -> Option<T> {
self.read_atomically(move |p| {
let mut result = T::ZERO;
let mut digit_count = 0;
let has_leading_zero = p.peek_char() == Some('0');
while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
result = result.checked_mul(radix)?;
result = result.checked_add(digit)?;
digit_count += 1;
if let Some(max_digits) = max_digits {
if digit_count > max_digits {
return None;
}
}
}
if digit_count == 0 {
None
} else if !allow_zero_prefix && has_leading_zero && digit_count > 1 {
None
} else {
Some(result)
}
})
}
fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> {
self.read_atomically(|p| {
let mut groups = [0; 4];
for (i, slot) in groups.iter_mut().enumerate() {
*slot = p.read_separator('.', i, |p| {
p.read_number(10, Some(3), false)
})?;
}
Some(groups.into())
})
}
fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> {
fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) {
let limit = groups.len();
for (i, slot) in groups.iter_mut().enumerate() {
if i < limit - 1 {
let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr());
if let Some(v4_addr) = ipv4 {
let [one, two, three, four] = v4_addr.octets();
groups[i + 0] = u16::from_be_bytes([one, two]);
groups[i + 1] = u16::from_be_bytes([three, four]);
return (i + 2, true);
}
}
let group = p.read_separator(':', i, |p| p.read_number(16, Some(4), true));
match group {
Some(g) => *slot = g,
None => return (i, false),
}
}
(groups.len(), false)
}
self.read_atomically(|p| {
let mut head = [0; 8];
let (head_size, head_ipv4) = read_groups(p, &mut head);
if head_size == 8 {
return Some(head.into());
}
if head_ipv4 {
return None;
}
p.read_given_char(':')?;
p.read_given_char(':')?;
let mut tail = [0; 7];
let limit = 8 - (head_size + 1);
let (tail_size, _) = read_groups(p, &mut tail[..limit]);
head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]);
Some(head.into())
})
}
fn read_ip_addr(&mut self) -> Option<IpAddr> {
self.read_ipv4_addr().map(IpAddr::V4).or_else(move || self.read_ipv6_addr().map(IpAddr::V6))
}
fn read_port(&mut self) -> Option<u16> {
self.read_atomically(|p| {
p.read_given_char(':')?;
p.read_number(10, None, true)
})
}
fn read_scope_id(&mut self) -> Option<u32> {
self.read_atomically(|p| {
p.read_given_char('%')?;
p.read_number(10, None, true)
})
}
fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> {
self.read_atomically(|p| {
let ip = p.read_ipv4_addr()?;
let port = p.read_port()?;
Some(SocketAddrV4::new(ip, port))
})
}
fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> {
self.read_atomically(|p| {
p.read_given_char('[')?;
let ip = p.read_ipv6_addr()?;
let scope_id = p.read_scope_id().unwrap_or(0);
p.read_given_char(']')?;
let port = p.read_port()?;
Some(SocketAddrV6::new(ip, port, 0, scope_id))
})
}
fn read_socket_addr(&mut self) -> Option<SocketAddr> {
self.read_socket_addr_v4()
.map(SocketAddr::V4)
.or_else(|| self.read_socket_addr_v6().map(SocketAddr::V6))
}
}
impl IpAddr {
#[unstable(feature = "addr_parse_ascii", issue = "101035")]
pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
Parser::new(b).parse_with(|p| p.read_ip_addr(), AddrKind::Ip)
}
}
#[stable(feature = "ip_addr", since = "1.7.0")]
impl FromStr for IpAddr {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
Self::parse_ascii(s.as_bytes())
}
}
impl Ipv4Addr {
#[unstable(feature = "addr_parse_ascii", issue = "101035")]
pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
if b.len() > 15 {
Err(AddrParseError(AddrKind::Ipv4))
} else {
Parser::new(b).parse_with(|p| p.read_ipv4_addr(), AddrKind::Ipv4)
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for Ipv4Addr {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> {
Self::parse_ascii(s.as_bytes())
}
}
impl Ipv6Addr {
#[unstable(feature = "addr_parse_ascii", issue = "101035")]
pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
Parser::new(b).parse_with(|p| p.read_ipv6_addr(), AddrKind::Ipv6)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for Ipv6Addr {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> {
Self::parse_ascii(s.as_bytes())
}
}
impl SocketAddrV4 {
#[unstable(feature = "addr_parse_ascii", issue = "101035")]
pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
Parser::new(b).parse_with(|p| p.read_socket_addr_v4(), AddrKind::SocketV4)
}
}
#[stable(feature = "socket_addr_from_str", since = "1.5.0")]
impl FromStr for SocketAddrV4 {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> {
Self::parse_ascii(s.as_bytes())
}
}
impl SocketAddrV6 {
#[unstable(feature = "addr_parse_ascii", issue = "101035")]
pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
Parser::new(b).parse_with(|p| p.read_socket_addr_v6(), AddrKind::SocketV6)
}
}
#[stable(feature = "socket_addr_from_str", since = "1.5.0")]
impl FromStr for SocketAddrV6 {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> {
Self::parse_ascii(s.as_bytes())
}
}
impl SocketAddr {
#[unstable(feature = "addr_parse_ascii", issue = "101035")]
pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
Parser::new(b).parse_with(|p| p.read_socket_addr(), AddrKind::Socket)
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for SocketAddr {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> {
Self::parse_ascii(s.as_bytes())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum AddrKind {
Ip,
Ipv4,
Ipv6,
Socket,
SocketV4,
SocketV6,
}
#[stable(feature = "rust1", since = "1.0.0")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddrParseError(AddrKind);
#[stable(feature = "addr_parse_error_error", since = "1.4.0")]
impl fmt::Display for AddrParseError {
#[allow(deprecated, deprecated_in_future)]
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str(self.description())
}
}
#[stable(feature = "addr_parse_error_error", since = "1.4.0")]
impl Error for AddrParseError {
#[allow(deprecated)]
fn description(&self) -> &str {
match self.0 {
AddrKind::Ip => "invalid IP address syntax",
AddrKind::Ipv4 => "invalid IPv4 address syntax",
AddrKind::Ipv6 => "invalid IPv6 address syntax",
AddrKind::Socket => "invalid socket address syntax",
AddrKind::SocketV4 => "invalid IPv4 socket address syntax",
AddrKind::SocketV6 => "invalid IPv6 socket address syntax",
}
}
}