ia0 / data-encoding

Efficient and customizable data-encoding functions in Rust
https://data-encoding.rs/
MIT License
177 stars 23 forks source link

decode_len return a wrong result #111

Closed zier-one closed 2 months ago

zier-one commented 2 months ago

Steps to reproduce: Just run the code:

use data_encoding::BASE64;

fn main() {
    let enc = "kRjZkHs9cWCMThkuZSoK1SPXLrkwheZSKJ6dAUAnqf8=";
    let enc_bytes = enc.as_bytes();
    let dec = BASE64.decode(enc_bytes).unwrap();
    let enc2 = BASE64.encode(dec.as_slice());
    // PASS enc == enc2
    assert_eq!(enc, enc2);

    // PANIC HERE
    // assertion `left == right` failed
    // left: 32
    // right: 33
    assert_eq!(dec.len(), BASE64.decode_len(enc_bytes.len()).unwrap());
}

You can run this code on https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8bb5438031e22614f781f65f11fdeb27

decode_len return a wrong result, the right one should be 32.

ia0 commented 2 months ago

Thanks a lot for the issue! This is working as intended and probably a misunderstanding of the documentation. The documentation of decode_len points to decode_mut which explains that this is only a maximum. I'm improving the documentation in #112.

Here's an example of why decode_len can't possibly return the exact length. Both inputs AA== and AAA= have a length of 4 bytes. However, the first decodes to 1 byte while the second decodes to 2 bytes. How can decode_len return 2 different values by taking only the number 4 as input. It can only possibly return the smallest number greater or equal to all possible return value if the actual input was known (the definition of the maximum).

zier-one commented 2 months ago

OK, thank for your attention and answers.