mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-30 00:01:25 +00:00
Changes to pass Miri testing.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
|
||||
#### Conformed RFCs
|
||||
### Conformed RFCs
|
||||
-------------
|
||||
- [RFC 822 - Standard for ARPA Internet Text Messages](https://datatracker.ietf.org/doc/html/rfc822)
|
||||
- [RFC 5322 - Internet Message Format](https://datatracker.ietf.org/doc/html/rfc5322)
|
||||
@@ -15,8 +15,18 @@
|
||||
- [RFC 2369 - The Use of URLs as Meta-Syntax for Core Mail List Commands and their Transport through Message Header Fields](https://datatracker.ietf.org/doc/html/rfc2369)
|
||||
- [RFC 2919 - List-Id: A Structured Field and Namespace for the Identification of Mailing Lists](https://datatracker.ietf.org/doc/html/rfc2919)
|
||||
- [RFC 8621 - The JSON Meta Application Protocol (JMAP) for Mail (Section 4.1.4)](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4)
|
||||
#### TODO
|
||||
--------------
|
||||
- UTF-7 support
|
||||
- Respect attribute orders in continuations
|
||||
|
||||
### Supported Character Sets
|
||||
-------------
|
||||
- UTF-8
|
||||
- UTF-16, UTF-16BE, UTF-16LE
|
||||
- UTF-7
|
||||
- US-ASCII
|
||||
- ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16
|
||||
- CP1250, CP1251, CP1252, CP1253, CP1254, CP1255, CP1256, CP1257, CP1258
|
||||
- KOI8-R, KOI8_U
|
||||
- MACINTOSH
|
||||
- IBM850
|
||||
- TIS-620
|
||||
- SHIFT_JIS, BIG5, EUC-JP, EUC-KR, GB18030, GBK, ISO-2022-JP, WINDOWS-874, IBM-866 (supported via the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs))
|
||||
|
||||
|
||||
+43
-8
@@ -21,6 +21,14 @@ pub trait Base64Decoder<'x> {
|
||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, bool, Option<&'x [u8]>);
|
||||
}
|
||||
|
||||
// Miri does not play well with UnsafeCells, see https://github.com/rust-lang/miri/issues/1665
|
||||
//
|
||||
// When testing under Miri,
|
||||
// (*data)[i] = val;
|
||||
// is used instead of
|
||||
// *(*data).get_unchecked_mut(i) = val;
|
||||
//
|
||||
|
||||
impl<'x> Base64Decoder<'x> for MessageStream<'x> {
|
||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, bool, Option<&'x [u8]>) {
|
||||
unsafe {
|
||||
@@ -69,15 +77,32 @@ impl<'x> Base64Decoder<'x> for MessageStream<'x> {
|
||||
if *ch == b'=' {
|
||||
match byte_count {
|
||||
1 | 2 => {
|
||||
*((*data).get_unchecked_mut(write_pos)) =
|
||||
*chunk.bytes.get_unchecked(0);
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = *chunk.bytes.get_unchecked(0);
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) =
|
||||
*chunk.bytes.get_unchecked(0);
|
||||
}
|
||||
|
||||
write_pos += 1;
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
(*data)
|
||||
.get_unchecked_mut(write_pos..write_pos + 2)
|
||||
.copy_from_slice(chunk.bytes.get_unchecked(0..2));
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = *chunk.bytes.get_unchecked(0);
|
||||
(*data)[write_pos + 1] = *chunk.bytes.get_unchecked(1);
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
(*data)
|
||||
.get_unchecked_mut(write_pos..write_pos + 2)
|
||||
.copy_from_slice(chunk.bytes.get_unchecked(0..2));
|
||||
}
|
||||
|
||||
write_pos += 2;
|
||||
byte_count = 0;
|
||||
}
|
||||
@@ -102,9 +127,19 @@ impl<'x> Base64Decoder<'x> for MessageStream<'x> {
|
||||
chunk.val |= *val;
|
||||
|
||||
if byte_count == 0 {
|
||||
(*data)
|
||||
.get_unchecked_mut(write_pos..write_pos + 3)
|
||||
.copy_from_slice(chunk.bytes.get_unchecked(0..3));
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = *chunk.bytes.get_unchecked(0);
|
||||
(*data)[write_pos + 1] = *chunk.bytes.get_unchecked(1);
|
||||
(*data)[write_pos + 2] = *chunk.bytes.get_unchecked(2);
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
(*data)
|
||||
.get_unchecked_mut(write_pos..write_pos + 3)
|
||||
.copy_from_slice(chunk.bytes.get_unchecked(0..3));
|
||||
}
|
||||
|
||||
write_pos += 3;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,15 +136,11 @@ pub fn decoder_utf16_be(bytes: &[u8]) -> Cow<str> {
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn decoder_utf16(bytes: &[u8]) -> Cow<str> {
|
||||
let (bytes, fnc): (&[u8], fn([u8; 2]) -> u16) = if bytes.len() >= 4 {
|
||||
// Read BOM
|
||||
match bytes[0..2] {
|
||||
[0xfe, 0xff] => (&bytes[2..], u16::from_be_bytes),
|
||||
[0xff, 0xfe] => (&bytes[2..], u16::from_le_bytes),
|
||||
_ => (bytes, u16::from_le_bytes),
|
||||
}
|
||||
} else {
|
||||
(bytes, u16::from_le_bytes)
|
||||
// Read BOM
|
||||
let (bytes, fnc): (&[u8], fn([u8; 2]) -> u16) = match bytes.get(0..2) {
|
||||
Some([0xfe, 0xff]) => (bytes.get(2..).unwrap_or(&[]), u16::from_be_bytes),
|
||||
Some([0xff, 0xfe]) => (bytes.get(2..).unwrap_or(&[]), u16::from_le_bytes),
|
||||
_ => (bytes, u16::from_le_bytes),
|
||||
};
|
||||
|
||||
decoder_utf16_(bytes, fnc)
|
||||
|
||||
@@ -26,6 +26,14 @@ pub trait QuotedPrintableDecoder<'x> {
|
||||
) -> (bool, bool, Option<&'x [u8]>);
|
||||
}
|
||||
|
||||
// Miri does not play well with UnsafeCells, see https://github.com/rust-lang/miri/issues/1665
|
||||
//
|
||||
// When testing under Miri,
|
||||
// (*data)[i] = val;
|
||||
// is used instead of
|
||||
// *(*data).get_unchecked_mut(i) = val;
|
||||
//
|
||||
|
||||
impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
fn decode_quoted_printable(
|
||||
&self,
|
||||
@@ -36,7 +44,7 @@ impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
let mut success = boundary.is_empty();
|
||||
let mut is_utf8_safe = true;
|
||||
|
||||
let data = &mut *self.data.get();
|
||||
let mut data = &mut *self.data.get();
|
||||
let data_len = (*data).len();
|
||||
|
||||
let stream_pos = &mut *self.pos.get();
|
||||
@@ -56,7 +64,10 @@ impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
if ch == *boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
if is_word || self.is_boundary_end(read_pos) {
|
||||
let done = is_word || self.is_boundary_end(read_pos);
|
||||
data = &mut *self.data.get(); // Avoid violating the Stacked Borrows rules
|
||||
|
||||
if done {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
@@ -68,7 +79,14 @@ impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
if match_count > 0 {
|
||||
for ch in boundary[..match_count].iter() {
|
||||
if *ch != b'\n' || QuotedPrintableState::Eq != state {
|
||||
*((*data).get_unchecked_mut(write_pos)) = *ch;
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = *ch;
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = *ch;
|
||||
}
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && *ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
@@ -102,18 +120,40 @@ impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
} else if QuotedPrintableState::Eq == state {
|
||||
state = QuotedPrintableState::None;
|
||||
} else {
|
||||
*((*data).get_unchecked_mut(write_pos)) = b'\n';
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = b'\n';
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = b'\n';
|
||||
}
|
||||
|
||||
write_pos += 1;
|
||||
}
|
||||
}
|
||||
b'_' if is_word => {
|
||||
*((*data).get_unchecked_mut(write_pos)) = b' ';
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = b' ';
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = b' ';
|
||||
}
|
||||
write_pos += 1;
|
||||
}
|
||||
b'\r' => (),
|
||||
_ => match state {
|
||||
QuotedPrintableState::None => {
|
||||
*((*data).get_unchecked_mut(write_pos)) = ch;
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = ch;
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = ch;
|
||||
}
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
@@ -134,7 +174,16 @@ impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
state = QuotedPrintableState::None;
|
||||
if hex2 != -1 {
|
||||
let ch = ((hex1 as u8) << 4) | hex2 as u8;
|
||||
*((*data).get_unchecked_mut(write_pos)) = ch;
|
||||
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = ch;
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = ch;
|
||||
}
|
||||
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
|
||||
@@ -123,7 +123,7 @@ impl<'x> MessageStream<'x> {
|
||||
|
||||
pub fn get_bytes_to_boundary(&self, boundary: &[u8]) -> (bool, bool, Option<&'x [u8]>) {
|
||||
unsafe {
|
||||
let data = &mut *self.data.get();
|
||||
let mut data = &mut *self.data.get();
|
||||
|
||||
let stream_pos = &mut *self.pos.get();
|
||||
let start_pos = *stream_pos;
|
||||
@@ -143,7 +143,10 @@ impl<'x> MessageStream<'x> {
|
||||
if ch == boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
if self.is_boundary_end(pos) {
|
||||
let is_boundary_end = self.is_boundary_end(pos);
|
||||
data = &mut *self.data.get(); // Avoid violating the Stacked Borrows rules
|
||||
|
||||
if is_boundary_end {
|
||||
let match_pos = pos - match_count;
|
||||
*stream_pos = pos;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user