Debian Codegraph @

src/parser.rs

raw symbols SCIP
1 // Effectively all the code in this repo is copied with permission from Rust's std library.
2 // They hold the copyright (http://rust-lang.org/COPYRIGHT) and whatever other rights, but this
3 // crate is MIT licensed also, so it's all good.
4
5 //! A private parser implementation of IPv4, IPv6, and socket addresses.
6 //!
7 //! This module is "publicly exported" through the `FromStr` implementations
8 //! below.
9
10 #[cfg(test)]
11 mod tests;
12
13 use core::convert::TryInto;
14 use core::fmt;
15 use core::str::FromStr;
16
17 use super::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
18
19 trait ReadNumberHelper: Sized {
20 const ZERO: Self;
21 fn checked_mul(&self, other: u32) -> Option<Self>;
22 fn checked_add(&self, other: u32) -> Option<Self>;
23 }
24
25 macro_rules! impl_helper {
26 ($($t:ty)*) => ($(impl ReadNumberHelper for $t {
27 const ZERO: Self = 0;
28 #[inline]
29 fn checked_mul(&self, other: u32) -> Option<Self> {
30 Self::checked_mul(*self, other.try_into().ok()?)
31 }
32 #[inline]
33 fn checked_add(&self, other: u32) -> Option<Self> {
34 Self::checked_add(*self, other.try_into().ok()?)
35 }
36 })*)
37 }
38
39 impl_helper! { u8 u16 u32 }
40
41 struct Parser<'a> {
42 // Parsing as ASCII, so can use byte array.
43 state: &'a [u8],
44 }
45
46 impl<'a> Parser<'a> {
47 fn new(input: &'a str) -> Parser<'a> {
48 Parser {
49 state: input.as_bytes(),
50 }
51 }
52
53 /// Run a parser, and restore the pre-parse state if it fails.
54 fn read_atomically<T, F>(&mut self, inner: F) -> Option<T>
55 where
56 F: FnOnce(&mut Parser<'_>) -> Option<T>,
57 {
58 let state = self.state;
59 let result = inner(self);
60 if result.is_none() {
61 self.state = state;
62 }
63 result
64 }
65
66 /// Run a parser, but fail if the entire input wasn't consumed.
67 /// Doesn't run atomically.
68 fn parse_with<T, F>(&mut self, inner: F) -> Result<T, AddrParseError>
69 where
70 F: FnOnce(&mut Parser<'_>) -> Option<T>,
71 {
72 let result = inner(self);
73 if self.state.is_empty() { result } else { None }.ok_or(AddrParseError(()))
74 }
75
76 /// Peek the next character from the input
77 fn peek_char(&self) -> Option<char> {
78 self.state.first().map(|&b| char::from(b))
79 }
80
81 /// Read the next character from the input
82 fn read_char(&mut self) -> Option<char> {
83 self.state.split_first().map(|(&b, tail)| {
84 self.state = tail;
85 char::from(b)
86 })
87 }
88
89 #[must_use]
90 /// Read the next character from the input if it matches the target.
91 fn read_given_char(&mut self, target: char) -> Option<()> {
92 self.read_atomically(|p| {
93 p.read_char()
94 .and_then(|c| if c == target { Some(()) } else { None })
95 })
96 }
97
98 /// Helper for reading separators in an indexed loop. Reads the separator
99 /// character iff index > 0, then runs the parser. When used in a loop,
100 /// the separator character will only be read on index > 0 (see
101 /// read_ipv4_addr for an example)
102 fn read_separator<T, F>(&mut self, sep: char, index: usize, inner: F) -> Option<T>
103 where
104 F: FnOnce(&mut Parser<'_>) -> Option<T>,
105 {
106 self.read_atomically(move |p| {
107 if index > 0 {
108 p.read_given_char(sep)?;
109 }
110 inner(p)
111 })
112 }
113
114 // Read a number off the front of the input in the given radix, stopping
115 // at the first non-digit character or eof. Fails if the number has more
116 // digits than max_digits or if there is no number.
117 fn read_number<T: ReadNumberHelper>(
118 &mut self,
119 radix: u32,
120 max_digits: Option<usize>,
121 ) -> Option<T> {
122 self.read_atomically(move |p| {
123 let mut result = T::ZERO;
124 let mut digit_count = 0;
125
126 while let Some(digit) = p.read_atomically(|p| p.read_char()?.to_digit(radix)) {
127 result = result.checked_mul(radix)?;
128 result = result.checked_add(digit)?;
129 digit_count += 1;
130 if let Some(max_digits) = max_digits {
131 if digit_count > max_digits {
132 return None;
133 }
134 }
135 }
136
137 if digit_count == 0 {
138 None
139 } else {
140 Some(result)
141 }
142 })
143 }
144
145 /// Read an IPv4 address.
146 fn read_ipv4_addr(&mut self) -> Option<Ipv4Addr> {
147 self.read_atomically(|p| {
148 let mut groups = [0; 4];
149
150 for (i, slot) in groups.iter_mut().enumerate() {
151 *slot = p.read_separator('.', i, |p| {
152 // Disallow octal number in IP string.
153 // https://tools.ietf.org/html/rfc6943#section-3.1.1
154 match (p.peek_char(), p.read_number(10, None)) {
155 (Some('0'), Some(number)) if number != 0 => None,
156 (_, number) => number,
157 }
158 })?;
159 }
160
161 Some(groups.into())
162 })
163 }
164
165 /// Read an IPv6 Address.
166 fn read_ipv6_addr(&mut self) -> Option<Ipv6Addr> {
167 /// Read a chunk of an IPv6 address into `groups`. Returns the number
168 /// of groups read, along with a bool indicating if an embedded
169 /// trailing IPv4 address was read. Specifically, read a series of
170 /// colon-separated IPv6 groups (0x0000 - 0xFFFF), with an optional
171 /// trailing embedded IPv4 address.
172 fn read_groups(p: &mut Parser<'_>, groups: &mut [u16]) -> (usize, bool) {
173 let limit = groups.len();
174
175 for (i, slot) in groups.iter_mut().enumerate() {
176 // Try to read a trailing embedded IPv4 address. There must be
177 // at least two groups left.
178 if i < limit - 1 {
179 let ipv4 = p.read_separator(':', i, |p| p.read_ipv4_addr());
180
181 if let Some(v4_addr) = ipv4 {
182 let [one, two, three, four] = v4_addr.octets();
183 groups[i + 0] = u16::from_be_bytes([one, two]);
184 groups[i + 1] = u16::from_be_bytes([three, four]);
185 return (i + 2, true);
186 }
187 }
188
189 let group = p.read_separator(':', i, |p| p.read_number(16, Some(4)));
190
191 match group {
192 Some(g) => *slot = g,
193 None => return (i, false),
194 }
195 }
196 (groups.len(), false)
197 }
198
199 self.read_atomically(|p| {
200 // Read the front part of the address; either the whole thing, or up
201 // to the first ::
202 let mut head = [0; 8];
203 let (head_size, head_ipv4) = read_groups(p, &mut head);
204
205 if head_size == 8 {
206 return Some(head.into());
207 }
208
209 // IPv4 part is not allowed before `::`
210 if head_ipv4 {
211 return None;
212 }
213
214 // Read `::` if previous code parsed less than 8 groups.
215 // `::` indicates one or more groups of 16 bits of zeros.
216 p.read_given_char(':')?;
217 p.read_given_char(':')?;
218
219 // Read the back part of the address. The :: must contain at least one
220 // set of zeroes, so our max length is 7.
221 let mut tail = [0; 7];
222 let limit = 8 - (head_size + 1);
223 let (tail_size, _) = read_groups(p, &mut tail[..limit]);
224
225 // Concat the head and tail of the IP address
226 head[(8 - tail_size)..8].copy_from_slice(&tail[..tail_size]);
227
228 Some(head.into())
229 })
230 }
231
232 /// Read an IP Address, either IPv4 or IPv6.
233 fn read_ip_addr(&mut self) -> Option<IpAddr> {
234 self.read_ipv4_addr()
235 .map(IpAddr::V4)
236 .or_else(move || self.read_ipv6_addr().map(IpAddr::V6))
237 }
238
239 /// Read a `:` followed by a port in base 10.
240 fn read_port(&mut self) -> Option<u16> {
241 self.read_atomically(|p| {
242 p.read_given_char(':')?;
243 p.read_number(10, None)
244 })
245 }
246
247 /// Read a `%` followed by a scope ID in base 10.
248 fn read_scope_id(&mut self) -> Option<u32> {
249 self.read_atomically(|p| {
250 p.read_given_char('%')?;
251 p.read_number(10, None)
252 })
253 }
254
255 /// Read an IPv4 address with a port.
256 fn read_socket_addr_v4(&mut self) -> Option<SocketAddrV4> {
257 self.read_atomically(|p| {
258 let ip = p.read_ipv4_addr()?;
259 let port = p.read_port()?;
260 Some(SocketAddrV4::new(ip, port))
261 })
262 }
263
264 /// Read an IPv6 address with a port.
265 fn read_socket_addr_v6(&mut self) -> Option<SocketAddrV6> {
266 self.read_atomically(|p| {
267 p.read_given_char('[')?;
268 let ip = p.read_ipv6_addr()?;
269 let scope_id = p.read_scope_id().unwrap_or(0);
270 p.read_given_char(']')?;
271
272 let port = p.read_port()?;
273 Some(SocketAddrV6::new(ip, port, 0, scope_id))
274 })
275 }
276
277 /// Read an IP address with a port
278 fn read_socket_addr(&mut self) -> Option<SocketAddr> {
279 self.read_socket_addr_v4()
280 .map(SocketAddr::V4)
281 .or_else(|| self.read_socket_addr_v6().map(SocketAddr::V6))
282 }
283 }
284
285 impl FromStr for IpAddr {
286 type Err = AddrParseError;
287 fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
288 Parser::new(s).parse_with(|p| p.read_ip_addr())
289 }
290 }
291
292 impl FromStr for Ipv4Addr {
293 type Err = AddrParseError;
294 fn from_str(s: &str) -> Result<Ipv4Addr, AddrParseError> {
295 Parser::new(s).parse_with(|p| p.read_ipv4_addr())
296 }
297 }
298
299 impl FromStr for Ipv6Addr {
300 type Err = AddrParseError;
301 fn from_str(s: &str) -> Result<Ipv6Addr, AddrParseError> {
302 Parser::new(s).parse_with(|p| p.read_ipv6_addr())
303 }
304 }
305
306 impl FromStr for SocketAddrV4 {
307 type Err = AddrParseError;
308 fn from_str(s: &str) -> Result<SocketAddrV4, AddrParseError> {
309 Parser::new(s).parse_with(|p| p.read_socket_addr_v4())
310 }
311 }
312
313 impl FromStr for SocketAddrV6 {
314 type Err = AddrParseError;
315 fn from_str(s: &str) -> Result<SocketAddrV6, AddrParseError> {
316 Parser::new(s).parse_with(|p| p.read_socket_addr_v6())
317 }
318 }
319
320 impl FromStr for SocketAddr {
321 type Err = AddrParseError;
322 fn from_str(s: &str) -> Result<SocketAddr, AddrParseError> {
323 Parser::new(s).parse_with(|p| p.read_socket_addr())
324 }
325 }
326
327 /// An error which can be returned when parsing an IP address or a socket address.
328 ///
329 /// This error is used as the error type for the [`FromStr`] implementation for
330 /// [`IpAddr`], [`Ipv4Addr`], [`Ipv6Addr`], [`SocketAddr`], [`SocketAddrV4`], and
331 /// [`SocketAddrV6`].
332 ///
333 /// # Potential causes
334 ///
335 /// `AddrParseError` may be thrown because the provided string does not parse as the given type,
336 /// often because it includes information only handled by a different address type.
337 ///
338 /// ```should_panic
339 /// use no_std_net::IpAddr;
340 /// let _foo: IpAddr = "127.0.0.1:8080".parse().expect("Cannot handle the socket port");
341 /// ```
342 ///
343 /// [`IpAddr`] doesn't handle the port. Use [`SocketAddr`] instead.
344 ///
345 /// ```
346 /// use no_std_net::SocketAddr;
347 ///
348 /// // No problem, the `panic!` message has disappeared.
349 /// let _foo: SocketAddr = "127.0.0.1:8080".parse().expect("unreachable panic");
350 /// ```
351 #[derive(Debug, Clone, PartialEq, Eq)]
352 pub struct AddrParseError(());
353
354 impl fmt::Display for AddrParseError {
355 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
356 fmt.write_str("invalid IP address syntax")
357 }
358 }