From 59549dddcb5b079e1cb39fb6236af4eced972359 Mon Sep 17 00:00:00 2001 From: Bill Date: Tue, 9 Apr 2024 05:12:25 -0700 Subject: [PATCH] Add BASE64_MIME_PERMISSIVE to ignore trailing bits (#103) Fixes #102 --------- Co-authored-by: Julien Cretin --- bin/Cargo.toml | 4 +-- lib/CHANGELOG.md | 6 +++++ lib/Cargo.toml | 2 +- lib/README.md | 35 ++++++++++++------------ lib/macro/Cargo.toml | 6 ++--- lib/macro/internal/Cargo.toml | 4 +-- lib/macro/src/lib.rs | 8 ++++++ lib/src/lib.rs | 50 ++++++++++++++++++++++++++++++++++- lib/tests/lib.rs | 1 + www/Cargo.toml | 2 +- www/src/lib.rs | 1 + 11 files changed, 92 insertions(+), 27 deletions(-) diff --git a/bin/Cargo.toml b/bin/Cargo.toml index 484d010..2ead6e0 100644 --- a/bin/Cargo.toml +++ b/bin/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "data-encoding-bin" -version = "0.3.3" +version = "0.3.4-git" authors = ["Julien Cretin "] license = "MIT" edition = "2021" @@ -16,5 +16,5 @@ name = "data-encoding" path = "src/main.rs" [dependencies] -data-encoding = { version = "2.5.0", path = "../lib" } +data-encoding = { version = "2.6.0-git", path = "../lib" } getopts = "0.2" diff --git a/lib/CHANGELOG.md b/lib/CHANGELOG.md index 6b2b3a9..f668d72 100644 --- a/lib/CHANGELOG.md +++ b/lib/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2.6.0-git + +### Minor + +- Add `BASE64_MIME_PERMISSIVE` constant to ignore trailing bits (fixes #102) + ## 2.5.0 ### Minor diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 19ca78b..531c7c7 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "data-encoding" -version = "2.5.0" +version = "2.6.0-git" authors = ["Julien Cretin "] license = "MIT" edition = "2018" diff --git a/lib/README.md b/lib/README.md index 41e5d86..f2d892e 100644 --- a/lib/README.md +++ b/lib/README.md @@ -3,23 +3,24 @@ This library provides the following common encodings: -| Name | Description | -| --- | --- | -| `HEXLOWER` | lowercase hexadecimal | -| `HEXLOWER_PERMISSIVE` | lowercase hexadecimal (case-insensitive decoding) | -| `HEXUPPER` | uppercase hexadecimal | -| `HEXUPPER_PERMISSIVE` | uppercase hexadecimal (case-insensitive decoding) | -| `BASE32` | RFC4648 base32 | -| `BASE32_NOPAD` | RFC4648 base32 (no padding) | -| `BASE32_DNSSEC` | RFC5155 base32 | -| `BASE32_DNSCURVE` | DNSCurve base32 | -| `BASE32HEX` | RFC4648 base32hex | -| `BASE32HEX_NOPAD` | RFC4648 base32hex (no padding) | -| `BASE64` | RFC4648 base64 | -| `BASE64_NOPAD` | RFC4648 base64 (no padding) | -| `BASE64_MIME` | RFC2045-like base64 | -| `BASE64URL` | RFC4648 base64url | -| `BASE64URL_NOPAD` | RFC4648 base64url (no padding) | +| Name | Description | +|--------------------------|---------------------------------------------------| +| `HEXLOWER` | lowercase hexadecimal | +| `HEXLOWER_PERMISSIVE` | lowercase hexadecimal (case-insensitive decoding) | +| `HEXUPPER` | uppercase hexadecimal | +| `HEXUPPER_PERMISSIVE` | uppercase hexadecimal (case-insensitive decoding) | +| `BASE32` | RFC4648 base32 | +| `BASE32_NOPAD` | RFC4648 base32 (no padding) | +| `BASE32_DNSSEC` | RFC5155 base32 | +| `BASE32_DNSCURVE` | DNSCurve base32 | +| `BASE32HEX` | RFC4648 base32hex | +| `BASE32HEX_NOPAD` | RFC4648 base32hex (no padding) | +| `BASE64` | RFC4648 base64 | +| `BASE64_NOPAD` | RFC4648 base64 (no padding) | +| `BASE64_MIME` | RFC2045-like base64 | +| `BASE64_MIME_PERMISSIVE` | RFC2045-like base64 (ignoring trailing bits) | +| `BASE64URL` | RFC4648 base64url | +| `BASE64URL_NOPAD` | RFC4648 base64url (no padding) | It also provides the possibility to define custom little-endian ASCII base-conversion encodings for bases of size 2, 4, 8, 16, 32, and 64 (for which diff --git a/lib/macro/Cargo.toml b/lib/macro/Cargo.toml index 87f92d0..a085616 100644 --- a/lib/macro/Cargo.toml +++ b/lib/macro/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "data-encoding-macro" -version = "0.1.14" +version = "0.1.15-git" authors = ["Julien Cretin "] license = "MIT" edition = "2018" @@ -14,5 +14,5 @@ description = "Macros for data-encoding" include = ["Cargo.toml", "LICENSE", "README.md", "src/lib.rs"] [dependencies] -data-encoding = { version = "2.5.0", path = "..", default-features = false } -data-encoding-macro-internal = { version = "0.1.12", path = "internal" } +data-encoding = { version = "2.6.0-git", path = "..", default-features = false } +data-encoding-macro-internal = { version = "0.1.13-git", path = "internal" } diff --git a/lib/macro/internal/Cargo.toml b/lib/macro/internal/Cargo.toml index 7246d7e..5594425 100644 --- a/lib/macro/internal/Cargo.toml +++ b/lib/macro/internal/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "data-encoding-macro-internal" -version = "0.1.12" +version = "0.1.13-git" authors = ["Julien Cretin "] license = "MIT" edition = "2018" @@ -14,7 +14,7 @@ include = ["Cargo.toml", "LICENSE", "README.md", "src/lib.rs"] proc-macro = true [dependencies.data-encoding] -version = "2.5.0" +version = "2.6.0-git" path = "../.." default-features = false features = ["alloc"] diff --git a/lib/macro/src/lib.rs b/lib/macro/src/lib.rs index a0d8087..a9fc654 100644 --- a/lib/macro/src/lib.rs +++ b/lib/macro/src/lib.rs @@ -246,6 +246,14 @@ make! { wrap_width: 76, wrap_separator: "\r\n", } +make! { + base64_mime_permissive base64_mime_permissive_array = BASE64_MIME_PERMISSIVE; + symbols: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", + padding: '=', + wrap_width: 76, + wrap_separator: "\r\n", + check_trailing_bits: false, +} make! { base64url base64url_array = BASE64URL; symbols: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 22743ee..ed8b194 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -2387,7 +2387,7 @@ const BASE64_NOPAD_IMPL: &[u8] = &[ /// This encoding is a static version of: /// /// ```rust -/// # use data_encoding::{Specification, Wrap, BASE64_MIME}; +/// # use data_encoding::{Specification, BASE64_MIME}; /// let mut spec = Specification::new(); /// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); /// spec.padding = Some('='); @@ -2429,6 +2429,54 @@ const BASE64_MIME_IMPL: &[u8] = &[ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 30, 76, 13, 10, ]; +/// MIME base64 encoding without trailing bits check +/// +/// This encoding is a static version of: +/// +/// ```rust +/// # use data_encoding::{Specification, BASE64_MIME_PERMISSIVE}; +/// let mut spec = Specification::new(); +/// spec.symbols.push_str("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"); +/// spec.padding = Some('='); +/// spec.wrap.width = 76; +/// spec.wrap.separator.push_str("\r\n"); +/// spec.check_trailing_bits = false; +/// assert_eq!(BASE64_MIME_PERMISSIVE, spec.encoding().unwrap()); +/// ``` +/// +/// It does not exactly conform to [RFC2045] because it does not print the header +/// and does not ignore all characters. +/// +/// [RFC2045]: https://tools.ietf.org/html/rfc2045 +pub const BASE64_MIME_PERMISSIVE: Encoding = Encoding::internal_new(BASE64_MIME_PERMISSIVE_IMPL); +const BASE64_MIME_PERMISSIVE_IMPL: &[u8] = &[ + 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, + 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, + 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, + 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, + 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, + 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, + 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, + 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, + 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 65, 66, 67, 68, + 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, + 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, + 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 129, 128, 128, 129, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 62, 128, 128, 128, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 130, 128, + 128, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 128, 128, 128, 128, 128, 128, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, + 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 61, 14, 76, 13, 10, +]; + /// Padded base64url encoding /// /// This encoding is a static version of: diff --git a/lib/tests/lib.rs b/lib/tests/lib.rs index 91f86ba..b3a2ada 100644 --- a/lib/tests/lib.rs +++ b/lib/tests/lib.rs @@ -412,6 +412,7 @@ fn round_trip() { test(data_encoding::BASE64); test(data_encoding::BASE64_NOPAD); test(data_encoding::BASE64_MIME); + test(data_encoding::BASE64_MIME_PERMISSIVE); test(data_encoding::BASE64URL); test(data_encoding::BASE64URL_NOPAD); } diff --git a/www/Cargo.toml b/www/Cargo.toml index 708500d..bca5756 100644 --- a/www/Cargo.toml +++ b/www/Cargo.toml @@ -12,7 +12,7 @@ publish = false crate-type = ["cdylib"] [dependencies] -data-encoding = "2" +data-encoding = { path = "../lib" } lazy_static = "1" wasm-bindgen = "0.2" diff --git a/www/src/lib.rs b/www/src/lib.rs index 73345f2..0328ca7 100644 --- a/www/src/lib.rs +++ b/www/src/lib.rs @@ -63,6 +63,7 @@ lazy_static! { add!(BASE64URL); add!(BASE64URL_NOPAD); add!(BASE64_MIME); + add!(BASE64_MIME_PERMISSIVE); add!(BASE64_NOPAD); add!(HEXLOWER); add!(HEXLOWER_PERMISSIVE);