Git Product home page Git Product logo

base85's Issues

some suggestions

Hello,

in my code I need to convert a 128 bit number (a UUID) into a string. I thought of base85 and found your crate. I ended up not using it (fixed 128 bits -> base85 string is trivial), but while looking at the code I do have some suggestions. Mainly, that it's probably faster to use a match than lazy_static, (which costs an atomic lookup) plus a hashtable lookup. Also, the BASE85_CHARS does not have to be in a lazy static at all, and it does not have to be statically allocated. I came up with the example code below. Which eventually I did not need, but at least the excercise kept me from doom-scrolling all night ... :].

Another suggestion: u32::from_be_bytes(indata[data_index .. data_index + 4].try_into().unwrap()].

I hope this is useful.

#[inline]
fn x85_to_char(x85: u8) -> u8 {
  static B85_TO_CHAR: &'static[u8] = b"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
  B85_TO_CHAR[x85 as usize]
}

#[inline]
fn char_to_x85(c: u8) -> u8 {
  match c {
      b'0' ..= b'9' => c - b'0',
      b'A' ..= b'Z' => c - b'A' + 10,
      b'a' ..= b'z' => c - b'a' + 36,
      b'!' => 62,
      b'#' => 63,
      b'$' => 64,
      b'%' => 65,
      b'&' => 66,
      b'(' => 67,
      b')' => 68,
      b'*' => 69,
      b'+' => 70,
      b'-' => 71,
      b';' => 72,
      b'<' => 73,
      b'=' => 74,
      b'>' => 75,
      b'?' => 76,
      b'@' => 77,
      b'^' => 78,
      b'_' => 79,
      b'`' => 80,
      b'{' => 81,
      b'|' => 82,
      b'}' => 83,
      b'~' => 84,
      _ => unreachable!(),
  }
}

Integer overflow when decoding a `}`

I noticed the library panics on certain inputs containing }:

[dependencies]
base85 = "2.0.0"
fn main() {
    println!("{:?}", base85::decode("}"));
}
$ cargo run
thread 'main' panicked at 'attempt to multiply with overflow', /home/finnb/.cargo/registry/src/index.crates.io-6f17d22bba15001f/base85-2.0.0/src/lib.rs:158:27

$ cargo run --release
Err(UnexpectedEof)

As far as I can tell, it is trying to compute 83*85^4=4332651875 which overflows u32.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.