blob: 1c0afc0ef5faecf49ae3bc5de2c3fbb810d4dd34 (
plain)
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
|
use std::convert::TryInto;
use std::mem::size_of;
pub fn read_ne_u8(input: &mut &[u8]) -> u8 {
let (int_bytes, rest) = input.split_at(size_of::<u8>());
*input = rest;
u8::from_ne_bytes(int_bytes.try_into().unwrap())
}
pub fn read_ne_u32(input: &mut &[u8]) -> u32 {
let (int_bytes, rest) = input.split_at(size_of::<u32>());
*input = rest;
u32::from_ne_bytes(int_bytes.try_into().unwrap())
}
pub fn read_ne_usize(input: &mut &[u8]) -> usize {
let (int_bytes, rest) = input.split_at(size_of::<usize>());
*input = rest;
usize::from_ne_bytes(int_bytes.try_into().unwrap())
}
pub fn read_string(input: &mut &[u8], len: usize) -> Option<String> {
let (string_bytes, rest) = input.split_at(len);
*input = rest;
String::from_utf8(string_bytes.to_vec()).ok()
}
|