Skip to content

Commit ad6539b

Browse files
authored
Inline the utf-8 crate (#536)
* Inline the utf-8 crate Fixes #333 * clippy * rustfmt nightly
1 parent df722bd commit ad6539b

4 files changed

Lines changed: 158 additions & 7 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ rand = "0.9.0"
3737
sha1 = { version = "0.10", optional = true }
3838
thiserror = "2.0.7"
3939
url = { version = "2.1.0", optional = true }
40-
utf-8 = "0.7.5"
4140

4241
[dependencies.native-tls-crate]
4342
optional = true

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod server;
3030
pub mod stream;
3131
#[cfg(all(any(feature = "native-tls", feature = "__rustls-tls"), feature = "handshake"))]
3232
mod tls;
33+
mod utf8;
3334
pub mod util;
3435

3536
const READ_BUFFER_CHUNK_SIZE: usize = 4096;

src/protocol/message.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use crate::{
66
use std::{fmt, result::Result as StdResult, str};
77

88
mod string_collect {
9-
use utf8::DecodeError;
9+
use crate::utf8::DecodeError;
1010

1111
use crate::error::{Error, Result};
1212

1313
#[derive(Debug)]
1414
pub struct StringCollector {
1515
data: String,
16-
incomplete: Option<utf8::Incomplete>,
16+
incomplete: Option<crate::utf8::Incomplete>,
1717
}
1818

1919
impl StringCollector {
@@ -31,9 +31,9 @@ mod string_collect {
3131
let mut input: &[u8] = tail.as_ref();
3232

3333
if let Some(mut incomplete) = self.incomplete.take() {
34-
if let Some((result, rest)) = incomplete.try_complete(input) {
35-
input = rest;
36-
match result {
34+
if let Some(completed) = incomplete.try_complete(input) {
35+
input = completed.remaining_input;
36+
match completed.result {
3737
Ok(text) => self.data.push_str(text),
3838
Err(result_bytes) => {
3939
return Err(Error::Utf8(String::from_utf8_lossy(result_bytes).into()))
@@ -46,7 +46,7 @@ mod string_collect {
4646
}
4747

4848
if !input.is_empty() {
49-
match utf8::decode(input) {
49+
match crate::utf8::decode(input) {
5050
Ok(text) => {
5151
self.data.push_str(text);
5252
Ok(())

src/utf8.rs

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
use std::{cmp, error::Error, fmt, str};
2+
3+
#[derive(Debug, Copy, Clone)]
4+
pub(crate) enum DecodeError<'a> {
5+
/// In lossy decoding insert `valid_prefix`, then `"\u{FFFD}"`,
6+
/// then call `decode()` again with `remaining_input`.
7+
Invalid { valid_prefix: &'a str, invalid_sequence: &'a [u8], remaining_input: &'a [u8] },
8+
9+
/// Call the `incomplete_suffix.try_complete` method with more input when available.
10+
/// If no more input is available, this is an invalid byte sequence.
11+
Incomplete { valid_prefix: &'a str, incomplete_suffix: Incomplete },
12+
}
13+
14+
impl<'a> fmt::Display for DecodeError<'a> {
15+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16+
match *self {
17+
DecodeError::Invalid { valid_prefix, invalid_sequence, remaining_input } => write!(
18+
f,
19+
"found invalid byte sequence {invalid_sequence:02x?} after \
20+
{valid_byte_count} valid bytes, followed by {unprocessed_byte_count} more \
21+
unprocessed bytes",
22+
invalid_sequence = invalid_sequence,
23+
valid_byte_count = valid_prefix.len(),
24+
unprocessed_byte_count = remaining_input.len()
25+
),
26+
DecodeError::Incomplete { valid_prefix, incomplete_suffix } => write!(
27+
f,
28+
"found incomplete byte sequence {incomplete_suffix:02x?} after \
29+
{valid_byte_count} bytes",
30+
incomplete_suffix = incomplete_suffix,
31+
valid_byte_count = valid_prefix.len()
32+
),
33+
}
34+
}
35+
}
36+
37+
impl<'a> Error for DecodeError<'a> {}
38+
39+
#[derive(Debug, Copy, Clone)]
40+
pub(crate) struct Incomplete {
41+
pub(crate) buffer: [u8; 4],
42+
pub(crate) buffer_len: u8,
43+
}
44+
45+
#[derive(Debug, Copy, Clone)]
46+
pub(crate) struct Completed<'buffer, 'input> {
47+
pub(crate) result: Result<&'buffer str, &'buffer [u8]>,
48+
pub(crate) remaining_input: &'input [u8],
49+
}
50+
51+
pub(crate) fn decode(input: &'_ [u8]) -> Result<&'_ str, DecodeError<'_>> {
52+
let error = match str::from_utf8(input) {
53+
Ok(valid) => return Ok(valid),
54+
Err(error) => error,
55+
};
56+
57+
// FIXME: separate function from here to guide inlining?
58+
let (valid, after_valid) = input.split_at(error.valid_up_to());
59+
let valid = unsafe { str::from_utf8_unchecked(valid) };
60+
61+
match error.error_len() {
62+
Some(invalid_sequence_length) => {
63+
let (invalid, rest) = after_valid.split_at(invalid_sequence_length);
64+
Err(DecodeError::Invalid {
65+
valid_prefix: valid,
66+
invalid_sequence: invalid,
67+
remaining_input: rest,
68+
})
69+
}
70+
None => Err(DecodeError::Incomplete {
71+
valid_prefix: valid,
72+
incomplete_suffix: Incomplete::new(after_valid),
73+
}),
74+
}
75+
}
76+
77+
impl Incomplete {
78+
pub(crate) fn new(bytes: &[u8]) -> Self {
79+
let mut buffer = [0, 0, 0, 0];
80+
let len = bytes.len();
81+
buffer[..len].copy_from_slice(bytes);
82+
Incomplete { buffer, buffer_len: len as u8 }
83+
}
84+
85+
/// * `None`: still incomplete, call `try_complete` again with more input.
86+
/// If no more input is available, this is invalid byte sequence.
87+
/// * `Some(completed)`: We’re done with this `Incomplete`,
88+
/// with either a valid chunk on invalid byte sequence in `completed.result`.
89+
/// To keep decoding, pass `completed.remaining_input` to `decode()`.
90+
pub(crate) fn try_complete<'input>(
91+
&mut self,
92+
input: &'input [u8],
93+
) -> Option<Completed<'_, 'input>> {
94+
let (consumed, opt_result) = self.try_complete_offsets(input);
95+
let result = opt_result?;
96+
let remaining_input = &input[consumed..];
97+
let result_bytes = self.take_buffer();
98+
let result = match result {
99+
Ok(()) => Ok(unsafe { str::from_utf8_unchecked(result_bytes) }),
100+
Err(()) => Err(result_bytes),
101+
};
102+
Some(Completed { result, remaining_input })
103+
}
104+
105+
fn take_buffer(&mut self) -> &[u8] {
106+
let len = self.buffer_len as usize;
107+
self.buffer_len = 0;
108+
&self.buffer[..len]
109+
}
110+
111+
/// (consumed_from_input, None): not enough input
112+
/// (consumed_from_input, Some(Err(()))): error bytes in buffer
113+
/// (consumed_from_input, Some(Ok(()))): UTF-8 string in buffer
114+
fn try_complete_offsets(&mut self, input: &[u8]) -> (usize, Option<Result<(), ()>>) {
115+
let initial_buffer_len = self.buffer_len as usize;
116+
let copied_from_input;
117+
{
118+
let unwritten = &mut self.buffer[initial_buffer_len..];
119+
copied_from_input = cmp::min(unwritten.len(), input.len());
120+
unwritten[..copied_from_input].copy_from_slice(&input[..copied_from_input]);
121+
}
122+
let spliced = &self.buffer[..initial_buffer_len + copied_from_input];
123+
match str::from_utf8(spliced) {
124+
Ok(_) => {
125+
self.buffer_len = spliced.len() as u8;
126+
(copied_from_input, Some(Ok(())))
127+
}
128+
Err(error) => {
129+
let valid_up_to = error.valid_up_to();
130+
if valid_up_to > 0 {
131+
let consumed = valid_up_to.checked_sub(initial_buffer_len).unwrap();
132+
self.buffer_len = valid_up_to as u8;
133+
(consumed, Some(Ok(())))
134+
} else {
135+
match error.error_len() {
136+
Some(invalid_sequence_length) => {
137+
let consumed =
138+
invalid_sequence_length.checked_sub(initial_buffer_len).unwrap();
139+
self.buffer_len = invalid_sequence_length as u8;
140+
(consumed, Some(Err(())))
141+
}
142+
None => {
143+
self.buffer_len = spliced.len() as u8;
144+
(copied_from_input, None)
145+
}
146+
}
147+
}
148+
}
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)