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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! byte-io: a simple crate for read/write numbers to/from binary.
//!
//! This crate only contains 4 functions:
//!
//! * `write_be`: write number to big-endian slice.
//!
//! * `read_be`: read number from big-endian slice.
//!
//! * `write_le`: write number to little-endian slice.
//!
//! * `read_le`: read number from little-endian slice.
//!
//! ## Examples:
//!
//! Read from a slice is simple:
//!
//! ```
//! use byte_io::*;
//!
//! fn main() {
//!     let data = [0x00, 0x00, 0x01, 0x01, 0xAB, 0xCD, 0xEF, 0x89];
//!     assert_eq!(read_be::<u32>(&data), 0x0101);
//!     assert_eq!(read_be::<u16>(&data[4..]), 0xABCD);
//!     assert_eq!(read_le::<u16>(&data[4..]), 0xCDAB);
//!     assert_eq!(read_le::<u8>(&data[4..]), 0xAB);
//! }
//!
//! ```
//!
//! Write is also easy:
//!
//! ```
//! use byte_io::*;
//!
//! fn main() {
//!     let mut buf = [0u8;8];
//!     write_be(&0xABCDEFu32, &mut buf);
//!     assert_eq!(buf, [0x00, 0xAB, 0xCD, 0xEF, 0x00, 0x00, 0x00, 0x00]);
//!     write_le(&0xABCDEFu32, &mut buf[4..]);
//!     assert_eq!(buf, [0x00, 0xAB, 0xCD, 0xEF, 0xEF, 0xCD, 0xAB, 0x00]);
//! }
//! ```
//!
//! Moreover, you can even read/write `Vec<T>`:
//!
//! ```
//! use byte_io::*;
//!
//! fn main() {
//!     let mut buf = [0u8;8];
//!     let data = vec![0x1234u16,0x5678u16];
//!     write_le(&data, &mut buf);
//!     assert_eq!(buf, [0x34, 0x12, 0x78, 0x56, 0x00, 0x00, 0x00, 0x00]);
//!     assert_eq!(data, read_le::<Vec<u16>>(&buf[0..4]));
//!     let u32_vec = read_be::<Vec<u32>>(&buf[4..]);
//!     assert_eq!(u32_vec.len(), 1);
//!     assert_eq!(u32_vec.first(), Some(&0));
//! }
//! ```
//!
//! The following code also works:
//!
//! ```
//! use byte_io::*;
//!
//! fn main() {
//!     let buf = [0xAA, 0xBB, 0xCC, 0xDD];
//!     assert_eq!(u32::from_u8_be(&buf), 0xAABBCCDD);
//! }
//! ```
//!
//!
//! ## Implementation Details
//!
//! byte-io does __NOT__ focus on efficiency, which means that it may be slow
//! while handling big streams (e.g. hundreds of Mbytes or more).
//!
//! Generally speaking, byte-io implements the two traits for numbers: `Readable` and
//! `Writeable`. Every type implements these two traits can be deceded/enceded from
//! binary stream.
use std::marker;
use std::mem::{size_of, transmute};

/// write a number to stream as big-endian.
///
/// panics if buffer does not contain enough space.
///
/// ```
/// use byte_io::*;
///
/// let mut buf = [0u8;8];
/// write_be(&1u64, &mut buf);
/// assert_eq!(buf, [0,0,0,0,0,0,0,1]);
/// ```
pub fn write_be<T: Writeable>(v: &T, buffer: &mut [u8]) {
    T::to_u8_be(v, buffer);
}

/// read a number from stream as big-endian.
///
/// panics if buffer does not contain enough bytes.
///
/// ```
/// use byte_io::*;
/// let data = [0xAB, 0xCD, 0xEF, 0x01, 0x23];
/// assert_eq!(read_be::<u32>(&data), 0xABCDEF01);
/// assert_eq!(read_be::<i16>(&data[3..]), 0x0123);
/// ```
pub fn read_be<T: Readable>(buffer: &[u8]) -> T {
    T::from_u8_be(buffer)
}

/// write a number to stream as little-endian.
///
/// panics if buffer does not contain enough space.
///
/// ```
/// use byte_io::*;
///
/// let mut buf = [0u8;8];
/// write_le(&1u64, &mut buf);
/// assert_eq!(buf, [1,0,0,0,0,0,0,0]);
/// ```
pub fn write_le<T: Writeable>(v: &T, buffer: &mut [u8]) {
    T::to_u8_le(v, buffer);
}

/// read a number from stream as big-endian.
///
/// panics if buffer does not contain enough bytes.
///
/// ```
/// use byte_io::*;
/// let data = [0xAB, 0xCD, 0xEF, 0x01, 0x23];
/// assert_eq!(read_le::<u32>(&data), 0x01EFCDAB);
/// assert_eq!(read_le::<i16>(&data[3..]), 0x2301);
/// ```
pub fn read_le<T: Readable>(buffer: &[u8]) -> T {
    T::from_u8_le(buffer)
}

/// Any type implementing Readable can be decoded from binary.
pub trait Readable : marker::Sized {
    fn from_u8_be(&[u8]) -> Self;
    fn from_u8_le(&[u8]) -> Self;
}

/// Any type implementing Writeable can be encoded from binary.
pub trait Writeable : marker::Sized {
    fn to_u8_be(&Self, &mut [u8]);
    fn to_u8_le(&Self, &mut [u8]);
}


impl<T: Readable> Readable for Vec<T> {
    fn from_u8_be(input: &[u8]) -> Self {
        let t_size = size_of::<T>();
        let mut output = Vec::new();
        for i in 0..input.len() / t_size {
            output.push(T::from_u8_be(&input[i * t_size..(i + 1) * t_size]));
        }
        output
    }

    fn from_u8_le(input: &[u8]) -> Self {
        let t_size = size_of::<T>();
        let mut output = Vec::new();
        for i in 0..input.len() / t_size {
            output.push(T::from_u8_le(&input[i * t_size..(i + 1) * t_size]));
        }
        output
    }
}

impl<T: Writeable> Writeable for Vec<T> {
    fn to_u8_be(v: &Self, buf: &mut [u8]) {
        let t_size = size_of::<T>();
        for (i, v) in v.iter().enumerate() {
            T::to_u8_be(v, &mut buf[i * t_size..(i + 1) * t_size]);
        }
    }

    fn to_u8_le(v: &Self, buf: &mut [u8]) {
        let t_size = size_of::<T>();
        for (i, v) in v.iter().enumerate() {
            T::to_u8_le(v, &mut buf[i * t_size..(i + 1) * t_size]);
        }
    }
}

impl Writeable for i8 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = *v as u8;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[0] = *v as u8;
    }
}

impl Readable for i8 {
    fn from_u8_be(i: &[u8]) -> Self {
        i[0] as i8
    }

    fn from_u8_le(i: &[u8]) -> Self {
        i[0] as i8
    }
}

impl Readable for u8 {
    fn from_u8_be(a: &[u8]) -> Self {
        a[0]
    }

    fn from_u8_le(a: &[u8]) -> Self {
        a[0]
    }
}

impl Writeable for u8 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = *v;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[0] = *v;
    }
}

impl Readable for i16 {
    fn from_u8_be(i: &[u8]) -> Self {
        (i[0] as i16) << 8 | i[1] as i16
    }

    fn from_u8_le(i: &[u8]) -> Self {
        (i[1] as i16) << 8 | i[0] as i16
    }
}

impl Writeable for i16 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = (*v >> 8) as u8;
        a[1] = *v as u8;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[1] = (*v >> 8) as u8;
        a[0] = *v as u8;
    }
}

impl Readable for u16 {
    fn from_u8_be(i: &[u8]) -> Self {
        (i[0] as u16) << 8 | i[1] as u16
    }

    fn from_u8_le(i: &[u8]) -> Self {
        (i[1] as u16) << 8 | i[0] as u16
    }
}

impl Writeable for u16 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = (*v >> 8) as u8;
        a[1] = *v as u8;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[1] = (*v >> 8) as u8;
        a[0] = *v as u8;
    }
}

impl Readable for i32 {
    fn from_u8_be(i: &[u8]) -> Self {
        (i[0] as i32) << 24 | (i[1] as i32) << 16 | (i[2] as i32) << 8 | i[3] as i32
    }

    fn from_u8_le(i: &[u8]) -> Self {
        (i[3] as i32) << 24 | (i[2] as i32) << 16 | (i[1] as i32) << 8 | i[0] as i32
    }
}

impl Writeable for i32 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = (*v >> 24) as u8;
        a[1] = (*v >> 16) as u8;
        a[2] = (*v >> 8) as u8;
        a[3] = *v as u8;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[3] = (*v >> 24) as u8;
        a[2] = (*v >> 16) as u8;
        a[1] = (*v >> 8) as u8;
        a[0] = *v as u8;
    }
}

impl Readable for u32 {
    fn from_u8_be(i: &[u8]) -> Self {
        (i[0] as u32) << 24 | (i[1] as u32) << 16 | (i[2] as u32) << 8 | i[3] as u32
    }

    fn from_u8_le(i: &[u8]) -> Self {
        (i[3] as u32) << 24 | (i[2] as u32) << 16 | (i[1] as u32) << 8 | i[0] as u32
    }
}

impl Writeable for u32 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = (*v >> 24) as u8;
        a[1] = (*v >> 16) as u8;
        a[2] = (*v >> 8) as u8;
        a[3] = *v as u8;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[3] = (*v >> 24) as u8;
        a[2] = (*v >> 16) as u8;
        a[1] = (*v >> 8) as u8;
        a[0] = *v as u8;
    }
}

impl Readable for i64 {
    fn from_u8_be(i: &[u8]) -> Self {
        (i[0] as i64) << 56 | (i[1] as i64) << 48 | (i[2] as i64) << 40 | (i[3] as i64) << 32 |
        (i[4] as i64) << 24 | (i[5] as i64) << 16 |
        (i[6] as i64) << 8 | i[7] as i64
    }

    fn from_u8_le(i: &[u8]) -> Self {
        (i[7] as i64) << 56 | (i[6] as i64) << 48 | (i[5] as i64) << 40 | (i[4] as i64) << 32 |
        (i[3] as i64) << 24 | (i[2] as i64) << 16 |
        (i[1] as i64) << 8 | i[0] as i64
    }
}

impl Writeable for i64 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = (*v >> 56) as u8;
        a[1] = (*v >> 48) as u8;
        a[2] = (*v >> 40) as u8;
        a[3] = (*v >> 32) as u8;
        a[4] = (*v >> 24) as u8;
        a[5] = (*v >> 16) as u8;
        a[6] = (*v >> 8) as u8;
        a[7] = *v as u8;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[7] = (*v >> 56) as u8;
        a[6] = (*v >> 48) as u8;
        a[5] = (*v >> 40) as u8;
        a[4] = (*v >> 32) as u8;
        a[3] = (*v >> 24) as u8;
        a[2] = (*v >> 16) as u8;
        a[1] = (*v >> 8) as u8;
        a[0] = *v as u8;
    }
}

impl Readable for u64 {
    fn from_u8_be(i: &[u8]) -> Self {
        (i[0] as u64) << 56 | (i[1] as u64) << 48 | (i[2] as u64) << 40 | (i[3] as u64) << 32 |
        (i[4] as u64) << 24 | (i[5] as u64) << 16 |
        (i[6] as u64) << 8 | i[7] as u64
    }

    fn from_u8_le(i: &[u8]) -> Self {
        (i[7] as u64) << 56 | (i[6] as u64) << 48 | (i[5] as u64) << 40 | (i[4] as u64) << 32 |
        (i[3] as u64) << 24 | (i[2] as u64) << 16 |
        (i[1] as u64) << 8 | i[0] as u64
    }
}

impl Writeable for u64 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = (*v >> 56) as u8;
        a[1] = (*v >> 48) as u8;
        a[2] = (*v >> 40) as u8;
        a[3] = (*v >> 32) as u8;
        a[4] = (*v >> 24) as u8;
        a[5] = (*v >> 16) as u8;
        a[6] = (*v >> 8) as u8;
        a[7] = *v as u8;
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[7] = (*v >> 56) as u8;
        a[6] = (*v >> 48) as u8;
        a[5] = (*v >> 40) as u8;
        a[4] = (*v >> 32) as u8;
        a[3] = (*v >> 24) as u8;
        a[2] = (*v >> 16) as u8;
        a[1] = (*v >> 8) as u8;
        a[0] = *v as u8;
    }
}

impl Readable for bool {
    fn from_u8_be(i: &[u8]) -> Self {
        i[0] > 0
    }

    fn from_u8_le(i: &[u8]) -> Self {
        i[0] > 0
    }
}

impl Writeable for bool {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        a[0] = if *v {
            1u8
        } else {
            0u8
        };
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        a[0] = if *v {
            1u8
        } else {
            0u8
        };
    }
}

impl Readable for f32 {
    fn from_u8_be(i: &[u8]) -> Self {
        unsafe { transmute(u32::from_u8_be(i)) }
    }

    fn from_u8_le(i: &[u8]) -> Self {
        unsafe { transmute(u32::from_u8_le(i)) }
    }
}

impl Writeable for f32 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        unsafe { u32::to_u8_be(transmute(v), a) }
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        unsafe { u32::to_u8_le(transmute(v), a) }
    }
}

impl Readable for f64 {
    fn from_u8_be(i: &[u8]) -> Self {
        unsafe { transmute(u64::from_u8_be(i)) }
    }

    fn from_u8_le(i: &[u8]) -> Self {
        unsafe { transmute(u64::from_u8_le(i)) }
    }
}

impl Writeable for f64 {
    fn to_u8_be(v: &Self, a: &mut [u8]) {
        unsafe { u64::to_u8_be(transmute(v), a) }
    }

    fn to_u8_le(v: &Self, a: &mut [u8]) {
        unsafe { u64::to_u8_le(transmute(v), a) }
    }
}