Skip to content

Commit

Permalink
Merge fd2ed2d into 69ec23e
Browse files Browse the repository at this point in the history
  • Loading branch information
ia0 committed Nov 16, 2017
2 parents 69ec23e + fd2ed2d commit 8b3f60c
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 81 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,18 @@ The typical definition of a custom encoding looks like:

```rust
lazy_static! {
static ref DNSCURVE: data_encoding::Encoding = {
use data_encoding::{Specification, BitOrder};
static ref HEX: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
spec.bit_order = BitOrder::LeastSignificantFirst;
spec.symbols.push_str("0123456789abcdef");
spec.translate.from.push_str("ABCDEF");
spec.translate.to.push_str("abcdef");
spec.encoding().unwrap()
};
static ref BASE64: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
spec.padding = Some('=');
spec.encoding().unwrap()
};
}
Expand Down
1 change: 1 addition & 0 deletions cmp/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ lazy_static! {
fn lazy_static_hex() {
assert_eq!(HEX.encode(b"Hello"), "48656c6c6f");
assert_eq!(HEX.decode(b"48656c6c6f").unwrap(), b"Hello");
assert_eq!(*HEX, data_encoding::HEXLOWER_PERMISSIVE);
}
1 change: 1 addition & 0 deletions lib/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Minor

- Add `BASE32_DNSCURVE`
- Add `BASE32HEX_NOPAD` and `BASE32_DNSSEC`

### Patch
Expand Down
17 changes: 11 additions & 6 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ The typical definition of a custom encoding looks like:

```rust
lazy_static! {
static ref DNSCURVE: data_encoding::Encoding = {
use data_encoding::{Specification, BitOrder};
static ref HEX: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
spec.bit_order = BitOrder::LeastSignificantFirst;
spec.symbols.push_str("0123456789abcdef");
spec.translate.from.push_str("ABCDEF");
spec.translate.to.push_str("abcdef");
spec.encoding().unwrap()
};
static ref BASE64: Encoding = {
let mut spec = Specification::new();
spec.symbols.push_str(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
spec.padding = Some('=');
spec.encoding().unwrap()
};
}
Expand Down
45 changes: 15 additions & 30 deletions lib/benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
extern crate test;
extern crate data_encoding;

use data_encoding::{HEXLOWER, BASE32, BASE64, BASE64_NOPAD};
use data_encoding::{BASE32, BASE32_DNSCURVE, BASE64, BASE64_NOPAD, HEXLOWER,
Specification};
use test::Bencher;

#[bench]
fn base02_encode_base(b: &mut Bencher) {
let input = &[0u8; 4096];
let output = &mut [0u8; 32768];
let mut spec = data_encoding::Specification::new();
let mut spec = Specification::new();
spec.symbols.push_str("01");
let base = spec.encoding().unwrap();
b.iter(|| base.encode_mut(input, output));
Expand All @@ -20,7 +21,7 @@ fn base02_encode_base(b: &mut Bencher) {
fn base02_decode_base(b: &mut Bencher) {
let input = &[b'0'; 4096];
let output = &mut [0u8; 512];
let mut spec = data_encoding::Specification::new();
let mut spec = Specification::new();
spec.symbols.push_str("01");
let base = spec.encoding().unwrap();
b.iter(|| base.decode_mut(input, output));
Expand All @@ -30,7 +31,7 @@ fn base02_decode_base(b: &mut Bencher) {
fn base04_encode_base(b: &mut Bencher) {
let input = &[0u8; 4096];
let output = &mut [0u8; 16384];
let mut spec = data_encoding::Specification::new();
let mut spec = Specification::new();
spec.symbols.push_str("0123");
let base = spec.encoding().unwrap();
b.iter(|| base.encode_mut(input, output));
Expand All @@ -40,7 +41,7 @@ fn base04_encode_base(b: &mut Bencher) {
fn base04_decode_base(b: &mut Bencher) {
let input = &[b'0'; 4096];
let output = &mut [0u8; 1024];
let mut spec = data_encoding::Specification::new();
let mut spec = Specification::new();
spec.symbols.push_str("0123");
let base = spec.encoding().unwrap();
b.iter(|| base.decode_mut(input, output));
Expand All @@ -50,7 +51,7 @@ fn base04_decode_base(b: &mut Bencher) {
fn base08_encode_base(b: &mut Bencher) {
let input = &[0u8; 4096];
let output = &mut [0u8; 10923];
let mut spec = data_encoding::Specification::new();
let mut spec = Specification::new();
spec.symbols.push_str("01234567");
let base = spec.encoding().unwrap();
b.iter(|| base.encode_mut(input, output));
Expand All @@ -60,7 +61,7 @@ fn base08_encode_base(b: &mut Bencher) {
fn base08_decode_base(b: &mut Bencher) {
let input = &[b'0'; 4096];
let output = &mut [0u8; 1536];
let mut spec = data_encoding::Specification::new();
let mut spec = Specification::new();
spec.symbols.push_str("01234567");
let base = spec.encoding().unwrap();
b.iter(|| base.decode_mut(input, output));
Expand Down Expand Up @@ -121,7 +122,9 @@ fn base64_decode_pad(b: &mut Bencher) {
for i in 0 .. 20 {
let x = 4096 * i / 20 / 4 * 4;
input[x + 3] = b'=';
if i % 2 == 0 { input[x + 2] = b'='; }
if i % 2 == 0 {
input[x + 2] = b'=';
}
}
let output = &mut [0u8; 3072];
b.iter(|| BASE64.decode_mut(input, output).unwrap());
Expand All @@ -131,7 +134,7 @@ fn base64_decode_pad(b: &mut Bencher) {
fn base64_encode_wrap(b: &mut Bencher) {
let input = &[0u8; 4096];
let output = &mut [0u8; 5608];
let mut spec = data_encoding::BASE64.specification();
let mut spec = BASE64.specification();
spec.wrap.width = 76;
spec.wrap.separator.push_str("\r\n");
let base64 = spec.encoding().unwrap();
Expand All @@ -146,7 +149,7 @@ fn base64_decode_wrap(b: &mut Bencher) {
input[x + 3] = b'\n';
}
let output = &mut [0u8; 3072];
let mut spec = data_encoding::BASE64.specification();
let mut spec = BASE64.specification();
spec.wrap.width = 76;
spec.wrap.separator.push_str("\r\n");
let base64 = spec.encoding().unwrap();
Expand All @@ -155,32 +158,14 @@ fn base64_decode_wrap(b: &mut Bencher) {

#[bench]
fn dnscurve_decode_base(b: &mut Bencher) {
let dns_curve = {
use data_encoding::{Specification, BitOrder};
let mut spec = Specification::new();
spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
spec.bit_order = BitOrder::LeastSignificantFirst;
spec.encoding().unwrap()
};
let input = &[b'0'; 4096];
let output = &mut [0u8; 2560];
b.iter(|| dns_curve.decode_mut(input, output));
b.iter(|| BASE32_DNSCURVE.decode_mut(input, output));
}

#[bench]
fn dnscurve_encode_base(b: &mut Bencher) {
let dns_curve = {
use data_encoding::{Specification, BitOrder};
let mut spec = Specification::new();
spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
spec.bit_order = BitOrder::LeastSignificantFirst;
spec.encoding().unwrap()
};
let input = &[0u8; 4096];
let output = &mut [0u8; 6554];
b.iter(|| dns_curve.encode_mut(input, output));
b.iter(|| BASE32_DNSCURVE.encode_mut(input, output));
}
71 changes: 58 additions & 13 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,23 +735,15 @@ pub enum BitOrder {
///
/// # Examples
///
/// Here is how one would implement the [DNSCurve] base32 encoding:
/// DNSCurve [base32] uses least significant bit first:
///
/// ```rust
/// let dns_curve = {
/// use data_encoding::{Specification, BitOrder};
/// let mut spec = Specification::new();
/// spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
/// spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
/// spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
/// spec.bit_order = BitOrder::LeastSignificantFirst;
/// spec.encoding().unwrap()
/// };
/// assert_eq!(dns_curve.encode(&[0x64, 0x88]), "4321");
/// assert_eq!(dns_curve.decode(b"4321").unwrap(), vec![0x64, 0x88]);
/// use data_encoding::BASE32_DNSCURVE;
/// assert_eq!(BASE32_DNSCURVE.encode(&[0x64, 0x88]), "4321");
/// assert_eq!(BASE32_DNSCURVE.decode(b"4321").unwrap(), vec![0x64, 0x88]);
/// ```
///
/// [DNSCurve]: https://dnscurve.org/in-implement.html
/// [base32]: constant.BASE32_DNSCURVE.html
LeastSignificantFirst,
}
use BitOrder::*;
Expand Down Expand Up @@ -1987,6 +1979,59 @@ const BASE32_DNSSEC_IMPL: Encoding = Encoding(std::borrow::Cow::Borrowed(&[
128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,
128, 128, 128, 29]));

/// DNSCurve base32 encoding
///
/// This encoding is a static version of:
///
/// ```rust
/// # use data_encoding::{BitOrder, Specification, BASE32_DNSCURVE};
/// let mut spec = Specification::new();
/// spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
/// spec.bit_order = BitOrder::LeastSignificantFirst;
/// spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
/// spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
/// assert_eq!(BASE32_DNSCURVE, spec.encoding().unwrap());
/// ```
///
/// It is conform to [DNSCurve].
///
/// [DNSCurve]: https://dnscurve.org/in-implement.html
pub const BASE32_DNSCURVE: Encoding = BASE32_DNSCURVE_IMPL;
const BASE32_DNSCURVE_IMPL: Encoding = Encoding(std::borrow::Cow::Borrowed(&[
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 98, 99, 100, 102, 103, 104, 106,
107, 108, 109, 110, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122,
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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 128, 128, 128, 128, 128, 128,
128, 128, 10, 11, 12, 128, 13, 14, 15, 128, 16, 17, 18, 19, 20, 128, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 128, 128, 128, 128, 128, 128, 128, 10,
11, 12, 128, 13, 14, 15, 128, 16, 17, 18, 19, 20, 128, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 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, 128, 21]));

/// Padded base64 encoding
///
/// This encoding is a static version of:
Expand Down
46 changes: 20 additions & 26 deletions lib/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ test!{
test(b, b"foobar", b"CPNMUOJ1E8======");
}

test!{
fn base32_dnscurve;
let b = &data_encoding::BASE32_DNSCURVE;
test(b, &[0x64, 0x88], b"4321");
test(b, b"f", b"63");
test(b, b"fo", b"6vv0");
test(b, b"foo", b"6vvy6");
test(b, b"foob", b"6vvy6k1");
test(b, b"fooba", b"6vvy6k5d");
test(b, b"foobar", b"6vvy6k5dl3");
assert_eq!(b.decode(b"6VVY6K5DL3").unwrap(), b"foobar");
assert!(b.decode(b"4322").is_err());
assert!(b.decode(b"4324").is_err());
assert!(b.decode(b"4328").is_err());
assert!(b.decode(b"432j").is_err());
assert!(b.decode(b"08").is_err());
assert!(b.decode(b"0000j").is_err());
assert!(b.decode(b"0000004").is_err());
}

test!{
fn base64;
let b = &data_encoding::BASE64;
Expand Down Expand Up @@ -326,32 +346,6 @@ fn translate() {
assert_eq!(base.decode(b"_____. . . ... ..").unwrap(), []);
}

#[test]
fn dns_curve() {
let mut spec = Specification::new();
spec.symbols.push_str("0123456789bcdfghjklmnpqrstuvwxyz");
spec.bit_order = data_encoding::BitOrder::LeastSignificantFirst;
spec.translate.from.push_str("BCDFGHJKLMNPQRSTUVWXYZ");
spec.translate.to.push_str("bcdfghjklmnpqrstuvwxyz");
let base = spec.encoding().unwrap();
assert_eq!(base.encode(&[0x64, 0x88]), "4321");
assert_eq!(base.decode(b"4321").unwrap(), vec![0x64, 0x88]);
assert!(base.decode(b"4322").is_err());
assert!(base.decode(b"4324").is_err());
assert!(base.decode(b"4328").is_err());
assert!(base.decode(b"432j").is_err());
assert!(base.decode(b"08").is_err());
assert!(base.decode(b"0000j").is_err());
assert!(base.decode(b"0000004").is_err());
assert_eq!(base.encode(b"f"), "63");
assert_eq!(base.encode(b"fo"), "6vv0");
assert_eq!(base.encode(b"foo"), "6vvy6");
assert_eq!(base.encode(b"foob"), "6vvy6k1");
assert_eq!(base.encode(b"fooba"), "6vvy6k5d");
assert_eq!(base.encode(b"foobar"), "6vvy6k5dl3");
assert_eq!(base.decode(b"6VVY6K5DL3").unwrap(), b"foobar");
}

#[test]
fn specification() {
assert_eq!(errmsg(Specification::new().encoding()),
Expand Down

0 comments on commit 8b3f60c

Please sign in to comment.