rust-dnsbox/lib/dnsbox-base/src/common_types/name/label_offsets.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

2018-02-10 10:32:25 +00:00
use smallvec::SmallVec;
2020-03-07 15:57:47 +00:00
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
2018-02-10 10:32:25 +00:00
pub enum LabelOffset {
LabelStart(u8),
PacketStart(u16),
}
// the heap meta data is usually at least 2*usize big; assuming 64-bit
// platforms it should be ok to use 16 bytes in the smallvec.
2020-03-07 15:57:47 +00:00
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
2018-02-10 10:32:25 +00:00
pub enum LabelOffsets {
2020-03-07 15:57:47 +00:00
Uncompressed(SmallVec<[u8; 16]>),
Compressed(usize, SmallVec<[LabelOffset; 4]>),
2018-02-10 10:32:25 +00:00
}
impl LabelOffsets {
pub fn len(&self) -> u8 {
let l = match *self {
LabelOffsets::Uncompressed(ref offs) => offs.len(),
LabelOffsets::Compressed(_, ref offs) => offs.len(),
};
debug_assert!(l < 128);
l as u8
}
pub fn label_pos(&self, ndx: u8) -> usize {
debug_assert!(ndx < 127);
match *self {
LabelOffsets::Uncompressed(ref offs) => offs[ndx as usize] as usize,
LabelOffsets::Compressed(start, ref offs) => match offs[ndx as usize] {
LabelOffset::LabelStart(o) => start + (o as usize),
LabelOffset::PacketStart(o) => o as usize,
2020-03-07 15:57:47 +00:00
},
2018-02-10 10:32:25 +00:00
}
}
pub fn is_compressed(&self) -> bool {
match *self {
LabelOffsets::Uncompressed(_) => false,
LabelOffsets::Compressed(_, _) => true,
}
}
}