Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull/97 #129

Merged
merged 6 commits into from Mar 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
};