mirror of
https://github.com/stalwartlabs/mail-parser.git
synced 2026-11-28 16:01:16 +00:00
base64_decode_stream and quoted_printable_decode_char
This commit is contained in:
@@ -3,7 +3,7 @@ use libfuzzer_sys::fuzz_target;
|
||||
|
||||
use mail_parser::{
|
||||
decoders::{
|
||||
base64::decode_base64,
|
||||
base64::base64_decode,
|
||||
charsets::{
|
||||
map::get_charset_decoder,
|
||||
single_byte::decoder_iso_8859_1,
|
||||
@@ -48,10 +48,10 @@ fuzz_target!(|data: &[u8]| {
|
||||
MessageStream::new(data).skip_crlf();
|
||||
MessageStream::new(data).is_multipart_end();
|
||||
|
||||
decode_base64(data);
|
||||
base64_decode(data);
|
||||
MessageStream::new(data).decode_base64_word();
|
||||
MessageStream::new(data).decode_base64_mime(b"\n");
|
||||
decode_quoted_printable(data);
|
||||
quoted_printable_decode(data);
|
||||
MessageStream::new(data).decode_quoted_printable_word();
|
||||
MessageStream::new(data).decode_quoted_printable_mime(b"\n");
|
||||
|
||||
|
||||
+39
-28
@@ -13,13 +13,22 @@ use std::borrow::Cow;
|
||||
|
||||
use crate::parsers::MessageStream;
|
||||
|
||||
pub fn decode_base64(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
#[inline(always)]
|
||||
pub fn base64_decode(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
base64_decode_stream(bytes.iter(), bytes.len(), u8::MAX)
|
||||
}
|
||||
|
||||
pub fn base64_decode_stream<'x>(
|
||||
stream: impl Iterator<Item = &'x u8>,
|
||||
stream_len: usize,
|
||||
stop_char: u8,
|
||||
) -> Option<Vec<u8>> {
|
||||
let mut chunk: u32 = 0;
|
||||
let mut byte_count: u8 = 0;
|
||||
|
||||
let mut buf = Vec::with_capacity(bytes.len() / 4 * 3);
|
||||
let mut buf = Vec::with_capacity(stream_len / 4 * 3);
|
||||
|
||||
for &ch in bytes {
|
||||
for &ch in stream {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
let val = unsafe {
|
||||
*BASE64_MAP
|
||||
@@ -46,33 +55,35 @@ pub fn decode_base64(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..3]);
|
||||
}
|
||||
}
|
||||
} else if ch == b'=' {
|
||||
match byte_count {
|
||||
1 | 2 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.push(*chunk.to_le_bytes().get_unchecked(0));
|
||||
} else {
|
||||
match ch {
|
||||
b'=' => match byte_count {
|
||||
1 | 2 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.push(*chunk.to_le_bytes().get_unchecked(0));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
byte_count = 0;
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.push(chunk.to_le_bytes()[0]);
|
||||
byte_count = 0;
|
||||
}
|
||||
3 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..2));
|
||||
3 => {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
unsafe {
|
||||
buf.extend_from_slice(chunk.to_le_bytes().get_unchecked(0..2));
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
buf.extend_from_slice(&chunk.to_le_bytes()[0..2]);
|
||||
byte_count = 0;
|
||||
}
|
||||
0 => (),
|
||||
_ => {
|
||||
return None;
|
||||
}
|
||||
0 => (),
|
||||
_ => {
|
||||
return None;
|
||||
}
|
||||
},
|
||||
b' ' | b'\t' | b'\r' | b'\n' => (),
|
||||
_ => return if ch == stop_char { buf.into() } else { None },
|
||||
}
|
||||
} else if !ch.is_ascii_whitespace() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -312,7 +323,7 @@ mod tests {
|
||||
("w6HDqcOtw7PDug==", "áéíóú"),
|
||||
] {
|
||||
assert_eq!(
|
||||
super::decode_base64(encoded_str.as_bytes()).unwrap_or_default(),
|
||||
super::base64_decode(encoded_str.as_bytes()).unwrap_or_default(),
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {:?}",
|
||||
encoded_str
|
||||
|
||||
@@ -20,7 +20,7 @@ enum QuotedPrintableState {
|
||||
Hex1,
|
||||
}
|
||||
|
||||
pub fn decode_quoted_printable(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
let mut buf = Vec::with_capacity(bytes.len());
|
||||
|
||||
let mut state = QuotedPrintableState::None;
|
||||
@@ -83,6 +83,30 @@ pub fn decode_quoted_printable(bytes: &[u8]) -> Option<Vec<u8>> {
|
||||
buf.into()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn quoted_printable_decode_char(hex1: u8, hex2: u8) -> Option<u8> {
|
||||
#[cfg(feature = "ludicrous_mode")]
|
||||
{
|
||||
let hex1 = unsafe { *HEX_MAP.get_unchecked(hex1 as usize) };
|
||||
let hex2 = unsafe { *HEX_MAP.get_unchecked(hex2 as usize) };
|
||||
if hex1 != -1 && hex2 != -1 {
|
||||
(((hex1 as u8) << 4) | hex2 as u8).into()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
#[cfg(not(feature = "ludicrous_mode"))]
|
||||
{
|
||||
let hex1 = HEX_MAP[hex1 as usize];
|
||||
let hex2 = HEX_MAP[hex2 as usize];
|
||||
if hex1 != -1 && hex2 != -1 {
|
||||
(((hex1 as u8) << 4) | hex2 as u8).into()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'x> MessageStream<'x> {
|
||||
pub fn decode_quoted_printable_mime(&mut self, boundary: &[u8]) -> (usize, Cow<'x, [u8]>) {
|
||||
let mut buf = Vec::with_capacity(128);
|
||||
@@ -305,7 +329,7 @@ mod tests {
|
||||
("\n\n", "\n\n"),
|
||||
] {
|
||||
assert_eq!(
|
||||
super::decode_quoted_printable(encoded_str.as_bytes()).unwrap_or_default(),
|
||||
super::quoted_printable_decode(encoded_str.as_bytes()).unwrap_or_default(),
|
||||
expected_result.as_bytes(),
|
||||
"Failed for {:?}",
|
||||
encoded_str
|
||||
|
||||
Reference in New Issue
Block a user