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 | ||
|
|
ee2d08be07 | ||
|
|
1717f803db | ||
|
|
549e8d2428 | ||
|
|
972b4d6dfc | ||
|
|
5d4c4a06c7 | ||
|
|
17833a57b4 | ||
|
|
52a90c3eff | ||
|
|
31bb44cc29 | ||
|
|
24e17cf675 | ||
|
|
73af8a971e | ||
|
|
847359ae39 | ||
|
|
419a9a9fad | ||
|
|
80c7b78646 | ||
|
|
4eebde1020 | ||
|
|
260fd98d7d |
@@ -0,0 +1,22 @@
|
||||
name: "CI"
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Build
|
||||
run: cargo build --verbose
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
@@ -0,0 +1,18 @@
|
||||
stages:
|
||||
- build
|
||||
|
||||
rust-latest:
|
||||
stage: build
|
||||
image: rust:latest
|
||||
script:
|
||||
- cargo build --verbose
|
||||
- cargo test --verbose
|
||||
|
||||
rust-nightly:
|
||||
stage: build
|
||||
image: rustlang/rust:nightly
|
||||
script:
|
||||
- cargo build --verbose
|
||||
- cargo test --verbose
|
||||
allow_failure: true
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
language: rust
|
||||
rust:
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
matrix:
|
||||
allow_failures:
|
||||
- rust: nightly
|
||||
Generated
+1
-1
@@ -59,7 +59,7 @@ checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.1.0"
|
||||
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.0"
|
||||
version = "0.2.1"
|
||||
edition = "2018"
|
||||
authors = [ "Stalwart Labs <hello@stalw.art>"]
|
||||
license = "Apache-2.0 OR MIT"
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
# mail-parser
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
[](https://crates.io/crates/mail-parser)
|
||||
[](https://github.com/stalwartlabs/mail-parser/actions/workflows/rust.yml)
|
||||
[](https://docs.rs/mail-parser)
|
||||
[](http://www.apache.org/licenses/LICENSE-2.0)
|
||||
[](https://twitter.com/stalwartlabs)
|
||||
|
||||
_mail-parser_ is an **e-mail parsing library** written in Rust that fully conforms to the Internet Message Format standard (_RFC 5322_), the
|
||||
Multipurpose Internet Mail Extensions (MIME; _RFC 2045 - 2049_) as well as other [internet messaging RFCs](#conformed-rfcs).
|
||||
|
||||
It also supports decoding messages in [41 different character sets](#supported-character-sets) including obsolete formats such as UTF-7.
|
||||
All Unicode (UTF-*) and single-byte character sets are handled internally by the library while support for legacy multi-byte encodings of Chinese
|
||||
and Japanse languages such as BIG5 or ISO-2022-JP is provided by the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs).
|
||||
and Japanese languages such as BIG5 or ISO-2022-JP is provided by the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs).
|
||||
|
||||
In general, this library abides by the Postel's law or [Robustness Principle](https://en.wikipedia.org/wiki/Robustness_principle) which
|
||||
states that an implementation must be conservative in its sending behavior and liberal in its receiving behavior. This means that
|
||||
_mail-parser_ will make a best effort to parse non-conformat e-mail messages as long as these do not deviate too much from the standard.
|
||||
_mail-parser_ will make a best effort to parse non-conformant e-mail messages as long as these do not deviate too much from the standard.
|
||||
|
||||
Unlike other e-mail parsing libraries that return nested representations of the different MIME parts in a message, this library
|
||||
conforms to [RFC 8621, Section 4.1.4](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4) and provides a more human-friendly
|
||||
@@ -23,18 +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.
|
||||
- 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",
|
||||
@@ -69,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!(
|
||||
@@ -183,7 +183,7 @@ Performance and memory safety were two important factors while designing _mail-p
|
||||
println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||
```
|
||||
|
||||
## Testing & Benchmarking
|
||||
## Testing, Fuzzing & Benchmarking
|
||||
|
||||
To run the testsuite:
|
||||
|
||||
@@ -191,13 +191,19 @@ To run the testsuite:
|
||||
$ cargo test --all-features
|
||||
```
|
||||
|
||||
or, to run the testsuite using MIRI:
|
||||
or, to run the testsuite with MIRI:
|
||||
|
||||
```bash
|
||||
$ cargo +nightly miri test --all-features
|
||||
```
|
||||
|
||||
and to run the benchmarks:
|
||||
To fuzz the library with `cargo-fuzz`:
|
||||
|
||||
```bash
|
||||
$ cargo +nightly fuzz run mail_parser
|
||||
```
|
||||
|
||||
and, to run the benchmarks:
|
||||
|
||||
```bash
|
||||
$ cargo +nightly bench --all-features
|
||||
|
||||
+17
-20
@@ -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,13 +63,9 @@ 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());
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
#[bench]
|
||||
@@ -79,23 +75,19 @@ fn bench_stalwart(b: &mut Bencher) {
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
// These libraries do not support all RFCs and might be faster
|
||||
// on the benchmarks as they skip certain encoded parts or fail
|
||||
// while trying to parse the messages.
|
||||
// Also no body conversion between HTML/text is done by these.
|
||||
// on the benchmarks as they do not parse all header fields,
|
||||
// do not decode encoded parts or fail while trying to parse the messages.
|
||||
// Also no text body conversion between HTML/plain-text is done by these.
|
||||
//
|
||||
// To benchmark against these libraries, add to Cargo.toml dev-dependencies:
|
||||
//
|
||||
// email = "0.0.21"
|
||||
// email-format = "0.8"
|
||||
// email-parser = "0.5.0"
|
||||
// email-parser = { version = "0.5.0", features=["headers", "mime"] }
|
||||
// mailparse = "0.13"
|
||||
|
||||
#[bench]
|
||||
fn bench_mailparse(b: &mut Bencher) {
|
||||
bench_all_samples(b, "mailparse", |bytes, _str| {
|
||||
mailparse::parse_mail(bytes).unwrap();
|
||||
});
|
||||
}
|
||||
/*
|
||||
|
||||
#[bench]
|
||||
fn bench_email_parser(b: &mut Bencher) {
|
||||
@@ -104,6 +96,13 @@ fn bench_email_parser(b: &mut Bencher) {
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn bench_mailparse(b: &mut Bencher) {
|
||||
bench_all_samples(b, "mailparse", |bytes, _str| {
|
||||
mailparse::parse_mail(bytes).unwrap();
|
||||
});
|
||||
}
|
||||
|
||||
#[bench]
|
||||
fn benchmark_email(b: &mut Bencher) {
|
||||
bench_all_samples(b, "email", |_bytes, str| {
|
||||
@@ -119,6 +118,4 @@ fn bench_email_format(b: &mut Bencher) {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
/*
|
||||
* 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 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",
|
||||
"Date: Sat, 20 Nov 2021 14:22:01 -0800\n",
|
||||
"Subject: Why not both importing AND exporting? =?utf-8?b?4pi6?=\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"festivus\";\n\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: text/html; charset=\"us-ascii\"\n",
|
||||
"Content-Transfer-Encoding: base64\n\n",
|
||||
"PGh0bWw+PHA+SSB3YXMgdGhpbmtpbmcgYWJvdXQgcXVpdHRpbmcgdGhlICZsZHF1bztle\n",
|
||||
"HBvcnRpbmcmcmRxdW87IHRvIGZvY3VzIGp1c3Qgb24gdGhlICZsZHF1bztpbXBvcnRpbm\n",
|
||||
"cmcmRxdW87LDwvcD48cD5idXQgdGhlbiBJIHRob3VnaHQsIHdoeSBub3QgZG8gYm90aD8\n",
|
||||
"gJiN4MjYzQTs8L3A+PC9odG1sPg==\n",
|
||||
"--festivus\n",
|
||||
"Content-Type: message/rfc822\n\n",
|
||||
"From: \"Cosmo Kramer\" <kramer@kramerica.com>\n",
|
||||
"Subject: Exporting my book about coffee tables\n",
|
||||
"Content-Type: multipart/mixed; boundary=\"giddyup\";\n\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: text/plain; charset=\"utf-16\"\n",
|
||||
"Content-Transfer-Encoding: quoted-printable\n\n",
|
||||
"=FF=FE=0C!5=D8\"=DD5=D8)=DD5=D8-=DD =005=D8*=DD5=D8\"=DD =005=D8\"=\n",
|
||||
"=DD5=D85=DD5=D8-=DD5=D8,=DD5=D8/=DD5=D81=DD =005=D8*=DD5=D86=DD =\n",
|
||||
"=005=D8=1F=DD5=D8,=DD5=D8,=DD5=D8(=DD =005=D8-=DD5=D8)=DD5=D8\"=\n",
|
||||
"=DD5=D8=1E=DD5=D80=DD5=D8\"=DD!=00\n",
|
||||
"--giddyup\n",
|
||||
"Content-Type: image/gif; name*1=\"about \"; name*0=\"Book \";\n",
|
||||
" name*2*=utf-8''%e2%98%95 tables.gif\n",
|
||||
"Content-Transfer-Encoding: Base64\n",
|
||||
"Content-Disposition: attachment\n\n",
|
||||
"R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7\n",
|
||||
"--giddyup--\n",
|
||||
"--festivus--\n",
|
||||
)
|
||||
.as_bytes();
|
||||
|
||||
let message = Message::parse(input);
|
||||
|
||||
// Parses addresses (including comments), lists and groups
|
||||
assert_eq!(
|
||||
message.get_from(),
|
||||
&Address::Address(Addr {
|
||||
name: Some("Art Vandelay (Vandelay Industries)".into()),
|
||||
address: Some("art@vandelay.com".into())
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_to(),
|
||||
&Address::GroupList(vec![
|
||||
Group {
|
||||
name: Some("Colleagues".into()),
|
||||
addresses: vec![Addr {
|
||||
name: Some("James Smythe".into()),
|
||||
address: Some("james@vandelay.com".into())
|
||||
}]
|
||||
},
|
||||
Group {
|
||||
name: Some("Friends".into()),
|
||||
addresses: vec![
|
||||
Addr {
|
||||
name: None,
|
||||
address: Some("jane@example.com".into())
|
||||
},
|
||||
Addr {
|
||||
name: Some("John Smîth".into()),
|
||||
address: Some("john@example.com".into())
|
||||
}
|
||||
]
|
||||
}
|
||||
])
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
message.get_date().unwrap().to_iso8601(),
|
||||
"2021-11-20T14:22:01-08:00"
|
||||
);
|
||||
|
||||
// RFC2047 support for encoded text in message readers
|
||||
assert_eq!(
|
||||
message.get_subject().unwrap(),
|
||||
"Why not both importing AND exporting? ☺"
|
||||
);
|
||||
|
||||
// HTML and text body parts are returned conforming to RFC8621, Section 4.1.4
|
||||
assert_eq!(
|
||||
message.get_html_body(0).unwrap().to_string(),
|
||||
concat!(
|
||||
"<html><p>I was thinking about quitting the “exporting” to ",
|
||||
"focus just on the “importing”,</p><p>but then I thought,",
|
||||
" why not do both? ☺</p></html>"
|
||||
)
|
||||
);
|
||||
|
||||
// HTML parts are converted to plain text (and viceversa) when missing
|
||||
assert_eq!(
|
||||
message.get_text_body(0).unwrap().to_string(),
|
||||
concat!(
|
||||
"I was thinking about quitting the “exporting” to focus just on the",
|
||||
" “importing”,\nbut then I thought, why not do both? ☺\n"
|
||||
)
|
||||
);
|
||||
|
||||
// Supports nested messages as well as multipart/digest
|
||||
let nested_message = match message.get_attachment(0).unwrap() {
|
||||
MessagePart::Message(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
nested_message.get_subject().unwrap(),
|
||||
"Exporting my book about coffee tables"
|
||||
);
|
||||
|
||||
// Handles UTF-* as well as many legacy encodings
|
||||
assert_eq!(
|
||||
nested_message.get_text_body(0).unwrap().to_string(),
|
||||
"ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!"
|
||||
);
|
||||
assert_eq!(
|
||||
nested_message.get_html_body(0).unwrap().to_string(),
|
||||
"<html><body>ℌ𝔢𝔩𝔭 𝔪𝔢 𝔢𝔵𝔭𝔬𝔯𝔱 𝔪𝔶 𝔟𝔬𝔬𝔨 𝔭𝔩𝔢𝔞𝔰𝔢!</body></html>"
|
||||
);
|
||||
|
||||
let nested_attachment = match nested_message.get_attachment(0).unwrap() {
|
||||
MessagePart::Binary(v) => v,
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
assert_eq!(nested_attachment.len(), 42);
|
||||
|
||||
// Full RFC2231 support for continuations and character sets
|
||||
assert_eq!(
|
||||
nested_attachment
|
||||
.get_header()
|
||||
.unwrap()
|
||||
.get_content_type()
|
||||
.unwrap()
|
||||
.get_attribute("name")
|
||||
.unwrap(),
|
||||
"Book about ☕ tables.gif"
|
||||
);
|
||||
|
||||
// Integrates with Serde
|
||||
println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||
println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
target
|
||||
corpus
|
||||
artifacts
|
||||
Generated
+128
@@ -0,0 +1,128 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "arbitrary"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "577b08a4acd7b99869f863c50011b01eb73424ccc798ecd996f2e24817adfca7"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.71"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a74ea89a0a1b98f6332de42c95baff457ada66d1cb4030f9ff151b2041a1c746"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libfuzzer-sys"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36a9a84a6e8b55dfefb04235e55edb2b9a2a18488fcae777a6bdaa6f06f1deb3"
|
||||
dependencies = [
|
||||
"arbitrary",
|
||||
"cc",
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"serde",
|
||||
"serde_bytes",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mail-parser-fuzz"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"libfuzzer-sys",
|
||||
"mail-parser",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.130"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_bytes"
|
||||
version = "0.11.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "16ae07dd2f88a366f15bd0632ba725227018c69a1c8550a927324f8eb8368bb9"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.130"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.81"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
@@ -0,0 +1,25 @@
|
||||
[package]
|
||||
name = "mail-parser-fuzz"
|
||||
version = "0.0.0"
|
||||
authors = ["Automatically generated"]
|
||||
publish = false
|
||||
edition = "2018"
|
||||
|
||||
[package.metadata]
|
||||
cargo-fuzz = true
|
||||
|
||||
[dependencies]
|
||||
libfuzzer-sys = "0.4"
|
||||
|
||||
[dependencies.mail-parser]
|
||||
path = ".."
|
||||
|
||||
# Prevent this from interfering with workspaces
|
||||
[workspace]
|
||||
members = ["."]
|
||||
|
||||
[[bin]]
|
||||
name = "mail_parser"
|
||||
path = "fuzz_targets/mail_parser.rs"
|
||||
test = false
|
||||
doc = false
|
||||
@@ -0,0 +1,141 @@
|
||||
#![no_main]
|
||||
use libfuzzer_sys::fuzz_target;
|
||||
|
||||
use mail_parser::{
|
||||
decoders::{
|
||||
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::decode_rfc2047,
|
||||
hex::decode_hex,
|
||||
html::{add_html_token, html_to_text, text_to_html},
|
||||
quoted_printable::decode_quoted_printable,
|
||||
},
|
||||
parsers::{
|
||||
fields::{
|
||||
address::parse_address,
|
||||
content_type::parse_content_type,
|
||||
date::parse_date,
|
||||
id::parse_id,
|
||||
list::parse_comma_separared,
|
||||
raw::{parse_and_ignore, parse_raw},
|
||||
unstructured::parse_unstructured,
|
||||
},
|
||||
mime::*,
|
||||
header::parse_header_name,
|
||||
message::MessageStream,
|
||||
},
|
||||
Message,
|
||||
};
|
||||
|
||||
fuzz_target!(|data: &[u8]| {
|
||||
// Fuzz every parsing function
|
||||
for n_fuzz in 1..=24 {
|
||||
let mut stream = MessageStream::new(&data);
|
||||
|
||||
match n_fuzz {
|
||||
1 => {
|
||||
parse_date(&mut stream);
|
||||
}
|
||||
2 => {
|
||||
parse_address(&mut stream);
|
||||
}
|
||||
3 => {
|
||||
parse_id(&mut stream);
|
||||
}
|
||||
4 => {
|
||||
parse_comma_separared(&mut stream);
|
||||
}
|
||||
5 => {
|
||||
parse_and_ignore(&mut stream);
|
||||
}
|
||||
6 => {
|
||||
parse_raw(&mut stream);
|
||||
}
|
||||
7 => {
|
||||
parse_unstructured(&mut stream);
|
||||
}
|
||||
8 => {
|
||||
parse_content_type(&mut stream);
|
||||
}
|
||||
9 => {
|
||||
parse_header_name(&mut stream);
|
||||
}
|
||||
10 => {
|
||||
decode_rfc2047(&mut stream, 0);
|
||||
}
|
||||
11 => {
|
||||
seek_next_part(&mut stream, b"\n");
|
||||
}
|
||||
12 => {
|
||||
get_bytes_to_boundary(&mut stream, 0, b"\n", false);
|
||||
}
|
||||
13 => {
|
||||
get_bytes_to_boundary(&mut stream, 0, &[], false);
|
||||
}
|
||||
14 => {
|
||||
skip_crlf(&mut stream);
|
||||
}
|
||||
15 => {
|
||||
is_boundary_end(&mut stream, 0);
|
||||
}
|
||||
16 => {
|
||||
skip_multipart_end(&mut stream);
|
||||
}
|
||||
17 => {
|
||||
decode_base64(&mut stream, 0, b"\n", true);
|
||||
}
|
||||
18 => {
|
||||
decode_base64(&mut stream, 0, b"\n", false);
|
||||
}
|
||||
19 => {
|
||||
decode_base64(&mut stream, 0, &[], true);
|
||||
}
|
||||
20 => {
|
||||
decode_base64(&mut stream, 0, &[], false);
|
||||
}
|
||||
21 => {
|
||||
decode_quoted_printable(&mut stream, 0, b"\n\n", true); // QP Boundaries have to be at least 2 bytes long
|
||||
}
|
||||
22 => {
|
||||
decode_quoted_printable(&mut stream, 0, b"\n\n", false); // QP Boundaries have to be at least 2 bytes long
|
||||
}
|
||||
23 => {
|
||||
decode_quoted_printable(&mut stream, 0, &[], true);
|
||||
}
|
||||
24 => {
|
||||
decode_quoted_printable(&mut stream, 0, &[], false);
|
||||
}
|
||||
0 | 25..=u32::MAX => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
// Fuzz HTML functions
|
||||
let mut html_str = String::with_capacity(data.len());
|
||||
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]) -> String] = &[
|
||||
decoder_utf7,
|
||||
decoder_utf16_le,
|
||||
decoder_utf16_be,
|
||||
decoder_utf16,
|
||||
decoder_iso_8859_1,
|
||||
];
|
||||
|
||||
for decoder in decoders {
|
||||
decoder(data);
|
||||
}
|
||||
|
||||
// Fuzz the entire library
|
||||
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,
|
||||
|
||||
+611
-615
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,128 +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 {
|
||||
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>());
|
||||
|
||||
// Safe because n_bytes is always lower than size_of::<u32>
|
||||
unsafe {
|
||||
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 because BASEMAP 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,84 +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 {
|
||||
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(),
|
||||
@@ -165,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
-2
@@ -39,7 +39,7 @@ pub fn decode_hex(src: &[u8]) -> (bool, Vec<u8>) {
|
||||
result.push(*ch);
|
||||
}
|
||||
HexState::Percent => {
|
||||
hex1 = unsafe { *HEX_MAP.get_unchecked(*ch as usize) };
|
||||
hex1 = HEX_MAP[*ch as usize];
|
||||
if hex1 != -1 {
|
||||
state = HexState::Hex1;
|
||||
} else {
|
||||
@@ -48,7 +48,7 @@ pub fn decode_hex(src: &[u8]) -> (bool, Vec<u8>) {
|
||||
}
|
||||
}
|
||||
HexState::Hex1 => {
|
||||
let hex2 = unsafe { *HEX_MAP.get_unchecked(*ch as usize) };
|
||||
let hex2 = HEX_MAP[*ch as usize];
|
||||
|
||||
state = HexState::None;
|
||||
if hex2 != -1 {
|
||||
|
||||
+21
-27
@@ -30,36 +30,32 @@ pub fn add_html_token(result: &mut String, token: &[u8], add_space: bool) {
|
||||
} else if (2..=31).contains(&entity.len()) {
|
||||
let mut hash = entity.len() as u32;
|
||||
|
||||
/* Safe because ENTITY_HASH's size is 260 (u8::MAX + 5)
|
||||
and ENTITY_MAP's size is 18016 (18079 - 64 + 1) */
|
||||
debug_assert_eq!(ENTITY_HASH.len(), u8::MAX as usize + 5);
|
||||
debug_assert_eq!(ENTITY_MAP.len(), 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 because 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,7 +231,7 @@ pub fn text_to_html(input: &str) -> String {
|
||||
}
|
||||
result.extend_from_slice(b"</body></html>");
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
+152
-184
@@ -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,208 +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);
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
while read_pos < data_len {
|
||||
let ch = *(*data).get_unchecked(read_pos);
|
||||
read_pos += 1;
|
||||
// if success is false, a boundary was provided
|
||||
if !success {
|
||||
let mut is_boundary_end = true;
|
||||
|
||||
if !success {
|
||||
if ch == *boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
let done = is_word || self.is_boundary_end(read_pos);
|
||||
data = &mut *self.data.get(); // Avoid violating the Stacked Borrows rules
|
||||
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 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))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = *ch;
|
||||
}
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && *ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
state = QuotedPrintableState::None;
|
||||
|
||||
if ch == *boundary.get_unchecked(0) {
|
||||
match_count = 1;
|
||||
continue;
|
||||
} else {
|
||||
match_count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = b'\n';
|
||||
}
|
||||
|
||||
write_pos += 1;
|
||||
}
|
||||
}
|
||||
b'_' if is_word => {
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = b' ';
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = b' ';
|
||||
}
|
||||
write_pos += 1;
|
||||
}
|
||||
b'\r' => (),
|
||||
_ => match state {
|
||||
QuotedPrintableState::None => {
|
||||
#[cfg(miri)]
|
||||
{
|
||||
(*data)[write_pos] = ch;
|
||||
}
|
||||
#[cfg(not(miri))]
|
||||
{
|
||||
*((*data).get_unchecked_mut(write_pos)) = ch;
|
||||
}
|
||||
write_pos += 1;
|
||||
if is_utf8_safe && ch > 0x7f {
|
||||
is_utf8_safe = false;
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Eq => {
|
||||
hex1 = *HEX_MAP.get_unchecked(ch as usize);
|
||||
if hex1 != -1 {
|
||||
state = QuotedPrintableState::Hex1;
|
||||
} else {
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
QuotedPrintableState::Hex1 => {
|
||||
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))]
|
||||
{
|
||||
*((*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 {
|
||||
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!(
|
||||
@@ -289,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{}",
|
||||
@@ -314,6 +270,18 @@ 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[..]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+331
-113
@@ -10,37 +10,37 @@
|
||||
*/
|
||||
|
||||
//! # mail-parser
|
||||
//!
|
||||
//!
|
||||
//! _mail-parser_ is an **e-mail parsing library** written in Rust that fully conforms to the Internet Message Format standard (_RFC 5322_), the
|
||||
//! Multipurpose Internet Mail Extensions (MIME; _RFC 2045 - 2049_) as well as other [internet messaging RFCs](#conformed-rfcs).
|
||||
//!
|
||||
//!
|
||||
//! It also supports decoding messages in [41 different character sets](#supported-character-sets) including obsolete formats such as UTF-7.
|
||||
//! All Unicode (UTF-*) and single-byte character sets are handled internally by the library while support for legacy multi-byte encodings of Chinese
|
||||
//! and Japanse languages such as BIG5 or ISO-2022-JP is provided by the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs).
|
||||
//!
|
||||
//! In general, this library abides by the Postel's law or [Robustness Principle](https://en.wikipedia.org/wiki/Robustness_principle) which
|
||||
//! and Japanese languages such as BIG5 or ISO-2022-JP is provided by the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs).
|
||||
//!
|
||||
//! In general, this library abides by the Postel's law or [Robustness Principle](https://en.wikipedia.org/wiki/Robustness_principle) which
|
||||
//! states that an implementation must be conservative in its sending behavior and liberal in its receiving behavior. This means that
|
||||
//! _mail-parser_ will make a best effort to parse non-conformat e-mail messages as long as these do not deviate too much from the standard.
|
||||
//!
|
||||
//! Unlike other e-mail parsing libraries that return nested representations of the different MIME parts in a message, this library
|
||||
//! _mail-parser_ will make a best effort to parse non-conformant e-mail messages as long as these do not deviate too much from the standard.
|
||||
//!
|
||||
//! Unlike other e-mail parsing libraries that return nested representations of the different MIME parts in a message, this library
|
||||
//! conforms to [RFC 8621, Section 4.1.4](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4) and provides a more human-friendly
|
||||
//! representation of the message contents consisting of just text body parts, html body parts and attachments. Additionally, conversion to/from
|
||||
//! HTML and plain text inline body parts is done automatically when the _alternative_ version is missing.
|
||||
//!
|
||||
//!
|
||||
//! 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.
|
||||
//! - 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).
|
||||
//!
|
||||
//!
|
||||
//! ## Conformed RFCs
|
||||
//!
|
||||
//!
|
||||
//! - [RFC 822 - Standard for ARPA Internet Text Messages](https://datatracker.ietf.org/doc/html/rfc822)
|
||||
//! - [RFC 5322 - Internet Message Format](https://datatracker.ietf.org/doc/html/rfc5322)
|
||||
//! - [RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies](https://datatracker.ietf.org/doc/html/rfc2045)
|
||||
@@ -55,60 +55,60 @@
|
||||
//! - [RFC 2369 - The Use of URLs as Meta-Syntax for Core Mail List Commands and their Transport through Message Header Fields](https://datatracker.ietf.org/doc/html/rfc2369)
|
||||
//! - [RFC 2919 - List-Id: A Structured Field and Namespace for the Identification of Mailing Lists](https://datatracker.ietf.org/doc/html/rfc2919)
|
||||
//! - [RFC 8621 - The JSON Meta Application Protocol (JMAP) for Mail (Section 4.1.4)](https://datatracker.ietf.org/doc/html/rfc8621#section-4.1.4)
|
||||
//!
|
||||
//!
|
||||
//! ## Supported Character Sets
|
||||
//!
|
||||
//!
|
||||
//! - UTF-8
|
||||
//! - UTF-16, UTF-16BE, UTF-16LE
|
||||
//! - UTF-7
|
||||
//! - US-ASCII
|
||||
//! - ISO-8859-1
|
||||
//! - ISO-8859-2
|
||||
//! - ISO-8859-3
|
||||
//! - ISO-8859-4
|
||||
//! - ISO-8859-5
|
||||
//! - ISO-8859-6
|
||||
//! - ISO-8859-7
|
||||
//! - ISO-8859-8
|
||||
//! - ISO-8859-9
|
||||
//! - ISO-8859-10
|
||||
//! - ISO-8859-13
|
||||
//! - ISO-8859-14
|
||||
//! - ISO-8859-15
|
||||
//! - ISO-8859-1
|
||||
//! - ISO-8859-2
|
||||
//! - ISO-8859-3
|
||||
//! - ISO-8859-4
|
||||
//! - ISO-8859-5
|
||||
//! - ISO-8859-6
|
||||
//! - ISO-8859-7
|
||||
//! - ISO-8859-8
|
||||
//! - ISO-8859-9
|
||||
//! - ISO-8859-10
|
||||
//! - ISO-8859-13
|
||||
//! - ISO-8859-14
|
||||
//! - ISO-8859-15
|
||||
//! - ISO-8859-16
|
||||
//! - CP1250
|
||||
//! - CP1251
|
||||
//! - CP1252
|
||||
//! - CP1253
|
||||
//! - CP1254
|
||||
//! - CP1255
|
||||
//! - CP1256
|
||||
//! - CP1257
|
||||
//! - CP1250
|
||||
//! - CP1251
|
||||
//! - CP1252
|
||||
//! - CP1253
|
||||
//! - CP1254
|
||||
//! - CP1255
|
||||
//! - CP1256
|
||||
//! - CP1257
|
||||
//! - CP1258
|
||||
//! - KOI8-R
|
||||
//! - KOI8_U
|
||||
//! - MACINTOSH
|
||||
//! - IBM850
|
||||
//! - TIS-620
|
||||
//!
|
||||
//!
|
||||
//! Supported character sets via the optional dependency [encoding_rs](https://crates.io/crates/encoding_rs):
|
||||
//!
|
||||
//! - SHIFT_JIS
|
||||
//! - BIG5
|
||||
//! - EUC-JP
|
||||
//! - EUC-KR
|
||||
//! - EUC-JP
|
||||
//! - EUC-KR
|
||||
//! - GB18030
|
||||
//! - GBK
|
||||
//! - ISO-2022-JP
|
||||
//! - ISO-2022-JP
|
||||
//! - WINDOWS-874
|
||||
//! - IBM-866
|
||||
//!
|
||||
//!
|
||||
//! ## Usage Example
|
||||
//!
|
||||
//!
|
||||
//! ```
|
||||
//! 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",
|
||||
@@ -143,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!(
|
||||
@@ -193,7 +192,7 @@
|
||||
//! "Why not both importing AND exporting? ☺"
|
||||
//! );
|
||||
//!
|
||||
//! // HTML and text body parts are returned conforming to RFC8621, Section 4.1.4
|
||||
//! // HTML and text body parts are returned conforming to RFC8621, Section 4.1.4
|
||||
//! assert_eq!(
|
||||
//! message.get_html_body(0).unwrap().to_string(),
|
||||
//! concat!(
|
||||
@@ -256,10 +255,9 @@
|
||||
//! println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||
//! println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||
//!```
|
||||
|
||||
|
||||
mod decoders;
|
||||
mod parsers;
|
||||
#[forbid(unsafe_code)]
|
||||
pub mod decoders;
|
||||
pub mod parsers;
|
||||
|
||||
use std::{borrow::Cow, collections::HashMap, fmt, slice::Iter};
|
||||
|
||||
@@ -272,13 +270,22 @@ use serde::{Deserialize, Serialize};
|
||||
pub struct Message<'x> {
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
header: Box<MessageHeader<'x>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
html_body: Vec<InlinePart<'x>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
text_body: Vec<InlinePart<'x>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
attachments: Vec<MessagePart<'x>>,
|
||||
}
|
||||
@@ -287,7 +294,10 @@ pub struct Message<'x> {
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct TextPart<'x> {
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
header: Option<MimeHeader<'x>>,
|
||||
contents: Cow<'x, str>,
|
||||
@@ -297,7 +307,10 @@ pub struct TextPart<'x> {
|
||||
#[derive(Debug, Default, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct BinaryPart<'x> {
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
header: Option<MimeHeader<'x>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(with = "serde_bytes"))]
|
||||
@@ -315,12 +328,12 @@ pub enum InlinePart<'x> {
|
||||
}
|
||||
|
||||
/// A text, binary or nested e-mail MIME message part.
|
||||
///
|
||||
///
|
||||
/// - Text: Any text/* part
|
||||
/// - Binary: Any other part type that is not text, usually attachments.
|
||||
/// - InlineBinary: Same as the Binary variant but an inline part according to RFC 8621, Section 4.1.4
|
||||
/// - Message: A nested RFC5322 message.
|
||||
///
|
||||
///
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub enum MessagePart<'x> {
|
||||
@@ -343,113 +356,221 @@ pub enum MessagePart<'x> {
|
||||
#[derive(PartialEq, Debug, Default)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct MessageHeader<'x> {
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub bcc: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub cc: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub comments: Option<Vec<Cow<'x, str>>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub date: Option<DateTime>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub from: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub in_reply_to: Option<Vec<Cow<'x, str>>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub keywords: Option<Vec<Cow<'x, str>>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub list_archive: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub list_help: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub list_id: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub list_owner: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub list_post: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub list_subscribe: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub list_unsubscribe: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub message_id: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub mime_version: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub received: Option<Vec<Cow<'x, str>>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub references: Option<Vec<Cow<'x, str>>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub reply_to: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub resent_bcc: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub resent_cc: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub resent_date: Option<Vec<DateTime>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub resent_from: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub resent_message_id: Option<Vec<Cow<'x, str>>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub resent_sender: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub resent_to: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub return_path: Option<Vec<Cow<'x, str>>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub sender: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub subject: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Address::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Address::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub to: Address<'x>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_description: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_disposition: Option<ContentType<'x>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_id: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_transfer_encoding: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_type: Option<ContentType<'x>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(borrow))]
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "HashMap::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "HashMap::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub others: HashMap<&'x str, Vec<Cow<'x, str>>>,
|
||||
}
|
||||
@@ -458,19 +579,34 @@ pub struct MessageHeader<'x> {
|
||||
#[derive(PartialEq, Debug, Default)]
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct MimeHeader<'x> {
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_description: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_disposition: Option<ContentType<'x>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_id: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_transfer_encoding: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub content_type: Option<ContentType<'x>>,
|
||||
}
|
||||
@@ -480,12 +616,18 @@ pub struct MimeHeader<'x> {
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct Addr<'x> {
|
||||
/// The address name including comments
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub name: Option<Cow<'x, str>>,
|
||||
|
||||
/// An e-mail address (RFC5322/RFC2369) or URL (RFC2369)
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub address: Option<Cow<'x, str>>,
|
||||
}
|
||||
@@ -495,12 +637,18 @@ pub struct Addr<'x> {
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct Group<'x> {
|
||||
/// Group name
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub name: Option<Cow<'x, str>>,
|
||||
|
||||
/// Addressess member of the group
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Vec::is_empty"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Vec::is_empty")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
pub addresses: Vec<Addr<'x>>,
|
||||
}
|
||||
@@ -533,10 +681,16 @@ pub enum Address<'x> {
|
||||
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
|
||||
pub struct ContentType<'x> {
|
||||
c_type: Cow<'x, str>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
c_subtype: Option<Cow<'x, str>>,
|
||||
#[cfg_attr(feature = "serde_support", serde(skip_serializing_if = "Option::is_none"))]
|
||||
#[cfg_attr(
|
||||
feature = "serde_support",
|
||||
serde(skip_serializing_if = "Option::is_none")
|
||||
)]
|
||||
#[cfg_attr(feature = "serde_support", serde(default))]
|
||||
attributes: Option<HashMap<Cow<'x, str>, Cow<'x, str>>>,
|
||||
}
|
||||
@@ -784,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);
|
||||
|
||||
/*
|
||||
|
||||
+22
-21
@@ -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,10 +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) {
|
||||
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;
|
||||
}
|
||||
@@ -132,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 {
|
||||
@@ -157,7 +158,7 @@ pub fn parse_date(stream: &MessageStream, abort_on_invalid: bool) -> Option<Date
|
||||
parts[2]
|
||||
},
|
||||
month: if month_pos == 3 && month_hash <= 30 {
|
||||
(unsafe { *MONTH_MAP.get_unchecked(month_hash) }) as u32
|
||||
MONTH_MAP[month_hash] as u32
|
||||
} else {
|
||||
parts[1]
|
||||
},
|
||||
@@ -188,13 +189,13 @@ static MONTH_HASH: &[u8] = &[
|
||||
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
|
||||
];
|
||||
|
||||
pub static MONTH_MAP: &[u8] = &[
|
||||
pub static MONTH_MAP: &[u8; 31] = &[
|
||||
5, 0, 0, 0, 10, 3, 0, 0, 0, 7, 1, 0, 0, 0, 12, 6, 0, 0, 0, 8, 4, 0, 0, 0, 2, 9, 0, 0, 0, 0, 11,
|
||||
];
|
||||
|
||||
#[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() {
|
||||
@@ -259,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
|
||||
|
||||
+30
-29
@@ -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> {
|
||||
@@ -45,14 +48,17 @@ impl<'x> MimeHeader<'x> {
|
||||
}
|
||||
}
|
||||
|
||||
enum HeaderParserResult<'x> {
|
||||
Supported(fn(&mut dyn MessageField<'x>, &MessageStream<'x>)),
|
||||
pub enum HeaderParserResult<'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,34 +69,30 @@ pub fn parse_headers<'x>(header: &mut dyn MessageField<'x>, stream: &MessageStre
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
|
||||
if field.eq_ignore_ascii_case(unsafe {
|
||||
HDR_NAMES.get_unchecked(token_hash)
|
||||
}) {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,29 +100,28 @@ fn parse_header_name<'x>(stream: &MessageStream<'x>) -> HeaderParserResult<'x> {
|
||||
}
|
||||
}
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -129,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;
|
||||
@@ -159,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 {
|
||||
@@ -196,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>)] = &[
|
||||
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,
|
||||
@@ -257,7 +258,7 @@ static HDR_FNCS: &[for<'x, 'y> fn(&mut dyn MessageField<'x>, &MessageStream<'x>)
|
||||
super::fields::parse_resent_from,
|
||||
];
|
||||
|
||||
static HDR_NAMES: &[&[u8]] = &[
|
||||
static HDR_NAMES: &[&[u8]; 58] = &[
|
||||
b"date",
|
||||
b"",
|
||||
b"sender",
|
||||
|
||||
+121
-94
@@ -13,16 +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,
|
||||
},
|
||||
MessagePart, BinaryPart, InlinePart, ContentType, Message, MessageHeader, MimeHeader,
|
||||
TextPart,
|
||||
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 {
|
||||
@@ -38,24 +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 {
|
||||
(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> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a byte slice containing the RFC5322 raw message and returns a
|
||||
/// 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.
|
||||
///
|
||||
/// 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);
|
||||
/// To make sure parsing was successfull use `is_empty()`.
|
||||
///
|
||||
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 {
|
||||
@@ -388,9 +417,7 @@ impl<'x> Message<'x> {
|
||||
if let (Some(mut prev_message), Some(mut prev_state)) =
|
||||
(message_stack.pop(), state_stack.pop())
|
||||
{
|
||||
prev_message
|
||||
.attachments
|
||||
.push(MessagePart::Message(message));
|
||||
prev_message.attachments.push(MessagePart::Message(message));
|
||||
message = prev_message;
|
||||
prev_state.mime_boundary = state.mime_boundary;
|
||||
state = prev_state;
|
||||
@@ -400,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
|
||||
@@ -448,27 +475,25 @@ 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;
|
||||
}
|
||||
}
|
||||
|
||||
while let Some(mut prev_message) = message_stack.pop() {
|
||||
prev_message
|
||||
.attachments
|
||||
.push(MessagePart::Message(message));
|
||||
prev_message.attachments.push(MessagePart::Message(message));
|
||||
message = prev_message;
|
||||
}
|
||||
|
||||
@@ -516,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,254 +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> {
|
||||
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]> {
|
||||
unsafe { (*self.data.get()).get(from..to) }
|
||||
}
|
||||
|
||||
pub fn get_string(&self, from: usize, to: usize, utf8_valid: bool) -> Option<Cow<'x, str>> {
|
||||
unsafe {
|
||||
if utf8_valid {
|
||||
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 {
|
||||
unsafe { *self.pos.get() >= (*self.data.get()).len() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn set_pos(&self, pos: usize) {
|
||||
unsafe {
|
||||
*self.pos.get() = pos;
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn get_pos(&self) -> usize {
|
||||
unsafe { *self.pos.get() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn next(&self) -> Option<&'x u8> {
|
||||
unsafe {
|
||||
let pos = &mut *self.pos.get();
|
||||
let data = &mut *self.data.get();
|
||||
|
||||
if *pos < data.len() {
|
||||
let result = data.get_unchecked(*pos);
|
||||
*pos += 1;
|
||||
Some(result)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn peek(&self) -> Option<&'x u8> {
|
||||
unsafe { (*self.data.get()).get(*self.pos.get()) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn advance(&self, pos: usize) {
|
||||
unsafe {
|
||||
*self.pos.get() += pos;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn seek_next_part(&self, boundary: &[u8]) -> bool {
|
||||
if !boundary.is_empty() {
|
||||
unsafe {
|
||||
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;
|
||||
|
||||
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 {
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
|
||||
if ch == boundary.get_unchecked(match_count) {
|
||||
match_count += 1;
|
||||
if match_count == boundary.len() {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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 {
|
||||
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) {
|
||||
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(),
|
||||
@@ -143,5 +142,4 @@ fn test_api() {
|
||||
|
||||
//println!("{}", serde_json::to_string_pretty(&message).unwrap());
|
||||
//println!("{}", serde_yaml::to_string(&message).unwrap());
|
||||
|
||||
}
|
||||
|
||||
+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