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
169
170
171
172
173
174
175
176
177
178
179
pub static ALPHABET32_ENCODE:&'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
];

pub static ALPHABET32_DECODE:&'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)->B32DecodeError;
    fn decode(data:I)->Result<O,B32DecodeError>;
    unsafe fn unsafe_decode(data:I)->O;
}

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

fn b32_validation(ptd:*const u8,len:usize,pta:*const u8,max_pad:usize,pad:u8,rem:usize,min_len:usize)->B32DecodeError{
    if len==0 {return B32DecodeError::Null;}
    if len<min_len {return B32DecodeError::InvalidLength;}
    if len%rem!=0 {return B32DecodeError::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 B32DecodeError::InvalidPadding;}
    if ((pad>>pdl)&0b1)==0 {return B32DecodeError::InvalidPadding;}
    unsafe{while ind>-1{if *pta.offset(*ptd.offset(ind) as isize)==255{return B32DecodeError::InvalidByte;}ind-=1;}}
    return B32DecodeError::Null;
}

include!("include/encode/be_encode.rs");
include!("include/encode/le_encode.rs");

#[cfg(target_endian = "little")]
const b32_encode_1:fn(*const u8,usize,*const u8)->String=b32_encode_le_1;

#[cfg(target_endian = "little")]
const b32_encode_2:fn(*const u8,usize,*const u8)->String=b32_encode_le_2;

#[cfg(target_endian = "little")]
const b32_encode_4:fn(*const u8,usize,*const u8)->String=b32_encode_le_4;

#[cfg(target_endian = "little")]
const b32_encode_8:fn(*const u8,usize,*const u8)->String=b32_encode_le_8;


// #[cfg(target_endian = "big")]
// const b32_encode_1:fn(*const u8,usize,*const u8)->String=b32_encode_be_1;

// #[cfg(target_endian = "big")]
// const b32_encode_2:fn(*const u8,usize,*const u8)->String=b32_encode_be_2;

// #[cfg(target_endian = "big")]
// const b32_encode_4:fn(*const u8,usize,*const u8)->String=b32_encode_be_4;

// #[cfg(target_endian = "big")]
// const b32_encode_8:fn(*const u8,usize,*const u8)->String=b32_encode_be_8;



#[allow(dead_code)]
pub fn b32_decode(ptr_input:*const u8,len_input:usize,ptr_alphabet:*const u8,mut pointer:*mut u8,np:usize){
    let mut ptr_data=ptr_input;
    let mut len_data=len_input;
    unsafe{
        while len_data>8{
            *pointer.offset(0)=*ptr_alphabet.offset(*ptr_data.offset(0) as isize)<<3|*ptr_alphabet.offset(*ptr_data.offset(1) as isize)>>2;
            *pointer.offset(1)=*ptr_alphabet.offset(*ptr_data.offset(1) as isize)<<6|*ptr_alphabet.offset(*ptr_data.offset(2) as isize)<<1|*ptr_alphabet.offset(*ptr_data.offset(3) as isize)>>4;
            *pointer.offset(2)=*ptr_alphabet.offset(*ptr_data.offset(3) as isize)<<4|*ptr_alphabet.offset(*ptr_data.offset(4) as isize)>>1;
            *pointer.offset(3)=*ptr_alphabet.offset(*ptr_data.offset(4) as isize)<<7|*ptr_alphabet.offset(*ptr_data.offset(5) as isize)<<2|*ptr_alphabet.offset(*ptr_data.offset(6) as isize)>>3;
            *pointer.offset(4)=*ptr_alphabet.offset(*ptr_data.offset(6) as isize)<<5|*ptr_alphabet.offset(*ptr_data.offset(7) as isize)<<0;
            pointer=pointer.offset(5);
            ptr_data=ptr_data.offset(8);
            len_data-=8;
        }
        if len_data==8{
            if np>0{
                if np>3{
                    if np>5{
                        *pointer.offset(0)=*ptr_alphabet.offset(*ptr_data.offset(0) as isize)<<3|*ptr_alphabet.offset(*ptr_data.offset(1) as isize)>>2;
                        *pointer.offset(1)=*ptr_alphabet.offset(*ptr_data.offset(1) as isize)<<6;
                    }else{
                        *pointer.offset(0)=*ptr_alphabet.offset(*ptr_data.offset(0) as isize)<<3|*ptr_alphabet.offset(*ptr_data.offset(1) as isize)>>2;
                        *pointer.offset(1)=*ptr_alphabet.offset(*ptr_data.offset(1) as isize)<<6|*ptr_alphabet.offset(*ptr_data.offset(2) as isize)<<1|*ptr_alphabet.offset(*ptr_data.offset(3) as isize)>>4;
                        *pointer.offset(2)=*ptr_alphabet.offset(*ptr_data.offset(3) as isize)<<4;
                    }
                }else{
                    if np>2{
                        *pointer.offset(0)=*ptr_alphabet.offset(*ptr_data.offset(0) as isize)<<3|*ptr_alphabet.offset(*ptr_data.offset(1) as isize)>>2;
                        *pointer.offset(1)=*ptr_alphabet.offset(*ptr_data.offset(1) as isize)<<6|*ptr_alphabet.offset(*ptr_data.offset(2) as isize)<<1|*ptr_alphabet.offset(*ptr_data.offset(3) as isize)>>4;
                        *pointer.offset(2)=*ptr_alphabet.offset(*ptr_data.offset(3) as isize)<<4|*ptr_alphabet.offset(*ptr_data.offset(4) as isize)>>1;
                        *pointer.offset(3)=*ptr_alphabet.offset(*ptr_data.offset(4) as isize)<<7;
                    }else{
                        *pointer.offset(0)=*ptr_alphabet.offset(*ptr_data.offset(0) as isize)<<3|*ptr_alphabet.offset(*ptr_data.offset(1) as isize)>>2;
                        *pointer.offset(1)=*ptr_alphabet.offset(*ptr_data.offset(1) as isize)<<6|*ptr_alphabet.offset(*ptr_data.offset(2) as isize)<<1|*ptr_alphabet.offset(*ptr_data.offset(3) as isize)>>4;
                        *pointer.offset(2)=*ptr_alphabet.offset(*ptr_data.offset(3) as isize)<<4|*ptr_alphabet.offset(*ptr_data.offset(4) as isize)>>1;
                        *pointer.offset(3)=*ptr_alphabet.offset(*ptr_data.offset(4) as isize)<<7|*ptr_alphabet.offset(*ptr_data.offset(5) as isize)<<2|*ptr_alphabet.offset(*ptr_data.offset(6) as isize)>>3;
                        *pointer.offset(4)=*ptr_alphabet.offset(*ptr_data.offset(6) as isize)<<5;
                    }
                }
            }else{
                *pointer.offset(0)=*ptr_alphabet.offset(*ptr_data.offset(0) as isize)<<3|*ptr_alphabet.offset(*ptr_data.offset(1) as isize)>>2;
                *pointer.offset(1)=*ptr_alphabet.offset(*ptr_data.offset(1) as isize)<<6|*ptr_alphabet.offset(*ptr_data.offset(2) as isize)<<1|*ptr_alphabet.offset(*ptr_data.offset(3) as isize)>>4;
                *pointer.offset(2)=*ptr_alphabet.offset(*ptr_data.offset(3) as isize)<<4|*ptr_alphabet.offset(*ptr_data.offset(4) as isize)>>1;
                *pointer.offset(3)=*ptr_alphabet.offset(*ptr_data.offset(4) as isize)<<7|*ptr_alphabet.offset(*ptr_data.offset(5) as isize)<<2|*ptr_alphabet.offset(*ptr_data.offset(6) as isize)>>3;
                *pointer.offset(4)=*ptr_alphabet.offset(*ptr_data.offset(6) as isize)<<5|*ptr_alphabet.offset(*ptr_data.offset(7) as isize)<<0;
            }
        }
    }
}


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

include!("./include/i8.rs");
include!("./include/i16.rs");
include!("./include/i32.rs");
include!("./include/i64.rs");

include!("./include/vec_u8.rs");
include!("./include/vec_u16.rs");
include!("./include/vec_u32.rs");
include!("./include/vec_u64.rs");

include!("./include/vec_i8.rs");
include!("./include/vec_i16.rs");
include!("./include/vec_i32.rs");
include!("./include/vec_i64.rs");

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