Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. use data_encoding::{DecodeError, DecodeKind, Encoding, BASE32_NOPAD};
  2.  
  3. fn main() {
  4. println!("{:?}", decode(b"77777777"));
  5. println!("{:?}", decode(b"777777777"));
  6. println!("{:?}", decode(b"7777777777"));
  7. }
  8.  
  9. fn decode(bytes: &[u8]) -> Result<Vec<u8>, DecodeError> {
  10. let mut spec = BASE32_NOPAD.specification();
  11. spec.check_trailing_bits = false;
  12. truncate_decode(&spec.encoding().unwrap(), bytes)
  13. }
  14.  
  15. fn truncate_decode(encoding: &Encoding, input: &[u8]) -> Result<Vec<u8>, DecodeError> {
  16. match encoding.decode(input) {
  17. Err(DecodeError {
  18. position,
  19. kind: DecodeKind::Length,
  20. }) => encoding.decode(&input[..position]),
  21. output => output,
  22. }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement