1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//!
//!  This is a convertor for bilibili's video ID.
//!
//!  For simplier usage, you can use enc(u64) -> String and
//!     dec(&str) -> u64 instead of BiliAv & BiliBv.
//!
//!

use lazy_static::*;
use std::convert::From;
use std::fmt;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn enc_works() {
        assert_eq!(BiliAv(170001).enc().get(), "17x411w7KC");
        assert_eq!(BiliAv(314).enc().get(), "1xx411c7XW");

        assert_eq!(enc(314), "1xx411c7XW".to_owned());
    }

    #[test]
    fn dec_works() {
        assert_eq!(BiliBv("17x411w7KC".to_string()).dec().get(), 170001);
        assert_eq!(BiliBv("1xx411c7XW".to_string()).dec().get(), 314);

        assert_eq!(dec("1xx411c7XW"), 314);
    }

    #[test]
    fn display_works() {
        assert_eq!(
            format!("{}", BiliBv("1xx411c7XW".to_string())),
            "bv1xx411c7XW".to_string()
        );
        assert_eq!(format!("{}", BiliAv(170001)), "av170001".to_string())
    }
}

const TABLE: &[u8; 58] = b"fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
const S: [u8; 6] = [9, 8, 1, 6, 2, 4];
const XOR: u64 = 177451812;
const ADD: u64 = 8728348608;

lazy_static! {
    static ref TR: [u8; 256] = {
        let mut tr = [0u8; 256];
        for i in 0..58 {
            tr[TABLE[i] as usize] = i as u8;
        }
        tr
    };
}

#[derive(Copy, Clone, Debug)]
pub struct BiliAv(pub u64);

#[derive(Clone, Debug)]
pub struct BiliBv(pub String);

impl BiliBv {
    /// Get BV Id ( NO 'bv' INCLUDED! ) :
    ///
    /// ```
    /// use bv2av::*;
    /// assert_eq!(BiliBv::from("17x411w7KC").get(),
    ///             "17x411w7KC");
    /// ```
    ///
    pub fn get(&self) -> &str {
        &self.0
    }

    /// Decode Bv
    ///
    /// ```
    /// use bv2av::*;
    ///
    /// assert_eq!(BiliBv::from("1xx411c7XW").dec().get(), 314);
    /// ```
    ///
    pub fn dec(&self) -> BiliAv {
        let str = self.get();
        BiliAv(dec(str))
    }
}

impl std::fmt::Display for BiliBv {
    /// Will add 'bv'
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "bv{}", self.get())
    }
}

impl std::fmt::Display for BiliAv {
    /// Will add 'av'
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "av{}", self.get())
    }
}

impl From<u64> for BiliAv {
    fn from(av_num: u64) -> Self {
        BiliAv(av_num)
    }
}

impl From<&str> for BiliBv {
    fn from(str: &str) -> Self {
        BiliBv(str.to_string())
    }
}

impl BiliAv {
    /// Get AV Number:
    ///
    /// ```
    /// use bv2av::BiliAv;
    ///
    /// assert_eq!(BiliAv(170001).get()
    ///     , 170001);
    ///
    /// ```

    pub fn get(&self) -> u64 {
        self.0
    }

    /// Encode Bv
    ///
    /// ```
    /// use bv2av::{BiliAv,BiliBv};
    ///
    /// assert_eq!(BiliAv(170001).enc().get(), "17x411w7KC");
    /// ```

    pub fn enc(&self) -> BiliBv {
        let num = self.0;
        BiliBv(enc(num))
    }
}

pub fn dec(str: &str) -> u64 {
    let b = str.as_bytes();
    ((0..6)
        .map(|i| {
            let index = S[i as usize] as usize;
            let index = b[index];
            TR[index as usize] as u64 * 58u64.pow(i)
        })
        .sum::<u64>()
        - ADD)
        ^ XOR
}

pub fn enc(num: u64) -> String {
    let num = (num ^ XOR) + ADD;
    let mut str = String::from("1  4 1 7  ");
    let b = unsafe { str.as_bytes_mut() };
    for i in 0..6 {
        let index = S[i as usize] as usize;
        b[index] = TABLE[(num / 58u64.pow(i) % 58) as usize];
    }
    str
}