diff options
Diffstat (limited to 'internals/src/bind/quoted_printable.rs')
-rw-r--r-- | internals/src/bind/quoted_printable.rs | 143 |
1 files changed, 62 insertions, 81 deletions
diff --git a/internals/src/bind/quoted_printable.rs b/internals/src/bind/quoted_printable.rs index 7ef6291..7540419 100644 --- a/internals/src/bind/quoted_printable.rs +++ b/internals/src/bind/quoted_printable.rs @@ -1,9 +1,9 @@ -use soft_ascii_string::{ SoftAsciiChar, SoftAsciiString }; -use { quoted_printable as extern_quoted_printable }; +use quoted_printable as extern_quoted_printable; +use soft_ascii_string::{SoftAsciiChar, SoftAsciiString}; -use failure::Fail; -use ::error::{EncodingError, EncodingErrorKind}; use super::encoded_word::EncodedWordWriter; +use error::{EncodingError, EncodingErrorKind}; +use failure::Fail; /// a quoted printable encoding suitable for content transfer encoding, /// but _not_ suited for the encoding in encoded words @@ -14,24 +14,18 @@ pub fn normal_encode<A: AsRef<[u8]>>(data: A) -> SoftAsciiString { /// a quoted printable decoding suitable for content transfer encoding #[inline] -pub fn normal_decode<R: AsRef<[u8]>>(input: R) - -> Result<Vec<u8>, EncodingError> -{ +pub fn normal_decode<R: AsRef<[u8]>>(input: R) -> Result<Vec<u8>, EncodingError> { //extern_quoted_printable h - extern_quoted_printable::decode( - input.as_ref(), extern_quoted_printable::ParseMode::Strict - ).map_err(|err| err - .context(EncodingErrorKind::Malformed) - .into() - ) + extern_quoted_printable::decode(input.as_ref(), extern_quoted_printable::ParseMode::Strict) + .map_err(|err| err.context(EncodingErrorKind::Malformed).into()) } /// a quoted printable decoding suitable for decoding a quoted printable /// encpded text in encoded words #[inline(always)] -pub fn encoded_word_decode<R: AsRef<[u8]>>( input: R ) -> Result<Vec<u8>, EncodingError> { +pub fn encoded_word_decode<R: AsRef<[u8]>>(input: R) -> Result<Vec<u8>, EncodingError> { //we can just use the stadard decoding - normal_decode( input ) + normal_decode(input) } //FIXME we don't use EncodedWord context here, @@ -39,13 +33,14 @@ pub fn encoded_word_decode<R: AsRef<[u8]>>( input: R ) -> Result<Vec<u8>, Encodi // making it compatilble with all context, but not nessesary // the best solution... /// Simple wrapper around ecoded_word_encode for utf8 strings only -pub fn encoded_word_encode_utf8<'a, O>(word: &str, writer: &mut O ) - where O: EncodedWordWriter +pub fn encoded_word_encode_utf8<'a, O>(word: &str, writer: &mut O) +where + O: EncodedWordWriter, { - let iter = word.char_indices().map( |(idx, ch)| { - &word.as_bytes()[idx..idx+ch.len_utf8()] - }); - encoded_word_encode(iter, writer ); + let iter = word + .char_indices() + .map(|(idx, ch)| &word.as_bytes()[idx..idx + ch.len_utf8()]); + encoded_word_encode(iter, writer); } /// @@ -100,8 +95,10 @@ pub fn encoded_word_encode_utf8<'a, O>(word: &str, writer: &mut O ) /// might be encoded with completely different bytes, but when the RFC speaks of /// '\r','\n' it normally means the bytes 10/13 independent of the character set, /// or if they appear in a image, zip-archiev etc. ) -pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) - where I: Iterator<Item=&'a [u8]>, O: EncodedWordWriter +pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O) +where + I: Iterator<Item = &'a [u8]>, + O: EncodedWordWriter, { out.write_ecw_start(); let max_payload_len = out.max_payload_len(); @@ -118,19 +115,22 @@ pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) // this is the way to go as long as we don't want to behave differently for // different context, the COMMENT context allows more chars, and the // TEXT context even more - b'!' | b'*' | - b'+' | b'-' | - b'/' | b'_' | - b'0'...b'9' | - b'A'...b'Z' | - b'a'...b'z' => { + b'!' + | b'*' + | b'+' + | b'-' + | b'/' + | b'_' + | b'0'...b'9' + | b'A'...b'Z' + | b'a'...b'z' => { buf[buf_idx] = SoftAsciiChar::from_unchecked(byte as char); buf_idx += 1; - }, + } _otherwise => { buf[buf_idx] = SoftAsciiChar::from_unchecked('='); - buf[buf_idx+1] = lower_nibble_to_hex( byte >> 4 ); - buf[buf_idx+2] = lower_nibble_to_hex( byte ); + buf[buf_idx + 1] = lower_nibble_to_hex(byte >> 4); + buf[buf_idx + 2] = lower_nibble_to_hex(byte); buf_idx += 3; } } @@ -140,10 +140,13 @@ pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) remaining = max_payload_len; } if buf_idx > remaining { - panic!( "single character longer then max length ({:?}) of encoded word", remaining ); + panic!( + "single character longer then max length ({:?}) of encoded word", + remaining + ); } for idx in 0..buf_idx { - out.write_char( buf[idx] ) + out.write_char(buf[idx]) } remaining -= buf_idx; } @@ -151,39 +154,33 @@ pub fn encoded_word_encode<'a, I, O>(input: I, out: &mut O ) } #[inline] -fn lower_nibble_to_hex( half_byte: u8 ) -> SoftAsciiChar { +fn lower_nibble_to_hex(half_byte: u8) -> SoftAsciiChar { static CHARS: &[char] = &[ - '0', '1', '2', '3', '4', '5', - '6', '7', '8', '9', 'A', 'B', - 'C', 'D', 'E', 'F' + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', ]; - SoftAsciiChar::from_unchecked(CHARS[ (half_byte & 0x0F) as usize ]) + SoftAsciiChar::from_unchecked(CHARS[(half_byte & 0x0F) as usize]) } - - - #[cfg(test)] mod test { - use soft_ascii_string::SoftAsciiStr; - use ::bind::encoded_word::EncodedWordEncoding; use super::super::encoded_word::VecWriter; use super::*; + use bind::encoded_word::EncodedWordEncoding; + use soft_ascii_string::SoftAsciiStr; #[test] fn to_hex() { let data = &[ ('0', 0b11110000), - ('0', 0b0 ), + ('0', 0b0), ('7', 0b0111), ('7', 0b10111), - ('F', 0b1111) + ('F', 0b1111), ]; for &(ch, byte) in data { - assert_eq!( lower_nibble_to_hex( byte), ch ); + assert_eq!(lower_nibble_to_hex(byte), ch); } - } macro_rules! test_ecw_encode { @@ -241,7 +238,7 @@ mod test { ] } - test_ecw_encode!{ encode_ascii, + test_ecw_encode! { encode_ascii, data "abcdefghijklmnopqrstuvwxyz \t?=0123456789!@#$%^&*()_+-" => [ "=?utf8?Q?abcdefghijklmnopqrstuvwxyz=20=09=3F=3D0123456789!=40=23=24=25=5E?=", "=?utf8?Q?=26*=28=29_+-?=" @@ -254,7 +251,6 @@ mod test { ] } - test_ecw_encode! { split_into_multiple_ecws, data "0123456789012345678901234567890123456789012345678901234567891234newline" => [ "=?utf8?Q?0123456789012345678901234567890123456789012345678901234567891234?=", @@ -262,7 +258,7 @@ mod test { ] } - test_ecw_encode!{ bigger_chunks, + test_ecw_encode! { bigger_chunks, data "ランダムテキスト ראַנדאָם טעקסט" => [ //ランダムテキス "=?utf8?Q?=E3=83=A9=E3=83=B3=E3=83=80=E3=83=A0=E3=83=86=E3=82=AD=E3=82=B9?=", @@ -279,44 +275,29 @@ mod test { ("=28=29=22", "()\""), ( "=7B=7D=7E=40=23=24=25=5E=26*=28=29=3D=7C=5C=5B=5D=27=3B=3A=2E", - "{}~@#$%^&*()=|\\[]';:." - ), - ( - "=3F=3D=20=09=0D=0A", - "?= \t\r\n" - ), - ( - "=26*=28=29_+-", - "&*()_+-" + "{}~@#$%^&*()=|\\[]';:.", ), + ("=3F=3D=20=09=0D=0A", "?= \t\r\n"), + ("=26*=28=29_+-", "&*()_+-"), ( "abcdefghijklmnopqrstuvwxyz=20=09=3F=3D0123456789!=40=23=24=25=5E", - "abcdefghijklmnopqrstuvwxyz \t?=0123456789!@#$%^" - ), - ( - "=0D=0A", - "\r\n" + "abcdefghijklmnopqrstuvwxyz \t?=0123456789!@#$%^", ), + ("=0D=0A", "\r\n"), ( "=E3=83=A9=E3=83=B3=E3=83=80=E3=83=A0=E3=83=86=E3=82=AD=E3=82=B9", - "ランダムテキス" + "ランダムテキス", ), ( "=E3=83=88=20=D7=A8=D7=90=D6=B7=D7=A0=D7=93=D7=90=D6=B8=D7=9D=20", - "ト ראַנדאָם " + "ト ראַנדאָם ", ), - ( - "=D7=98=D7=A2=D7=A7=D7=A1=D7=98", - "טעקסט" - ) + ("=D7=98=D7=A2=D7=A7=D7=A1=D7=98", "טעקסט"), ]; for &(inp, outp) in pairs.iter() { let dec = assert_ok!(encoded_word_decode(inp)); let dec = String::from_utf8(dec).unwrap(); - assert_eq!( - outp.as_bytes(), - dec.as_bytes() - ); + assert_eq!(outp.as_bytes(), dec.as_bytes()); } } @@ -342,11 +323,11 @@ mod test { #[test] fn normal_decode_text() { let text = concat!( - "This is a llllllllllllllllllllllllllllllllllllll00000000000000000000ng test=\r\n", - " 0123456789qwertyuio\r\n", - "With many lines\r\n", - "And utf=E2=86=92=E2=86=92=E2=86=92=E2=86=928" - ); + "This is a llllllllllllllllllllllllllllllllllllll00000000000000000000ng test=\r\n", + " 0123456789qwertyuio\r\n", + "With many lines\r\n", + "And utf=E2=86=92=E2=86=92=E2=86=92=E2=86=928" + ); let encoded = String::from_utf8(normal_decode(text).unwrap()).unwrap(); assert_eq!( concat!( @@ -357,4 +338,4 @@ mod test { encoded.as_str() ); } -}
\ No newline at end of file +} |