crabmail

Static HTML email archive viewer in Rust
git clone git://git.alexwennerberg.com/crabmail
Log | Files | Refs | README | LICENSE

commit 30f91a66dbf5bc4207743c44ddfb740d43c1a29d
parent 42679585662e27be931dc1a892368aa7e1bef4fc
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sun, 16 May 2021 16:05:15 -0700

Add (WIP) html escaping

Diffstat:
Msrc/main.rs | 2++
Asrc/utils.rs | 44++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -4,6 +4,8 @@ use std::fs::{File, OpenOptions}; use std::io::prelude::*; use std::path::Path; +mod utils; + const HELP: &str = "\ Usage: crabmail diff --git a/src/utils.rs b/src/utils.rs @@ -0,0 +1,44 @@ +use std::io; +use std::io::Write; +// Derived from https://github.com/raphlinus/pulldown-cmark/blob/master/src/escape.rs +// Added ' escape +// !!!WIP!!! -- still need to add tests, audit security, etc + +const fn create_html_escape_table() -> [u8; 256] { + let mut table = [0; 256]; + table[b'"' as usize] = 1; + table[b'&' as usize] = 2; + table[b'<' as usize] = 3; + table[b'>' as usize] = 4; + table[b'\'' as usize] = 5; + table +} + +static HTML_ESCAPE_TABLE: [u8; 256] = create_html_escape_table(); + +static HTML_ESCAPES: [&str; 6] = ["", "&quot;", "&amp;", "&lt;", "&gt;", "&#39"]; + +fn escape_html(mut w: String, s: &str) { + let bytes = s.as_bytes(); + let mut mark = 0; + let mut i = 0; + while i < s.len() { + match bytes[i..] + .iter() + .position(|&c| HTML_ESCAPE_TABLE[c as usize] != 0) + { + Some(pos) => { + i += pos; + } + None => break, + } + let c = bytes[i]; + let escape = HTML_ESCAPE_TABLE[c as usize]; + let escape_seq = HTML_ESCAPES[escape as usize]; + w.push_str(&s[mark..i]); + w.push_str(escape_seq); + i += 1; + mark = i; // all escaped characters are ASCII + } + w.push_str(&s[mark..]) +}