use bitcoin::util::base58::Error;
use std::{fmt, iter, slice, str};
use bitcoin::util::base58;
static BASE26_CHARS: &'static [u8] = b"abcdefghijklmnopqrstuvwxyz";
static BASE26_DIGITS: [Option<u8>; 128] = [
None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
Some(0),
Some(1),
Some(2),
Some(3),
Some(4),
Some(5),
Some(6), Some(7),
Some(8),
Some(9),
Some(10),
Some(11),
Some(12),
Some(13),
Some(14), Some(15),
Some(16),
Some(17),
Some(18),
Some(19),
Some(20),
Some(21),
Some(22), Some(23),
Some(24),
Some(25),
None,
None,
None,
None,
None, ];
pub fn from(data: &str) -> Result<Vec<u8>, Error> {
let mut scratch = vec![0u8; 1 + data.len() * 9 / 15];
for d58 in data.bytes() {
if d58 as usize > BASE26_DIGITS.len() {
return Err(Error::BadByte(d58));
}
let mut carry = match BASE26_DIGITS[d58 as usize] {
Some(d58) => d58 as u32,
None => {
return Err(Error::BadByte(d58));
}
};
for d256 in scratch.iter_mut().rev() {
carry += *d256 as u32 * 26;
*d256 = carry as u8;
carry /= 256;
}
assert_eq!(carry, 0);
}
let mut ret: Vec<u8> = data
.bytes()
.take_while(|&x| x == BASE26_CHARS[0])
.map(|_| 0)
.collect();
ret.extend(scratch.into_iter().skip_while(|&x| x == 0));
Ok(ret)
}
impl<T: Default + Copy> SmallVec<T> {
pub fn new() -> SmallVec<T> {
SmallVec {
len: 0,
stack: [T::default(); 100],
heap: Vec::new(),
}
}
pub fn push(&mut self, val: T) {
if self.len < 100 {
self.stack[self.len] = val;
self.len += 1;
} else {
self.heap.push(val);
}
}
pub fn iter(&self) -> iter::Chain<slice::Iter<T>, slice::Iter<T>> {
self.stack[0..self.len].iter().chain(self.heap.iter())
}
pub fn iter_mut(&mut self) -> iter::Chain<slice::IterMut<T>, slice::IterMut<T>> {
self.stack[0..self.len]
.iter_mut()
.chain(self.heap.iter_mut())
}
}
struct SmallVec<T> {
len: usize,
stack: [T; 100],
heap: Vec<T>,
}
fn format_iter<I, W>(writer: &mut W, data: I) -> Result<(), fmt::Error>
where
I: Iterator<Item = u8> + Clone,
W: fmt::Write,
{
let mut ret = SmallVec::new();
let mut leading_zero_count = 0;
let mut leading_zeroes = true;
for d256 in data {
let mut carry = d256 as usize;
if leading_zeroes && carry == 0 {
leading_zero_count += 1;
} else {
leading_zeroes = false;
}
for ch in ret.iter_mut() {
let new_ch = *ch as usize * 256 + carry;
*ch = (new_ch % 26) as u8;
carry = new_ch / 26;
}
while carry > 0 {
ret.push((carry % 26) as u8);
carry /= 26;
}
}
for _ in 0..leading_zero_count {
ret.push(0);
}
for ch in ret.iter().rev() {
writer.write_char(BASE26_CHARS[*ch as usize] as char)?;
}
Ok(())
}
fn encode_iter<I>(data: I) -> String
where
I: Iterator<Item = u8> + Clone,
{
let mut ret = String::new();
format_iter(&mut ret, data).expect("writing into string shouldn't fail");
ret
}
pub fn encode_slice(data: &[u8]) -> String {
encode_iter(data.iter().cloned())
}
#[test]
fn test() {
let data = crate::util::sha256_str("asdf").to_vec();
println!("{}", hex::encode(data.clone()));
println!("{}", base58::check_encode_slice(&data));
let res = encode_slice(&data.clone());
println!("{}", res);
println!("{}", res.to_uppercase());
let res2 = from(&res.clone()).expect("decoded");
assert_eq!(data, res2.clone());
let res3 = encode_slice(&res2.clone());
assert_eq!(res3, res);
}