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
//! This crate provides functions to read numbers from a stream of bytes
//! either in big-endian or little-endian. Functions return Result type
//! instead of panic!.
//!
//! # Examples
//!
//! Read signed 16-bit integers:
//! ```rust
//! use endianness::*;
//!
//! let v = vec![0, 128, 128, 0];
//! assert_eq!(-32768, read_i16(&v[0..2], ByteOrder::LittleEndian).unwrap());
//! assert_eq!(-32768, read_i16(&v[2..4], ByteOrder::BigEndian).unwrap());
//! ```
//!
//! Read a signed 32-bit integer:
//! ```rust
//! use endianness::*;
//!
//! let v = vec![0, 128, 128, 0];
//! match read_i32(&v, ByteOrder::LittleEndian) {
//! Ok(n) => println!("Read value {}", n), // 8421376
//! Err(err) => println!("Error: {}", err),
//! }
//! ```
//!
//! Read a single-precision floating point number:
//! ```rust
//! use endianness::*;
//!
//! let v = vec![194, 255, 0, 0];
//! assert_eq!(-127.5, read_f32(&v[0..4], ByteOrder::BigEndian).unwrap());
//! ```
//!

#![crate_name = "endianness"]

#![deny(missing_docs, missing_debug_implementations,
        missing_copy_implementations, trivial_casts, trivial_numeric_casts,
        unused_extern_crates, unused_import_braces, unused_qualifications)]

use std::mem;
use std::fmt;
use std::error;

/// The 'ByteOrder' type. It represents the order of bytes in a stream we read from.
#[derive(Debug, Copy, Clone)]
pub enum ByteOrder {
    /// Intel byte order
    LittleEndian,
    /// Motorola byte order
    BigEndian,
}

/// The error type.
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum EndiannessError {
    /// The stream is too small to read the requested type.
    ShortSlice,
}

impl fmt::Display for EndiannessError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            EndiannessError::ShortSlice => write!(f, "The slice length is too short."),
        }
    }
}

impl error::Error for EndiannessError {
    fn description(&self) -> &str {
        match *self {
            EndiannessError::ShortSlice => "The slice length is too short.",
        }
    }
    fn cause(&self) -> Option<&error::Error> {
        match *self {
            EndiannessError::ShortSlice => None,
        }
    }
}

/// Result type alias that fixes Error parameter.
pub type EndiannessResult<T> = Result<T, EndiannessError>;

/// Reads unsigned 16-bit integer from a stream of bytes.
pub fn read_u16(data: &[u8], endianness: ByteOrder) -> EndiannessResult<u16> {
    if data.len() < 2 {
        Err(EndiannessError::ShortSlice)
    } else {
        match endianness {
            ByteOrder::BigEndian => Ok(((data[0] as u16) << 8) + (data[1] as u16)),
            ByteOrder::LittleEndian => Ok(((data[1] as u16) << 8) + (data[0] as u16)),
        }
    }
}

/// Reads signed 16-bit integer from a stream of bytes.
pub fn read_i16(data: &[u8], endianness: ByteOrder) -> EndiannessResult<i16> {
    Ok(try!(read_u16(data, endianness)) as i16)
}

/// Reads unsigned 32-bit integer from a stream of bytes.
pub fn read_u32(data: &[u8], endianness: ByteOrder) -> EndiannessResult<u32> {
    if data.len() < 4 {
        Err(EndiannessError::ShortSlice)
    } else {
        match endianness {
            ByteOrder::BigEndian => {
                Ok(((data[0] as u32) << 24) + ((data[1] as u32) << 16) + ((data[2] as u32) << 8) +
                   (data[3] as u32))
            }
            ByteOrder::LittleEndian => {
                Ok(((data[3] as u32) << 24) + ((data[2] as u32) << 16) + ((data[1] as u32) << 8) +
                   (data[0] as u32))
            }
        }
    }
}

/// Reads signed 32-bit integer from a stream of bytes.
pub fn read_i32(data: &[u8], endianness: ByteOrder) -> EndiannessResult<i32> {
    Ok(try!(read_u32(data, endianness)) as i32)
}

/// Reads unsigned 64-bit integer from a stream of bytes.
pub fn read_u64(data: &[u8], endianness: ByteOrder) -> EndiannessResult<u64> {
    if data.len() < 8 {
        Err(EndiannessError::ShortSlice)
    } else {
        match endianness {
            ByteOrder::BigEndian => {
                Ok(((data[0] as u64) << 56) + ((data[1] as u64) << 48) +
                   ((data[2] as u64) << 40) + ((data[3] as u64) << 32) +
                   ((data[4] as u64) << 24) + ((data[5] as u64) << 16) +
                   ((data[6] as u64) << 8) + (data[7] as u64))
            }
            ByteOrder::LittleEndian => {
                Ok(((data[7] as u64) << 56) + ((data[6] as u64) << 48) +
                   ((data[5] as u64) << 40) + ((data[4] as u64) << 32) +
                   ((data[3] as u64) << 24) + ((data[2] as u64) << 16) +
                   ((data[1] as u64) << 8) + (data[0] as u64))
            }
        }
    }
}

/// Reads signed 64-bit integer from a stream of bytes.
pub fn read_i64(data: &[u8], endianness: ByteOrder) -> EndiannessResult<i64> {
    Ok(try!(read_u64(data, endianness)) as i64)
}

/// Reads a single-precision floating point number.
pub fn read_f32(data: &[u8], endianness: ByteOrder) -> EndiannessResult<f32> {
    let u = try!(read_u32(data, endianness));
    Ok(unsafe { mem::transmute(u) })
}

/// Reads a double-precision floating point number.
pub fn read_f64(data: &[u8], endianness: ByteOrder) -> EndiannessResult<f64> {
    let u = try!(read_u64(data, endianness));
    Ok(unsafe { mem::transmute(u) })
}

#[cfg(test)]
#[allow(unsafe_code)]
mod tests {
    // Macro to test that all of the functions return an error type
    // when given a slice that is too short for them.
    macro_rules! short_slice {
        ($name:ident, $read:ident) => (
            mod $name {
                use {ByteOrder, EndiannessError, $read};

                #[test]
                fn read_big_endian() {
                    assert_eq!(EndiannessError::ShortSlice, $read(&[], ByteOrder::BigEndian).unwrap_err());
                }

                #[test]
                fn read_little_endian() {
                    assert_eq!(EndiannessError::ShortSlice, $read(&[], ByteOrder::LittleEndian).unwrap_err());
                }
            }
        );
    }

    short_slice!(short_slice_u16, read_u16);
    short_slice!(short_slice_i16, read_i16);
    short_slice!(short_slice_u32, read_u32);
    short_slice!(short_slice_i32, read_i32);
    short_slice!(short_slice_u64, read_u64);
    short_slice!(short_slice_i64, read_i64);
    short_slice!(short_slice_f32, read_f32);
    short_slice!(short_slice_f64, read_f64);

    // A macro to perform generative testing using the following invariant:
    // for any integer N that was transmuted to a stream of bytes read functions must return N.
    macro_rules! read_correctness {
        ($name:ident, $ty:ty, $size: expr, $read:ident, $max:expr) => (
            mod $name {
                use std::mem;
                use {ByteOrder, $read};

                extern crate quickcheck;
                extern crate rand;
                use self::quickcheck::{QuickCheck, StdGen, Testable};

                #[test]
                fn read_big_endian() {
                    #[cfg(target_endian = "little")]
                    fn prop(n: $ty) -> bool {
                        let mut data = unsafe { mem::transmute::<_, [u8; $size]>(n) };
                        data.reverse();
                        n == $read(&data, ByteOrder::BigEndian).unwrap()
                    }

                    #[cfg(target_endian = "big")]
                    fn prop(n: $ty) -> bool {
                        let data = unsafe { mem::transmute::<_, [u8; $size]>(n) };
                        n == $read(&data, ByteOrder::BigEndian).unwrap()
                    }

                    // cast function explicitly to get rid off warns about trivial cast (why?)
                    let f: fn($ty) -> bool = prop;
                    quick_check(f);
                }

                #[test]
                fn read_little_endian() {
                    #[cfg(target_endian = "little")]
                    fn prop(n: $ty) -> bool {
                        let data = unsafe { mem::transmute::<_, [u8; $size]>(n) };
                        n == $read(&data, ByteOrder::LittleEndian).unwrap()
                    }

                    #[cfg(target_endian = "big")]
                    fn prop(n: $ty) -> bool {
                        let mut data = unsafe { mem::transmute::<_, [u8; $size]>(n) };
                        data.reverse();
                        n == $read(&data, ByteOrder::LittleEndian).unwrap()
                    }

                    // cast function explicitly to get rid off warns about trivial cast (why?)
                    let f: fn($ty) -> bool = prop;
                    quick_check(f);
                }

                fn quick_check<T: Testable>(prop: T) {
                    QuickCheck::new()
                        .gen(StdGen::new(rand::thread_rng(), $max as usize))
                        .quickcheck(prop);
                }
            }
        );
    }

    read_correctness!(test_u16, u16, 2, read_u16, ::std::u16::MAX);
    read_correctness!(test_i16, i16, 2, read_i16, ::std::i16::MAX);
    read_correctness!(test_u32, u32, 4, read_u32, ::std::u32::MAX);
    read_correctness!(test_i32, i32, 4, read_i32, ::std::i32::MAX);
    read_correctness!(test_u64, u64, 8, read_u64, ::std::u64::MAX);
    read_correctness!(test_i64, i64, 8, read_i64, ::std::i64::MAX);
    read_correctness!(test_f32, f32, 4, read_f32, ::std::u32::MAX);
    read_correctness!(test_f64, f64, 8, read_f64, ::std::u64::MAX);
}