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
#![allow(missing_docs)]
use crate::sa_hex::unhex;
use crate::{AsciiCase, Error, OutBuf, ERROR};
use core::slice;
#[inline(always)]
fn shl4(x: u8) -> u8 {
x.wrapping_shl(4)
}
#[inline(always)]
unsafe fn read<T>(base: *const T, offset: usize) -> T {
base.add(offset).read()
}
#[inline(always)]
unsafe fn write<T>(base: *mut T, offset: usize, value: T) {
base.add(offset).write(value)
}
#[inline]
pub fn check(src: &[u8]) -> bool {
unsafe fn check_unroll1(n: usize, src: *const u8) -> bool {
let mut i = 0;
let mut ans = 0;
while i < n {
ans |= unhex(read(src, i));
i += 1;
}
ans != 0xff
}
unsafe fn check_unroll4(n: usize, src: *const u8) -> bool {
let mut i = 0;
while i < n {
let y1 = unhex(read(src, i));
let y2 = unhex(read(src, i + 1));
let y3 = unhex(read(src, i + 2));
let y4 = unhex(read(src, i + 3));
if y1 | y2 | y3 | y4 == 0xff {
return false;
}
i += 4;
}
true
}
let n = src.len();
let src = src.as_ptr();
unsafe {
let n1 = n & 3;
let n4 = n - n1;
if n4 > 0 && !check_unroll4(n4, src) {
return false;
}
check_unroll1(n1, src.add(n4))
}
}
const fn full_table(table: &[u8; 16]) -> [u16; 256] {
let mut buf = [0; 256];
let mut i = 0;
while i < 256 {
let hi = table[i >> 4];
let lo = table[i & 0xf];
buf[i] = u16::from_ne_bytes([hi, lo]);
i += 1;
}
buf
}
const UPPER_TABLE: &[u8; 16] = b"0123456789ABCDEF";
const LOWER_TABLE: &[u8; 16] = b"0123456789abcdef";
pub const FULL_LOWER_TABLE: &[u16; 256] = &full_table(LOWER_TABLE);
pub const FULL_UPPER_TABLE: &[u16; 256] = &full_table(UPPER_TABLE);
#[inline]
pub fn encode<'s, 'd>(
src: &'s [u8],
dst: OutBuf<'d, u8>,
case: AsciiCase,
) -> Result<&'d mut [u8], Error> {
if dst.len() / 2 < src.len() {
return Err(ERROR);
}
let table = match case {
AsciiCase::Lower => FULL_LOWER_TABLE,
AsciiCase::Upper => FULL_UPPER_TABLE,
};
unsafe {
let dst = dst.as_mut_ptr();
encode_unchecked(src, dst, table);
Ok(slice::from_raw_parts_mut(dst, src.len() * 2))
}
}
#[inline]
pub(crate) unsafe fn encode_unchecked(src: &[u8], dst: *mut u8, table: &[u16; 256]) {
let (n, src) = (src.len(), src.as_ptr());
let table = table.as_ptr();
let mut i = 0;
while i < n {
let x = read(src, i);
let y = read(table, x as usize);
dst.add(i * 2).cast::<u16>().write_unaligned(y);
i += 1;
}
}
#[inline]
pub fn decode<'s, 'd>(src: &'s [u8], dst: OutBuf<'d, u8>) -> Result<&'d mut [u8], Error> {
let n = src.len();
let m = n / 2;
if !(n % 2 == 0 && dst.len() >= m) {
return Err(ERROR);
}
unsafe {
let dst = dst.as_mut_ptr();
decode_unchecked(m, src.as_ptr(), dst)?;
Ok(slice::from_raw_parts_mut(dst, m))
}
}
#[inline]
pub fn decode_inplace(buf: &mut [u8]) -> Result<&mut [u8], Error> {
let n = buf.len();
let m = n / 2;
if n % 2 != 0 {
return Err(ERROR);
}
unsafe {
let src = buf.as_ptr();
let dst = buf.as_mut_ptr();
decode_unchecked(m, src, dst)?;
Ok(slice::from_raw_parts_mut(dst, m))
}
}
#[inline]
pub(crate) unsafe fn decode_unchecked(m: usize, src: *const u8, dst: *mut u8) -> Result<(), Error> {
let mut i = 0;
while i < m {
let y1 = unhex(read(src, i * 2));
let y2 = unhex(read(src, i * 2 + 1));
if y1 | y2 == 0xff {
return Err(ERROR);
}
let z = shl4(y1) | y2;
write(dst, i, z);
i += 1;
}
Ok(())
}
#[test]
fn test() {
crate::tests::test(check, decode, encode, decode_inplace);
}