FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
fast32/src/base32/encode_bytes.rs at main · rogusdev/fast32 · GitHub
rogusdev
/
fast32
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
17
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
fast32
/
src
/
base32
/
encode_bytes.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
186 lines (156 loc) · 6.96 KB
Breadcrumbs
fast32
/
src
/
base32
/
encode_bytes.rs
Copy path
File metadata and controls
186 lines (156 loc) · 6.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// unsafe writing adapted from https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#1878
use
core
::
ptr
::
write
;
use
crate
::
shared
::
*
;
use
super
::
alphabet
::
{
BITS
,
WIDTH_DEC
,
WIDTH_ENC
}
;
const
U8_MASK_MID_2
:
u8
=
0b01111100
;
const
U8_MASK_MID_1
:
u8
=
0b00111110
;
/// Capacity needed in dest `Vec<u8>` to encode this byte array -- without padding!
///
/// Also see padded [`capacity_encode`](super::Alphabet32Padded::capacity_encode())
#
[
inline
]
pub
const
fn
capacity_encode
(
a
:
&
[
u8
]
)
->
usize
{
// https://stackoverflow.com/questions/23636240/how-do-i-predict-the-required-size-of-a-base32-decode-output
(
a
.
len
(
)
*
WIDTH_ENC
+
(
WIDTH_DEC
-
1
)
)
/
WIDTH_DEC
}
#
[
inline
]
const
fn
rem_enc
(
rem
:
usize
)
->
usize
{
match
rem
{
4
=>
7
,
3
=>
5
,
2
=>
4
,
1
=>
2
,
_ =>
0
,
}
}
/// Encode byte array with given encoding, into a `String`
///
/// Example:
/// ```
/// use fast32::base32::RFC4648;
/// assert_eq!(RFC4648.encode(&[0x00, 0x12]), "AAJA====");
/// ```
pub
fn
encode
(
enc
:
&
'
static
[
u8
;
BITS
]
,
a
:
&
[
u8
]
)
->
String
{
let
len_dec = a
.
len
(
)
;
let
rem = len_dec %
WIDTH_DEC
;
let
max = len_dec /
WIDTH_DEC
;
let
p_max = max
*
WIDTH_ENC
;
let
rem_enc =
rem_enc
(
rem
)
;
let
mut
b =
Vec
::
<
u8
>
::
with_capacity
(
p_max + rem_enc
)
;
encode_inner
(
enc
,
a
,
&
mut
b
,
max
,
0
,
p_max
,
rem
,
rem_enc
)
;
unsafe
{
String
::
from_utf8_unchecked
(
b
)
}
}
/// Encode byte array with given encoding, into an existing `Vec<u8>`
///
/// Example:
/// ```
/// use fast32::base32::RFC4648;
/// let mut b = Vec::<u8>::with_capacity(8);
/// RFC4648.encode_into(&[0x00, 0x12], &mut b);
/// assert_eq!(&b, b"AAJA====");
/// ```
///
/// Panics if not enough capacity in `b` for encoding -- see [`capacity_encode`](self::capacity_encode())
pub
fn
encode_into
(
enc
:
&
'
static
[
u8
;
BITS
]
,
a
:
&
[
u8
]
,
b
:
&
mut
Vec
<
u8
>
)
{
let
len_dec = a
.
len
(
)
;
let
rem = len_dec %
WIDTH_DEC
;
let
max = len_dec /
WIDTH_DEC
;
let
len_enc = b
.
len
(
)
;
let
p_max = len_enc + max
*
WIDTH_ENC
;
let
rem_enc =
rem_enc
(
rem
)
;
assert
!
(
b
.
capacity
(
)
>= p_max + rem_enc
,
"Missing capacity for encoding"
)
;
encode_inner
(
enc
,
a
,
b
,
max
,
len_enc
,
p_max
,
rem
,
rem_enc
)
;
}
#
[
rustfmt
::
skip
]
#
[
inline
(
always
)
]
fn
encode_inner
(
enc
:
&
'
static
[
u8
;
BITS
]
,
a
:
&
[
u8
]
,
b
:
&
mut
Vec
<
u8
>
,
max
:
usize
,
len_enc
:
usize
,
p_max
:
usize
,
rem
:
usize
,
rem_enc
:
usize
)
{
// 5 bytes is 8 chars exactly
// 4 bytes is 7 chars
// 3 bytes is 5 chars
// 2 bytes is 4 chars
// 1 bytes is 2 chars
// 6 bytes is 10 chars
// 7 bytes is 12 chars
// 8 bytes is 13 chars
// 9 bytes is 15 chars
// (src_size * 8 + 4) / 5
// (1 * 8 + 4) / 5 == 2
// (2 * 8 + 4) / 5 == 4
// (3 * 8 + 4) / 5 == 5
// (4 * 8 + 4) / 5 == 7
// (5 * 8 + 4) / 5 == 8
// (6 * 8 + 4) / 5 == 10
// (7 * 8 + 4) / 5 == 12
// (8 * 8 + 4) / 5 == 13
// (9 * 8 + 4) / 5 == 15
// (dst_size * 5) / 8
// (2 * 5) / 8 == 1
// (4 * 5) / 8 == 2
// (5 * 5) / 8 == 3
// (7 * 5) / 8 == 4
// (8 * 5) / 8 == 5
// (10 * 5) / 8 == 6
// (12 * 5) / 8 == 7
// (13 * 5) / 8 == 8
// (15 * 5) / 8 == 9
for
i
in
0
..max
{
// adapted from https://doc.rust-lang.org/src/alloc/vec/mod.rs.html#1878
unsafe
{
let
c = i
*
WIDTH_DEC
;
let
p = len_enc + i
*
WIDTH_ENC
;
let
end = b
.
as_mut_ptr
(
)
.
add
(
p
)
;
write
(
end
,
enc
[
(
(
a
[
c
]
&
U8_MASK_TOP_5
)
>>
3
)
as
usize
]
)
;
write
(
end
.
add
(
1
)
,
enc
[
(
(
(
a
[
c
]
&
U8_MASK_BOT_3
)
<<
2
)
|
(
(
a
[
c+
1
]
&
U8_MASK_TOP_2
)
>>
6
)
)
as
usize
]
)
;
write
(
end
.
add
(
2
)
,
enc
[
(
(
a
[
c+
1
]
&
U8_MASK_MID_1
)
>>
1
)
as
usize
]
)
;
write
(
end
.
add
(
3
)
,
enc
[
(
(
(
a
[
c+
1
]
&
U8_MASK_BOT_1
)
<<
4
)
|
(
(
a
[
c+
2
]
&
U8_MASK_TOP_4
)
>>
4
)
)
as
usize
]
)
;
write
(
end
.
add
(
4
)
,
enc
[
(
(
(
a
[
c+
2
]
&
U8_MASK_BOT_4
)
<<
1
)
|
(
(
a
[
c+
3
]
&
U8_MASK_TOP_1
)
>>
7
)
)
as
usize
]
)
;
write
(
end
.
add
(
5
)
,
enc
[
(
(
a
[
c+
3
]
&
U8_MASK_MID_2
)
>>
2
)
as
usize
]
)
;
write
(
end
.
add
(
6
)
,
enc
[
(
(
(
a
[
c+
3
]
&
U8_MASK_BOT_2
)
<<
3
)
|
(
(
a
[
c+
4
]
&
U8_MASK_TOP_3
)
>>
5
)
)
as
usize
]
)
;
write
(
end
.
add
(
7
)
,
enc
[
(
a
[
c+
4
]
&
U8_MASK_BOT_5
)
as
usize
]
)
;
b
.
set_len
(
p +
WIDTH_ENC
)
;
}
}
match
rem
{
4
=>
unsafe
{
let
c = max
*
WIDTH_DEC
;
let
end = b
.
as_mut_ptr
(
)
.
add
(
p_max
)
;
write
(
end
,
enc
[
(
(
a
[
c
]
&
U8_MASK_TOP_5
)
>>
3
)
as
usize
]
)
;
write
(
end
.
add
(
1
)
,
enc
[
(
(
(
a
[
c
]
&
U8_MASK_BOT_3
)
<<
2
)
|
(
(
a
[
c+
1
]
&
U8_MASK_TOP_2
)
>>
6
)
)
as
usize
]
)
;
write
(
end
.
add
(
2
)
,
enc
[
(
(
a
[
c+
1
]
&
U8_MASK_MID_1
)
>>
1
)
as
usize
]
)
;
write
(
end
.
add
(
3
)
,
enc
[
(
(
(
a
[
c+
1
]
&
U8_MASK_BOT_1
)
<<
4
)
|
(
(
a
[
c+
2
]
&
U8_MASK_TOP_4
)
>>
4
)
)
as
usize
]
)
;
write
(
end
.
add
(
4
)
,
enc
[
(
(
(
a
[
c+
2
]
&
U8_MASK_BOT_4
)
<<
1
)
|
(
(
a
[
c+
3
]
&
U8_MASK_TOP_1
)
>>
7
)
)
as
usize
]
)
;
write
(
end
.
add
(
5
)
,
enc
[
(
(
a
[
c+
3
]
&
U8_MASK_MID_2
)
>>
2
)
as
usize
]
)
;
write
(
end
.
add
(
6
)
,
enc
[
(
(
(
a
[
c+
3
]
&
U8_MASK_BOT_2
)
<<
3
)
)
as
usize
]
)
;
b
.
set_len
(
p_max + rem_enc
)
;
}
3
=>
unsafe
{
let
c = max
*
WIDTH_DEC
;
let
end = b
.
as_mut_ptr
(
)
.
add
(
p_max
)
;
write
(
end
,
enc
[
(
(
a
[
c
]
&
U8_MASK_TOP_5
)
>>
3
)
as
usize
]
)
;
write
(
end
.
add
(
1
)
,
enc
[
(
(
(
a
[
c
]
&
U8_MASK_BOT_3
)
<<
2
)
|
(
(
a
[
c+
1
]
&
U8_MASK_TOP_2
)
>>
6
)
)
as
usize
]
)
;
write
(
end
.
add
(
2
)
,
enc
[
(
(
a
[
c+
1
]
&
U8_MASK_MID_1
)
>>
1
)
as
usize
]
)
;
write
(
end
.
add
(
3
)
,
enc
[
(
(
(
a
[
c+
1
]
&
U8_MASK_BOT_1
)
<<
4
)
|
(
(
a
[
c+
2
]
&
U8_MASK_TOP_4
)
>>
4
)
)
as
usize
]
)
;
write
(
end
.
add
(
4
)
,
enc
[
(
(
(
a
[
c+
2
]
&
U8_MASK_BOT_4
)
<<
1
)
)
as
usize
]
)
;
b
.
set_len
(
p_max + rem_enc
)
;
}
2
=>
unsafe
{
let
c = max
*
WIDTH_DEC
;
let
end = b
.
as_mut_ptr
(
)
.
add
(
p_max
)
;
write
(
end
,
enc
[
(
(
a
[
c
]
&
U8_MASK_TOP_5
)
>>
3
)
as
usize
]
)
;
write
(
end
.
add
(
1
)
,
enc
[
(
(
(
a
[
c
]
&
U8_MASK_BOT_3
)
<<
2
)
|
(
(
a
[
c+
1
]
&
U8_MASK_TOP_2
)
>>
6
)
)
as
usize
]
)
;
write
(
end
.
add
(
2
)
,
enc
[
(
(
a
[
c+
1
]
&
U8_MASK_MID_1
)
>>
1
)
as
usize
]
)
;
write
(
end
.
add
(
3
)
,
enc
[
(
(
(
a
[
c+
1
]
&
U8_MASK_BOT_1
)
<<
4
)
)
as
usize
]
)
;
b
.
set_len
(
p_max + rem_enc
)
;
}
1
=>
unsafe
{
let
c = max
*
WIDTH_DEC
;
let
end = b
.
as_mut_ptr
(
)
.
add
(
p_max
)
;
write
(
end
,
enc
[
(
(
a
[
c
]
&
U8_MASK_TOP_5
)
>>
3
)
as
usize
]
)
;
write
(
end
.
add
(
1
)
,
enc
[
(
(
(
a
[
c
]
&
U8_MASK_BOT_3
)
<<
2
)
)
as
usize
]
)
;
b
.
set_len
(
p_max + rem_enc
)
;
}
_ =>
{
}
}
}
Back
|
FazBrowse Home
|
New Git URL