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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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
];
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)->B32DecodeError{
if len==0 {return B32DecodeError::Null;}
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;
}
fn b32_decode(mut ptr_data:*const u8,mut len_data:usize,ptr_alphabet:*const u8)->String{
let np;
unsafe{
np=if len_data>6 {
if *ptr_data.offset((len_data-6) as isize)==61 {6}else{
if *ptr_data.offset((len_data-4) as isize)==61 {4}else{
if *ptr_data.offset((len_data-3) as isize)==61 {3}else{
if *ptr_data.offset((len_data-1) as isize)==61 {1}else{0}
}
}
}
}else{0}
}
let size=(len_data-np)*5/8;
let mut string:String=String::with_capacity(size);
unsafe{
let mut vector=&mut string.as_mut_vec();
vector.resize(size,0);
let mut pointer=vector.as_mut_ptr();
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;
}
}
}
return string;
}
#[allow(dead_code)]
fn b32_decode2(mut ptr_data:*const u8,mut len_data:usize,ptr_alphabet:*const u8,mut pointer:*mut u8,np:usize){
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/string.rs");