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
mod ffi;
use ffi::{ MutPtrExt, SliceTExt, WriteTExt, sys };
use std::{ ptr, os::raw::c_char };
use crate::ffi::sys::slice_t;
const API: u16 = 0x01_00;
const USER_SECRET: &[u8] = b"Testolope";
const UID: &[u8] = b"TestCapsuleFormat.3A0351A7-FE90-4383-9E68-FCC20033D5F1";
const CONFIGS: &[&[u8]] = &[b"Default"];
fn log(s: impl AsRef<str>) {
println!("{}", s.as_ref())
}
fn try_catch(f: impl FnOnce() -> Result<(), *const c_char>) -> *const c_char {
f().err().unwrap_or(ptr::null())
}
#[no_mangle]
extern "C" fn init(api: u16, _log_level: u8) -> *const c_char {
match api {
API => ptr::null(),
_ => b"Unsupported API version\0".as_ptr().cast()
}
}
#[no_mangle]
extern "C" fn id(sink: *mut sys::write_t) -> *const c_char {
try_catch(|| sink.checked_write(UID))
}
#[no_mangle]
extern "C" fn configs(sink: *mut sys::write_t) -> *const c_char {
try_catch(|| CONFIGS.iter().try_for_each(|c| sink.checked_write(c)))
}
#[no_mangle]
extern "C" fn set_context(_context: *const sys::slice_t) -> *const c_char {
ptr::null()
}
#[no_mangle]
extern "C" fn auth_info_protect(is_required: *mut u8, retries: *mut u64, config: *const slice_t)
-> *const c_char
{
try_catch(|| {
if !CONFIGS.contains(&config.checked_slice()?) {
Err(b"Invalid configuration\0".as_ptr().cast())?
}
is_required.checked_set(1)?;
retries.checked_set(u64::max_value())?;
Ok(())
})
}
#[no_mangle]
extern "C" fn auth_info_recover(is_required: *mut u8, retries: *mut u64, config: *const slice_t)
-> *const c_char
{
try_catch(|| {
if !CONFIGS.contains(&config.checked_slice()?) {
Err(b"Invalid configuration\0".as_ptr().cast())?
}
is_required.checked_set(1)?;
retries.checked_set(u64::max_value())?;
Ok(())
})
}
#[no_mangle]
extern "C" fn protect(sink: *mut sys::write_t, data: *const sys::slice_t,
config: *const sys::slice_t, auth: *const sys::slice_t) -> *const c_char
{
try_catch(|| {
if !CONFIGS.contains(&config.checked_slice()?) {
Err(b"Invalid configuration\0".as_ptr().cast())?
}
let auth = auth.checked_slice()
.map_err(|_| b"Missing authentication parameter\0".as_ptr().cast())?;
if auth != USER_SECRET {
Err(b"Invalid authentication\0".as_ptr().cast())?
}
let mut data: Vec<u8> = data.checked_slice()?.to_vec();
data.reverse();
sink.checked_write(data)
})
}
#[no_mangle]
extern "C" fn recover(sink: *mut sys::write_t, data: *const sys::slice_t, auth: *const sys::slice_t)
-> *const c_char
{
try_catch(|| {
let auth = auth.checked_slice()
.map_err(|_| b"Missing authentication parameter\0".as_ptr().cast())?;
if auth != USER_SECRET {
Err(b"Invalid authentication\0".as_ptr().cast())?
}
let mut data = data.checked_slice()?.to_vec();
data.reverse();
sink.checked_write(data)
})
}
#[test]
fn test_types() {
struct Fns {
_init: sys::init,
_id: sys::id,
_configs: sys::configs,
_set_context: sys::set_context,
_auth_info_protect: sys::auth_info_protect,
_auth_info_recover: sys::auth_info_recover,
_protect: sys::protect,
_recover: sys::recover
}
let _fns = Fns {
_init: Some(init),
_id: Some(id),
_configs: Some(configs),
_set_context: Some(set_context),
_auth_info_protect: Some(auth_info_protect),
_auth_info_recover: Some(auth_info_recover),
_protect: Some(protect),
_recover: Some(recover)
};
}