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
static ALPHABET32:&'static[u8;32]=&[
    065,066,067,068,069,070,071,072,
    073,074,075,076,077,078,079,080,
    081,082,083,084,085,086,087,088,
    089,090,050,051,052,053,054,055
];

static _ALPHABET32:&'static[u8;256]=&[
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,026,027,028,029,030,031,
    255,255,255,255,255,255,255,255,
    255,000,001,002,003,004,005,006,
    007,008,009,010,011,012,013,014,
    015,016,017,018,019,020,021,022,
    023,024,025,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255,
    255,255,255,255,255,255,255,255
];

pub struct B32<T>(T);
pub trait B32Encode<I,O>{fn encode(data:I)->O;}

pub trait B32Decode<I,O>{
    fn validation(data:I)->B32Error;
    fn decode(data:I)->Result<O,B32Error>;
    unsafe fn unsafe_decode(data:I)->O;
}


#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum B32Error{
    Null,
    InvalidChar,
    InvalidPadding,
    InvalidLength
}


fn b32_validation(ptd:*const u8,len:usize,pta:*const u8,max_pad:usize,pad:u8,rem:usize)->B32Error{
    if len==0 {return B32Error::Null;}
    if len%rem!=0 {return B32Error::InvalidLength;}
    let mut ind:isize=len as isize;
    #[allow(unused_parens)]
    unsafe{while({ind-=1;ind>0 && (*ptd.offset(ind)==61)}){}}
    let pdl=len-1-ind as usize;
    if pdl>max_pad {return B32Error::InvalidPadding;}
    if ((pad>>pdl)&0b1)==0 {return B32Error::InvalidPadding;}
    unsafe{while ind>-1{if *pta.offset(*ptd.offset(ind) as isize)==255{return B32Error::InvalidChar;}ind-=1;}}
    return B32Error::Null;
}



// include!("./include/u8.rs");
// include!("./include/u16.rs");
// include!("./include/u32.rs");
// include!("./include/u64.rs");

include!("./include/string.rs");