mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-12-03 08:01:48 +00:00
Replaced MessageStream::next with slice iterators.
This commit is contained in:
+83
-84
@@ -13,107 +13,101 @@ use std::borrow::Cow;
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
|
||||
pub trait Base64Decoder<'x> {
|
||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, Option<Cow<'x, [u8]>>);
|
||||
}
|
||||
pub fn decode_base64<'x>(
|
||||
stream: &MessageStream<'x>,
|
||||
start_pos: usize,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (usize, Option<Cow<'x, [u8]>>) {
|
||||
let mut success = boundary.is_empty();
|
||||
|
||||
impl<'x> Base64Decoder<'x> for MessageStream<'x> {
|
||||
fn decode_base64(&self, boundary: &[u8], is_word: bool) -> (bool, Option<Cow<'x, [u8]>>) {
|
||||
let mut success = boundary.is_empty();
|
||||
let mut chunk: u32 = 0;
|
||||
let mut byte_count: u8 = 0;
|
||||
let mut match_count: usize = 0;
|
||||
|
||||
let mut chunk: u32 = 0;
|
||||
let mut byte_count: u8 = 0;
|
||||
let mut match_count: usize = 0;
|
||||
let mut bytes_read = 0;
|
||||
|
||||
let start_pos = self.pos.get();
|
||||
let mut read_pos = start_pos;
|
||||
let mut buf = Vec::with_capacity(stream.data.len() - start_pos);
|
||||
|
||||
let mut buf = Vec::with_capacity(self.data.len() - read_pos);
|
||||
for ch in stream.data[start_pos..].iter() {
|
||||
bytes_read += 1;
|
||||
|
||||
for ch in self.data[read_pos..].iter() {
|
||||
read_pos += 1;
|
||||
|
||||
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[byte_count as usize][*ch as usize];
|
||||
|
||||
if val >= 0x01ffffff {
|
||||
if *ch == b'=' {
|
||||
match byte_count {
|
||||
1 | 2 => {
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
0 => (),
|
||||
_ => {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if !ch.is_ascii_whitespace() || is_word {
|
||||
success = false;
|
||||
if !success {
|
||||
if *ch == boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
success = true;
|
||||
break;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
byte_count = (byte_count + 1) & 3;
|
||||
|
||||
if byte_count == 1 {
|
||||
chunk = val;
|
||||
} else {
|
||||
chunk |= val;
|
||||
|
||||
if byte_count == 0 {
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
||||
} else if match_count > 0 {
|
||||
if *ch == boundary[0] {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.pos.set(read_pos);
|
||||
let val = BASE64_MAP[byte_count as usize][*ch as usize];
|
||||
|
||||
(
|
||||
success,
|
||||
if !buf.is_empty() {
|
||||
buf.shrink_to_fit();
|
||||
Some(buf.into())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
if val >= 0x01ffffff {
|
||||
if *ch == b'=' {
|
||||
match byte_count {
|
||||
1 | 2 => {
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
0 => (),
|
||||
_ => {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if !ch.is_ascii_whitespace() || is_word {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
byte_count = (byte_count + 1) & 3;
|
||||
|
||||
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();
|
||||
Some(buf.into())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
|
||||
use super::Base64Decoder;
|
||||
use crate::{decoders::base64::decode_base64, parsers::message_stream::MessageStream};
|
||||
|
||||
#[test]
|
||||
fn decode_base64() {
|
||||
fn decode_base64_strings() {
|
||||
let inputs = [
|
||||
("VGVzdA==", "Test", "", true),
|
||||
("WWU=", "Ye", "", true),
|
||||
@@ -169,8 +163,13 @@ mod tests {
|
||||
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.as_ref().unwrap().as_ref()).unwrap();
|
||||
|
||||
@@ -9,80 +9,101 @@
|
||||
* except according to those terms.
|
||||
*/
|
||||
|
||||
use crate::{
|
||||
decoders::{
|
||||
base64::Base64Decoder, charsets::map::get_charset_decoder,
|
||||
quoted_printable::QuotedPrintableDecoder,
|
||||
},
|
||||
parsers::message_stream::MessageStream,
|
||||
};
|
||||
use crate::{decoders::charsets::map::get_charset_decoder, parsers::message_stream::MessageStream};
|
||||
|
||||
pub fn parse_encoded_word(stream: &MessageStream) -> Option<String> {
|
||||
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;
|
||||
use super::{base64::decode_base64, quoted_printable::decode_quoted_printable, DecodeFnc};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
match ch {
|
||||
b'?' => {
|
||||
if charset_end == charset_start {
|
||||
charset_end = stream.get_pos() - 1;
|
||||
enum Rfc2047State {
|
||||
Init,
|
||||
Charset,
|
||||
Encoding,
|
||||
Data,
|
||||
}
|
||||
|
||||
pub fn decode_rfc2047(stream: &MessageStream, start_pos: usize) -> (usize, Option<String>) {
|
||||
let mut read_pos: usize = start_pos;
|
||||
let mut state = Rfc2047State::Init;
|
||||
|
||||
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, bytes) = match stream.next() {
|
||||
Some(b'q') | Some(b'Q') if stream.skip_byte(&b'?') => {
|
||||
stream.decode_quoted_printable(b"?=", true)
|
||||
}
|
||||
Some(b'b') | Some(b'B') if stream.skip_byte(&b'?') => stream.decode_base64(b"?=", true),
|
||||
_ => (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())
|
||||
if let (bytes_read @ 1..=usize::MAX, Some(bytes)) =
|
||||
decode_fnc.unwrap()(stream, read_pos, b"?=", true)
|
||||
{
|
||||
decoder(bytes?.as_ref()).into()
|
||||
(
|
||||
(read_pos - start_pos) + bytes_read,
|
||||
if let Some(decoder) = get_charset_decoder(&stream.data[charset_start..charset_end]) {
|
||||
decoder(bytes.as_ref()).into()
|
||||
} else {
|
||||
String::from_utf8(bytes.into_owned())
|
||||
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
||||
.into()
|
||||
},
|
||||
)
|
||||
} else {
|
||||
String::from_utf8(bytes?.into_owned())
|
||||
.unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
|
||||
.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_stream::MessageStream};
|
||||
|
||||
#[test]
|
||||
fn decode_rfc2047() {
|
||||
fn decode_rfc2047_string() {
|
||||
let inputs = [
|
||||
(
|
||||
"?iso-8859-1?q?this=20is=20some=20text?=".to_string(),
|
||||
@@ -163,12 +184,12 @@ mod tests {
|
||||
for input in inputs {
|
||||
let str = input.0.to_string();
|
||||
|
||||
match parse_encoded_word(&MessageStream::new(str.as_bytes())) {
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::parsers::message_stream::MessageStream;
|
||||
|
||||
/*
|
||||
* Copyright Stalwart Labs, Minter Ltd. See the COPYING
|
||||
* file at the top-level directory of this distribution.
|
||||
@@ -15,3 +19,6 @@ 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, Option<Cow<'x, [u8]>>);
|
||||
|
||||
+126
-131
@@ -20,162 +20,151 @@ enum QuotedPrintableState {
|
||||
Hex1,
|
||||
}
|
||||
|
||||
pub trait QuotedPrintableDecoder<'x> {
|
||||
fn decode_quoted_printable(
|
||||
&self,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (bool, Option<Cow<'x, [u8]>>);
|
||||
}
|
||||
pub fn decode_quoted_printable<'x>(
|
||||
stream: &MessageStream<'x>,
|
||||
start_pos: usize,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (usize, Option<Cow<'x, [u8]>>) {
|
||||
let mut success = boundary.is_empty();
|
||||
|
||||
impl<'x> QuotedPrintableDecoder<'x> for MessageStream<'x> {
|
||||
fn decode_quoted_printable(
|
||||
&self,
|
||||
boundary: &[u8],
|
||||
is_word: bool,
|
||||
) -> (bool, Option<Cow<'x, [u8]>>) {
|
||||
let mut success = boundary.is_empty();
|
||||
let mut bytes_read = 0;
|
||||
|
||||
let start_pos = self.pos.get();
|
||||
let mut read_pos = start_pos;
|
||||
let mut buf = Vec::with_capacity(stream.data.len() - start_pos);
|
||||
|
||||
let mut buf = Vec::with_capacity(self.data.len() - read_pos);
|
||||
let mut state = QuotedPrintableState::None;
|
||||
let mut match_count = 0;
|
||||
let mut hex1 = 0;
|
||||
|
||||
let mut state = QuotedPrintableState::None;
|
||||
let mut match_count = 0;
|
||||
let mut hex1 = 0;
|
||||
debug_assert!(boundary.is_empty() || boundary.len() >= 2);
|
||||
|
||||
debug_assert!(boundary.is_empty() || boundary.len() >= 2);
|
||||
for ch in stream.data[start_pos..].iter() {
|
||||
bytes_read += 1;
|
||||
|
||||
for ch in self.data[read_pos..].iter() {
|
||||
read_pos += 1;
|
||||
// if success is false, a boundary was provided
|
||||
if !success {
|
||||
let mut is_boundary_end = true;
|
||||
|
||||
// if success is false, a boundary was provided
|
||||
if !success {
|
||||
let mut is_boundary_end = true;
|
||||
|
||||
if *ch == boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
let done = if is_word {
|
||||
true
|
||||
} else {
|
||||
is_boundary_end = self.is_boundary_end(read_pos);
|
||||
is_boundary_end
|
||||
};
|
||||
|
||||
if done {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
if *ch == boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
let done = if is_word {
|
||||
true
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
is_boundary_end =
|
||||
crate::parsers::mime::is_boundary_end(stream, start_pos + bytes_read);
|
||||
is_boundary_end
|
||||
};
|
||||
|
||||
if match_count > 0 {
|
||||
for ch in boundary[..match_count].iter() {
|
||||
if *ch != b'\n' || QuotedPrintableState::Eq != state {
|
||||
buf.push(*ch);
|
||||
}
|
||||
}
|
||||
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 {
|
||||
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 {
|
||||
// There was a full boundary match but without a proper MIME
|
||||
// ending, reset match count and continue.
|
||||
match_count = 0;
|
||||
continue;
|
||||
if done {
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
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 => {
|
||||
if match_count > 0 {
|
||||
for ch in boundary[..match_count].iter() {
|
||||
if *ch != b'\n' || QuotedPrintableState::Eq != state {
|
||||
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;
|
||||
|
||||
state = QuotedPrintableState::None;
|
||||
if hex2 != -1 {
|
||||
let ch = ((hex1 as u8) << 4) | hex2 as u8;
|
||||
|
||||
buf.push(ch);
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
// 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 {
|
||||
// There was a full boundary match but without a proper MIME
|
||||
// ending, reset match count and continue.
|
||||
match_count = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.pos.set(read_pos);
|
||||
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];
|
||||
|
||||
(
|
||||
success,
|
||||
if !buf.is_empty() {
|
||||
buf.shrink_to_fit();
|
||||
Some(buf.into())
|
||||
} else {
|
||||
None
|
||||
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();
|
||||
Some(buf.into())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
decoders::quoted_printable::QuotedPrintableDecoder, parsers::message_stream::MessageStream,
|
||||
decoders::quoted_printable::decode_quoted_printable, parsers::message_stream::MessageStream,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn decode_quoted_printable() {
|
||||
fn decode_quoted_printable_string() {
|
||||
let inputs = [
|
||||
(
|
||||
concat!(
|
||||
@@ -248,9 +237,15 @@ mod tests {
|
||||
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.as_ref().unwrap()).unwrap();
|
||||
@@ -274,13 +269,13 @@ mod tests {
|
||||
// MIRI overflow tests
|
||||
for input in [b"\x0a\x0a\x00\x0b".to_vec(), b":B\x0a%".to_vec()] {
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
stream.decode_quoted_printable(b"\n\n", true);
|
||||
decode_quoted_printable(&stream, 0, b"\n\n", true);
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
stream.decode_quoted_printable(b"\n\n", false);
|
||||
decode_quoted_printable(&stream, 0, b"\n\n", false);
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
stream.decode_quoted_printable(&[], true);
|
||||
decode_quoted_printable(&stream, 0, &[], true);
|
||||
let stream = MessageStream::new(&input[..]);
|
||||
stream.decode_quoted_printable(&[], false);
|
||||
decode_quoted_printable(&stream, 0, &[], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
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_stream::MessageStream, Addr, Address,
|
||||
Group,
|
||||
};
|
||||
|
||||
impl<'x> Default for Address<'x> {
|
||||
@@ -249,13 +249,20 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
result: Vec::new(),
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let start_pos = stream.get_pos();
|
||||
let mut iter = stream.data[start_pos..].iter();
|
||||
let mut read_pos = start_pos;
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
read_pos += 1;
|
||||
|
||||
match ch {
|
||||
b'\n' => {
|
||||
add_token(&mut parser, stream, false);
|
||||
match stream.peek() {
|
||||
match stream.data.get(read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
read_pos += 1;
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
@@ -267,7 +274,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 = read_pos - 1;
|
||||
}
|
||||
add_token(&mut parser, stream, false);
|
||||
}
|
||||
@@ -308,7 +315,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, read_pos) {
|
||||
let add_space = parser.state != AddressState::Quote; // Make borrow-checker happy
|
||||
add_token(&mut parser, stream, add_space);
|
||||
(if parser.state != AddressState::Comment {
|
||||
@@ -317,6 +324,8 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
&mut parser.comment_tokens
|
||||
})
|
||||
.push(token.into());
|
||||
read_pos += d_bytes_read;
|
||||
iter = stream.data[read_pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -329,10 +338,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 = read_pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = read_pos;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
@@ -378,15 +387,17 @@ pub fn parse_address<'x>(stream: &MessageStream<'x>) -> Address<'x> {
|
||||
}
|
||||
|
||||
if parser.token_start == 0 {
|
||||
parser.token_start = stream.get_pos();
|
||||
parser.token_start = read_pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = read_pos;
|
||||
}
|
||||
}
|
||||
|
||||
add_address(&mut parser);
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
if parser.group_name.is_some() || !parser.result.is_empty() {
|
||||
add_group(&mut parser);
|
||||
if parser.result.len() > 1 {
|
||||
|
||||
@@ -15,9 +15,7 @@ use std::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
decoders::{
|
||||
charsets::map::get_charset_decoder, encoded_word::parse_encoded_word, hex::decode_hex,
|
||||
},
|
||||
decoders::{charsets::map::get_charset_decoder, encoded_word::decode_rfc2047, hex::decode_hex},
|
||||
parsers::message_stream::MessageStream,
|
||||
ContentType,
|
||||
};
|
||||
@@ -49,6 +47,7 @@ struct ContentTypeParser<'x> {
|
||||
attributes: HashMap<Cow<'x, str>, Cow<'x, str>>,
|
||||
continuations: Option<Vec<Continuation<'x>>>,
|
||||
|
||||
read_pos: usize,
|
||||
token_start: usize,
|
||||
token_end: usize,
|
||||
|
||||
@@ -127,7 +126,7 @@ fn add_partial_value<'x>(
|
||||
.get_string(
|
||||
parser.token_start - 1,
|
||||
if in_quote && to_cur_pos {
|
||||
stream.get_pos() - 1
|
||||
parser.read_pos - 1
|
||||
} else {
|
||||
parser.token_end
|
||||
},
|
||||
@@ -271,11 +270,15 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
is_token_start: true,
|
||||
is_escaped: false,
|
||||
|
||||
read_pos: stream.get_pos(),
|
||||
token_start: 0,
|
||||
token_end: 0,
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut iter = stream.data[parser.read_pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
parser.read_pos += 1;
|
||||
match ch {
|
||||
b' ' | b'\t' => {
|
||||
if !parser.is_token_start {
|
||||
@@ -283,10 +286,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 = parser.read_pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = parser.read_pos;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
@@ -312,10 +315,11 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match stream.peek() {
|
||||
match stream.data.get(parser.read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
parser.state = ContentState::AttributeName;
|
||||
stream.advance(1);
|
||||
parser.read_pos += 1;
|
||||
iter.next();
|
||||
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
@@ -326,6 +330,7 @@ pub fn parse_content_type<'x>(stream: &MessageStream<'x>) -> Option<ContentType<
|
||||
if parser.continuations.is_some() {
|
||||
merge_continuations(&mut parser);
|
||||
}
|
||||
stream.set_pos(parser.read_pos);
|
||||
return if let Some(content_type) = parser.c_type {
|
||||
Some(ContentType {
|
||||
c_type: content_type,
|
||||
@@ -395,9 +400,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, parser.read_pos) {
|
||||
add_partial_value(&mut parser, stream, false);
|
||||
parser.values.push(token.into());
|
||||
parser.read_pos += d_bytes_read;
|
||||
iter = stream.data[parser.read_pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -487,13 +494,15 @@ 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 = parser.read_pos;
|
||||
parser.token_end = parser.token_start;
|
||||
} else {
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = parser.read_pos;
|
||||
}
|
||||
}
|
||||
|
||||
stream.set_pos(parser.read_pos);
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
+17
-12
@@ -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: &MessageStream) -> Option<DateTime> {
|
||||
let mut pos = 0;
|
||||
let mut parts = [0u32; 7];
|
||||
let mut parts_sizes = [
|
||||
@@ -57,13 +57,19 @@ 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 read_pos = stream.get_pos();
|
||||
let mut iter = stream.data[read_pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
let mut next_part = false;
|
||||
read_pos += 1;
|
||||
|
||||
match ch {
|
||||
b'\n' => match stream.peek() {
|
||||
b'\n' => match stream.data.get(read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
read_pos += 1;
|
||||
|
||||
if !is_new_token && !ignore && comment_count == 0 {
|
||||
next_part = true;
|
||||
} else {
|
||||
@@ -78,7 +84,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(read_pos) {
|
||||
read_pos += 1;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -131,12 +139,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 {
|
||||
@@ -148,6 +151,8 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
||||
}
|
||||
}
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
if pos >= 6 {
|
||||
Some(DateTime {
|
||||
year: if (1..=99).contains(&parts[2]) {
|
||||
@@ -259,7 +264,7 @@ mod tests {
|
||||
|
||||
for input in inputs {
|
||||
let str = input.0.to_string();
|
||||
match parse_date(&MessageStream::new(str.as_bytes()), false) {
|
||||
match parse_date(&MessageStream::new(str.as_bytes())) {
|
||||
Some(date) => {
|
||||
//println!("{} -> {}", input.0.escape_debug(), date.to_iso8601());
|
||||
assert_eq!(input.1, date.to_iso8601());
|
||||
|
||||
@@ -19,14 +19,22 @@ pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
let mut is_id_part = false;
|
||||
let mut ids = Vec::new();
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut read_pos = stream.get_pos();
|
||||
let mut iter = stream.data[read_pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
read_pos += 1;
|
||||
match ch {
|
||||
b'\n' => match stream.peek() {
|
||||
b'\n' => match stream.data.get(read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
read_pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => return if !ids.is_empty() { Some(ids) } else { None },
|
||||
_ => {
|
||||
stream.set_pos(read_pos);
|
||||
return if !ids.is_empty() { Some(ids) } else { None };
|
||||
}
|
||||
},
|
||||
b'<' => {
|
||||
is_id_part = true;
|
||||
@@ -46,11 +54,14 @@ pub fn parse_id<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'x, str>>> {
|
||||
}
|
||||
if is_id_part {
|
||||
if token_start == 0 {
|
||||
token_start = stream.get_pos();
|
||||
token_start = read_pos;
|
||||
}
|
||||
token_end = stream.get_pos();
|
||||
token_end = read_pos;
|
||||
}
|
||||
}
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
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_stream::MessageStream};
|
||||
|
||||
struct ListParser<'x> {
|
||||
token_start: usize,
|
||||
@@ -62,21 +62,27 @@ pub fn parse_comma_separared<'x>(stream: &MessageStream<'x>) -> Option<Vec<Cow<'
|
||||
list: Vec::new(),
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut read_pos = stream.get_pos();
|
||||
let mut iter = stream.data[read_pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
read_pos += 1;
|
||||
match ch {
|
||||
b'\n' => {
|
||||
add_token(&mut parser, stream, false);
|
||||
|
||||
match stream.peek() {
|
||||
match stream.data.get(read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
read_pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
add_tokens_to_list(&mut parser);
|
||||
stream.set_pos(read_pos);
|
||||
return if !parser.list.is_empty() {
|
||||
parser.list.into()
|
||||
} else {
|
||||
@@ -92,9 +98,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, read_pos) {
|
||||
add_token(&mut parser, stream, true);
|
||||
parser.tokens.push(token.into());
|
||||
read_pos += bytes_read;
|
||||
iter = stream.data[read_pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -112,12 +120,14 @@ 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 = read_pos;
|
||||
}
|
||||
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = read_pos;
|
||||
}
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ pub trait FieldSet<'x> {
|
||||
|
||||
impl<'x> FieldSet<'x> for MessageHeader<'x> {
|
||||
fn set_date(&mut self, stream: &MessageStream<'x>) {
|
||||
self.date = self::date::parse_date(stream, false);
|
||||
self.date = self::date::parse_date(stream);
|
||||
}
|
||||
|
||||
fn set_sender(&mut self, stream: &MessageStream<'x>) {
|
||||
@@ -139,7 +139,7 @@ impl<'x> FieldSet<'x> for MessageHeader<'x> {
|
||||
}
|
||||
|
||||
fn set_resent_date(&mut self, stream: &MessageStream<'x>) {
|
||||
append_date_line(&mut self.resent_date, self::date::parse_date(stream, false));
|
||||
append_date_line(&mut self.resent_date, self::date::parse_date(stream));
|
||||
}
|
||||
|
||||
fn set_to(&mut self, stream: &MessageStream<'x>) {
|
||||
|
||||
@@ -17,14 +17,20 @@ pub fn parse_raw<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
let mut token_start: usize = 0;
|
||||
let mut token_end: usize = 0;
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut read_pos = stream.get_pos();
|
||||
let mut iter = stream.data[read_pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
read_pos += 1;
|
||||
match ch {
|
||||
b'\n' => match stream.peek() {
|
||||
b'\n' => match stream.data.get(read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
read_pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
stream.set_pos(read_pos);
|
||||
return if token_start > 0 {
|
||||
stream
|
||||
.get_string(token_start - 1, token_end)
|
||||
@@ -40,27 +46,36 @@ pub fn parse_raw<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>> {
|
||||
}
|
||||
|
||||
if token_start == 0 {
|
||||
token_start = stream.get_pos();
|
||||
token_start = read_pos;
|
||||
}
|
||||
|
||||
token_end = stream.get_pos();
|
||||
token_end = read_pos;
|
||||
}
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn parse_and_ignore(stream: &MessageStream) {
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut read_pos = stream.get_pos();
|
||||
let mut iter = stream.data[read_pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
read_pos += 1;
|
||||
|
||||
if ch == &b'\n' {
|
||||
match stream.peek() {
|
||||
match stream.data.get(read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
read_pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => return,
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
stream.set_pos(read_pos);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
|
||||
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_stream::MessageStream};
|
||||
struct UnstructuredParser<'x> {
|
||||
token_start: usize,
|
||||
token_end: usize,
|
||||
@@ -47,20 +47,26 @@ pub fn parse_unstructured<'x>(stream: &MessageStream<'x>) -> Option<Cow<'x, str>
|
||||
tokens: Vec::new(),
|
||||
};
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut read_pos = stream.get_pos();
|
||||
let mut iter = stream.data[read_pos..].iter();
|
||||
|
||||
while let Some(ch) = iter.next() {
|
||||
read_pos += 1;
|
||||
match ch {
|
||||
b'\n' => {
|
||||
add_token(&mut parser, stream, false);
|
||||
|
||||
match stream.peek() {
|
||||
match stream.data.get(read_pos) {
|
||||
Some(b' ' | b'\t') => {
|
||||
if !parser.is_token_start {
|
||||
parser.is_token_start = true;
|
||||
}
|
||||
stream.advance(1);
|
||||
iter.next();
|
||||
read_pos += 1;
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
stream.set_pos(read_pos);
|
||||
return match parser.tokens.len() {
|
||||
1 => parser.tokens.pop().unwrap().into(),
|
||||
0 => None,
|
||||
@@ -76,9 +82,11 @@ 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, read_pos) {
|
||||
add_token(&mut parser, stream, true);
|
||||
parser.tokens.push(token.into());
|
||||
read_pos += bytes_read;
|
||||
iter = stream.data[read_pos..].iter();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -91,12 +99,14 @@ 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 = read_pos;
|
||||
}
|
||||
|
||||
parser.token_end = stream.get_pos();
|
||||
parser.token_end = read_pos;
|
||||
}
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
||||
+13
-4
@@ -70,12 +70,18 @@ pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'
|
||||
let mut token_hash: usize = 0;
|
||||
let mut last_ch: u8 = 0;
|
||||
|
||||
while let Some(ch) = stream.next() {
|
||||
let mut read_pos = stream.get_pos();
|
||||
|
||||
for ch in stream.data[read_pos..].iter() {
|
||||
read_pos += 1;
|
||||
|
||||
match ch {
|
||||
b':' => {
|
||||
if token_start != 0 {
|
||||
let field = stream.get_bytes(token_start - 1, token_end).unwrap();
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
if (2..=25).contains(&token_len) {
|
||||
token_hash +=
|
||||
token_len + HDR_HASH[last_ch.to_ascii_lowercase() as usize] as usize;
|
||||
@@ -92,16 +98,16 @@ pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'
|
||||
}
|
||||
}
|
||||
b'\n' => {
|
||||
stream.rewind(1);
|
||||
stream.set_pos(read_pos - 1);
|
||||
return HeaderParserResult::Lf;
|
||||
}
|
||||
_ => {
|
||||
if !(*ch).is_ascii_whitespace() {
|
||||
if token_start == 0 {
|
||||
token_start = stream.get_pos();
|
||||
token_start = read_pos;
|
||||
token_end = token_start;
|
||||
} else {
|
||||
token_end = stream.get_pos();
|
||||
token_end = read_pos;
|
||||
last_ch = *ch;
|
||||
}
|
||||
|
||||
@@ -113,6 +119,9 @@ pub fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stream.set_pos(read_pos);
|
||||
|
||||
HeaderParserResult::Eof
|
||||
}
|
||||
|
||||
|
||||
+66
-49
@@ -13,15 +13,21 @@ use std::borrow::Cow;
|
||||
|
||||
use crate::{
|
||||
decoders::{
|
||||
base64::Base64Decoder,
|
||||
base64::decode_base64,
|
||||
charsets::map::get_charset_decoder,
|
||||
html::{html_to_text, text_to_html},
|
||||
quoted_printable::QuotedPrintableDecoder,
|
||||
quoted_printable::decode_quoted_printable,
|
||||
DecodeFnc,
|
||||
},
|
||||
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,
|
||||
message_stream::MessageStream,
|
||||
mime::{get_bytes_to_boundary, seek_next_part, skip_multipart_end},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
enum MimeType {
|
||||
@@ -171,7 +177,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(&stream, mime_boundary.as_ref()) {
|
||||
let new_state = MessageParserState {
|
||||
in_alternative: state.in_alternative
|
||||
|| mime_type == MimeType::MultipartAlernative,
|
||||
@@ -215,61 +221,72 @@ impl<'x> Message<'x> {
|
||||
|
||||
stream.skip_crlf();
|
||||
|
||||
let (success, mut bytes) = 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(
|
||||
let start_pos = stream.get_pos();
|
||||
|
||||
let (is_binary, decode_fnc): (bool, DecodeFnc) = match header
|
||||
.get_content_transfer_encoding()
|
||||
{
|
||||
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,
|
||||
start_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.is_eof() || (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,
|
||||
start_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, None)
|
||||
};
|
||||
|
||||
// Attempt to recover contents of an invalid message
|
||||
if !success {
|
||||
if !stream.is_eof() {
|
||||
// Get raw MIME part
|
||||
let (success, 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_bytes) = stream.get_bytes_to_boundary(&[][..]);
|
||||
if r_bytes.is_some() {
|
||||
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, start_pos, &[][..], false);
|
||||
if bytes_read > 0 {
|
||||
bytes = r_bytes;
|
||||
stream.set_pos(start_pos + bytes_read);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
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.set_pos(start_pos + bytes_read);
|
||||
}
|
||||
mime_type = MimeType::TextOther;
|
||||
is_inline = false;
|
||||
is_text = true;
|
||||
} else {
|
||||
stream.set_pos(start_pos + bytes_read);
|
||||
}
|
||||
|
||||
let is_inline = is_inline
|
||||
@@ -388,7 +405,7 @@ impl<'x> Message<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
if stream.skip_multipart_end() {
|
||||
if skip_multipart_end(&stream) {
|
||||
// End of MIME part reached
|
||||
|
||||
if MimeType::MultipartAlernative == state.mime_type
|
||||
@@ -436,7 +453,7 @@ 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(&stream, mime_boundary) {
|
||||
continue 'inner;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,105 +48,6 @@ impl<'x> MessageStream<'x> {
|
||||
self.pos.get()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn next(&self) -> Option<&'x u8> {
|
||||
let pos = self.pos.get();
|
||||
let result = self.data.get(pos);
|
||||
self.pos.set(pos + 1);
|
||||
result
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn peek(&self) -> Option<&'x u8> {
|
||||
self.data.get(self.pos.get())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn advance(&self, pos: usize) {
|
||||
self.pos.set(self.pos.get() + pos)
|
||||
}
|
||||
|
||||
pub fn seek_next_part(&self, boundary: &[u8]) -> bool {
|
||||
if !boundary.is_empty() {
|
||||
let mut pos = self.pos.get();
|
||||
|
||||
let mut match_count = 0;
|
||||
|
||||
for ch in self.data[pos..].iter() {
|
||||
pos += 1;
|
||||
|
||||
if ch == &boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
self.pos.set(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(&self, boundary: &[u8]) -> (bool, Option<Cow<'x, [u8]>>) {
|
||||
let mut pos = self.pos.get();
|
||||
|
||||
let start_pos = pos;
|
||||
|
||||
return if !boundary.is_empty() {
|
||||
let mut match_count = 0;
|
||||
|
||||
for ch in self.data[pos..].iter() {
|
||||
pos += 1;
|
||||
|
||||
if ch == &boundary[match_count] {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
if self.is_boundary_end(pos) {
|
||||
let match_pos = pos - match_count;
|
||||
self.pos.set(pos);
|
||||
|
||||
return (
|
||||
true,
|
||||
if start_pos < match_pos {
|
||||
Cow::from(&self.data[start_pos..match_pos]).into()
|
||||
} else {
|
||||
None
|
||||
},
|
||||
);
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
} else if match_count > 0 {
|
||||
if ch == &boundary[0] {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(false, None)
|
||||
} else if pos < self.data.len() {
|
||||
self.pos.set(self.data.len());
|
||||
(true, Cow::from(&self.data[start_pos..]).into())
|
||||
} else {
|
||||
(false, None)
|
||||
};
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn skip_crlf(&self) {
|
||||
let mut pos = self.pos.get();
|
||||
@@ -162,45 +63,4 @@ impl<'x> MessageStream<'x> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn skip_byte(&self, ch: &u8) -> bool {
|
||||
let pos = self.pos.get();
|
||||
|
||||
if self.data.get(pos) == Some(ch) {
|
||||
self.pos.set(pos + 1);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn is_boundary_end(&self, pos: usize) -> bool {
|
||||
matches!(
|
||||
self.data.get(pos..),
|
||||
Some([b'\n' | b'\r' | b' ' | b'\t', ..]) | Some([b'-', b'-', ..]) | Some([]) | None
|
||||
)
|
||||
}
|
||||
|
||||
pub fn skip_multipart_end(&self) -> bool {
|
||||
let pos = self.pos.get();
|
||||
|
||||
match self.data.get(pos..pos + 2) {
|
||||
Some(b"--") => {
|
||||
if let Some(byte) = self.data.get(pos + 2) {
|
||||
if !(*byte).is_ascii_whitespace() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
self.pos.set(pos + 2);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rewind(&self, r: usize) {
|
||||
self.pos.set(self.pos.get() - r);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use super::message_stream::MessageStream;
|
||||
|
||||
pub fn seek_next_part(stream: &MessageStream, boundary: &[u8]) -> bool {
|
||||
if !boundary.is_empty() {
|
||||
let mut pos = stream.pos.get();
|
||||
|
||||
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.set_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, Option<Cow<'x, [u8]>>) {
|
||||
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 {
|
||||
Cow::from(&stream.data[start_pos..match_pos]).into()
|
||||
} else {
|
||||
None
|
||||
},
|
||||
);
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
} else if match_count > 0 {
|
||||
if ch == &boundary[0] {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
(0, None)
|
||||
} else if start_pos < stream.data.len() {
|
||||
(
|
||||
stream.data.len() - start_pos,
|
||||
Cow::from(&stream.data[start_pos..]).into(),
|
||||
)
|
||||
} else {
|
||||
(0, None)
|
||||
};
|
||||
}
|
||||
|
||||
#[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: &MessageStream) -> bool {
|
||||
let pos = stream.pos.get();
|
||||
|
||||
match stream.data.get(pos..pos + 2) {
|
||||
Some(b"--") => {
|
||||
if let Some(byte) = stream.data.get(pos + 2) {
|
||||
if !(*byte).is_ascii_whitespace() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
stream.set_pos(pos + 2);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
@@ -13,3 +13,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