Before refactoring

This commit is contained in:
Mauro D
2021-10-06 15:36:06 +00:00
parent fb01c89f64
commit 9876382d2a
9 changed files with 259 additions and 145 deletions
+6 -2
View File
@@ -89,9 +89,13 @@ fn main() {
//let hdr = Header::parse(&mut parser);
let val = (HEX_TO_VAL['3' as usize] << 4) as u8 | HEX_TO_VAL['f' as usize] as u8;
let pepe = "coco".as_bytes();
let t = pepe.iter();
println!("{}", val);
/*static HASH_VALUES: &'static [i8] =
+3
View File
@@ -0,0 +1,3 @@
pub mod address;
pub mod unstructured;
+64
View File
@@ -0,0 +1,64 @@
use std::borrow::{Borrow, Cow};
use crate::parsers::{message_stream::MessageStream, rfc2047::Rfc2047Parser};
enum UnstructState {
Lf,
Space,
Char
}
pub fn parse_unstructured<'x>(stream: &'x mut MessageStream) -> Option<Cow<'x, str>> {
let mut token_start: isize = -1;
let mut token_end: isize = -1;
let mut state = UnstructState::Space;
//let mut tokens = Vec::new();
while let Some(ch) = stream.next_mut() {
match ch {
b'\n' => {
if let UnstructState::Lf = state {
stream.rewind(1);
break;
}
state = UnstructState::Lf;
token_start = -1;
},
b' ' | b'\t' | b'\r' => {
state = UnstructState::Space;
},
_ => {
if *ch > 126 {
*ch = b'!';
}
match state {
UnstructState::Lf => {
stream.rewind(1);
break;
},
UnstructState::Space => {
if token_start == -1 {
//token_start = stream.get_read_pos();
}
/*if *ch == b'=' && stream.skip_byte(b'?') {
tokens.push(stream.end_write());
if let Some(_) = Rfc2047Parser::parse(stream) {
tokens.push(stream.end_write());
token_start = -1;
} else {
}
}*/
},
UnstructState::Char => todo!(),
}
}
}
}
None
}
+118 -119
View File
@@ -1,144 +1,143 @@
use std::borrow::Cow;
use crate::parsers::fields::address::{Address, AddressList};
use crate::parsers::message_stream::MessageStream;
use crate::parsers::address::*;
use super::fields::unstructured::parse_unstructured;
pub enum Header<'x> {
Sender(Address<'x>),
From(Address<'x>),
To(AddressList<'x>),
Cc(AddressList<'x>),
Bcc(AddressList<'x>),
Subject(&'x str),
Subject(Cow<'x, str>),
Other((&'x str, &'x str)),
}
/*
List-Help
List-Unsubscribe
List-Subscribe
List-Post
List-Owner
List-Archive
*/
pub enum EncodingType {
QuotedPrintable,
Base64,
Bit7,
Bit8
}
pub struct NonAsciiText<'x> {
encoding: EncodingType,
charset: &'x [u8],
data: &'x [u8]
}
impl<'x> Header<'x> {
pub fn parse(s: &'x mut MessageStream) -> Option<Header<'x>> {
let mut ch_hash_1: u8 = 0;
let mut ch_hash_2: u8 = 0;
let mut start_pos: isize = -1;
let mut end_pos: isize = -1;
let hash_ch = |x: u8| -> u32 {
match x {
98 | 99 | 101 | 102 | 105 | 114 => 0,
103 | 104 | 107 => 5,
100 | 116 => 10,
109 | 115 => 15,
111 => 25,
_ => 38
pub fn parse(stream: &'x mut MessageStream) -> Option<Header<'x>> {
while let Some(header) = Header::parse_hdr_name(stream) {
match header {
(0, b"cc") => {}
(1, b"bcc") => {}
(7, b"resent-cc") => {}
(8, b"resent-bcc") => {}
(9, b"resent-date") => {}
(10, b"content-type") => {}
(11, b"resent-sender") => {}
(12, b"date") => {}
(13, b"list-owner") => {}
(14, b"resent-from") => {}
(15, b"list-archive") => {}
(16, b"received") => {}
(17, b"list-subscribe") => {}
(18, b"content-id") => {}
(19, b"list-unsubscribe") => {}
(22, b"list-post") => {}
(23, b"message-id") => {}
(24, b"sender") => {}
(25, b"resent-message-id") => {}
(26, b"comments") => {}
(27, b"list-help") => {}
(28, b"references") => {}
(29, b"return-path") => {}
(30, b"mime-version") => {}
(31, b"keywords") => {}
(32, b"content-description") => {}
(33, b"content-transfer-encoding") => {}
(35, b"subject") => return Some(Header::Subject(parse_unstructured(stream)?)),
(36, b"reply-to") => {}
(37, b"resent-to") => {}
(39, b"in-reply-to") => {}
(40, b"to") => {}
(42, b"from") => {}
_ => ()
}
};
}
while let Some(ch) = s.next() {
None
}
pub fn parse_hdr_name(stream: &'x mut MessageStream) -> Option<(u32, &'x [u8])> {
let mut started = false;
let mut first_ch: u8 = 0;
let mut last_ch: u8 = 0;
while let Some(ch) = stream.next_mut() {
match ch {
10 => return None,
13 | 9 | 32 => (), // CR TAB SPACE
58 if start_pos != -1 => { // :
let field_len: u32 = (end_pos - start_pos + 1) as u32;
if (2..=25).contains(&field_len) {
let hash = field_len + hash_ch(ch_hash_2) + hash_ch(ch_hash_1);
if (2..=37).contains(&hash) {
let field = (*s).get_bytes(start_pos as usize, (end_pos + 1) as usize).unwrap();
match hash - 2 {
0 if field == b"cc" => {
b':' => {
if started {
return Some((
stream.get_written_bytes() as u32
+ unsafe {
*HDR_HASH.get_unchecked(first_ch as usize) as u32
+ *HDR_HASH.get_unchecked(last_ch as usize) as u32
},
1 if field == b"bcc" => {
},
7 if field == b"resent-cc" => {
},
8 if field == b"resent-bcc" => {
},
9 if field == b"resent-date" => {
},
10 if field == b"content-type" => {
},
11 if field == b"resent-sender" => {
},
12 if field == b"date" => {
},
14 if field == b"return-path" => {
},
16 if field == b"received" => {
},
17 if field == b"from" => {
},
18 if field == b"content-id" => {
},
19 if field == b"sender" => {
},
21 if field == b"comments" => {
},
23 if field == b"references" => {
},
24 if field == b"resent-from" => {
},
25 if field == b"resent-message-id" => {
},
26 if field == b"keywords" => {
},
28 if field == b"content-transfer-encoding" => {
},
30 if field == b"subject" => {
println!("Subject!");
},
31 if field == b"reply-to" => {
},
32 if field == b"resent-to" => {
},
33 if field == b"message-id" => {
},
34 if field == b"in-reply-to" => {
},
35 if field == b"to" => {
},
_ => ()
}
}
stream.end_write().unwrap(),
));
}
},
}
b' ' | b'\n' | b'\r' => (),
_ => {
if start_pos != -1 {
ch_hash_2 = *ch;
end_pos = (*s).get_read_pos();
} else {
end_pos = start_pos;
ch_hash_1 = *ch;
start_pos = (*s).get_read_pos();
if ch.is_ascii_uppercase() {
*ch += 32;
}
if !started {
first_ch = *ch;
stream.begin_write();
started = true;
} else {
last_ch = *ch;
}
stream.write_skip();
}
}
}
None
}
}
#[cfg(test)]
mod tests {
use crate::parsers::{header::Header, message_stream::MessageStream};
#[test]
fn parse_field_name() {
unsafe {
let inputs = [
("From: ".to_string(), "from"),
("\n\n \nreceived-FROM: ".to_string(), "received-from"),
(" subject : ".to_string(), "subject"),
("X-Custom-Field : ".to_string(), "x-custom-field"),
("mal formed: ".to_string(), "mal forme"), // Not expected to parse this
];
for input in inputs {
match Header::parse_hdr_name(&mut MessageStream::new(
input.0.clone().as_bytes_mut(),
)) {
Some((_, string)) => assert_eq!(input.1, std::str::from_utf8(string).unwrap()),
None => panic!("Failed to parse '{}'", input.0),
}
}
}
}
}
static HDR_HASH: &[u8] = &[
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 0, 0, 10, 0, 35, 10, 20, 0, 45, 5, 5, 5, 15, 30, 15, 45, 0, 20, 10, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
];
+33 -17
View File
@@ -1,35 +1,30 @@
use std::borrow::{Borrow};
use std::{borrow::{Borrow}, cell::Cell};
pub struct MessageStream<'x> {
data: &'x mut [u8],
data: &'x [u8],
read_pos: isize,
write_pos: usize,
write_start: usize
write_start: usize,
test: Cell<usize>
}
impl<'x> MessageStream<'x> {
pub fn new(data: &mut [u8]) -> MessageStream {
pub fn new(data: &[u8]) -> MessageStream {
MessageStream {
data,
read_pos: -1,
write_pos: 0,
write_start: 0
write_start: 0,
test: Cell::new(0)
}
}
pub fn get_bytes(&self, from: usize, to: usize) -> Option<&[u8]> {
self.test.set(1);
self.data.get(from..to)
}
pub fn get_str_lossy(&'x self, from: usize, to: usize) -> Option<&str> {
self.data.get(from..to).map(|bytes| unsafe { std::str::from_utf8_unchecked(bytes) })
//self.data.get(from..to).map(|bytes| String::from_utf8_lossy(bytes).borrow() )
}
pub fn get_str(&'x self, from: usize, to: usize) -> Option<&str> {
self.data.get(from..to).map(|bytes| unsafe { std::str::from_utf8_unchecked(bytes) })
}
#[inline(always)]
pub fn begin_write(&mut self) {
if self.read_pos > 0 {
@@ -40,21 +35,31 @@ impl<'x> MessageStream<'x> {
#[inline(always)]
pub fn write(&mut self, ch: u8) {
self.data[self.write_pos] = ch;
//self.data[self.write_pos] = ch;
self.write_pos += 1;
}
#[inline(always)]
pub fn write_slice(&mut self, chars: &[u8]) {
self.data[self.write_pos..(self.write_pos+chars.len())].copy_from_slice(chars);
//self.data[self.write_pos..(self.write_pos+chars.len())].copy_from_slice(chars);
self.write_pos += chars.len();
}
#[inline(always)]
pub fn write_skip(&mut self) {
self.write_pos += 1;
}
#[inline(always)]
pub fn end_write(&self) -> Option<&[u8]> {
self.data.get(self.write_start..self.write_pos)
}
#[inline(always)]
pub fn get_written_bytes(&self) -> isize {
self.write_pos as isize - self.write_start as isize
}
#[inline(always)]
pub fn get_read_pos(&self) -> isize {
self.read_pos
@@ -74,7 +79,8 @@ impl<'x> MessageStream<'x> {
#[inline(always)]
pub fn next_mut(&mut self) -> Option<&mut u8> {
self.read_pos += 1;
self.data.get_mut(self.read_pos as usize)
None
//self.data.get_mut(self.read_pos as usize)
}
pub fn skip_byte(&mut self, ch: u8) -> bool {
@@ -98,6 +104,16 @@ impl<'x> MessageStream<'x> {
}
}
/*pub fn skip_spaces(&mut self) -> Option<&u8> {
loop {
self.read_pos += 1;
let ch = self.data.get(self.read_pos as usize)?;
if !ch.is_ascii_whitespace() {
return Some(ch);
}
}
}*/
pub fn rewind(&mut self, read_pos: usize) {
self.read_pos = self.read_pos - std::cmp::min(self.read_pos, read_pos as isize);
}
+2 -1
View File
@@ -1,5 +1,6 @@
pub mod address;
pub mod fields;
pub mod header;
pub mod message_stream;
pub mod rfc2047;
pub mod token;
+6 -6
View File
@@ -8,7 +8,7 @@ use crate::{
parsers::message_stream::MessageStream,
};
enum Rfc2047Parser {
pub enum Rfc2047Parser {
Charset,
Language,
Encoding,
@@ -116,17 +116,17 @@ mod tests {
("iso-8859-1?q?this=20is=20some=20text?=".to_string(), "this is some text", true),
("iso-8859-1?q?this is some text?=".to_string(), "this is some text", true),
("US-ASCII?Q?Keith_Moore?=".to_string(), "Keith Moore", false),
("ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=".to_string(), "Keld Jørn Simonsen", true),
("iso_8859-1:1987?Q?Keld_J=F8rn_Simonsen?=".to_string(), "Keld Jørn Simonsen", true),
("ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=".to_string(), "If you can read this yo", true),
("ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=".to_string(), "u understand the example.", true),
("ISO-8859-1?Q?Olle_J=E4rnefors?=".to_string(), "Olle Järnefors", true),
("ISO-8859-1?Q?Patrik_F=E4ltstr=F6m?=".to_string(), "Patrik Fältström", true),
("ISO-8859-1?Q?a?=".to_string(), "a", true),
("ISO-8859-1?Q?a_b?=".to_string(), "a b", true),
("ISO-8859-1*?Q?a?=".to_string(), "a", true),
("ISO-8859-1**?Q?a_b?=".to_string(), "a b", true),
("utf-8?b?VGjDrXMgw61zIHbDoWzDrWQgw5pURjg=?=".to_string(), "Thís ís válíd ÚTF8", false),
("utf-8?q?Th=C3=ADs_=C3=ADs_v=C3=A1l=C3=ADd_=C3=9ATF8?=".to_string(), "Thís ís válíd ÚTF8", false),
("utf-8*unknown?q?Th=C3=ADs_=C3=ADs_v=C3=A1l=C3=ADd_=C3=9ATF8?=".to_string(), "Thís ís válíd ÚTF8", false),
("Iso-8859-6?Q?=E5=D1=CD=C8=C7 =C8=C7=E4=D9=C7=E4=E5?=".to_string(), "مرحبا بالعالم", true),
("Iso-8859-6?b?5dHNyMcgyMfk2cfk5Q==?=".to_string(), "مرحبا بالعالم", true),
("Iso-8859-6*arabic?b?5dHNyMcgyMfk2cfk5Q==?=".to_string(), "مرحبا بالعالم", true),
#[cfg(feature = "multibytedecode")]
("shift_jis?B?g26DjYFbgUWDj4Fbg4uDaA==?=".to_string(), "ハロー・ワールド", true),
#[cfg(feature = "multibytedecode")]
+27
View File
@@ -0,0 +1,27 @@
pub enum TokenPart {
ByteRange((u32, u32)),
String(String)
}
pub struct Token {
items: Vec<TokenPart>
}
impl Token {
pub fn new() -> Token {
Token { items: Vec::new() }
}
pub fn push_string(&mut self, string: String) {
self.items.push(TokenPart::String(string));
}
pub fn push_bytes(&mut self, from: u32, to: u32) {
self.items.push(TokenPart::ByteRange((from, to)));
}
}