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

use super::Plain;

use core::{mem, slice};

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Error {
    TooShort,
    BadAlignment,
}

/// This trait exposes all the functionality of this crate
/// as methods on applicable types.
///
/// It is implemented automatically for every type marked with
/// the [`Plain`](trait.Plain.html) trait, as well as their slices,
/// so you should never need to implement it yourself.
///
pub trait Methods: Plain {
    /// Same as [`from_bytes()`](fn.from_bytes.html), except attached to T.
    #[inline]
    fn from_bytes<'a>(bytes: &'a [u8]) -> Result<&'a Self, Error>;

    /// Same as [`from_mut_bytes()`](fn.from_mut_bytes.html), except attached to T.
    #[inline]
    fn from_mut_bytes<'a>(bytes: &'a mut [u8]) -> Result<&'a mut Self, Error>;

    /// Same as [`as_bytes()`](fn.as_bytes.html), except as a method of T.
    #[inline(always)]
    fn as_bytes(&self) -> &[u8] {
        self::as_bytes(self)
    }

    /// Same as [`as_mut_bytes()`](fn.as_mut_bytes.html), except as a method of T.
    #[inline(always)]
    fn as_mut_bytes(&mut self) -> &mut [u8] {
        self::as_mut_bytes(self)
    }
}

#[inline(always)]
fn check_instance_size<T>(bytes: &[u8]) -> Result<(), Error> {
    if bytes.len() < mem::size_of::<T>() {
        // slice is too short for target type
        Err(Error::TooShort)
    } else {
        Ok(())
    }
}

#[inline(always)]
fn check_alignment<T>(bytes: &[u8]) -> Result<(), Error> {
    let align_offset = (bytes.as_ptr() as usize) % mem::align_of::<T>();
    
    if align_offset != 0 {
        // badly aligned slice
        Err(Error::BadAlignment)
    } else {
        Ok(())
    }
}

impl<S> Methods for S
    where S: Plain + Sized
{
    #[inline]
    fn from_bytes(bytes: &[u8]) -> Result<&S, Error> {
        try!(check_instance_size::<S>(bytes));
        try!(check_alignment::<S>(bytes));
        Ok(unsafe { &*(bytes.as_ptr() as *const S) })
    }

    #[inline]
    fn from_mut_bytes(bytes: &mut [u8]) -> Result<&mut S, Error> {
        try!(check_instance_size::<S>(bytes));
        try!(check_alignment::<S>(bytes));
        Ok(unsafe { &mut *(bytes.as_mut_ptr() as *mut S) })
    }
}

impl<S> Methods for [S]
    where S: Plain + Sized
{
    #[inline]
    fn from_bytes(bytes: &[u8]) -> Result<&[S], Error> {
        try!(check_alignment::<S>(bytes));
        let len = bytes.len() / mem::size_of::<S>();
        Ok(unsafe { slice::from_raw_parts(bytes.as_ptr() as *const S, len) })
    }

    #[inline]
    fn from_mut_bytes(bytes: &mut [u8]) -> Result<&mut [S], Error> {
        try!(check_alignment::<S>(bytes));
        let len = bytes.len() / mem::size_of::<S>();
        Ok(unsafe { slice::from_raw_parts_mut(bytes.as_mut_ptr() as *mut S, len) })
    }
}

/// Safely converts a reference to any type to an immutable
/// byte slice of appropriate length.
/// 
/// Since the result can't be used to modify the source
/// structure, this is perfectly safe to do on any type,
/// even if mostly useless for most Rust types.
///
/// This function cannot fail.
///
/// <strong>Beware:</strong> Types that are not `#repr(C)` mostly have
/// unspecified layout, and in fact can even change
/// layout from build to build. Two types with exactly
/// same definition can even have different layout in 
/// the same build!
/// It's perfectly safe to read the data and e.g.
/// print it on screen for educational value, but
/// interpreting the data to mean anything is bound
/// to cause bugs. `#repr(C)` structures don't have
/// this problem as their layout is perfectly well
/// defined.
///
#[inline]
pub fn as_bytes<S>(s: &S) -> &[u8]
    where S: ?Sized
{
    // Even though slices can't under normal circumstances be cast
    // to pointers, here in generic code it works.
    // This means that `s` can be a slice or a regular reference,
    // at the caller's discretion.
    let bptr = s as *const S as *const u8;
    let bsize = mem::size_of_val(s);
    unsafe { slice::from_raw_parts(bptr, bsize) }
}

/// Safely converts a reference to a [`Plain`](trait.Plain.html) type to a mutable
/// byte slice of appropriate length.
/// 
/// In contrast to [`as_bytes()`](fn.as_bytes.html), argument must be `Plain`,
/// in order to prevent invoking UB via writing illegal
/// values into it.
///
/// This function cannot fail.
///
#[inline]
pub fn as_mut_bytes<S>(s: &mut S) -> &mut [u8]
    where S: Plain + ?Sized
{
    let bptr = s as *mut S as *mut u8;
    let bsize = mem::size_of_val(s);
    unsafe { slice::from_raw_parts_mut(bptr, bsize) }
}

/// Safely converts a byte slice to a [`Plain`](trait.Plain.html) type
/// reference.
///
/// However, if the byte slice is not long enough
/// to contain target type, or if it doesn't
/// satisfy the type's alignment requirements,
/// the function returns an error.
///
/// If the target type is a slice, its length will be
/// set appropriately to the length of the input slice.
/// E.g. a byte array of length 16 would result in
/// a slice `&[u32]` with 4 entries.
///
/// It is currently unspecified what happens when
/// the result is a slice and the input slice length
/// is not a multiple of the result type.
///
/// In most cases it's much preferrable to allocate
/// a value/slice of the target type and use
/// [`as_mut_bytes()`](fn.as_mut_bytes.html) instead.
///
#[inline]
pub fn from_bytes<T>(bytes: &[u8]) -> Result<&T, Error>
    where T: Methods + ?Sized
{
    T::from_bytes(bytes)
}

/// See [`from_bytes()`](fn.from_bytes.html).
///
/// Does the same, except with mutable references.
#[inline]
pub fn from_mut_bytes<T>(bytes: &mut [u8]) -> Result<&mut T, Error>
    where T: Methods + ?Sized
{
    T::from_mut_bytes(bytes)
}

#[cfg(test)]
mod tests {

    use super::*;
    use core::mem;

    #[repr(C)]
    #[derive(Debug, Copy, Eq, Clone, PartialEq)]
    struct Dummy1 {
        field1: u64,
        field2: u32,
        field3: u16,
        field4: u8,
        field5: u8,
    }

    unsafe impl Plain for Dummy1 {}

    #[repr(C)]
    #[derive(Debug, Copy, Eq, Clone, PartialEq)]
    struct Dummy2 {
        field1: u8,
        field2: u8,
        field3: u16,
        field4: u32,
        field5: u64,
    }

    unsafe impl Plain for Dummy2 {}

    #[test]
    fn one_too_short() {
        let b = vec![0u8; mem::size_of::<Dummy1>()-1];

        let r = Dummy1::from_bytes(&b);
        assert!(r == Err(Error::TooShort));
    }

    #[test]
    fn well_aligned() {
        let b = vec![0u8; mem::size_of::<Dummy1>()+1];

        // No failure.
        Dummy1::from_bytes(&b).unwrap();
    }

    #[test]
    fn unaligned() {
        let b = vec![0u8; mem::size_of::<Dummy1>()+1];
        let b = &b[1..];

        let r = Dummy1::from_bytes(&b);
        assert!(r == Err(Error::BadAlignment));
    }

    #[test]
    fn basic_function() {
        let t1 = Dummy1 {
            field1: 0xaaaaaaaaaaaaaaaau64,
            field2: 0xbbbbbbbbu32,
            field3: 0xccccu16,
            field4: 0xddu8,
            field5: 0xeeu8,
        };

        let r1: &Dummy2 = from_bytes(t1.as_bytes()).unwrap();

        assert!(r1.field1 == 0xaau8);
        assert!(r1.field2 == 0xaau8);
        assert!(r1.field3 == 0xaaaau16);
        assert!(r1.field4 == 0xaaaaaaaau32);
        assert!(r1.field5 == 0xbbbbbbbbccccddeeu64 || r1.field5 == 0xeeddccccbbbbbbbbu64);

        let r2 = r1.as_bytes();
        assert!(r2.len() == mem::size_of::<Dummy1>());
        assert!(r2[5] == 0xaa);

        // nop
        let r3 = r2.as_bytes();

        let r4 = Dummy1::from_bytes(r3).unwrap();

        let r5 = from_bytes::<Dummy2>(r4.as_bytes()).unwrap();

        let r6 = from_bytes::<[Dummy1]>(r5.as_bytes()).unwrap();

        assert!(r6.len() == 1);
        assert!(t1 == r6[0]);
    }
}