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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use crate::result::*;
use crate::thread;
use crate::diag::abort;
use crate::diag::log;
use crate::diag::log::Logger;
use alloc::string::String;
use core::str;
use core::ptr;
use core::fmt;
use core::panic;
pub mod rc;
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
#[repr(C)]
pub struct Uuid {
pub uuid: [u8; 0x10]
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[repr(C)]
pub struct PointerAndSize {
pub address: *mut u8,
pub size: usize
}
impl PointerAndSize {
pub const fn empty() -> Self {
Self { address: ptr::null_mut(), size: 0 }
}
pub const fn new(address: *mut u8, size: usize) -> Self {
Self { address, size }
}
pub fn is_valid(&self) -> bool {
!self.address.is_null() && (self.size != 0)
}
}
const fn const_usize_min(a: usize, b: usize) -> usize {
if a > b {
b
}
else {
a
}
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct CString<const S: usize> {
pub c_str: [u8; S]
}
impl<const S: usize> fmt::Debug for CString<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let str_data = self.get_str().unwrap_or("<empty>");
write!(f, "{}", str_data)
}
}
impl<const S: usize> PartialEq for CString<S> {
fn eq(&self, other: &Self) -> bool {
if let Ok(self_str) = self.get_str() {
if let Ok(other_str) = other.get_str() {
return self_str == other_str;
}
}
false
}
}
impl<const S: usize> Eq for CString<S> {}
impl<const S: usize> Default for CString<S> {
fn default() -> Self {
Self::new()
}
}
impl<const S: usize> CString<S> {
pub const fn new() -> Self {
Self { c_str: [0; S] }
}
pub const fn from_raw(raw_bytes: [u8; S]) -> Self {
Self { c_str: raw_bytes }
}
pub const fn from_str(string: &str) -> Self {
let mut cstr = Self::new();
cstr.set_str(string);
cstr
}
pub fn from_string(string: String) -> Self {
let mut cstr = Self::new();
cstr.set_string(string);
cstr
}
const fn copy_str_to(string: &str, ptr: *mut u8, ptr_len: usize) {
unsafe {
ptr::write_bytes(ptr, 0, ptr_len);
if !string.is_empty() {
ptr::copy(string.as_ptr(), ptr, const_usize_min(string.len(), ptr_len - 1));
}
}
}
fn copy_string_to(string: String, ptr: *mut u8, ptr_len: usize) {
unsafe {
ptr::write_bytes(ptr, 0, ptr_len);
if !string.is_empty() {
ptr::copy(string.as_ptr(), ptr, core::cmp::min(ptr_len - 1, string.len()));
}
}
}
fn read_str_from(ptr: *const u8, str_len: usize) -> Result<&'static str> {
if str_len == 0 {
Ok("")
}
else {
unsafe {
match core::str::from_utf8(core::slice::from_raw_parts(ptr, str_len)) {
Ok(name) => Ok(name.trim_end_matches('\0')),
Err(_) => rc::ResultInvalidUtf8Conversion::make_err()
}
}
}
}
fn read_string_from(ptr: *const u8, str_len: usize) -> Result<String> {
Ok(String::from(Self::read_str_from(ptr, str_len)?))
}
pub fn len(&self) -> usize {
for i in 0..S {
if self.c_str[i] == 0 {
return i;
}
}
S
}
pub const fn set_str(&mut self, string: &str) {
Self::copy_str_to(string, &mut self.c_str as *mut _ as *mut u8, S)
}
pub fn set_string(&mut self, string: String) {
Self::copy_string_to(string, &mut self.c_str as *mut _ as *mut u8, S)
}
pub fn get_str(&self) -> Result<&'static str> {
Self::read_str_from(&self.c_str as *const _ as *const u8, self.len())
}
pub fn get_string(&self) -> Result<String> {
Self::read_string_from(&self.c_str as *const _ as *const u8, self.len())
}
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct CString16<const S: usize> {
pub c_str: [u16; S]
}
impl<const S: usize> fmt::Debug for CString16<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Ok(string) = self.get_string() {
write!(f, "{}", string)
}
else {
write!(f, "<empty>")
}
}
}
impl<const S: usize> PartialEq for CString16<S> {
fn eq(&self, other: &Self) -> bool {
if let Ok(self_str) = self.get_string() {
if let Ok(other_str) = other.get_string() {
return self_str == other_str;
}
}
false
}
}
impl<const S: usize> Eq for CString16<S> {}
impl<const S: usize> Default for CString16<S> {
fn default() -> Self {
Self::new()
}
}
impl<const S: usize> CString16<S> {
pub const fn new() -> Self {
Self { c_str: [0; S] }
}
pub const fn from_raw(raw_bytes: [u16; S]) -> Self {
Self { c_str: raw_bytes }
}
pub fn from_str(string: &str) -> Result<Self> {
let mut cstr = Self::new();
cstr.set_str(string)?;
Ok(cstr)
}
pub fn from_string(string: String) -> Result<Self> {
let mut cstr = Self::new();
cstr.set_string(string)?;
Ok(cstr)
}
fn copy_str_to(string: &str, ptr: *mut u16, ptr_len: usize) -> Result<()> {
let mut encode_buf: [u16; 2] = [0; 2];
let mut i: isize = 0;
unsafe {
ptr::write_bytes(ptr, 0, ptr_len);
for ch in string.chars() {
let enc = ch.encode_utf16(&mut encode_buf);
*ptr.offset(i) = enc[0];
i += 1;
if i as usize > (ptr_len - 1) {
break;
}
}
}
Ok(())
}
fn read_string_from(ptr: *const u16, str_len: usize) -> Result<String> {
let mut string = String::new();
if str_len > 0 {
unsafe {
let tmp_slice = core::slice::from_raw_parts(ptr, str_len);
for ch_v in core::char::decode_utf16(tmp_slice.iter().cloned()) {
if let Ok(ch) = ch_v {
string.push(ch);
}
else {
break;
}
}
}
}
Ok(string)
}
pub fn len(&self) -> usize {
for i in 0..S {
if self.c_str[i] == 0 {
return i;
}
}
S
}
pub fn set_str(&mut self, string: &str) -> Result<()> {
Self::copy_str_to(string, &mut self.c_str as *mut _ as *mut u16, S)
}
pub fn set_string(&mut self, string: String) -> Result<()> {
self.set_str(string.as_str())
}
pub fn get_string(&self) -> Result<String> {
Self::read_string_from(&self.c_str as *const _ as *const u16, self.len())
}
pub fn swap_chars(&self) -> Self {
let mut self_copy = *self;
for i in 0..S {
self_copy.c_str[i] = self.c_str[i].swap_bytes();
}
self_copy
}
}
pub fn str_ptr_len(str_ptr: *const u8) -> usize {
unsafe {
let mut iter_ptr = str_ptr as *mut u8;
while (*iter_ptr) != 0 {
iter_ptr = iter_ptr.add(1);
}
iter_ptr.offset_from(str_ptr) as usize
}
}
pub fn str_copy<'a>(dst_str: &'a str, src_str: &'a str) -> &'a str {
let dst_str_len = dst_str.len().min(src_str.len());
unsafe {
let dst_buf = dst_str.as_ptr() as *mut u8;
let src_buf = src_str.as_ptr();
for i in 0..dst_str_len as isize {
*dst_buf.offset(i) = *src_buf.offset(i);
}
let dst_slice = core::slice::from_raw_parts_mut(dst_buf, dst_str_len);
core::str::from_utf8_unchecked(dst_slice)
}
}
pub fn raw_transmute<T: Copy, U: Copy>(t: T) -> U {
unsafe {
union RawTransmuteUnion<T: Copy, U: Copy> {
t: T,
u: U
}
let tmp = RawTransmuteUnion::<T, U> { t };
tmp.u
}
}
pub fn simple_panic_handler<L: Logger>(info: &panic::PanicInfo, desired_level: abort::AbortLevel) -> ! {
let thread_name = match thread::get_current_thread().name.get_str() {
Ok(name) => name,
_ => "<unknown>",
};
diag_log!(L { log::LogSeverity::Fatal, true } => "Panic! at thread '{}' -> {}\n", thread_name, info);
abort::abort(desired_level, super::rc::ResultPanicked::make())
}