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
#![warn(unsafe_op_in_unsafe_fn)]
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]
use ffizz_passby::OpaqueStruct;
struct ByteBuffer(Vec<u8>);
#[derive(Clone, Copy)]
#[repr(C)]
pub struct byte_buffer_t([u64; 4]);
impl OpaqueStruct for ByteBuffer {
type CType = byte_buffer_t;
}
#[no_mangle]
pub unsafe extern "C" fn byte_buffer_new() -> byte_buffer_t {
unsafe { ByteBuffer::return_val(ByteBuffer(Vec::new())) }
}
#[no_mangle]
pub unsafe extern "C" fn byte_buffer_init(bb: *mut byte_buffer_t) {
unsafe { ByteBuffer::initialize(bb, ByteBuffer(Vec::new())) }
}
#[no_mangle]
pub unsafe extern "C" fn byte_buffer_free(bb: *mut byte_buffer_t) {
let bb = unsafe { ByteBuffer::take(bb) };
drop(bb);
}
#[no_mangle]
pub unsafe extern "C" fn byte_buffer_checksum(bb: *const byte_buffer_t) -> u8 {
unsafe {
ByteBuffer::with_ref(bb, |bb| {
bb.0.iter().copied().reduce(|a, b| a ^ b).unwrap_or(0)
})
}
}
#[no_mangle]
pub unsafe extern "C" fn byte_buffer_push(bb: *mut byte_buffer_t, b: u8) {
unsafe { ByteBuffer::with_mut_ref(bb, |bb| bb.0.push(b)) }
}
#[no_mangle]
pub unsafe extern "C" fn byte_buffer_combine(
bb1: *mut byte_buffer_t,
bb2: *mut byte_buffer_t,
) -> byte_buffer_t {
let mut bb1 = unsafe { ByteBuffer::take(bb1) };
let bb2 = unsafe { ByteBuffer::take(bb2) };
bb1.0.extend(&bb2.0[..]);
unsafe { ByteBuffer::return_val(bb1) }
}
fn main() {
let mut bb1 = unsafe { byte_buffer_new() };
assert_eq!(
unsafe { byte_buffer_checksum(&bb1 as *const byte_buffer_t) },
0
);
unsafe {
byte_buffer_push(&mut bb1 as *mut byte_buffer_t, 0xf0);
byte_buffer_push(&mut bb1 as *mut byte_buffer_t, 0x0f);
}
assert_eq!(
unsafe { byte_buffer_checksum(&bb1 as *const byte_buffer_t) },
0xff
);
let mut bb2: byte_buffer_t = unsafe { std::mem::zeroed() };
unsafe {
byte_buffer_init(&mut bb2 as *mut byte_buffer_t);
}
unsafe {
byte_buffer_push(&mut bb2 as *mut byte_buffer_t, 0xa5);
byte_buffer_push(&mut bb2 as *mut byte_buffer_t, 0x5b);
}
assert_eq!(
unsafe { byte_buffer_checksum(&bb2 as *const byte_buffer_t) },
0xfe
);
let mut bb3 = unsafe {
byte_buffer_combine(
&mut bb1 as *mut byte_buffer_t,
&mut bb2 as *mut byte_buffer_t,
)
};
assert_eq!(
unsafe { byte_buffer_checksum(&bb3 as *const byte_buffer_t) },
0xff ^ 0xfe
);
unsafe { byte_buffer_free(&mut bb3 as *mut byte_buffer_t) };
}