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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright (c) 2018 Rouven Spreckels <n3vu0r@qu1x.org>
//
// Usage of the works is permitted provided that
// this instrument is retained with the works, so that
// any entity that uses the works is notified of this instrument.
//
// DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.

//! **In Situ Endian-independent Bytes Access**
//!
//! # Contents
//!
//!   * [Usage](#usage)
//!
//! # Usage
//!
//! This crate is [on crates.io](https://crates.io/crates/in-situ) and can be
//! used by adding `in-situ` to the dependencies in your project's
//! `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! in-situ = "0.3"
//! ```
//!
//! and this to your crate root:
//!
//! ```
//! extern crate in_situ;
//! ```

#![deny(missing_docs)]

extern crate byteorder;
extern crate bytes;

use byteorder::{ByteOrder, NativeEndian, BE, LE};
use bytes::{Bytes, BytesMut};

use std::mem;
use std::fmt::Debug;
use std::hash::Hash;

/// Size of `u8`.
pub const U8: usize = 1;

/// Size of `u16`.
pub const U16: usize = 2;

/// Size of `u24`.
pub const U24: usize = 3;

/// Size of `u32`.
pub const U32: usize = 4;

/// Size of `u64`.
pub const U64: usize = 8;

/// Size of `u128`.
pub const U128: usize = 16;

/// Size of `i8`.
pub const I8: usize = 1;

/// Size of `i16`.
pub const I16: usize = 2;

/// Size of `i24`.
pub const I24: usize = 3;

/// Size of `i32`.
pub const I32: usize = 4;

/// Size of `i64`.
pub const I64: usize = 8;

/// Size of `i128`.
pub const I128: usize = 16;

/// Size of `f32`.
pub const F32: usize = 4;

/// Size of `f64`.
pub const F64: usize = 8;

/// Calculates padding of `align`ed bytes `offset` via two's complement
/// shortcuts instead of branching and modulo operations.
pub fn padding(offset: usize, align: usize) -> usize {
	let padding = -(offset as isize) as usize & align - 1;
	debug_assert_eq!(padding, (align - offset % align) % align);
	padding
}

/// Calculates `align`ed bytes `offset` via two's complement shortcuts instead
/// of branching and modulo operations.
pub fn aligned(offset: usize, align: usize) -> usize {
	let aligned = offset + align - 1 & -(align as isize) as usize;
	debug_assert_eq!(aligned, offset + padding(offset, align));
	aligned
}

/// Provides endian-independent immutable bytes access, thus requires methods
/// to be implemented detecting or hardcoding the word size and endianness. This
/// trait requires the `AsRef<[u8]>` trait to access slices of generic types. It
/// is not implemented for the `Raw` trait but instead for its wrapper types
/// since each wrapper might implement the endianness detection differently.
pub trait InSitu: AsRef<[u8]> {
	/// The word size of the slice, not to be confused with the various
	/// word sizes of how to access the slice.
	fn swap_size(&self) -> usize;

	/// Whether the underlying bytes are in big-endian (be) or little-endian
	/// (le) byte order.
	fn is_be(&self) -> bool;

	/// Inversion of `is_be()`.
	fn is_le(&self) -> bool {
		!self.is_be()
	}

	/// Tests if the underlying byte order has the machine's native endianness.
	fn is_native(&self) -> bool {
		self.is_be() == (NativeEndian::read_u16(&[0, 1]) == 1)
	}

	/// Convert `is_be()`/`is_le()` into `Order`.
	fn order(&self) -> Order {
		match self.is_be()
			{ true => Order::BE, false => Order::LE }
	}

	/// If `is_le()`, translates big-endian `offset` of word with `word_size` in
	/// slice of `swap_size()` into little-endian via bitwise instead of
	/// branching and modulo operations, otherwise passes through `offset`.
	fn at(&self, offset: usize, word_size: usize) -> usize {
		if self.is_be()
			{ offset } else { offset ^ self.swap_size() - word_size }
	}

	/// Gets `u8` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn u8(&self, offset: usize) -> u8 {
		let offset = self.at(offset, U8);
		self.as_ref()[offset]
	}

	/// Gets `u16` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn u16(&self, offset: usize) -> u16 {
		let offset = self.at(offset, U16);
		if self.is_be() {
			BE::read_u16(&self.as_ref()[offset..])
		} else {
			LE::read_u16(&self.as_ref()[offset..])
		}
	}

	/// Gets `u24` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn u24(&self, offset: usize) -> u32 {
		let offset = self.at(offset, U24);
		if self.is_be() {
			BE::read_u24(&self.as_ref()[offset..])
		} else {
			LE::read_u24(&self.as_ref()[offset..])
		}
	}

	/// Gets `u32` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn u32(&self, offset: usize) -> u32 {
		let offset = self.at(offset, U32);
		if self.is_be() {
			BE::read_u32(&self.as_ref()[offset..])
		} else {
			LE::read_u32(&self.as_ref()[offset..])
		}
	}

	/// Gets `u64` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn u64(&self, offset: usize) -> u64 {
		let offset = self.at(offset, U64);
		if self.is_be() {
			BE::read_u64(&self.as_ref()[offset..])
		} else {
			LE::read_u64(&self.as_ref()[offset..])
		}
	}

	/// Gets `u128` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn u128(&self, offset: usize) -> u128 {
		let offset = self.at(offset, U128);
		if self.is_be() {
			BE::read_u128(&self.as_ref()[offset..])
		} else {
			LE::read_u128(&self.as_ref()[offset..])
		}
	}

	/// Gets unsigned integer of `word_size <= 8` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn uint(&self, offset: usize, word_size: usize) -> u64 {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::read_uint(&self.as_ref()[offset..], word_size)
		} else {
			LE::read_uint(&self.as_ref()[offset..], word_size)
		}
	}

	/// Gets unsigned integer of `word_size <= 16` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn uint128(&self, offset: usize, word_size: usize) -> u128 {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::read_uint128(&self.as_ref()[offset..], word_size)
		} else {
			LE::read_uint128(&self.as_ref()[offset..], word_size)
		}
	}

	/// Gets `i8` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn i8(&self, offset: usize) -> i8 {
		let offset = self.at(offset, I8);
		self.as_ref()[offset] as i8
	}

	/// Gets `i16` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn i16(&self, offset: usize) -> i16 {
		let offset = self.at(offset, I16);
		if self.is_be() {
			BE::read_i16(&self.as_ref()[offset..])
		} else {
			LE::read_i16(&self.as_ref()[offset..])
		}
	}

	/// Gets `i24` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn i24(&self, offset: usize) -> i32 {
		let offset = self.at(offset, I24);
		if self.is_be() {
			BE::read_i24(&self.as_ref()[offset..])
		} else {
			LE::read_i24(&self.as_ref()[offset..])
		}
	}

	/// Gets `i32` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn i32(&self, offset: usize) -> i32 {
		let offset = self.at(offset, I32);
		if self.is_be() {
			BE::read_i32(&self.as_ref()[offset..])
		} else {
			LE::read_i32(&self.as_ref()[offset..])
		}
	}

	/// Gets `i64` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn i64(&self, offset: usize) -> i64 {
		let offset = self.at(offset, I64);
		if self.is_be() {
			BE::read_i64(&self.as_ref()[offset..])
		} else {
			LE::read_i64(&self.as_ref()[offset..])
		}
	}

	/// Gets `u128` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn i128(&self, offset: usize) -> i128 {
		let offset = self.at(offset, I128);
		if self.is_be() {
			BE::read_i128(&self.as_ref()[offset..])
		} else {
			LE::read_i128(&self.as_ref()[offset..])
		}
	}

	/// Gets signed integer of `word_size <= 8` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn int(&self, offset: usize, word_size: usize) -> i64 {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::read_int(&self.as_ref()[offset..], word_size)
		} else {
			LE::read_int(&self.as_ref()[offset..], word_size)
		}
	}

	/// Gets signed integer of `word_size <= 16` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn int128(&self, offset: usize, word_size: usize) -> i128 {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::read_int128(&self.as_ref()[offset..], word_size)
		} else {
			LE::read_int128(&self.as_ref()[offset..], word_size)
		}
	}

	/// Gets `f32` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn f32(&self, offset: usize) -> f32 {
		let offset = self.at(offset, F32);
		if self.is_be() {
			BE::read_f32(&self.as_ref()[offset..])
		} else {
			LE::read_f32(&self.as_ref()[offset..])
		}
	}

	/// Gets `f64` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn f64(&self, offset: usize) -> f64 {
		let offset = self.at(offset, F64);
		if self.is_be() {
			BE::read_f64(&self.as_ref()[offset..])
		} else {
			LE::read_f64(&self.as_ref()[offset..])
		}
	}
}

/// Provides endian-independent mutable bytes access, thus requires `InSitu`
/// trait to know about endianness. This trait is auto-implemented for all
/// `InSitu + AsMut<[u8]>` implementors.
pub trait InSituMut: InSitu + AsMut<[u8]> {
	/// Sets `u8` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_u8(&mut self, offset: usize, value: u8) {
		let at = self.at(offset, U8);
		self.as_mut()[at] = value;
	}

	/// Sets `u16` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_u16(&mut self, offset: usize, value: u16) {
		let offset = self.at(offset, U16);
		if self.is_be() {
			BE::write_u16(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_u16(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `u24` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_u24(&mut self, offset: usize, value: u32) {
		let offset = self.at(offset, U24);
		if self.is_be() {
			BE::write_u24(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_u24(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `u32` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_u32(&mut self, offset: usize, value: u32) {
		let offset = self.at(offset, U32);
		if self.is_be() {
			BE::write_u32(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_u32(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `u64` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_u64(&mut self, offset: usize, value: u64) {
		let offset = self.at(offset, U64);
		if self.is_be() {
			BE::write_u64(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_u64(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `u128` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_u128(&mut self, offset: usize, value: u128) {
		let offset = self.at(offset, U128);
		if self.is_be() {
			BE::write_u128(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_u128(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets unsigned integer of `word_size <= 8` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn set_uint(&mut self, offset: usize, value: u64, word_size: usize) {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::write_uint(&mut self.as_mut()[offset..], value, word_size)
		} else {
			LE::write_uint(&mut self.as_mut()[offset..], value, word_size)
		}
	}

	/// Sets unsigned integer of `word_size <= 16` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn set_uint128(&mut self, offset: usize, value: u128, word_size: usize) {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::write_uint128(&mut self.as_mut()[offset..], value, word_size)
		} else {
			LE::write_uint128(&mut self.as_mut()[offset..], value, word_size)
		}
	}

	/// Sets `i8` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_i8(&mut self, offset: usize, value: i8) {
		let at = self.at(offset, I8);
		self.as_mut()[at] = value as u8;
	}

	/// Sets `i16` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_i16(&mut self, offset: usize, value: i16) {
		let offset = self.at(offset, I16);
		if self.is_be() {
			BE::write_i16(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_i16(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `i24` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_i24(&mut self, offset: usize, value: i32) {
		let offset = self.at(offset, I24);
		if self.is_be() {
			BE::write_i24(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_i24(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `i32` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_i32(&mut self, offset: usize, value: i32) {
		let offset = self.at(offset, I32);
		if self.is_be() {
			BE::write_i32(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_i32(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `i64` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_i64(&mut self, offset: usize, value: i64) {
		let offset = self.at(offset, I64);
		if self.is_be() {
			BE::write_i64(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_i64(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `i128` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_i128(&mut self, offset: usize, value: i128) {
		let offset = self.at(offset, I128);
		if self.is_be() {
			BE::write_i128(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_i128(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets signed integer of `word_size <= 8` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn set_int(&mut self, offset: usize, value: i64, word_size: usize) {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::write_int(&mut self.as_mut()[offset..], value, word_size)
		} else {
			LE::write_int(&mut self.as_mut()[offset..], value, word_size)
		}
	}

	/// Sets signed integer of `word_size <= 16` in slice of `swap_size()` at
	/// big-endian `offset` endian-independently.
	fn set_int128(&mut self, offset: usize, value: i128, word_size: usize) {
		let offset = self.at(offset, word_size);
		if self.is_be() {
			BE::write_int128(&mut self.as_mut()[offset..], value, word_size)
		} else {
			LE::write_int128(&mut self.as_mut()[offset..], value, word_size)
		}
	}

	/// Sets `f32` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_f32(&mut self, offset: usize, value: f32) {
		let offset = self.at(offset, F32);
		if self.is_be() {
			BE::write_f32(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_f32(&mut self.as_mut()[offset..], value)
		}
	}

	/// Sets `f64` in slice of `swap_size()` at big-endian `offset`
	/// endian-independently.
	fn set_f64(&mut self, offset: usize, value: f64) {
		let offset = self.at(offset, F64);
		if self.is_be() {
			BE::write_f64(&mut self.as_mut()[offset..], value)
		} else {
			LE::write_f64(&mut self.as_mut()[offset..], value)
		}
	}
}

// Auto-implement `InSituMut` for `InSitu + AsMut<[u8]>` implementors.
impl<T: InSitu + AsMut<[u8]>> InSituMut for T {}

/// Abstracts immutable as well as mutable generic bytes view types like `[u8]`,
/// `mut [u8]`, `Bytes`, or `BytesMut` as immutable views and requires some
/// standard nice-to-have but easily-to-get traits, so the wrapper can just
/// derive them. Requires methods to be implemented to split views into
/// subviews.
pub trait Raw: AsRef<[u8]>
	+ Default + PartialEq + Eq + PartialOrd + Ord + Debug + Hash {
	/// Splits the bytes into two at the given index.
	///
	/// Afterwards `self` contains elements `[0, at)`, and the returned `Self`
	/// contains elements `[at, len)`.
	fn split_off(&mut self, at: usize) -> Self;

	/// Splits the bytes into two at the given index.
	///
	/// Afterwards `self` contains elements `[at, len)`, and the returned `Self`
	/// contains elements `[0, at)`.
	fn split_to(&mut self, at: usize) -> Self;
}

/// Abstracts mutable generic bytes view types like `mut [u8]` or `BytesMut` as
/// mutable view. This trait is auto-implemented for `Raw + AsMut<[u8]>`
/// implementors extending the immutable views with mutable ones.
pub trait RawMut: Raw + AsMut<[u8]> {}

// Auto-implement `RawMut` for `Raw + AsMut<[u8]>` implementors.
impl<T: Raw + AsMut<[u8]>> RawMut for T {}

impl<'a> Raw for &'a [u8] {
	fn split_off(&mut self, at: usize) -> Self {
		let (l, r) = self.split_at(at);
		*self = l;
		r
	}
	fn split_to(&mut self, at: usize) -> Self {
		let (l, r) = self.split_at(at);
		*self = r;
		l
	}
}

impl<'a> Raw for &'a mut [u8] {
	fn split_off(&mut self, at: usize) -> Self {
		let slice = mem::replace(self, &mut []);
		let (l, r) = slice.split_at_mut(at);
		*self = l;
		r
	}
	fn split_to(&mut self, at: usize) -> Self {
		let slice = mem::replace(self, &mut []);
		let (l, r) = slice.split_at_mut(at);
		*self = r;
		l
	}
}

impl Raw for Bytes {
	fn split_off(&mut self, at: usize) -> Self {
		self.split_off(at)
	}
	fn split_to(&mut self, at: usize) -> Self {
		self.split_to(at)
	}
}

impl Raw for BytesMut {
	fn split_off(&mut self, at: usize) -> Self {
		self.split_off(at)
	}
	fn split_to(&mut self, at: usize) -> Self {
		self.split_to(at)
	}
}

/// Type describing the underlying byte order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Order {
	/// Big-endian byte order.
	BE,
	/// Little-endian byte order.
	LE,
}

/// Type of `from` methods' argument specifying to take the bytes of either the
/// header only or the whole packet.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Take {
	/// Take bytes of header only.
	Header,
	/// Take bytes of whole packet.
	Packet,
}