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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
#[macro_use]
extern crate alloc as std;

pub mod xstd {
    pub use std::collections::BTreeMap;
    pub use std::string::String;
    pub use std::vec::Vec;
    pub use std::boxed::Box;
    pub use std::format;
    pub use std::vec;
    pub use std::string::ToString;
    pub use core::option::Option;
}

use xstd::*;

pub trait NullSafePtr<T: Sized> {
    fn safe_ptr(&self) -> *const T;
}

pub trait NullSafeMutPtr<T: Sized> {
    fn safe_mut_ptr(&mut self) -> *mut T;
}

impl<T: Sized> NullSafePtr<T> for &[T] {
    fn safe_ptr(&self) -> *const T {
        if self.is_empty() {
            core::ptr::null()
        } else {
            self.as_ptr()
        }
    }
}

impl<T: Sized> NullSafeMutPtr<T> for &mut [T] {
    fn safe_mut_ptr(&mut self) -> *mut T {
        if self.is_empty() {
            core::ptr::null_mut()
        } else {
            self.as_mut_ptr()
        }
    }
}

impl<T: Sized> NullSafePtr<T> for Vec<T> {
    fn safe_ptr(&self) -> *const T {
        self.as_slice().safe_ptr()
    }
}

impl NullSafePtr<u8> for str {
    fn safe_ptr(&self) -> *const u8 {
        self.as_bytes().safe_ptr()
    }
}

pub trait AsByteSlice {
    /// # Safety
    /// The method is unsafe because any padding bytes in the struct may be uninitialized memory (giving undefined behavior).
    /// Also, there are not any Endianness assumtions. The caller should care about it.
    unsafe fn as_byte_slice(&self) -> &[u8];
}

pub trait AsByteSliceMut {
    /// # Safety
    /// The method is unsafe because any padding bytes in the struct may be uninitialized memory (giving undefined behavior).
    /// Also, there are not any Endianness assumtions. The caller should care about it.
    unsafe fn as_byte_slice_mut(&mut self) -> &mut [u8];
}

macro_rules! impl_int {
    ($name:ty) => {
        impl AsByteSlice for $name {
            unsafe fn as_byte_slice(&self) -> &[u8] {
                let byte_size = core::mem::size_of::<$name>();
                core::slice::from_raw_parts(self as *const _ as *const u8, byte_size)
            }
        }

        impl AsByteSlice for [$name] {
            unsafe fn as_byte_slice(&self) -> &[u8] {
                let byte_size = self.len() * core::mem::size_of::<$name>();
                core::slice::from_raw_parts(self.as_ptr() as *const u8, byte_size)
            }
        }

        impl AsByteSlice for Vec<$name> {
            unsafe fn as_byte_slice(&self) -> &[u8] {
                let byte_size = self.len() * core::mem::size_of::<$name>();
                core::slice::from_raw_parts(self.as_ptr() as *const u8, byte_size)
            }
        }

        impl AsByteSliceMut for $name {
            unsafe fn as_byte_slice_mut(&mut self) -> &mut [u8] {
                let byte_size = core::mem::size_of::<$name>();
                core::slice::from_raw_parts_mut(self as *mut _ as *mut u8, byte_size)
            }
        }

        impl AsByteSliceMut for [$name] {
            unsafe fn as_byte_slice_mut(&mut self) -> &mut [u8] {
                let byte_size = self.len() * core::mem::size_of::<$name>();
                core::slice::from_raw_parts_mut(self.as_mut_ptr() as *mut u8, byte_size)
            }
        }

        impl AsByteSliceMut for Vec<$name> {
            unsafe fn as_byte_slice_mut(&mut self) -> &mut [u8] {
                let byte_size = self.len() * core::mem::size_of::<$name>();
                core::slice::from_raw_parts_mut(self.as_mut_ptr() as *mut u8, byte_size)
            }
        }
    };
}

impl_int!(u8);
impl_int!(u16);
impl_int!(u32);
impl_int!(u64);
impl_int!(i8);
impl_int!(i16);
impl_int!(i32);
impl_int!(i64);

#[derive(Clone)]
pub struct StructBuffer<T: Sized> {
    buffer: Vec<u8>,
    _marker: core::marker::PhantomData<T>,
}

#[allow(clippy::len_without_is_empty)]
impl<T: Sized + Clone + Copy> StructBuffer<T> {
    /// Creates a buffer capable to hold the value of type `T`.
    ///
    /// # Safety
    /// The buffer is uninitialized!
    pub unsafe fn new() -> Self {
        Self {
            buffer: alloc_buffer(core::mem::size_of::<T>()),
            _marker: Default::default(),
        }
    }

    /// Creates a buffer capable to hold the value of type `T` plus `ext_size` bytes.
    ///
    /// # Safety
    /// The buffer is uninitialized!
    pub unsafe fn with_ext(ext_size: usize) -> Self {
        Self {
            buffer: alloc_buffer(core::mem::size_of::<T>() + ext_size),
            _marker: Default::default(),
        }
    }

    /// Creates a StructBuffer for the type `T` using supplied `buffer`.
    ///
    /// # Safety
    /// The buffer size should be >= mem::size_of::<T>() !
    pub unsafe fn with_buffer(buffer: Vec<u8>) -> Self {
        if buffer.len() < core::mem::size_of::<T>() {
            panic!("Insufficient buffer size!")
        }

        Self {
            buffer,
            _marker: Default::default(),
        }
    }

    /// Creates a StructBuffer for the type `T` using supplied `T` value.
    ///
    /// # Safety
    /// The buffer size should be >= mem::size_of::<T>() !
    pub fn with_value(value: &T) -> Self {
        let buffer = unsafe {
            let mut buffer = alloc_buffer(core::mem::size_of::<T>());
            let value_bytes = core::slice::from_raw_parts(value as *const _ as *const u8, core::mem::size_of::<T>());
            buffer.as_byte_slice_mut().copy_from_slice(value_bytes);
            buffer
        };

        Self {
            buffer,
            _marker: Default::default(),
        }
    }

    /// Creates the value of type `T` represented by the all-zero byte-pattern.
    pub fn zeroed() -> Self {
        Self {
            buffer: vec![0_u8; core::mem::size_of::<T>()],
            _marker: Default::default(),
        }
    }

    pub fn len(&self) -> usize {
        self.buffer.len()
    }

    pub fn raw(&self) -> &T {
        #[allow(clippy::cast_ptr_alignment)]
        unsafe {
            &*(self.buffer.as_ptr() as *const T)
        }
    }

    pub fn raw_mut(&mut self) -> &mut T {
        #[allow(clippy::cast_ptr_alignment)]
        unsafe {
            &mut *(self.buffer.as_mut_ptr() as *mut T)
        }
    }

    pub fn buffer(&self) -> &[u8] {
        &self.buffer
    }

    pub fn ext_buffer(&self) -> &[u8] {
        &self.buffer[core::mem::size_of::<T>()..]
    }

    pub fn ext_buffer_mut(&mut self) -> &mut [u8] {
        &mut self.buffer[core::mem::size_of::<T>()..]
    }

    pub fn has_ext_buffer(&self) -> bool {
        !self.ext_buffer().is_empty()
    }

    pub fn copy(&self) -> T {
        *self.raw()
    }

    pub fn take(self) -> T {
        *self.raw()
    }
}

impl<T: Sized + Clone + Copy> core::ops::Deref for StructBuffer<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.raw()
    }
}

impl<T: Sized + Clone + Copy> core::ops::DerefMut for StructBuffer<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.raw_mut()
    }
}

impl<T: Sized + Clone + Copy> AsByteSlice for StructBuffer<T> {
    unsafe fn as_byte_slice(&self) -> &[u8] {
        self.buffer.as_byte_slice()
    }
}

impl<T: Sized + Clone + Copy> AsByteSliceMut for StructBuffer<T> {
    unsafe fn as_byte_slice_mut(&mut self) -> &mut [u8] {
        self.buffer.as_byte_slice_mut()
    }
}

/// # Safety
/// The allocated buffer is uninitialized and should be entirely rewritten before read.
pub unsafe fn alloc_buffer(size: usize) -> Vec<u8> {
    let mut buffer = Vec::with_capacity(size);
    buffer.set_len(size);
    buffer
}

#[cfg(test)]
mod tests {
    use super::*;

    #[repr(C, packed)]
    #[derive(Copy, Clone)]
    struct S {
        byte: u8,
        word: u16,
    }

    #[test]
    fn as_byte_slice_for_vec() {
        let vec: Vec<u8> = vec![1, 2, 3];
        let bytes = unsafe { vec.as_byte_slice() };
        assert_eq!(3, bytes.len());

        let vec: Vec<u16> = vec![1, 2, 3];
        let bytes = unsafe { vec.as_byte_slice() };
        assert_eq!(6, bytes.len());

        let vec: Vec<u32> = vec![1, 2, 3];
        let bytes = unsafe { vec.as_byte_slice() };
        assert_eq!(12, bytes.len());
    }

    #[test]
    fn as_byte_slice_for_slice() {
        let vec: Vec<u8> = vec![1, 2, 3];
        let slice = vec.as_slice();
        let bytes = unsafe { slice.as_byte_slice() };
        assert_eq!(3, bytes.len());

        let vec: Vec<u16> = vec![1, 2, 3];
        let slice = vec.as_slice();
        let bytes = unsafe { slice.as_byte_slice() };
        assert_eq!(6, bytes.len());

        let vec: Vec<u32> = vec![1, 2, 3];
        let slice = vec.as_slice();
        let bytes = unsafe { slice.as_byte_slice() };
        assert_eq!(12, bytes.len());
    }

    #[test]
    fn as_byte_slice_for_struct() {
        let mut buffer = StructBuffer::<S>::zeroed();
        assert_eq!(3, buffer.len());

        unsafe {
            // packed fileds
            assert_eq!(0, buffer.byte);
            assert_eq!(0, buffer.word)
        }

        buffer.byte = 12;
        unsafe {
            assert_eq!(12, buffer.byte);
            assert_eq!(0, buffer.word)
        }

        let bytes = unsafe { buffer.as_byte_slice() };
        assert_eq!(3, bytes.len());

        let s = buffer.copy();
        unsafe {
            assert_eq!(12, s.byte);
            assert_eq!(0, s.word)
        }

        let s = buffer.take();
        unsafe {
            assert_eq!(12, s.byte);
            assert_eq!(0, s.word)
        }
    }

    #[test]
    fn as_byte_slice_for_primitive() {
        let b = 4_u8;
        let bytes = unsafe { b.as_byte_slice() };
        assert_eq!(1, bytes.len());

        let b = 4_u16;
        let bytes = unsafe { b.as_byte_slice() };
        assert_eq!(2, bytes.len());

        let b = 4_u32;
        let bytes = unsafe { b.as_byte_slice() };
        assert_eq!(4, bytes.len());

        let b = 4_u64;
        let bytes = unsafe { b.as_byte_slice() };
        assert_eq!(8, bytes.len());
    }

    #[test]
    fn ext_buffer() {
        let mut buffer = unsafe { StructBuffer::<S>::with_ext(4) };
        assert_eq!(7, buffer.len());
        assert!(buffer.has_ext_buffer());
        assert!(buffer.ext_buffer().len() == 4);
        assert!(buffer.ext_buffer_mut().len() == 4);

        let mut buffer = StructBuffer::<S>::zeroed();
        assert_eq!(3, buffer.len());
        assert!(!buffer.has_ext_buffer());
        assert!(buffer.ext_buffer().len() == 0);
        assert!(buffer.ext_buffer_mut().len() == 0);
    }

    #[test]
    fn with_value() {
        let mut buffer = StructBuffer::<S>::with_value(&S{ byte: 78, word: 0x1326});
        assert_eq!(3, buffer.len());
        assert!(!buffer.has_ext_buffer());
        assert!(buffer.ext_buffer().len() == 0);
        assert!(buffer.ext_buffer_mut().len() == 0);

        assert!( buffer.byte == 78 );
        assert!( buffer.word == 0x1326 );
    }
}