mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-29 00:01:49 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6b0f8052ee | ||
|
|
bfaeeeb886 | ||
|
|
514f989e7e | ||
|
|
bcf182bbc1 | ||
|
|
78c4cd424f | ||
|
|
4835ccfb9e | ||
|
|
c52670cf1f | ||
|
|
96d8123b74 | ||
|
|
6f98f52d77 |
Generated
+1
-1
@@ -59,7 +59,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.1.1"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"serde",
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "mail-parser"
|
||||
description = "Fast and robust e-mail parsing library for Rust"
|
||||
version = "0.1.1"
|
||||
version = "0.2.1"
|
||||
edition = "2018"
|
||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||
license = "Apache-2.0 OR MIT"
|
||||
|
||||
@@ -24,20 +24,18 @@ HTML and plain text inline body parts is done automatically when the _alternativ
|
||||
|
||||
Performance and memory safety were two important factors while designing _mail-parser_:
|
||||
|
||||
- **Zero-copy parsing** is done in most cases (unless when decoding non-UTF8 text or when RFC2047/RFC2231 encoded parts are present).
|
||||
Practically all strings and u8 slices returned by this library are `Cow<str>` or `Cow<[u8]>` references to the input raw message.
|
||||
- Memory allocations are always avoided unless they are really necessary. In fact, all Base64 and Quoted-Printable parts are decoded in
|
||||
place re-using the input buffer.
|
||||
- [Perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function) is used for fast look-up of message header fields, character
|
||||
set names and aliases, HTML entities as well as month names while parsing _Date_ fields.
|
||||
- Although some `unsafe` code was used to obtain performance gains of about 10%, every function in the library has been
|
||||
[fuzzed](#testing-fuzzing--benchmarking) and also heavily [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
- Fully battle-tested with millions of real-world e-mail messages dating from 1995 until today.
|
||||
- **Zero-copy**: Practically all strings returned by this library are `Cow<str>` references to the input raw message.
|
||||
- **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
|
||||
- **Fast parsing** of message header fields, character set names and HTML entities using [perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function).
|
||||
- Written in **100% safe** Rust with no external dependencies.
|
||||
- Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
|
||||
meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
- Thoroughly **battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
|
||||
|
||||
## Usage Example
|
||||
|
||||
```rust
|
||||
let mut input = concat!(
|
||||
let input = concat!(
|
||||
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||
@@ -72,10 +70,9 @@ Performance and memory safety were two important factors while designing _mail-p
|
||||
"--giddyup--\n",
|
||||
"--festivus--\n",
|
||||
)
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
.as_bytes();
|
||||
|
||||
let message = Message::parse(&mut input[..]);
|
||||
let message = Message::parse(input);
|
||||
|
||||
// Parses addresses (including comments), lists and groups
|
||||
assert_eq!(
|
||||
|
||||
@@ -17,7 +17,7 @@ use std::{fs, path::PathBuf};
|
||||
|
||||
use test::Bencher;
|
||||
|
||||
fn bench_all_samples(b: &mut Bencher, name: &str, fnc: fn(&mut [u8], &str)) {
|
||||
fn bench_all_samples(b: &mut Bencher, name: &str, fnc: fn(&[u8], &str)) {
|
||||
const SEPARATOR: &[u8] = "\n---- EXPECTED STRUCTURE ----\n".as_bytes();
|
||||
|
||||
println!("Benchmarking {}...\n", name);
|
||||
@@ -63,10 +63,7 @@ fn bench_all_samples(b: &mut Bencher, name: &str, fnc: fn(&mut [u8], &str)) {
|
||||
|
||||
b.iter(|| {
|
||||
for test_msg in &test_data {
|
||||
let input_str = String::from_utf8_lossy(&test_msg);
|
||||
let mut input_bytes = test_msg.clone();
|
||||
|
||||
fnc(&mut input_bytes[..], &input_str);
|
||||
fnc(test_msg, String::from_utf8_lossy(&test_msg).as_ref());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,18 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use mail_parser::{Addr, Address, BodyPart, Group, Message, MessagePart, MimeFieldGet};
|
||||
|
||||
fn main() {
|
||||
let mut input = concat!(
|
||||
let input = concat!(
|
||||
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||
@@ -36,10 +47,9 @@ fn main() {
|
||||
"--giddyup--\n",
|
||||
"--festivus--\n",
|
||||
)
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
.as_bytes();
|
||||
|
||||
let message = Message::parse(&mut input[..]);
|
||||
let message = Message::parse(input);
|
||||
|
||||
// Parses addresses (including comments), lists and groups
|
||||
assert_eq!(
|
||||
|
||||
Generated
+1
-1
@@ -42,7 +42,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.1.0"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"serde",
|
||||
|
||||
@@ -1,19 +1,18 @@
|
||||
#![no_main]
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
use std::borrow::Cow;
|
||||
|
||||
use mail_parser::{
|
||||
decoders::{
|
||||
base64::Base64Decoder,
|
||||
base64::decode_base64,
|
||||
charsets::{
|
||||
map::get_charset_decoder,
|
||||
single_byte::decoder_iso_8859_1,
|
||||
utf::{decoder_utf16, decoder_utf16_be, decoder_utf16_le, decoder_utf7},
|
||||
},
|
||||
encoded_word::parse_encoded_word,
|
||||
encoded_word::decode_rfc2047,
|
||||
hex::decode_hex,
|
||||
html::{add_html_token, html_to_text, text_to_html},
|
||||
quoted_printable::QuotedPrintableDecoder,
|
||||
quoted_printable::decode_quoted_printable,
|
||||
},
|
||||
parsers::{
|
||||
fields::{
|
||||
@@ -25,8 +24,9 @@ use mail_parser::{
|
||||
raw::{parse_and_ignore, parse_raw},
|
||||
unstructured::parse_unstructured,
|
||||
},
|
||||
mime::*,
|
||||
header::parse_header_name,
|
||||
message_stream::MessageStream,
|
||||
message::MessageStream,
|
||||
},
|
||||
Message,
|
||||
};
|
||||
@@ -34,81 +34,80 @@ use mail_parser::{
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
// Fuzz every parsing function
|
||||
for n_fuzz in 1..=24 {
|
||||
let mut data = Vec::from(data);
|
||||
let stream = MessageStream::new(&mut data);
|
||||
let mut stream = MessageStream::new(&data);
|
||||
|
||||
match n_fuzz {
|
||||
1 => {
|
||||
parse_date(&stream, false);
|
||||
parse_date(&mut stream);
|
||||
}
|
||||
2 => {
|
||||
parse_address(&stream);
|
||||
parse_address(&mut stream);
|
||||
}
|
||||
3 => {
|
||||
parse_id(&stream);
|
||||
parse_id(&mut stream);
|
||||
}
|
||||
4 => {
|
||||
parse_comma_separared(&stream);
|
||||
parse_comma_separared(&mut stream);
|
||||
}
|
||||
5 => {
|
||||
parse_and_ignore(&stream);
|
||||
parse_and_ignore(&mut stream);
|
||||
}
|
||||
6 => {
|
||||
parse_raw(&stream);
|
||||
parse_raw(&mut stream);
|
||||
}
|
||||
7 => {
|
||||
parse_unstructured(&stream);
|
||||
parse_unstructured(&mut stream);
|
||||
}
|
||||
8 => {
|
||||
parse_content_type(&stream);
|
||||
parse_content_type(&mut stream);
|
||||
}
|
||||
9 => {
|
||||
parse_header_name(&stream);
|
||||
parse_header_name(&mut stream);
|
||||
}
|
||||
10 => {
|
||||
parse_encoded_word(&stream);
|
||||
decode_rfc2047(&mut stream, 0);
|
||||
}
|
||||
11 => {
|
||||
stream.seek_next_part(b"\n");
|
||||
seek_next_part(&mut stream, b"\n");
|
||||
}
|
||||
12 => {
|
||||
stream.get_bytes_to_boundary(b"\n");
|
||||
get_bytes_to_boundary(&mut stream, 0, b"\n", false);
|
||||
}
|
||||
13 => {
|
||||
stream.get_bytes_to_boundary(&[]);
|
||||
get_bytes_to_boundary(&mut stream, 0, &[], false);
|
||||
}
|
||||
14 => {
|
||||
stream.skip_crlf();
|
||||
skip_crlf(&mut stream);
|
||||
}
|
||||
15 => {
|
||||
stream.is_boundary_end(0);
|
||||
is_boundary_end(&mut stream, 0);
|
||||
}
|
||||
16 => {
|
||||
stream.skip_multipart_end();
|
||||
skip_multipart_end(&mut stream);
|
||||
}
|
||||
17 => {
|
||||
stream.decode_base64(b"\n", true);
|
||||
decode_base64(&mut stream, 0, b"\n", true);
|
||||
}
|
||||
18 => {
|
||||
stream.decode_base64(b"\n", false);
|
||||
decode_base64(&mut stream, 0, b"\n", false);
|
||||
}
|
||||
19 => {
|
||||
stream.decode_base64(&[], true);
|
||||
decode_base64(&mut stream, 0, &[], true);
|
||||
}
|
||||
20 => {
|
||||
stream.decode_base64(&[], false);
|
||||
decode_base64(&mut stream, 0, &[], false);
|
||||
}
|
||||
21 => {
|
||||
stream.decode_quoted_printable(b"\n\n", true); // QP Boundaries have to be at least 2 bytes long
|
||||
decode_quoted_printable(&mut stream, 0, b"\n\n", true); // QP Boundaries have to be at least 2 bytes long
|
||||
}
|
||||
22 => {
|
||||
stream.decode_quoted_printable(b"\n\n", false); // QP Boundaries have to be at least 2 bytes long
|
||||
decode_quoted_printable(&mut stream, 0, b"\n\n", false); // QP Boundaries have to be at least 2 bytes long
|
||||
}
|
||||
23 => {
|
||||
stream.decode_quoted_printable(&[], true);
|
||||
decode_quoted_printable(&mut stream, 0, &[], true);
|
||||
}
|
||||
24 => {
|
||||
stream.decode_quoted_printable(&[], false);
|
||||
decode_quoted_printable(&mut stream, 0, &[], false);
|
||||
}
|
||||
0 | 25..=u32::MAX => unreachable!(),
|
||||
}
|
||||
@@ -116,16 +115,16 @@ fuzz_target!(|data: &[u8]| {
|
||||
|
||||
// Fuzz HTML functions
|
||||
let mut html_str = String::with_capacity(data.len());
|
||||
add_html_token(&mut html_str, data, false);
|
||||
let html_str = String::from_utf8_lossy(data);
|
||||
html_to_text(&html_str);
|
||||
text_to_html(&html_str);
|
||||
let str_data = String::from_utf8_lossy(data);
|
||||
add_html_token(&mut html_str, str_data.as_ref().as_bytes(), false);
|
||||
html_to_text(&str_data);
|
||||
text_to_html(&str_data);
|
||||
|
||||
// Fuzz decoding functions
|
||||
decode_hex(data);
|
||||
get_charset_decoder(data);
|
||||
|
||||
let decoders: &[for<'x> fn(&'x [u8]) -> Cow<'x, str>] = &[
|
||||
let decoders: &[for<'x> fn(&'x [u8]) -> String] = &[
|
||||
decoder_utf7,
|
||||
decoder_utf16_le,
|
||||
decoder_utf16_be,
|
||||
@@ -138,6 +137,5 @@ fuzz_target!(|data: &[u8]| {
|
||||
}
|
||||
|
||||
// Fuzz the entire library
|
||||
let mut data = Vec::from(data);
|
||||
Message::parse(&mut data[..]);
|
||||
Message::parse(&data[..]);
|
||||
});
|
||||
|
||||
+90
-138
@@ -9,165 +9,108 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
#[repr(C)]
|
||||
pub union Base64Chunk {
|
||||
pub val: u32,
|
||||
pub bytes: [u8; std::mem::size_of::<u32>()],
|
||||
}
|
||||
use super::DecodeResult;
|
||||
|
||||
pub trait Base64Decoder<'x> {
|
||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, bool, Option<&'x [u8]>);
|
||||
}
|
||||
pub fn decode_base64<'x>(
|
||||
stream: &MessageStream<'x>,
|
||||
start_pos: usize,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (usize, DecodeResult) {
|
||||
let mut success = boundary.is_empty();
|
||||
|
||||
// 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;
|
||||
//
|
||||
let mut chunk: u32 = 0;
|
||||
let mut byte_count: u8 = 0;
|
||||
let mut match_count: usize = 0;
|
||||
|
||||
impl<'x> Base64Decoder<'x> for MessageStream<'x> {
|
||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, bool, Option<&'x [u8]>) {
|
||||
unsafe {
|
||||
let mut success = boundary.is_empty();
|
||||
let mut bytes_read = 0;
|
||||
|
||||
let mut chunk = Base64Chunk { val: 0 };
|
||||
let mut byte_count: u8 = 0;
|
||||
let mut match_count: usize = 0;
|
||||
let mut buf = Vec::with_capacity(stream.data.len() - start_pos);
|
||||
|
||||
let data = &mut *self.data.get();
|
||||
let data_len = (*data).len();
|
||||
for ch in stream.data[start_pos..].iter() {
|
||||
bytes_read += 1;
|
||||
|
||||
let stream_pos = &mut *self.pos.get();
|
||||
let start_pos = *stream_pos;
|
||||
let mut read_pos = *stream_pos;
|
||||
let mut write_pos = *stream_pos;
|
||||
|
||||
while read_pos < data_len {
|
||||
let ch = (*data).get_unchecked(read_pos);
|
||||
read_pos += 1;
|
||||
|
||||
if !success {
|
||||
if ch == boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
success = true;
|
||||
break;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
} else if match_count > 0 {
|
||||
if ch == boundary.get_unchecked(0) {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
if !success {
|
||||
if *ch == boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
success = true;
|
||||
break;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
} else if match_count > 0 {
|
||||
if *ch == boundary[0] {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let val = BASE64_MAP
|
||||
.get_unchecked(byte_count as usize)
|
||||
.get_unchecked(*ch as usize);
|
||||
let val = BASE64_MAP[byte_count as usize][*ch as usize];
|
||||
|
||||
if *val >= 0x01ffffff {
|
||||
if *ch == b'=' {
|
||||
match byte_count {
|
||||
1 | 2 => {
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = *chunk.bytes.get_unchecked(0);
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) =
|
||||
*chunk.bytes.get_unchecked(0);
|
||||
}
|
||||
if val >= 0x01ffffff {
|
||||
if *ch == b'=' {
|
||||
match byte_count {
|
||||
1 | 2 => {
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
|
||||
write_pos += 1;
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
#[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;
|
||||
}
|
||||
0 => (),
|
||||
_ => {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if !(*ch).is_ascii_whitespace() || is_word {
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
0 => (),
|
||||
_ => {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
byte_count = (byte_count + 1) & 3;
|
||||
|
||||
if byte_count == 1 {
|
||||
chunk.val = *val;
|
||||
} else {
|
||||
chunk.val |= *val;
|
||||
|
||||
if byte_count == 0 {
|
||||
#[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;
|
||||
}
|
||||
}
|
||||
} else if !ch.is_ascii_whitespace() || is_word {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
*stream_pos = read_pos;
|
||||
byte_count = (byte_count + 1) & 3;
|
||||
|
||||
(
|
||||
success,
|
||||
false,
|
||||
if write_pos > start_pos {
|
||||
Some((*data).get_unchecked(start_pos..write_pos))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
if byte_count == 1 {
|
||||
chunk = val;
|
||||
} else {
|
||||
chunk |= val;
|
||||
|
||||
if byte_count == 0 {
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(
|
||||
if success { bytes_read } else { 0 },
|
||||
if !buf.is_empty() {
|
||||
buf.shrink_to_fit();
|
||||
DecodeResult::Owned(buf)
|
||||
} else {
|
||||
DecodeResult::Empty
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
|
||||
use super::Base64Decoder;
|
||||
use crate::{
|
||||
decoders::{base64::decode_base64, DecodeResult},
|
||||
parsers::message::MessageStream,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn decode_base64() {
|
||||
fn decode_base64_strings() {
|
||||
let inputs = [
|
||||
("VGVzdA==", "Test", "", true),
|
||||
("WWU=", "Ye", "", true),
|
||||
@@ -220,14 +163,23 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let stream = MessageStream::new(unsafe { str.as_bytes_mut() });
|
||||
let str = input.0.to_string();
|
||||
let stream = MessageStream::new(str.as_bytes());
|
||||
|
||||
let (success, _, result) = stream.decode_base64(input.2.as_bytes(), input.3);
|
||||
assert_eq!(success, !input.1.is_empty(), "Failed for '{:?}'", input.0);
|
||||
let (bytes_read, result) = decode_base64(&stream, 0, input.2.as_bytes(), input.3);
|
||||
assert_eq!(
|
||||
bytes_read > 0,
|
||||
!input.1.is_empty(),
|
||||
"Failed for '{:?}'",
|
||||
input.0
|
||||
);
|
||||
|
||||
if !input.1.is_empty() {
|
||||
let result_str = std::str::from_utf8(result.unwrap()).unwrap();
|
||||
let bytes = match result {
|
||||
DecodeResult::Owned(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let result_str = std::str::from_utf8(&bytes).unwrap();
|
||||
//println!("'{}' -> '{}'", input.0.escape_debug(), result_str.escape_debug());
|
||||
assert_eq!(
|
||||
result_str,
|
||||
|
||||
+610
-619
File diff suppressed because it is too large
Load Diff
@@ -9,14 +9,12 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
pub mod map;
|
||||
pub mod multi_byte;
|
||||
pub mod single_byte;
|
||||
pub mod utf;
|
||||
|
||||
pub type DecoderFnc<'x> = fn(&'x [u8]) -> Cow<'x, str>;
|
||||
pub type DecoderFnc<'x> = fn(&'x [u8]) -> String;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
@@ -9,8 +9,6 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[cfg(feature = "full_encoding")]
|
||||
use encoding_rs::*;
|
||||
|
||||
@@ -18,7 +16,7 @@ use encoding_rs::*;
|
||||
use super::map::decoder_default;
|
||||
|
||||
#[cfg(feature = "full_encoding")]
|
||||
fn multi_byte_decoder(mut decoder: Decoder, bytes: &[u8]) -> Cow<str> {
|
||||
fn multi_byte_decoder(mut decoder: Decoder, bytes: &[u8]) -> String {
|
||||
let mut result = String::with_capacity(bytes.len() * 3);
|
||||
|
||||
if let (CoderResult::OutputFull, _, _) = decoder.decode_to_string(bytes, &mut result, true) {
|
||||
@@ -26,10 +24,10 @@ fn multi_byte_decoder(mut decoder: Decoder, bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
|
||||
result.shrink_to_fit();
|
||||
result.into()
|
||||
result
|
||||
}
|
||||
|
||||
pub fn decoder_shift_jis(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_shift_jis(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(SHIFT_JIS.new_decoder(), bytes)
|
||||
@@ -41,7 +39,7 @@ pub fn decoder_shift_jis(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_big5(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_big5(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(BIG5.new_decoder(), bytes)
|
||||
@@ -53,7 +51,7 @@ pub fn decoder_big5(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_euc_jp(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_euc_jp(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(EUC_JP.new_decoder(), bytes)
|
||||
@@ -65,7 +63,7 @@ pub fn decoder_euc_jp(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_euc_kr(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_euc_kr(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(EUC_KR.new_decoder(), bytes)
|
||||
@@ -77,7 +75,7 @@ pub fn decoder_euc_kr(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_gb18030(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_gb18030(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(GB18030.new_decoder(), bytes)
|
||||
@@ -89,7 +87,7 @@ pub fn decoder_gb18030(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_gbk(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_gbk(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(GBK.new_decoder(), bytes)
|
||||
@@ -101,7 +99,7 @@ pub fn decoder_gbk(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_iso2022_jp(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso2022_jp(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(ISO_2022_JP.new_decoder(), bytes)
|
||||
@@ -113,7 +111,7 @@ pub fn decoder_iso2022_jp(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_windows874(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_windows874(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(WINDOWS_874.new_decoder(), bytes)
|
||||
@@ -125,7 +123,7 @@ pub fn decoder_windows874(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_ibm866(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_ibm866(bytes: &[u8]) -> String {
|
||||
#[cfg(feature = "full_encoding")]
|
||||
{
|
||||
multi_byte_decoder(IBM866.new_decoder(), bytes)
|
||||
|
||||
@@ -9,129 +9,126 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
fn single_byte_decoder<'x>(table: &[char], bytes: &'x [u8]) -> Cow<'x, str> {
|
||||
fn single_byte_decoder(table: &[char], bytes: &[u8]) -> String {
|
||||
let mut result = String::with_capacity(bytes.len() * 2);
|
||||
|
||||
for byte in bytes {
|
||||
// SAFE: table's size is always u8::MAX
|
||||
result.push(unsafe { *table.get_unchecked(*byte as usize) });
|
||||
result.push(table[*byte as usize]);
|
||||
}
|
||||
|
||||
result.shrink_to_fit();
|
||||
result.into()
|
||||
result
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_1(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_1(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_1, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_2(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_2(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_2, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_3(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_3(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_3, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_4(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_4(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_4, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_5(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_5(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_5, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_6(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_6(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_6, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_7(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_7(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_7, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_8(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_8(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_8, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_9(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_9(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_9, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_10(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_10(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_10, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_13(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_13(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_13, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_14(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_14(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_14, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_15(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_15(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_15, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_iso_8859_16(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_iso_8859_16(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(ISO_8859_16, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1250(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1250(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1250, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1251(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1251(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1251, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1252(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1252(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1252, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1253(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1253(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1253, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1254(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1254(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1254, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1255(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1255(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1255, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1256(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1256(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1256, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1257(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1257(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1257, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_cp1258(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_cp1258(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(CP1258, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_koi8_r(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_koi8_r(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(KOI8_R, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_koi8_u(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_koi8_u(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(KOI8_U, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_macintosh(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_macintosh(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(MACINTOSH, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_ibm_850(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_ibm_850(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(IBM850, bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_tis_620(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_tis_620(bytes: &[u8]) -> String {
|
||||
single_byte_decoder(TIS_620, bytes)
|
||||
}
|
||||
|
||||
|
||||
@@ -9,38 +9,32 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
char::{decode_utf16, REPLACEMENT_CHARACTER},
|
||||
};
|
||||
use std::char::{decode_utf16, REPLACEMENT_CHARACTER};
|
||||
|
||||
use crate::decoders::base64::{Base64Chunk, BASE64_MAP};
|
||||
use crate::decoders::base64::BASE64_MAP;
|
||||
|
||||
struct Utf7DecoderState {
|
||||
utf16_bytes: Vec<u16>,
|
||||
pending_byte: Option<u8>,
|
||||
b64_bytes: Base64Chunk,
|
||||
b64_bytes: u32,
|
||||
}
|
||||
|
||||
fn add_utf16_bytes(state: &mut Utf7DecoderState, n_bytes: usize) {
|
||||
debug_assert!(n_bytes < std::mem::size_of::<u32>());
|
||||
|
||||
unsafe {
|
||||
// SAFE: union access of `n_bytes`, which is always lower than size_of::<u32>
|
||||
for byte in state.b64_bytes.bytes.get_unchecked(0..n_bytes) {
|
||||
if let Some(pending_byte) = state.pending_byte {
|
||||
state
|
||||
.utf16_bytes
|
||||
.push(u16::from_be_bytes([pending_byte, *byte]));
|
||||
state.pending_byte = None;
|
||||
} else {
|
||||
state.pending_byte = Some(*byte);
|
||||
}
|
||||
for byte in state.b64_bytes.to_le_bytes()[0..n_bytes].iter() {
|
||||
if let Some(pending_byte) = state.pending_byte {
|
||||
state
|
||||
.utf16_bytes
|
||||
.push(u16::from_be_bytes([pending_byte, *byte]));
|
||||
state.pending_byte = None;
|
||||
} else {
|
||||
state.pending_byte = Some(*byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_utf7(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_utf7(bytes: &[u8]) -> String {
|
||||
let mut result = String::with_capacity(bytes.len());
|
||||
let mut byte_count: u8 = 0;
|
||||
let mut in_b64 = false;
|
||||
@@ -48,28 +42,20 @@ pub fn decoder_utf7(bytes: &[u8]) -> Cow<str> {
|
||||
let mut state = Utf7DecoderState {
|
||||
utf16_bytes: Vec::with_capacity(10),
|
||||
pending_byte: None,
|
||||
b64_bytes: Base64Chunk { val: 0 },
|
||||
b64_bytes: 0,
|
||||
};
|
||||
|
||||
for byte in bytes {
|
||||
if in_b64 {
|
||||
// SAFE: BASEMAP's size is [4][u8::MAX]
|
||||
let val = unsafe {
|
||||
BASE64_MAP
|
||||
.get_unchecked(byte_count as usize)
|
||||
.get_unchecked(*byte as usize)
|
||||
};
|
||||
let val = BASE64_MAP[byte_count as usize][*byte as usize];
|
||||
|
||||
if *val < 0x01ffffff {
|
||||
if val < 0x01ffffff {
|
||||
byte_count = (byte_count + 1) & 3;
|
||||
|
||||
if byte_count == 1 {
|
||||
state.b64_bytes.val = *val;
|
||||
state.b64_bytes = val;
|
||||
} else {
|
||||
// SAFE: union read/write
|
||||
unsafe {
|
||||
state.b64_bytes.val |= *val;
|
||||
}
|
||||
state.b64_bytes |= val;
|
||||
|
||||
if byte_count == 0 {
|
||||
add_utf16_bytes(&mut state, 3);
|
||||
@@ -112,30 +98,29 @@ pub fn decoder_utf7(bytes: &[u8]) -> Cow<str> {
|
||||
}
|
||||
|
||||
result.shrink_to_fit();
|
||||
result.into()
|
||||
result
|
||||
}
|
||||
|
||||
fn decoder_utf16_(bytes: &[u8], fnc: fn([u8; 2]) -> u16) -> Cow<str> {
|
||||
fn decoder_utf16_(bytes: &[u8], fnc: fn([u8; 2]) -> u16) -> String {
|
||||
if bytes.len() >= 2 {
|
||||
decode_utf16(bytes.chunks_exact(2).map(|c| fnc([c[0], c[1]])))
|
||||
.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
|
||||
.collect::<String>()
|
||||
.into()
|
||||
} else {
|
||||
"".into()
|
||||
"".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decoder_utf16_le(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_utf16_le(bytes: &[u8]) -> String {
|
||||
decoder_utf16_(bytes, u16::from_le_bytes)
|
||||
}
|
||||
|
||||
pub fn decoder_utf16_be(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_utf16_be(bytes: &[u8]) -> String {
|
||||
decoder_utf16_(bytes, u16::from_be_bytes)
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
pub fn decoder_utf16(bytes: &[u8]) -> Cow<str> {
|
||||
pub fn decoder_utf16(bytes: &[u8]) -> String {
|
||||
// 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),
|
||||
@@ -146,8 +131,9 @@ pub fn decoder_utf16(bytes: &[u8]) -> Cow<str> {
|
||||
decoder_utf16_(bytes, fnc)
|
||||
}
|
||||
|
||||
pub fn decoder_utf8(bytes: &[u8]) -> Cow<str> {
|
||||
String::from_utf8_lossy(bytes)
|
||||
// Not currently used at the moment
|
||||
pub fn decoder_utf8(bytes: &[u8]) -> String {
|
||||
String::from_utf8_lossy(bytes).into_owned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -9,85 +9,106 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use std::borrow::Cow;
|
||||
use crate::{decoders::charsets::map::get_charset_decoder, parsers::message::MessageStream};
|
||||
|
||||
use crate::{
|
||||
decoders::{
|
||||
base64::Base64Decoder, charsets::map::get_charset_decoder,
|
||||
quoted_printable::QuotedPrintableDecoder,
|
||||
},
|
||||
parsers::message_stream::MessageStream,
|
||||
use super::{
|
||||
base64::decode_base64, quoted_printable::decode_quoted_printable, DecodeFnc, DecodeResult,
|
||||
};
|
||||
|
||||
use super::charsets::map::decoder_default;
|
||||
enum Rfc2047State {
|
||||
Init,
|
||||
Charset,
|
||||
Encoding,
|
||||
Data,
|
||||
}
|
||||
|
||||
pub fn parse_encoded_word<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
if !stream.skip_byte(&b'?') {
|
||||
return None;
|
||||
}
|
||||
let charset_start = stream.get_pos();
|
||||
let mut charset_end = charset_start;
|
||||
let backup_pos = charset_start - 1;
|
||||
pub fn decode_rfc2047(stream: &MessageStream, start_pos: usize) -> (usize, Option<String>) {
|
||||
let mut read_pos: usize = start_pos;
|
||||
let mut state = Rfc2047State::Init;
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
match ch {
|
||||
b'?' => {
|
||||
if charset_end == charset_start {
|
||||
charset_end = stream.get_pos() - 1;
|
||||
let mut charset_start = 0;
|
||||
let mut charset_end = 0;
|
||||
let mut decode_fnc: Option<DecodeFnc> = None;
|
||||
|
||||
for ch in stream.data[start_pos..].iter() {
|
||||
read_pos += 1;
|
||||
|
||||
match state {
|
||||
Rfc2047State::Init => {
|
||||
if ch != &b'?' {
|
||||
return (0, None);
|
||||
}
|
||||
break;
|
||||
state = Rfc2047State::Charset;
|
||||
charset_start = read_pos;
|
||||
charset_end = read_pos;
|
||||
}
|
||||
b'*' => {
|
||||
if charset_end == charset_start {
|
||||
charset_end = stream.get_pos() - 1;
|
||||
Rfc2047State::Charset => match ch {
|
||||
b'?' => {
|
||||
if charset_end == charset_start {
|
||||
charset_end = read_pos - 1;
|
||||
}
|
||||
if (charset_end - charset_start) < 2 {
|
||||
return (0, None);
|
||||
}
|
||||
state = Rfc2047State::Encoding;
|
||||
}
|
||||
b'*' => {
|
||||
if charset_end == charset_start {
|
||||
charset_end = read_pos - 1;
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
return (0, None);
|
||||
}
|
||||
_ => (),
|
||||
},
|
||||
Rfc2047State::Encoding => {
|
||||
match ch {
|
||||
b'q' | b'Q' => decode_fnc = Some(decode_quoted_printable),
|
||||
b'b' | b'B' => decode_fnc = Some(decode_base64),
|
||||
_ => {
|
||||
return (0, None);
|
||||
}
|
||||
}
|
||||
state = Rfc2047State::Data;
|
||||
}
|
||||
Rfc2047State::Data => {
|
||||
if ch != &b'?' {
|
||||
return (0, None);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
stream.set_pos(backup_pos);
|
||||
return None;
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
if !(2..=45).contains(&(charset_end - charset_start)) {
|
||||
stream.set_pos(backup_pos);
|
||||
return None;
|
||||
}
|
||||
|
||||
let (success, is_utf8_safe, bytes) = match stream.next() {
|
||||
Some(b'q') | Some(b'Q') if stream.skip_byte(&b'?') => {
|
||||
stream.decode_quoted_printable(b"?=", true)
|
||||
if let Some(decode_fnc) = decode_fnc {
|
||||
if let (bytes_read @ 1..=usize::MAX, DecodeResult::Owned(bytes)) =
|
||||
decode_fnc(stream, read_pos, b"?=", true)
|
||||
{
|
||||
return (
|
||||
(read_pos - start_pos) + bytes_read,
|
||||
if let Some(decoder) = get_charset_decoder(&stream.data[charset_start..charset_end])
|
||||
{
|
||||
decoder(&bytes).into()
|
||||
} else {
|
||||
String::from_utf8(bytes)
|
||||
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
||||
.into()
|
||||
},
|
||||
);
|
||||
}
|
||||
Some(b'b') | Some(b'B') if stream.skip_byte(&b'?') => stream.decode_base64(b"?=", true),
|
||||
_ => (false, false, None),
|
||||
};
|
||||
|
||||
if !success {
|
||||
stream.set_pos(backup_pos);
|
||||
return None;
|
||||
}
|
||||
|
||||
if let Some(decoder) =
|
||||
get_charset_decoder(stream.get_bytes(charset_start, charset_end).unwrap())
|
||||
{
|
||||
decoder(bytes?).into()
|
||||
} else if is_utf8_safe {
|
||||
// SAFE: slice checked previously
|
||||
unsafe { Cow::from(std::str::from_utf8_unchecked(bytes?)).into() }
|
||||
} else {
|
||||
decoder_default(bytes?).into()
|
||||
}
|
||||
(0, None)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream,
|
||||
};
|
||||
use crate::{decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream};
|
||||
|
||||
#[test]
|
||||
fn decode_rfc2047() {
|
||||
fn decode_rfc2047_string() {
|
||||
let inputs = [
|
||||
(
|
||||
"?iso-8859-1?q?this=20is=20some=20text?=".to_string(),
|
||||
@@ -166,14 +187,14 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let str = input.0.to_string();
|
||||
|
||||
match parse_encoded_word(&MessageStream::new(unsafe { str.as_bytes_mut() })) {
|
||||
Some(string) => {
|
||||
match decode_rfc2047(&MessageStream::new(str.as_bytes()), 0) {
|
||||
(_, Some(string)) => {
|
||||
//println!("Decoded '{}'", string);
|
||||
assert_eq!(string, input.1);
|
||||
}
|
||||
None => panic!("Failed to decode '{}'", input.0),
|
||||
_ => panic!("Failed to decode '{}'", input.0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-4
@@ -39,8 +39,7 @@ pub fn decode_hex(src: &[u8]) -> (bool, Vec<u8>) {
|
||||
result.push(*ch);
|
||||
}
|
||||
HexState::Percent => {
|
||||
// SAFE: HEX_MAP's size is u8::MAX
|
||||
hex1 = unsafe { *HEX_MAP.get_unchecked(*ch as usize) };
|
||||
hex1 = HEX_MAP[*ch as usize];
|
||||
if hex1 != -1 {
|
||||
state = HexState::Hex1;
|
||||
} else {
|
||||
@@ -49,8 +48,7 @@ pub fn decode_hex(src: &[u8]) -> (bool, Vec<u8>) {
|
||||
}
|
||||
}
|
||||
HexState::Hex1 => {
|
||||
// SAFE: HEX_MAP's size is u8::MAX
|
||||
let hex2 = unsafe { *HEX_MAP.get_unchecked(*ch as usize) };
|
||||
let hex2 = HEX_MAP[*ch as usize];
|
||||
|
||||
state = HexState::None;
|
||||
if hex2 != -1 {
|
||||
|
||||
+21
-28
@@ -33,33 +33,29 @@ pub fn add_html_token(result: &mut String, token: &[u8], add_space: bool) {
|
||||
debug_assert_eq!(ENTITY_HASH.len(), u8::MAX as usize + 5);
|
||||
debug_assert_eq!(ENTITY_MAP.len(), 18079 - 64 + 1);
|
||||
|
||||
/* SAFE: ENTITY_HASH's size is 260 (u8::MAX + 5)
|
||||
and ENTITY_MAP's size is 18016 (18079 - 64 + 1) */
|
||||
unsafe {
|
||||
for (pos, ch) in entity.iter().enumerate() {
|
||||
match pos {
|
||||
0 | 5 | 6 | 9 | 11 => {
|
||||
hash += *ENTITY_HASH.get_unchecked(*ch as usize);
|
||||
}
|
||||
1 => {
|
||||
hash += *ENTITY_HASH.get_unchecked(*ch as usize + 4);
|
||||
}
|
||||
2 | 4 => {
|
||||
hash += *ENTITY_HASH.get_unchecked(*ch as usize + 1);
|
||||
}
|
||||
3 => {
|
||||
hash += *ENTITY_HASH.get_unchecked(*ch as usize + 3);
|
||||
}
|
||||
_ => (),
|
||||
for (pos, ch) in entity.iter().enumerate() {
|
||||
match pos {
|
||||
0 | 5 | 6 | 9 | 11 => {
|
||||
hash += ENTITY_HASH[*ch as usize];
|
||||
}
|
||||
if pos == entity.len() - 1 {
|
||||
hash += *ENTITY_HASH.get_unchecked(*ch as usize);
|
||||
1 => {
|
||||
hash += ENTITY_HASH[*ch as usize + 4];
|
||||
}
|
||||
2 | 4 => {
|
||||
hash += ENTITY_HASH[*ch as usize + 1];
|
||||
}
|
||||
3 => {
|
||||
hash += ENTITY_HASH[*ch as usize + 3];
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
if pos == entity.len() - 1 {
|
||||
hash += ENTITY_HASH[*ch as usize];
|
||||
}
|
||||
}
|
||||
|
||||
if (64..=18079).contains(&hash) {
|
||||
entity_code = *ENTITY_MAP.get_unchecked((hash - 64) as usize);
|
||||
}
|
||||
if (64..=18079).contains(&hash) {
|
||||
entity_code = ENTITY_MAP[(hash - 64) as usize];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,9 +65,7 @@ pub fn add_html_token(result: &mut String, token: &[u8], add_space: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// SAFE: the input string is tokenized by ASCII chars, therefore the resulting
|
||||
// u8 array is UTF-8 safe.
|
||||
result.push_str(unsafe { std::str::from_utf8_unchecked(token) });
|
||||
result.push_str(std::str::from_utf8(token).unwrap());
|
||||
}
|
||||
|
||||
pub fn html_to_text(input: &str) -> String {
|
||||
@@ -237,8 +231,7 @@ pub fn text_to_html(input: &str) -> String {
|
||||
}
|
||||
result.extend_from_slice(b"</body></html>");
|
||||
|
||||
// SAFE: `result` contains a sequence of a valid `input` string tokens and UTF-8 safe slices
|
||||
unsafe { String::from_utf8_unchecked(result) }
|
||||
String::from_utf8(result).unwrap()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -9,9 +9,19 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
pub mod base64;
|
||||
pub mod charsets;
|
||||
pub mod encoded_word;
|
||||
pub mod hex;
|
||||
pub mod html;
|
||||
pub mod quoted_printable;
|
||||
|
||||
pub type DecodeFnc<'x> = fn(&MessageStream<'x>, usize, &[u8], bool) -> (usize, DecodeResult);
|
||||
|
||||
pub enum DecodeResult {
|
||||
Owned(Vec<u8>),
|
||||
Borrowed((usize, usize)),
|
||||
Empty,
|
||||
}
|
||||
|
||||
+149
-235
@@ -9,7 +9,9 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
use super::DecodeResult;
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
enum QuotedPrintableState {
|
||||
@@ -18,250 +20,152 @@ enum QuotedPrintableState {
|
||||
Hex1,
|
||||
}
|
||||
|
||||
pub trait QuotedPrintableDecoder<'x> {
|
||||
fn decode_quoted_printable(
|
||||
&self,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (bool, bool, Option<&'x [u8]>);
|
||||
}
|
||||
pub fn decode_quoted_printable<'x>(
|
||||
stream: &MessageStream<'x>,
|
||||
start_pos: usize,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (usize, DecodeResult) {
|
||||
let mut success = boundary.is_empty();
|
||||
|
||||
// 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;
|
||||
//
|
||||
let mut bytes_read = 0;
|
||||
|
||||
impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
fn decode_quoted_printable(
|
||||
&self,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (bool, bool, Option<&'x [u8]>) {
|
||||
unsafe {
|
||||
let mut success = boundary.is_empty();
|
||||
let mut is_utf8_safe = true;
|
||||
let mut buf = Vec::with_capacity(stream.data.len() - start_pos);
|
||||
|
||||
// SAFE: exclusive access to `self.data`
|
||||
let mut data = &mut *self.data.get();
|
||||
let data_len = (*data).len();
|
||||
let mut state = QuotedPrintableState::None;
|
||||
let mut match_count = 0;
|
||||
let mut hex1 = 0;
|
||||
|
||||
// SAFE: exclusive access to `self.pos`
|
||||
let stream_pos = &mut *self.pos.get();
|
||||
let start_pos = *stream_pos;
|
||||
let mut read_pos = *stream_pos;
|
||||
let mut write_pos = *stream_pos;
|
||||
debug_assert!(boundary.is_empty() || boundary.len() >= 2);
|
||||
|
||||
let mut state = QuotedPrintableState::None;
|
||||
let mut match_count = 0;
|
||||
let mut hex1 = 0;
|
||||
for ch in stream.data[start_pos..].iter() {
|
||||
bytes_read += 1;
|
||||
|
||||
debug_assert!(boundary.is_empty() || boundary.len() >= 2);
|
||||
// if success is false, a boundary was provided
|
||||
if !success {
|
||||
let mut is_boundary_end = true;
|
||||
|
||||
while read_pos < data_len {
|
||||
// SAFE: slice bounds checked in while loop
|
||||
let ch = *(*data).get_unchecked(read_pos);
|
||||
read_pos += 1;
|
||||
if *ch == boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
let done = if is_word {
|
||||
true
|
||||
} else {
|
||||
is_boundary_end =
|
||||
crate::parsers::mime::is_boundary_end(stream, start_pos + bytes_read);
|
||||
is_boundary_end
|
||||
};
|
||||
|
||||
// if success is false, a boundary was provided
|
||||
if !success {
|
||||
let mut is_boundary_end = true;
|
||||
|
||||
// SAFE: bounds are checked after the first match occurs
|
||||
if ch == *boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
let done = if is_word {
|
||||
true
|
||||
} else {
|
||||
// SAFE: exclusive access to `self.data` is re-obtained after
|
||||
// is_boundary_end finishes.
|
||||
is_boundary_end = self.is_boundary_end(read_pos);
|
||||
is_boundary_end
|
||||
};
|
||||
data = &mut *self.data.get(); // Avoid violating the Stacked Borrows rules
|
||||
|
||||
if done {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
if done {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if match_count > 0 {
|
||||
for ch in boundary[..match_count].iter() {
|
||||
if *ch != b'\n' || QuotedPrintableState::Eq != state {
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = *ch;
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
// SAFE: `write_pos` increments in parallel to `read_pos`
|
||||
// which is bound checked in the while loop
|
||||
*((*data).get_unchecked_mut(write_pos)) = *ch;
|
||||
}
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && *ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
state = QuotedPrintableState::None;
|
||||
|
||||
// is_boundary_end is always true except in the rare event
|
||||
// that a boundary was found without a proper MIME ending (-- or \r\n)
|
||||
if is_boundary_end {
|
||||
|
||||
// SAFE: `boundary` is never empty at this point
|
||||
if ch == *boundary.get_unchecked(0) {
|
||||
// Char matched beginning of boundary, get next char.
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
// Reset match count and decode character
|
||||
match_count = 0;
|
||||
}
|
||||
} else {
|
||||
// There was a full boundary match but without a proper MIME
|
||||
// ending, reset match count and continue.
|
||||
match_count = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match ch {
|
||||
b'=' => {
|
||||
if let QuotedPrintableState::None = state {
|
||||
state = QuotedPrintableState::Eq
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
if is_word {
|
||||
success = false;
|
||||
break;
|
||||
} else if QuotedPrintableState::Eq == state {
|
||||
state = QuotedPrintableState::None;
|
||||
} else {
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = b'\n';
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
// SAFE: `write_pos` increments in parallel to `read_pos`
|
||||
// which is bound checked in the while loop
|
||||
*((*data).get_unchecked_mut(write_pos)) = b'\n';
|
||||
}
|
||||
|
||||
write_pos += 1;
|
||||
}
|
||||
}
|
||||
b'_' if is_word => {
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = b' ';
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
// SAFE: `write_pos` increments in parallel to `read_pos`
|
||||
// which is bound checked in the while loop
|
||||
*((*data).get_unchecked_mut(write_pos)) = b' ';
|
||||
}
|
||||
write_pos += 1;
|
||||
}
|
||||
b'\r' => (),
|
||||
_ => match state {
|
||||
QuotedPrintableState::None => {
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = ch;
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
// SAFE: `write_pos` increments in parallel to `read_pos`
|
||||
// which is bound checked in the while loop
|
||||
*((*data).get_unchecked_mut(write_pos)) = ch;
|
||||
}
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Eq => {
|
||||
// SAFE: HEX_MAP's size is u8::MAX
|
||||
hex1 = *HEX_MAP.get_unchecked(ch as usize);
|
||||
if hex1 != -1 {
|
||||
state = QuotedPrintableState::Hex1;
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Hex1 => {
|
||||
// SAFE: HEX_MAP's size is u8::MAX
|
||||
let hex2 = *HEX_MAP.get_unchecked(ch as usize);
|
||||
|
||||
state = QuotedPrintableState::None;
|
||||
if hex2 != -1 {
|
||||
let ch = ((hex1 as u8) << 4) | hex2 as u8;
|
||||
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = ch;
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
// SAFE: `write_pos` increments in parallel to `read_pos`
|
||||
// which is bound checked in the while loop
|
||||
*((*data).get_unchecked_mut(write_pos)) = ch;
|
||||
}
|
||||
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
}
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
*stream_pos = read_pos;
|
||||
if match_count > 0 {
|
||||
for ch in boundary[..match_count].iter() {
|
||||
if *ch != b'\n' || QuotedPrintableState::Eq != state {
|
||||
buf.push(*ch);
|
||||
}
|
||||
}
|
||||
state = QuotedPrintableState::None;
|
||||
|
||||
(
|
||||
success,
|
||||
is_utf8_safe,
|
||||
if write_pos > start_pos {
|
||||
// SAFE: bounds are checked
|
||||
Some((*data).get_unchecked(start_pos..write_pos))
|
||||
// is_boundary_end is always true except in the rare event
|
||||
// that a boundary was found without a proper MIME ending (-- or \r\n)
|
||||
if is_boundary_end {
|
||||
if *ch == boundary[0] {
|
||||
// Char matched beginning of boundary, get next char.
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
// Reset match count and decode character
|
||||
match_count = 0;
|
||||
}
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
// There was a full boundary match but without a proper MIME
|
||||
// ending, reset match count and continue.
|
||||
match_count = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match ch {
|
||||
b'=' => {
|
||||
if let QuotedPrintableState::None = state {
|
||||
state = QuotedPrintableState::Eq
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
if is_word {
|
||||
success = false;
|
||||
break;
|
||||
} else if QuotedPrintableState::Eq == state {
|
||||
state = QuotedPrintableState::None;
|
||||
} else {
|
||||
buf.push(b'\n');
|
||||
}
|
||||
}
|
||||
b'_' if is_word => {
|
||||
buf.push(b' ');
|
||||
}
|
||||
b'\r' => (),
|
||||
_ => match state {
|
||||
QuotedPrintableState::None => {
|
||||
buf.push(*ch);
|
||||
}
|
||||
QuotedPrintableState::Eq => {
|
||||
hex1 = HEX_MAP[*ch as usize];
|
||||
if hex1 != -1 {
|
||||
state = QuotedPrintableState::Hex1;
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Hex1 => {
|
||||
let hex2 = HEX_MAP[*ch as usize];
|
||||
|
||||
state = QuotedPrintableState::None;
|
||||
if hex2 != -1 {
|
||||
let ch = ((hex1 as u8) << 4) | hex2 as u8;
|
||||
|
||||
buf.push(ch);
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
(
|
||||
if success { bytes_read } else { 0 },
|
||||
if !buf.is_empty() {
|
||||
buf.shrink_to_fit();
|
||||
DecodeResult::Owned(buf)
|
||||
} else {
|
||||
DecodeResult::Empty
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
decoders::quoted_printable::QuotedPrintableDecoder, parsers::message_stream::MessageStream,
|
||||
decoders::{quoted_printable::decode_quoted_printable, DecodeResult},
|
||||
parsers::message::MessageStream,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn decode_quoted_printable() {
|
||||
fn decode_quoted_printable_string() {
|
||||
let inputs = [
|
||||
(
|
||||
concat!(
|
||||
@@ -331,15 +235,25 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let stream = MessageStream::new(unsafe { str.as_bytes_mut() });
|
||||
let str = input.0.to_string();
|
||||
let stream = MessageStream::new(str.as_bytes());
|
||||
|
||||
let (success, _, result) = stream.decode_quoted_printable(input.2.as_bytes(), input.3);
|
||||
let (bytes_read, result) =
|
||||
decode_quoted_printable(&stream, 0, input.2.as_bytes(), input.3);
|
||||
|
||||
assert_eq!(success, !input.1.is_empty(), "Failed for '{:?}'", input.0);
|
||||
assert_eq!(
|
||||
bytes_read > 0,
|
||||
!input.1.is_empty(),
|
||||
"Failed for '{:?}'",
|
||||
input.0
|
||||
);
|
||||
|
||||
if !input.1.is_empty() {
|
||||
let result_str = std::str::from_utf8(result.unwrap()).unwrap();
|
||||
let bytes = match result {
|
||||
DecodeResult::Owned(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let result_str = std::str::from_utf8(&bytes).unwrap();
|
||||
|
||||
/*println!(
|
||||
"Decoded '{}'\n -> to ->\n'{}'\n{}",
|
||||
@@ -358,15 +272,15 @@ mod tests {
|
||||
}
|
||||
|
||||
// MIRI overflow tests
|
||||
for mut input in [b"\x0a\x0a\x00\x0b".to_vec(), b":B\x0a%".to_vec()] {
|
||||
let stream = MessageStream::new(&mut input[..]);
|
||||
stream.decode_quoted_printable(b"\n\n", true);
|
||||
let stream = MessageStream::new(&mut input[..]);
|
||||
stream.decode_quoted_printable(b"\n\n", false);
|
||||
let stream = MessageStream::new(&mut input[..]);
|
||||
stream.decode_quoted_printable(&[], true);
|
||||
let stream = MessageStream::new(&mut input[..]);
|
||||
stream.decode_quoted_printable(&[], false);
|
||||
for input in [b"\x0a\x0a\x00\x0b".to_vec(), b":B\x0a%".to_vec()] {
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
decode_quoted_printable(&stream, 0, b"\n\n", true);
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
decode_quoted_printable(&stream, 0, b"\n\n", false);
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
decode_quoted_printable(&stream, 0, &[], true);
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
decode_quoted_printable(&stream, 0, &[], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+75
-14
@@ -29,15 +29,13 @@
|
||||
//!
|
||||
//! Performance and memory safety were two important factors while designing _mail-parser_:
|
||||
//!
|
||||
//! - **Zero-copy parsing** is done in most cases (unless when decoding non-UTF8 text or when RFC2047/RFC2231 encoded parts are present).
|
||||
//! Practically all strings and u8 slices returned by this library are `Cow<str>` or `Cow<[u8]>` references to the input raw message.
|
||||
//! - Memory allocations are always avoided unless they are really necessary. In fact, all Base64 and Quoted-Printable parts are decoded in
|
||||
//! place re-using the input buffer.
|
||||
//! - [Perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function) is used for fast look-up of message header fields, character
|
||||
//! set names and aliases, HTML entities as well as month names while parsing _Date_ fields.
|
||||
//! - Although some `unsafe` code was used to obtain performance gains of about 10%, every function in the library has been
|
||||
//! [fuzzed](#testing-fuzzing--benchmarking) and also heavily [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
//! - Fully battle-tested with millions of real-world e-mail messages dating from 1995 until today.
|
||||
//! - **Zero-copy**: Practically all strings returned by this library are `Cow<str>` references to the input raw message.
|
||||
//! - **High performance Base64 decoding** based on Chromium's decoder ([the fastest non-SIMD decoder](https://github.com/lemire/fastbase64)).
|
||||
//! - **Fast parsing** of message header fields, character set names and HTML entities using [perfect hashing](https://en.wikipedia.org/wiki/Perfect_hash_function).
|
||||
//! - Written in **100% safe** Rust with no external dependencies.
|
||||
//! - Every function in the library has been [fuzzed](#testing-fuzzing--benchmarking) and
|
||||
//! meticulously [tested with MIRI](#testing-fuzzing--benchmarking).
|
||||
//! - Thoroughly **battle-tested** with millions of real-world e-mail messages dating from 1995 until today.
|
||||
//!
|
||||
//! Jump to the [example](#usage-example).
|
||||
//!
|
||||
@@ -110,7 +108,7 @@
|
||||
//! ```
|
||||
//! use mail_parser::*;
|
||||
//!
|
||||
//! let mut input = concat!(
|
||||
//! let input = concat!(
|
||||
//! "From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||
//! "To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||
//! " jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||
@@ -145,10 +143,9 @@
|
||||
//! "--giddyup--\n",
|
||||
//! "--festivus--\n",
|
||||
//! )
|
||||
//! .as_bytes()
|
||||
//! .to_vec();
|
||||
//! .as_bytes();
|
||||
//!
|
||||
//! let message = Message::parse(&mut input[..]);
|
||||
//! let message = Message::parse(input);
|
||||
//!
|
||||
//! // Parses addresses (including comments), lists and groups
|
||||
//! assert_eq!(
|
||||
@@ -258,7 +255,7 @@
|
||||
//! println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||
//! println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||
//!```
|
||||
|
||||
#[forbid(unsafe_code)]
|
||||
pub mod decoders;
|
||||
pub mod parsers;
|
||||
|
||||
@@ -941,6 +938,70 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> MessageHeader<'x> {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
for addr in [
|
||||
&self.from,
|
||||
&self.to,
|
||||
&self.cc,
|
||||
&self.bcc,
|
||||
&self.reply_to,
|
||||
&self.sender,
|
||||
&self.resent_to,
|
||||
&self.resent_from,
|
||||
&self.resent_bcc,
|
||||
&self.resent_cc,
|
||||
&self.resent_sender,
|
||||
&self.list_archive,
|
||||
&self.list_help,
|
||||
&self.list_id,
|
||||
&self.list_owner,
|
||||
&self.list_post,
|
||||
&self.list_subscribe,
|
||||
&self.list_unsubscribe,
|
||||
] {
|
||||
if !addr.is_empty() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for str in [
|
||||
&self.subject,
|
||||
&self.message_id,
|
||||
&self.mime_version,
|
||||
&self.content_description,
|
||||
&self.content_id,
|
||||
&self.content_transfer_encoding,
|
||||
] {
|
||||
if str.is_some() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if self.date.is_some() || !self.others.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
for vec in [
|
||||
&self.comments,
|
||||
&self.in_reply_to,
|
||||
&self.keywords,
|
||||
&self.received,
|
||||
&self.references,
|
||||
&self.resent_message_id,
|
||||
&self.return_path,
|
||||
] {
|
||||
if vec.is_some() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
self.content_disposition.is_none()
|
||||
&& self.resent_date.is_none()
|
||||
&& self.content_type.is_none()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> MimeFieldGet<'x> for Message<'x> {
|
||||
fn get_content_description(&self) -> Option<&Cow<'x, str>> {
|
||||
self.header.content_description.as_ref()
|
||||
|
||||
@@ -12,8 +12,7 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::{
|
||||
decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream, Addr,
|
||||
Address, Group,
|
||||
decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream, Addr, Address, Group,
|
||||
};
|
||||
|
||||
impl<'x> Default for Address<'x> {
|
||||
@@ -40,7 +39,6 @@ pub struct AddressParser<'x> {
|
||||
token_start: usize,
|
||||
token_end: usize,
|
||||
|
||||
is_token_safe: bool,
|
||||
is_token_email: bool,
|
||||
is_token_start: bool,
|
||||
is_escaped: bool,
|
||||
@@ -64,13 +62,7 @@ pub fn add_token<'x>(
|
||||
add_trail_space: bool,
|
||||
) {
|
||||
if parser.token_start > 0 {
|
||||
let token = stream
|
||||
.get_string(
|
||||
parser.token_start - 1,
|
||||
parser.token_end,
|
||||
parser.is_token_safe,
|
||||
)
|
||||
.unwrap();
|
||||
let token = String::from_utf8_lossy(&stream.data[parser.token_start - 1..parser.token_end]);
|
||||
let mut add_space = false;
|
||||
let list = match parser.state {
|
||||
AddressState::Address => &mut parser.mail_tokens,
|
||||
@@ -100,7 +92,6 @@ pub fn add_token<'x>(
|
||||
}
|
||||
|
||||
parser.token_start = 0;
|
||||
parser.is_token_safe = true;
|
||||
parser.is_token_email = false;
|
||||
parser.is_token_start = true;
|
||||
parser.is_escaped = false;
|
||||
@@ -233,12 +224,11 @@ pub fn add_group(parser: &mut AddressParser) {
|
||||
});
|
||||
}
|
||||
|
||||
pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
pub fn parse_address<'x>(stream: &mut MessageStream<'x>) -> Address<'x> {
|
||||
let mut parser = AddressParser {
|
||||
token_start: 0,
|
||||
token_end: 0,
|
||||
|
||||
is_token_safe: true,
|
||||
is_token_email: false,
|
||||
is_token_start: true,
|
||||
is_escaped: false,
|
||||
@@ -256,13 +246,18 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
result: Vec::new(),
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
stream.pos += 1;
|
||||
|
||||
match ch {
|
||||
b'\n' => {
|
||||
add_token(&mut parser, stream, false);
|
||||
match stream.peek() {
|
||||
match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
stream.pos += 1;
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
@@ -274,7 +269,7 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
b'\\' if parser.state != AddressState::Name => {
|
||||
if parser.token_start > 0 {
|
||||
if parser.state == AddressState::Quote {
|
||||
parser.token_end = stream.get_pos() - 1;
|
||||
parser.token_end = stream.pos - 1;
|
||||
}
|
||||
add_token(&mut parser, stream, false);
|
||||
}
|
||||
@@ -315,7 +310,7 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
parser.is_token_email = true;
|
||||
}
|
||||
b'=' if parser.is_token_start && !parser.is_escaped => {
|
||||
if let Some(token) = parse_encoded_word(stream) {
|
||||
if let (d_bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||
let add_space = parser.state != AddressState::Quote; // Make borrow-checker happy
|
||||
add_token(&mut parser, stream, add_space);
|
||||
(if parser.state != AddressState::Comment {
|
||||
@@ -323,7 +318,9 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
} else {
|
||||
&mut parser.comment_tokens
|
||||
})
|
||||
.push(token);
|
||||
.push(token.into());
|
||||
stream.pos += d_bytes_read;
|
||||
iter = stream.data[stream.pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -336,10 +333,10 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
}
|
||||
if parser.state == AddressState::Quote {
|
||||
if parser.token_start == 0 {
|
||||
parser.token_start = stream.get_pos();
|
||||
parser.token_start = stream.pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = stream.pos;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
@@ -373,12 +370,7 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
add_group(&mut parser);
|
||||
continue;
|
||||
}
|
||||
0..=0x7f => (),
|
||||
_ => {
|
||||
if parser.is_token_safe {
|
||||
parser.is_token_safe = false;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if parser.is_escaped {
|
||||
@@ -390,10 +382,10 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
}
|
||||
|
||||
if parser.token_start == 0 {
|
||||
parser.token_start = stream.get_pos();
|
||||
parser.token_start = stream.pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = stream.pos;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -420,7 +412,7 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
mod tests {
|
||||
#[test]
|
||||
fn parse_addresses() {
|
||||
use crate::parsers::{fields::address::parse_address, message_stream::MessageStream};
|
||||
use crate::parsers::fields::address::parse_address;
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -941,8 +933,9 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let result = parse_address(&MessageStream::new(unsafe { str.as_bytes_mut() }));
|
||||
println!("Testiong {}", input.0);
|
||||
let str = input.0.to_string();
|
||||
let result = parse_address(&mut MessageStream::new(str.as_bytes()));
|
||||
let expected: Address = serde_yaml::from_str(input.1).unwrap_or(Address::Empty);
|
||||
|
||||
/*if input.0.len() >= 70 {
|
||||
|
||||
@@ -15,12 +15,8 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
decoders::{
|
||||
charsets::map::{decoder_default, get_charset_decoder},
|
||||
encoded_word::parse_encoded_word,
|
||||
hex::decode_hex,
|
||||
},
|
||||
parsers::message_stream::MessageStream,
|
||||
decoders::{charsets::map::get_charset_decoder, encoded_word::decode_rfc2047, hex::decode_hex},
|
||||
parsers::message::MessageStream,
|
||||
ContentType,
|
||||
};
|
||||
|
||||
@@ -58,24 +54,20 @@ struct ContentTypeParser<'x> {
|
||||
is_encoded_attribute: bool,
|
||||
is_escaped: bool,
|
||||
is_lower_case: bool,
|
||||
is_token_safe: bool,
|
||||
is_token_start: bool,
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn reset_parser(parser: &mut ContentTypeParser) {
|
||||
parser.token_start = 0;
|
||||
parser.is_token_safe = true;
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
|
||||
fn add_attribute<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>) -> bool {
|
||||
if parser.token_start > 0 {
|
||||
let mut attr = stream.get_string(
|
||||
parser.token_start - 1,
|
||||
parser.token_end,
|
||||
parser.is_token_safe,
|
||||
);
|
||||
let mut attr = Some(String::from_utf8_lossy(
|
||||
&stream.data[parser.token_start - 1..parser.token_end],
|
||||
));
|
||||
|
||||
if !parser.is_lower_case {
|
||||
attr.as_mut().unwrap().to_mut().make_ascii_lowercase();
|
||||
@@ -98,13 +90,8 @@ fn add_attribute<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<
|
||||
|
||||
fn add_attribute_parameter<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>) {
|
||||
if parser.token_start > 0 {
|
||||
let attr_part = stream
|
||||
.get_string(
|
||||
parser.token_start - 1,
|
||||
parser.token_end,
|
||||
parser.is_token_safe,
|
||||
)
|
||||
.unwrap();
|
||||
let attr_part =
|
||||
String::from_utf8_lossy(&stream.data[parser.token_start - 1..parser.token_end]);
|
||||
|
||||
if parser.attr_charset.is_none() {
|
||||
parser.attr_charset = attr_part.into();
|
||||
@@ -134,19 +121,13 @@ fn add_partial_value<'x>(
|
||||
if parser.token_start > 0 {
|
||||
let in_quote = parser.state == ContentState::AttributeQuotedValue;
|
||||
|
||||
parser.values.push(
|
||||
stream
|
||||
.get_string(
|
||||
parser.token_start - 1,
|
||||
if in_quote && to_cur_pos {
|
||||
stream.get_pos() - 1
|
||||
} else {
|
||||
parser.token_end
|
||||
},
|
||||
parser.is_token_safe,
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
parser.values.push(String::from_utf8_lossy(
|
||||
&stream.data[parser.token_start - 1..if in_quote && to_cur_pos {
|
||||
stream.pos - 1
|
||||
} else {
|
||||
parser.token_end
|
||||
}],
|
||||
));
|
||||
if !in_quote {
|
||||
parser.values.push(" ".into());
|
||||
}
|
||||
@@ -162,11 +143,9 @@ fn add_value<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>)
|
||||
|
||||
let has_values = !parser.values.is_empty();
|
||||
let value = if parser.token_start > 0 {
|
||||
stream.get_string(
|
||||
parser.token_start - 1,
|
||||
parser.token_end,
|
||||
parser.is_token_safe,
|
||||
)
|
||||
Some(String::from_utf8_lossy(
|
||||
&stream.data[parser.token_start - 1..parser.token_end],
|
||||
))
|
||||
} else {
|
||||
if !has_values {
|
||||
return;
|
||||
@@ -200,16 +179,17 @@ fn add_value<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>)
|
||||
|
||||
if parser.is_encoded_attribute {
|
||||
if let (true, decoded_bytes) = decode_hex(value.as_bytes()) {
|
||||
value = get_charset_decoder(
|
||||
parser
|
||||
.attr_charset
|
||||
.as_ref()
|
||||
.unwrap_or(&"utf-8".into())
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap_or(decoder_default)(&decoded_bytes)
|
||||
.into_owned()
|
||||
.into();
|
||||
value = if let Some(decoder) = parser
|
||||
.attr_charset
|
||||
.as_ref()
|
||||
.and_then(|c| get_charset_decoder(c.as_bytes()))
|
||||
{
|
||||
decoder(&decoded_bytes).into()
|
||||
} else {
|
||||
String::from_utf8(decoded_bytes)
|
||||
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
||||
.into()
|
||||
}
|
||||
}
|
||||
parser.is_encoded_attribute = false;
|
||||
}
|
||||
@@ -239,15 +219,10 @@ fn add_value<'x>(parser: &mut ContentTypeParser<'x>, stream: &MessageStream<'x>)
|
||||
|
||||
fn add_attr_position(parser: &mut ContentTypeParser, stream: &MessageStream) -> bool {
|
||||
if parser.token_start > 0 {
|
||||
parser.attr_position = stream
|
||||
.get_string(
|
||||
parser.token_start - 1,
|
||||
parser.token_end,
|
||||
parser.is_token_safe,
|
||||
)
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap_or(0);
|
||||
parser.attr_position =
|
||||
String::from_utf8_lossy(&stream.data[parser.token_start - 1..parser.token_end])
|
||||
.parse()
|
||||
.unwrap_or(0);
|
||||
|
||||
reset_parser(parser);
|
||||
true
|
||||
@@ -269,7 +244,7 @@ fn merge_continuations(parser: &mut ContentTypeParser) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<'x>> {
|
||||
pub fn parse_content_type<'x>(stream: &mut MessageStream<'x>) -> Option<ContentType<'x>> {
|
||||
let mut parser = ContentTypeParser {
|
||||
state: ContentState::Type,
|
||||
state_stack: Vec::new(),
|
||||
@@ -288,7 +263,6 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
is_continuation: false,
|
||||
is_encoded_attribute: false,
|
||||
is_lower_case: true,
|
||||
is_token_safe: true,
|
||||
is_token_start: true,
|
||||
is_escaped: false,
|
||||
|
||||
@@ -296,7 +270,10 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
token_end: 0,
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
stream.pos += 1;
|
||||
match ch {
|
||||
b' ' | b'\t' => {
|
||||
if !parser.is_token_start {
|
||||
@@ -304,10 +281,10 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
}
|
||||
if let ContentState::AttributeQuotedValue = parser.state {
|
||||
if parser.token_start == 0 {
|
||||
parser.token_start = stream.get_pos();
|
||||
parser.token_start = stream.pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = stream.pos;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
@@ -333,10 +310,11 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match stream.peek() {
|
||||
match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
parser.state = ContentState::AttributeName;
|
||||
stream.advance(1);
|
||||
stream.pos += 1;
|
||||
iter.next();
|
||||
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
@@ -347,6 +325,7 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
if parser.continuations.is_some() {
|
||||
merge_continuations(&mut parser);
|
||||
}
|
||||
|
||||
return if let Some(content_type) = parser.c_type {
|
||||
Some(ContentType {
|
||||
c_type: content_type,
|
||||
@@ -416,9 +395,11 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
ContentState::AttributeValue | ContentState::AttributeQuotedValue
|
||||
if parser.is_token_start =>
|
||||
{
|
||||
if let Some(token) = parse_encoded_word(stream) {
|
||||
if let (d_bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||
add_partial_value(&mut parser, stream, false);
|
||||
parser.values.push(token);
|
||||
parser.values.push(token.into());
|
||||
stream.pos += d_bytes_read;
|
||||
iter = stream.data[stream.pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -496,12 +477,7 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
continue;
|
||||
}
|
||||
b'\r' => continue,
|
||||
0..=0x7f => (),
|
||||
_ => {
|
||||
if parser.is_token_safe {
|
||||
parser.is_token_safe = false;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if parser.is_escaped {
|
||||
@@ -513,20 +489,23 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
}
|
||||
|
||||
if parser.token_start == 0 {
|
||||
parser.token_start = stream.get_pos();
|
||||
parser.token_start = stream.pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = stream.pos;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
#[test]
|
||||
fn parse_content_fields() {
|
||||
use crate::parsers::{fields::content_type::ContentType, message_stream::MessageStream};
|
||||
use crate::parsers::fields::content_type::ContentType;
|
||||
|
||||
use super::parse_content_type;
|
||||
|
||||
@@ -1353,9 +1332,9 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let str = input.0.to_string();
|
||||
|
||||
let result = parse_content_type(&MessageStream::new(unsafe { str.as_bytes_mut() }));
|
||||
let result = parse_content_type(&mut MessageStream::new(str.as_bytes()));
|
||||
let expected: Option<ContentType> = serde_yaml::from_str(input.1).unwrap_or(None);
|
||||
|
||||
/*
|
||||
|
||||
+21
-22
@@ -11,7 +11,7 @@
|
||||
|
||||
use std::fmt;
|
||||
|
||||
use crate::{parsers::message_stream::MessageStream, DateTime};
|
||||
use crate::{parsers::message::MessageStream, DateTime};
|
||||
|
||||
impl DateTime {
|
||||
/// Returns an ISO-8601 representation of the parsed RFC5322 datetime field
|
||||
@@ -37,7 +37,7 @@ impl fmt::Display for DateTime {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<DateTime> {
|
||||
pub fn parse_date(stream: &mut MessageStream) -> Option<DateTime> {
|
||||
let mut pos = 0;
|
||||
let mut parts = [0u32; 7];
|
||||
let mut parts_sizes = [
|
||||
@@ -57,13 +57,18 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
||||
let mut ignore = true;
|
||||
let mut comment_count = 0;
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
let mut next_part = false;
|
||||
stream.pos += 1;
|
||||
|
||||
match ch {
|
||||
b'\n' => match stream.peek() {
|
||||
b'\n' => match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
stream.pos += 1;
|
||||
|
||||
if !is_new_token && !ignore && comment_count == 0 {
|
||||
next_part = true;
|
||||
} else {
|
||||
@@ -78,7 +83,9 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
||||
} else if *ch == b'(' {
|
||||
comment_count += 1;
|
||||
} else if *ch == b'\\' {
|
||||
stream.skip_byte(&b')');
|
||||
if let Some(b')') = stream.data.get(stream.pos) {
|
||||
stream.pos += 1;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -115,11 +122,9 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
||||
b'a'..=b'z' | b'A'..=b'Z' => {
|
||||
if pos == 1 {
|
||||
if (1..=2).contains(&month_pos) {
|
||||
// SAFE: MONTH_HASH's size is u8::MAX
|
||||
month_hash += unsafe {
|
||||
*MONTH_HASH
|
||||
.get_unchecked((if *ch <= b'Z' { *ch + 32 } else { *ch }) as usize)
|
||||
} as usize;
|
||||
month_hash += MONTH_HASH
|
||||
[(if *ch <= b'Z' { *ch + 32 } else { *ch }) as usize]
|
||||
as usize;
|
||||
}
|
||||
month_pos += 1;
|
||||
}
|
||||
@@ -133,12 +138,7 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
||||
continue;
|
||||
}
|
||||
b',' | b'\r' => (),
|
||||
_ => {
|
||||
if abort_on_invalid {
|
||||
stream.rewind(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if next_part {
|
||||
@@ -158,8 +158,7 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
||||
parts[2]
|
||||
},
|
||||
month: if month_pos == 3 && month_hash <= 30 {
|
||||
// SAFE: MONTH_MAP's size is 31
|
||||
(unsafe { *MONTH_MAP.get_unchecked(month_hash) }) as u32
|
||||
MONTH_MAP[month_hash] as u32
|
||||
} else {
|
||||
parts[1]
|
||||
},
|
||||
@@ -196,7 +195,7 @@ pub static MONTH_MAP: &[u8; 31] = &[
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::{fields::date::parse_date, message_stream::MessageStream};
|
||||
use crate::parsers::{fields::date::parse_date, message::MessageStream};
|
||||
|
||||
#[test]
|
||||
fn parse_dates() {
|
||||
@@ -261,8 +260,8 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
match parse_date(&MessageStream::new(unsafe { str.as_bytes_mut() }), false) {
|
||||
let str = input.0.to_string();
|
||||
match parse_date(&mut MessageStream::new(str.as_bytes())) {
|
||||
Some(date) => {
|
||||
//println!("{} -> {}", input.0.escape_debug(), date.to_iso8601());
|
||||
assert_eq!(input.1, date.to_iso8601());
|
||||
|
||||
+23
-24
@@ -11,23 +11,28 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
let mut token_start: usize = 0;
|
||||
let mut token_end: usize = 0;
|
||||
let mut is_token_safe = true;
|
||||
let mut is_id_part = false;
|
||||
let mut ids = Vec::new();
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
stream.pos += 1;
|
||||
match ch {
|
||||
b'\n' => match stream.peek() {
|
||||
b'\n' => match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
stream.pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => return if !ids.is_empty() { Some(ids) } else { None },
|
||||
_ => {
|
||||
return if !ids.is_empty() { Some(ids) } else { None };
|
||||
}
|
||||
},
|
||||
b'<' => {
|
||||
is_id_part = true;
|
||||
@@ -36,38 +41,32 @@ pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
b'>' => {
|
||||
is_id_part = false;
|
||||
if token_start > 0 {
|
||||
ids.push(
|
||||
stream
|
||||
.get_string(token_start - 1, token_end, is_token_safe)
|
||||
.unwrap(),
|
||||
);
|
||||
is_token_safe = true;
|
||||
ids.push(String::from_utf8_lossy(
|
||||
&stream.data[token_start - 1..token_end],
|
||||
));
|
||||
token_start = 0;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
b' ' | b'\t' | b'\r' => continue,
|
||||
0..=0x7f => (),
|
||||
_ => {
|
||||
if is_token_safe {
|
||||
is_token_safe = false;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
if is_id_part {
|
||||
if token_start == 0 {
|
||||
token_start = stream.get_pos();
|
||||
token_start = stream.pos;
|
||||
}
|
||||
token_end = stream.get_pos();
|
||||
token_end = stream.pos;
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::{fields::id::parse_id, message_stream::MessageStream};
|
||||
use crate::parsers::fields::id::parse_id;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
#[test]
|
||||
fn parse_message_ids() {
|
||||
@@ -103,10 +102,10 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let str = input.0.to_string();
|
||||
assert_eq!(
|
||||
input.1,
|
||||
parse_id(&MessageStream::new(unsafe { str.as_bytes_mut() })).unwrap(),
|
||||
parse_id(&mut MessageStream::new(str.as_bytes())).unwrap(),
|
||||
"Failed to parse '{:?}'",
|
||||
input.0
|
||||
);
|
||||
|
||||
+24
-30
@@ -11,12 +11,11 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::{decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream};
|
||||
use crate::{decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream};
|
||||
|
||||
struct ListParser<'x> {
|
||||
token_start: usize,
|
||||
token_end: usize,
|
||||
is_token_safe: bool,
|
||||
is_token_start: bool,
|
||||
tokens: Vec<Cow<'x, str>>,
|
||||
list: Vec<Cow<'x, str>>,
|
||||
@@ -27,22 +26,15 @@ fn add_token<'x>(parser: &mut ListParser<'x>, stream: &MessageStream<'x>, add_sp
|
||||
if !parser.tokens.is_empty() {
|
||||
parser.tokens.push(" ".into());
|
||||
}
|
||||
parser.tokens.push(
|
||||
stream
|
||||
.get_string(
|
||||
parser.token_start - 1,
|
||||
parser.token_end,
|
||||
parser.is_token_safe,
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
parser.tokens.push(String::from_utf8_lossy(
|
||||
&stream.data[parser.token_start - 1..parser.token_end],
|
||||
));
|
||||
|
||||
if add_space {
|
||||
parser.tokens.push(" ".into());
|
||||
}
|
||||
|
||||
parser.token_start = 0;
|
||||
parser.is_token_safe = true;
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
}
|
||||
@@ -59,31 +51,35 @@ fn add_tokens_to_list(parser: &mut ListParser) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
pub fn parse_comma_separared<'x>(stream: &mut MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
let mut parser = ListParser {
|
||||
token_start: 0,
|
||||
token_end: 0,
|
||||
is_token_safe: true,
|
||||
is_token_start: true,
|
||||
tokens: Vec::new(),
|
||||
list: Vec::new(),
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
stream.pos += 1;
|
||||
match ch {
|
||||
b'\n' => {
|
||||
add_token(&mut parser, stream, false);
|
||||
|
||||
match stream.peek() {
|
||||
match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
stream.pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
add_tokens_to_list(&mut parser);
|
||||
|
||||
return if !parser.list.is_empty() {
|
||||
parser.list.into()
|
||||
} else {
|
||||
@@ -99,9 +95,11 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
||||
continue;
|
||||
}
|
||||
b'=' if parser.is_token_start => {
|
||||
if let Some(token) = parse_encoded_word(stream) {
|
||||
if let (bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||
add_token(&mut parser, stream, true);
|
||||
parser.tokens.push(token);
|
||||
parser.tokens.push(token.into());
|
||||
stream.pos += bytes_read;
|
||||
iter = stream.data[stream.pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -111,12 +109,7 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
||||
continue;
|
||||
}
|
||||
b'\r' => continue,
|
||||
0..=0x7f => (),
|
||||
_ => {
|
||||
if parser.is_token_safe {
|
||||
parser.is_token_safe = false;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if parser.is_token_start {
|
||||
@@ -124,10 +117,10 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
||||
}
|
||||
|
||||
if parser.token_start == 0 {
|
||||
parser.token_start = stream.get_pos();
|
||||
parser.token_start = stream.pos;
|
||||
}
|
||||
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = stream.pos;
|
||||
}
|
||||
|
||||
None
|
||||
@@ -135,7 +128,8 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::{fields::list::parse_comma_separared, message_stream::MessageStream};
|
||||
use crate::parsers::fields::list::parse_comma_separared;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
#[test]
|
||||
fn parse_comma_separated_text() {
|
||||
@@ -177,9 +171,9 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let str = input.0.to_string();
|
||||
assert_eq!(
|
||||
parse_comma_separared(&MessageStream::new(unsafe { str.as_bytes_mut() }),).unwrap(),
|
||||
parse_comma_separared(&mut MessageStream::new(str.as_bytes()),).unwrap(),
|
||||
input.1
|
||||
);
|
||||
}
|
||||
|
||||
+154
-148
@@ -21,7 +21,7 @@ use self::{
|
||||
unstructured::parse_unstructured,
|
||||
};
|
||||
|
||||
use super::message_stream::MessageStream;
|
||||
use super::message::MessageStream;
|
||||
|
||||
pub mod address;
|
||||
pub mod content_type;
|
||||
@@ -35,166 +35,166 @@ pub trait MessageField<'x>: FieldSet<'x> + MimeFieldGet<'x> {}
|
||||
impl<'x, T: FieldSet<'x> + MimeFieldGet<'x>> MessageField<'x> for T {}
|
||||
|
||||
pub trait FieldSet<'x> {
|
||||
fn set_date(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_sender(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_received(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_references(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_cc(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_comments(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_resent_cc(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_content_id(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_resent_message_id(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_reply_to(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_resent_to(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_resent_bcc(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_subject(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_keywords(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_list_help(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_list_id(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_list_owner(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_resent_date(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_to(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_bcc(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_from(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_content_transfer_encoding(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_return_path(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_list_archive(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_resent_sender(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_list_subscribe(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_message_id(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_content_type(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_list_post(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_in_reply_to(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_content_description(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_resent_from(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_content_disposition(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_list_unsubscribe(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_mime_version(&mut self, stream: &MessageStream<'x>);
|
||||
fn set_unsupported(&mut self, stream: &MessageStream<'x>, name: &'x [u8]);
|
||||
fn set_date(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_sender(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_received(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_references(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_cc(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_comments(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_resent_cc(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_content_id(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_resent_message_id(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_reply_to(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_resent_to(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_resent_bcc(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_subject(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_keywords(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_list_help(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_list_id(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_list_owner(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_resent_date(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_to(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_bcc(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_from(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_content_transfer_encoding(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_return_path(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_list_archive(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_resent_sender(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_list_subscribe(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_message_id(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_content_type(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_list_post(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_in_reply_to(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_content_description(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_resent_from(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_content_disposition(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_list_unsubscribe(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_mime_version(&mut self, stream: &mut MessageStream<'x>);
|
||||
fn set_unsupported(&mut self, stream: &mut MessageStream<'x>, name: &'x [u8]);
|
||||
}
|
||||
|
||||
impl<'x> FieldSet<'x> for MessageHeader<'x> {
|
||||
fn set_date(&mut self, stream: &MessageStream<'x>) {
|
||||
self.date = self::date::parse_date(stream, false);
|
||||
fn set_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.date = self::date::parse_date(stream);
|
||||
}
|
||||
|
||||
fn set_sender(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.sender = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_received(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_received(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_line(&mut self.received, parse_raw(stream));
|
||||
}
|
||||
|
||||
fn set_references(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_references(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.references = parse_id(stream);
|
||||
}
|
||||
|
||||
fn set_cc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.cc = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_comments(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_comments(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_line(&mut self.comments, parse_unstructured(stream));
|
||||
}
|
||||
|
||||
fn set_resent_cc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_address_line(&mut self.resent_cc, parse_address(stream));
|
||||
}
|
||||
|
||||
fn set_resent_message_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_line_many(&mut self.resent_message_id, parse_id(stream));
|
||||
}
|
||||
|
||||
fn set_reply_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.reply_to = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_resent_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_address_line(&mut self.resent_to, parse_address(stream));
|
||||
}
|
||||
|
||||
fn set_resent_bcc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_address_line(&mut self.resent_bcc, parse_address(stream));
|
||||
}
|
||||
|
||||
fn set_subject(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_subject(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.subject = parse_unstructured(stream);
|
||||
}
|
||||
|
||||
fn set_keywords(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_keywords(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_line_many(&mut self.keywords, parse_comma_separared(stream));
|
||||
}
|
||||
|
||||
fn set_list_help(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_help(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.list_help = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_list_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.list_id = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_list_owner(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_owner(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.list_owner = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_resent_date(&mut self, stream: &MessageStream<'x>) {
|
||||
append_date_line(&mut self.resent_date, self::date::parse_date(stream, false));
|
||||
fn set_resent_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_date_line(&mut self.resent_date, self::date::parse_date(stream));
|
||||
}
|
||||
|
||||
fn set_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.to = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_bcc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.bcc = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_from(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.from = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_return_path(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_return_path(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_line_many(&mut self.return_path, parse_id(stream));
|
||||
}
|
||||
|
||||
fn set_list_archive(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_archive(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.list_archive = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_resent_sender(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_address_line(&mut self.resent_sender, parse_address(stream));
|
||||
}
|
||||
|
||||
fn set_list_subscribe(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_subscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.list_subscribe = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_message_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.message_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
||||
}
|
||||
|
||||
fn set_list_post(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_post(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.list_post = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_in_reply_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_in_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.reply_to = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_resent_from(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||
append_address_line(&mut self.resent_from, parse_address(stream));
|
||||
}
|
||||
|
||||
fn set_list_unsubscribe(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_unsubscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.list_unsubscribe = parse_address(stream);
|
||||
}
|
||||
|
||||
fn set_mime_version(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_mime_version(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.mime_version = parse_raw(stream);
|
||||
}
|
||||
|
||||
fn set_unsupported(&mut self, stream: &MessageStream<'x>, name: &'x [u8]) {
|
||||
fn set_unsupported(&mut self, stream: &mut MessageStream<'x>, name: &'x [u8]) {
|
||||
if let Ok(name) = std::str::from_utf8(name) {
|
||||
if let Some(value) = parse_unstructured(stream) {
|
||||
if let Some(arr) = self.others.get_mut(name) {
|
||||
@@ -206,319 +206,325 @@ impl<'x> FieldSet<'x> for MessageHeader<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_content_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
||||
}
|
||||
|
||||
fn set_content_transfer_encoding(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_transfer_encoding(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_transfer_encoding = parse_unstructured(stream);
|
||||
}
|
||||
|
||||
fn set_content_type(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_type(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_type = self::content_type::parse_content_type(stream);
|
||||
}
|
||||
|
||||
fn set_content_description(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_description(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_description = parse_unstructured(stream);
|
||||
}
|
||||
|
||||
fn set_content_disposition(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_disposition(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_disposition = self::content_type::parse_content_type(stream);
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> FieldSet<'x> for MimeHeader<'x> {
|
||||
fn set_date(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_sender(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_received(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_received(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_references(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_references(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_cc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_comments(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_comments(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_resent_cc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_cc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_resent_message_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_reply_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_resent_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_resent_bcc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_subject(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_subject(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_keywords(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_keywords(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_list_help(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_help(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_list_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_list_owner(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_owner(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_resent_date(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_date(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_bcc(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_bcc(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_from(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_return_path(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_return_path(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_list_archive(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_archive(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_resent_sender(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_sender(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_list_subscribe(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_subscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_message_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_message_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_list_post(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_post(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_in_reply_to(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_in_reply_to(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_resent_from(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_resent_from(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_list_unsubscribe(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_list_unsubscribe(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_mime_version(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_mime_version(&mut self, stream: &mut MessageStream<'x>) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_unsupported(&mut self, stream: &MessageStream<'x>, _name: &'x [u8]) {
|
||||
fn set_unsupported(&mut self, stream: &mut MessageStream<'x>, _name: &'x [u8]) {
|
||||
parse_and_ignore(stream);
|
||||
}
|
||||
|
||||
fn set_content_id(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_id(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_id = parse_id(stream).map(|mut v| v.pop().unwrap());
|
||||
}
|
||||
|
||||
fn set_content_transfer_encoding(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_transfer_encoding(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_transfer_encoding = parse_unstructured(stream);
|
||||
}
|
||||
|
||||
fn set_content_type(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_type(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_type = self::content_type::parse_content_type(stream);
|
||||
}
|
||||
|
||||
fn set_content_description(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_description(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_description = parse_unstructured(stream);
|
||||
}
|
||||
|
||||
fn set_content_disposition(&mut self, stream: &MessageStream<'x>) {
|
||||
fn set_content_disposition(&mut self, stream: &mut MessageStream<'x>) {
|
||||
self.content_disposition = self::content_type::parse_content_type(stream);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_date<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_date<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_date(stream);
|
||||
}
|
||||
|
||||
pub fn parse_sender<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_sender<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_sender(stream);
|
||||
}
|
||||
|
||||
pub fn parse_received<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_received<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_received(stream);
|
||||
}
|
||||
|
||||
pub fn parse_references<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_references<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_references(stream);
|
||||
}
|
||||
|
||||
pub fn parse_cc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_cc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_cc(stream);
|
||||
}
|
||||
|
||||
pub fn parse_comments<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_comments<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_comments(stream);
|
||||
}
|
||||
|
||||
pub fn parse_resent_cc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_resent_cc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_resent_cc(stream);
|
||||
}
|
||||
|
||||
pub fn parse_content_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_content_id<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_content_id(stream);
|
||||
}
|
||||
|
||||
pub fn parse_resent_message_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_resent_message_id<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
stream: &mut MessageStream<'x>,
|
||||
) {
|
||||
header.set_resent_message_id(stream);
|
||||
}
|
||||
|
||||
pub fn parse_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_reply_to(stream);
|
||||
}
|
||||
|
||||
pub fn parse_resent_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_resent_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_resent_to(stream);
|
||||
}
|
||||
|
||||
pub fn parse_resent_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_resent_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_resent_bcc(stream);
|
||||
}
|
||||
|
||||
pub fn parse_subject<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_subject<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_subject(stream);
|
||||
}
|
||||
|
||||
pub fn parse_keywords<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_keywords<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_keywords(stream);
|
||||
}
|
||||
|
||||
pub fn parse_list_help<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_list_help<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_list_help(stream);
|
||||
}
|
||||
|
||||
pub fn parse_list_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_list_id<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_list_id(stream);
|
||||
}
|
||||
|
||||
pub fn parse_list_owner<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_list_owner<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_list_owner(stream);
|
||||
}
|
||||
|
||||
pub fn parse_resent_date<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_resent_date<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_resent_date(stream);
|
||||
}
|
||||
|
||||
pub fn parse_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_to(stream);
|
||||
}
|
||||
|
||||
pub fn parse_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_bcc<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_bcc(stream);
|
||||
}
|
||||
|
||||
pub fn parse_from<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_from<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_from(stream);
|
||||
}
|
||||
|
||||
pub fn parse_content_transfer_encoding<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
stream: &MessageStream<'x>,
|
||||
stream: &mut MessageStream<'x>,
|
||||
) {
|
||||
header.set_content_transfer_encoding(stream);
|
||||
}
|
||||
|
||||
pub fn parse_return_path<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_return_path<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_return_path(stream);
|
||||
}
|
||||
|
||||
pub fn parse_list_archive<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_list_archive<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_list_archive(stream);
|
||||
}
|
||||
|
||||
pub fn parse_resent_sender<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_resent_sender<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_resent_sender(stream);
|
||||
}
|
||||
|
||||
pub fn parse_list_subscribe<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_list_subscribe<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_list_subscribe(stream);
|
||||
}
|
||||
|
||||
pub fn parse_message_id<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_message_id<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_message_id(stream);
|
||||
}
|
||||
|
||||
pub fn parse_content_type<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_content_type<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_content_type(stream);
|
||||
}
|
||||
|
||||
pub fn parse_list_post<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_list_post<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_list_post(stream);
|
||||
}
|
||||
|
||||
pub fn parse_in_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_in_reply_to<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_in_reply_to(stream);
|
||||
}
|
||||
|
||||
pub fn parse_content_description<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
stream: &MessageStream<'x>,
|
||||
stream: &mut MessageStream<'x>,
|
||||
) {
|
||||
header.set_content_description(stream);
|
||||
}
|
||||
|
||||
pub fn parse_resent_from<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_resent_from<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_resent_from(stream);
|
||||
}
|
||||
|
||||
pub fn parse_content_disposition<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
stream: &MessageStream<'x>,
|
||||
stream: &mut MessageStream<'x>,
|
||||
) {
|
||||
header.set_content_disposition(stream);
|
||||
}
|
||||
|
||||
pub fn parse_list_unsubscribe<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_list_unsubscribe<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
stream: &mut MessageStream<'x>,
|
||||
) {
|
||||
header.set_list_unsubscribe(stream);
|
||||
}
|
||||
|
||||
pub fn parse_mime_version<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) {
|
||||
pub fn parse_mime_version<'x>(header: &mut dyn MessageField<'x>, stream: &mut MessageStream<'x>) {
|
||||
header.set_mime_version(stream);
|
||||
}
|
||||
|
||||
@@ -569,11 +575,11 @@ fn append_line_many<'x>(
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_no_op<'x>(_header: &mut dyn MessageField<'x>, _stream: &MessageStream<'x>) {}
|
||||
pub fn parse_no_op<'x>(_header: &mut dyn MessageField<'x>, _stream: &mut MessageStream<'x>) {}
|
||||
|
||||
pub fn parse_unsupported<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
stream: &MessageStream<'x>,
|
||||
stream: &mut MessageStream<'x>,
|
||||
|
||||
name: &'x [u8],
|
||||
) {
|
||||
|
||||
+27
-26
@@ -11,59 +11,59 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
pub fn parse_raw<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
pub fn parse_raw<'x>(stream: &mut MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
let mut token_start: usize = 0;
|
||||
let mut token_end: usize = 0;
|
||||
let mut is_token_safe = true;
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
stream.pos += 1;
|
||||
match ch {
|
||||
b'\n' => match stream.peek() {
|
||||
b'\n' => match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
stream.pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
return if token_start > 0 {
|
||||
stream
|
||||
.get_string(token_start - 1, token_end, is_token_safe)
|
||||
.unwrap()
|
||||
.into()
|
||||
String::from_utf8_lossy(&stream.data[token_start - 1..token_end]).into()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
}
|
||||
},
|
||||
b' ' | b'\t' | b'\r' => continue,
|
||||
0..=0x7f => (),
|
||||
_ => {
|
||||
if is_token_safe {
|
||||
is_token_safe = false;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if token_start == 0 {
|
||||
token_start = stream.get_pos();
|
||||
token_start = stream.pos;
|
||||
}
|
||||
|
||||
token_end = stream.get_pos();
|
||||
token_end = stream.pos;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn parse_and_ignore(stream: &MessageStream) {
|
||||
while let Some(ch) = stream.next() {
|
||||
pub fn parse_and_ignore(stream: &mut MessageStream) {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
stream.pos += 1;
|
||||
|
||||
if ch == &b'\n' {
|
||||
match stream.peek() {
|
||||
match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
stream.pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => return,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,8 @@ pub fn parse_and_ignore(stream: &MessageStream) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::{fields::raw::parse_raw, message_stream::MessageStream};
|
||||
use crate::parsers::fields::raw::parse_raw;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
#[test]
|
||||
fn parse_raw_text() {
|
||||
@@ -93,9 +94,9 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let str = input.0.to_string();
|
||||
assert_eq!(
|
||||
parse_raw(&MessageStream::new(unsafe { str.as_bytes_mut() })).unwrap(),
|
||||
parse_raw(&mut MessageStream::new(str.as_bytes())).unwrap(),
|
||||
input.1,
|
||||
"Failed for '{:?}'",
|
||||
input.0
|
||||
|
||||
@@ -11,11 +11,10 @@
|
||||
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::{decoders::encoded_word::parse_encoded_word, parsers::message_stream::MessageStream};
|
||||
use crate::{decoders::encoded_word::decode_rfc2047, parsers::message::MessageStream};
|
||||
struct UnstructuredParser<'x> {
|
||||
token_start: usize,
|
||||
token_end: usize,
|
||||
is_token_safe: bool,
|
||||
is_token_start: bool,
|
||||
tokens: Vec<Cow<'x, str>>,
|
||||
}
|
||||
@@ -25,46 +24,42 @@ fn add_token<'x>(parser: &mut UnstructuredParser<'x>, stream: &MessageStream<'x>
|
||||
if !parser.tokens.is_empty() {
|
||||
parser.tokens.push(" ".into());
|
||||
}
|
||||
parser.tokens.push(
|
||||
stream
|
||||
.get_string(
|
||||
parser.token_start - 1,
|
||||
parser.token_end,
|
||||
parser.is_token_safe,
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
parser.tokens.push(String::from_utf8_lossy(
|
||||
&stream.data[parser.token_start - 1..parser.token_end],
|
||||
));
|
||||
|
||||
if add_space {
|
||||
parser.tokens.push(" ".into());
|
||||
}
|
||||
|
||||
parser.token_start = 0;
|
||||
parser.is_token_safe = true;
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
pub fn parse_unstructured<'x>(stream: &mut MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
let mut parser = UnstructuredParser {
|
||||
token_start: 0,
|
||||
token_end: 0,
|
||||
is_token_safe: true,
|
||||
is_token_start: true,
|
||||
tokens: Vec::new(),
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[stream.pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
stream.pos += 1;
|
||||
match ch {
|
||||
b'\n' => {
|
||||
add_token(&mut parser, stream, false);
|
||||
|
||||
match stream.peek() {
|
||||
match stream.data.get(stream.pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
stream.pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
@@ -83,19 +78,16 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
||||
continue;
|
||||
}
|
||||
b'=' if parser.is_token_start => {
|
||||
if let Some(token) = parse_encoded_word(stream) {
|
||||
if let (bytes_read, Some(token)) = decode_rfc2047(stream, stream.pos) {
|
||||
add_token(&mut parser, stream, true);
|
||||
parser.tokens.push(token);
|
||||
parser.tokens.push(token.into());
|
||||
stream.pos += bytes_read;
|
||||
iter = stream.data[stream.pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
b'\r' => continue,
|
||||
0..=0x7f => (),
|
||||
_ => {
|
||||
if parser.is_token_safe {
|
||||
parser.is_token_safe = false;
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
|
||||
if parser.is_token_start {
|
||||
@@ -103,10 +95,10 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
||||
}
|
||||
|
||||
if parser.token_start == 0 {
|
||||
parser.token_start = stream.get_pos();
|
||||
parser.token_start = stream.pos;
|
||||
}
|
||||
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = stream.pos;
|
||||
}
|
||||
|
||||
None
|
||||
@@ -117,9 +109,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn parse_unstructured_text() {
|
||||
use crate::parsers::{
|
||||
fields::unstructured::parse_unstructured, message_stream::MessageStream,
|
||||
};
|
||||
use crate::parsers::fields::unstructured::parse_unstructured;
|
||||
use crate::parsers::message::MessageStream;
|
||||
|
||||
let inputs = [
|
||||
("Saying Hello\n", "Saying Hello", true),
|
||||
@@ -191,9 +182,9 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
let str = input.0.to_string();
|
||||
assert_eq!(
|
||||
parse_unstructured(&MessageStream::new(unsafe { str.as_bytes_mut() }),).unwrap(),
|
||||
parse_unstructured(&mut MessageStream::new(str.as_bytes()),).unwrap(),
|
||||
input.1,
|
||||
"Failed to parse '{:?}'",
|
||||
input.0
|
||||
|
||||
+28
-31
@@ -9,9 +9,12 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use crate::{parsers::message_stream::MessageStream, MessageHeader, MimeHeader};
|
||||
use crate::{MessageHeader, MimeHeader};
|
||||
|
||||
use super::fields::{parse_unsupported, MessageField};
|
||||
use super::{
|
||||
fields::{parse_unsupported, MessageField},
|
||||
message::MessageStream,
|
||||
};
|
||||
|
||||
impl<'x> MessageHeader<'x> {
|
||||
pub fn new() -> MessageHeader<'x> {
|
||||
@@ -46,13 +49,16 @@ impl<'x> MimeHeader<'x> {
|
||||
}
|
||||
|
||||
pub enum HeaderParserResult<'x> {
|
||||
Supported(fn(&mut dyn MessageField<'x>, &MessageStream<'x>)),
|
||||
Supported(fn(&mut dyn MessageField<'x>, &mut MessageStream<'x>)),
|
||||
Unsupported(&'x [u8]),
|
||||
Lf,
|
||||
Eof,
|
||||
}
|
||||
|
||||
pub fn parse_headers<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStream<'x>) -> bool {
|
||||
pub fn parse_headers<'x>(
|
||||
header: &mut dyn MessageField<'x>,
|
||||
stream: &mut MessageStream<'x>,
|
||||
) -> bool {
|
||||
loop {
|
||||
match parse_header_name(stream) {
|
||||
HeaderParserResult::Supported(fnc) => fnc(header, stream),
|
||||
@@ -63,37 +69,30 @@ pub fn parse_headers<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStre
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'x> {
|
||||
pub fn parse_header_name<'x>(stream: &mut MessageStream<'x>) -> HeaderParserResult<'x> {
|
||||
let mut token_start: usize = 0;
|
||||
let mut token_end: usize = 0;
|
||||
let mut token_len: usize = 0;
|
||||
let mut token_hash: usize = 0;
|
||||
let mut last_ch: u8 = 0;
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
for ch in stream.data[stream.pos..].iter() {
|
||||
stream.pos += 1;
|
||||
|
||||
match ch {
|
||||
b':' => {
|
||||
if token_start != 0 {
|
||||
let field = stream.get_bytes(token_start - 1, token_end).unwrap();
|
||||
let field = &stream.data[token_start - 1..token_end];
|
||||
|
||||
if (2..=25).contains(&token_len) {
|
||||
// SAFE: HDR_HASH's size is u8::MAX
|
||||
token_hash += token_len
|
||||
+ unsafe {
|
||||
*HDR_HASH.get_unchecked(last_ch.to_ascii_lowercase() as usize)
|
||||
} as usize;
|
||||
token_hash +=
|
||||
token_len + HDR_HASH[last_ch.to_ascii_lowercase() as usize] as usize;
|
||||
|
||||
if (4..=61).contains(&token_hash) {
|
||||
let token_hash = token_hash - 4;
|
||||
|
||||
// SAFE: HDR_NAME's size is 58
|
||||
if field.eq_ignore_ascii_case(unsafe {
|
||||
HDR_NAMES.get_unchecked(token_hash)
|
||||
}) {
|
||||
// SAFE: HDR_FNCS's size is 58
|
||||
return HeaderParserResult::Supported(unsafe {
|
||||
*HDR_FNCS.get_unchecked(token_hash)
|
||||
});
|
||||
if field.eq_ignore_ascii_case(HDR_NAMES[token_hash]) {
|
||||
return HeaderParserResult::Supported(HDR_FNCS[token_hash]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -101,30 +100,28 @@ pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
stream.rewind(1);
|
||||
stream.pos -= 1;
|
||||
return HeaderParserResult::Lf;
|
||||
}
|
||||
_ => {
|
||||
if !(*ch).is_ascii_whitespace() {
|
||||
if token_start == 0 {
|
||||
token_start = stream.get_pos();
|
||||
token_start = stream.pos;
|
||||
token_end = token_start;
|
||||
} else {
|
||||
token_end = stream.get_pos();
|
||||
token_end = stream.pos;
|
||||
last_ch = *ch;
|
||||
}
|
||||
|
||||
if let 0 | 9 = token_len {
|
||||
// SAFE: HDR_HASH's size is u8::MAX
|
||||
token_hash +=
|
||||
unsafe { *HDR_HASH.get_unchecked((*ch).to_ascii_lowercase() as usize) }
|
||||
as usize;
|
||||
token_hash += HDR_HASH[(*ch).to_ascii_lowercase() as usize] as usize;
|
||||
}
|
||||
token_len += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HeaderParserResult::Eof
|
||||
}
|
||||
|
||||
@@ -133,7 +130,7 @@ mod tests {
|
||||
use crate::parsers::{
|
||||
fields::{parse_from, parse_mime_version, parse_received, parse_subject},
|
||||
header::parse_header_name,
|
||||
message_stream::MessageStream,
|
||||
message::MessageStream,
|
||||
};
|
||||
|
||||
use super::HeaderParserResult;
|
||||
@@ -163,8 +160,8 @@ mod tests {
|
||||
];
|
||||
|
||||
for input in inputs {
|
||||
let mut str = input.0.to_string();
|
||||
match parse_header_name(&MessageStream::new(unsafe { str.as_bytes_mut() })) {
|
||||
let str = input.0.to_string();
|
||||
match parse_header_name(&mut MessageStream::new(str.as_bytes())) {
|
||||
HeaderParserResult::Supported(f) => {
|
||||
if let HeaderParserResult::Supported(val) = input.1 {
|
||||
if f as usize == val as usize {
|
||||
@@ -200,7 +197,7 @@ static HDR_HASH: &[u8] = &[
|
||||
62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62, 62,
|
||||
];
|
||||
|
||||
static HDR_FNCS: &[for<'x, 'y> fn(&mut dyn MessageField<'x>, &MessageStream<'x>); 58] = &[
|
||||
static HDR_FNCS: &[for<'x, 'y> fn(&mut dyn MessageField<'x>, &mut MessageStream<'x>); 58] = &[
|
||||
super::fields::parse_date,
|
||||
super::fields::parse_no_op,
|
||||
super::fields::parse_sender,
|
||||
|
||||
+115
-84
@@ -13,15 +13,20 @@ use std::borrow::Cow;
|
||||
|
||||
use crate::{
|
||||
decoders::{
|
||||
base64::Base64Decoder,
|
||||
charsets::map::{decoder_default, get_charset_decoder},
|
||||
base64::decode_base64,
|
||||
charsets::map::get_charset_decoder,
|
||||
html::{html_to_text, text_to_html},
|
||||
quoted_printable::QuotedPrintableDecoder,
|
||||
quoted_printable::decode_quoted_printable,
|
||||
DecodeFnc, DecodeResult,
|
||||
},
|
||||
BinaryPart, ContentType, InlinePart, Message, MessageHeader, MessagePart, MimeHeader, TextPart,
|
||||
};
|
||||
|
||||
use super::{fields::MessageField, header::parse_headers, message_stream::MessageStream};
|
||||
use super::{
|
||||
fields::MessageField,
|
||||
header::parse_headers,
|
||||
mime::{get_bytes_to_boundary, seek_next_part, skip_crlf, skip_multipart_end},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum MimeType {
|
||||
@@ -37,25 +42,35 @@ enum MimeType {
|
||||
Other,
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn bytes_to_string<'x>(
|
||||
bytes: &'x [u8],
|
||||
fn result_to_string<'x>(
|
||||
result: DecodeResult,
|
||||
data: &'x [u8],
|
||||
content_type: Option<&ContentType>,
|
||||
is_utf8_safe: bool,
|
||||
) -> Cow<'x, str> {
|
||||
if let Some(content_type) = content_type {
|
||||
if let Some(charset) = content_type.get_attribute("charset") {
|
||||
if let Some(decoder_fnc) = get_charset_decoder(charset.as_bytes()) {
|
||||
return decoder_fnc(bytes);
|
||||
}
|
||||
match (
|
||||
result,
|
||||
content_type.and_then(|ct| {
|
||||
ct.get_attribute("charset")
|
||||
.and_then(|c| get_charset_decoder(c.as_bytes()))
|
||||
}),
|
||||
) {
|
||||
(DecodeResult::Owned(vec), Some(charset_decoder)) => charset_decoder(&vec).into(),
|
||||
(DecodeResult::Owned(vec), None) => String::from_utf8(vec)
|
||||
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
||||
.into(),
|
||||
(DecodeResult::Borrowed((from, to)), Some(charset_decoder)) => {
|
||||
charset_decoder(&data[from..to]).into()
|
||||
}
|
||||
(DecodeResult::Borrowed((from, to)), None) => String::from_utf8_lossy(&data[from..to]),
|
||||
(DecodeResult::Empty, _) => "\n".to_string().into(),
|
||||
}
|
||||
}
|
||||
|
||||
if !is_utf8_safe {
|
||||
decoder_default(bytes)
|
||||
} else {
|
||||
// SAFE: `bytes` slice is ASCII and UTF-8 safe
|
||||
(unsafe { std::str::from_utf8_unchecked(bytes) }).into()
|
||||
fn result_to_bytes(result: DecodeResult, data: &[u8]) -> Cow<[u8]> {
|
||||
match result {
|
||||
DecodeResult::Owned(vec) => Cow::Owned(vec),
|
||||
DecodeResult::Borrowed((from, to)) => Cow::Borrowed(&data[from..to]),
|
||||
DecodeResult::Empty => Cow::from(vec![b'?']),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +137,17 @@ impl MessageParserState {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MessageStream<'x> {
|
||||
pub data: &'x [u8],
|
||||
pub pos: usize,
|
||||
}
|
||||
|
||||
impl<'x> MessageStream<'x> {
|
||||
pub fn new(data: &'x [u8]) -> MessageStream<'x> {
|
||||
MessageStream { data, pos: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> Message<'x> {
|
||||
fn new() -> Message<'x> {
|
||||
Message {
|
||||
@@ -130,17 +156,20 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `false` if at least one header field was successfully parsed.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.header.is_empty()
|
||||
}
|
||||
|
||||
/// Parses a byte slice containing the RFC5322 raw message and returns a
|
||||
/// `Message` struct.
|
||||
///
|
||||
/// This function never panics, a best-effort is made to parse the message and
|
||||
/// if no headers are found and empty `Message` struct is returned.
|
||||
/// To make sure parsing was successfull use `is_empty()`.
|
||||
///
|
||||
/// Note: The `parse()` function requires a mutable `[u8]` as input in order
|
||||
/// to perform BASE64/Quoted-Printable decoding in-place.
|
||||
///
|
||||
pub fn parse(bytes: &'x mut [u8]) -> Message<'x> {
|
||||
let stream = MessageStream::new(bytes);
|
||||
pub fn parse(bytes: &'x [u8]) -> Message<'x> {
|
||||
let mut stream = MessageStream::new(bytes);
|
||||
|
||||
let mut message = Message::new();
|
||||
let mut message_stack = Vec::new();
|
||||
@@ -159,7 +188,7 @@ impl<'x> Message<'x> {
|
||||
};
|
||||
|
||||
// Parse headers
|
||||
if !parse_headers(header, &stream) {
|
||||
if !parse_headers(header, &mut stream) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -175,7 +204,7 @@ impl<'x> Message<'x> {
|
||||
{
|
||||
let mime_boundary = ("\n--".to_string() + mime_boundary).into_bytes();
|
||||
|
||||
if stream.seek_next_part(mime_boundary.as_ref()) {
|
||||
if seek_next_part(&mut stream, mime_boundary.as_ref()) {
|
||||
let new_state = MessageParserState {
|
||||
in_alternative: state.in_alternative
|
||||
|| mime_type == MimeType::MultipartAlernative,
|
||||
@@ -190,7 +219,7 @@ impl<'x> Message<'x> {
|
||||
mime_part_header.clear();
|
||||
state_stack.push(state);
|
||||
state = new_state;
|
||||
stream.skip_crlf();
|
||||
skip_crlf(&mut stream);
|
||||
continue;
|
||||
} else {
|
||||
mime_type = MimeType::TextOther;
|
||||
@@ -213,72 +242,76 @@ impl<'x> Message<'x> {
|
||||
state_stack.push(state);
|
||||
message = Message::new();
|
||||
state = new_state;
|
||||
stream.skip_crlf();
|
||||
skip_crlf(&mut stream);
|
||||
continue;
|
||||
}
|
||||
|
||||
stream.skip_crlf();
|
||||
skip_crlf(&mut stream);
|
||||
|
||||
let (success, mut is_utf8_safe, mut bytes) = match header
|
||||
let (is_binary, decode_fnc): (bool, DecodeFnc) = match header
|
||||
.get_content_transfer_encoding()
|
||||
{
|
||||
Some(encoding) if encoding.eq_ignore_ascii_case("base64") => stream.decode_base64(
|
||||
state
|
||||
.mime_boundary
|
||||
.as_ref()
|
||||
.map_or_else(|| &[][..], |b| &b[..]),
|
||||
false,
|
||||
),
|
||||
Some(encoding) if encoding.eq_ignore_ascii_case("quoted-printable") => stream
|
||||
.decode_quoted_printable(
|
||||
Some(encoding) if encoding.eq_ignore_ascii_case("base64") => (false, decode_base64),
|
||||
Some(encoding) if encoding.eq_ignore_ascii_case("quoted-printable") => {
|
||||
(false, decode_quoted_printable)
|
||||
}
|
||||
_ => (true, get_bytes_to_boundary),
|
||||
};
|
||||
|
||||
let (bytes_read, mut bytes) = decode_fnc(
|
||||
&stream,
|
||||
stream.pos,
|
||||
state
|
||||
.mime_boundary
|
||||
.as_ref()
|
||||
.map_or_else(|| &[][..], |b| &b[..]),
|
||||
false,
|
||||
);
|
||||
|
||||
// Attempt to recover contents of an invalid message
|
||||
if bytes_read == 0 {
|
||||
if stream.pos >= stream.data.len() || (is_binary && state.mime_boundary.is_none()) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Get raw MIME part
|
||||
let (bytes_read, r_bytes) = if !is_binary {
|
||||
get_bytes_to_boundary(
|
||||
&stream,
|
||||
stream.pos,
|
||||
state
|
||||
.mime_boundary
|
||||
.as_ref()
|
||||
.map_or_else(|| &[][..], |b| &b[..]),
|
||||
false,
|
||||
),
|
||||
_ => stream.get_bytes_to_boundary(
|
||||
state
|
||||
.mime_boundary
|
||||
.as_ref()
|
||||
.map_or_else(|| &[][..], |b| &b[..]),
|
||||
),
|
||||
};
|
||||
)
|
||||
} else {
|
||||
(0, DecodeResult::Empty)
|
||||
};
|
||||
|
||||
// Attempt to recover contents of an invalid message
|
||||
if !success {
|
||||
if !stream.is_eof() {
|
||||
// Get raw MIME part
|
||||
let (success, r_is_utf8_safe, r_bytes) = stream.get_bytes_to_boundary(
|
||||
state
|
||||
.mime_boundary
|
||||
.as_ref()
|
||||
.map_or_else(|| &[][..], |b| &b[..]),
|
||||
);
|
||||
if !success {
|
||||
// If there is MIME boundary, ignore it and get raw message
|
||||
if !stream.is_eof() && state.mime_boundary.is_some() {
|
||||
let (_, r_is_utf8_safe, r_bytes) =
|
||||
stream.get_bytes_to_boundary(&[][..]);
|
||||
if r_bytes.is_some() {
|
||||
is_utf8_safe = r_is_utf8_safe;
|
||||
bytes = r_bytes;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
if bytes_read == 0 {
|
||||
// If there is MIME boundary, ignore it and get raw message
|
||||
if state.mime_boundary.is_some() {
|
||||
let (bytes_read, r_bytes) =
|
||||
get_bytes_to_boundary(&stream, stream.pos, &[][..], false);
|
||||
if bytes_read > 0 {
|
||||
bytes = r_bytes;
|
||||
stream.pos += bytes_read;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
is_utf8_safe = r_is_utf8_safe;
|
||||
bytes = r_bytes;
|
||||
break;
|
||||
}
|
||||
mime_type = MimeType::TextOther;
|
||||
is_inline = false;
|
||||
is_text = true;
|
||||
} else if bytes.is_none() {
|
||||
break;
|
||||
} else {
|
||||
bytes = r_bytes;
|
||||
stream.pos += bytes_read;
|
||||
}
|
||||
mime_type = MimeType::TextOther;
|
||||
is_inline = false;
|
||||
is_text = true;
|
||||
} else {
|
||||
stream.pos += bytes_read;
|
||||
}
|
||||
|
||||
let is_inline = is_inline
|
||||
@@ -318,11 +351,7 @@ impl<'x> Message<'x> {
|
||||
|
||||
if is_text {
|
||||
let text_part = TextPart {
|
||||
contents: bytes_to_string(
|
||||
bytes.map_or_else(|| "\n".as_bytes(), |v| v),
|
||||
header.get_content_type(),
|
||||
is_utf8_safe,
|
||||
),
|
||||
contents: result_to_string(bytes, stream.data, header.get_content_type()),
|
||||
header: if !mime_part_header.is_empty() {
|
||||
Some(std::mem::take(&mut mime_part_header))
|
||||
} else {
|
||||
@@ -358,7 +387,7 @@ impl<'x> Message<'x> {
|
||||
} else {
|
||||
None
|
||||
},
|
||||
contents: bytes.map_or_else(|| "?".as_bytes(), |v| v).into(),
|
||||
contents: result_to_bytes(bytes, stream.data),
|
||||
};
|
||||
|
||||
if add_to_html {
|
||||
@@ -398,7 +427,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
if stream.skip_multipart_end() {
|
||||
if skip_multipart_end(&mut stream) {
|
||||
// End of MIME part reached
|
||||
|
||||
if MimeType::MultipartAlernative == state.mime_type
|
||||
@@ -446,19 +475,19 @@ impl<'x> Message<'x> {
|
||||
|
||||
if let Some(ref mime_boundary) = state.mime_boundary {
|
||||
// Ancestor has a MIME boundary, seek it.
|
||||
if stream.seek_next_part(mime_boundary) {
|
||||
if seek_next_part(&mut stream, mime_boundary) {
|
||||
continue 'inner;
|
||||
}
|
||||
}
|
||||
}
|
||||
break 'outer;
|
||||
} else {
|
||||
stream.skip_crlf();
|
||||
skip_crlf(&mut stream);
|
||||
// Headers of next part expected next, break inner look.
|
||||
break 'inner;
|
||||
}
|
||||
}
|
||||
} else if stream.is_eof() {
|
||||
} else if stream.pos >= stream.data.len() {
|
||||
break 'outer;
|
||||
}
|
||||
}
|
||||
@@ -512,6 +541,8 @@ mod tests {
|
||||
let input = input.split_at_mut(pos);
|
||||
let message = Message::parse(input.0);
|
||||
|
||||
assert!(!message.is_empty());
|
||||
|
||||
assert_eq!(
|
||||
message,
|
||||
serde_json::from_slice::<Message>(&input.1[SEPARATOR.len()..]).unwrap(),
|
||||
|
||||
@@ -1,281 +0,0 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use std::{borrow::Cow, cell::UnsafeCell};
|
||||
|
||||
pub struct MessageStream<'x> {
|
||||
// SAFE: MessageStream is always executed on a single thread and this struct
|
||||
// is never shared outside the library.
|
||||
pub data: UnsafeCell<&'x mut [u8]>,
|
||||
pub pos: UnsafeCell<usize>,
|
||||
}
|
||||
|
||||
impl<'x> MessageStream<'x> {
|
||||
pub fn new(data: &'x mut [u8]) -> MessageStream<'x> {
|
||||
MessageStream {
|
||||
data: data.into(),
|
||||
pos: 0.into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn get_bytes(&self, from: usize, to: usize) -> Option<&'x [u8]> {
|
||||
// SAFE: exclusive access to `data`
|
||||
unsafe { (*self.data.get()).get(from..to) }
|
||||
}
|
||||
|
||||
pub fn get_string(&self, from: usize, to: usize, utf8_valid: bool) -> Option<Cow<'x, str>> {
|
||||
// SAFE: exclusive access to `data`
|
||||
unsafe {
|
||||
if utf8_valid {
|
||||
// SAFE: slice region is US-ASCII and UTF-8 safe
|
||||
Cow::from(std::str::from_utf8_unchecked(
|
||||
(*self.data.get()).get(from..to)?,
|
||||
))
|
||||
.into()
|
||||
} else {
|
||||
String::from_utf8_lossy((*self.data.get()).get(from..to)?).into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_eof(&self) -> bool {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
unsafe { *self.pos.get() >= (*self.data.get()).len() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn set_pos(&self, pos: usize) {
|
||||
// SAFE: exclusive access to `pos`
|
||||
unsafe {
|
||||
*self.pos.get() = pos;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn get_pos(&self) -> usize {
|
||||
// SAFE: exclusive access to `pos`
|
||||
unsafe { *self.pos.get() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn next(&self) -> Option<&'x u8> {
|
||||
unsafe {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
let pos = &mut *self.pos.get();
|
||||
let data = &mut *self.data.get();
|
||||
|
||||
if *pos < data.len() {
|
||||
// SAFE: bounds are checked
|
||||
let result = data.get_unchecked(*pos);
|
||||
*pos += 1;
|
||||
Some(result)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn peek(&self) -> Option<&'x u8> {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
unsafe { (*self.data.get()).get(*self.pos.get()) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn advance(&self, pos: usize) {
|
||||
// SAFE: exclusive access to `pos`
|
||||
unsafe {
|
||||
*self.pos.get() += pos;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seek_next_part(&self, boundary: &[u8]) -> bool {
|
||||
if !boundary.is_empty() {
|
||||
unsafe {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
let cur_pos = &mut *self.pos.get();
|
||||
let data = &mut *self.data.get();
|
||||
|
||||
let mut pos = *cur_pos;
|
||||
let mut match_count = 0;
|
||||
|
||||
for ch in (*data)[*cur_pos..].iter() {
|
||||
pos += 1;
|
||||
|
||||
// SAFE: bounds are checked after the first match occurs
|
||||
if ch == boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
*cur_pos = pos;
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
} else if match_count > 0 {
|
||||
// SAFE: `boundary` can't be empty
|
||||
if ch == boundary.get_unchecked(0) {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn get_bytes_to_boundary(&self, boundary: &[u8]) -> (bool, bool, Option<&'x [u8]>) {
|
||||
unsafe {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
let mut data = &mut *self.data.get();
|
||||
let stream_pos = &mut *self.pos.get();
|
||||
|
||||
let start_pos = *stream_pos;
|
||||
|
||||
return if !boundary.is_empty() {
|
||||
let mut pos = *stream_pos;
|
||||
let mut match_count = 0;
|
||||
let mut is_utf8_safe = true;
|
||||
|
||||
for ch in (*data)[pos..].iter() {
|
||||
pos += 1;
|
||||
|
||||
if is_utf8_safe && *ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
}
|
||||
|
||||
// SAFE: bounds are checked after the first match occurs
|
||||
if ch == boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
// SAFE: exclusive access to `self.data` is re-obtained after
|
||||
// is_boundary_end finishes.
|
||||
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;
|
||||
|
||||
return (
|
||||
true,
|
||||
is_utf8_safe,
|
||||
if start_pos < match_pos {
|
||||
(*data).get(start_pos..match_pos)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
);
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
} else if match_count > 0 {
|
||||
// SAFE: `boundary` is never empty
|
||||
if ch == boundary.get_unchecked(0) {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(false, false, None)
|
||||
} else if *stream_pos < (*data).len() {
|
||||
*stream_pos = (*data).len();
|
||||
(true, false, (*data).get(start_pos..))
|
||||
} else {
|
||||
(false, false, None)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn skip_crlf(&self) {
|
||||
unsafe {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
let cur_pos = &mut *self.pos.get();
|
||||
let data = &mut *self.data.get();
|
||||
|
||||
let mut pos = *cur_pos;
|
||||
|
||||
for ch in (*data)[*cur_pos..].iter() {
|
||||
match ch {
|
||||
b'\r' | b' ' | b'\t' => pos += 1,
|
||||
b'\n' => {
|
||||
*cur_pos = pos + 1;
|
||||
break;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn skip_byte(&self, ch: &u8) -> bool {
|
||||
unsafe {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
let pos = &mut *self.pos.get();
|
||||
let data = &mut *self.data.get();
|
||||
|
||||
if (*data).get(*pos) == Some(ch) {
|
||||
*pos += 1;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_boundary_end(&self, pos: usize) -> bool {
|
||||
// SAFE: exclusive access to `data` and `pos`
|
||||
matches!(
|
||||
unsafe { (*self.data.get()).get(pos..) },
|
||||
Some([b'\n' | b'\r' | b' ' | b'\t', ..]) | Some([b'-', b'-', ..]) | Some([]) | None
|
||||
)
|
||||
}
|
||||
|
||||
pub fn skip_multipart_end(&self) -> bool {
|
||||
unsafe {
|
||||
// SAFE: exclusive access to `pos`
|
||||
let pos = &mut *self.pos.get();
|
||||
|
||||
match (*self.data.get()).get(*pos..*pos + 2) {
|
||||
Some(b"--") => {
|
||||
if let Some(byte) = (*self.data.get()).get(*pos + 2) {
|
||||
if !(*byte).is_ascii_whitespace() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
*pos += 2;
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rewind(&self, r: usize) {
|
||||
// SAFE: exclusive access to `pos`
|
||||
unsafe {
|
||||
*self.pos.get() -= r;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
* https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
* <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
||||
* option. This file may not be copied, modified, or distributed
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use crate::decoders::DecodeResult;
|
||||
|
||||
use super::message::MessageStream;
|
||||
|
||||
pub fn seek_next_part(stream: &mut MessageStream, boundary: &[u8]) -> bool {
|
||||
if !boundary.is_empty() {
|
||||
let mut pos = stream.pos;
|
||||
|
||||
let mut match_count = 0;
|
||||
|
||||
for ch in stream.data[pos..].iter() {
|
||||
pos += 1;
|
||||
|
||||
if ch == &boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
stream.pos = pos;
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
} else if match_count > 0 {
|
||||
if ch == &boundary[0] {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn get_bytes_to_boundary<'x>(
|
||||
stream: &MessageStream<'x>,
|
||||
start_pos: usize,
|
||||
boundary: &[u8],
|
||||
_is_word: bool,
|
||||
) -> (usize, DecodeResult) {
|
||||
let mut read_pos = start_pos;
|
||||
|
||||
return if !boundary.is_empty() {
|
||||
let mut match_count = 0;
|
||||
|
||||
for ch in stream.data[read_pos..].iter() {
|
||||
read_pos += 1;
|
||||
|
||||
if ch == &boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
if is_boundary_end(stream, read_pos) {
|
||||
let match_pos = read_pos - match_count;
|
||||
|
||||
return (
|
||||
read_pos - start_pos,
|
||||
if start_pos < match_pos {
|
||||
DecodeResult::Borrowed((start_pos, match_pos))
|
||||
} else {
|
||||
DecodeResult::Empty
|
||||
},
|
||||
);
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
} else if match_count > 0 {
|
||||
if ch == &boundary[0] {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(0, DecodeResult::Empty)
|
||||
} else if start_pos < stream.data.len() {
|
||||
(
|
||||
stream.data.len() - start_pos,
|
||||
DecodeResult::Borrowed((start_pos, stream.data.len())),
|
||||
)
|
||||
} else {
|
||||
(0, DecodeResult::Empty)
|
||||
};
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_boundary_end(stream: &MessageStream, pos: usize) -> bool {
|
||||
matches!(
|
||||
stream.data.get(pos..),
|
||||
Some([b'\n' | b'\r' | b' ' | b'\t', ..]) | Some([b'-', b'-', ..]) | Some([]) | None
|
||||
)
|
||||
}
|
||||
|
||||
pub fn skip_multipart_end(stream: &mut MessageStream) -> bool {
|
||||
match stream.data.get(stream.pos..stream.pos + 2) {
|
||||
Some(b"--") => {
|
||||
if let Some(byte) = stream.data.get(stream.pos + 2) {
|
||||
if !(*byte).is_ascii_whitespace() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
stream.pos += 2;
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn skip_crlf(stream: &mut MessageStream) {
|
||||
for ch in stream.data[stream.pos..].iter() {
|
||||
match ch {
|
||||
b'\r' | b' ' | b'\t' => stream.pos += 1,
|
||||
b'\n' => {
|
||||
stream.pos += 1;
|
||||
break;
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -12,4 +12,4 @@
|
||||
pub mod fields;
|
||||
pub mod header;
|
||||
pub mod message;
|
||||
pub mod message_stream;
|
||||
pub mod mime;
|
||||
|
||||
@@ -13,7 +13,7 @@ use mail_parser::*;
|
||||
|
||||
#[test]
|
||||
fn test_api() {
|
||||
let mut input = concat!(
|
||||
let input = concat!(
|
||||
"From: Art Vandelay <art@vandelay.com> (Vandelay Industries)\n",
|
||||
"To: \"Colleagues\": \"James Smythe\" <james@vandelay.com>; Friends:\n",
|
||||
" jane@example.com, =?UTF-8?Q?John_Sm=C3=AEth?= <john@example.com>;\n",
|
||||
@@ -48,10 +48,9 @@ fn test_api() {
|
||||
"--giddyup--\n",
|
||||
"--festivus--\n",
|
||||
)
|
||||
.as_bytes()
|
||||
.to_vec();
|
||||
.as_bytes();
|
||||
|
||||
let message = Message::parse(&mut input[..]);
|
||||
let message = Message::parse(input);
|
||||
|
||||
assert_eq!(
|
||||
message.get_from(),
|
||||
|
||||
+2
-2
@@ -142,7 +142,7 @@ Content-Transfer-Encoding: Quoted-printable
|
||||
"c_subtype": "basic"
|
||||
}
|
||||
},
|
||||
"contents": ".. base64-encoded 8000 Hz single-channel\n mu-law-format audio data goes here ...\n"
|
||||
"contents": "... base64-encoded 8000 Hz single-channel\n mu-law-format audio data goes here ...\n"
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -154,7 +154,7 @@ Content-Transfer-Encoding: Quoted-printable
|
||||
"c_subtype": "jpeg"
|
||||
}
|
||||
},
|
||||
"contents": ".. base64-encoded image data goes here ...\n"
|
||||
"contents": "... base64-encoded image data goes here ...\n"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
Vendored
+1
-1
@@ -78,7 +78,7 @@ Content-Type: text/plain; charset=utf-8
|
||||
}
|
||||
}
|
||||
},
|
||||
"contents": "garbage"
|
||||
"contents": "?garbage"
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user