Skip to content

Commit

Permalink
Merge pull request #129 from /pull/97
Browse files Browse the repository at this point in the history
Pull/97
  • Loading branch information
marshallpierce committed Mar 8, 2020
2 parents 5d3970e + a6d49bc commit bfa7b90
Show file tree
Hide file tree
Showing 6 changed files with 371 additions and 1 deletion.
16 changes: 15 additions & 1 deletion examples/make_tables.rs
Expand Up @@ -26,7 +26,7 @@ fn main() {
.chain(0x30..0x3A)
// -
.chain(0x2D..0x2E)
// _s
// _
.chain(0x5F..0x60)
.collect();
print_encode_table(&url_alphabet, "URL_SAFE_ENCODE", 0);
Expand All @@ -41,6 +41,20 @@ fn main() {
.collect();
print_encode_table(&crypt_alphabet, "CRYPT_ENCODE", 0);
print_decode_table(&crypt_alphabet, "CRYPT_DECODE", 0);

// A-Z
let imap_alphabet: Vec<u8> = (0x41..0x5B)
// a-z
.chain(0x61..0x7B)
// 0-9
.chain(0x30..0x3A)
// +
.chain(0x2B..0x2C)
// ,
.chain(0x2C..0x2D)
.collect();
print_encode_table(&imap_alphabet, "IMAP_MUTF7_ENCODE", 0);
print_decode_table(&imap_alphabet, "IMAP_MUTF7_DECODE", 0);
}

fn print_encode_table(alphabet: &[u8], const_name: &str, indent_depth: usize) {
Expand Down
8 changes: 8 additions & 0 deletions src/decode.rs
Expand Up @@ -856,4 +856,12 @@ mod tests {
}
}
}

#[test]
fn decode_imap() {
assert_eq!(
decode_config(b"+,,+", crate::IMAP_MUTF7),
decode_config(b"+//+", crate::STANDARD_NO_PAD)
);
}
}
8 changes: 8 additions & 0 deletions src/encode.rs
Expand Up @@ -664,4 +664,12 @@ mod tests {

assert_eq!(encoded_len, encoded.len());
}

#[test]
fn encode_imap() {
assert_eq!(
encode_config(b"\xFB\xFF", crate::IMAP_MUTF7),
encode_config(b"\xFB\xFF", crate::STANDARD_NO_PAD).replace("/", ",")
);
}
}
13 changes: 13 additions & 0 deletions src/lib.rs
Expand Up @@ -119,6 +119,10 @@ pub enum CharacterSet {
///
/// Not standardized, but folk wisdom on the net asserts that this alphabet is what crypt uses.
Crypt,
/// The character set used in IMAP-modified UTF-7 (uses `+` and `,`).
///
/// See [RFC 3501](https://tools.ietf.org/html/rfc3501#section-5.1.3)
ImapMutf7,
}

impl CharacterSet {
Expand All @@ -127,6 +131,7 @@ impl CharacterSet {
CharacterSet::Standard => tables::STANDARD_ENCODE,
CharacterSet::UrlSafe => tables::URL_SAFE_ENCODE,
CharacterSet::Crypt => tables::CRYPT_ENCODE,
CharacterSet::ImapMutf7 => tables::IMAP_MUTF7_ENCODE,
}
}

Expand All @@ -135,6 +140,7 @@ impl CharacterSet {
CharacterSet::Standard => tables::STANDARD_DECODE,
CharacterSet::UrlSafe => tables::URL_SAFE_DECODE,
CharacterSet::Crypt => tables::CRYPT_DECODE,
CharacterSet::ImapMutf7 => tables::IMAP_MUTF7_DECODE,
}
}
}
Expand Down Expand Up @@ -211,3 +217,10 @@ pub const CRYPT: Config = Config {
pad: false,
decode_allow_trailing_bits: false,
};

/// IMAP modified UTF-7 requirements
pub const IMAP_MUTF7: Config = Config {
char_set: CharacterSet::ImapMutf7,
pad: false,
decode_allow_trailing_bits: false,
};

0 comments on commit bfa7b90

Please sign in to comment.